blob: b243fdbdbb0823789024eb75ebb557ff25e02ad9 [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
Noralf Trønnescfe63422016-08-23 13:54:06 +020032#include <linux/console.h>
Andy Shevchenko3b40a442010-02-02 14:40:32 -080033#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100034#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.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>
Rob Clarkbbb1e522015-08-25 15:35:58 -040041#include <drm/drm_atomic.h>
42#include <drm/drm_atomic_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100043
Ville Syrjälä699fbee2016-09-19 16:33:44 +030044#include "drm_crtc_helper_internal.h"
45
Daniel Vetterf64c5572015-08-25 15:45:13 +020046static bool drm_fbdev_emulation = true;
47module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
48MODULE_PARM_DESC(fbdev_emulation,
49 "Enable legacy fbdev emulation [default=true]");
50
Xinliang Liu5f152572017-02-15 17:19:08 +010051static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
52module_param(drm_fbdev_overalloc, int, 0444);
53MODULE_PARM_DESC(drm_fbdev_overalloc,
54 "Overallocation of the fbdev buffer (%) [default="
55 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
56
Dave Airlie785b93e2009-08-28 15:46:53 +100057static LIST_HEAD(kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +000058static DEFINE_MUTEX(kernel_fb_helper_lock);
Dave Airlie785b93e2009-08-28 15:46:53 +100059
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010060/**
61 * DOC: fbdev helpers
62 *
63 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020064 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010065 * helper functions used by many drivers to implement the kernel mode setting
66 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010067 *
Thierry Reding10a23102014-06-27 17:19:24 +020068 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
69 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
70 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
71 * default behaviour can override the third step with their own code.
Daniel Vettered84e252017-02-07 15:10:49 +010072 * Teardown is done with drm_fb_helper_fini() after the fbdev device is
73 * unregisters using drm_fb_helper_unregister_fbi().
Daniel Vetter207fd322013-01-20 22:13:14 +010074 *
75 * At runtime drivers should restore the fbdev console by calling
Daniel Vetter6806cdf2017-01-25 07:26:43 +010076 * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
77 * callback. They should also notify the fb helper code from updates to the
78 * output configuration by calling drm_fb_helper_hotplug_event(). For easier
Daniel Vetter207fd322013-01-20 22:13:14 +010079 * integration with the output polling code in drm_crtc_helper.c the modeset
Daniel Vetter6806cdf2017-01-25 07:26:43 +010080 * code provides a &drm_mode_config_funcs.output_poll_changed callback.
Daniel Vetter207fd322013-01-20 22:13:14 +010081 *
82 * All other functions exported by the fb helper library can be used to
83 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020084 *
85 * It is possible, though perhaps somewhat tricky, to implement race-free
86 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
87 * helper must be called first to initialize the minimum required to make
88 * hotplug detection work. Drivers also need to make sure to properly set up
Daniel Vetter6806cdf2017-01-25 07:26:43 +010089 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
Thierry Reding10a23102014-06-27 17:19:24 +020090 * it is safe to enable interrupts and start processing hotplug events. At the
91 * same time, drivers should initialize all modeset objects such as CRTCs,
92 * encoders and connectors. To finish up the fbdev helper initialization, the
93 * drm_fb_helper_init() function is called. To probe for all attached displays
94 * and set up an initial configuration using the detected hardware, drivers
95 * should call drm_fb_helper_single_add_all_connectors() followed by
96 * drm_fb_helper_initial_config().
Noralf Trønneseaa434d2016-04-28 17:18:33 +020097 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +010098 * If &drm_framebuffer_funcs.dirty is set, the
Noralf Trønnes2dad5512016-05-11 18:09:17 +020099 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100100 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
Noralf Trønnes2dad5512016-05-11 18:09:17 +0200101 * away. This worker then calls the dirty() function ensuring that it will
102 * always run in process context since the fb_*() function could be running in
103 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
104 * callback it will also schedule dirty_work with the damage collected from the
105 * mmap page writes.
Daniel Vetterd0ddc0332012-11-01 14:45:17 +0100106 */
107
Chris Wilson966a6a12016-11-29 12:02:15 +0000108#define drm_fb_helper_for_each_connector(fbh, i__) \
109 for (({ lockdep_assert_held(&(fbh)->dev->mode_config.mutex); }), \
110 i__ = 0; i__ < (fbh)->connector_count; i__++)
111
Daniel Vetter207fd322013-01-20 22:13:14 +0100112/**
113 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
114 * emulation helper
115 * @fb_helper: fbdev initialized with drm_fb_helper_init
116 *
117 * This functions adds all the available connectors for use with the given
118 * fb_helper. This is a separate step to allow drivers to freely assign
119 * connectors to the fbdev, e.g. if some are reserved for special purposes or
120 * not adequate to be used for the fbcon.
121 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200122 * This function is protected against concurrent connector hotadds/removals
123 * using drm_fb_helper_add_one_connector() and
124 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100125 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000126int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000127{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000128 struct drm_device *dev = fb_helper->dev;
129 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100130 struct drm_connector_list_iter conn_iter;
131 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000132
Daniel Vetterf64c5572015-08-25 15:45:13 +0200133 if (!drm_fbdev_emulation)
134 return 0;
135
Daniel Vetter169faec2015-07-09 23:44:27 +0200136 mutex_lock(&dev->mode_config.mutex);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100137 drm_connector_list_iter_get(dev, &conn_iter);
138 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100139 ret = drm_fb_helper_add_one_connector(fb_helper, connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000140
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100141 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000142 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000143 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100144 goto out;
145
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000146fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000147 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300148 struct drm_fb_helper_connector *fb_helper_connector =
149 fb_helper->connector_info[i];
150
Thierry Redingad093602017-02-28 15:46:39 +0100151 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300152
153 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000154 fb_helper->connector_info[i] = NULL;
155 }
156 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100157out:
158 drm_connector_list_iter_put(&conn_iter);
Daniel Vetter169faec2015-07-09 23:44:27 +0200159 mutex_unlock(&dev->mode_config.mutex);
160
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100161 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000162}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000163EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000164
Dave Airlie65c2a892014-06-05 14:01:30 +1000165int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
166{
167 struct drm_fb_helper_connector **temp;
168 struct drm_fb_helper_connector *fb_helper_connector;
169
Daniel Vetterf64c5572015-08-25 15:45:13 +0200170 if (!drm_fbdev_emulation)
171 return 0;
172
Dave Airlie65c2a892014-06-05 14:01:30 +1000173 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
174 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
Damien Lespiau14f476f2014-08-08 19:15:20 +0100175 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 +1000176 if (!temp)
177 return -ENOMEM;
178
179 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
180 fb_helper->connector_info = temp;
181 }
182
183
184 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
185 if (!fb_helper_connector)
186 return -ENOMEM;
187
Thierry Redingad093602017-02-28 15:46:39 +0100188 drm_connector_get(connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000189 fb_helper_connector->connector = connector;
190 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
191 return 0;
192}
193EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
194
195int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
196 struct drm_connector *connector)
197{
198 struct drm_fb_helper_connector *fb_helper_connector;
199 int i, j;
200
Daniel Vetterf64c5572015-08-25 15:45:13 +0200201 if (!drm_fbdev_emulation)
202 return 0;
203
Dave Airlie65c2a892014-06-05 14:01:30 +1000204 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
205
206 for (i = 0; i < fb_helper->connector_count; i++) {
207 if (fb_helper->connector_info[i]->connector == connector)
208 break;
209 }
210
211 if (i == fb_helper->connector_count)
212 return -EINVAL;
213 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100214 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000215
216 for (j = i + 1; j < fb_helper->connector_count; j++) {
217 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
218 }
219 fb_helper->connector_count--;
220 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500221
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
Maarten Lankhorst7ea77282016-06-07 12:49:30 +0200253 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Jason Wessel99231022010-10-13 14:09:43 -0500254}
255
Daniel Vetter207fd322013-01-20 22:13:14 +0100256/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100257 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100258 * @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;
Stefan Christ1b99b722016-11-14 00:03:11 +0100275 if (funcs->mode_set_base_atomic == NULL)
276 continue;
277
Jason Wessel99231022010-10-13 14:09:43 -0500278 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500279 funcs->mode_set_base_atomic(mode_set->crtc,
280 mode_set->fb,
281 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500282 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500283 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500284 }
285 }
286
287 return 0;
288}
289EXPORT_SYMBOL(drm_fb_helper_debug_enter);
290
291/* Find the real fb for a given fb helper CRTC */
292static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
293{
294 struct drm_device *dev = crtc->dev;
295 struct drm_crtc *c;
296
Daniel Vetter6295d602015-07-09 23:44:25 +0200297 drm_for_each_crtc(c, dev) {
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500298 if (crtc->base.id == c->base.id)
Matt Roperf4510a22014-04-01 15:22:40 -0700299 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500300 }
301
302 return NULL;
303}
304
Daniel Vetter207fd322013-01-20 22:13:14 +0100305/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100306 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100307 * @info: fbdev registered by the helper
308 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500309int drm_fb_helper_debug_leave(struct fb_info *info)
310{
311 struct drm_fb_helper *helper = info->par;
312 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200313 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500314 struct drm_framebuffer *fb;
315 int i;
316
317 for (i = 0; i < helper->crtc_count; i++) {
318 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
319 crtc = mode_set->crtc;
320 funcs = crtc->helper_private;
321 fb = drm_mode_config_fb(crtc);
322
323 if (!crtc->enabled)
324 continue;
325
326 if (!fb) {
327 DRM_ERROR("no fb to restore??\n");
328 continue;
329 }
330
Stefan Christ1b99b722016-11-14 00:03:11 +0100331 if (funcs->mode_set_base_atomic == NULL)
332 continue;
333
Jason Wessel99231022010-10-13 14:09:43 -0500334 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500335 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500336 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500337 }
338
339 return 0;
340}
341EXPORT_SYMBOL(drm_fb_helper_debug_leave);
342
Rob Clarkbbb1e522015-08-25 15:35:58 -0400343static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper)
344{
345 struct drm_device *dev = fb_helper->dev;
346 struct drm_plane *plane;
347 struct drm_atomic_state *state;
348 int i, ret;
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100349 unsigned plane_mask;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400350
351 state = drm_atomic_state_alloc(dev);
352 if (!state)
353 return -ENOMEM;
354
355 state->acquire_ctx = dev->mode_config.acquire_ctx;
356retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100357 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400358 drm_for_each_plane(plane, dev) {
359 struct drm_plane_state *plane_state;
360
361 plane_state = drm_atomic_get_plane_state(state, plane);
362 if (IS_ERR(plane_state)) {
363 ret = PTR_ERR(plane_state);
364 goto fail;
365 }
366
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300367 plane_state->rotation = DRM_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400368
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100369 plane->old_fb = plane->fb;
370 plane_mask |= 1 << drm_plane_index(plane);
371
Rob Clarkbbb1e522015-08-25 15:35:58 -0400372 /* disable non-primary: */
373 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
374 continue;
375
376 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
377 if (ret != 0)
378 goto fail;
379 }
380
381 for(i = 0; i < fb_helper->crtc_count; i++) {
382 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
383
384 ret = __drm_atomic_helper_set_config(mode_set, state);
385 if (ret != 0)
386 goto fail;
387 }
388
389 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400390
391fail:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100392 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700393
Rob Clarkbbb1e522015-08-25 15:35:58 -0400394 if (ret == -EDEADLK)
395 goto backoff;
396
Chris Wilson08536952016-10-14 13:18:18 +0100397 drm_atomic_state_put(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400398 return ret;
399
400backoff:
401 drm_atomic_state_clear(state);
402 drm_atomic_legacy_backoff(state);
403
404 goto retry;
405}
406
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200407static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100408{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300409 struct drm_device *dev = fb_helper->dev;
410 struct drm_plane *plane;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300411 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100412
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300413 drm_warn_on_modeset_not_all_locked(dev);
414
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -0800415 if (drm_drv_uses_atomic_modeset(dev))
Rob Clarkbbb1e522015-08-25 15:35:58 -0400416 return restore_fbdev_mode_atomic(fb_helper);
417
Daniel Vetter6295d602015-07-09 23:44:25 +0200418 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700419 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
420 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100421
Ville Syrjälä6686df82016-10-21 22:22:45 +0300422 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300423 drm_mode_plane_set_obj_prop(plane,
424 plane->rotation_property,
425 DRM_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530426 }
427
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100428 for (i = 0; i < fb_helper->crtc_count; i++) {
429 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300430 struct drm_crtc *crtc = mode_set->crtc;
431 int ret;
432
Alex Deucher03f9abb2015-09-30 14:47:37 -0400433 if (crtc->funcs->cursor_set2) {
434 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
435 if (ret)
Dave Airlie48f87dd2015-10-16 10:10:32 +1000436 return ret;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400437 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300438 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
439 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200440 return ret;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300441 }
442
Daniel Vetter2d13b672012-12-11 13:47:23 +0100443 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100444 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200445 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100446 }
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200447
448 return 0;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100449}
Rob Clark5ea1f752014-05-30 12:29:48 -0400450
451/**
452 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
453 * @fb_helper: fbcon to restore
454 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100455 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400456 * when implementing an fbcon on top of kms using this helper. This ensures that
457 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200458 *
459 * RETURNS:
460 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400461 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200462int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400463{
464 struct drm_device *dev = fb_helper->dev;
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200465 bool do_delayed;
466 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000467
Daniel Vetterf64c5572015-08-25 15:45:13 +0200468 if (!drm_fbdev_emulation)
469 return -ENODEV;
470
Rob Clark5ea1f752014-05-30 12:29:48 -0400471 drm_modeset_lock_all(dev);
472 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000473
474 do_delayed = fb_helper->delayed_hotplug;
475 if (do_delayed)
476 fb_helper->delayed_hotplug = false;
Rob Clark5ea1f752014-05-30 12:29:48 -0400477 drm_modeset_unlock_all(dev);
Dave Airliee2809c72014-11-26 13:15:24 +1000478
479 if (do_delayed)
480 drm_fb_helper_hotplug_event(fb_helper);
Rob Clark5ea1f752014-05-30 12:29:48 -0400481 return ret;
482}
483EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100484
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200485static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
486{
487 struct drm_device *dev = fb_helper->dev;
488 struct drm_crtc *crtc;
489 int bound = 0, crtcs_bound = 0;
490
491 /* Sometimes user space wants everything disabled, so don't steal the
492 * display if there's a master. */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200493 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200494 return false;
495
496 drm_for_each_crtc(crtc, dev) {
497 if (crtc->primary->fb)
498 crtcs_bound++;
499 if (crtc->primary->fb == fb_helper->fb)
500 bound++;
501 }
502
503 if (bound < crtcs_bound)
504 return false;
505
506 return true;
507}
508
509#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100510/*
511 * restore fbcon display for all kms driver's using this helper, used for sysrq
512 * and panic handling.
513 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530514static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000515{
Dave Airlie785b93e2009-08-28 15:46:53 +1000516 bool ret, error = false;
517 struct drm_fb_helper *helper;
518
519 if (list_empty(&kernel_fb_helper_list))
520 return false;
521
522 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200523 struct drm_device *dev = helper->dev;
524
525 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100526 continue;
527
Daniel Vetterdd908c82015-07-28 13:18:41 +0200528 drm_modeset_lock_all(dev);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200529 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100530 if (ret)
531 error = true;
Daniel Vettercb597bb2014-07-27 19:09:33 +0200532 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000533 }
534 return error;
535}
536
Dave Airlie785b93e2009-08-28 15:46:53 +1000537static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
538{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100539 bool ret;
540 ret = drm_fb_helper_force_kernel_mode();
541 if (ret == true)
542 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000543}
544static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
545
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700546static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000547{
548 schedule_work(&drm_fb_helper_restore_work);
549}
550
551static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
552 .handler = drm_fb_helper_sysrq,
553 .help_msg = "force-fb(V)",
554 .action_msg = "Restore framebuffer console",
555};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000556#else
557static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200558#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000559
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100560static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000561{
562 struct drm_fb_helper *fb_helper = info->par;
563 struct drm_device *dev = fb_helper->dev;
564 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700565 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700566 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000567
568 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100569 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000570 */
Daniel Vetter84849902012-12-02 00:28:11 +0100571 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100572 if (!drm_fb_helper_is_bound(fb_helper)) {
573 drm_modeset_unlock_all(dev);
574 return;
575 }
576
Jesse Barnese87b2c42009-09-17 18:14:41 -0700577 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000578 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000579
Dave Airlie8be48d92010-03-30 05:34:14 +0000580 if (!crtc->enabled)
581 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000582
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100583 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000584 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700585 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200586 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500587 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100588 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700589 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000590 }
Daniel Vetter84849902012-12-02 00:28:11 +0100591 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000592}
593
Daniel Vetter207fd322013-01-20 22:13:14 +0100594/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100595 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100596 * @blank: desired blanking state
597 * @info: fbdev registered by the helper
598 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000599int drm_fb_helper_blank(int blank, struct fb_info *info)
600{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200601 if (oops_in_progress)
602 return -EBUSY;
603
Dave Airlie785b93e2009-08-28 15:46:53 +1000604 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000605 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000606 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100607 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000608 break;
James Simmons731b5a12009-10-29 20:39:07 +0000609 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000610 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100611 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000612 break;
James Simmons731b5a12009-10-29 20:39:07 +0000613 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000614 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100615 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000616 break;
James Simmons731b5a12009-10-29 20:39:07 +0000617 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000618 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100619 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000620 break;
James Simmons731b5a12009-10-29 20:39:07 +0000621 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000622 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100623 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000624 break;
625 }
626 return 0;
627}
628EXPORT_SYMBOL(drm_fb_helper_blank);
629
Ville Syrjäläa2889602016-10-26 17:41:18 +0300630static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
631 struct drm_mode_set *modeset)
632{
633 int i;
634
635 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100636 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300637 modeset->connectors[i] = NULL;
638 }
639 modeset->num_connectors = 0;
640
641 drm_mode_destroy(helper->dev, modeset->mode);
642 modeset->mode = NULL;
643
644 /* FIXME should hold a ref? */
645 modeset->fb = NULL;
646}
647
Dave Airlie785b93e2009-08-28 15:46:53 +1000648static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
649{
650 int i;
651
Dave Airlie6e86d582016-04-27 11:24:51 +1000652 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100653 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000654 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000655 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000656 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300657
Sascha Hauera1b77362012-02-01 11:38:22 +0100658 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300659 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
660
661 drm_fb_helper_modeset_release(helper, modeset);
662 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100663 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000664 kfree(helper->crtc_info);
665}
666
Noralf Trønnescfe63422016-08-23 13:54:06 +0200667static void drm_fb_helper_resume_worker(struct work_struct *work)
668{
669 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
670 resume_work);
671
672 console_lock();
673 fb_set_suspend(helper->fbdev, 0);
674 console_unlock();
675}
676
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200677static void drm_fb_helper_dirty_work(struct work_struct *work)
678{
679 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
680 dirty_work);
681 struct drm_clip_rect *clip = &helper->dirty_clip;
682 struct drm_clip_rect clip_copy;
683 unsigned long flags;
684
685 spin_lock_irqsave(&helper->dirty_lock, flags);
686 clip_copy = *clip;
687 clip->x1 = clip->y1 = ~0;
688 clip->x2 = clip->y2 = 0;
689 spin_unlock_irqrestore(&helper->dirty_lock, flags);
690
Takashi Iwai87d3b652016-10-20 17:05:30 +0200691 /* call dirty callback only when it has been really touched */
692 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
693 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200694}
695
Daniel Vetter207fd322013-01-20 22:13:14 +0100696/**
Thierry Reding10a23102014-06-27 17:19:24 +0200697 * drm_fb_helper_prepare - setup a drm_fb_helper structure
698 * @dev: DRM device
699 * @helper: driver-allocated fbdev helper structure to set up
700 * @funcs: pointer to structure of functions associate with this helper
701 *
702 * Sets up the bare minimum to make the framebuffer helper usable. This is
703 * useful to implement race-free initialization of the polling helpers.
704 */
705void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
706 const struct drm_fb_helper_funcs *funcs)
707{
708 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200709 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200710 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200711 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
712 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Reding10a23102014-06-27 17:19:24 +0200713 helper->funcs = funcs;
714 helper->dev = dev;
715}
716EXPORT_SYMBOL(drm_fb_helper_prepare);
717
718/**
Daniel Vettered84e252017-02-07 15:10:49 +0100719 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100720 * @dev: drm device
721 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100722 * @max_conn_count: max connector count
723 *
724 * This allocates the structures for the fbdev helper with the given limits.
725 * Note that this won't yet touch the hardware (through the driver interfaces)
726 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
727 * to allow driver writes more control over the exact init sequence.
728 *
Thierry Reding10a23102014-06-27 17:19:24 +0200729 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100730 *
731 * RETURNS:
732 * Zero if everything went ok, nonzero otherwise.
733 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000734int drm_fb_helper_init(struct drm_device *dev,
735 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200736 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000737{
Dave Airlie785b93e2009-08-28 15:46:53 +1000738 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200739 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000740 int i;
741
Daniel Vetterf64c5572015-08-25 15:45:13 +0200742 if (!drm_fbdev_emulation)
743 return 0;
744
Xiubo Li04cfe972014-03-10 09:33:58 +0800745 if (!max_conn_count)
746 return -EINVAL;
747
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200748 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000749 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000750 return -ENOMEM;
751
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200752 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000753 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
754 if (!fb_helper->connector_info) {
755 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000756 return -ENOMEM;
757 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000758 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000759 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000760
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200761 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000762 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000763 kcalloc(max_conn_count,
764 sizeof(struct drm_connector *),
765 GFP_KERNEL);
766
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200767 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000768 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000769 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000770 }
771
772 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200773 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000774 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000775 i++;
776 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100777
Dave Airlie785b93e2009-08-28 15:46:53 +1000778 return 0;
779out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000780 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000781 return -ENOMEM;
782}
Dave Airlie4abe3522010-03-30 05:34:18 +0000783EXPORT_SYMBOL(drm_fb_helper_init);
784
Archit Tanejab8017d62015-07-22 14:57:56 +0530785/**
786 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
787 * @fb_helper: driver-allocated fbdev helper
788 *
789 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100790 * by the driver within the fb_probe fb_helper callback function. Drivers do not
791 * need to release the allocated fb_info structure themselves, this is
792 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530793 *
794 * RETURNS:
795 * fb_info pointer if things went okay, pointer containing error code
796 * otherwise
797 */
798struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
799{
800 struct device *dev = fb_helper->dev->dev;
801 struct fb_info *info;
802 int ret;
803
804 info = framebuffer_alloc(0, dev);
805 if (!info)
806 return ERR_PTR(-ENOMEM);
807
808 ret = fb_alloc_cmap(&info->cmap, 256, 0);
809 if (ret)
810 goto err_release;
811
812 info->apertures = alloc_apertures(1);
813 if (!info->apertures) {
814 ret = -ENOMEM;
815 goto err_free_cmap;
816 }
817
818 fb_helper->fbdev = info;
819
820 return info;
821
822err_free_cmap:
823 fb_dealloc_cmap(&info->cmap);
824err_release:
825 framebuffer_release(info);
826 return ERR_PTR(ret);
827}
828EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
829
830/**
831 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
832 * @fb_helper: driver-allocated fbdev helper
833 *
834 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100835 * framebuffer device. This must be called before releasing all resources for
836 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530837 */
838void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
839{
840 if (fb_helper && fb_helper->fbdev)
841 unregister_framebuffer(fb_helper->fbdev);
842}
843EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
844
845/**
Daniel Vettered84e252017-02-07 15:10:49 +0100846 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
847 * @fb_helper: driver-allocated fbdev helper
848 *
849 * This cleans up all remaining resources associated with @fb_helper. Must be
850 * called after drm_fb_helper_unlink_fbi() was called.
851 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000852void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
853{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100854 struct fb_info *info;
855
856 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200857 return;
858
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100859 info = fb_helper->fbdev;
860 if (info) {
861 if (info->cmap.len)
862 fb_dealloc_cmap(&info->cmap);
863 framebuffer_release(info);
864 }
865 fb_helper->fbdev = NULL;
866
Chris Wilson24f76b22017-02-07 12:49:56 +0000867 cancel_work_sync(&fb_helper->resume_work);
Chris Wilsonf21b9a92017-02-07 12:49:55 +0000868 cancel_work_sync(&fb_helper->dirty_work);
869
Chris Wilsona53ca632016-11-29 12:02:17 +0000870 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000871 if (!list_empty(&fb_helper->kernel_fb_list)) {
872 list_del(&fb_helper->kernel_fb_list);
873 if (list_empty(&kernel_fb_helper_list)) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000874 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
875 }
876 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000877 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000878
879 drm_fb_helper_crtc_free(fb_helper);
880
Dave Airlie4abe3522010-03-30 05:34:18 +0000881}
882EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000883
Archit Taneja47074ab2015-07-22 14:57:57 +0530884/**
885 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
886 * @fb_helper: driver-allocated fbdev helper
887 *
888 * A wrapper around unlink_framebuffer implemented by fbdev core
889 */
890void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
891{
892 if (fb_helper && fb_helper->fbdev)
893 unlink_framebuffer(fb_helper->fbdev);
894}
895EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
896
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200897static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
898 u32 width, u32 height)
899{
900 struct drm_fb_helper *helper = info->par;
901 struct drm_clip_rect *clip = &helper->dirty_clip;
902 unsigned long flags;
903
904 if (!helper->fb->funcs->dirty)
905 return;
906
907 spin_lock_irqsave(&helper->dirty_lock, flags);
908 clip->x1 = min_t(u32, clip->x1, x);
909 clip->y1 = min_t(u32, clip->y1, y);
910 clip->x2 = max_t(u32, clip->x2, x + width);
911 clip->y2 = max_t(u32, clip->y2, y + height);
912 spin_unlock_irqrestore(&helper->dirty_lock, flags);
913
914 schedule_work(&helper->dirty_work);
915}
916
917/**
918 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
919 * @info: fb_info struct pointer
920 * @pagelist: list of dirty mmap framebuffer pages
921 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100922 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200923 * callback function for flushing the fbdev mmap writes.
924 */
925void drm_fb_helper_deferred_io(struct fb_info *info,
926 struct list_head *pagelist)
927{
928 unsigned long start, end, min, max;
929 struct page *page;
930 u32 y1, y2;
931
932 min = ULONG_MAX;
933 max = 0;
934 list_for_each_entry(page, pagelist, lru) {
935 start = page->index << PAGE_SHIFT;
936 end = start + PAGE_SIZE - 1;
937 min = min(min, start);
938 max = max(max, end);
939 }
940
941 if (min < max) {
942 y1 = min / info->fix.line_length;
943 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
944 info->var.yres);
945 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
946 }
947}
948EXPORT_SYMBOL(drm_fb_helper_deferred_io);
949
Archit Tanejacbb1a822015-07-31 16:21:41 +0530950/**
951 * drm_fb_helper_sys_read - wrapper around fb_sys_read
952 * @info: fb_info struct pointer
953 * @buf: userspace buffer to read from framebuffer memory
954 * @count: number of bytes to read from framebuffer memory
955 * @ppos: read offset within framebuffer memory
956 *
957 * A wrapper around fb_sys_read implemented by fbdev core
958 */
959ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
960 size_t count, loff_t *ppos)
961{
962 return fb_sys_read(info, buf, count, ppos);
963}
964EXPORT_SYMBOL(drm_fb_helper_sys_read);
965
966/**
967 * drm_fb_helper_sys_write - wrapper around fb_sys_write
968 * @info: fb_info struct pointer
969 * @buf: userspace buffer to write to framebuffer memory
970 * @count: number of bytes to write to framebuffer memory
971 * @ppos: write offset within framebuffer memory
972 *
973 * A wrapper around fb_sys_write implemented by fbdev core
974 */
975ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
976 size_t count, loff_t *ppos)
977{
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200978 ssize_t ret;
979
980 ret = fb_sys_write(info, buf, count, ppos);
981 if (ret > 0)
982 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
983 info->var.yres);
984
985 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +0530986}
987EXPORT_SYMBOL(drm_fb_helper_sys_write);
988
Archit Taneja742547b2015-07-31 16:21:42 +0530989/**
990 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
991 * @info: fbdev registered by the helper
992 * @rect: info about rectangle to fill
993 *
994 * A wrapper around sys_fillrect implemented by fbdev core
995 */
996void drm_fb_helper_sys_fillrect(struct fb_info *info,
997 const struct fb_fillrect *rect)
998{
999 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001000 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1001 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301002}
1003EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1004
1005/**
1006 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1007 * @info: fbdev registered by the helper
1008 * @area: info about area to copy
1009 *
1010 * A wrapper around sys_copyarea implemented by fbdev core
1011 */
1012void drm_fb_helper_sys_copyarea(struct fb_info *info,
1013 const struct fb_copyarea *area)
1014{
1015 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001016 drm_fb_helper_dirty(info, area->dx, area->dy,
1017 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301018}
1019EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1020
1021/**
1022 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1023 * @info: fbdev registered by the helper
1024 * @image: info about image to blit
1025 *
1026 * A wrapper around sys_imageblit implemented by fbdev core
1027 */
1028void drm_fb_helper_sys_imageblit(struct fb_info *info,
1029 const struct fb_image *image)
1030{
1031 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001032 drm_fb_helper_dirty(info, image->dx, image->dy,
1033 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301034}
1035EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1036
1037/**
1038 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1039 * @info: fbdev registered by the helper
1040 * @rect: info about rectangle to fill
1041 *
1042 * A wrapper around cfb_imageblit implemented by fbdev core
1043 */
1044void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1045 const struct fb_fillrect *rect)
1046{
1047 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001048 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1049 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301050}
1051EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1052
1053/**
1054 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1055 * @info: fbdev registered by the helper
1056 * @area: info about area to copy
1057 *
1058 * A wrapper around cfb_copyarea implemented by fbdev core
1059 */
1060void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1061 const struct fb_copyarea *area)
1062{
1063 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001064 drm_fb_helper_dirty(info, area->dx, area->dy,
1065 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301066}
1067EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1068
1069/**
1070 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1071 * @info: fbdev registered by the helper
1072 * @image: info about image to blit
1073 *
1074 * A wrapper around cfb_imageblit implemented by fbdev core
1075 */
1076void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1077 const struct fb_image *image)
1078{
1079 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001080 drm_fb_helper_dirty(info, image->dx, image->dy,
1081 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301082}
1083EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1084
Archit Tanejafdefa582015-07-31 16:21:43 +05301085/**
1086 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1087 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001088 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301089 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001090 * A wrapper around fb_set_suspend implemented by fbdev core.
1091 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1092 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301093 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001094void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301095{
1096 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001097 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301098}
1099EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1100
Noralf Trønnescfe63422016-08-23 13:54:06 +02001101/**
1102 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1103 * takes the console lock
1104 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001105 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001106 *
1107 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1108 * isn't available on resume, a worker is tasked with waiting for the lock
1109 * to become available. The console lock can be pretty contented on resume
1110 * due to all the printk activity.
1111 *
1112 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001113 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001114 *
1115 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1116 */
1117void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001118 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001119{
1120 if (!fb_helper || !fb_helper->fbdev)
1121 return;
1122
1123 /* make sure there's no pending/ongoing resume */
1124 flush_work(&fb_helper->resume_work);
1125
1126 if (suspend) {
1127 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1128 return;
1129
1130 console_lock();
1131
1132 } else {
1133 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1134 return;
1135
1136 if (!console_trylock()) {
1137 schedule_work(&fb_helper->resume_work);
1138 return;
1139 }
1140 }
1141
1142 fb_set_suspend(fb_helper->fbdev, suspend);
1143 console_unlock();
1144}
1145EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1146
Dave Airliec850cb72009-10-23 18:49:03 +10001147static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001148 u16 blue, u16 regno, struct fb_info *info)
1149{
1150 struct drm_fb_helper *fb_helper = info->par;
1151 struct drm_framebuffer *fb = fb_helper->fb;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001152
Dave Airliec850cb72009-10-23 18:49:03 +10001153 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
1154 u32 *palette;
1155 u32 value;
1156 /* place color in psuedopalette */
1157 if (regno > 16)
1158 return -EINVAL;
1159 palette = (u32 *)info->pseudo_palette;
1160 red >>= (16 - info->var.red.length);
1161 green >>= (16 - info->var.green.length);
1162 blue >>= (16 - info->var.blue.length);
1163 value = (red << info->var.red.offset) |
1164 (green << info->var.green.offset) |
1165 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +00001166 if (info->var.transp.length > 0) {
1167 u32 mask = (1 << info->var.transp.length) - 1;
1168 mask <<= info->var.transp.offset;
1169 value |= mask;
1170 }
Dave Airliec850cb72009-10-23 18:49:03 +10001171 palette[regno] = value;
1172 return 0;
1173 }
1174
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001175 /*
1176 * The driver really shouldn't advertise pseudo/directcolor
1177 * visuals if it can't deal with the palette.
1178 */
1179 if (WARN_ON(!fb_helper->funcs->gamma_set ||
1180 !fb_helper->funcs->gamma_get))
1181 return -EINVAL;
1182
Ville Syrjälä272725c2016-12-14 23:32:20 +02001183 WARN_ON(fb->format->cpp[0] != 1);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001184
Daniel Vetterfef14802016-03-30 11:51:17 +02001185 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001186
Dave Airliec850cb72009-10-23 18:49:03 +10001187 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001188}
1189
Daniel Vetter207fd322013-01-20 22:13:14 +01001190/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001191 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
Daniel Vetter207fd322013-01-20 22:13:14 +01001192 * @cmap: cmap to set
1193 * @info: fbdev registered by the helper
1194 */
Dave Airlie068143d2009-10-05 09:58:02 +10001195int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1196{
1197 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001198 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +02001199 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +10001200 u16 *red, *green, *blue, *transp;
1201 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +01001202 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001203 int start;
1204
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001205 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001206 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001207
1208 drm_modeset_lock_all(dev);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001209 if (!drm_fb_helper_is_bound(fb_helper)) {
1210 drm_modeset_unlock_all(dev);
1211 return -EBUSY;
1212 }
1213
Dave Airlie8be48d92010-03-30 05:34:14 +00001214 for (i = 0; i < fb_helper->crtc_count; i++) {
1215 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1216 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +10001217
1218 red = cmap->red;
1219 green = cmap->green;
1220 blue = cmap->blue;
1221 transp = cmap->transp;
1222 start = cmap->start;
1223
roel062ac622011-03-07 18:00:34 +01001224 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +10001225 u16 hred, hgreen, hblue, htransp = 0xffff;
1226
1227 hred = *red++;
1228 hgreen = *green++;
1229 hblue = *blue++;
1230
1231 if (transp)
1232 htransp = *transp++;
1233
Dave Airliec850cb72009-10-23 18:49:03 +10001234 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1235 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001236 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +10001237 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001238 if (crtc_funcs->load_lut)
1239 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +10001240 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001241 out:
1242 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +10001243 return rc;
1244}
1245EXPORT_SYMBOL(drm_fb_helper_setcmap);
1246
Daniel Vetter207fd322013-01-20 22:13:14 +01001247/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001248 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001249 * @var: screeninfo to check
1250 * @info: fbdev registered by the helper
1251 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001252int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1253 struct fb_info *info)
1254{
1255 struct drm_fb_helper *fb_helper = info->par;
1256 struct drm_framebuffer *fb = fb_helper->fb;
1257 int depth;
1258
Jason Wesself90ebd92010-08-05 09:22:32 -05001259 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001260 return -EINVAL;
1261
Stefan Agner865afb12016-10-11 16:15:04 -07001262 /*
1263 * Changes struct fb_var_screeninfo are currently not pushed back
1264 * to KMS, hence fail if different settings are requested.
1265 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001266 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Stefan Agner865afb12016-10-11 16:15:04 -07001267 var->xres != fb->width || var->yres != fb->height ||
1268 var->xres_virtual != fb->width || var->yres_virtual != fb->height) {
1269 DRM_DEBUG("fb userspace requested width/height/bpp different than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001270 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1271 var->xres, var->yres, var->bits_per_pixel,
1272 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001273 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001274 return -EINVAL;
1275 }
1276
1277 switch (var->bits_per_pixel) {
1278 case 16:
1279 depth = (var->green.length == 6) ? 16 : 15;
1280 break;
1281 case 32:
1282 depth = (var->transp.length > 0) ? 32 : 24;
1283 break;
1284 default:
1285 depth = var->bits_per_pixel;
1286 break;
1287 }
1288
1289 switch (depth) {
1290 case 8:
1291 var->red.offset = 0;
1292 var->green.offset = 0;
1293 var->blue.offset = 0;
1294 var->red.length = 8;
1295 var->green.length = 8;
1296 var->blue.length = 8;
1297 var->transp.length = 0;
1298 var->transp.offset = 0;
1299 break;
1300 case 15:
1301 var->red.offset = 10;
1302 var->green.offset = 5;
1303 var->blue.offset = 0;
1304 var->red.length = 5;
1305 var->green.length = 5;
1306 var->blue.length = 5;
1307 var->transp.length = 1;
1308 var->transp.offset = 15;
1309 break;
1310 case 16:
1311 var->red.offset = 11;
1312 var->green.offset = 5;
1313 var->blue.offset = 0;
1314 var->red.length = 5;
1315 var->green.length = 6;
1316 var->blue.length = 5;
1317 var->transp.length = 0;
1318 var->transp.offset = 0;
1319 break;
1320 case 24:
1321 var->red.offset = 16;
1322 var->green.offset = 8;
1323 var->blue.offset = 0;
1324 var->red.length = 8;
1325 var->green.length = 8;
1326 var->blue.length = 8;
1327 var->transp.length = 0;
1328 var->transp.offset = 0;
1329 break;
1330 case 32:
1331 var->red.offset = 16;
1332 var->green.offset = 8;
1333 var->blue.offset = 0;
1334 var->red.length = 8;
1335 var->green.length = 8;
1336 var->blue.length = 8;
1337 var->transp.length = 8;
1338 var->transp.offset = 24;
1339 break;
1340 default:
1341 return -EINVAL;
1342 }
1343 return 0;
1344}
1345EXPORT_SYMBOL(drm_fb_helper_check_var);
1346
Daniel Vetter207fd322013-01-20 22:13:14 +01001347/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001348 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001349 * @info: fbdev registered by the helper
1350 *
1351 * This will let fbcon do the mode init and is called at initialization time by
1352 * the fbdev core when registering the driver, and later on through the hotplug
1353 * callback.
1354 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001355int drm_fb_helper_set_par(struct fb_info *info)
1356{
1357 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001358 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001359
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001360 if (oops_in_progress)
1361 return -EBUSY;
1362
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001363 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001364 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001365 return -EINVAL;
1366 }
1367
Rob Clark5ea1f752014-05-30 12:29:48 -04001368 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001369
Dave Airlie785b93e2009-08-28 15:46:53 +10001370 return 0;
1371}
1372EXPORT_SYMBOL(drm_fb_helper_set_par);
1373
Rob Clark1edf0262015-08-25 15:35:59 -04001374static int pan_display_atomic(struct fb_var_screeninfo *var,
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001375 struct fb_info *info)
Rob Clark1edf0262015-08-25 15:35:59 -04001376{
1377 struct drm_fb_helper *fb_helper = info->par;
1378 struct drm_device *dev = fb_helper->dev;
1379 struct drm_atomic_state *state;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001380 struct drm_plane *plane;
Rob Clark1edf0262015-08-25 15:35:59 -04001381 int i, ret;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001382 unsigned plane_mask;
Rob Clark1edf0262015-08-25 15:35:59 -04001383
1384 state = drm_atomic_state_alloc(dev);
1385 if (!state)
1386 return -ENOMEM;
1387
1388 state->acquire_ctx = dev->mode_config.acquire_ctx;
1389retry:
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001390 plane_mask = 0;
Rob Clark1edf0262015-08-25 15:35:59 -04001391 for(i = 0; i < fb_helper->crtc_count; i++) {
1392 struct drm_mode_set *mode_set;
1393
1394 mode_set = &fb_helper->crtc_info[i].mode_set;
1395
1396 mode_set->x = var->xoffset;
1397 mode_set->y = var->yoffset;
1398
1399 ret = __drm_atomic_helper_set_config(mode_set, state);
1400 if (ret != 0)
1401 goto fail;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001402
1403 plane = mode_set->crtc->primary;
Matt Roper7118fd92015-12-18 17:27:01 -08001404 plane_mask |= (1 << drm_plane_index(plane));
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001405 plane->old_fb = plane->fb;
Rob Clark1edf0262015-08-25 15:35:59 -04001406 }
1407
1408 ret = drm_atomic_commit(state);
1409 if (ret != 0)
1410 goto fail;
1411
1412 info->var.xoffset = var->xoffset;
1413 info->var.yoffset = var->yoffset;
1414
Rob Clark1edf0262015-08-25 15:35:59 -04001415fail:
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001416 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001417
Rob Clark1edf0262015-08-25 15:35:59 -04001418 if (ret == -EDEADLK)
1419 goto backoff;
1420
Chris Wilson08536952016-10-14 13:18:18 +01001421 drm_atomic_state_put(state);
Rob Clark1edf0262015-08-25 15:35:59 -04001422 return ret;
1423
1424backoff:
1425 drm_atomic_state_clear(state);
1426 drm_atomic_legacy_backoff(state);
1427
1428 goto retry;
1429}
1430
Daniel Vetter207fd322013-01-20 22:13:14 +01001431/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001432 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
Daniel Vetter207fd322013-01-20 22:13:14 +01001433 * @var: updated screen information
1434 * @info: fbdev registered by the helper
1435 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001436int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1437 struct fb_info *info)
1438{
1439 struct drm_fb_helper *fb_helper = info->par;
1440 struct drm_device *dev = fb_helper->dev;
1441 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001442 int ret = 0;
1443 int i;
1444
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001445 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001446 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001447
1448 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +01001449 if (!drm_fb_helper_is_bound(fb_helper)) {
1450 drm_modeset_unlock_all(dev);
1451 return -EBUSY;
1452 }
1453
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08001454 if (drm_drv_uses_atomic_modeset(dev)) {
Rob Clark1edf0262015-08-25 15:35:59 -04001455 ret = pan_display_atomic(var, info);
1456 goto unlock;
1457 }
1458
Dave Airlie8be48d92010-03-30 05:34:14 +00001459 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001460 modeset = &fb_helper->crtc_info[i].mode_set;
1461
1462 modeset->x = var->xoffset;
1463 modeset->y = var->yoffset;
1464
1465 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001466 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001467 if (!ret) {
1468 info->var.xoffset = var->xoffset;
1469 info->var.yoffset = var->yoffset;
1470 }
1471 }
1472 }
Rob Clark1edf0262015-08-25 15:35:59 -04001473unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001474 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +10001475 return ret;
1476}
1477EXPORT_SYMBOL(drm_fb_helper_pan_display);
1478
Daniel Vetter8acf6582013-01-21 23:38:37 +01001479/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001480 * Allocates the backing storage and sets up the fbdev info structure through
1481 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1482 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001483 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001484static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1485 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001486{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001487 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001488 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001489 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001490 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001491 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001492
1493 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1494 sizes.surface_depth = 24;
1495 sizes.surface_bpp = 32;
1496 sizes.fb_width = (unsigned)-1;
1497 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001498
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001499 /* if driver picks 8 or 16 by default use that
1500 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001501 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001502 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001503
Dave Airlie785b93e2009-08-28 15:46:53 +10001504 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001505 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001506 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001507 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001508
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001509 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001510
1511 if (cmdline_mode->bpp_specified) {
1512 switch (cmdline_mode->bpp) {
1513 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001514 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001515 break;
1516 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001517 sizes.surface_depth = 15;
1518 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001519 break;
1520 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001521 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001522 break;
1523 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001524 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001525 break;
1526 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001527 sizes.surface_depth = 24;
1528 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001529 break;
1530 }
1531 break;
1532 }
1533 }
1534
Dave Airlie8be48d92010-03-30 05:34:14 +00001535 crtc_count = 0;
1536 for (i = 0; i < fb_helper->crtc_count; i++) {
1537 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001538 struct drm_mode_set *mode_set;
1539 int x, y, j;
1540 /* in case of tile group, are we the last tile vert or horiz?
1541 * If no tile group you are always the last one both vertically
1542 * and horizontally
1543 */
1544 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001545
Dave Airlie8be48d92010-03-30 05:34:14 +00001546 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001547 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001548
1549 if (!desired_mode)
1550 continue;
1551
1552 crtc_count++;
1553
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001554 x = fb_helper->crtc_info[i].x;
1555 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001556
1557 if (gamma_size == 0)
1558 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1559
1560 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1561 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001562
1563 for (j = 0; j < mode_set->num_connectors; j++) {
1564 struct drm_connector *connector = mode_set->connectors[j];
1565 if (connector->has_tile) {
1566 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1567 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1568 /* cloning to multiple tiles is just crazy-talk, so: */
1569 break;
1570 }
1571 }
1572
1573 if (lasth)
1574 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1575 if (lastv)
1576 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001577 }
1578
Dave Airlie38651672010-03-30 05:34:13 +00001579 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001580 /* hmm everyone went away - assume VGA cable just fell out
1581 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001582 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001583 sizes.fb_width = sizes.surface_width = 1024;
1584 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001585 }
1586
Xinliang Liu5f152572017-02-15 17:19:08 +01001587 /* Handle our overallocation */
1588 sizes.surface_height *= drm_fbdev_overalloc;
1589 sizes.surface_height /= 100;
1590
Dave Airlie38651672010-03-30 05:34:13 +00001591 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001592 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1593 if (ret < 0)
1594 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001595
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001596 /*
1597 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1598 * events, but at init time drm_setup_crtcs needs to be called before
1599 * the fb is allocated (since we need to figure out the desired size of
1600 * the fb before we can allocate it ...). Hence we need to fix things up
1601 * here again.
1602 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001603 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001604 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1605 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1606
Dave Airlie785b93e2009-08-28 15:46:53 +10001607 return 0;
1608}
Dave Airlie785b93e2009-08-28 15:46:53 +10001609
Daniel Vetter207fd322013-01-20 22:13:14 +01001610/**
1611 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1612 * @info: fbdev registered by the helper
1613 * @pitch: desired pitch
1614 * @depth: desired depth
1615 *
1616 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1617 * fbdev emulations. Drivers which support acceleration methods which impose
1618 * additional constraints need to set up their own limits.
1619 *
1620 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001621 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001622 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001623void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1624 uint32_t depth)
1625{
1626 info->fix.type = FB_TYPE_PACKED_PIXELS;
1627 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1628 FB_VISUAL_TRUECOLOR;
1629 info->fix.mmio_start = 0;
1630 info->fix.mmio_len = 0;
1631 info->fix.type_aux = 0;
1632 info->fix.xpanstep = 1; /* doing it in hw */
1633 info->fix.ypanstep = 1; /* doing it in hw */
1634 info->fix.ywrapstep = 0;
1635 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001636
1637 info->fix.line_length = pitch;
1638 return;
1639}
1640EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1641
Daniel Vetter207fd322013-01-20 22:13:14 +01001642/**
1643 * drm_fb_helper_fill_var - initalizes variable fbdev information
1644 * @info: fbdev instance to set up
1645 * @fb_helper: fb helper instance to use as template
1646 * @fb_width: desired fb width
1647 * @fb_height: desired fb height
1648 *
1649 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001650 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001651 *
1652 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001653 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1654 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001655 */
Dave Airlie38651672010-03-30 05:34:13 +00001656void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001657 uint32_t fb_width, uint32_t fb_height)
1658{
Dave Airlie38651672010-03-30 05:34:13 +00001659 struct drm_framebuffer *fb = fb_helper->fb;
1660 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001661 info->var.xres_virtual = fb->width;
1662 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001663 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001664 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001665 info->var.xoffset = 0;
1666 info->var.yoffset = 0;
1667 info->var.activate = FB_ACTIVATE_NOW;
1668 info->var.height = -1;
1669 info->var.width = -1;
1670
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001671 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001672 case 8:
1673 info->var.red.offset = 0;
1674 info->var.green.offset = 0;
1675 info->var.blue.offset = 0;
1676 info->var.red.length = 8; /* 8bit DAC */
1677 info->var.green.length = 8;
1678 info->var.blue.length = 8;
1679 info->var.transp.offset = 0;
1680 info->var.transp.length = 0;
1681 break;
1682 case 15:
1683 info->var.red.offset = 10;
1684 info->var.green.offset = 5;
1685 info->var.blue.offset = 0;
1686 info->var.red.length = 5;
1687 info->var.green.length = 5;
1688 info->var.blue.length = 5;
1689 info->var.transp.offset = 15;
1690 info->var.transp.length = 1;
1691 break;
1692 case 16:
1693 info->var.red.offset = 11;
1694 info->var.green.offset = 5;
1695 info->var.blue.offset = 0;
1696 info->var.red.length = 5;
1697 info->var.green.length = 6;
1698 info->var.blue.length = 5;
1699 info->var.transp.offset = 0;
1700 break;
1701 case 24:
1702 info->var.red.offset = 16;
1703 info->var.green.offset = 8;
1704 info->var.blue.offset = 0;
1705 info->var.red.length = 8;
1706 info->var.green.length = 8;
1707 info->var.blue.length = 8;
1708 info->var.transp.offset = 0;
1709 info->var.transp.length = 0;
1710 break;
1711 case 32:
1712 info->var.red.offset = 16;
1713 info->var.green.offset = 8;
1714 info->var.blue.offset = 0;
1715 info->var.red.length = 8;
1716 info->var.green.length = 8;
1717 info->var.blue.length = 8;
1718 info->var.transp.offset = 24;
1719 info->var.transp.length = 8;
1720 break;
1721 default:
1722 break;
1723 }
1724
1725 info->var.xres = fb_width;
1726 info->var.yres = fb_height;
1727}
1728EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001729
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001730static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1731 uint32_t maxX,
1732 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001733{
1734 struct drm_connector *connector;
1735 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001736 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001737
Chris Wilson966a6a12016-11-29 12:02:15 +00001738 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001739 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001740 count += connector->funcs->fill_modes(connector, maxX, maxY);
1741 }
1742
1743 return count;
1744}
1745
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001746struct 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 +00001747{
1748 struct drm_display_mode *mode;
1749
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001750 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001751 if (mode->hdisplay > width ||
1752 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001753 continue;
1754 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1755 return mode;
1756 }
1757 return NULL;
1758}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001759EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001760
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001761static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001762{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001763 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001764}
1765
Vincent Abrioua09759e2017-01-06 17:44:43 +01001766struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00001767{
Chris Wilson1794d252011-04-17 07:43:32 +01001768 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001769 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001770 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001771
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001772 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001773 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001774 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001775
1776 /* attempt to find a matching mode in the list of modes
1777 * we have gotten so far, if not add a CVT mode that conforms
1778 */
1779 if (cmdline_mode->rb || cmdline_mode->margins)
1780 goto create_mode;
1781
Takashi Iwaic683f422014-03-19 14:53:13 +01001782 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001783again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001784 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001785 /* check width/height */
1786 if (mode->hdisplay != cmdline_mode->xres ||
1787 mode->vdisplay != cmdline_mode->yres)
1788 continue;
1789
1790 if (cmdline_mode->refresh_specified) {
1791 if (mode->vrefresh != cmdline_mode->refresh)
1792 continue;
1793 }
1794
1795 if (cmdline_mode->interlace) {
1796 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1797 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001798 } else if (prefer_non_interlace) {
1799 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1800 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001801 }
1802 return mode;
1803 }
1804
Takashi Iwaic683f422014-03-19 14:53:13 +01001805 if (prefer_non_interlace) {
1806 prefer_non_interlace = false;
1807 goto again;
1808 }
1809
Dave Airlie38651672010-03-30 05:34:13 +00001810create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001811 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1812 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001813 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001814 return mode;
1815}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001816EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001817
1818static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1819{
1820 bool enable;
1821
Sachin Kamat96081cd2012-11-15 03:43:30 +00001822 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001823 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001824 else
Dave Airlie38651672010-03-30 05:34:13 +00001825 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001826
Dave Airlie38651672010-03-30 05:34:13 +00001827 return enable;
1828}
1829
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001830static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1831 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001832{
1833 bool any_enabled = false;
1834 struct drm_connector *connector;
1835 int i = 0;
1836
Chris Wilson966a6a12016-11-29 12:02:15 +00001837 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001838 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001839 enabled[i] = drm_connector_enabled(connector, true);
1840 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1841 enabled[i] ? "yes" : "no");
1842 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001843 }
1844
1845 if (any_enabled)
1846 return;
1847
Chris Wilson966a6a12016-11-29 12:02:15 +00001848 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001849 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001850 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001851 }
1852}
1853
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001854static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1855 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001856 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001857 bool *enabled, int width, int height)
1858{
1859 int count, i, j;
1860 bool can_clone = false;
1861 struct drm_fb_helper_connector *fb_helper_conn;
1862 struct drm_display_mode *dmt_mode, *mode;
1863
1864 /* only contemplate cloning in the single crtc case */
1865 if (fb_helper->crtc_count > 1)
1866 return false;
1867
1868 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00001869 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001870 if (enabled[i])
1871 count++;
1872 }
1873
1874 /* only contemplate cloning if more than one connector is enabled */
1875 if (count <= 1)
1876 return false;
1877
1878 /* check the command line or if nothing common pick 1024x768 */
1879 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00001880 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001881 if (!enabled[i])
1882 continue;
1883 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01001884 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001885 if (!modes[i]) {
1886 can_clone = false;
1887 break;
1888 }
1889 for (j = 0; j < i; j++) {
1890 if (!enabled[j])
1891 continue;
1892 if (!drm_mode_equal(modes[j], modes[i]))
1893 can_clone = false;
1894 }
1895 }
1896
1897 if (can_clone) {
1898 DRM_DEBUG_KMS("can clone using command line\n");
1899 return true;
1900 }
1901
1902 /* try and find a 1024x768 mode on each connector */
1903 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001904 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001905
Chris Wilson966a6a12016-11-29 12:02:15 +00001906 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001907 if (!enabled[i])
1908 continue;
1909
1910 fb_helper_conn = fb_helper->connector_info[i];
1911 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1912 if (drm_mode_equal(mode, dmt_mode))
1913 modes[i] = mode;
1914 }
1915 if (!modes[i])
1916 can_clone = false;
1917 }
1918
1919 if (can_clone) {
1920 DRM_DEBUG_KMS("can clone using 1024x768\n");
1921 return true;
1922 }
1923 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1924 return false;
1925}
1926
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001927static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1928 struct drm_display_mode **modes,
1929 struct drm_fb_offset *offsets,
1930 int idx,
1931 int h_idx, int v_idx)
1932{
1933 struct drm_fb_helper_connector *fb_helper_conn;
1934 int i;
1935 int hoffset = 0, voffset = 0;
1936
Chris Wilson966a6a12016-11-29 12:02:15 +00001937 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001938 fb_helper_conn = fb_helper->connector_info[i];
1939 if (!fb_helper_conn->connector->has_tile)
1940 continue;
1941
1942 if (!modes[i] && (h_idx || v_idx)) {
1943 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1944 fb_helper_conn->connector->base.id);
1945 continue;
1946 }
1947 if (fb_helper_conn->connector->tile_h_loc < h_idx)
1948 hoffset += modes[i]->hdisplay;
1949
1950 if (fb_helper_conn->connector->tile_v_loc < v_idx)
1951 voffset += modes[i]->vdisplay;
1952 }
1953 offsets[idx].x = hoffset;
1954 offsets[idx].y = voffset;
1955 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1956 return 0;
1957}
1958
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001959static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001960 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001961 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00001962 bool *enabled, int width, int height)
1963{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001964 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00001965 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
1966 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001967 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00001968 int i;
1969
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001970retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00001971 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001972 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001973
Chris Wilsonc96521e2016-11-27 17:09:10 +00001974 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00001975 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001976
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001977 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00001978 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001979 continue;
1980 }
1981
1982 /* first pass over all the untiled connectors */
1983 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1984 continue;
1985
1986 if (tile_pass == 1) {
1987 if (fb_helper_conn->connector->tile_h_loc != 0 ||
1988 fb_helper_conn->connector->tile_v_loc != 0)
1989 continue;
1990
1991 } else {
1992 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1993 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1994 /* if this tile_pass doesn't cover any of the tiles - keep going */
1995 continue;
1996
1997 /* find the tile offsets for this pass - need
1998 to find all tiles left and above */
1999 drm_get_tile_offsets(fb_helper, modes, offsets,
2000 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2001 }
Dave Airlie38651672010-03-30 05:34:13 +00002002 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002003 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002004
2005 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002006 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002007 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002008 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2009 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 +00002010 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002011 }
2012 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002013 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2014 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002015 break;
2016 }
2017 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2018 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002019 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002020 }
2021
2022 if ((conn_configured & mask) != mask) {
2023 tile_pass++;
2024 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002025 }
2026 return true;
2027}
2028
Dave Airlie8be48d92010-03-30 05:34:14 +00002029static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2030 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002031 struct drm_display_mode **modes,
2032 int n, int width, int height)
2033{
2034 int c, o;
2035 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002036 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002037 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002038 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002039 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002040 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002041
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002042 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002043 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002044
2045 fb_helper_conn = fb_helper->connector_info[n];
2046 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002047
2048 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002049 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002050 if (modes[n] == NULL)
2051 return best_score;
2052
Lyude255f0e72016-05-12 10:56:59 -04002053 crtcs = kzalloc(fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002054 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002055 if (!crtcs)
2056 return best_score;
2057
2058 my_score = 1;
2059 if (connector->status == connector_status_connected)
2060 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002061 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002062 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002063 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002064 my_score++;
2065
2066 connector_funcs = connector->helper_private;
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002067
2068 /*
2069 * If the DRM device implements atomic hooks and ->best_encoder() is
2070 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2071 * helper.
2072 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002073 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002074 !connector_funcs->best_encoder)
2075 encoder = drm_atomic_helper_best_encoder(connector);
2076 else
2077 encoder = connector_funcs->best_encoder(connector);
2078
Dave Airlie38651672010-03-30 05:34:13 +00002079 if (!encoder)
2080 goto out;
2081
Dave Airlie38651672010-03-30 05:34:13 +00002082 /* select a crtc for this connector and then attempt to configure
2083 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00002084 for (c = 0; c < fb_helper->crtc_count; c++) {
2085 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002086
Sachin Kamat96081cd2012-11-15 03:43:30 +00002087 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002088 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002089
2090 for (o = 0; o < n; o++)
2091 if (best_crtcs[o] == crtc)
2092 break;
2093
2094 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002095 /* ignore cloning unless only a single crtc */
2096 if (fb_helper->crtc_count > 1)
2097 continue;
2098
2099 if (!drm_mode_equal(modes[o], modes[n]))
2100 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002101 }
2102
2103 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002104 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2105 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002106 width, height);
2107 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002108 best_score = score;
2109 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002110 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002111 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002112 }
Dave Airlie38651672010-03-30 05:34:13 +00002113 }
2114out:
2115 kfree(crtcs);
2116 return best_score;
2117}
2118
Chris Wilson64e94402016-11-29 12:02:16 +00002119static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2120 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002121{
Dave Airlie8be48d92010-03-30 05:34:14 +00002122 struct drm_device *dev = fb_helper->dev;
2123 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002124 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002125 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002126 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002127 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002128
2129 DRM_DEBUG_KMS("\n");
Chris Wilson64e94402016-11-29 12:02:16 +00002130 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2131 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie38651672010-03-30 05:34:13 +00002132
Chris Wilson966a6a12016-11-29 12:02:15 +00002133 /* prevent concurrent modification of connector_count by hotplug */
2134 lockdep_assert_held(&fb_helper->dev->mode_config.mutex);
2135
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002136 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002137 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002138 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002139 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002140 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002141 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002142 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002143 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002144 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002145 DRM_ERROR("Memory allocation failed\n");
2146 goto out;
2147 }
2148
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002149 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002150
Jesse Barnes11e17a02013-02-19 13:31:39 -08002151 if (!(fb_helper->funcs->initial_config &&
2152 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002153 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002154 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002155 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2156 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2157 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002158
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002159 if (!drm_target_cloned(fb_helper, modes, offsets,
2160 enabled, width, height) &&
2161 !drm_target_preferred(fb_helper, modes, offsets,
2162 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002163 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002164
2165 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2166 width, height);
2167
2168 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002169 }
Dave Airlie38651672010-03-30 05:34:13 +00002170
Dave Airlie8be48d92010-03-30 05:34:14 +00002171 /* need to set the modesets up here for use later */
2172 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002173 for (i = 0; i < fb_helper->crtc_count; i++)
2174 drm_fb_helper_modeset_release(fb_helper,
2175 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002176
Chris Wilson966a6a12016-11-29 12:02:15 +00002177 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002178 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002179 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002180 struct drm_fb_offset *offset = &offsets[i];
Ville Syrjäläa2889602016-10-26 17:41:18 +03002181 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00002182
Dave Airlie8be48d92010-03-30 05:34:14 +00002183 if (mode && fb_crtc) {
Ville Syrjäläa2889602016-10-26 17:41:18 +03002184 struct drm_connector *connector =
2185 fb_helper->connector_info[i]->connector;
2186
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002187 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2188 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002189
Dave Airlie8be48d92010-03-30 05:34:14 +00002190 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002191 fb_crtc->x = offset->x;
2192 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002193 modeset->mode = drm_mode_duplicate(dev,
2194 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002195 drm_connector_get(connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002196 modeset->connectors[modeset->num_connectors++] = connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002197 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002198 modeset->x = offset->x;
2199 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002200 }
Dave Airlie38651672010-03-30 05:34:13 +00002201 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002202out:
Dave Airlie38651672010-03-30 05:34:13 +00002203 kfree(crtcs);
2204 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002205 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002206 kfree(enabled);
2207}
2208
2209/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002210 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002211 * @fb_helper: fb_helper device struct
2212 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002213 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002214 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002215 * At the moment, this is a cloned configuration across all heads with
2216 * a new framebuffer object as the backing store.
2217 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002218 * Note that this also registers the fbdev and so allows userspace to call into
2219 * the driver through the fbdev interfaces.
2220 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002221 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2222 * to let the driver allocate and initialize the fbdev info structure and the
2223 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002224 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2225 * values for the fbdev info structure.
2226 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002227 * HANG DEBUGGING:
2228 *
2229 * When you have fbcon support built-in or already loaded, this function will do
2230 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2231 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2232 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2233 * to consoles, not even serial console. This means when your driver crashes,
2234 * you will see absolutely nothing else but a system stuck in this function,
2235 * with no further output. Any kind of printk() you place within your own driver
2236 * or in the drm core modeset code will also never show up.
2237 *
2238 * Standard debug practice is to run the fbcon setup without taking the
2239 * console_lock as a hack, to be able to see backtraces and crashes on the
2240 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2241 * cmdline option.
2242 *
2243 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002244 * first modeset from userspace will crash in the same way, and is even easier
2245 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002246 * kernel cmdline option.
2247 *
Dave Airlie38651672010-03-30 05:34:13 +00002248 * RETURNS:
2249 * Zero if everything went ok, nonzero otherwise.
2250 */
Thierry Reding01934c22014-12-19 11:21:32 +01002251int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002252{
Dave Airlie8be48d92010-03-30 05:34:14 +00002253 struct drm_device *dev = fb_helper->dev;
Chris Wilson966a6a12016-11-29 12:02:15 +00002254 struct fb_info *info;
Chris Wilson966a6a12016-11-29 12:02:15 +00002255 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002256
Daniel Vetterf64c5572015-08-25 15:45:13 +02002257 if (!drm_fbdev_emulation)
2258 return 0;
2259
Daniel Vetter53f19042014-03-20 14:26:35 +01002260 mutex_lock(&dev->mode_config.mutex);
Chris Wilson64e94402016-11-29 12:02:16 +00002261 drm_setup_crtcs(fb_helper,
2262 dev->mode_config.max_width,
2263 dev->mode_config.max_height);
Chris Wilson966a6a12016-11-29 12:02:15 +00002264 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2265 mutex_unlock(&dev->mode_config.mutex);
2266 if (ret)
2267 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002268
Chris Wilson966a6a12016-11-29 12:02:15 +00002269 info = fb_helper->fbdev;
2270 info->var.pixclock = 0;
2271 ret = register_framebuffer(info);
2272 if (ret < 0)
2273 return ret;
2274
2275 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2276 info->node, info->fix.id);
2277
Chris Wilsona53ca632016-11-29 12:02:17 +00002278 mutex_lock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002279 if (list_empty(&kernel_fb_helper_list))
2280 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2281
2282 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +00002283 mutex_unlock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002284
2285 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002286}
Dave Airlie8be48d92010-03-30 05:34:14 +00002287EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002288
Chris Wilson73943712011-04-22 11:03:57 +01002289/**
2290 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002291 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01002292 * @fb_helper: the drm_fb_helper
2293 *
Chris Wilson73943712011-04-22 11:03:57 +01002294 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002295 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002296 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002297 * Called at runtime, takes the mode config locks to be able to check/change the
2298 * modeset configuration. Must be run from process context (which usually means
2299 * either the output polling work or a work item launched from the driver's
2300 * hotplug interrupt).
2301 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002302 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002303 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002304 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2305 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002306 *
Chris Wilson73943712011-04-22 11:03:57 +01002307 * RETURNS:
2308 * 0 on success and a non-zero error code otherwise.
2309 */
2310int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002311{
Chris Wilson73943712011-04-22 11:03:57 +01002312 struct drm_device *dev = fb_helper->dev;
Dave Airlie4abe3522010-03-30 05:34:18 +00002313
Daniel Vetterf64c5572015-08-25 15:45:13 +02002314 if (!drm_fbdev_emulation)
2315 return 0;
2316
Chris Wilson64e94402016-11-29 12:02:16 +00002317 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002318 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002319 fb_helper->delayed_hotplug = true;
Chris Wilson64e94402016-11-29 12:02:16 +00002320 mutex_unlock(&dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01002321 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002322 }
Dave Airlie38651672010-03-30 05:34:13 +00002323 DRM_DEBUG_KMS("\n");
2324
Chris Wilson64e94402016-11-29 12:02:16 +00002325 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
Dave Airlie4abe3522010-03-30 05:34:18 +00002326
Chris Wilson64e94402016-11-29 12:02:16 +00002327 mutex_unlock(&dev->mode_config.mutex);
Daniel Vetter89ced122013-04-11 14:26:55 +00002328
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002329 drm_fb_helper_set_par(fb_helper->fbdev);
2330
2331 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002332}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002333EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002334
David Rientjes6a108a12011-01-20 14:44:16 -08002335/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002336 * but the module doesn't depend on any fb console symbols. At least
2337 * attempt to load fbcon to avoid leaving the system without a usable console.
2338 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002339int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002340{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002341#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002342 const char *name = "fbcon";
2343 struct module *fbcon;
2344
2345 mutex_lock(&module_mutex);
2346 fbcon = find_module(name);
2347 mutex_unlock(&module_mutex);
2348
2349 if (!fbcon)
2350 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002351#endif
David Fries3ce05162010-12-12 12:39:22 -06002352 return 0;
2353}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002354EXPORT_SYMBOL(drm_fb_helper_modinit);