blob: 39f3be2f37a29aedfbcbc3a941c59737b0465bec [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 *
Daniel Vetter169faec2015-07-09 23:44:27 +020092 * This function is protected against concurrent connector hotadds/removals
93 * using drm_fb_helper_add_one_connector() and
94 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +010095 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +000096int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100097{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000098 struct drm_device *dev = fb_helper->dev;
99 struct drm_connector *connector;
100 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000101
Daniel Vetter169faec2015-07-09 23:44:27 +0200102 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter6295d602015-07-09 23:44:25 +0200103 drm_for_each_connector(connector, dev) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000104 struct drm_fb_helper_connector *fb_helper_connector;
105
106 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
107 if (!fb_helper_connector)
108 goto fail;
109
110 fb_helper_connector->connector = connector;
111 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
112 }
Daniel Vetter169faec2015-07-09 23:44:27 +0200113 mutex_unlock(&dev->mode_config.mutex);
Dave Airlied50ba252009-09-23 14:44:08 +1000114 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000115fail:
116 for (i = 0; i < fb_helper->connector_count; i++) {
117 kfree(fb_helper->connector_info[i]);
118 fb_helper->connector_info[i] = NULL;
119 }
120 fb_helper->connector_count = 0;
Daniel Vetter169faec2015-07-09 23:44:27 +0200121 mutex_unlock(&dev->mode_config.mutex);
122
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000123 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +1000124}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000125EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000126
Dave Airlie65c2a892014-06-05 14:01:30 +1000127int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
128{
129 struct drm_fb_helper_connector **temp;
130 struct drm_fb_helper_connector *fb_helper_connector;
131
132 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
133 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
Damien Lespiau14f476f2014-08-08 19:15:20 +0100134 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 +1000135 if (!temp)
136 return -ENOMEM;
137
138 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
139 fb_helper->connector_info = temp;
140 }
141
142
143 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
144 if (!fb_helper_connector)
145 return -ENOMEM;
146
147 fb_helper_connector->connector = connector;
148 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
149 return 0;
150}
151EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
152
Rob Clark2148f182015-01-26 10:11:08 -0500153static void remove_from_modeset(struct drm_mode_set *set,
154 struct drm_connector *connector)
155{
156 int i, j;
157
158 for (i = 0; i < set->num_connectors; i++) {
159 if (set->connectors[i] == connector)
160 break;
161 }
162
163 if (i == set->num_connectors)
164 return;
165
166 for (j = i + 1; j < set->num_connectors; j++) {
167 set->connectors[j - 1] = set->connectors[j];
168 }
169 set->num_connectors--;
170
171 /* because i915 is pissy about this..
172 * TODO maybe need to makes sure we set it back to !=NULL somewhere?
173 */
174 if (set->num_connectors == 0)
175 set->fb = NULL;
176}
177
Dave Airlie65c2a892014-06-05 14:01:30 +1000178int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
179 struct drm_connector *connector)
180{
181 struct drm_fb_helper_connector *fb_helper_connector;
182 int i, j;
183
184 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
185
186 for (i = 0; i < fb_helper->connector_count; i++) {
187 if (fb_helper->connector_info[i]->connector == connector)
188 break;
189 }
190
191 if (i == fb_helper->connector_count)
192 return -EINVAL;
193 fb_helper_connector = fb_helper->connector_info[i];
194
195 for (j = i + 1; j < fb_helper->connector_count; j++) {
196 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
197 }
198 fb_helper->connector_count--;
199 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500200
201 /* also cleanup dangling references to the connector: */
202 for (i = 0; i < fb_helper->crtc_count; i++)
203 remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector);
204
Dave Airlie65c2a892014-06-05 14:01:30 +1000205 return 0;
206}
207EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
208
Jason Wessel99231022010-10-13 14:09:43 -0500209static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
210{
211 uint16_t *r_base, *g_base, *b_base;
212 int i;
213
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300214 if (helper->funcs->gamma_get == NULL)
215 return;
216
Jason Wessel99231022010-10-13 14:09:43 -0500217 r_base = crtc->gamma_store;
218 g_base = r_base + crtc->gamma_size;
219 b_base = g_base + crtc->gamma_size;
220
221 for (i = 0; i < crtc->gamma_size; i++)
222 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
223}
224
225static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
226{
227 uint16_t *r_base, *g_base, *b_base;
228
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200229 if (crtc->funcs->gamma_set == NULL)
230 return;
231
Jason Wessel99231022010-10-13 14:09:43 -0500232 r_base = crtc->gamma_store;
233 g_base = r_base + crtc->gamma_size;
234 b_base = g_base + crtc->gamma_size;
235
236 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
237}
238
Daniel Vetter207fd322013-01-20 22:13:14 +0100239/**
240 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
241 * @info: fbdev registered by the helper
242 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500243int drm_fb_helper_debug_enter(struct fb_info *info)
244{
245 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200246 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500247 int i;
248
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500249 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
250 for (i = 0; i < helper->crtc_count; i++) {
251 struct drm_mode_set *mode_set =
252 &helper->crtc_info[i].mode_set;
253
254 if (!mode_set->crtc->enabled)
255 continue;
256
257 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500258 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500259 funcs->mode_set_base_atomic(mode_set->crtc,
260 mode_set->fb,
261 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500262 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500263 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500264 }
265 }
266
267 return 0;
268}
269EXPORT_SYMBOL(drm_fb_helper_debug_enter);
270
271/* Find the real fb for a given fb helper CRTC */
272static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
273{
274 struct drm_device *dev = crtc->dev;
275 struct drm_crtc *c;
276
Daniel Vetter6295d602015-07-09 23:44:25 +0200277 drm_for_each_crtc(c, dev) {
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500278 if (crtc->base.id == c->base.id)
Matt Roperf4510a22014-04-01 15:22:40 -0700279 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500280 }
281
282 return NULL;
283}
284
Daniel Vetter207fd322013-01-20 22:13:14 +0100285/**
286 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
287 * @info: fbdev registered by the helper
288 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500289int drm_fb_helper_debug_leave(struct fb_info *info)
290{
291 struct drm_fb_helper *helper = info->par;
292 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200293 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500294 struct drm_framebuffer *fb;
295 int i;
296
297 for (i = 0; i < helper->crtc_count; i++) {
298 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
299 crtc = mode_set->crtc;
300 funcs = crtc->helper_private;
301 fb = drm_mode_config_fb(crtc);
302
303 if (!crtc->enabled)
304 continue;
305
306 if (!fb) {
307 DRM_ERROR("no fb to restore??\n");
308 continue;
309 }
310
Jason Wessel99231022010-10-13 14:09:43 -0500311 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500312 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500313 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500314 }
315
316 return 0;
317}
318EXPORT_SYMBOL(drm_fb_helper_debug_leave);
319
Rob Clark5ea1f752014-05-30 12:29:48 -0400320static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100321{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300322 struct drm_device *dev = fb_helper->dev;
323 struct drm_plane *plane;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100324 bool error = false;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300325 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100326
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300327 drm_warn_on_modeset_not_all_locked(dev);
328
Daniel Vetter6295d602015-07-09 23:44:25 +0200329 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700330 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
331 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100332
Sonika Jindal9783de22014-08-05 11:26:57 +0530333 if (dev->mode_config.rotation_property) {
Thomas Wood3a5f87c2014-08-20 14:45:00 +0100334 drm_mode_plane_set_obj_prop(plane,
335 dev->mode_config.rotation_property,
336 BIT(DRM_ROTATE_0));
Sonika Jindal9783de22014-08-05 11:26:57 +0530337 }
338 }
339
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100340 for (i = 0; i < fb_helper->crtc_count; i++) {
341 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300342 struct drm_crtc *crtc = mode_set->crtc;
343 int ret;
344
345 if (crtc->funcs->cursor_set) {
346 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
347 if (ret)
348 error = true;
349 }
350
Daniel Vetter2d13b672012-12-11 13:47:23 +0100351 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100352 if (ret)
353 error = true;
354 }
355 return error;
356}
Rob Clark5ea1f752014-05-30 12:29:48 -0400357/**
358 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
359 * @fb_helper: fbcon to restore
360 *
361 * This should be called from driver's drm ->lastclose callback
362 * when implementing an fbcon on top of kms using this helper. This ensures that
363 * the user isn't greeted with a black screen when e.g. X dies.
364 *
365 * Use this variant if you need to bypass locking (panic), or already
366 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
367 */
368static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
369{
370 return restore_fbdev_mode(fb_helper);
371}
372
373/**
374 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
375 * @fb_helper: fbcon to restore
376 *
377 * This should be called from driver's drm ->lastclose callback
378 * when implementing an fbcon on top of kms using this helper. This ensures that
379 * the user isn't greeted with a black screen when e.g. X dies.
380 */
381bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
382{
383 struct drm_device *dev = fb_helper->dev;
384 bool ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000385 bool do_delayed = false;
386
Rob Clark5ea1f752014-05-30 12:29:48 -0400387 drm_modeset_lock_all(dev);
388 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000389
390 do_delayed = fb_helper->delayed_hotplug;
391 if (do_delayed)
392 fb_helper->delayed_hotplug = false;
Rob Clark5ea1f752014-05-30 12:29:48 -0400393 drm_modeset_unlock_all(dev);
Dave Airliee2809c72014-11-26 13:15:24 +1000394
395 if (do_delayed)
396 drm_fb_helper_hotplug_event(fb_helper);
Rob Clark5ea1f752014-05-30 12:29:48 -0400397 return ret;
398}
399EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100400
Daniel Vetterd21bf462013-01-20 18:09:52 +0100401/*
402 * restore fbcon display for all kms driver's using this helper, used for sysrq
403 * and panic handling.
404 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530405static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000406{
Dave Airlie785b93e2009-08-28 15:46:53 +1000407 bool ret, error = false;
408 struct drm_fb_helper *helper;
409
410 if (list_empty(&kernel_fb_helper_list))
411 return false;
412
413 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200414 struct drm_device *dev = helper->dev;
415
416 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100417 continue;
418
Daniel Vettercb597bb2014-07-27 19:09:33 +0200419 /*
420 * NOTE: Use trylock mode to avoid deadlocks and sleeping in
421 * panic context.
Rob Clark51fd3712013-11-19 12:10:12 -0500422 */
Daniel Vettercb597bb2014-07-27 19:09:33 +0200423 if (__drm_modeset_lock_all(dev, true) != 0) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200424 error = true;
425 continue;
426 }
427
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100428 ret = drm_fb_helper_restore_fbdev_mode(helper);
429 if (ret)
430 error = true;
Thierry Redingb77f0762014-04-29 11:44:32 +0200431
Daniel Vettercb597bb2014-07-27 19:09:33 +0200432 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000433 }
434 return error;
435}
436
Daniel Vetter20c60c32012-12-17 12:13:23 +0100437static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
438{
439 struct drm_device *dev = fb_helper->dev;
440 struct drm_crtc *crtc;
441 int bound = 0, crtcs_bound = 0;
442
Paulo Zanoni520edd12013-11-27 18:24:08 -0200443 /* Sometimes user space wants everything disabled, so don't steal the
444 * display if there's a master. */
445 if (dev->primary->master)
446 return false;
447
Daniel Vetter6295d602015-07-09 23:44:25 +0200448 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700449 if (crtc->primary->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100450 crtcs_bound++;
Matt Roperf4510a22014-04-01 15:22:40 -0700451 if (crtc->primary->fb == fb_helper->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100452 bound++;
453 }
454
455 if (bound < crtcs_bound)
456 return false;
Paulo Zanoni520edd12013-11-27 18:24:08 -0200457
Daniel Vetter20c60c32012-12-17 12:13:23 +0100458 return true;
459}
460
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200461#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000462static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
463{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100464 bool ret;
465 ret = drm_fb_helper_force_kernel_mode();
466 if (ret == true)
467 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000468}
469static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
470
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700471static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000472{
473 schedule_work(&drm_fb_helper_restore_work);
474}
475
476static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
477 .handler = drm_fb_helper_sysrq,
478 .help_msg = "force-fb(V)",
479 .action_msg = "Restore framebuffer console",
480};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000481#else
482static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200483#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000484
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100485static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000486{
487 struct drm_fb_helper *fb_helper = info->par;
488 struct drm_device *dev = fb_helper->dev;
489 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700490 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700491 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000492
493 /*
Daniel Vetter1b1d5392013-01-24 16:42:07 +0100494 * fbdev->blank can be called from irq context in case of a panic.
495 * Since we already have our own special panic handler which will
496 * restore the fbdev console mode completely, just bail out early.
497 */
498 if (oops_in_progress)
499 return;
500
501 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100502 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000503 */
Daniel Vetter84849902012-12-02 00:28:11 +0100504 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100505 if (!drm_fb_helper_is_bound(fb_helper)) {
506 drm_modeset_unlock_all(dev);
507 return;
508 }
509
Jesse Barnese87b2c42009-09-17 18:14:41 -0700510 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000511 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000512
Dave Airlie8be48d92010-03-30 05:34:14 +0000513 if (!crtc->enabled)
514 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000515
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100516 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700517 for (j = 0; j < fb_helper->connector_count; j++) {
518 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200519 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500520 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100521 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700522 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000523 }
Daniel Vetter84849902012-12-02 00:28:11 +0100524 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000525}
526
Daniel Vetter207fd322013-01-20 22:13:14 +0100527/**
528 * drm_fb_helper_blank - implementation for ->fb_blank
529 * @blank: desired blanking state
530 * @info: fbdev registered by the helper
531 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000532int drm_fb_helper_blank(int blank, struct fb_info *info)
533{
534 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000535 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000536 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100537 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000538 break;
James Simmons731b5a12009-10-29 20:39:07 +0000539 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000540 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100541 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000542 break;
James Simmons731b5a12009-10-29 20:39:07 +0000543 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000544 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100545 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000546 break;
James Simmons731b5a12009-10-29 20:39:07 +0000547 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000548 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100549 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000550 break;
James Simmons731b5a12009-10-29 20:39:07 +0000551 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000552 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100553 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000554 break;
555 }
556 return 0;
557}
558EXPORT_SYMBOL(drm_fb_helper_blank);
559
560static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
561{
562 int i;
563
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000564 for (i = 0; i < helper->connector_count; i++)
565 kfree(helper->connector_info[i]);
566 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100567 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000568 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100569 if (helper->crtc_info[i].mode_set.mode)
570 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
571 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000572 kfree(helper->crtc_info);
573}
574
Daniel Vetter207fd322013-01-20 22:13:14 +0100575/**
Thierry Reding10a23102014-06-27 17:19:24 +0200576 * drm_fb_helper_prepare - setup a drm_fb_helper structure
577 * @dev: DRM device
578 * @helper: driver-allocated fbdev helper structure to set up
579 * @funcs: pointer to structure of functions associate with this helper
580 *
581 * Sets up the bare minimum to make the framebuffer helper usable. This is
582 * useful to implement race-free initialization of the polling helpers.
583 */
584void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
585 const struct drm_fb_helper_funcs *funcs)
586{
587 INIT_LIST_HEAD(&helper->kernel_fb_list);
588 helper->funcs = funcs;
589 helper->dev = dev;
590}
591EXPORT_SYMBOL(drm_fb_helper_prepare);
592
593/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100594 * drm_fb_helper_init - initialize a drm_fb_helper structure
595 * @dev: drm device
596 * @fb_helper: driver-allocated fbdev helper structure to initialize
597 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
598 * @max_conn_count: max connector count
599 *
600 * This allocates the structures for the fbdev helper with the given limits.
601 * Note that this won't yet touch the hardware (through the driver interfaces)
602 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
603 * to allow driver writes more control over the exact init sequence.
604 *
Thierry Reding10a23102014-06-27 17:19:24 +0200605 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100606 *
607 * RETURNS:
608 * Zero if everything went ok, nonzero otherwise.
609 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000610int drm_fb_helper_init(struct drm_device *dev,
611 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000612 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000613{
Dave Airlie785b93e2009-08-28 15:46:53 +1000614 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000615 int i;
616
Xiubo Li04cfe972014-03-10 09:33:58 +0800617 if (!max_conn_count)
618 return -EINVAL;
619
Dave Airlie4abe3522010-03-30 05:34:18 +0000620 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
621 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000622 return -ENOMEM;
623
Dave Airlie4abe3522010-03-30 05:34:18 +0000624 fb_helper->crtc_count = crtc_count;
625 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
626 if (!fb_helper->connector_info) {
627 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000628 return -ENOMEM;
629 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000630 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000631 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000632
633 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000634 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000635 kcalloc(max_conn_count,
636 sizeof(struct drm_connector *),
637 GFP_KERNEL);
638
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200639 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000640 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000641 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000642 }
643
644 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200645 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000646 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000647 i++;
648 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100649
Dave Airlie785b93e2009-08-28 15:46:53 +1000650 return 0;
651out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000652 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000653 return -ENOMEM;
654}
Dave Airlie4abe3522010-03-30 05:34:18 +0000655EXPORT_SYMBOL(drm_fb_helper_init);
656
Archit Tanejab8017d62015-07-22 14:57:56 +0530657/**
658 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
659 * @fb_helper: driver-allocated fbdev helper
660 *
661 * A helper to alloc fb_info and the members cmap and apertures. Called
662 * by the driver within the fb_probe fb_helper callback function.
663 *
664 * RETURNS:
665 * fb_info pointer if things went okay, pointer containing error code
666 * otherwise
667 */
668struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
669{
670 struct device *dev = fb_helper->dev->dev;
671 struct fb_info *info;
672 int ret;
673
674 info = framebuffer_alloc(0, dev);
675 if (!info)
676 return ERR_PTR(-ENOMEM);
677
678 ret = fb_alloc_cmap(&info->cmap, 256, 0);
679 if (ret)
680 goto err_release;
681
682 info->apertures = alloc_apertures(1);
683 if (!info->apertures) {
684 ret = -ENOMEM;
685 goto err_free_cmap;
686 }
687
688 fb_helper->fbdev = info;
689
690 return info;
691
692err_free_cmap:
693 fb_dealloc_cmap(&info->cmap);
694err_release:
695 framebuffer_release(info);
696 return ERR_PTR(ret);
697}
698EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
699
700/**
701 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
702 * @fb_helper: driver-allocated fbdev helper
703 *
704 * A wrapper around unregister_framebuffer, to release the fb_info
705 * framebuffer device
706 */
707void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
708{
709 if (fb_helper && fb_helper->fbdev)
710 unregister_framebuffer(fb_helper->fbdev);
711}
712EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
713
714/**
715 * drm_fb_helper_release_fbi - dealloc fb_info and its members
716 * @fb_helper: driver-allocated fbdev helper
717 *
718 * A helper to free memory taken by fb_info and the members cmap and
719 * apertures
720 */
721void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
722{
723 if (fb_helper) {
724 struct fb_info *info = fb_helper->fbdev;
725
726 if (info) {
727 if (info->cmap.len)
728 fb_dealloc_cmap(&info->cmap);
729 framebuffer_release(info);
730 }
731
732 fb_helper->fbdev = NULL;
733 }
734}
735EXPORT_SYMBOL(drm_fb_helper_release_fbi);
736
Dave Airlie4abe3522010-03-30 05:34:18 +0000737void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
738{
739 if (!list_empty(&fb_helper->kernel_fb_list)) {
740 list_del(&fb_helper->kernel_fb_list);
741 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000742 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
743 }
744 }
745
746 drm_fb_helper_crtc_free(fb_helper);
747
Dave Airlie4abe3522010-03-30 05:34:18 +0000748}
749EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000750
Archit Taneja47074ab2015-07-22 14:57:57 +0530751/**
752 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
753 * @fb_helper: driver-allocated fbdev helper
754 *
755 * A wrapper around unlink_framebuffer implemented by fbdev core
756 */
757void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
758{
759 if (fb_helper && fb_helper->fbdev)
760 unlink_framebuffer(fb_helper->fbdev);
761}
762EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
763
Archit Tanejacbb1a822015-07-31 16:21:41 +0530764/**
765 * drm_fb_helper_sys_read - wrapper around fb_sys_read
766 * @info: fb_info struct pointer
767 * @buf: userspace buffer to read from framebuffer memory
768 * @count: number of bytes to read from framebuffer memory
769 * @ppos: read offset within framebuffer memory
770 *
771 * A wrapper around fb_sys_read implemented by fbdev core
772 */
773ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
774 size_t count, loff_t *ppos)
775{
776 return fb_sys_read(info, buf, count, ppos);
777}
778EXPORT_SYMBOL(drm_fb_helper_sys_read);
779
780/**
781 * drm_fb_helper_sys_write - wrapper around fb_sys_write
782 * @info: fb_info struct pointer
783 * @buf: userspace buffer to write to framebuffer memory
784 * @count: number of bytes to write to framebuffer memory
785 * @ppos: write offset within framebuffer memory
786 *
787 * A wrapper around fb_sys_write implemented by fbdev core
788 */
789ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
790 size_t count, loff_t *ppos)
791{
792 return fb_sys_write(info, buf, count, ppos);
793}
794EXPORT_SYMBOL(drm_fb_helper_sys_write);
795
Dave Airliec850cb72009-10-23 18:49:03 +1000796static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000797 u16 blue, u16 regno, struct fb_info *info)
798{
799 struct drm_fb_helper *fb_helper = info->par;
800 struct drm_framebuffer *fb = fb_helper->fb;
801 int pindex;
802
Dave Airliec850cb72009-10-23 18:49:03 +1000803 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
804 u32 *palette;
805 u32 value;
806 /* place color in psuedopalette */
807 if (regno > 16)
808 return -EINVAL;
809 palette = (u32 *)info->pseudo_palette;
810 red >>= (16 - info->var.red.length);
811 green >>= (16 - info->var.green.length);
812 blue >>= (16 - info->var.blue.length);
813 value = (red << info->var.red.offset) |
814 (green << info->var.green.offset) |
815 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000816 if (info->var.transp.length > 0) {
817 u32 mask = (1 << info->var.transp.length) - 1;
818 mask <<= info->var.transp.offset;
819 value |= mask;
820 }
Dave Airliec850cb72009-10-23 18:49:03 +1000821 palette[regno] = value;
822 return 0;
823 }
824
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300825 /*
826 * The driver really shouldn't advertise pseudo/directcolor
827 * visuals if it can't deal with the palette.
828 */
829 if (WARN_ON(!fb_helper->funcs->gamma_set ||
830 !fb_helper->funcs->gamma_get))
831 return -EINVAL;
832
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000833 pindex = regno;
834
835 if (fb->bits_per_pixel == 16) {
836 pindex = regno << 3;
837
838 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000839 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000840 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000841 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000842
843 if (fb->depth == 16) {
844 u16 r, g, b;
845 int i;
846 if (regno < 32) {
847 for (i = 0; i < 8; i++)
848 fb_helper->funcs->gamma_set(crtc, red,
849 green, blue, pindex + i);
850 }
851
852 fb_helper->funcs->gamma_get(crtc, &r,
853 &g, &b,
854 pindex >> 1);
855
856 for (i = 0; i < 4; i++)
857 fb_helper->funcs->gamma_set(crtc, r,
858 green, b,
859 (pindex >> 1) + i);
860 }
861 }
862
863 if (fb->depth != 16)
864 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000865 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000866}
867
Daniel Vetter207fd322013-01-20 22:13:14 +0100868/**
869 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
870 * @cmap: cmap to set
871 * @info: fbdev registered by the helper
872 */
Dave Airlie068143d2009-10-05 09:58:02 +1000873int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
874{
875 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300876 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +0200877 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000878 u16 *red, *green, *blue, *transp;
879 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100880 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000881 int start;
882
Rui Wang9aa609e2014-12-15 11:28:26 -0800883 if (__drm_modeset_lock_all(dev, !!oops_in_progress)) {
884 return -EBUSY;
885 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300886 if (!drm_fb_helper_is_bound(fb_helper)) {
887 drm_modeset_unlock_all(dev);
888 return -EBUSY;
889 }
890
Dave Airlie8be48d92010-03-30 05:34:14 +0000891 for (i = 0; i < fb_helper->crtc_count; i++) {
892 crtc = fb_helper->crtc_info[i].mode_set.crtc;
893 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000894
895 red = cmap->red;
896 green = cmap->green;
897 blue = cmap->blue;
898 transp = cmap->transp;
899 start = cmap->start;
900
roel062ac622011-03-07 18:00:34 +0100901 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000902 u16 hred, hgreen, hblue, htransp = 0xffff;
903
904 hred = *red++;
905 hgreen = *green++;
906 hblue = *blue++;
907
908 if (transp)
909 htransp = *transp++;
910
Dave Airliec850cb72009-10-23 18:49:03 +1000911 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
912 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300913 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +1000914 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300915 if (crtc_funcs->load_lut)
916 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +1000917 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300918 out:
919 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +1000920 return rc;
921}
922EXPORT_SYMBOL(drm_fb_helper_setcmap);
923
Daniel Vetter207fd322013-01-20 22:13:14 +0100924/**
925 * drm_fb_helper_check_var - implementation for ->fb_check_var
926 * @var: screeninfo to check
927 * @info: fbdev registered by the helper
928 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000929int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
930 struct fb_info *info)
931{
932 struct drm_fb_helper *fb_helper = info->par;
933 struct drm_framebuffer *fb = fb_helper->fb;
934 int depth;
935
Jason Wesself90ebd92010-08-05 09:22:32 -0500936 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000937 return -EINVAL;
938
939 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100940 if (var->bits_per_pixel > fb->bits_per_pixel ||
941 var->xres > fb->width || var->yres > fb->height ||
942 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000943 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100944 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
945 var->xres, var->yres, var->bits_per_pixel,
946 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000947 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000948 return -EINVAL;
949 }
950
951 switch (var->bits_per_pixel) {
952 case 16:
953 depth = (var->green.length == 6) ? 16 : 15;
954 break;
955 case 32:
956 depth = (var->transp.length > 0) ? 32 : 24;
957 break;
958 default:
959 depth = var->bits_per_pixel;
960 break;
961 }
962
963 switch (depth) {
964 case 8:
965 var->red.offset = 0;
966 var->green.offset = 0;
967 var->blue.offset = 0;
968 var->red.length = 8;
969 var->green.length = 8;
970 var->blue.length = 8;
971 var->transp.length = 0;
972 var->transp.offset = 0;
973 break;
974 case 15:
975 var->red.offset = 10;
976 var->green.offset = 5;
977 var->blue.offset = 0;
978 var->red.length = 5;
979 var->green.length = 5;
980 var->blue.length = 5;
981 var->transp.length = 1;
982 var->transp.offset = 15;
983 break;
984 case 16:
985 var->red.offset = 11;
986 var->green.offset = 5;
987 var->blue.offset = 0;
988 var->red.length = 5;
989 var->green.length = 6;
990 var->blue.length = 5;
991 var->transp.length = 0;
992 var->transp.offset = 0;
993 break;
994 case 24:
995 var->red.offset = 16;
996 var->green.offset = 8;
997 var->blue.offset = 0;
998 var->red.length = 8;
999 var->green.length = 8;
1000 var->blue.length = 8;
1001 var->transp.length = 0;
1002 var->transp.offset = 0;
1003 break;
1004 case 32:
1005 var->red.offset = 16;
1006 var->green.offset = 8;
1007 var->blue.offset = 0;
1008 var->red.length = 8;
1009 var->green.length = 8;
1010 var->blue.length = 8;
1011 var->transp.length = 8;
1012 var->transp.offset = 24;
1013 break;
1014 default:
1015 return -EINVAL;
1016 }
1017 return 0;
1018}
1019EXPORT_SYMBOL(drm_fb_helper_check_var);
1020
Daniel Vetter207fd322013-01-20 22:13:14 +01001021/**
1022 * drm_fb_helper_set_par - implementation for ->fb_set_par
1023 * @info: fbdev registered by the helper
1024 *
1025 * This will let fbcon do the mode init and is called at initialization time by
1026 * the fbdev core when registering the driver, and later on through the hotplug
1027 * callback.
1028 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001029int drm_fb_helper_set_par(struct fb_info *info)
1030{
1031 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001032 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001033
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001034 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001035 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001036 return -EINVAL;
1037 }
1038
Rob Clark5ea1f752014-05-30 12:29:48 -04001039 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001040
Dave Airlie785b93e2009-08-28 15:46:53 +10001041 return 0;
1042}
1043EXPORT_SYMBOL(drm_fb_helper_set_par);
1044
Daniel Vetter207fd322013-01-20 22:13:14 +01001045/**
1046 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1047 * @var: updated screen information
1048 * @info: fbdev registered by the helper
1049 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001050int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1051 struct fb_info *info)
1052{
1053 struct drm_fb_helper *fb_helper = info->par;
1054 struct drm_device *dev = fb_helper->dev;
1055 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001056 int ret = 0;
1057 int i;
1058
Rui Wang9aa609e2014-12-15 11:28:26 -08001059 if (__drm_modeset_lock_all(dev, !!oops_in_progress)) {
1060 return -EBUSY;
1061 }
Daniel Vetter20c60c32012-12-17 12:13:23 +01001062 if (!drm_fb_helper_is_bound(fb_helper)) {
1063 drm_modeset_unlock_all(dev);
1064 return -EBUSY;
1065 }
1066
Dave Airlie8be48d92010-03-30 05:34:14 +00001067 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001068 modeset = &fb_helper->crtc_info[i].mode_set;
1069
1070 modeset->x = var->xoffset;
1071 modeset->y = var->yoffset;
1072
1073 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001074 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001075 if (!ret) {
1076 info->var.xoffset = var->xoffset;
1077 info->var.yoffset = var->yoffset;
1078 }
1079 }
1080 }
Daniel Vetter84849902012-12-02 00:28:11 +01001081 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +10001082 return ret;
1083}
1084EXPORT_SYMBOL(drm_fb_helper_pan_display);
1085
Daniel Vetter8acf6582013-01-21 23:38:37 +01001086/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001087 * Allocates the backing storage and sets up the fbdev info structure through
1088 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1089 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001090 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001091static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1092 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001093{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001094 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001095 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001096 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +10001097 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +00001098 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001099 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001100
1101 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1102 sizes.surface_depth = 24;
1103 sizes.surface_bpp = 32;
1104 sizes.fb_width = (unsigned)-1;
1105 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001106
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001107 /* if driver picks 8 or 16 by default use that
1108 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001109 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001110 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001111
Dave Airlie785b93e2009-08-28 15:46:53 +10001112 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001113 for (i = 0; i < fb_helper->connector_count; i++) {
1114 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001115 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001116
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001117 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001118
1119 if (cmdline_mode->bpp_specified) {
1120 switch (cmdline_mode->bpp) {
1121 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001122 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001123 break;
1124 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001125 sizes.surface_depth = 15;
1126 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001127 break;
1128 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001129 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001130 break;
1131 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001132 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001133 break;
1134 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001135 sizes.surface_depth = 24;
1136 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001137 break;
1138 }
1139 break;
1140 }
1141 }
1142
Dave Airlie8be48d92010-03-30 05:34:14 +00001143 crtc_count = 0;
1144 for (i = 0; i < fb_helper->crtc_count; i++) {
1145 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001146 struct drm_mode_set *mode_set;
1147 int x, y, j;
1148 /* in case of tile group, are we the last tile vert or horiz?
1149 * If no tile group you are always the last one both vertically
1150 * and horizontally
1151 */
1152 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001153
Dave Airlie8be48d92010-03-30 05:34:14 +00001154 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001155 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001156
1157 if (!desired_mode)
1158 continue;
1159
1160 crtc_count++;
1161
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001162 x = fb_helper->crtc_info[i].x;
1163 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001164
1165 if (gamma_size == 0)
1166 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1167
1168 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1169 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001170
1171 for (j = 0; j < mode_set->num_connectors; j++) {
1172 struct drm_connector *connector = mode_set->connectors[j];
1173 if (connector->has_tile) {
1174 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1175 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1176 /* cloning to multiple tiles is just crazy-talk, so: */
1177 break;
1178 }
1179 }
1180
1181 if (lasth)
1182 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1183 if (lastv)
1184 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001185 }
1186
Dave Airlie38651672010-03-30 05:34:13 +00001187 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001188 /* hmm everyone went away - assume VGA cable just fell out
1189 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001190 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001191 sizes.fb_width = sizes.surface_width = 1024;
1192 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001193 }
1194
Dave Airlie38651672010-03-30 05:34:13 +00001195 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001196 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1197 if (ret < 0)
1198 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001199
Dave Airlie38651672010-03-30 05:34:13 +00001200 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001201
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001202 /*
1203 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1204 * events, but at init time drm_setup_crtcs needs to be called before
1205 * the fb is allocated (since we need to figure out the desired size of
1206 * the fb before we can allocate it ...). Hence we need to fix things up
1207 * here again.
1208 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001209 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001210 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1211 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1212
Dave Airlie785b93e2009-08-28 15:46:53 +10001213
Daniel Vetter8acf6582013-01-21 23:38:37 +01001214 info->var.pixclock = 0;
1215 if (register_framebuffer(info) < 0)
1216 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001217
Daniel Vetter8acf6582013-01-21 23:38:37 +01001218 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1219 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001220
Dave Airlie785b93e2009-08-28 15:46:53 +10001221 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001222 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1223 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001224
1225 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001226
Dave Airlie785b93e2009-08-28 15:46:53 +10001227 return 0;
1228}
Dave Airlie785b93e2009-08-28 15:46:53 +10001229
Daniel Vetter207fd322013-01-20 22:13:14 +01001230/**
1231 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1232 * @info: fbdev registered by the helper
1233 * @pitch: desired pitch
1234 * @depth: desired depth
1235 *
1236 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1237 * fbdev emulations. Drivers which support acceleration methods which impose
1238 * additional constraints need to set up their own limits.
1239 *
1240 * Drivers should call this (or their equivalent setup code) from their
1241 * ->fb_probe callback.
1242 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001243void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1244 uint32_t depth)
1245{
1246 info->fix.type = FB_TYPE_PACKED_PIXELS;
1247 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1248 FB_VISUAL_TRUECOLOR;
1249 info->fix.mmio_start = 0;
1250 info->fix.mmio_len = 0;
1251 info->fix.type_aux = 0;
1252 info->fix.xpanstep = 1; /* doing it in hw */
1253 info->fix.ypanstep = 1; /* doing it in hw */
1254 info->fix.ywrapstep = 0;
1255 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001256
1257 info->fix.line_length = pitch;
1258 return;
1259}
1260EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1261
Daniel Vetter207fd322013-01-20 22:13:14 +01001262/**
1263 * drm_fb_helper_fill_var - initalizes variable fbdev information
1264 * @info: fbdev instance to set up
1265 * @fb_helper: fb helper instance to use as template
1266 * @fb_width: desired fb width
1267 * @fb_height: desired fb height
1268 *
1269 * Sets up the variable fbdev metainformation from the given fb helper instance
1270 * and the drm framebuffer allocated in fb_helper->fb.
1271 *
1272 * Drivers should call this (or their equivalent setup code) from their
1273 * ->fb_probe callback after having allocated the fbdev backing
1274 * storage framebuffer.
1275 */
Dave Airlie38651672010-03-30 05:34:13 +00001276void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001277 uint32_t fb_width, uint32_t fb_height)
1278{
Dave Airlie38651672010-03-30 05:34:13 +00001279 struct drm_framebuffer *fb = fb_helper->fb;
1280 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001281 info->var.xres_virtual = fb->width;
1282 info->var.yres_virtual = fb->height;
1283 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001284 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001285 info->var.xoffset = 0;
1286 info->var.yoffset = 0;
1287 info->var.activate = FB_ACTIVATE_NOW;
1288 info->var.height = -1;
1289 info->var.width = -1;
1290
1291 switch (fb->depth) {
1292 case 8:
1293 info->var.red.offset = 0;
1294 info->var.green.offset = 0;
1295 info->var.blue.offset = 0;
1296 info->var.red.length = 8; /* 8bit DAC */
1297 info->var.green.length = 8;
1298 info->var.blue.length = 8;
1299 info->var.transp.offset = 0;
1300 info->var.transp.length = 0;
1301 break;
1302 case 15:
1303 info->var.red.offset = 10;
1304 info->var.green.offset = 5;
1305 info->var.blue.offset = 0;
1306 info->var.red.length = 5;
1307 info->var.green.length = 5;
1308 info->var.blue.length = 5;
1309 info->var.transp.offset = 15;
1310 info->var.transp.length = 1;
1311 break;
1312 case 16:
1313 info->var.red.offset = 11;
1314 info->var.green.offset = 5;
1315 info->var.blue.offset = 0;
1316 info->var.red.length = 5;
1317 info->var.green.length = 6;
1318 info->var.blue.length = 5;
1319 info->var.transp.offset = 0;
1320 break;
1321 case 24:
1322 info->var.red.offset = 16;
1323 info->var.green.offset = 8;
1324 info->var.blue.offset = 0;
1325 info->var.red.length = 8;
1326 info->var.green.length = 8;
1327 info->var.blue.length = 8;
1328 info->var.transp.offset = 0;
1329 info->var.transp.length = 0;
1330 break;
1331 case 32:
1332 info->var.red.offset = 16;
1333 info->var.green.offset = 8;
1334 info->var.blue.offset = 0;
1335 info->var.red.length = 8;
1336 info->var.green.length = 8;
1337 info->var.blue.length = 8;
1338 info->var.transp.offset = 24;
1339 info->var.transp.length = 8;
1340 break;
1341 default:
1342 break;
1343 }
1344
1345 info->var.xres = fb_width;
1346 info->var.yres = fb_height;
1347}
1348EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001349
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001350static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1351 uint32_t maxX,
1352 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001353{
1354 struct drm_connector *connector;
1355 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001356 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001357
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001358 for (i = 0; i < fb_helper->connector_count; i++) {
1359 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001360 count += connector->funcs->fill_modes(connector, maxX, maxY);
1361 }
1362
1363 return count;
1364}
1365
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001366struct 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 +00001367{
1368 struct drm_display_mode *mode;
1369
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001370 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001371 if (mode->hdisplay > width ||
1372 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001373 continue;
1374 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1375 return mode;
1376 }
1377 return NULL;
1378}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001379EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001380
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001381static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001382{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001383 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001384}
1385
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001386struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001387 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001388{
Chris Wilson1794d252011-04-17 07:43:32 +01001389 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001390 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001391 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001392
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001393 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001394 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001395 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001396
1397 /* attempt to find a matching mode in the list of modes
1398 * we have gotten so far, if not add a CVT mode that conforms
1399 */
1400 if (cmdline_mode->rb || cmdline_mode->margins)
1401 goto create_mode;
1402
Takashi Iwaic683f422014-03-19 14:53:13 +01001403 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001404again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001405 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001406 /* check width/height */
1407 if (mode->hdisplay != cmdline_mode->xres ||
1408 mode->vdisplay != cmdline_mode->yres)
1409 continue;
1410
1411 if (cmdline_mode->refresh_specified) {
1412 if (mode->vrefresh != cmdline_mode->refresh)
1413 continue;
1414 }
1415
1416 if (cmdline_mode->interlace) {
1417 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1418 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001419 } else if (prefer_non_interlace) {
1420 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1421 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001422 }
1423 return mode;
1424 }
1425
Takashi Iwaic683f422014-03-19 14:53:13 +01001426 if (prefer_non_interlace) {
1427 prefer_non_interlace = false;
1428 goto again;
1429 }
1430
Dave Airlie38651672010-03-30 05:34:13 +00001431create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001432 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1433 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001434 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001435 return mode;
1436}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001437EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001438
1439static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1440{
1441 bool enable;
1442
Sachin Kamat96081cd2012-11-15 03:43:30 +00001443 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001444 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001445 else
Dave Airlie38651672010-03-30 05:34:13 +00001446 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001447
Dave Airlie38651672010-03-30 05:34:13 +00001448 return enable;
1449}
1450
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001451static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1452 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001453{
1454 bool any_enabled = false;
1455 struct drm_connector *connector;
1456 int i = 0;
1457
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001458 for (i = 0; i < fb_helper->connector_count; i++) {
1459 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001460 enabled[i] = drm_connector_enabled(connector, true);
1461 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1462 enabled[i] ? "yes" : "no");
1463 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001464 }
1465
1466 if (any_enabled)
1467 return;
1468
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001469 for (i = 0; i < fb_helper->connector_count; i++) {
1470 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001471 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001472 }
1473}
1474
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001475static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1476 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001477 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001478 bool *enabled, int width, int height)
1479{
1480 int count, i, j;
1481 bool can_clone = false;
1482 struct drm_fb_helper_connector *fb_helper_conn;
1483 struct drm_display_mode *dmt_mode, *mode;
1484
1485 /* only contemplate cloning in the single crtc case */
1486 if (fb_helper->crtc_count > 1)
1487 return false;
1488
1489 count = 0;
1490 for (i = 0; i < fb_helper->connector_count; i++) {
1491 if (enabled[i])
1492 count++;
1493 }
1494
1495 /* only contemplate cloning if more than one connector is enabled */
1496 if (count <= 1)
1497 return false;
1498
1499 /* check the command line or if nothing common pick 1024x768 */
1500 can_clone = true;
1501 for (i = 0; i < fb_helper->connector_count; i++) {
1502 if (!enabled[i])
1503 continue;
1504 fb_helper_conn = fb_helper->connector_info[i];
1505 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1506 if (!modes[i]) {
1507 can_clone = false;
1508 break;
1509 }
1510 for (j = 0; j < i; j++) {
1511 if (!enabled[j])
1512 continue;
1513 if (!drm_mode_equal(modes[j], modes[i]))
1514 can_clone = false;
1515 }
1516 }
1517
1518 if (can_clone) {
1519 DRM_DEBUG_KMS("can clone using command line\n");
1520 return true;
1521 }
1522
1523 /* try and find a 1024x768 mode on each connector */
1524 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001525 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001526
1527 for (i = 0; i < fb_helper->connector_count; i++) {
1528
1529 if (!enabled[i])
1530 continue;
1531
1532 fb_helper_conn = fb_helper->connector_info[i];
1533 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1534 if (drm_mode_equal(mode, dmt_mode))
1535 modes[i] = mode;
1536 }
1537 if (!modes[i])
1538 can_clone = false;
1539 }
1540
1541 if (can_clone) {
1542 DRM_DEBUG_KMS("can clone using 1024x768\n");
1543 return true;
1544 }
1545 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1546 return false;
1547}
1548
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001549static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1550 struct drm_display_mode **modes,
1551 struct drm_fb_offset *offsets,
1552 int idx,
1553 int h_idx, int v_idx)
1554{
1555 struct drm_fb_helper_connector *fb_helper_conn;
1556 int i;
1557 int hoffset = 0, voffset = 0;
1558
1559 for (i = 0; i < fb_helper->connector_count; i++) {
1560 fb_helper_conn = fb_helper->connector_info[i];
1561 if (!fb_helper_conn->connector->has_tile)
1562 continue;
1563
1564 if (!modes[i] && (h_idx || v_idx)) {
1565 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1566 fb_helper_conn->connector->base.id);
1567 continue;
1568 }
1569 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1570 hoffset += modes[i]->hdisplay;
1571
1572 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1573 voffset += modes[i]->vdisplay;
1574 }
1575 offsets[idx].x = hoffset;
1576 offsets[idx].y = voffset;
1577 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1578 return 0;
1579}
1580
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001581static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001582 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001583 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00001584 bool *enabled, int width, int height)
1585{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001586 struct drm_fb_helper_connector *fb_helper_conn;
1587 int i;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001588 uint64_t conn_configured = 0, mask;
1589 int tile_pass = 0;
1590 mask = (1 << fb_helper->connector_count) - 1;
1591retry:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001592 for (i = 0; i < fb_helper->connector_count; i++) {
1593 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001594
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001595 if (conn_configured & (1 << i))
Dave Airlie38651672010-03-30 05:34:13 +00001596 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001597
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001598 if (enabled[i] == false) {
1599 conn_configured |= (1 << i);
1600 continue;
1601 }
1602
1603 /* first pass over all the untiled connectors */
1604 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1605 continue;
1606
1607 if (tile_pass == 1) {
1608 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1609 fb_helper_conn->connector->tile_v_loc != 0)
1610 continue;
1611
1612 } else {
1613 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1614 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1615 /* if this tile_pass doesn't cover any of the tiles - keep going */
1616 continue;
1617
1618 /* find the tile offsets for this pass - need
1619 to find all tiles left and above */
1620 drm_get_tile_offsets(fb_helper, modes, offsets,
1621 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1622 }
Dave Airlie38651672010-03-30 05:34:13 +00001623 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001624 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001625
1626 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001627 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001628 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001629 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1630 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001631 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001632 }
1633 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001634 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1635 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001636 break;
1637 }
1638 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1639 "none");
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001640 conn_configured |= (1 << i);
1641 }
1642
1643 if ((conn_configured & mask) != mask) {
1644 tile_pass++;
1645 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00001646 }
1647 return true;
1648}
1649
Dave Airlie8be48d92010-03-30 05:34:14 +00001650static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1651 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001652 struct drm_display_mode **modes,
1653 int n, int width, int height)
1654{
1655 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001656 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001657 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02001658 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00001659 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001660 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001661 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001662 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001663
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001664 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001665 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001666
1667 fb_helper_conn = fb_helper->connector_info[n];
1668 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001669
1670 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001671 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001672 if (modes[n] == NULL)
1673 return best_score;
1674
Dave Airlie8be48d92010-03-30 05:34:14 +00001675 crtcs = kzalloc(dev->mode_config.num_connector *
1676 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001677 if (!crtcs)
1678 return best_score;
1679
1680 my_score = 1;
1681 if (connector->status == connector_status_connected)
1682 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001683 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001684 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001685 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001686 my_score++;
1687
1688 connector_funcs = connector->helper_private;
1689 encoder = connector_funcs->best_encoder(connector);
1690 if (!encoder)
1691 goto out;
1692
Dave Airlie38651672010-03-30 05:34:13 +00001693 /* select a crtc for this connector and then attempt to configure
1694 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001695 for (c = 0; c < fb_helper->crtc_count; c++) {
1696 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001697
Sachin Kamat96081cd2012-11-15 03:43:30 +00001698 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001699 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001700
1701 for (o = 0; o < n; o++)
1702 if (best_crtcs[o] == crtc)
1703 break;
1704
1705 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001706 /* ignore cloning unless only a single crtc */
1707 if (fb_helper->crtc_count > 1)
1708 continue;
1709
1710 if (!drm_mode_equal(modes[o], modes[n]))
1711 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001712 }
1713
1714 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001715 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1716 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001717 width, height);
1718 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001719 best_score = score;
1720 memcpy(best_crtcs, crtcs,
1721 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001722 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001723 }
Dave Airlie38651672010-03-30 05:34:13 +00001724 }
1725out:
1726 kfree(crtcs);
1727 return best_score;
1728}
1729
Dave Airlie8be48d92010-03-30 05:34:14 +00001730static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001731{
Dave Airlie8be48d92010-03-30 05:34:14 +00001732 struct drm_device *dev = fb_helper->dev;
1733 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001734 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001735 struct drm_fb_offset *offsets;
Dave Airlie8be48d92010-03-30 05:34:14 +00001736 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001737 bool *enabled;
1738 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001739 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001740
1741 DRM_DEBUG_KMS("\n");
1742
1743 width = dev->mode_config.max_width;
1744 height = dev->mode_config.max_height;
1745
Dave Airlie38651672010-03-30 05:34:13 +00001746 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001747 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001748 modes = kcalloc(dev->mode_config.num_connector,
1749 sizeof(struct drm_display_mode *), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001750 offsets = kcalloc(dev->mode_config.num_connector,
1751 sizeof(struct drm_fb_offset), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001752 enabled = kcalloc(dev->mode_config.num_connector,
1753 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001754 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001755 DRM_ERROR("Memory allocation failed\n");
1756 goto out;
1757 }
1758
Dave Airlie38651672010-03-30 05:34:13 +00001759
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001760 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001761
Jesse Barnes11e17a02013-02-19 13:31:39 -08001762 if (!(fb_helper->funcs->initial_config &&
1763 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001764 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08001765 enabled, width, height))) {
1766 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1767 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001768 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08001769
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001770 if (!drm_target_cloned(fb_helper, modes, offsets,
1771 enabled, width, height) &&
1772 !drm_target_preferred(fb_helper, modes, offsets,
1773 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001774 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001775
1776 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1777 width, height);
1778
1779 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001780 }
Dave Airlie38651672010-03-30 05:34:13 +00001781
Dave Airlie8be48d92010-03-30 05:34:14 +00001782 /* need to set the modesets up here for use later */
1783 /* fill out the connector<->crtc mappings into the modesets */
1784 for (i = 0; i < fb_helper->crtc_count; i++) {
1785 modeset = &fb_helper->crtc_info[i].mode_set;
1786 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001787 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001788 }
Dave Airlie38651672010-03-30 05:34:13 +00001789
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001790 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001791 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001792 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001793 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001794 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001795
Dave Airlie8be48d92010-03-30 05:34:14 +00001796 if (mode && fb_crtc) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001797 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1798 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Dave Airlie8be48d92010-03-30 05:34:14 +00001799 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001800 fb_crtc->x = offset->x;
1801 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00001802 if (modeset->mode)
1803 drm_mode_destroy(dev, modeset->mode);
1804 modeset->mode = drm_mode_duplicate(dev,
1805 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001806 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001807 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001808 modeset->x = offset->x;
1809 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00001810 }
Dave Airlie38651672010-03-30 05:34:13 +00001811 }
1812
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001813 /* Clear out any old modes if there are no more connected outputs. */
1814 for (i = 0; i < fb_helper->crtc_count; i++) {
1815 modeset = &fb_helper->crtc_info[i].mode_set;
1816 if (modeset->num_connectors == 0) {
1817 BUG_ON(modeset->fb);
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001818 if (modeset->mode)
1819 drm_mode_destroy(dev, modeset->mode);
1820 modeset->mode = NULL;
1821 }
1822 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001823out:
Dave Airlie38651672010-03-30 05:34:13 +00001824 kfree(crtcs);
1825 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001826 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00001827 kfree(enabled);
1828}
1829
1830/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001831 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001832 * @fb_helper: fb_helper device struct
1833 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001834 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001835 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001836 * At the moment, this is a cloned configuration across all heads with
1837 * a new framebuffer object as the backing store.
1838 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001839 * Note that this also registers the fbdev and so allows userspace to call into
1840 * the driver through the fbdev interfaces.
1841 *
1842 * This function will call down into the ->fb_probe callback to let
1843 * the driver allocate and initialize the fbdev info structure and the drm
1844 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1845 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1846 * values for the fbdev info structure.
1847 *
Dave Airlie38651672010-03-30 05:34:13 +00001848 * RETURNS:
1849 * Zero if everything went ok, nonzero otherwise.
1850 */
Thierry Reding01934c22014-12-19 11:21:32 +01001851int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001852{
Dave Airlie8be48d92010-03-30 05:34:14 +00001853 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001854 int count = 0;
1855
Daniel Vetter53f19042014-03-20 14:26:35 +01001856 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001857 count = drm_fb_helper_probe_connector_modes(fb_helper,
1858 dev->mode_config.max_width,
1859 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001860 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001861 /*
1862 * we shouldn't end up with no modes here.
1863 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001864 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001865 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001866
Dave Airlie8be48d92010-03-30 05:34:14 +00001867 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001868
Dave Airlie4abe3522010-03-30 05:34:18 +00001869 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001870}
Dave Airlie8be48d92010-03-30 05:34:14 +00001871EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001872
Chris Wilson73943712011-04-22 11:03:57 +01001873/**
1874 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001875 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001876 * @fb_helper: the drm_fb_helper
1877 *
Chris Wilson73943712011-04-22 11:03:57 +01001878 * Scan the connectors attached to the fb_helper and try to put together a
1879 * setup after *notification of a change in output configuration.
1880 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001881 * Called at runtime, takes the mode config locks to be able to check/change the
1882 * modeset configuration. Must be run from process context (which usually means
1883 * either the output polling work or a work item launched from the driver's
1884 * hotplug interrupt).
1885 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001886 * Note that drivers may call this even before calling
1887 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1888 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1889 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001890 *
Chris Wilson73943712011-04-22 11:03:57 +01001891 * RETURNS:
1892 * 0 on success and a non-zero error code otherwise.
1893 */
1894int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001895{
Chris Wilson73943712011-04-22 11:03:57 +01001896 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001897 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001898
Daniel Vetter89ced122013-04-11 14:26:55 +00001899 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001900 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001901 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00001902 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01001903 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001904 }
Dave Airlie38651672010-03-30 05:34:13 +00001905 DRM_DEBUG_KMS("\n");
1906
Dave Airlie4abe3522010-03-30 05:34:18 +00001907 max_width = fb_helper->fb->width;
1908 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001909
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001910 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00001911 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1912
1913 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001914 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001915 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01001916 drm_fb_helper_set_par(fb_helper->fbdev);
1917
1918 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00001919}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001920EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001921
David Rientjes6a108a12011-01-20 14:44:16 -08001922/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001923 * but the module doesn't depend on any fb console symbols. At least
1924 * attempt to load fbcon to avoid leaving the system without a usable console.
1925 */
David Rientjes6a108a12011-01-20 14:44:16 -08001926#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001927static int __init drm_fb_helper_modinit(void)
1928{
1929 const char *name = "fbcon";
1930 struct module *fbcon;
1931
1932 mutex_lock(&module_mutex);
1933 fbcon = find_module(name);
1934 mutex_unlock(&module_mutex);
1935
1936 if (!fbcon)
1937 request_module_nowait(name);
1938 return 0;
1939}
1940
1941module_init(drm_fb_helper_modinit);
1942#endif