blob: 7278c23ea98b9918029b7f0080bb882f5bdb0ab4 [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
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +020059 * drm_fb_helper_restore_fbdev_mode_unlocked() from their ->lastclose callback.
60 * They should also notify the fb helper code from updates to the output
Daniel Vetter207fd322013-01-20 22:13:14 +010061 * 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/**
359 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
360 * @fb_helper: fbcon to restore
361 *
362 * This should be called from driver's drm ->lastclose callback
363 * when implementing an fbcon on top of kms using this helper. This ensures that
364 * the user isn't greeted with a black screen when e.g. X dies.
365 */
366bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
367{
368 struct drm_device *dev = fb_helper->dev;
369 bool ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000370 bool do_delayed = false;
371
Rob Clark5ea1f752014-05-30 12:29:48 -0400372 drm_modeset_lock_all(dev);
373 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000374
375 do_delayed = fb_helper->delayed_hotplug;
376 if (do_delayed)
377 fb_helper->delayed_hotplug = false;
Rob Clark5ea1f752014-05-30 12:29:48 -0400378 drm_modeset_unlock_all(dev);
Dave Airliee2809c72014-11-26 13:15:24 +1000379
380 if (do_delayed)
381 drm_fb_helper_hotplug_event(fb_helper);
Rob Clark5ea1f752014-05-30 12:29:48 -0400382 return ret;
383}
384EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100385
Daniel Vetterd21bf462013-01-20 18:09:52 +0100386/*
387 * restore fbcon display for all kms driver's using this helper, used for sysrq
388 * and panic handling.
389 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530390static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000391{
Dave Airlie785b93e2009-08-28 15:46:53 +1000392 bool ret, error = false;
393 struct drm_fb_helper *helper;
394
395 if (list_empty(&kernel_fb_helper_list))
396 return false;
397
398 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200399 struct drm_device *dev = helper->dev;
400
401 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100402 continue;
403
Daniel Vetterdd908c82015-07-28 13:18:41 +0200404 drm_modeset_lock_all(dev);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200405 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100406 if (ret)
407 error = true;
Daniel Vettercb597bb2014-07-27 19:09:33 +0200408 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000409 }
410 return error;
411}
412
Daniel Vetter20c60c32012-12-17 12:13:23 +0100413static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
414{
415 struct drm_device *dev = fb_helper->dev;
416 struct drm_crtc *crtc;
417 int bound = 0, crtcs_bound = 0;
418
Paulo Zanoni520edd12013-11-27 18:24:08 -0200419 /* Sometimes user space wants everything disabled, so don't steal the
420 * display if there's a master. */
421 if (dev->primary->master)
422 return false;
423
Daniel Vetter6295d602015-07-09 23:44:25 +0200424 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700425 if (crtc->primary->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100426 crtcs_bound++;
Matt Roperf4510a22014-04-01 15:22:40 -0700427 if (crtc->primary->fb == fb_helper->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100428 bound++;
429 }
430
431 if (bound < crtcs_bound)
432 return false;
Paulo Zanoni520edd12013-11-27 18:24:08 -0200433
Daniel Vetter20c60c32012-12-17 12:13:23 +0100434 return true;
435}
436
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200437#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000438static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
439{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100440 bool ret;
441 ret = drm_fb_helper_force_kernel_mode();
442 if (ret == true)
443 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000444}
445static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
446
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700447static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000448{
449 schedule_work(&drm_fb_helper_restore_work);
450}
451
452static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
453 .handler = drm_fb_helper_sysrq,
454 .help_msg = "force-fb(V)",
455 .action_msg = "Restore framebuffer console",
456};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000457#else
458static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200459#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000460
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100461static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000462{
463 struct drm_fb_helper *fb_helper = info->par;
464 struct drm_device *dev = fb_helper->dev;
465 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700466 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700467 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000468
469 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100470 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000471 */
Daniel Vetter84849902012-12-02 00:28:11 +0100472 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100473 if (!drm_fb_helper_is_bound(fb_helper)) {
474 drm_modeset_unlock_all(dev);
475 return;
476 }
477
Jesse Barnese87b2c42009-09-17 18:14:41 -0700478 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000479 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000480
Dave Airlie8be48d92010-03-30 05:34:14 +0000481 if (!crtc->enabled)
482 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000483
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100484 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700485 for (j = 0; j < fb_helper->connector_count; j++) {
486 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200487 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500488 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100489 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700490 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000491 }
Daniel Vetter84849902012-12-02 00:28:11 +0100492 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000493}
494
Daniel Vetter207fd322013-01-20 22:13:14 +0100495/**
496 * drm_fb_helper_blank - implementation for ->fb_blank
497 * @blank: desired blanking state
498 * @info: fbdev registered by the helper
499 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000500int drm_fb_helper_blank(int blank, struct fb_info *info)
501{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200502 if (oops_in_progress)
503 return -EBUSY;
504
Dave Airlie785b93e2009-08-28 15:46:53 +1000505 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000506 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000507 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100508 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000509 break;
James Simmons731b5a12009-10-29 20:39:07 +0000510 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000511 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100512 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000513 break;
James Simmons731b5a12009-10-29 20:39:07 +0000514 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000515 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100516 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000517 break;
James Simmons731b5a12009-10-29 20:39:07 +0000518 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000519 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100520 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000521 break;
James Simmons731b5a12009-10-29 20:39:07 +0000522 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000523 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100524 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000525 break;
526 }
527 return 0;
528}
529EXPORT_SYMBOL(drm_fb_helper_blank);
530
531static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
532{
533 int i;
534
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000535 for (i = 0; i < helper->connector_count; i++)
536 kfree(helper->connector_info[i]);
537 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100538 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000539 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100540 if (helper->crtc_info[i].mode_set.mode)
541 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
542 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000543 kfree(helper->crtc_info);
544}
545
Daniel Vetter207fd322013-01-20 22:13:14 +0100546/**
Thierry Reding10a23102014-06-27 17:19:24 +0200547 * drm_fb_helper_prepare - setup a drm_fb_helper structure
548 * @dev: DRM device
549 * @helper: driver-allocated fbdev helper structure to set up
550 * @funcs: pointer to structure of functions associate with this helper
551 *
552 * Sets up the bare minimum to make the framebuffer helper usable. This is
553 * useful to implement race-free initialization of the polling helpers.
554 */
555void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
556 const struct drm_fb_helper_funcs *funcs)
557{
558 INIT_LIST_HEAD(&helper->kernel_fb_list);
559 helper->funcs = funcs;
560 helper->dev = dev;
561}
562EXPORT_SYMBOL(drm_fb_helper_prepare);
563
564/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100565 * drm_fb_helper_init - initialize a drm_fb_helper structure
566 * @dev: drm device
567 * @fb_helper: driver-allocated fbdev helper structure to initialize
568 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
569 * @max_conn_count: max connector count
570 *
571 * This allocates the structures for the fbdev helper with the given limits.
572 * Note that this won't yet touch the hardware (through the driver interfaces)
573 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
574 * to allow driver writes more control over the exact init sequence.
575 *
Thierry Reding10a23102014-06-27 17:19:24 +0200576 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100577 *
578 * RETURNS:
579 * Zero if everything went ok, nonzero otherwise.
580 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000581int drm_fb_helper_init(struct drm_device *dev,
582 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000583 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000584{
Dave Airlie785b93e2009-08-28 15:46:53 +1000585 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000586 int i;
587
Xiubo Li04cfe972014-03-10 09:33:58 +0800588 if (!max_conn_count)
589 return -EINVAL;
590
Dave Airlie4abe3522010-03-30 05:34:18 +0000591 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
592 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000593 return -ENOMEM;
594
Dave Airlie4abe3522010-03-30 05:34:18 +0000595 fb_helper->crtc_count = crtc_count;
596 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
597 if (!fb_helper->connector_info) {
598 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000599 return -ENOMEM;
600 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000601 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000602 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000603
604 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000605 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000606 kcalloc(max_conn_count,
607 sizeof(struct drm_connector *),
608 GFP_KERNEL);
609
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200610 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000611 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000612 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000613 }
614
615 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200616 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000617 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000618 i++;
619 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100620
Dave Airlie785b93e2009-08-28 15:46:53 +1000621 return 0;
622out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000623 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000624 return -ENOMEM;
625}
Dave Airlie4abe3522010-03-30 05:34:18 +0000626EXPORT_SYMBOL(drm_fb_helper_init);
627
Archit Tanejab8017d62015-07-22 14:57:56 +0530628/**
629 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
630 * @fb_helper: driver-allocated fbdev helper
631 *
632 * A helper to alloc fb_info and the members cmap and apertures. Called
633 * by the driver within the fb_probe fb_helper callback function.
634 *
635 * RETURNS:
636 * fb_info pointer if things went okay, pointer containing error code
637 * otherwise
638 */
639struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
640{
641 struct device *dev = fb_helper->dev->dev;
642 struct fb_info *info;
643 int ret;
644
645 info = framebuffer_alloc(0, dev);
646 if (!info)
647 return ERR_PTR(-ENOMEM);
648
649 ret = fb_alloc_cmap(&info->cmap, 256, 0);
650 if (ret)
651 goto err_release;
652
653 info->apertures = alloc_apertures(1);
654 if (!info->apertures) {
655 ret = -ENOMEM;
656 goto err_free_cmap;
657 }
658
659 fb_helper->fbdev = info;
660
661 return info;
662
663err_free_cmap:
664 fb_dealloc_cmap(&info->cmap);
665err_release:
666 framebuffer_release(info);
667 return ERR_PTR(ret);
668}
669EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
670
671/**
672 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
673 * @fb_helper: driver-allocated fbdev helper
674 *
675 * A wrapper around unregister_framebuffer, to release the fb_info
676 * framebuffer device
677 */
678void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
679{
680 if (fb_helper && fb_helper->fbdev)
681 unregister_framebuffer(fb_helper->fbdev);
682}
683EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
684
685/**
686 * drm_fb_helper_release_fbi - dealloc fb_info and its members
687 * @fb_helper: driver-allocated fbdev helper
688 *
689 * A helper to free memory taken by fb_info and the members cmap and
690 * apertures
691 */
692void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
693{
694 if (fb_helper) {
695 struct fb_info *info = fb_helper->fbdev;
696
697 if (info) {
698 if (info->cmap.len)
699 fb_dealloc_cmap(&info->cmap);
700 framebuffer_release(info);
701 }
702
703 fb_helper->fbdev = NULL;
704 }
705}
706EXPORT_SYMBOL(drm_fb_helper_release_fbi);
707
Dave Airlie4abe3522010-03-30 05:34:18 +0000708void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
709{
710 if (!list_empty(&fb_helper->kernel_fb_list)) {
711 list_del(&fb_helper->kernel_fb_list);
712 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000713 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
714 }
715 }
716
717 drm_fb_helper_crtc_free(fb_helper);
718
Dave Airlie4abe3522010-03-30 05:34:18 +0000719}
720EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000721
Archit Taneja47074ab2015-07-22 14:57:57 +0530722/**
723 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
724 * @fb_helper: driver-allocated fbdev helper
725 *
726 * A wrapper around unlink_framebuffer implemented by fbdev core
727 */
728void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
729{
730 if (fb_helper && fb_helper->fbdev)
731 unlink_framebuffer(fb_helper->fbdev);
732}
733EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
734
Archit Tanejacbb1a822015-07-31 16:21:41 +0530735/**
736 * drm_fb_helper_sys_read - wrapper around fb_sys_read
737 * @info: fb_info struct pointer
738 * @buf: userspace buffer to read from framebuffer memory
739 * @count: number of bytes to read from framebuffer memory
740 * @ppos: read offset within framebuffer memory
741 *
742 * A wrapper around fb_sys_read implemented by fbdev core
743 */
744ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
745 size_t count, loff_t *ppos)
746{
747 return fb_sys_read(info, buf, count, ppos);
748}
749EXPORT_SYMBOL(drm_fb_helper_sys_read);
750
751/**
752 * drm_fb_helper_sys_write - wrapper around fb_sys_write
753 * @info: fb_info struct pointer
754 * @buf: userspace buffer to write to framebuffer memory
755 * @count: number of bytes to write to framebuffer memory
756 * @ppos: write offset within framebuffer memory
757 *
758 * A wrapper around fb_sys_write implemented by fbdev core
759 */
760ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
761 size_t count, loff_t *ppos)
762{
763 return fb_sys_write(info, buf, count, ppos);
764}
765EXPORT_SYMBOL(drm_fb_helper_sys_write);
766
Archit Taneja742547b2015-07-31 16:21:42 +0530767/**
768 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
769 * @info: fbdev registered by the helper
770 * @rect: info about rectangle to fill
771 *
772 * A wrapper around sys_fillrect implemented by fbdev core
773 */
774void drm_fb_helper_sys_fillrect(struct fb_info *info,
775 const struct fb_fillrect *rect)
776{
777 sys_fillrect(info, rect);
778}
779EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
780
781/**
782 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
783 * @info: fbdev registered by the helper
784 * @area: info about area to copy
785 *
786 * A wrapper around sys_copyarea implemented by fbdev core
787 */
788void drm_fb_helper_sys_copyarea(struct fb_info *info,
789 const struct fb_copyarea *area)
790{
791 sys_copyarea(info, area);
792}
793EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
794
795/**
796 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
797 * @info: fbdev registered by the helper
798 * @image: info about image to blit
799 *
800 * A wrapper around sys_imageblit implemented by fbdev core
801 */
802void drm_fb_helper_sys_imageblit(struct fb_info *info,
803 const struct fb_image *image)
804{
805 sys_imageblit(info, image);
806}
807EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
808
809/**
810 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
811 * @info: fbdev registered by the helper
812 * @rect: info about rectangle to fill
813 *
814 * A wrapper around cfb_imageblit implemented by fbdev core
815 */
816void drm_fb_helper_cfb_fillrect(struct fb_info *info,
817 const struct fb_fillrect *rect)
818{
819 cfb_fillrect(info, rect);
820}
821EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
822
823/**
824 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
825 * @info: fbdev registered by the helper
826 * @area: info about area to copy
827 *
828 * A wrapper around cfb_copyarea implemented by fbdev core
829 */
830void drm_fb_helper_cfb_copyarea(struct fb_info *info,
831 const struct fb_copyarea *area)
832{
833 cfb_copyarea(info, area);
834}
835EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
836
837/**
838 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
839 * @info: fbdev registered by the helper
840 * @image: info about image to blit
841 *
842 * A wrapper around cfb_imageblit implemented by fbdev core
843 */
844void drm_fb_helper_cfb_imageblit(struct fb_info *info,
845 const struct fb_image *image)
846{
847 cfb_imageblit(info, image);
848}
849EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
850
Archit Tanejafdefa582015-07-31 16:21:43 +0530851/**
852 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
853 * @fb_helper: driver-allocated fbdev helper
854 * @state: desired state, zero to resume, non-zero to suspend
855 *
856 * A wrapper around fb_set_suspend implemented by fbdev core
857 */
858void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
859{
860 if (fb_helper && fb_helper->fbdev)
861 fb_set_suspend(fb_helper->fbdev, state);
862}
863EXPORT_SYMBOL(drm_fb_helper_set_suspend);
864
Dave Airliec850cb72009-10-23 18:49:03 +1000865static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000866 u16 blue, u16 regno, struct fb_info *info)
867{
868 struct drm_fb_helper *fb_helper = info->par;
869 struct drm_framebuffer *fb = fb_helper->fb;
870 int pindex;
871
Dave Airliec850cb72009-10-23 18:49:03 +1000872 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
873 u32 *palette;
874 u32 value;
875 /* place color in psuedopalette */
876 if (regno > 16)
877 return -EINVAL;
878 palette = (u32 *)info->pseudo_palette;
879 red >>= (16 - info->var.red.length);
880 green >>= (16 - info->var.green.length);
881 blue >>= (16 - info->var.blue.length);
882 value = (red << info->var.red.offset) |
883 (green << info->var.green.offset) |
884 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000885 if (info->var.transp.length > 0) {
886 u32 mask = (1 << info->var.transp.length) - 1;
887 mask <<= info->var.transp.offset;
888 value |= mask;
889 }
Dave Airliec850cb72009-10-23 18:49:03 +1000890 palette[regno] = value;
891 return 0;
892 }
893
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300894 /*
895 * The driver really shouldn't advertise pseudo/directcolor
896 * visuals if it can't deal with the palette.
897 */
898 if (WARN_ON(!fb_helper->funcs->gamma_set ||
899 !fb_helper->funcs->gamma_get))
900 return -EINVAL;
901
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000902 pindex = regno;
903
904 if (fb->bits_per_pixel == 16) {
905 pindex = regno << 3;
906
907 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000908 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000909 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000910 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000911
912 if (fb->depth == 16) {
913 u16 r, g, b;
914 int i;
915 if (regno < 32) {
916 for (i = 0; i < 8; i++)
917 fb_helper->funcs->gamma_set(crtc, red,
918 green, blue, pindex + i);
919 }
920
921 fb_helper->funcs->gamma_get(crtc, &r,
922 &g, &b,
923 pindex >> 1);
924
925 for (i = 0; i < 4; i++)
926 fb_helper->funcs->gamma_set(crtc, r,
927 green, b,
928 (pindex >> 1) + i);
929 }
930 }
931
932 if (fb->depth != 16)
933 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000934 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000935}
936
Daniel Vetter207fd322013-01-20 22:13:14 +0100937/**
938 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
939 * @cmap: cmap to set
940 * @info: fbdev registered by the helper
941 */
Dave Airlie068143d2009-10-05 09:58:02 +1000942int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
943{
944 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300945 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +0200946 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000947 u16 *red, *green, *blue, *transp;
948 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100949 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000950 int start;
951
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200952 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -0800953 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200954
955 drm_modeset_lock_all(dev);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300956 if (!drm_fb_helper_is_bound(fb_helper)) {
957 drm_modeset_unlock_all(dev);
958 return -EBUSY;
959 }
960
Dave Airlie8be48d92010-03-30 05:34:14 +0000961 for (i = 0; i < fb_helper->crtc_count; i++) {
962 crtc = fb_helper->crtc_info[i].mode_set.crtc;
963 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000964
965 red = cmap->red;
966 green = cmap->green;
967 blue = cmap->blue;
968 transp = cmap->transp;
969 start = cmap->start;
970
roel062ac622011-03-07 18:00:34 +0100971 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000972 u16 hred, hgreen, hblue, htransp = 0xffff;
973
974 hred = *red++;
975 hgreen = *green++;
976 hblue = *blue++;
977
978 if (transp)
979 htransp = *transp++;
980
Dave Airliec850cb72009-10-23 18:49:03 +1000981 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
982 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300983 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +1000984 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300985 if (crtc_funcs->load_lut)
986 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +1000987 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300988 out:
989 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +1000990 return rc;
991}
992EXPORT_SYMBOL(drm_fb_helper_setcmap);
993
Daniel Vetter207fd322013-01-20 22:13:14 +0100994/**
995 * drm_fb_helper_check_var - implementation for ->fb_check_var
996 * @var: screeninfo to check
997 * @info: fbdev registered by the helper
998 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000999int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1000 struct fb_info *info)
1001{
1002 struct drm_fb_helper *fb_helper = info->par;
1003 struct drm_framebuffer *fb = fb_helper->fb;
1004 int depth;
1005
Jason Wesself90ebd92010-08-05 09:22:32 -05001006 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001007 return -EINVAL;
1008
1009 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +01001010 if (var->bits_per_pixel > fb->bits_per_pixel ||
1011 var->xres > fb->width || var->yres > fb->height ||
1012 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +10001013 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001014 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1015 var->xres, var->yres, var->bits_per_pixel,
1016 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +10001017 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +10001018 return -EINVAL;
1019 }
1020
1021 switch (var->bits_per_pixel) {
1022 case 16:
1023 depth = (var->green.length == 6) ? 16 : 15;
1024 break;
1025 case 32:
1026 depth = (var->transp.length > 0) ? 32 : 24;
1027 break;
1028 default:
1029 depth = var->bits_per_pixel;
1030 break;
1031 }
1032
1033 switch (depth) {
1034 case 8:
1035 var->red.offset = 0;
1036 var->green.offset = 0;
1037 var->blue.offset = 0;
1038 var->red.length = 8;
1039 var->green.length = 8;
1040 var->blue.length = 8;
1041 var->transp.length = 0;
1042 var->transp.offset = 0;
1043 break;
1044 case 15:
1045 var->red.offset = 10;
1046 var->green.offset = 5;
1047 var->blue.offset = 0;
1048 var->red.length = 5;
1049 var->green.length = 5;
1050 var->blue.length = 5;
1051 var->transp.length = 1;
1052 var->transp.offset = 15;
1053 break;
1054 case 16:
1055 var->red.offset = 11;
1056 var->green.offset = 5;
1057 var->blue.offset = 0;
1058 var->red.length = 5;
1059 var->green.length = 6;
1060 var->blue.length = 5;
1061 var->transp.length = 0;
1062 var->transp.offset = 0;
1063 break;
1064 case 24:
1065 var->red.offset = 16;
1066 var->green.offset = 8;
1067 var->blue.offset = 0;
1068 var->red.length = 8;
1069 var->green.length = 8;
1070 var->blue.length = 8;
1071 var->transp.length = 0;
1072 var->transp.offset = 0;
1073 break;
1074 case 32:
1075 var->red.offset = 16;
1076 var->green.offset = 8;
1077 var->blue.offset = 0;
1078 var->red.length = 8;
1079 var->green.length = 8;
1080 var->blue.length = 8;
1081 var->transp.length = 8;
1082 var->transp.offset = 24;
1083 break;
1084 default:
1085 return -EINVAL;
1086 }
1087 return 0;
1088}
1089EXPORT_SYMBOL(drm_fb_helper_check_var);
1090
Daniel Vetter207fd322013-01-20 22:13:14 +01001091/**
1092 * drm_fb_helper_set_par - implementation for ->fb_set_par
1093 * @info: fbdev registered by the helper
1094 *
1095 * This will let fbcon do the mode init and is called at initialization time by
1096 * the fbdev core when registering the driver, and later on through the hotplug
1097 * callback.
1098 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001099int drm_fb_helper_set_par(struct fb_info *info)
1100{
1101 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001102 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001103
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001104 if (oops_in_progress)
1105 return -EBUSY;
1106
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001107 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001108 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001109 return -EINVAL;
1110 }
1111
Rob Clark5ea1f752014-05-30 12:29:48 -04001112 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001113
Dave Airlie785b93e2009-08-28 15:46:53 +10001114 return 0;
1115}
1116EXPORT_SYMBOL(drm_fb_helper_set_par);
1117
Daniel Vetter207fd322013-01-20 22:13:14 +01001118/**
1119 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1120 * @var: updated screen information
1121 * @info: fbdev registered by the helper
1122 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001123int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1124 struct fb_info *info)
1125{
1126 struct drm_fb_helper *fb_helper = info->par;
1127 struct drm_device *dev = fb_helper->dev;
1128 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001129 int ret = 0;
1130 int i;
1131
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001132 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001133 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001134
1135 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +01001136 if (!drm_fb_helper_is_bound(fb_helper)) {
1137 drm_modeset_unlock_all(dev);
1138 return -EBUSY;
1139 }
1140
Dave Airlie8be48d92010-03-30 05:34:14 +00001141 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001142 modeset = &fb_helper->crtc_info[i].mode_set;
1143
1144 modeset->x = var->xoffset;
1145 modeset->y = var->yoffset;
1146
1147 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001148 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001149 if (!ret) {
1150 info->var.xoffset = var->xoffset;
1151 info->var.yoffset = var->yoffset;
1152 }
1153 }
1154 }
Daniel Vetter84849902012-12-02 00:28:11 +01001155 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +10001156 return ret;
1157}
1158EXPORT_SYMBOL(drm_fb_helper_pan_display);
1159
Daniel Vetter8acf6582013-01-21 23:38:37 +01001160/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001161 * Allocates the backing storage and sets up the fbdev info structure through
1162 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1163 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001164 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001165static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1166 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001167{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001168 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001169 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001170 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +10001171 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +00001172 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001173 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001174
1175 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1176 sizes.surface_depth = 24;
1177 sizes.surface_bpp = 32;
1178 sizes.fb_width = (unsigned)-1;
1179 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001180
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001181 /* if driver picks 8 or 16 by default use that
1182 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001183 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001184 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001185
Dave Airlie785b93e2009-08-28 15:46:53 +10001186 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001187 for (i = 0; i < fb_helper->connector_count; i++) {
1188 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001189 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001190
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001191 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001192
1193 if (cmdline_mode->bpp_specified) {
1194 switch (cmdline_mode->bpp) {
1195 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001196 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001197 break;
1198 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001199 sizes.surface_depth = 15;
1200 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001201 break;
1202 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001203 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001204 break;
1205 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001206 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001207 break;
1208 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001209 sizes.surface_depth = 24;
1210 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001211 break;
1212 }
1213 break;
1214 }
1215 }
1216
Dave Airlie8be48d92010-03-30 05:34:14 +00001217 crtc_count = 0;
1218 for (i = 0; i < fb_helper->crtc_count; i++) {
1219 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001220 struct drm_mode_set *mode_set;
1221 int x, y, j;
1222 /* in case of tile group, are we the last tile vert or horiz?
1223 * If no tile group you are always the last one both vertically
1224 * and horizontally
1225 */
1226 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001227
Dave Airlie8be48d92010-03-30 05:34:14 +00001228 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001229 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001230
1231 if (!desired_mode)
1232 continue;
1233
1234 crtc_count++;
1235
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001236 x = fb_helper->crtc_info[i].x;
1237 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001238
1239 if (gamma_size == 0)
1240 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1241
1242 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1243 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001244
1245 for (j = 0; j < mode_set->num_connectors; j++) {
1246 struct drm_connector *connector = mode_set->connectors[j];
1247 if (connector->has_tile) {
1248 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1249 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1250 /* cloning to multiple tiles is just crazy-talk, so: */
1251 break;
1252 }
1253 }
1254
1255 if (lasth)
1256 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1257 if (lastv)
1258 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001259 }
1260
Dave Airlie38651672010-03-30 05:34:13 +00001261 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001262 /* hmm everyone went away - assume VGA cable just fell out
1263 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001264 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001265 sizes.fb_width = sizes.surface_width = 1024;
1266 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001267 }
1268
Dave Airlie38651672010-03-30 05:34:13 +00001269 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001270 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1271 if (ret < 0)
1272 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001273
Dave Airlie38651672010-03-30 05:34:13 +00001274 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001275
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001276 /*
1277 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1278 * events, but at init time drm_setup_crtcs needs to be called before
1279 * the fb is allocated (since we need to figure out the desired size of
1280 * the fb before we can allocate it ...). Hence we need to fix things up
1281 * here again.
1282 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001283 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001284 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1285 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1286
Dave Airlie785b93e2009-08-28 15:46:53 +10001287
Daniel Vetter8acf6582013-01-21 23:38:37 +01001288 info->var.pixclock = 0;
1289 if (register_framebuffer(info) < 0)
1290 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001291
Daniel Vetter8acf6582013-01-21 23:38:37 +01001292 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1293 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001294
Dave Airlie785b93e2009-08-28 15:46:53 +10001295 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001296 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1297 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001298
1299 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001300
Dave Airlie785b93e2009-08-28 15:46:53 +10001301 return 0;
1302}
Dave Airlie785b93e2009-08-28 15:46:53 +10001303
Daniel Vetter207fd322013-01-20 22:13:14 +01001304/**
1305 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1306 * @info: fbdev registered by the helper
1307 * @pitch: desired pitch
1308 * @depth: desired depth
1309 *
1310 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1311 * fbdev emulations. Drivers which support acceleration methods which impose
1312 * additional constraints need to set up their own limits.
1313 *
1314 * Drivers should call this (or their equivalent setup code) from their
1315 * ->fb_probe callback.
1316 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001317void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1318 uint32_t depth)
1319{
1320 info->fix.type = FB_TYPE_PACKED_PIXELS;
1321 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1322 FB_VISUAL_TRUECOLOR;
1323 info->fix.mmio_start = 0;
1324 info->fix.mmio_len = 0;
1325 info->fix.type_aux = 0;
1326 info->fix.xpanstep = 1; /* doing it in hw */
1327 info->fix.ypanstep = 1; /* doing it in hw */
1328 info->fix.ywrapstep = 0;
1329 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001330
1331 info->fix.line_length = pitch;
1332 return;
1333}
1334EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1335
Daniel Vetter207fd322013-01-20 22:13:14 +01001336/**
1337 * drm_fb_helper_fill_var - initalizes variable fbdev information
1338 * @info: fbdev instance to set up
1339 * @fb_helper: fb helper instance to use as template
1340 * @fb_width: desired fb width
1341 * @fb_height: desired fb height
1342 *
1343 * Sets up the variable fbdev metainformation from the given fb helper instance
1344 * and the drm framebuffer allocated in fb_helper->fb.
1345 *
1346 * Drivers should call this (or their equivalent setup code) from their
1347 * ->fb_probe callback after having allocated the fbdev backing
1348 * storage framebuffer.
1349 */
Dave Airlie38651672010-03-30 05:34:13 +00001350void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001351 uint32_t fb_width, uint32_t fb_height)
1352{
Dave Airlie38651672010-03-30 05:34:13 +00001353 struct drm_framebuffer *fb = fb_helper->fb;
1354 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001355 info->var.xres_virtual = fb->width;
1356 info->var.yres_virtual = fb->height;
1357 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001358 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001359 info->var.xoffset = 0;
1360 info->var.yoffset = 0;
1361 info->var.activate = FB_ACTIVATE_NOW;
1362 info->var.height = -1;
1363 info->var.width = -1;
1364
1365 switch (fb->depth) {
1366 case 8:
1367 info->var.red.offset = 0;
1368 info->var.green.offset = 0;
1369 info->var.blue.offset = 0;
1370 info->var.red.length = 8; /* 8bit DAC */
1371 info->var.green.length = 8;
1372 info->var.blue.length = 8;
1373 info->var.transp.offset = 0;
1374 info->var.transp.length = 0;
1375 break;
1376 case 15:
1377 info->var.red.offset = 10;
1378 info->var.green.offset = 5;
1379 info->var.blue.offset = 0;
1380 info->var.red.length = 5;
1381 info->var.green.length = 5;
1382 info->var.blue.length = 5;
1383 info->var.transp.offset = 15;
1384 info->var.transp.length = 1;
1385 break;
1386 case 16:
1387 info->var.red.offset = 11;
1388 info->var.green.offset = 5;
1389 info->var.blue.offset = 0;
1390 info->var.red.length = 5;
1391 info->var.green.length = 6;
1392 info->var.blue.length = 5;
1393 info->var.transp.offset = 0;
1394 break;
1395 case 24:
1396 info->var.red.offset = 16;
1397 info->var.green.offset = 8;
1398 info->var.blue.offset = 0;
1399 info->var.red.length = 8;
1400 info->var.green.length = 8;
1401 info->var.blue.length = 8;
1402 info->var.transp.offset = 0;
1403 info->var.transp.length = 0;
1404 break;
1405 case 32:
1406 info->var.red.offset = 16;
1407 info->var.green.offset = 8;
1408 info->var.blue.offset = 0;
1409 info->var.red.length = 8;
1410 info->var.green.length = 8;
1411 info->var.blue.length = 8;
1412 info->var.transp.offset = 24;
1413 info->var.transp.length = 8;
1414 break;
1415 default:
1416 break;
1417 }
1418
1419 info->var.xres = fb_width;
1420 info->var.yres = fb_height;
1421}
1422EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001423
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001424static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1425 uint32_t maxX,
1426 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001427{
1428 struct drm_connector *connector;
1429 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001430 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001431
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001432 for (i = 0; i < fb_helper->connector_count; i++) {
1433 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001434 count += connector->funcs->fill_modes(connector, maxX, maxY);
1435 }
1436
1437 return count;
1438}
1439
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001440struct 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 +00001441{
1442 struct drm_display_mode *mode;
1443
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001444 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001445 if (mode->hdisplay > width ||
1446 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001447 continue;
1448 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1449 return mode;
1450 }
1451 return NULL;
1452}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001453EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001454
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001455static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001456{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001457 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001458}
1459
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001460struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001461 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001462{
Chris Wilson1794d252011-04-17 07:43:32 +01001463 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001464 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001465 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001466
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001467 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001468 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001469 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001470
1471 /* attempt to find a matching mode in the list of modes
1472 * we have gotten so far, if not add a CVT mode that conforms
1473 */
1474 if (cmdline_mode->rb || cmdline_mode->margins)
1475 goto create_mode;
1476
Takashi Iwaic683f422014-03-19 14:53:13 +01001477 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001478again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001479 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001480 /* check width/height */
1481 if (mode->hdisplay != cmdline_mode->xres ||
1482 mode->vdisplay != cmdline_mode->yres)
1483 continue;
1484
1485 if (cmdline_mode->refresh_specified) {
1486 if (mode->vrefresh != cmdline_mode->refresh)
1487 continue;
1488 }
1489
1490 if (cmdline_mode->interlace) {
1491 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1492 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001493 } else if (prefer_non_interlace) {
1494 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1495 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001496 }
1497 return mode;
1498 }
1499
Takashi Iwaic683f422014-03-19 14:53:13 +01001500 if (prefer_non_interlace) {
1501 prefer_non_interlace = false;
1502 goto again;
1503 }
1504
Dave Airlie38651672010-03-30 05:34:13 +00001505create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001506 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1507 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001508 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001509 return mode;
1510}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001511EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001512
1513static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1514{
1515 bool enable;
1516
Sachin Kamat96081cd2012-11-15 03:43:30 +00001517 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001518 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001519 else
Dave Airlie38651672010-03-30 05:34:13 +00001520 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001521
Dave Airlie38651672010-03-30 05:34:13 +00001522 return enable;
1523}
1524
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001525static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1526 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001527{
1528 bool any_enabled = false;
1529 struct drm_connector *connector;
1530 int i = 0;
1531
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001532 for (i = 0; i < fb_helper->connector_count; i++) {
1533 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001534 enabled[i] = drm_connector_enabled(connector, true);
1535 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1536 enabled[i] ? "yes" : "no");
1537 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001538 }
1539
1540 if (any_enabled)
1541 return;
1542
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001543 for (i = 0; i < fb_helper->connector_count; i++) {
1544 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001545 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001546 }
1547}
1548
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001549static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1550 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001551 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001552 bool *enabled, int width, int height)
1553{
1554 int count, i, j;
1555 bool can_clone = false;
1556 struct drm_fb_helper_connector *fb_helper_conn;
1557 struct drm_display_mode *dmt_mode, *mode;
1558
1559 /* only contemplate cloning in the single crtc case */
1560 if (fb_helper->crtc_count > 1)
1561 return false;
1562
1563 count = 0;
1564 for (i = 0; i < fb_helper->connector_count; i++) {
1565 if (enabled[i])
1566 count++;
1567 }
1568
1569 /* only contemplate cloning if more than one connector is enabled */
1570 if (count <= 1)
1571 return false;
1572
1573 /* check the command line or if nothing common pick 1024x768 */
1574 can_clone = true;
1575 for (i = 0; i < fb_helper->connector_count; i++) {
1576 if (!enabled[i])
1577 continue;
1578 fb_helper_conn = fb_helper->connector_info[i];
1579 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1580 if (!modes[i]) {
1581 can_clone = false;
1582 break;
1583 }
1584 for (j = 0; j < i; j++) {
1585 if (!enabled[j])
1586 continue;
1587 if (!drm_mode_equal(modes[j], modes[i]))
1588 can_clone = false;
1589 }
1590 }
1591
1592 if (can_clone) {
1593 DRM_DEBUG_KMS("can clone using command line\n");
1594 return true;
1595 }
1596
1597 /* try and find a 1024x768 mode on each connector */
1598 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001599 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001600
1601 for (i = 0; i < fb_helper->connector_count; i++) {
1602
1603 if (!enabled[i])
1604 continue;
1605
1606 fb_helper_conn = fb_helper->connector_info[i];
1607 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1608 if (drm_mode_equal(mode, dmt_mode))
1609 modes[i] = mode;
1610 }
1611 if (!modes[i])
1612 can_clone = false;
1613 }
1614
1615 if (can_clone) {
1616 DRM_DEBUG_KMS("can clone using 1024x768\n");
1617 return true;
1618 }
1619 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1620 return false;
1621}
1622
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001623static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1624 struct drm_display_mode **modes,
1625 struct drm_fb_offset *offsets,
1626 int idx,
1627 int h_idx, int v_idx)
1628{
1629 struct drm_fb_helper_connector *fb_helper_conn;
1630 int i;
1631 int hoffset = 0, voffset = 0;
1632
1633 for (i = 0; i < fb_helper->connector_count; i++) {
1634 fb_helper_conn = fb_helper->connector_info[i];
1635 if (!fb_helper_conn->connector->has_tile)
1636 continue;
1637
1638 if (!modes[i] && (h_idx || v_idx)) {
1639 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1640 fb_helper_conn->connector->base.id);
1641 continue;
1642 }
1643 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1644 hoffset += modes[i]->hdisplay;
1645
1646 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1647 voffset += modes[i]->vdisplay;
1648 }
1649 offsets[idx].x = hoffset;
1650 offsets[idx].y = voffset;
1651 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1652 return 0;
1653}
1654
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001655static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001656 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001657 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00001658 bool *enabled, int width, int height)
1659{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001660 struct drm_fb_helper_connector *fb_helper_conn;
1661 int i;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001662 uint64_t conn_configured = 0, mask;
1663 int tile_pass = 0;
1664 mask = (1 << fb_helper->connector_count) - 1;
1665retry:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001666 for (i = 0; i < fb_helper->connector_count; i++) {
1667 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001668
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001669 if (conn_configured & (1 << i))
Dave Airlie38651672010-03-30 05:34:13 +00001670 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001671
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001672 if (enabled[i] == false) {
1673 conn_configured |= (1 << i);
1674 continue;
1675 }
1676
1677 /* first pass over all the untiled connectors */
1678 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1679 continue;
1680
1681 if (tile_pass == 1) {
1682 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1683 fb_helper_conn->connector->tile_v_loc != 0)
1684 continue;
1685
1686 } else {
1687 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1688 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1689 /* if this tile_pass doesn't cover any of the tiles - keep going */
1690 continue;
1691
1692 /* find the tile offsets for this pass - need
1693 to find all tiles left and above */
1694 drm_get_tile_offsets(fb_helper, modes, offsets,
1695 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1696 }
Dave Airlie38651672010-03-30 05:34:13 +00001697 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001698 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001699
1700 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001701 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001702 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001703 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1704 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 +00001705 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001706 }
1707 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001708 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1709 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001710 break;
1711 }
1712 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1713 "none");
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001714 conn_configured |= (1 << i);
1715 }
1716
1717 if ((conn_configured & mask) != mask) {
1718 tile_pass++;
1719 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00001720 }
1721 return true;
1722}
1723
Dave Airlie8be48d92010-03-30 05:34:14 +00001724static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1725 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001726 struct drm_display_mode **modes,
1727 int n, int width, int height)
1728{
1729 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001730 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001731 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02001732 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00001733 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001734 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001735 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001736 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001737
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001738 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001739 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001740
1741 fb_helper_conn = fb_helper->connector_info[n];
1742 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001743
1744 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001745 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001746 if (modes[n] == NULL)
1747 return best_score;
1748
Dave Airlie8be48d92010-03-30 05:34:14 +00001749 crtcs = kzalloc(dev->mode_config.num_connector *
1750 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001751 if (!crtcs)
1752 return best_score;
1753
1754 my_score = 1;
1755 if (connector->status == connector_status_connected)
1756 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001757 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001758 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001759 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001760 my_score++;
1761
1762 connector_funcs = connector->helper_private;
1763 encoder = connector_funcs->best_encoder(connector);
1764 if (!encoder)
1765 goto out;
1766
Dave Airlie38651672010-03-30 05:34:13 +00001767 /* select a crtc for this connector and then attempt to configure
1768 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001769 for (c = 0; c < fb_helper->crtc_count; c++) {
1770 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001771
Sachin Kamat96081cd2012-11-15 03:43:30 +00001772 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001773 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001774
1775 for (o = 0; o < n; o++)
1776 if (best_crtcs[o] == crtc)
1777 break;
1778
1779 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001780 /* ignore cloning unless only a single crtc */
1781 if (fb_helper->crtc_count > 1)
1782 continue;
1783
1784 if (!drm_mode_equal(modes[o], modes[n]))
1785 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001786 }
1787
1788 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001789 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1790 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001791 width, height);
1792 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001793 best_score = score;
1794 memcpy(best_crtcs, crtcs,
1795 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001796 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001797 }
Dave Airlie38651672010-03-30 05:34:13 +00001798 }
1799out:
1800 kfree(crtcs);
1801 return best_score;
1802}
1803
Dave Airlie8be48d92010-03-30 05:34:14 +00001804static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001805{
Dave Airlie8be48d92010-03-30 05:34:14 +00001806 struct drm_device *dev = fb_helper->dev;
1807 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001808 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001809 struct drm_fb_offset *offsets;
Dave Airlie8be48d92010-03-30 05:34:14 +00001810 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001811 bool *enabled;
1812 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001813 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001814
1815 DRM_DEBUG_KMS("\n");
1816
1817 width = dev->mode_config.max_width;
1818 height = dev->mode_config.max_height;
1819
Dave Airlie38651672010-03-30 05:34:13 +00001820 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001821 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001822 modes = kcalloc(dev->mode_config.num_connector,
1823 sizeof(struct drm_display_mode *), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001824 offsets = kcalloc(dev->mode_config.num_connector,
1825 sizeof(struct drm_fb_offset), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001826 enabled = kcalloc(dev->mode_config.num_connector,
1827 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001828 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001829 DRM_ERROR("Memory allocation failed\n");
1830 goto out;
1831 }
1832
Dave Airlie38651672010-03-30 05:34:13 +00001833
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001834 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001835
Jesse Barnes11e17a02013-02-19 13:31:39 -08001836 if (!(fb_helper->funcs->initial_config &&
1837 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001838 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08001839 enabled, width, height))) {
1840 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1841 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001842 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08001843
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001844 if (!drm_target_cloned(fb_helper, modes, offsets,
1845 enabled, width, height) &&
1846 !drm_target_preferred(fb_helper, modes, offsets,
1847 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001848 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001849
1850 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1851 width, height);
1852
1853 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001854 }
Dave Airlie38651672010-03-30 05:34:13 +00001855
Dave Airlie8be48d92010-03-30 05:34:14 +00001856 /* need to set the modesets up here for use later */
1857 /* fill out the connector<->crtc mappings into the modesets */
1858 for (i = 0; i < fb_helper->crtc_count; i++) {
1859 modeset = &fb_helper->crtc_info[i].mode_set;
1860 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001861 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001862 }
Dave Airlie38651672010-03-30 05:34:13 +00001863
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001864 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001865 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001866 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001867 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001868 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001869
Dave Airlie8be48d92010-03-30 05:34:14 +00001870 if (mode && fb_crtc) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001871 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1872 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Dave Airlie8be48d92010-03-30 05:34:14 +00001873 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001874 fb_crtc->x = offset->x;
1875 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00001876 if (modeset->mode)
1877 drm_mode_destroy(dev, modeset->mode);
1878 modeset->mode = drm_mode_duplicate(dev,
1879 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001880 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001881 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001882 modeset->x = offset->x;
1883 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00001884 }
Dave Airlie38651672010-03-30 05:34:13 +00001885 }
1886
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001887 /* Clear out any old modes if there are no more connected outputs. */
1888 for (i = 0; i < fb_helper->crtc_count; i++) {
1889 modeset = &fb_helper->crtc_info[i].mode_set;
1890 if (modeset->num_connectors == 0) {
1891 BUG_ON(modeset->fb);
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001892 if (modeset->mode)
1893 drm_mode_destroy(dev, modeset->mode);
1894 modeset->mode = NULL;
1895 }
1896 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001897out:
Dave Airlie38651672010-03-30 05:34:13 +00001898 kfree(crtcs);
1899 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001900 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00001901 kfree(enabled);
1902}
1903
1904/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001905 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001906 * @fb_helper: fb_helper device struct
1907 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001908 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001909 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001910 * At the moment, this is a cloned configuration across all heads with
1911 * a new framebuffer object as the backing store.
1912 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001913 * Note that this also registers the fbdev and so allows userspace to call into
1914 * the driver through the fbdev interfaces.
1915 *
1916 * This function will call down into the ->fb_probe callback to let
1917 * the driver allocate and initialize the fbdev info structure and the drm
1918 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1919 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1920 * values for the fbdev info structure.
1921 *
Dave Airlie38651672010-03-30 05:34:13 +00001922 * RETURNS:
1923 * Zero if everything went ok, nonzero otherwise.
1924 */
Thierry Reding01934c22014-12-19 11:21:32 +01001925int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001926{
Dave Airlie8be48d92010-03-30 05:34:14 +00001927 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001928 int count = 0;
1929
Daniel Vetter53f19042014-03-20 14:26:35 +01001930 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001931 count = drm_fb_helper_probe_connector_modes(fb_helper,
1932 dev->mode_config.max_width,
1933 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001934 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001935 /*
1936 * we shouldn't end up with no modes here.
1937 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001938 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001939 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001940
Dave Airlie8be48d92010-03-30 05:34:14 +00001941 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001942
Dave Airlie4abe3522010-03-30 05:34:18 +00001943 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001944}
Dave Airlie8be48d92010-03-30 05:34:14 +00001945EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001946
Chris Wilson73943712011-04-22 11:03:57 +01001947/**
1948 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001949 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001950 * @fb_helper: the drm_fb_helper
1951 *
Chris Wilson73943712011-04-22 11:03:57 +01001952 * Scan the connectors attached to the fb_helper and try to put together a
1953 * setup after *notification of a change in output configuration.
1954 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001955 * Called at runtime, takes the mode config locks to be able to check/change the
1956 * modeset configuration. Must be run from process context (which usually means
1957 * either the output polling work or a work item launched from the driver's
1958 * hotplug interrupt).
1959 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001960 * Note that drivers may call this even before calling
1961 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1962 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1963 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001964 *
Chris Wilson73943712011-04-22 11:03:57 +01001965 * RETURNS:
1966 * 0 on success and a non-zero error code otherwise.
1967 */
1968int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001969{
Chris Wilson73943712011-04-22 11:03:57 +01001970 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001971 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001972
Daniel Vetter89ced122013-04-11 14:26:55 +00001973 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001974 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001975 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00001976 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01001977 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001978 }
Dave Airlie38651672010-03-30 05:34:13 +00001979 DRM_DEBUG_KMS("\n");
1980
Dave Airlie4abe3522010-03-30 05:34:18 +00001981 max_width = fb_helper->fb->width;
1982 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001983
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001984 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00001985 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1986
1987 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001988 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001989 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01001990 drm_fb_helper_set_par(fb_helper->fbdev);
1991
1992 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00001993}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001994EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001995
David Rientjes6a108a12011-01-20 14:44:16 -08001996/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001997 * but the module doesn't depend on any fb console symbols. At least
1998 * attempt to load fbcon to avoid leaving the system without a usable console.
1999 */
David Rientjes6a108a12011-01-20 14:44:16 -08002000#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002001static int __init drm_fb_helper_modinit(void)
2002{
2003 const char *name = "fbcon";
2004 struct module *fbcon;
2005
2006 mutex_lock(&module_mutex);
2007 fbcon = find_module(name);
2008 mutex_unlock(&module_mutex);
2009
2010 if (!fbcon)
2011 request_module_nowait(name);
2012 return 0;
2013}
2014
2015module_init(drm_fb_helper_modinit);
2016#endif