blob: ba12f51f106f283bbe110cb8a1114c52b157fdb0 [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
Daniel Vetterf64c5572015-08-25 15:45:13 +020042static bool drm_fbdev_emulation = true;
43module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
44MODULE_PARM_DESC(fbdev_emulation,
45 "Enable legacy fbdev emulation [default=true]");
46
Dave Airlie785b93e2009-08-28 15:46:53 +100047static LIST_HEAD(kernel_fb_helper_list);
48
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010049/**
50 * DOC: fbdev helpers
51 *
52 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020053 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010054 * helper functions used by many drivers to implement the kernel mode setting
55 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010056 *
Thierry Reding10a23102014-06-27 17:19:24 +020057 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
58 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
59 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
60 * default behaviour can override the third step with their own code.
61 * Teardown is done with drm_fb_helper_fini().
Daniel Vetter207fd322013-01-20 22:13:14 +010062 *
63 * At runtime drivers should restore the fbdev console by calling
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +020064 * drm_fb_helper_restore_fbdev_mode_unlocked() from their ->lastclose callback.
65 * They should also notify the fb helper code from updates to the output
Daniel Vetter207fd322013-01-20 22:13:14 +010066 * configuration by calling drm_fb_helper_hotplug_event(). For easier
67 * integration with the output polling code in drm_crtc_helper.c the modeset
Thierry Reding83c617c2014-04-29 11:44:35 +020068 * code provides a ->output_poll_changed callback.
Daniel Vetter207fd322013-01-20 22:13:14 +010069 *
70 * All other functions exported by the fb helper library can be used to
71 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020072 *
73 * It is possible, though perhaps somewhat tricky, to implement race-free
74 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
75 * helper must be called first to initialize the minimum required to make
76 * hotplug detection work. Drivers also need to make sure to properly set up
77 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
78 * it is safe to enable interrupts and start processing hotplug events. At the
79 * same time, drivers should initialize all modeset objects such as CRTCs,
80 * encoders and connectors. To finish up the fbdev helper initialization, the
81 * drm_fb_helper_init() function is called. To probe for all attached displays
82 * and set up an initial configuration using the detected hardware, drivers
83 * should call drm_fb_helper_single_add_all_connectors() followed by
84 * drm_fb_helper_initial_config().
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010085 */
86
Daniel Vetter207fd322013-01-20 22:13:14 +010087/**
88 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
89 * emulation helper
90 * @fb_helper: fbdev initialized with drm_fb_helper_init
91 *
92 * This functions adds all the available connectors for use with the given
93 * fb_helper. This is a separate step to allow drivers to freely assign
94 * connectors to the fbdev, e.g. if some are reserved for special purposes or
95 * not adequate to be used for the fbcon.
96 *
Daniel Vetter169faec2015-07-09 23:44:27 +020097 * This function is protected against concurrent connector hotadds/removals
98 * using drm_fb_helper_add_one_connector() and
99 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100100 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000101int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000102{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000103 struct drm_device *dev = fb_helper->dev;
104 struct drm_connector *connector;
105 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000106
Daniel Vetterf64c5572015-08-25 15:45:13 +0200107 if (!drm_fbdev_emulation)
108 return 0;
109
Daniel Vetter169faec2015-07-09 23:44:27 +0200110 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter6295d602015-07-09 23:44:25 +0200111 drm_for_each_connector(connector, dev) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000112 struct drm_fb_helper_connector *fb_helper_connector;
113
114 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
115 if (!fb_helper_connector)
116 goto fail;
117
118 fb_helper_connector->connector = connector;
119 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
120 }
Daniel Vetter169faec2015-07-09 23:44:27 +0200121 mutex_unlock(&dev->mode_config.mutex);
Dave Airlied50ba252009-09-23 14:44:08 +1000122 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000123fail:
124 for (i = 0; i < fb_helper->connector_count; i++) {
125 kfree(fb_helper->connector_info[i]);
126 fb_helper->connector_info[i] = NULL;
127 }
128 fb_helper->connector_count = 0;
Daniel Vetter169faec2015-07-09 23:44:27 +0200129 mutex_unlock(&dev->mode_config.mutex);
130
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000131 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +1000132}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000133EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000134
Dave Airlie65c2a892014-06-05 14:01:30 +1000135int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
136{
137 struct drm_fb_helper_connector **temp;
138 struct drm_fb_helper_connector *fb_helper_connector;
139
Daniel Vetterf64c5572015-08-25 15:45:13 +0200140 if (!drm_fbdev_emulation)
141 return 0;
142
Dave Airlie65c2a892014-06-05 14:01:30 +1000143 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
144 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
Damien Lespiau14f476f2014-08-08 19:15:20 +0100145 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 +1000146 if (!temp)
147 return -ENOMEM;
148
149 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
150 fb_helper->connector_info = temp;
151 }
152
153
154 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
155 if (!fb_helper_connector)
156 return -ENOMEM;
157
158 fb_helper_connector->connector = connector;
159 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
160 return 0;
161}
162EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
163
Rob Clark2148f182015-01-26 10:11:08 -0500164static void remove_from_modeset(struct drm_mode_set *set,
165 struct drm_connector *connector)
166{
167 int i, j;
168
169 for (i = 0; i < set->num_connectors; i++) {
170 if (set->connectors[i] == connector)
171 break;
172 }
173
174 if (i == set->num_connectors)
175 return;
176
177 for (j = i + 1; j < set->num_connectors; j++) {
178 set->connectors[j - 1] = set->connectors[j];
179 }
180 set->num_connectors--;
181
Maarten Lankhorstcebbb732015-08-11 09:54:59 +0200182 /*
Rob Clark2148f182015-01-26 10:11:08 -0500183 * TODO maybe need to makes sure we set it back to !=NULL somewhere?
184 */
Maarten Lankhorstcebbb732015-08-11 09:54:59 +0200185 if (set->num_connectors == 0) {
Rob Clark2148f182015-01-26 10:11:08 -0500186 set->fb = NULL;
Maarten Lankhorstcebbb732015-08-11 09:54:59 +0200187 drm_mode_destroy(connector->dev, set->mode);
188 set->mode = NULL;
189 }
Rob Clark2148f182015-01-26 10:11:08 -0500190}
191
Dave Airlie65c2a892014-06-05 14:01:30 +1000192int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
193 struct drm_connector *connector)
194{
195 struct drm_fb_helper_connector *fb_helper_connector;
196 int i, j;
197
Daniel Vetterf64c5572015-08-25 15:45:13 +0200198 if (!drm_fbdev_emulation)
199 return 0;
200
Dave Airlie65c2a892014-06-05 14:01:30 +1000201 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
202
203 for (i = 0; i < fb_helper->connector_count; i++) {
204 if (fb_helper->connector_info[i]->connector == connector)
205 break;
206 }
207
208 if (i == fb_helper->connector_count)
209 return -EINVAL;
210 fb_helper_connector = fb_helper->connector_info[i];
211
212 for (j = i + 1; j < fb_helper->connector_count; j++) {
213 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
214 }
215 fb_helper->connector_count--;
216 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500217
218 /* also cleanup dangling references to the connector: */
219 for (i = 0; i < fb_helper->crtc_count; i++)
220 remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector);
221
Dave Airlie65c2a892014-06-05 14:01:30 +1000222 return 0;
223}
224EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
225
Jason Wessel99231022010-10-13 14:09:43 -0500226static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
227{
228 uint16_t *r_base, *g_base, *b_base;
229 int i;
230
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300231 if (helper->funcs->gamma_get == NULL)
232 return;
233
Jason Wessel99231022010-10-13 14:09:43 -0500234 r_base = crtc->gamma_store;
235 g_base = r_base + crtc->gamma_size;
236 b_base = g_base + crtc->gamma_size;
237
238 for (i = 0; i < crtc->gamma_size; i++)
239 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
240}
241
242static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
243{
244 uint16_t *r_base, *g_base, *b_base;
245
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200246 if (crtc->funcs->gamma_set == NULL)
247 return;
248
Jason Wessel99231022010-10-13 14:09:43 -0500249 r_base = crtc->gamma_store;
250 g_base = r_base + crtc->gamma_size;
251 b_base = g_base + crtc->gamma_size;
252
253 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
254}
255
Daniel Vetter207fd322013-01-20 22:13:14 +0100256/**
257 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
258 * @info: fbdev registered by the helper
259 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500260int drm_fb_helper_debug_enter(struct fb_info *info)
261{
262 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200263 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500264 int i;
265
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500266 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
267 for (i = 0; i < helper->crtc_count; i++) {
268 struct drm_mode_set *mode_set =
269 &helper->crtc_info[i].mode_set;
270
271 if (!mode_set->crtc->enabled)
272 continue;
273
274 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500275 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500276 funcs->mode_set_base_atomic(mode_set->crtc,
277 mode_set->fb,
278 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500279 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500280 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500281 }
282 }
283
284 return 0;
285}
286EXPORT_SYMBOL(drm_fb_helper_debug_enter);
287
288/* Find the real fb for a given fb helper CRTC */
289static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
290{
291 struct drm_device *dev = crtc->dev;
292 struct drm_crtc *c;
293
Daniel Vetter6295d602015-07-09 23:44:25 +0200294 drm_for_each_crtc(c, dev) {
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500295 if (crtc->base.id == c->base.id)
Matt Roperf4510a22014-04-01 15:22:40 -0700296 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500297 }
298
299 return NULL;
300}
301
Daniel Vetter207fd322013-01-20 22:13:14 +0100302/**
303 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
304 * @info: fbdev registered by the helper
305 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500306int drm_fb_helper_debug_leave(struct fb_info *info)
307{
308 struct drm_fb_helper *helper = info->par;
309 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200310 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500311 struct drm_framebuffer *fb;
312 int i;
313
314 for (i = 0; i < helper->crtc_count; i++) {
315 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
316 crtc = mode_set->crtc;
317 funcs = crtc->helper_private;
318 fb = drm_mode_config_fb(crtc);
319
320 if (!crtc->enabled)
321 continue;
322
323 if (!fb) {
324 DRM_ERROR("no fb to restore??\n");
325 continue;
326 }
327
Jason Wessel99231022010-10-13 14:09:43 -0500328 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500329 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500330 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500331 }
332
333 return 0;
334}
335EXPORT_SYMBOL(drm_fb_helper_debug_leave);
336
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200337static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100338{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300339 struct drm_device *dev = fb_helper->dev;
340 struct drm_plane *plane;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300341 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100342
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300343 drm_warn_on_modeset_not_all_locked(dev);
344
Daniel Vetter6295d602015-07-09 23:44:25 +0200345 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700346 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
347 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100348
Sonika Jindal9783de22014-08-05 11:26:57 +0530349 if (dev->mode_config.rotation_property) {
Thomas Wood3a5f87c2014-08-20 14:45:00 +0100350 drm_mode_plane_set_obj_prop(plane,
351 dev->mode_config.rotation_property,
352 BIT(DRM_ROTATE_0));
Sonika Jindal9783de22014-08-05 11:26:57 +0530353 }
354 }
355
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100356 for (i = 0; i < fb_helper->crtc_count; i++) {
357 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300358 struct drm_crtc *crtc = mode_set->crtc;
359 int ret;
360
361 if (crtc->funcs->cursor_set) {
362 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
363 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200364 return ret;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300365 }
366
Daniel Vetter2d13b672012-12-11 13:47:23 +0100367 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100368 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200369 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100370 }
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200371
372 return 0;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100373}
Rob Clark5ea1f752014-05-30 12:29:48 -0400374
375/**
376 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
377 * @fb_helper: fbcon to restore
378 *
379 * This should be called from driver's drm ->lastclose callback
380 * when implementing an fbcon on top of kms using this helper. This ensures that
381 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200382 *
383 * RETURNS:
384 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400385 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200386int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400387{
388 struct drm_device *dev = fb_helper->dev;
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200389 bool do_delayed;
390 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000391
Daniel Vetterf64c5572015-08-25 15:45:13 +0200392 if (!drm_fbdev_emulation)
393 return -ENODEV;
394
Rob Clark5ea1f752014-05-30 12:29:48 -0400395 drm_modeset_lock_all(dev);
396 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000397
398 do_delayed = fb_helper->delayed_hotplug;
399 if (do_delayed)
400 fb_helper->delayed_hotplug = false;
Rob Clark5ea1f752014-05-30 12:29:48 -0400401 drm_modeset_unlock_all(dev);
Dave Airliee2809c72014-11-26 13:15:24 +1000402
403 if (do_delayed)
404 drm_fb_helper_hotplug_event(fb_helper);
Rob Clark5ea1f752014-05-30 12:29:48 -0400405 return ret;
406}
407EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100408
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200409static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
410{
411 struct drm_device *dev = fb_helper->dev;
412 struct drm_crtc *crtc;
413 int bound = 0, crtcs_bound = 0;
414
415 /* Sometimes user space wants everything disabled, so don't steal the
416 * display if there's a master. */
417 if (dev->primary->master)
418 return false;
419
420 drm_for_each_crtc(crtc, dev) {
421 if (crtc->primary->fb)
422 crtcs_bound++;
423 if (crtc->primary->fb == fb_helper->fb)
424 bound++;
425 }
426
427 if (bound < crtcs_bound)
428 return false;
429
430 return true;
431}
432
433#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100434/*
435 * restore fbcon display for all kms driver's using this helper, used for sysrq
436 * and panic handling.
437 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530438static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000439{
Dave Airlie785b93e2009-08-28 15:46:53 +1000440 bool ret, error = false;
441 struct drm_fb_helper *helper;
442
443 if (list_empty(&kernel_fb_helper_list))
444 return false;
445
446 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200447 struct drm_device *dev = helper->dev;
448
449 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100450 continue;
451
Daniel Vetterdd908c82015-07-28 13:18:41 +0200452 drm_modeset_lock_all(dev);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200453 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100454 if (ret)
455 error = true;
Daniel Vettercb597bb2014-07-27 19:09:33 +0200456 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000457 }
458 return error;
459}
460
Dave Airlie785b93e2009-08-28 15:46:53 +1000461static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
462{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100463 bool ret;
464 ret = drm_fb_helper_force_kernel_mode();
465 if (ret == true)
466 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000467}
468static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
469
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700470static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000471{
472 schedule_work(&drm_fb_helper_restore_work);
473}
474
475static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
476 .handler = drm_fb_helper_sysrq,
477 .help_msg = "force-fb(V)",
478 .action_msg = "Restore framebuffer console",
479};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000480#else
481static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200482#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000483
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100484static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000485{
486 struct drm_fb_helper *fb_helper = info->par;
487 struct drm_device *dev = fb_helper->dev;
488 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700489 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700490 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000491
492 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100493 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000494 */
Daniel Vetter84849902012-12-02 00:28:11 +0100495 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100496 if (!drm_fb_helper_is_bound(fb_helper)) {
497 drm_modeset_unlock_all(dev);
498 return;
499 }
500
Jesse Barnese87b2c42009-09-17 18:14:41 -0700501 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000502 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000503
Dave Airlie8be48d92010-03-30 05:34:14 +0000504 if (!crtc->enabled)
505 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000506
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100507 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700508 for (j = 0; j < fb_helper->connector_count; j++) {
509 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200510 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500511 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100512 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700513 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000514 }
Daniel Vetter84849902012-12-02 00:28:11 +0100515 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000516}
517
Daniel Vetter207fd322013-01-20 22:13:14 +0100518/**
519 * drm_fb_helper_blank - implementation for ->fb_blank
520 * @blank: desired blanking state
521 * @info: fbdev registered by the helper
522 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000523int drm_fb_helper_blank(int blank, struct fb_info *info)
524{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200525 if (oops_in_progress)
526 return -EBUSY;
527
Dave Airlie785b93e2009-08-28 15:46:53 +1000528 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000529 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000530 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100531 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000532 break;
James Simmons731b5a12009-10-29 20:39:07 +0000533 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000534 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100535 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000536 break;
James Simmons731b5a12009-10-29 20:39:07 +0000537 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000538 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100539 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000540 break;
James Simmons731b5a12009-10-29 20:39:07 +0000541 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000542 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100543 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000544 break;
James Simmons731b5a12009-10-29 20:39:07 +0000545 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000546 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100547 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000548 break;
549 }
550 return 0;
551}
552EXPORT_SYMBOL(drm_fb_helper_blank);
553
554static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
555{
556 int i;
557
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000558 for (i = 0; i < helper->connector_count; i++)
559 kfree(helper->connector_info[i]);
560 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100561 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000562 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100563 if (helper->crtc_info[i].mode_set.mode)
564 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
565 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000566 kfree(helper->crtc_info);
567}
568
Daniel Vetter207fd322013-01-20 22:13:14 +0100569/**
Thierry Reding10a23102014-06-27 17:19:24 +0200570 * drm_fb_helper_prepare - setup a drm_fb_helper structure
571 * @dev: DRM device
572 * @helper: driver-allocated fbdev helper structure to set up
573 * @funcs: pointer to structure of functions associate with this helper
574 *
575 * Sets up the bare minimum to make the framebuffer helper usable. This is
576 * useful to implement race-free initialization of the polling helpers.
577 */
578void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
579 const struct drm_fb_helper_funcs *funcs)
580{
581 INIT_LIST_HEAD(&helper->kernel_fb_list);
582 helper->funcs = funcs;
583 helper->dev = dev;
584}
585EXPORT_SYMBOL(drm_fb_helper_prepare);
586
587/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100588 * drm_fb_helper_init - initialize a drm_fb_helper structure
589 * @dev: drm device
590 * @fb_helper: driver-allocated fbdev helper structure to initialize
591 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
592 * @max_conn_count: max connector count
593 *
594 * This allocates the structures for the fbdev helper with the given limits.
595 * Note that this won't yet touch the hardware (through the driver interfaces)
596 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
597 * to allow driver writes more control over the exact init sequence.
598 *
Thierry Reding10a23102014-06-27 17:19:24 +0200599 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100600 *
601 * RETURNS:
602 * Zero if everything went ok, nonzero otherwise.
603 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000604int drm_fb_helper_init(struct drm_device *dev,
605 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000606 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000607{
Dave Airlie785b93e2009-08-28 15:46:53 +1000608 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000609 int i;
610
Daniel Vetterf64c5572015-08-25 15:45:13 +0200611 if (!drm_fbdev_emulation)
612 return 0;
613
Xiubo Li04cfe972014-03-10 09:33:58 +0800614 if (!max_conn_count)
615 return -EINVAL;
616
Dave Airlie4abe3522010-03-30 05:34:18 +0000617 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
618 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000619 return -ENOMEM;
620
Dave Airlie4abe3522010-03-30 05:34:18 +0000621 fb_helper->crtc_count = crtc_count;
622 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
623 if (!fb_helper->connector_info) {
624 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000625 return -ENOMEM;
626 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000627 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000628 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000629
630 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000631 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000632 kcalloc(max_conn_count,
633 sizeof(struct drm_connector *),
634 GFP_KERNEL);
635
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200636 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000637 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000638 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000639 }
640
641 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200642 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000643 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000644 i++;
645 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100646
Dave Airlie785b93e2009-08-28 15:46:53 +1000647 return 0;
648out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000649 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000650 return -ENOMEM;
651}
Dave Airlie4abe3522010-03-30 05:34:18 +0000652EXPORT_SYMBOL(drm_fb_helper_init);
653
Archit Tanejab8017d62015-07-22 14:57:56 +0530654/**
655 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
656 * @fb_helper: driver-allocated fbdev helper
657 *
658 * A helper to alloc fb_info and the members cmap and apertures. Called
659 * by the driver within the fb_probe fb_helper callback function.
660 *
661 * RETURNS:
662 * fb_info pointer if things went okay, pointer containing error code
663 * otherwise
664 */
665struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
666{
667 struct device *dev = fb_helper->dev->dev;
668 struct fb_info *info;
669 int ret;
670
671 info = framebuffer_alloc(0, dev);
672 if (!info)
673 return ERR_PTR(-ENOMEM);
674
675 ret = fb_alloc_cmap(&info->cmap, 256, 0);
676 if (ret)
677 goto err_release;
678
679 info->apertures = alloc_apertures(1);
680 if (!info->apertures) {
681 ret = -ENOMEM;
682 goto err_free_cmap;
683 }
684
685 fb_helper->fbdev = info;
686
687 return info;
688
689err_free_cmap:
690 fb_dealloc_cmap(&info->cmap);
691err_release:
692 framebuffer_release(info);
693 return ERR_PTR(ret);
694}
695EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
696
697/**
698 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
699 * @fb_helper: driver-allocated fbdev helper
700 *
701 * A wrapper around unregister_framebuffer, to release the fb_info
702 * framebuffer device
703 */
704void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
705{
706 if (fb_helper && fb_helper->fbdev)
707 unregister_framebuffer(fb_helper->fbdev);
708}
709EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
710
711/**
712 * drm_fb_helper_release_fbi - dealloc fb_info and its members
713 * @fb_helper: driver-allocated fbdev helper
714 *
715 * A helper to free memory taken by fb_info and the members cmap and
716 * apertures
717 */
718void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
719{
720 if (fb_helper) {
721 struct fb_info *info = fb_helper->fbdev;
722
723 if (info) {
724 if (info->cmap.len)
725 fb_dealloc_cmap(&info->cmap);
726 framebuffer_release(info);
727 }
728
729 fb_helper->fbdev = NULL;
730 }
731}
732EXPORT_SYMBOL(drm_fb_helper_release_fbi);
733
Dave Airlie4abe3522010-03-30 05:34:18 +0000734void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
735{
Daniel Vetterf64c5572015-08-25 15:45:13 +0200736 if (!drm_fbdev_emulation)
737 return;
738
Dave Airlie4abe3522010-03-30 05:34:18 +0000739 if (!list_empty(&fb_helper->kernel_fb_list)) {
740 list_del(&fb_helper->kernel_fb_list);
741 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000742 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
743 }
744 }
745
746 drm_fb_helper_crtc_free(fb_helper);
747
Dave Airlie4abe3522010-03-30 05:34:18 +0000748}
749EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000750
Archit Taneja47074ab2015-07-22 14:57:57 +0530751/**
752 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
753 * @fb_helper: driver-allocated fbdev helper
754 *
755 * A wrapper around unlink_framebuffer implemented by fbdev core
756 */
757void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
758{
759 if (fb_helper && fb_helper->fbdev)
760 unlink_framebuffer(fb_helper->fbdev);
761}
762EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
763
Archit Tanejacbb1a822015-07-31 16:21:41 +0530764/**
765 * drm_fb_helper_sys_read - wrapper around fb_sys_read
766 * @info: fb_info struct pointer
767 * @buf: userspace buffer to read from framebuffer memory
768 * @count: number of bytes to read from framebuffer memory
769 * @ppos: read offset within framebuffer memory
770 *
771 * A wrapper around fb_sys_read implemented by fbdev core
772 */
773ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
774 size_t count, loff_t *ppos)
775{
776 return fb_sys_read(info, buf, count, ppos);
777}
778EXPORT_SYMBOL(drm_fb_helper_sys_read);
779
780/**
781 * drm_fb_helper_sys_write - wrapper around fb_sys_write
782 * @info: fb_info struct pointer
783 * @buf: userspace buffer to write to framebuffer memory
784 * @count: number of bytes to write to framebuffer memory
785 * @ppos: write offset within framebuffer memory
786 *
787 * A wrapper around fb_sys_write implemented by fbdev core
788 */
789ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
790 size_t count, loff_t *ppos)
791{
792 return fb_sys_write(info, buf, count, ppos);
793}
794EXPORT_SYMBOL(drm_fb_helper_sys_write);
795
Archit Taneja742547b2015-07-31 16:21:42 +0530796/**
797 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
798 * @info: fbdev registered by the helper
799 * @rect: info about rectangle to fill
800 *
801 * A wrapper around sys_fillrect implemented by fbdev core
802 */
803void drm_fb_helper_sys_fillrect(struct fb_info *info,
804 const struct fb_fillrect *rect)
805{
806 sys_fillrect(info, rect);
807}
808EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
809
810/**
811 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
812 * @info: fbdev registered by the helper
813 * @area: info about area to copy
814 *
815 * A wrapper around sys_copyarea implemented by fbdev core
816 */
817void drm_fb_helper_sys_copyarea(struct fb_info *info,
818 const struct fb_copyarea *area)
819{
820 sys_copyarea(info, area);
821}
822EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
823
824/**
825 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
826 * @info: fbdev registered by the helper
827 * @image: info about image to blit
828 *
829 * A wrapper around sys_imageblit implemented by fbdev core
830 */
831void drm_fb_helper_sys_imageblit(struct fb_info *info,
832 const struct fb_image *image)
833{
834 sys_imageblit(info, image);
835}
836EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
837
838/**
839 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
840 * @info: fbdev registered by the helper
841 * @rect: info about rectangle to fill
842 *
843 * A wrapper around cfb_imageblit implemented by fbdev core
844 */
845void drm_fb_helper_cfb_fillrect(struct fb_info *info,
846 const struct fb_fillrect *rect)
847{
848 cfb_fillrect(info, rect);
849}
850EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
851
852/**
853 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
854 * @info: fbdev registered by the helper
855 * @area: info about area to copy
856 *
857 * A wrapper around cfb_copyarea implemented by fbdev core
858 */
859void drm_fb_helper_cfb_copyarea(struct fb_info *info,
860 const struct fb_copyarea *area)
861{
862 cfb_copyarea(info, area);
863}
864EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
865
866/**
867 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
868 * @info: fbdev registered by the helper
869 * @image: info about image to blit
870 *
871 * A wrapper around cfb_imageblit implemented by fbdev core
872 */
873void drm_fb_helper_cfb_imageblit(struct fb_info *info,
874 const struct fb_image *image)
875{
876 cfb_imageblit(info, image);
877}
878EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
879
Archit Tanejafdefa582015-07-31 16:21:43 +0530880/**
881 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
882 * @fb_helper: driver-allocated fbdev helper
883 * @state: desired state, zero to resume, non-zero to suspend
884 *
885 * A wrapper around fb_set_suspend implemented by fbdev core
886 */
887void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
888{
889 if (fb_helper && fb_helper->fbdev)
890 fb_set_suspend(fb_helper->fbdev, state);
891}
892EXPORT_SYMBOL(drm_fb_helper_set_suspend);
893
Dave Airliec850cb72009-10-23 18:49:03 +1000894static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000895 u16 blue, u16 regno, struct fb_info *info)
896{
897 struct drm_fb_helper *fb_helper = info->par;
898 struct drm_framebuffer *fb = fb_helper->fb;
899 int pindex;
900
Dave Airliec850cb72009-10-23 18:49:03 +1000901 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
902 u32 *palette;
903 u32 value;
904 /* place color in psuedopalette */
905 if (regno > 16)
906 return -EINVAL;
907 palette = (u32 *)info->pseudo_palette;
908 red >>= (16 - info->var.red.length);
909 green >>= (16 - info->var.green.length);
910 blue >>= (16 - info->var.blue.length);
911 value = (red << info->var.red.offset) |
912 (green << info->var.green.offset) |
913 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000914 if (info->var.transp.length > 0) {
915 u32 mask = (1 << info->var.transp.length) - 1;
916 mask <<= info->var.transp.offset;
917 value |= mask;
918 }
Dave Airliec850cb72009-10-23 18:49:03 +1000919 palette[regno] = value;
920 return 0;
921 }
922
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300923 /*
924 * The driver really shouldn't advertise pseudo/directcolor
925 * visuals if it can't deal with the palette.
926 */
927 if (WARN_ON(!fb_helper->funcs->gamma_set ||
928 !fb_helper->funcs->gamma_get))
929 return -EINVAL;
930
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000931 pindex = regno;
932
933 if (fb->bits_per_pixel == 16) {
934 pindex = regno << 3;
935
936 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000937 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000938 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000939 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000940
941 if (fb->depth == 16) {
942 u16 r, g, b;
943 int i;
944 if (regno < 32) {
945 for (i = 0; i < 8; i++)
946 fb_helper->funcs->gamma_set(crtc, red,
947 green, blue, pindex + i);
948 }
949
950 fb_helper->funcs->gamma_get(crtc, &r,
951 &g, &b,
952 pindex >> 1);
953
954 for (i = 0; i < 4; i++)
955 fb_helper->funcs->gamma_set(crtc, r,
956 green, b,
957 (pindex >> 1) + i);
958 }
959 }
960
961 if (fb->depth != 16)
962 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000963 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000964}
965
Daniel Vetter207fd322013-01-20 22:13:14 +0100966/**
967 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
968 * @cmap: cmap to set
969 * @info: fbdev registered by the helper
970 */
Dave Airlie068143d2009-10-05 09:58:02 +1000971int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
972{
973 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300974 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +0200975 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000976 u16 *red, *green, *blue, *transp;
977 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100978 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000979 int start;
980
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200981 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -0800982 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200983
984 drm_modeset_lock_all(dev);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300985 if (!drm_fb_helper_is_bound(fb_helper)) {
986 drm_modeset_unlock_all(dev);
987 return -EBUSY;
988 }
989
Dave Airlie8be48d92010-03-30 05:34:14 +0000990 for (i = 0; i < fb_helper->crtc_count; i++) {
991 crtc = fb_helper->crtc_info[i].mode_set.crtc;
992 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000993
994 red = cmap->red;
995 green = cmap->green;
996 blue = cmap->blue;
997 transp = cmap->transp;
998 start = cmap->start;
999
roel062ac622011-03-07 18:00:34 +01001000 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +10001001 u16 hred, hgreen, hblue, htransp = 0xffff;
1002
1003 hred = *red++;
1004 hgreen = *green++;
1005 hblue = *blue++;
1006
1007 if (transp)
1008 htransp = *transp++;
1009
Dave Airliec850cb72009-10-23 18:49:03 +10001010 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1011 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001012 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +10001013 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001014 if (crtc_funcs->load_lut)
1015 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +10001016 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001017 out:
1018 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +10001019 return rc;
1020}
1021EXPORT_SYMBOL(drm_fb_helper_setcmap);
1022
Daniel Vetter207fd322013-01-20 22:13:14 +01001023/**
1024 * drm_fb_helper_check_var - implementation for ->fb_check_var
1025 * @var: screeninfo to check
1026 * @info: fbdev registered by the helper
1027 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001028int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1029 struct fb_info *info)
1030{
1031 struct drm_fb_helper *fb_helper = info->par;
1032 struct drm_framebuffer *fb = fb_helper->fb;
1033 int depth;
1034
Jason Wesself90ebd92010-08-05 09:22:32 -05001035 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001036 return -EINVAL;
1037
1038 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +01001039 if (var->bits_per_pixel > fb->bits_per_pixel ||
1040 var->xres > fb->width || var->yres > fb->height ||
1041 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +10001042 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001043 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1044 var->xres, var->yres, var->bits_per_pixel,
1045 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +10001046 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +10001047 return -EINVAL;
1048 }
1049
1050 switch (var->bits_per_pixel) {
1051 case 16:
1052 depth = (var->green.length == 6) ? 16 : 15;
1053 break;
1054 case 32:
1055 depth = (var->transp.length > 0) ? 32 : 24;
1056 break;
1057 default:
1058 depth = var->bits_per_pixel;
1059 break;
1060 }
1061
1062 switch (depth) {
1063 case 8:
1064 var->red.offset = 0;
1065 var->green.offset = 0;
1066 var->blue.offset = 0;
1067 var->red.length = 8;
1068 var->green.length = 8;
1069 var->blue.length = 8;
1070 var->transp.length = 0;
1071 var->transp.offset = 0;
1072 break;
1073 case 15:
1074 var->red.offset = 10;
1075 var->green.offset = 5;
1076 var->blue.offset = 0;
1077 var->red.length = 5;
1078 var->green.length = 5;
1079 var->blue.length = 5;
1080 var->transp.length = 1;
1081 var->transp.offset = 15;
1082 break;
1083 case 16:
1084 var->red.offset = 11;
1085 var->green.offset = 5;
1086 var->blue.offset = 0;
1087 var->red.length = 5;
1088 var->green.length = 6;
1089 var->blue.length = 5;
1090 var->transp.length = 0;
1091 var->transp.offset = 0;
1092 break;
1093 case 24:
1094 var->red.offset = 16;
1095 var->green.offset = 8;
1096 var->blue.offset = 0;
1097 var->red.length = 8;
1098 var->green.length = 8;
1099 var->blue.length = 8;
1100 var->transp.length = 0;
1101 var->transp.offset = 0;
1102 break;
1103 case 32:
1104 var->red.offset = 16;
1105 var->green.offset = 8;
1106 var->blue.offset = 0;
1107 var->red.length = 8;
1108 var->green.length = 8;
1109 var->blue.length = 8;
1110 var->transp.length = 8;
1111 var->transp.offset = 24;
1112 break;
1113 default:
1114 return -EINVAL;
1115 }
1116 return 0;
1117}
1118EXPORT_SYMBOL(drm_fb_helper_check_var);
1119
Daniel Vetter207fd322013-01-20 22:13:14 +01001120/**
1121 * drm_fb_helper_set_par - implementation for ->fb_set_par
1122 * @info: fbdev registered by the helper
1123 *
1124 * This will let fbcon do the mode init and is called at initialization time by
1125 * the fbdev core when registering the driver, and later on through the hotplug
1126 * callback.
1127 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001128int drm_fb_helper_set_par(struct fb_info *info)
1129{
1130 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001131 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001132
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001133 if (oops_in_progress)
1134 return -EBUSY;
1135
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001136 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001137 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001138 return -EINVAL;
1139 }
1140
Rob Clark5ea1f752014-05-30 12:29:48 -04001141 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001142
Dave Airlie785b93e2009-08-28 15:46:53 +10001143 return 0;
1144}
1145EXPORT_SYMBOL(drm_fb_helper_set_par);
1146
Daniel Vetter207fd322013-01-20 22:13:14 +01001147/**
1148 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1149 * @var: updated screen information
1150 * @info: fbdev registered by the helper
1151 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001152int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1153 struct fb_info *info)
1154{
1155 struct drm_fb_helper *fb_helper = info->par;
1156 struct drm_device *dev = fb_helper->dev;
1157 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001158 int ret = 0;
1159 int i;
1160
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001161 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001162 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001163
1164 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +01001165 if (!drm_fb_helper_is_bound(fb_helper)) {
1166 drm_modeset_unlock_all(dev);
1167 return -EBUSY;
1168 }
1169
Dave Airlie8be48d92010-03-30 05:34:14 +00001170 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001171 modeset = &fb_helper->crtc_info[i].mode_set;
1172
1173 modeset->x = var->xoffset;
1174 modeset->y = var->yoffset;
1175
1176 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001177 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001178 if (!ret) {
1179 info->var.xoffset = var->xoffset;
1180 info->var.yoffset = var->yoffset;
1181 }
1182 }
1183 }
Daniel Vetter84849902012-12-02 00:28:11 +01001184 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +10001185 return ret;
1186}
1187EXPORT_SYMBOL(drm_fb_helper_pan_display);
1188
Daniel Vetter8acf6582013-01-21 23:38:37 +01001189/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001190 * Allocates the backing storage and sets up the fbdev info structure through
1191 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1192 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001193 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001194static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1195 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001196{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001197 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001198 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001199 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +10001200 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +00001201 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001202 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001203
1204 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1205 sizes.surface_depth = 24;
1206 sizes.surface_bpp = 32;
1207 sizes.fb_width = (unsigned)-1;
1208 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001209
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001210 /* if driver picks 8 or 16 by default use that
1211 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001212 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001213 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001214
Dave Airlie785b93e2009-08-28 15:46:53 +10001215 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001216 for (i = 0; i < fb_helper->connector_count; i++) {
1217 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001218 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001219
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001220 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001221
1222 if (cmdline_mode->bpp_specified) {
1223 switch (cmdline_mode->bpp) {
1224 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001225 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001226 break;
1227 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001228 sizes.surface_depth = 15;
1229 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001230 break;
1231 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001232 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001233 break;
1234 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001235 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001236 break;
1237 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001238 sizes.surface_depth = 24;
1239 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001240 break;
1241 }
1242 break;
1243 }
1244 }
1245
Dave Airlie8be48d92010-03-30 05:34:14 +00001246 crtc_count = 0;
1247 for (i = 0; i < fb_helper->crtc_count; i++) {
1248 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001249 struct drm_mode_set *mode_set;
1250 int x, y, j;
1251 /* in case of tile group, are we the last tile vert or horiz?
1252 * If no tile group you are always the last one both vertically
1253 * and horizontally
1254 */
1255 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001256
Dave Airlie8be48d92010-03-30 05:34:14 +00001257 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001258 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001259
1260 if (!desired_mode)
1261 continue;
1262
1263 crtc_count++;
1264
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001265 x = fb_helper->crtc_info[i].x;
1266 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001267
1268 if (gamma_size == 0)
1269 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1270
1271 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1272 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001273
1274 for (j = 0; j < mode_set->num_connectors; j++) {
1275 struct drm_connector *connector = mode_set->connectors[j];
1276 if (connector->has_tile) {
1277 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1278 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1279 /* cloning to multiple tiles is just crazy-talk, so: */
1280 break;
1281 }
1282 }
1283
1284 if (lasth)
1285 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1286 if (lastv)
1287 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001288 }
1289
Dave Airlie38651672010-03-30 05:34:13 +00001290 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001291 /* hmm everyone went away - assume VGA cable just fell out
1292 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001293 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001294 sizes.fb_width = sizes.surface_width = 1024;
1295 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001296 }
1297
Dave Airlie38651672010-03-30 05:34:13 +00001298 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001299 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1300 if (ret < 0)
1301 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001302
Dave Airlie38651672010-03-30 05:34:13 +00001303 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001304
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001305 /*
1306 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1307 * events, but at init time drm_setup_crtcs needs to be called before
1308 * the fb is allocated (since we need to figure out the desired size of
1309 * the fb before we can allocate it ...). Hence we need to fix things up
1310 * here again.
1311 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001312 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001313 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1314 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1315
Dave Airlie785b93e2009-08-28 15:46:53 +10001316
Daniel Vetter8acf6582013-01-21 23:38:37 +01001317 info->var.pixclock = 0;
1318 if (register_framebuffer(info) < 0)
1319 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001320
Daniel Vetter8acf6582013-01-21 23:38:37 +01001321 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1322 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001323
Dave Airlie785b93e2009-08-28 15:46:53 +10001324 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001325 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1326 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001327
1328 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001329
Dave Airlie785b93e2009-08-28 15:46:53 +10001330 return 0;
1331}
Dave Airlie785b93e2009-08-28 15:46:53 +10001332
Daniel Vetter207fd322013-01-20 22:13:14 +01001333/**
1334 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1335 * @info: fbdev registered by the helper
1336 * @pitch: desired pitch
1337 * @depth: desired depth
1338 *
1339 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1340 * fbdev emulations. Drivers which support acceleration methods which impose
1341 * additional constraints need to set up their own limits.
1342 *
1343 * Drivers should call this (or their equivalent setup code) from their
1344 * ->fb_probe callback.
1345 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001346void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1347 uint32_t depth)
1348{
1349 info->fix.type = FB_TYPE_PACKED_PIXELS;
1350 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1351 FB_VISUAL_TRUECOLOR;
1352 info->fix.mmio_start = 0;
1353 info->fix.mmio_len = 0;
1354 info->fix.type_aux = 0;
1355 info->fix.xpanstep = 1; /* doing it in hw */
1356 info->fix.ypanstep = 1; /* doing it in hw */
1357 info->fix.ywrapstep = 0;
1358 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001359
1360 info->fix.line_length = pitch;
1361 return;
1362}
1363EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1364
Daniel Vetter207fd322013-01-20 22:13:14 +01001365/**
1366 * drm_fb_helper_fill_var - initalizes variable fbdev information
1367 * @info: fbdev instance to set up
1368 * @fb_helper: fb helper instance to use as template
1369 * @fb_width: desired fb width
1370 * @fb_height: desired fb height
1371 *
1372 * Sets up the variable fbdev metainformation from the given fb helper instance
1373 * and the drm framebuffer allocated in fb_helper->fb.
1374 *
1375 * Drivers should call this (or their equivalent setup code) from their
1376 * ->fb_probe callback after having allocated the fbdev backing
1377 * storage framebuffer.
1378 */
Dave Airlie38651672010-03-30 05:34:13 +00001379void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001380 uint32_t fb_width, uint32_t fb_height)
1381{
Dave Airlie38651672010-03-30 05:34:13 +00001382 struct drm_framebuffer *fb = fb_helper->fb;
1383 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001384 info->var.xres_virtual = fb->width;
1385 info->var.yres_virtual = fb->height;
1386 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001387 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001388 info->var.xoffset = 0;
1389 info->var.yoffset = 0;
1390 info->var.activate = FB_ACTIVATE_NOW;
1391 info->var.height = -1;
1392 info->var.width = -1;
1393
1394 switch (fb->depth) {
1395 case 8:
1396 info->var.red.offset = 0;
1397 info->var.green.offset = 0;
1398 info->var.blue.offset = 0;
1399 info->var.red.length = 8; /* 8bit DAC */
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 15:
1406 info->var.red.offset = 10;
1407 info->var.green.offset = 5;
1408 info->var.blue.offset = 0;
1409 info->var.red.length = 5;
1410 info->var.green.length = 5;
1411 info->var.blue.length = 5;
1412 info->var.transp.offset = 15;
1413 info->var.transp.length = 1;
1414 break;
1415 case 16:
1416 info->var.red.offset = 11;
1417 info->var.green.offset = 5;
1418 info->var.blue.offset = 0;
1419 info->var.red.length = 5;
1420 info->var.green.length = 6;
1421 info->var.blue.length = 5;
1422 info->var.transp.offset = 0;
1423 break;
1424 case 24:
1425 info->var.red.offset = 16;
1426 info->var.green.offset = 8;
1427 info->var.blue.offset = 0;
1428 info->var.red.length = 8;
1429 info->var.green.length = 8;
1430 info->var.blue.length = 8;
1431 info->var.transp.offset = 0;
1432 info->var.transp.length = 0;
1433 break;
1434 case 32:
1435 info->var.red.offset = 16;
1436 info->var.green.offset = 8;
1437 info->var.blue.offset = 0;
1438 info->var.red.length = 8;
1439 info->var.green.length = 8;
1440 info->var.blue.length = 8;
1441 info->var.transp.offset = 24;
1442 info->var.transp.length = 8;
1443 break;
1444 default:
1445 break;
1446 }
1447
1448 info->var.xres = fb_width;
1449 info->var.yres = fb_height;
1450}
1451EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001452
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001453static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1454 uint32_t maxX,
1455 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001456{
1457 struct drm_connector *connector;
1458 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001459 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001460
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001461 for (i = 0; i < fb_helper->connector_count; i++) {
1462 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001463 count += connector->funcs->fill_modes(connector, maxX, maxY);
1464 }
1465
1466 return count;
1467}
1468
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001469struct 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 +00001470{
1471 struct drm_display_mode *mode;
1472
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001473 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001474 if (mode->hdisplay > width ||
1475 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001476 continue;
1477 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1478 return mode;
1479 }
1480 return NULL;
1481}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001482EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001483
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001484static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001485{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001486 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001487}
1488
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001489struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001490 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001491{
Chris Wilson1794d252011-04-17 07:43:32 +01001492 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001493 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001494 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001495
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001496 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001497 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001498 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001499
1500 /* attempt to find a matching mode in the list of modes
1501 * we have gotten so far, if not add a CVT mode that conforms
1502 */
1503 if (cmdline_mode->rb || cmdline_mode->margins)
1504 goto create_mode;
1505
Takashi Iwaic683f422014-03-19 14:53:13 +01001506 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001507again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001508 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001509 /* check width/height */
1510 if (mode->hdisplay != cmdline_mode->xres ||
1511 mode->vdisplay != cmdline_mode->yres)
1512 continue;
1513
1514 if (cmdline_mode->refresh_specified) {
1515 if (mode->vrefresh != cmdline_mode->refresh)
1516 continue;
1517 }
1518
1519 if (cmdline_mode->interlace) {
1520 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1521 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001522 } else if (prefer_non_interlace) {
1523 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1524 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001525 }
1526 return mode;
1527 }
1528
Takashi Iwaic683f422014-03-19 14:53:13 +01001529 if (prefer_non_interlace) {
1530 prefer_non_interlace = false;
1531 goto again;
1532 }
1533
Dave Airlie38651672010-03-30 05:34:13 +00001534create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001535 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1536 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001537 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001538 return mode;
1539}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001540EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001541
1542static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1543{
1544 bool enable;
1545
Sachin Kamat96081cd2012-11-15 03:43:30 +00001546 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001547 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001548 else
Dave Airlie38651672010-03-30 05:34:13 +00001549 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001550
Dave Airlie38651672010-03-30 05:34:13 +00001551 return enable;
1552}
1553
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001554static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1555 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001556{
1557 bool any_enabled = false;
1558 struct drm_connector *connector;
1559 int i = 0;
1560
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001561 for (i = 0; i < fb_helper->connector_count; i++) {
1562 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001563 enabled[i] = drm_connector_enabled(connector, true);
1564 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1565 enabled[i] ? "yes" : "no");
1566 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001567 }
1568
1569 if (any_enabled)
1570 return;
1571
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001572 for (i = 0; i < fb_helper->connector_count; i++) {
1573 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001574 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001575 }
1576}
1577
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001578static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1579 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001580 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001581 bool *enabled, int width, int height)
1582{
1583 int count, i, j;
1584 bool can_clone = false;
1585 struct drm_fb_helper_connector *fb_helper_conn;
1586 struct drm_display_mode *dmt_mode, *mode;
1587
1588 /* only contemplate cloning in the single crtc case */
1589 if (fb_helper->crtc_count > 1)
1590 return false;
1591
1592 count = 0;
1593 for (i = 0; i < fb_helper->connector_count; i++) {
1594 if (enabled[i])
1595 count++;
1596 }
1597
1598 /* only contemplate cloning if more than one connector is enabled */
1599 if (count <= 1)
1600 return false;
1601
1602 /* check the command line or if nothing common pick 1024x768 */
1603 can_clone = true;
1604 for (i = 0; i < fb_helper->connector_count; i++) {
1605 if (!enabled[i])
1606 continue;
1607 fb_helper_conn = fb_helper->connector_info[i];
1608 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1609 if (!modes[i]) {
1610 can_clone = false;
1611 break;
1612 }
1613 for (j = 0; j < i; j++) {
1614 if (!enabled[j])
1615 continue;
1616 if (!drm_mode_equal(modes[j], modes[i]))
1617 can_clone = false;
1618 }
1619 }
1620
1621 if (can_clone) {
1622 DRM_DEBUG_KMS("can clone using command line\n");
1623 return true;
1624 }
1625
1626 /* try and find a 1024x768 mode on each connector */
1627 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001628 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001629
1630 for (i = 0; i < fb_helper->connector_count; i++) {
1631
1632 if (!enabled[i])
1633 continue;
1634
1635 fb_helper_conn = fb_helper->connector_info[i];
1636 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1637 if (drm_mode_equal(mode, dmt_mode))
1638 modes[i] = mode;
1639 }
1640 if (!modes[i])
1641 can_clone = false;
1642 }
1643
1644 if (can_clone) {
1645 DRM_DEBUG_KMS("can clone using 1024x768\n");
1646 return true;
1647 }
1648 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1649 return false;
1650}
1651
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001652static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1653 struct drm_display_mode **modes,
1654 struct drm_fb_offset *offsets,
1655 int idx,
1656 int h_idx, int v_idx)
1657{
1658 struct drm_fb_helper_connector *fb_helper_conn;
1659 int i;
1660 int hoffset = 0, voffset = 0;
1661
1662 for (i = 0; i < fb_helper->connector_count; i++) {
1663 fb_helper_conn = fb_helper->connector_info[i];
1664 if (!fb_helper_conn->connector->has_tile)
1665 continue;
1666
1667 if (!modes[i] && (h_idx || v_idx)) {
1668 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1669 fb_helper_conn->connector->base.id);
1670 continue;
1671 }
1672 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1673 hoffset += modes[i]->hdisplay;
1674
1675 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1676 voffset += modes[i]->vdisplay;
1677 }
1678 offsets[idx].x = hoffset;
1679 offsets[idx].y = voffset;
1680 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1681 return 0;
1682}
1683
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001684static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001685 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001686 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00001687 bool *enabled, int width, int height)
1688{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001689 struct drm_fb_helper_connector *fb_helper_conn;
1690 int i;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001691 uint64_t conn_configured = 0, mask;
1692 int tile_pass = 0;
1693 mask = (1 << fb_helper->connector_count) - 1;
1694retry:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001695 for (i = 0; i < fb_helper->connector_count; i++) {
1696 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001697
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001698 if (conn_configured & (1 << i))
Dave Airlie38651672010-03-30 05:34:13 +00001699 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001700
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001701 if (enabled[i] == false) {
1702 conn_configured |= (1 << i);
1703 continue;
1704 }
1705
1706 /* first pass over all the untiled connectors */
1707 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1708 continue;
1709
1710 if (tile_pass == 1) {
1711 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1712 fb_helper_conn->connector->tile_v_loc != 0)
1713 continue;
1714
1715 } else {
1716 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1717 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1718 /* if this tile_pass doesn't cover any of the tiles - keep going */
1719 continue;
1720
1721 /* find the tile offsets for this pass - need
1722 to find all tiles left and above */
1723 drm_get_tile_offsets(fb_helper, modes, offsets,
1724 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1725 }
Dave Airlie38651672010-03-30 05:34:13 +00001726 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001727 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001728
1729 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001730 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001731 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001732 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1733 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 +00001734 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001735 }
1736 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001737 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1738 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001739 break;
1740 }
1741 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1742 "none");
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001743 conn_configured |= (1 << i);
1744 }
1745
1746 if ((conn_configured & mask) != mask) {
1747 tile_pass++;
1748 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00001749 }
1750 return true;
1751}
1752
Dave Airlie8be48d92010-03-30 05:34:14 +00001753static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1754 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001755 struct drm_display_mode **modes,
1756 int n, int width, int height)
1757{
1758 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001759 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001760 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02001761 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00001762 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001763 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001764 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001765 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001766
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001767 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001768 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001769
1770 fb_helper_conn = fb_helper->connector_info[n];
1771 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001772
1773 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001774 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001775 if (modes[n] == NULL)
1776 return best_score;
1777
Dave Airlie8be48d92010-03-30 05:34:14 +00001778 crtcs = kzalloc(dev->mode_config.num_connector *
1779 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001780 if (!crtcs)
1781 return best_score;
1782
1783 my_score = 1;
1784 if (connector->status == connector_status_connected)
1785 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001786 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001787 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001788 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001789 my_score++;
1790
1791 connector_funcs = connector->helper_private;
1792 encoder = connector_funcs->best_encoder(connector);
1793 if (!encoder)
1794 goto out;
1795
Dave Airlie38651672010-03-30 05:34:13 +00001796 /* select a crtc for this connector and then attempt to configure
1797 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001798 for (c = 0; c < fb_helper->crtc_count; c++) {
1799 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001800
Sachin Kamat96081cd2012-11-15 03:43:30 +00001801 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001802 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001803
1804 for (o = 0; o < n; o++)
1805 if (best_crtcs[o] == crtc)
1806 break;
1807
1808 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001809 /* ignore cloning unless only a single crtc */
1810 if (fb_helper->crtc_count > 1)
1811 continue;
1812
1813 if (!drm_mode_equal(modes[o], modes[n]))
1814 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001815 }
1816
1817 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001818 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1819 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001820 width, height);
1821 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001822 best_score = score;
1823 memcpy(best_crtcs, crtcs,
1824 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001825 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001826 }
Dave Airlie38651672010-03-30 05:34:13 +00001827 }
1828out:
1829 kfree(crtcs);
1830 return best_score;
1831}
1832
Dave Airlie8be48d92010-03-30 05:34:14 +00001833static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001834{
Dave Airlie8be48d92010-03-30 05:34:14 +00001835 struct drm_device *dev = fb_helper->dev;
1836 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001837 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001838 struct drm_fb_offset *offsets;
Dave Airlie8be48d92010-03-30 05:34:14 +00001839 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001840 bool *enabled;
1841 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001842 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001843
1844 DRM_DEBUG_KMS("\n");
1845
1846 width = dev->mode_config.max_width;
1847 height = dev->mode_config.max_height;
1848
Dave Airlie38651672010-03-30 05:34:13 +00001849 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001850 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001851 modes = kcalloc(dev->mode_config.num_connector,
1852 sizeof(struct drm_display_mode *), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001853 offsets = kcalloc(dev->mode_config.num_connector,
1854 sizeof(struct drm_fb_offset), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001855 enabled = kcalloc(dev->mode_config.num_connector,
1856 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001857 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001858 DRM_ERROR("Memory allocation failed\n");
1859 goto out;
1860 }
1861
Dave Airlie38651672010-03-30 05:34:13 +00001862
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001863 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001864
Jesse Barnes11e17a02013-02-19 13:31:39 -08001865 if (!(fb_helper->funcs->initial_config &&
1866 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001867 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08001868 enabled, width, height))) {
1869 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1870 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001871 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08001872
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001873 if (!drm_target_cloned(fb_helper, modes, offsets,
1874 enabled, width, height) &&
1875 !drm_target_preferred(fb_helper, modes, offsets,
1876 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001877 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001878
1879 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1880 width, height);
1881
1882 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001883 }
Dave Airlie38651672010-03-30 05:34:13 +00001884
Dave Airlie8be48d92010-03-30 05:34:14 +00001885 /* need to set the modesets up here for use later */
1886 /* fill out the connector<->crtc mappings into the modesets */
1887 for (i = 0; i < fb_helper->crtc_count; i++) {
1888 modeset = &fb_helper->crtc_info[i].mode_set;
1889 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001890 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001891 }
Dave Airlie38651672010-03-30 05:34:13 +00001892
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001893 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001894 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001895 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001896 struct drm_fb_offset *offset = &offsets[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001897 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001898
Dave Airlie8be48d92010-03-30 05:34:14 +00001899 if (mode && fb_crtc) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001900 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
1901 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Dave Airlie8be48d92010-03-30 05:34:14 +00001902 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001903 fb_crtc->x = offset->x;
1904 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00001905 if (modeset->mode)
1906 drm_mode_destroy(dev, modeset->mode);
1907 modeset->mode = drm_mode_duplicate(dev,
1908 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001909 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001910 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001911 modeset->x = offset->x;
1912 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00001913 }
Dave Airlie38651672010-03-30 05:34:13 +00001914 }
1915
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001916 /* Clear out any old modes if there are no more connected outputs. */
1917 for (i = 0; i < fb_helper->crtc_count; i++) {
1918 modeset = &fb_helper->crtc_info[i].mode_set;
1919 if (modeset->num_connectors == 0) {
1920 BUG_ON(modeset->fb);
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001921 if (modeset->mode)
1922 drm_mode_destroy(dev, modeset->mode);
1923 modeset->mode = NULL;
1924 }
1925 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001926out:
Dave Airlie38651672010-03-30 05:34:13 +00001927 kfree(crtcs);
1928 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001929 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00001930 kfree(enabled);
1931}
1932
1933/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001934 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001935 * @fb_helper: fb_helper device struct
1936 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001937 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001938 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001939 * At the moment, this is a cloned configuration across all heads with
1940 * a new framebuffer object as the backing store.
1941 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001942 * Note that this also registers the fbdev and so allows userspace to call into
1943 * the driver through the fbdev interfaces.
1944 *
1945 * This function will call down into the ->fb_probe callback to let
1946 * the driver allocate and initialize the fbdev info structure and the drm
1947 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1948 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1949 * values for the fbdev info structure.
1950 *
Dave Airlie38651672010-03-30 05:34:13 +00001951 * RETURNS:
1952 * Zero if everything went ok, nonzero otherwise.
1953 */
Thierry Reding01934c22014-12-19 11:21:32 +01001954int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001955{
Dave Airlie8be48d92010-03-30 05:34:14 +00001956 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001957 int count = 0;
1958
Daniel Vetterf64c5572015-08-25 15:45:13 +02001959 if (!drm_fbdev_emulation)
1960 return 0;
1961
Daniel Vetter53f19042014-03-20 14:26:35 +01001962 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001963 count = drm_fb_helper_probe_connector_modes(fb_helper,
1964 dev->mode_config.max_width,
1965 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001966 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001967 /*
1968 * we shouldn't end up with no modes here.
1969 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001970 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001971 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001972
Dave Airlie8be48d92010-03-30 05:34:14 +00001973 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001974
Dave Airlie4abe3522010-03-30 05:34:18 +00001975 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001976}
Dave Airlie8be48d92010-03-30 05:34:14 +00001977EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001978
Chris Wilson73943712011-04-22 11:03:57 +01001979/**
1980 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001981 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001982 * @fb_helper: the drm_fb_helper
1983 *
Chris Wilson73943712011-04-22 11:03:57 +01001984 * Scan the connectors attached to the fb_helper and try to put together a
1985 * setup after *notification of a change in output configuration.
1986 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001987 * Called at runtime, takes the mode config locks to be able to check/change the
1988 * modeset configuration. Must be run from process context (which usually means
1989 * either the output polling work or a work item launched from the driver's
1990 * hotplug interrupt).
1991 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001992 * Note that drivers may call this even before calling
1993 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1994 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1995 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001996 *
Chris Wilson73943712011-04-22 11:03:57 +01001997 * RETURNS:
1998 * 0 on success and a non-zero error code otherwise.
1999 */
2000int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002001{
Chris Wilson73943712011-04-22 11:03:57 +01002002 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01002003 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00002004
Daniel Vetterf64c5572015-08-25 15:45:13 +02002005 if (!drm_fbdev_emulation)
2006 return 0;
2007
Daniel Vetter89ced122013-04-11 14:26:55 +00002008 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002009 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002010 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00002011 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01002012 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002013 }
Dave Airlie38651672010-03-30 05:34:13 +00002014 DRM_DEBUG_KMS("\n");
2015
Dave Airlie4abe3522010-03-30 05:34:18 +00002016 max_width = fb_helper->fb->width;
2017 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00002018
Lespiau, Damien51bbd272013-09-28 16:24:05 +01002019 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00002020 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2021
2022 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00002023 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01002024 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002025 drm_fb_helper_set_par(fb_helper->fbdev);
2026
2027 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002028}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002029EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002030
David Rientjes6a108a12011-01-20 14:44:16 -08002031/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002032 * but the module doesn't depend on any fb console symbols. At least
2033 * attempt to load fbcon to avoid leaving the system without a usable console.
2034 */
David Rientjes6a108a12011-01-20 14:44:16 -08002035#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002036static int __init drm_fb_helper_modinit(void)
2037{
2038 const char *name = "fbcon";
2039 struct module *fbcon;
2040
2041 mutex_lock(&module_mutex);
2042 fbcon = find_module(name);
2043 mutex_unlock(&module_mutex);
2044
2045 if (!fbcon)
2046 request_module_nowait(name);
2047 return 0;
2048}
2049
2050module_init(drm_fb_helper_modinit);
2051#endif