blob: e4bbb53096029718b62e07b9f6d65f4976078b32 [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 Vetterdd908c82015-07-28 13:18:41 +0200419 drm_modeset_lock_all(dev);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100420 ret = drm_fb_helper_restore_fbdev_mode(helper);
421 if (ret)
422 error = true;
Daniel Vettercb597bb2014-07-27 19:09:33 +0200423 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000424 }
425 return error;
426}
427
Daniel Vetter20c60c32012-12-17 12:13:23 +0100428static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
429{
430 struct drm_device *dev = fb_helper->dev;
431 struct drm_crtc *crtc;
432 int bound = 0, crtcs_bound = 0;
433
Paulo Zanoni520edd12013-11-27 18:24:08 -0200434 /* Sometimes user space wants everything disabled, so don't steal the
435 * display if there's a master. */
436 if (dev->primary->master)
437 return false;
438
Daniel Vetter6295d602015-07-09 23:44:25 +0200439 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700440 if (crtc->primary->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100441 crtcs_bound++;
Matt Roperf4510a22014-04-01 15:22:40 -0700442 if (crtc->primary->fb == fb_helper->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100443 bound++;
444 }
445
446 if (bound < crtcs_bound)
447 return false;
Paulo Zanoni520edd12013-11-27 18:24:08 -0200448
Daniel Vetter20c60c32012-12-17 12:13:23 +0100449 return true;
450}
451
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200452#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000453static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
454{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100455 bool ret;
456 ret = drm_fb_helper_force_kernel_mode();
457 if (ret == true)
458 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000459}
460static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
461
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700462static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000463{
464 schedule_work(&drm_fb_helper_restore_work);
465}
466
467static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
468 .handler = drm_fb_helper_sysrq,
469 .help_msg = "force-fb(V)",
470 .action_msg = "Restore framebuffer console",
471};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000472#else
473static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200474#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000475
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100476static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000477{
478 struct drm_fb_helper *fb_helper = info->par;
479 struct drm_device *dev = fb_helper->dev;
480 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700481 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700482 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000483
484 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100485 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000486 */
Daniel Vetter84849902012-12-02 00:28:11 +0100487 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100488 if (!drm_fb_helper_is_bound(fb_helper)) {
489 drm_modeset_unlock_all(dev);
490 return;
491 }
492
Jesse Barnese87b2c42009-09-17 18:14:41 -0700493 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000494 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000495
Dave Airlie8be48d92010-03-30 05:34:14 +0000496 if (!crtc->enabled)
497 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000498
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100499 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700500 for (j = 0; j < fb_helper->connector_count; j++) {
501 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200502 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500503 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100504 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700505 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000506 }
Daniel Vetter84849902012-12-02 00:28:11 +0100507 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000508}
509
Daniel Vetter207fd322013-01-20 22:13:14 +0100510/**
511 * drm_fb_helper_blank - implementation for ->fb_blank
512 * @blank: desired blanking state
513 * @info: fbdev registered by the helper
514 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000515int drm_fb_helper_blank(int blank, struct fb_info *info)
516{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200517 if (oops_in_progress)
518 return -EBUSY;
519
Dave Airlie785b93e2009-08-28 15:46:53 +1000520 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000521 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000522 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100523 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000524 break;
James Simmons731b5a12009-10-29 20:39:07 +0000525 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000526 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100527 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000528 break;
James Simmons731b5a12009-10-29 20:39:07 +0000529 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000530 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100531 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000532 break;
James Simmons731b5a12009-10-29 20:39:07 +0000533 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000534 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100535 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000536 break;
James Simmons731b5a12009-10-29 20:39:07 +0000537 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000538 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100539 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000540 break;
541 }
542 return 0;
543}
544EXPORT_SYMBOL(drm_fb_helper_blank);
545
546static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
547{
548 int i;
549
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000550 for (i = 0; i < helper->connector_count; i++)
551 kfree(helper->connector_info[i]);
552 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100553 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000554 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100555 if (helper->crtc_info[i].mode_set.mode)
556 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
557 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000558 kfree(helper->crtc_info);
559}
560
Daniel Vetter207fd322013-01-20 22:13:14 +0100561/**
Thierry Reding10a23102014-06-27 17:19:24 +0200562 * drm_fb_helper_prepare - setup a drm_fb_helper structure
563 * @dev: DRM device
564 * @helper: driver-allocated fbdev helper structure to set up
565 * @funcs: pointer to structure of functions associate with this helper
566 *
567 * Sets up the bare minimum to make the framebuffer helper usable. This is
568 * useful to implement race-free initialization of the polling helpers.
569 */
570void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
571 const struct drm_fb_helper_funcs *funcs)
572{
573 INIT_LIST_HEAD(&helper->kernel_fb_list);
574 helper->funcs = funcs;
575 helper->dev = dev;
576}
577EXPORT_SYMBOL(drm_fb_helper_prepare);
578
579/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100580 * drm_fb_helper_init - initialize a drm_fb_helper structure
581 * @dev: drm device
582 * @fb_helper: driver-allocated fbdev helper structure to initialize
583 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
584 * @max_conn_count: max connector count
585 *
586 * This allocates the structures for the fbdev helper with the given limits.
587 * Note that this won't yet touch the hardware (through the driver interfaces)
588 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
589 * to allow driver writes more control over the exact init sequence.
590 *
Thierry Reding10a23102014-06-27 17:19:24 +0200591 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100592 *
593 * RETURNS:
594 * Zero if everything went ok, nonzero otherwise.
595 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000596int drm_fb_helper_init(struct drm_device *dev,
597 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000598 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000599{
Dave Airlie785b93e2009-08-28 15:46:53 +1000600 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000601 int i;
602
Xiubo Li04cfe972014-03-10 09:33:58 +0800603 if (!max_conn_count)
604 return -EINVAL;
605
Dave Airlie4abe3522010-03-30 05:34:18 +0000606 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
607 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000608 return -ENOMEM;
609
Dave Airlie4abe3522010-03-30 05:34:18 +0000610 fb_helper->crtc_count = crtc_count;
611 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
612 if (!fb_helper->connector_info) {
613 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000614 return -ENOMEM;
615 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000616 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000617 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000618
619 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000620 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000621 kcalloc(max_conn_count,
622 sizeof(struct drm_connector *),
623 GFP_KERNEL);
624
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200625 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000626 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000627 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000628 }
629
630 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200631 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000632 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000633 i++;
634 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100635
Dave Airlie785b93e2009-08-28 15:46:53 +1000636 return 0;
637out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000638 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000639 return -ENOMEM;
640}
Dave Airlie4abe3522010-03-30 05:34:18 +0000641EXPORT_SYMBOL(drm_fb_helper_init);
642
Archit Tanejab8017d62015-07-22 14:57:56 +0530643/**
644 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
645 * @fb_helper: driver-allocated fbdev helper
646 *
647 * A helper to alloc fb_info and the members cmap and apertures. Called
648 * by the driver within the fb_probe fb_helper callback function.
649 *
650 * RETURNS:
651 * fb_info pointer if things went okay, pointer containing error code
652 * otherwise
653 */
654struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
655{
656 struct device *dev = fb_helper->dev->dev;
657 struct fb_info *info;
658 int ret;
659
660 info = framebuffer_alloc(0, dev);
661 if (!info)
662 return ERR_PTR(-ENOMEM);
663
664 ret = fb_alloc_cmap(&info->cmap, 256, 0);
665 if (ret)
666 goto err_release;
667
668 info->apertures = alloc_apertures(1);
669 if (!info->apertures) {
670 ret = -ENOMEM;
671 goto err_free_cmap;
672 }
673
674 fb_helper->fbdev = info;
675
676 return info;
677
678err_free_cmap:
679 fb_dealloc_cmap(&info->cmap);
680err_release:
681 framebuffer_release(info);
682 return ERR_PTR(ret);
683}
684EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
685
686/**
687 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
688 * @fb_helper: driver-allocated fbdev helper
689 *
690 * A wrapper around unregister_framebuffer, to release the fb_info
691 * framebuffer device
692 */
693void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
694{
695 if (fb_helper && fb_helper->fbdev)
696 unregister_framebuffer(fb_helper->fbdev);
697}
698EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
699
700/**
701 * drm_fb_helper_release_fbi - dealloc fb_info and its members
702 * @fb_helper: driver-allocated fbdev helper
703 *
704 * A helper to free memory taken by fb_info and the members cmap and
705 * apertures
706 */
707void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
708{
709 if (fb_helper) {
710 struct fb_info *info = fb_helper->fbdev;
711
712 if (info) {
713 if (info->cmap.len)
714 fb_dealloc_cmap(&info->cmap);
715 framebuffer_release(info);
716 }
717
718 fb_helper->fbdev = NULL;
719 }
720}
721EXPORT_SYMBOL(drm_fb_helper_release_fbi);
722
Dave Airlie4abe3522010-03-30 05:34:18 +0000723void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
724{
725 if (!list_empty(&fb_helper->kernel_fb_list)) {
726 list_del(&fb_helper->kernel_fb_list);
727 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000728 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
729 }
730 }
731
732 drm_fb_helper_crtc_free(fb_helper);
733
Dave Airlie4abe3522010-03-30 05:34:18 +0000734}
735EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000736
Archit Taneja47074ab2015-07-22 14:57:57 +0530737/**
738 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
739 * @fb_helper: driver-allocated fbdev helper
740 *
741 * A wrapper around unlink_framebuffer implemented by fbdev core
742 */
743void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
744{
745 if (fb_helper && fb_helper->fbdev)
746 unlink_framebuffer(fb_helper->fbdev);
747}
748EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
749
Archit Tanejacbb1a822015-07-31 16:21:41 +0530750/**
751 * drm_fb_helper_sys_read - wrapper around fb_sys_read
752 * @info: fb_info struct pointer
753 * @buf: userspace buffer to read from framebuffer memory
754 * @count: number of bytes to read from framebuffer memory
755 * @ppos: read offset within framebuffer memory
756 *
757 * A wrapper around fb_sys_read implemented by fbdev core
758 */
759ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
760 size_t count, loff_t *ppos)
761{
762 return fb_sys_read(info, buf, count, ppos);
763}
764EXPORT_SYMBOL(drm_fb_helper_sys_read);
765
766/**
767 * drm_fb_helper_sys_write - wrapper around fb_sys_write
768 * @info: fb_info struct pointer
769 * @buf: userspace buffer to write to framebuffer memory
770 * @count: number of bytes to write to framebuffer memory
771 * @ppos: write offset within framebuffer memory
772 *
773 * A wrapper around fb_sys_write implemented by fbdev core
774 */
775ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
776 size_t count, loff_t *ppos)
777{
778 return fb_sys_write(info, buf, count, ppos);
779}
780EXPORT_SYMBOL(drm_fb_helper_sys_write);
781
Archit Taneja742547b2015-07-31 16:21:42 +0530782/**
783 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
784 * @info: fbdev registered by the helper
785 * @rect: info about rectangle to fill
786 *
787 * A wrapper around sys_fillrect implemented by fbdev core
788 */
789void drm_fb_helper_sys_fillrect(struct fb_info *info,
790 const struct fb_fillrect *rect)
791{
792 sys_fillrect(info, rect);
793}
794EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
795
796/**
797 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
798 * @info: fbdev registered by the helper
799 * @area: info about area to copy
800 *
801 * A wrapper around sys_copyarea implemented by fbdev core
802 */
803void drm_fb_helper_sys_copyarea(struct fb_info *info,
804 const struct fb_copyarea *area)
805{
806 sys_copyarea(info, area);
807}
808EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
809
810/**
811 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
812 * @info: fbdev registered by the helper
813 * @image: info about image to blit
814 *
815 * A wrapper around sys_imageblit implemented by fbdev core
816 */
817void drm_fb_helper_sys_imageblit(struct fb_info *info,
818 const struct fb_image *image)
819{
820 sys_imageblit(info, image);
821}
822EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
823
824/**
825 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
826 * @info: fbdev registered by the helper
827 * @rect: info about rectangle to fill
828 *
829 * A wrapper around cfb_imageblit implemented by fbdev core
830 */
831void drm_fb_helper_cfb_fillrect(struct fb_info *info,
832 const struct fb_fillrect *rect)
833{
834 cfb_fillrect(info, rect);
835}
836EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
837
838/**
839 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
840 * @info: fbdev registered by the helper
841 * @area: info about area to copy
842 *
843 * A wrapper around cfb_copyarea implemented by fbdev core
844 */
845void drm_fb_helper_cfb_copyarea(struct fb_info *info,
846 const struct fb_copyarea *area)
847{
848 cfb_copyarea(info, area);
849}
850EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
851
852/**
853 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
854 * @info: fbdev registered by the helper
855 * @image: info about image to blit
856 *
857 * A wrapper around cfb_imageblit implemented by fbdev core
858 */
859void drm_fb_helper_cfb_imageblit(struct fb_info *info,
860 const struct fb_image *image)
861{
862 cfb_imageblit(info, image);
863}
864EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
865
Archit Tanejafdefa582015-07-31 16:21:43 +0530866/**
867 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
868 * @fb_helper: driver-allocated fbdev helper
869 * @state: desired state, zero to resume, non-zero to suspend
870 *
871 * A wrapper around fb_set_suspend implemented by fbdev core
872 */
873void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
874{
875 if (fb_helper && fb_helper->fbdev)
876 fb_set_suspend(fb_helper->fbdev, state);
877}
878EXPORT_SYMBOL(drm_fb_helper_set_suspend);
879
Dave Airliec850cb72009-10-23 18:49:03 +1000880static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000881 u16 blue, u16 regno, struct fb_info *info)
882{
883 struct drm_fb_helper *fb_helper = info->par;
884 struct drm_framebuffer *fb = fb_helper->fb;
885 int pindex;
886
Dave Airliec850cb72009-10-23 18:49:03 +1000887 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
888 u32 *palette;
889 u32 value;
890 /* place color in psuedopalette */
891 if (regno > 16)
892 return -EINVAL;
893 palette = (u32 *)info->pseudo_palette;
894 red >>= (16 - info->var.red.length);
895 green >>= (16 - info->var.green.length);
896 blue >>= (16 - info->var.blue.length);
897 value = (red << info->var.red.offset) |
898 (green << info->var.green.offset) |
899 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000900 if (info->var.transp.length > 0) {
901 u32 mask = (1 << info->var.transp.length) - 1;
902 mask <<= info->var.transp.offset;
903 value |= mask;
904 }
Dave Airliec850cb72009-10-23 18:49:03 +1000905 palette[regno] = value;
906 return 0;
907 }
908
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300909 /*
910 * The driver really shouldn't advertise pseudo/directcolor
911 * visuals if it can't deal with the palette.
912 */
913 if (WARN_ON(!fb_helper->funcs->gamma_set ||
914 !fb_helper->funcs->gamma_get))
915 return -EINVAL;
916
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000917 pindex = regno;
918
919 if (fb->bits_per_pixel == 16) {
920 pindex = regno << 3;
921
922 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000923 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000924 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000925 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000926
927 if (fb->depth == 16) {
928 u16 r, g, b;
929 int i;
930 if (regno < 32) {
931 for (i = 0; i < 8; i++)
932 fb_helper->funcs->gamma_set(crtc, red,
933 green, blue, pindex + i);
934 }
935
936 fb_helper->funcs->gamma_get(crtc, &r,
937 &g, &b,
938 pindex >> 1);
939
940 for (i = 0; i < 4; i++)
941 fb_helper->funcs->gamma_set(crtc, r,
942 green, b,
943 (pindex >> 1) + i);
944 }
945 }
946
947 if (fb->depth != 16)
948 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000949 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000950}
951
Daniel Vetter207fd322013-01-20 22:13:14 +0100952/**
953 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
954 * @cmap: cmap to set
955 * @info: fbdev registered by the helper
956 */
Dave Airlie068143d2009-10-05 09:58:02 +1000957int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
958{
959 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300960 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +0200961 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000962 u16 *red, *green, *blue, *transp;
963 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100964 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000965 int start;
966
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200967 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -0800968 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200969
970 drm_modeset_lock_all(dev);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300971 if (!drm_fb_helper_is_bound(fb_helper)) {
972 drm_modeset_unlock_all(dev);
973 return -EBUSY;
974 }
975
Dave Airlie8be48d92010-03-30 05:34:14 +0000976 for (i = 0; i < fb_helper->crtc_count; i++) {
977 crtc = fb_helper->crtc_info[i].mode_set.crtc;
978 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000979
980 red = cmap->red;
981 green = cmap->green;
982 blue = cmap->blue;
983 transp = cmap->transp;
984 start = cmap->start;
985
roel062ac622011-03-07 18:00:34 +0100986 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000987 u16 hred, hgreen, hblue, htransp = 0xffff;
988
989 hred = *red++;
990 hgreen = *green++;
991 hblue = *blue++;
992
993 if (transp)
994 htransp = *transp++;
995
Dave Airliec850cb72009-10-23 18:49:03 +1000996 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
997 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300998 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +1000999 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001000 if (crtc_funcs->load_lut)
1001 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +10001002 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001003 out:
1004 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +10001005 return rc;
1006}
1007EXPORT_SYMBOL(drm_fb_helper_setcmap);
1008
Daniel Vetter207fd322013-01-20 22:13:14 +01001009/**
1010 * drm_fb_helper_check_var - implementation for ->fb_check_var
1011 * @var: screeninfo to check
1012 * @info: fbdev registered by the helper
1013 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001014int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1015 struct fb_info *info)
1016{
1017 struct drm_fb_helper *fb_helper = info->par;
1018 struct drm_framebuffer *fb = fb_helper->fb;
1019 int depth;
1020
Jason Wesself90ebd92010-08-05 09:22:32 -05001021 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001022 return -EINVAL;
1023
1024 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +01001025 if (var->bits_per_pixel > fb->bits_per_pixel ||
1026 var->xres > fb->width || var->yres > fb->height ||
1027 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +10001028 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001029 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1030 var->xres, var->yres, var->bits_per_pixel,
1031 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +10001032 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +10001033 return -EINVAL;
1034 }
1035
1036 switch (var->bits_per_pixel) {
1037 case 16:
1038 depth = (var->green.length == 6) ? 16 : 15;
1039 break;
1040 case 32:
1041 depth = (var->transp.length > 0) ? 32 : 24;
1042 break;
1043 default:
1044 depth = var->bits_per_pixel;
1045 break;
1046 }
1047
1048 switch (depth) {
1049 case 8:
1050 var->red.offset = 0;
1051 var->green.offset = 0;
1052 var->blue.offset = 0;
1053 var->red.length = 8;
1054 var->green.length = 8;
1055 var->blue.length = 8;
1056 var->transp.length = 0;
1057 var->transp.offset = 0;
1058 break;
1059 case 15:
1060 var->red.offset = 10;
1061 var->green.offset = 5;
1062 var->blue.offset = 0;
1063 var->red.length = 5;
1064 var->green.length = 5;
1065 var->blue.length = 5;
1066 var->transp.length = 1;
1067 var->transp.offset = 15;
1068 break;
1069 case 16:
1070 var->red.offset = 11;
1071 var->green.offset = 5;
1072 var->blue.offset = 0;
1073 var->red.length = 5;
1074 var->green.length = 6;
1075 var->blue.length = 5;
1076 var->transp.length = 0;
1077 var->transp.offset = 0;
1078 break;
1079 case 24:
1080 var->red.offset = 16;
1081 var->green.offset = 8;
1082 var->blue.offset = 0;
1083 var->red.length = 8;
1084 var->green.length = 8;
1085 var->blue.length = 8;
1086 var->transp.length = 0;
1087 var->transp.offset = 0;
1088 break;
1089 case 32:
1090 var->red.offset = 16;
1091 var->green.offset = 8;
1092 var->blue.offset = 0;
1093 var->red.length = 8;
1094 var->green.length = 8;
1095 var->blue.length = 8;
1096 var->transp.length = 8;
1097 var->transp.offset = 24;
1098 break;
1099 default:
1100 return -EINVAL;
1101 }
1102 return 0;
1103}
1104EXPORT_SYMBOL(drm_fb_helper_check_var);
1105
Daniel Vetter207fd322013-01-20 22:13:14 +01001106/**
1107 * drm_fb_helper_set_par - implementation for ->fb_set_par
1108 * @info: fbdev registered by the helper
1109 *
1110 * This will let fbcon do the mode init and is called at initialization time by
1111 * the fbdev core when registering the driver, and later on through the hotplug
1112 * callback.
1113 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001114int drm_fb_helper_set_par(struct fb_info *info)
1115{
1116 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001117 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001118
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001119 if (oops_in_progress)
1120 return -EBUSY;
1121
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001122 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001123 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001124 return -EINVAL;
1125 }
1126
Rob Clark5ea1f752014-05-30 12:29:48 -04001127 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001128
Dave Airlie785b93e2009-08-28 15:46:53 +10001129 return 0;
1130}
1131EXPORT_SYMBOL(drm_fb_helper_set_par);
1132
Daniel Vetter207fd322013-01-20 22:13:14 +01001133/**
1134 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1135 * @var: updated screen information
1136 * @info: fbdev registered by the helper
1137 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001138int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1139 struct fb_info *info)
1140{
1141 struct drm_fb_helper *fb_helper = info->par;
1142 struct drm_device *dev = fb_helper->dev;
1143 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001144 int ret = 0;
1145 int i;
1146
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001147 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001148 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001149
1150 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +01001151 if (!drm_fb_helper_is_bound(fb_helper)) {
1152 drm_modeset_unlock_all(dev);
1153 return -EBUSY;
1154 }
1155
Dave Airlie8be48d92010-03-30 05:34:14 +00001156 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001157 modeset = &fb_helper->crtc_info[i].mode_set;
1158
1159 modeset->x = var->xoffset;
1160 modeset->y = var->yoffset;
1161
1162 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001163 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001164 if (!ret) {
1165 info->var.xoffset = var->xoffset;
1166 info->var.yoffset = var->yoffset;
1167 }
1168 }
1169 }
Daniel Vetter84849902012-12-02 00:28:11 +01001170 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +10001171 return ret;
1172}
1173EXPORT_SYMBOL(drm_fb_helper_pan_display);
1174
Daniel Vetter8acf6582013-01-21 23:38:37 +01001175/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001176 * Allocates the backing storage and sets up the fbdev info structure through
1177 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1178 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001179 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001180static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1181 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001182{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001183 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001184 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001185 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +10001186 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +00001187 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001188 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001189
1190 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1191 sizes.surface_depth = 24;
1192 sizes.surface_bpp = 32;
1193 sizes.fb_width = (unsigned)-1;
1194 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001195
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001196 /* if driver picks 8 or 16 by default use that
1197 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001198 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001199 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001200
Dave Airlie785b93e2009-08-28 15:46:53 +10001201 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001202 for (i = 0; i < fb_helper->connector_count; i++) {
1203 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001204 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001205
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001206 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001207
1208 if (cmdline_mode->bpp_specified) {
1209 switch (cmdline_mode->bpp) {
1210 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001211 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001212 break;
1213 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001214 sizes.surface_depth = 15;
1215 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001216 break;
1217 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001218 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001219 break;
1220 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001221 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001222 break;
1223 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001224 sizes.surface_depth = 24;
1225 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001226 break;
1227 }
1228 break;
1229 }
1230 }
1231
Dave Airlie8be48d92010-03-30 05:34:14 +00001232 crtc_count = 0;
1233 for (i = 0; i < fb_helper->crtc_count; i++) {
1234 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001235 struct drm_mode_set *mode_set;
1236 int x, y, j;
1237 /* in case of tile group, are we the last tile vert or horiz?
1238 * If no tile group you are always the last one both vertically
1239 * and horizontally
1240 */
1241 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001242
Dave Airlie8be48d92010-03-30 05:34:14 +00001243 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001244 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001245
1246 if (!desired_mode)
1247 continue;
1248
1249 crtc_count++;
1250
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001251 x = fb_helper->crtc_info[i].x;
1252 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001253
1254 if (gamma_size == 0)
1255 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1256
1257 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1258 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001259
1260 for (j = 0; j < mode_set->num_connectors; j++) {
1261 struct drm_connector *connector = mode_set->connectors[j];
1262 if (connector->has_tile) {
1263 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1264 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1265 /* cloning to multiple tiles is just crazy-talk, so: */
1266 break;
1267 }
1268 }
1269
1270 if (lasth)
1271 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1272 if (lastv)
1273 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001274 }
1275
Dave Airlie38651672010-03-30 05:34:13 +00001276 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001277 /* hmm everyone went away - assume VGA cable just fell out
1278 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001279 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001280 sizes.fb_width = sizes.surface_width = 1024;
1281 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001282 }
1283
Dave Airlie38651672010-03-30 05:34:13 +00001284 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001285 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1286 if (ret < 0)
1287 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001288
Dave Airlie38651672010-03-30 05:34:13 +00001289 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001290
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001291 /*
1292 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1293 * events, but at init time drm_setup_crtcs needs to be called before
1294 * the fb is allocated (since we need to figure out the desired size of
1295 * the fb before we can allocate it ...). Hence we need to fix things up
1296 * here again.
1297 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001298 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001299 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1300 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1301
Dave Airlie785b93e2009-08-28 15:46:53 +10001302
Daniel Vetter8acf6582013-01-21 23:38:37 +01001303 info->var.pixclock = 0;
1304 if (register_framebuffer(info) < 0)
1305 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001306
Daniel Vetter8acf6582013-01-21 23:38:37 +01001307 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1308 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001309
Dave Airlie785b93e2009-08-28 15:46:53 +10001310 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001311 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1312 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001313
1314 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001315
Dave Airlie785b93e2009-08-28 15:46:53 +10001316 return 0;
1317}
Dave Airlie785b93e2009-08-28 15:46:53 +10001318
Daniel Vetter207fd322013-01-20 22:13:14 +01001319/**
1320 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1321 * @info: fbdev registered by the helper
1322 * @pitch: desired pitch
1323 * @depth: desired depth
1324 *
1325 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1326 * fbdev emulations. Drivers which support acceleration methods which impose
1327 * additional constraints need to set up their own limits.
1328 *
1329 * Drivers should call this (or their equivalent setup code) from their
1330 * ->fb_probe callback.
1331 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001332void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1333 uint32_t depth)
1334{
1335 info->fix.type = FB_TYPE_PACKED_PIXELS;
1336 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1337 FB_VISUAL_TRUECOLOR;
1338 info->fix.mmio_start = 0;
1339 info->fix.mmio_len = 0;
1340 info->fix.type_aux = 0;
1341 info->fix.xpanstep = 1; /* doing it in hw */
1342 info->fix.ypanstep = 1; /* doing it in hw */
1343 info->fix.ywrapstep = 0;
1344 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001345
1346 info->fix.line_length = pitch;
1347 return;
1348}
1349EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1350
Daniel Vetter207fd322013-01-20 22:13:14 +01001351/**
1352 * drm_fb_helper_fill_var - initalizes variable fbdev information
1353 * @info: fbdev instance to set up
1354 * @fb_helper: fb helper instance to use as template
1355 * @fb_width: desired fb width
1356 * @fb_height: desired fb height
1357 *
1358 * Sets up the variable fbdev metainformation from the given fb helper instance
1359 * and the drm framebuffer allocated in fb_helper->fb.
1360 *
1361 * Drivers should call this (or their equivalent setup code) from their
1362 * ->fb_probe callback after having allocated the fbdev backing
1363 * storage framebuffer.
1364 */
Dave Airlie38651672010-03-30 05:34:13 +00001365void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001366 uint32_t fb_width, uint32_t fb_height)
1367{
Dave Airlie38651672010-03-30 05:34:13 +00001368 struct drm_framebuffer *fb = fb_helper->fb;
1369 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001370 info->var.xres_virtual = fb->width;
1371 info->var.yres_virtual = fb->height;
1372 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001373 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001374 info->var.xoffset = 0;
1375 info->var.yoffset = 0;
1376 info->var.activate = FB_ACTIVATE_NOW;
1377 info->var.height = -1;
1378 info->var.width = -1;
1379
1380 switch (fb->depth) {
1381 case 8:
1382 info->var.red.offset = 0;
1383 info->var.green.offset = 0;
1384 info->var.blue.offset = 0;
1385 info->var.red.length = 8; /* 8bit DAC */
1386 info->var.green.length = 8;
1387 info->var.blue.length = 8;
1388 info->var.transp.offset = 0;
1389 info->var.transp.length = 0;
1390 break;
1391 case 15:
1392 info->var.red.offset = 10;
1393 info->var.green.offset = 5;
1394 info->var.blue.offset = 0;
1395 info->var.red.length = 5;
1396 info->var.green.length = 5;
1397 info->var.blue.length = 5;
1398 info->var.transp.offset = 15;
1399 info->var.transp.length = 1;
1400 break;
1401 case 16:
1402 info->var.red.offset = 11;
1403 info->var.green.offset = 5;
1404 info->var.blue.offset = 0;
1405 info->var.red.length = 5;
1406 info->var.green.length = 6;
1407 info->var.blue.length = 5;
1408 info->var.transp.offset = 0;
1409 break;
1410 case 24:
1411 info->var.red.offset = 16;
1412 info->var.green.offset = 8;
1413 info->var.blue.offset = 0;
1414 info->var.red.length = 8;
1415 info->var.green.length = 8;
1416 info->var.blue.length = 8;
1417 info->var.transp.offset = 0;
1418 info->var.transp.length = 0;
1419 break;
1420 case 32:
1421 info->var.red.offset = 16;
1422 info->var.green.offset = 8;
1423 info->var.blue.offset = 0;
1424 info->var.red.length = 8;
1425 info->var.green.length = 8;
1426 info->var.blue.length = 8;
1427 info->var.transp.offset = 24;
1428 info->var.transp.length = 8;
1429 break;
1430 default:
1431 break;
1432 }
1433
1434 info->var.xres = fb_width;
1435 info->var.yres = fb_height;
1436}
1437EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001438
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001439static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1440 uint32_t maxX,
1441 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001442{
1443 struct drm_connector *connector;
1444 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001445 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001446
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001447 for (i = 0; i < fb_helper->connector_count; i++) {
1448 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001449 count += connector->funcs->fill_modes(connector, maxX, maxY);
1450 }
1451
1452 return count;
1453}
1454
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001455struct 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 +00001456{
1457 struct drm_display_mode *mode;
1458
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001459 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001460 if (mode->hdisplay > width ||
1461 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001462 continue;
1463 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1464 return mode;
1465 }
1466 return NULL;
1467}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001468EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001469
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001470static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001471{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001472 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001473}
1474
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001475struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001476 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001477{
Chris Wilson1794d252011-04-17 07:43:32 +01001478 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001479 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001480 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001481
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001482 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001483 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001484 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001485
1486 /* attempt to find a matching mode in the list of modes
1487 * we have gotten so far, if not add a CVT mode that conforms
1488 */
1489 if (cmdline_mode->rb || cmdline_mode->margins)
1490 goto create_mode;
1491
Takashi Iwaic683f422014-03-19 14:53:13 +01001492 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001493again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001494 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001495 /* check width/height */
1496 if (mode->hdisplay != cmdline_mode->xres ||
1497 mode->vdisplay != cmdline_mode->yres)
1498 continue;
1499
1500 if (cmdline_mode->refresh_specified) {
1501 if (mode->vrefresh != cmdline_mode->refresh)
1502 continue;
1503 }
1504
1505 if (cmdline_mode->interlace) {
1506 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1507 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001508 } else if (prefer_non_interlace) {
1509 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1510 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001511 }
1512 return mode;
1513 }
1514
Takashi Iwaic683f422014-03-19 14:53:13 +01001515 if (prefer_non_interlace) {
1516 prefer_non_interlace = false;
1517 goto again;
1518 }
1519
Dave Airlie38651672010-03-30 05:34:13 +00001520create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001521 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1522 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001523 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001524 return mode;
1525}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001526EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001527
1528static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1529{
1530 bool enable;
1531
Sachin Kamat96081cd2012-11-15 03:43:30 +00001532 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001533 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001534 else
Dave Airlie38651672010-03-30 05:34:13 +00001535 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001536
Dave Airlie38651672010-03-30 05:34:13 +00001537 return enable;
1538}
1539
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001540static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1541 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001542{
1543 bool any_enabled = false;
1544 struct drm_connector *connector;
1545 int i = 0;
1546
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001547 for (i = 0; i < fb_helper->connector_count; i++) {
1548 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001549 enabled[i] = drm_connector_enabled(connector, true);
1550 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1551 enabled[i] ? "yes" : "no");
1552 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001553 }
1554
1555 if (any_enabled)
1556 return;
1557
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001558 for (i = 0; i < fb_helper->connector_count; i++) {
1559 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001560 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001561 }
1562}
1563
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001564static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1565 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001566 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001567 bool *enabled, int width, int height)
1568{
1569 int count, i, j;
1570 bool can_clone = false;
1571 struct drm_fb_helper_connector *fb_helper_conn;
1572 struct drm_display_mode *dmt_mode, *mode;
1573
1574 /* only contemplate cloning in the single crtc case */
1575 if (fb_helper->crtc_count > 1)
1576 return false;
1577
1578 count = 0;
1579 for (i = 0; i < fb_helper->connector_count; i++) {
1580 if (enabled[i])
1581 count++;
1582 }
1583
1584 /* only contemplate cloning if more than one connector is enabled */
1585 if (count <= 1)
1586 return false;
1587
1588 /* check the command line or if nothing common pick 1024x768 */
1589 can_clone = true;
1590 for (i = 0; i < fb_helper->connector_count; i++) {
1591 if (!enabled[i])
1592 continue;
1593 fb_helper_conn = fb_helper->connector_info[i];
1594 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1595 if (!modes[i]) {
1596 can_clone = false;
1597 break;
1598 }
1599 for (j = 0; j < i; j++) {
1600 if (!enabled[j])
1601 continue;
1602 if (!drm_mode_equal(modes[j], modes[i]))
1603 can_clone = false;
1604 }
1605 }
1606
1607 if (can_clone) {
1608 DRM_DEBUG_KMS("can clone using command line\n");
1609 return true;
1610 }
1611
1612 /* try and find a 1024x768 mode on each connector */
1613 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001614 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001615
1616 for (i = 0; i < fb_helper->connector_count; i++) {
1617
1618 if (!enabled[i])
1619 continue;
1620
1621 fb_helper_conn = fb_helper->connector_info[i];
1622 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1623 if (drm_mode_equal(mode, dmt_mode))
1624 modes[i] = mode;
1625 }
1626 if (!modes[i])
1627 can_clone = false;
1628 }
1629
1630 if (can_clone) {
1631 DRM_DEBUG_KMS("can clone using 1024x768\n");
1632 return true;
1633 }
1634 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1635 return false;
1636}
1637
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001638static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1639 struct drm_display_mode **modes,
1640 struct drm_fb_offset *offsets,
1641 int idx,
1642 int h_idx, int v_idx)
1643{
1644 struct drm_fb_helper_connector *fb_helper_conn;
1645 int i;
1646 int hoffset = 0, voffset = 0;
1647
1648 for (i = 0; i < fb_helper->connector_count; i++) {
1649 fb_helper_conn = fb_helper->connector_info[i];
1650 if (!fb_helper_conn->connector->has_tile)
1651 continue;
1652
1653 if (!modes[i] && (h_idx || v_idx)) {
1654 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1655 fb_helper_conn->connector->base.id);
1656 continue;
1657 }
1658 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1659 hoffset += modes[i]->hdisplay;
1660
1661 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1662 voffset += modes[i]->vdisplay;
1663 }
1664 offsets[idx].x = hoffset;
1665 offsets[idx].y = voffset;
1666 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1667 return 0;
1668}
1669
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001670static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001671 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001672 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00001673 bool *enabled, int width, int height)
1674{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001675 struct drm_fb_helper_connector *fb_helper_conn;
1676 int i;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001677 uint64_t conn_configured = 0, mask;
1678 int tile_pass = 0;
1679 mask = (1 << fb_helper->connector_count) - 1;
1680retry:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001681 for (i = 0; i < fb_helper->connector_count; i++) {
1682 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001683
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001684 if (conn_configured & (1 << i))
Dave Airlie38651672010-03-30 05:34:13 +00001685 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001686
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001687 if (enabled[i] == false) {
1688 conn_configured |= (1 << i);
1689 continue;
1690 }
1691
1692 /* first pass over all the untiled connectors */
1693 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1694 continue;
1695
1696 if (tile_pass == 1) {
1697 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1698 fb_helper_conn->connector->tile_v_loc != 0)
1699 continue;
1700
1701 } else {
1702 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1703 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1704 /* if this tile_pass doesn't cover any of the tiles - keep going */
1705 continue;
1706
1707 /* find the tile offsets for this pass - need
1708 to find all tiles left and above */
1709 drm_get_tile_offsets(fb_helper, modes, offsets,
1710 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1711 }
Dave Airlie38651672010-03-30 05:34:13 +00001712 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001713 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001714
1715 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001716 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001717 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001718 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1719 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 +00001720 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001721 }
1722 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001723 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1724 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001725 break;
1726 }
1727 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1728 "none");
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001729 conn_configured |= (1 << i);
1730 }
1731
1732 if ((conn_configured & mask) != mask) {
1733 tile_pass++;
1734 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00001735 }
1736 return true;
1737}
1738
Dave Airlie8be48d92010-03-30 05:34:14 +00001739static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1740 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001741 struct drm_display_mode **modes,
1742 int n, int width, int height)
1743{
1744 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001745 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001746 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02001747 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00001748 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001749 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001750 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001751 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001752
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001753 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001754 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001755
1756 fb_helper_conn = fb_helper->connector_info[n];
1757 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001758
1759 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001760 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001761 if (modes[n] == NULL)
1762 return best_score;
1763
Dave Airlie8be48d92010-03-30 05:34:14 +00001764 crtcs = kzalloc(dev->mode_config.num_connector *
1765 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001766 if (!crtcs)
1767 return best_score;
1768
1769 my_score = 1;
1770 if (connector->status == connector_status_connected)
1771 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001772 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001773 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001774 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001775 my_score++;
1776
1777 connector_funcs = connector->helper_private;
1778 encoder = connector_funcs->best_encoder(connector);
1779 if (!encoder)
1780 goto out;
1781
Dave Airlie38651672010-03-30 05:34:13 +00001782 /* select a crtc for this connector and then attempt to configure
1783 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001784 for (c = 0; c < fb_helper->crtc_count; c++) {
1785 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001786
Sachin Kamat96081cd2012-11-15 03:43:30 +00001787 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001788 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001789
1790 for (o = 0; o < n; o++)
1791 if (best_crtcs[o] == crtc)
1792 break;
1793
1794 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001795 /* ignore cloning unless only a single crtc */
1796 if (fb_helper->crtc_count > 1)
1797 continue;
1798
1799 if (!drm_mode_equal(modes[o], modes[n]))
1800 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001801 }
1802
1803 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001804 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1805 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001806 width, height);
1807 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001808 best_score = score;
1809 memcpy(best_crtcs, crtcs,
1810 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001811 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001812 }
Dave Airlie38651672010-03-30 05:34:13 +00001813 }
1814out:
1815 kfree(crtcs);
1816 return best_score;
1817}
1818
Dave Airlie8be48d92010-03-30 05:34:14 +00001819static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001820{
Dave Airlie8be48d92010-03-30 05:34:14 +00001821 struct drm_device *dev = fb_helper->dev;
1822 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001823 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001824 struct drm_fb_offset *offsets;
Dave Airlie8be48d92010-03-30 05:34:14 +00001825 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001826 bool *enabled;
1827 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001828 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001829
1830 DRM_DEBUG_KMS("\n");
1831
1832 width = dev->mode_config.max_width;
1833 height = dev->mode_config.max_height;
1834
Dave Airlie38651672010-03-30 05:34:13 +00001835 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001836 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001837 modes = kcalloc(dev->mode_config.num_connector,
1838 sizeof(struct drm_display_mode *), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001839 offsets = kcalloc(dev->mode_config.num_connector,
1840 sizeof(struct drm_fb_offset), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001841 enabled = kcalloc(dev->mode_config.num_connector,
1842 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001843 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001844 DRM_ERROR("Memory allocation failed\n");
1845 goto out;
1846 }
1847
Dave Airlie38651672010-03-30 05:34:13 +00001848
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001849 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001850
Jesse Barnes11e17a02013-02-19 13:31:39 -08001851 if (!(fb_helper->funcs->initial_config &&
1852 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001853 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08001854 enabled, width, height))) {
1855 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1856 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001857 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08001858
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001859 if (!drm_target_cloned(fb_helper, modes, offsets,
1860 enabled, width, height) &&
1861 !drm_target_preferred(fb_helper, modes, offsets,
1862 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001863 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001864
1865 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1866 width, height);
1867
1868 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001869 }
Dave Airlie38651672010-03-30 05:34:13 +00001870
Dave Airlie8be48d92010-03-30 05:34:14 +00001871 /* need to set the modesets up here for use later */
1872 /* fill out the connector<->crtc mappings into the modesets */
1873 for (i = 0; i < fb_helper->crtc_count; i++) {
1874 modeset = &fb_helper->crtc_info[i].mode_set;
1875 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001876 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001877 }
Dave Airlie38651672010-03-30 05:34:13 +00001878
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001879 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001880 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001881 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001882 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001883 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001884
Dave Airlie8be48d92010-03-30 05:34:14 +00001885 if (mode && fb_crtc) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001886 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1887 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Dave Airlie8be48d92010-03-30 05:34:14 +00001888 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001889 fb_crtc->x = offset->x;
1890 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00001891 if (modeset->mode)
1892 drm_mode_destroy(dev, modeset->mode);
1893 modeset->mode = drm_mode_duplicate(dev,
1894 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001895 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001896 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001897 modeset->x = offset->x;
1898 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00001899 }
Dave Airlie38651672010-03-30 05:34:13 +00001900 }
1901
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001902 /* Clear out any old modes if there are no more connected outputs. */
1903 for (i = 0; i < fb_helper->crtc_count; i++) {
1904 modeset = &fb_helper->crtc_info[i].mode_set;
1905 if (modeset->num_connectors == 0) {
1906 BUG_ON(modeset->fb);
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001907 if (modeset->mode)
1908 drm_mode_destroy(dev, modeset->mode);
1909 modeset->mode = NULL;
1910 }
1911 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001912out:
Dave Airlie38651672010-03-30 05:34:13 +00001913 kfree(crtcs);
1914 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001915 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00001916 kfree(enabled);
1917}
1918
1919/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001920 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001921 * @fb_helper: fb_helper device struct
1922 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001923 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001924 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001925 * At the moment, this is a cloned configuration across all heads with
1926 * a new framebuffer object as the backing store.
1927 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001928 * Note that this also registers the fbdev and so allows userspace to call into
1929 * the driver through the fbdev interfaces.
1930 *
1931 * This function will call down into the ->fb_probe callback to let
1932 * the driver allocate and initialize the fbdev info structure and the drm
1933 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1934 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1935 * values for the fbdev info structure.
1936 *
Dave Airlie38651672010-03-30 05:34:13 +00001937 * RETURNS:
1938 * Zero if everything went ok, nonzero otherwise.
1939 */
Thierry Reding01934c22014-12-19 11:21:32 +01001940int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001941{
Dave Airlie8be48d92010-03-30 05:34:14 +00001942 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001943 int count = 0;
1944
Daniel Vetter53f19042014-03-20 14:26:35 +01001945 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001946 count = drm_fb_helper_probe_connector_modes(fb_helper,
1947 dev->mode_config.max_width,
1948 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001949 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001950 /*
1951 * we shouldn't end up with no modes here.
1952 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001953 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001954 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001955
Dave Airlie8be48d92010-03-30 05:34:14 +00001956 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001957
Dave Airlie4abe3522010-03-30 05:34:18 +00001958 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001959}
Dave Airlie8be48d92010-03-30 05:34:14 +00001960EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001961
Chris Wilson73943712011-04-22 11:03:57 +01001962/**
1963 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001964 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001965 * @fb_helper: the drm_fb_helper
1966 *
Chris Wilson73943712011-04-22 11:03:57 +01001967 * Scan the connectors attached to the fb_helper and try to put together a
1968 * setup after *notification of a change in output configuration.
1969 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001970 * Called at runtime, takes the mode config locks to be able to check/change the
1971 * modeset configuration. Must be run from process context (which usually means
1972 * either the output polling work or a work item launched from the driver's
1973 * hotplug interrupt).
1974 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001975 * Note that drivers may call this even before calling
1976 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1977 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1978 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001979 *
Chris Wilson73943712011-04-22 11:03:57 +01001980 * RETURNS:
1981 * 0 on success and a non-zero error code otherwise.
1982 */
1983int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001984{
Chris Wilson73943712011-04-22 11:03:57 +01001985 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001986 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001987
Daniel Vetter89ced122013-04-11 14:26:55 +00001988 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001989 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001990 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00001991 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01001992 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001993 }
Dave Airlie38651672010-03-30 05:34:13 +00001994 DRM_DEBUG_KMS("\n");
1995
Dave Airlie4abe3522010-03-30 05:34:18 +00001996 max_width = fb_helper->fb->width;
1997 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001998
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001999 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00002000 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2001
2002 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00002003 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01002004 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002005 drm_fb_helper_set_par(fb_helper->fbdev);
2006
2007 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002008}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002009EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002010
David Rientjes6a108a12011-01-20 14:44:16 -08002011/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002012 * but the module doesn't depend on any fb console symbols. At least
2013 * attempt to load fbcon to avoid leaving the system without a usable console.
2014 */
David Rientjes6a108a12011-01-20 14:44:16 -08002015#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002016static int __init drm_fb_helper_modinit(void)
2017{
2018 const char *name = "fbcon";
2019 struct module *fbcon;
2020
2021 mutex_lock(&module_mutex);
2022 fbcon = find_module(name);
2023 mutex_unlock(&module_mutex);
2024
2025 if (!fbcon)
2026 request_module_nowait(name);
2027 return 0;
2028}
2029
2030module_init(drm_fb_helper_modinit);
2031#endif