blob: a0ea3241c65117ad056a67c8d3be17af317d80c5 [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
Thierry Reding50021ff2017-03-29 16:43:53 +0200112int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
113 struct drm_connector *connector)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200114{
Thierry Reding50021ff2017-03-29 16:43:53 +0200115 struct drm_fb_helper_connector *fb_conn;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200116 struct drm_fb_helper_connector **temp;
Thierry Reding50021ff2017-03-29 16:43:53 +0200117 unsigned int count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200118
119 if (!drm_fbdev_emulation)
120 return 0;
121
122 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
Thierry Reding50021ff2017-03-29 16:43:53 +0200123
124 count = fb_helper->connector_count + 1;
125
126 if (count > fb_helper->connector_info_alloc_count) {
127 size_t size = count * sizeof(fb_conn);
128
129 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200130 if (!temp)
131 return -ENOMEM;
132
Thierry Reding50021ff2017-03-29 16:43:53 +0200133 fb_helper->connector_info_alloc_count = count;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200134 fb_helper->connector_info = temp;
135 }
136
Thierry Reding50021ff2017-03-29 16:43:53 +0200137 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
138 if (!fb_conn)
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200139 return -ENOMEM;
140
141 drm_connector_get(connector);
Thierry Reding50021ff2017-03-29 16:43:53 +0200142 fb_conn->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
Thierry Reding39b8b2e2017-03-29 16:43:52 +0200144 return 0;
145}
146EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
147
Daniel Vetter207fd322013-01-20 22:13:14 +0100148/**
149 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
150 * emulation helper
151 * @fb_helper: fbdev initialized with drm_fb_helper_init
152 *
153 * This functions adds all the available connectors for use with the given
154 * fb_helper. This is a separate step to allow drivers to freely assign
155 * connectors to the fbdev, e.g. if some are reserved for special purposes or
156 * not adequate to be used for the fbcon.
157 *
Daniel Vetter169faec2015-07-09 23:44:27 +0200158 * This function is protected against concurrent connector hotadds/removals
159 * using drm_fb_helper_add_one_connector() and
160 * drm_fb_helper_remove_one_connector().
Daniel Vetter207fd322013-01-20 22:13:14 +0100161 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000162int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000163{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000164 struct drm_device *dev = fb_helper->dev;
165 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100166 struct drm_connector_list_iter conn_iter;
167 int i, ret = 0;
Dave Airlied50ba252009-09-23 14:44:08 +1000168
Daniel Vetterf64c5572015-08-25 15:45:13 +0200169 if (!drm_fbdev_emulation)
170 return 0;
171
Daniel Vetter169faec2015-07-09 23:44:27 +0200172 mutex_lock(&dev->mode_config.mutex);
Thierry Redingb982dab2017-02-28 15:46:43 +0100173 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100174 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100175 ret = drm_fb_helper_add_one_connector(fb_helper, connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000176
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100177 if (ret)
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000178 goto fail;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000179 }
Daniel Vetterc36a3252016-12-15 16:58:43 +0100180 goto out;
181
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000182fail:
Chris Wilson966a6a12016-11-29 12:02:15 +0000183 drm_fb_helper_for_each_connector(fb_helper, i) {
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300184 struct drm_fb_helper_connector *fb_helper_connector =
185 fb_helper->connector_info[i];
186
Thierry Redingad093602017-02-28 15:46:39 +0100187 drm_connector_put(fb_helper_connector->connector);
Ville Syrjälä7dfcb362016-10-26 12:05:52 +0300188
189 kfree(fb_helper_connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000190 fb_helper->connector_info[i] = NULL;
191 }
192 fb_helper->connector_count = 0;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100193out:
Thierry Redingb982dab2017-02-28 15:46:43 +0100194 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter169faec2015-07-09 23:44:27 +0200195 mutex_unlock(&dev->mode_config.mutex);
196
Maarten Lankhorst15fce292016-02-15 13:45:16 +0100197 return ret;
Dave Airlied50ba252009-09-23 14:44:08 +1000198}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000199EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000200
Dave Airlie65c2a892014-06-05 14:01:30 +1000201int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
202 struct drm_connector *connector)
203{
204 struct drm_fb_helper_connector *fb_helper_connector;
205 int i, j;
206
Daniel Vetterf64c5572015-08-25 15:45:13 +0200207 if (!drm_fbdev_emulation)
208 return 0;
209
Dave Airlie65c2a892014-06-05 14:01:30 +1000210 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
211
212 for (i = 0; i < fb_helper->connector_count; i++) {
213 if (fb_helper->connector_info[i]->connector == connector)
214 break;
215 }
216
217 if (i == fb_helper->connector_count)
218 return -EINVAL;
219 fb_helper_connector = fb_helper->connector_info[i];
Thierry Redingad093602017-02-28 15:46:39 +0100220 drm_connector_put(fb_helper_connector->connector);
Dave Airlie65c2a892014-06-05 14:01:30 +1000221
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200222 for (j = i + 1; j < fb_helper->connector_count; j++)
Dave Airlie65c2a892014-06-05 14:01:30 +1000223 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200224
Dave Airlie65c2a892014-06-05 14:01:30 +1000225 fb_helper->connector_count--;
226 kfree(fb_helper_connector);
Rob Clark2148f182015-01-26 10:11:08 -0500227
Dave Airlie65c2a892014-06-05 14:01:30 +1000228 return 0;
229}
230EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
231
Jason Wessel99231022010-10-13 14:09:43 -0500232static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
233{
234 uint16_t *r_base, *g_base, *b_base;
235 int i;
236
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300237 if (helper->funcs->gamma_get == NULL)
238 return;
239
Jason Wessel99231022010-10-13 14:09:43 -0500240 r_base = crtc->gamma_store;
241 g_base = r_base + crtc->gamma_size;
242 b_base = g_base + crtc->gamma_size;
243
244 for (i = 0; i < crtc->gamma_size; i++)
245 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
246}
247
248static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
249{
250 uint16_t *r_base, *g_base, *b_base;
251
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200252 if (crtc->funcs->gamma_set == NULL)
253 return;
254
Jason Wessel99231022010-10-13 14:09:43 -0500255 r_base = crtc->gamma_store;
256 g_base = r_base + crtc->gamma_size;
257 b_base = g_base + crtc->gamma_size;
258
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200259 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
260 crtc->gamma_size, NULL);
Jason Wessel99231022010-10-13 14:09:43 -0500261}
262
Daniel Vetter207fd322013-01-20 22:13:14 +0100263/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100264 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
Daniel Vetter207fd322013-01-20 22:13:14 +0100265 * @info: fbdev registered by the helper
266 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500267int drm_fb_helper_debug_enter(struct fb_info *info)
268{
269 struct drm_fb_helper *helper = info->par;
Jani Nikulabe26a662015-03-11 11:51:06 +0200270 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500271 int i;
272
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500273 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
274 for (i = 0; i < helper->crtc_count; i++) {
275 struct drm_mode_set *mode_set =
276 &helper->crtc_info[i].mode_set;
277
278 if (!mode_set->crtc->enabled)
279 continue;
280
281 funcs = mode_set->crtc->helper_private;
Stefan Christ1b99b722016-11-14 00:03:11 +0100282 if (funcs->mode_set_base_atomic == NULL)
283 continue;
284
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200285 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
286 continue;
287
Jason Wessel99231022010-10-13 14:09:43 -0500288 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500289 funcs->mode_set_base_atomic(mode_set->crtc,
290 mode_set->fb,
291 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500292 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500293 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500294 }
295 }
296
297 return 0;
298}
299EXPORT_SYMBOL(drm_fb_helper_debug_enter);
300
301/* Find the real fb for a given fb helper CRTC */
302static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
303{
304 struct drm_device *dev = crtc->dev;
305 struct drm_crtc *c;
306
Daniel Vetter6295d602015-07-09 23:44:25 +0200307 drm_for_each_crtc(c, dev) {
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500308 if (crtc->base.id == c->base.id)
Matt Roperf4510a22014-04-01 15:22:40 -0700309 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500310 }
311
312 return NULL;
313}
314
Daniel Vetter207fd322013-01-20 22:13:14 +0100315/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100316 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
Daniel Vetter207fd322013-01-20 22:13:14 +0100317 * @info: fbdev registered by the helper
318 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500319int drm_fb_helper_debug_leave(struct fb_info *info)
320{
321 struct drm_fb_helper *helper = info->par;
322 struct drm_crtc *crtc;
Jani Nikulabe26a662015-03-11 11:51:06 +0200323 const struct drm_crtc_helper_funcs *funcs;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500324 struct drm_framebuffer *fb;
325 int i;
326
327 for (i = 0; i < helper->crtc_count; i++) {
328 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200329
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500330 crtc = mode_set->crtc;
331 funcs = crtc->helper_private;
332 fb = drm_mode_config_fb(crtc);
333
334 if (!crtc->enabled)
335 continue;
336
337 if (!fb) {
338 DRM_ERROR("no fb to restore??\n");
339 continue;
340 }
341
Stefan Christ1b99b722016-11-14 00:03:11 +0100342 if (funcs->mode_set_base_atomic == NULL)
343 continue;
344
Daniel Vetter9c79e0b2017-04-03 10:32:59 +0200345 if (drm_drv_uses_atomic_modeset(crtc->dev))
346 continue;
347
Jason Wessel99231022010-10-13 14:09:43 -0500348 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500349 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500350 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500351 }
352
353 return 0;
354}
355EXPORT_SYMBOL(drm_fb_helper_debug_leave);
356
Rob Clarkbbb1e522015-08-25 15:35:58 -0400357static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper)
358{
359 struct drm_device *dev = fb_helper->dev;
360 struct drm_plane *plane;
361 struct drm_atomic_state *state;
362 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200363 unsigned int plane_mask;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400364
365 state = drm_atomic_state_alloc(dev);
366 if (!state)
367 return -ENOMEM;
368
369 state->acquire_ctx = dev->mode_config.acquire_ctx;
370retry:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100371 plane_mask = 0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400372 drm_for_each_plane(plane, dev) {
373 struct drm_plane_state *plane_state;
374
375 plane_state = drm_atomic_get_plane_state(state, plane);
376 if (IS_ERR(plane_state)) {
377 ret = PTR_ERR(plane_state);
378 goto fail;
379 }
380
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300381 plane_state->rotation = DRM_ROTATE_0;
Rob Clarkbbb1e522015-08-25 15:35:58 -0400382
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100383 plane->old_fb = plane->fb;
384 plane_mask |= 1 << drm_plane_index(plane);
385
Rob Clarkbbb1e522015-08-25 15:35:58 -0400386 /* disable non-primary: */
387 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
388 continue;
389
390 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
391 if (ret != 0)
392 goto fail;
393 }
394
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200395 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clarkbbb1e522015-08-25 15:35:58 -0400396 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
397
398 ret = __drm_atomic_helper_set_config(mode_set, state);
399 if (ret != 0)
400 goto fail;
401 }
402
403 ret = drm_atomic_commit(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400404
405fail:
Maarten Lankhorstf72c6b32015-11-11 11:29:10 +0100406 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Matt Roper94284032015-09-21 17:21:48 -0700407
Rob Clarkbbb1e522015-08-25 15:35:58 -0400408 if (ret == -EDEADLK)
409 goto backoff;
410
Chris Wilson08536952016-10-14 13:18:18 +0100411 drm_atomic_state_put(state);
Rob Clarkbbb1e522015-08-25 15:35:58 -0400412 return ret;
413
414backoff:
415 drm_atomic_state_clear(state);
416 drm_atomic_legacy_backoff(state);
417
418 goto retry;
419}
420
Daniel Vetter71286452017-04-03 10:33:04 +0200421static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100422{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300423 struct drm_device *dev = fb_helper->dev;
424 struct drm_plane *plane;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300425 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100426
Daniel Vetter6295d602015-07-09 23:44:25 +0200427 drm_for_each_plane(plane, dev) {
Matt Ropere27dde32014-04-01 15:22:30 -0700428 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
429 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100430
Ville Syrjälä6686df82016-10-21 22:22:45 +0300431 if (plane->rotation_property)
Ville Syrjäläd138dd32016-09-26 19:30:48 +0300432 drm_mode_plane_set_obj_prop(plane,
433 plane->rotation_property,
434 DRM_ROTATE_0);
Sonika Jindal9783de22014-08-05 11:26:57 +0530435 }
436
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100437 for (i = 0; i < fb_helper->crtc_count; i++) {
438 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300439 struct drm_crtc *crtc = mode_set->crtc;
440 int ret;
441
Alex Deucher03f9abb2015-09-30 14:47:37 -0400442 if (crtc->funcs->cursor_set2) {
443 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
444 if (ret)
Dave Airlie48f87dd2015-10-16 10:10:32 +1000445 return ret;
Alex Deucher03f9abb2015-09-30 14:47:37 -0400446 } else if (crtc->funcs->cursor_set) {
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300447 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
448 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200449 return ret;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300450 }
451
Daniel Vetter2d13b672012-12-11 13:47:23 +0100452 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100453 if (ret)
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200454 return ret;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100455 }
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200456
457 return 0;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100458}
Rob Clark5ea1f752014-05-30 12:29:48 -0400459
Daniel Vetter71286452017-04-03 10:33:04 +0200460static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
461{
462 struct drm_device *dev = fb_helper->dev;
463
464 drm_warn_on_modeset_not_all_locked(dev);
465
466 if (drm_drv_uses_atomic_modeset(dev))
467 return restore_fbdev_mode_atomic(fb_helper);
468 else
469 return restore_fbdev_mode_legacy(fb_helper);
470}
471
Rob Clark5ea1f752014-05-30 12:29:48 -0400472/**
473 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
474 * @fb_helper: fbcon to restore
475 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100476 * This should be called from driver's drm &drm_driver.lastclose callback
Rob Clark5ea1f752014-05-30 12:29:48 -0400477 * when implementing an fbcon on top of kms using this helper. This ensures that
478 * the user isn't greeted with a black screen when e.g. X dies.
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200479 *
480 * RETURNS:
481 * Zero if everything went ok, negative error code otherwise.
Rob Clark5ea1f752014-05-30 12:29:48 -0400482 */
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200483int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
Rob Clark5ea1f752014-05-30 12:29:48 -0400484{
485 struct drm_device *dev = fb_helper->dev;
Daniel Vetterb7bdf0a82015-08-25 17:20:28 +0200486 bool do_delayed;
487 int ret;
Dave Airliee2809c72014-11-26 13:15:24 +1000488
Daniel Vetterf64c5572015-08-25 15:45:13 +0200489 if (!drm_fbdev_emulation)
490 return -ENODEV;
491
Rob Clark5ea1f752014-05-30 12:29:48 -0400492 drm_modeset_lock_all(dev);
493 ret = restore_fbdev_mode(fb_helper);
Dave Airliee2809c72014-11-26 13:15:24 +1000494
495 do_delayed = fb_helper->delayed_hotplug;
496 if (do_delayed)
497 fb_helper->delayed_hotplug = false;
Rob Clark5ea1f752014-05-30 12:29:48 -0400498 drm_modeset_unlock_all(dev);
Dave Airliee2809c72014-11-26 13:15:24 +1000499
500 if (do_delayed)
501 drm_fb_helper_hotplug_event(fb_helper);
Rob Clark5ea1f752014-05-30 12:29:48 -0400502 return ret;
503}
504EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100505
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200506static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
507{
508 struct drm_device *dev = fb_helper->dev;
509 struct drm_crtc *crtc;
510 int bound = 0, crtcs_bound = 0;
511
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200512 /*
513 * Sometimes user space wants everything disabled, so don't steal the
514 * display if there's a master.
515 */
Johannes Bergf17b3ea2016-08-11 11:50:21 +0200516 if (READ_ONCE(dev->master))
Geert Uytterhoeven2c4124f2015-08-04 15:22:11 +0200517 return false;
518
519 drm_for_each_crtc(crtc, dev) {
520 if (crtc->primary->fb)
521 crtcs_bound++;
522 if (crtc->primary->fb == fb_helper->fb)
523 bound++;
524 }
525
526 if (bound < crtcs_bound)
527 return false;
528
529 return true;
530}
531
532#ifdef CONFIG_MAGIC_SYSRQ
Daniel Vetterd21bf462013-01-20 18:09:52 +0100533/*
534 * restore fbcon display for all kms driver's using this helper, used for sysrq
535 * and panic handling.
536 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530537static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000538{
Dave Airlie785b93e2009-08-28 15:46:53 +1000539 bool ret, error = false;
540 struct drm_fb_helper *helper;
541
542 if (list_empty(&kernel_fb_helper_list))
543 return false;
544
545 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200546 struct drm_device *dev = helper->dev;
547
548 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100549 continue;
550
Daniel Vetterdd908c82015-07-28 13:18:41 +0200551 drm_modeset_lock_all(dev);
Geert Uytterhoeven3d9e35a2015-08-04 15:22:10 +0200552 ret = restore_fbdev_mode(helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100553 if (ret)
554 error = true;
Daniel Vettercb597bb2014-07-27 19:09:33 +0200555 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000556 }
557 return error;
558}
559
Dave Airlie785b93e2009-08-28 15:46:53 +1000560static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
561{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100562 bool ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200563
Daniel Vetterd21bf462013-01-20 18:09:52 +0100564 ret = drm_fb_helper_force_kernel_mode();
565 if (ret == true)
566 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000567}
568static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
569
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700570static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000571{
572 schedule_work(&drm_fb_helper_restore_work);
573}
574
575static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
576 .handler = drm_fb_helper_sysrq,
577 .help_msg = "force-fb(V)",
578 .action_msg = "Restore framebuffer console",
579};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000580#else
581static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200582#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000583
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100584static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000585{
586 struct drm_fb_helper *fb_helper = info->par;
587 struct drm_device *dev = fb_helper->dev;
588 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700589 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700590 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000591
592 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100593 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000594 */
Daniel Vetter84849902012-12-02 00:28:11 +0100595 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100596 if (!drm_fb_helper_is_bound(fb_helper)) {
597 drm_modeset_unlock_all(dev);
598 return;
599 }
600
Jesse Barnese87b2c42009-09-17 18:14:41 -0700601 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000602 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000603
Dave Airlie8be48d92010-03-30 05:34:14 +0000604 if (!crtc->enabled)
605 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000606
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100607 /* Walk the connectors & encoders on this fb turning them on/off */
Chris Wilson966a6a12016-11-29 12:02:15 +0000608 drm_fb_helper_for_each_connector(fb_helper, j) {
Jesse Barnes023eb572010-07-02 10:48:08 -0700609 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200610 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500611 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100612 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700613 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000614 }
Daniel Vetter84849902012-12-02 00:28:11 +0100615 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000616}
617
Daniel Vetter207fd322013-01-20 22:13:14 +0100618/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100619 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
Daniel Vetter207fd322013-01-20 22:13:14 +0100620 * @blank: desired blanking state
621 * @info: fbdev registered by the helper
622 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000623int drm_fb_helper_blank(int blank, struct fb_info *info)
624{
Daniel Vetterc50bfd02015-07-28 13:18:40 +0200625 if (oops_in_progress)
626 return -EBUSY;
627
Dave Airlie785b93e2009-08-28 15:46:53 +1000628 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000629 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000630 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100631 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000632 break;
James Simmons731b5a12009-10-29 20:39:07 +0000633 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000634 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100635 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000636 break;
James Simmons731b5a12009-10-29 20:39:07 +0000637 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000638 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100639 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000640 break;
James Simmons731b5a12009-10-29 20:39:07 +0000641 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000642 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100643 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000644 break;
James Simmons731b5a12009-10-29 20:39:07 +0000645 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000646 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100647 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000648 break;
649 }
650 return 0;
651}
652EXPORT_SYMBOL(drm_fb_helper_blank);
653
Ville Syrjäläa2889602016-10-26 17:41:18 +0300654static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
655 struct drm_mode_set *modeset)
656{
657 int i;
658
659 for (i = 0; i < modeset->num_connectors; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100660 drm_connector_put(modeset->connectors[i]);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300661 modeset->connectors[i] = NULL;
662 }
663 modeset->num_connectors = 0;
664
665 drm_mode_destroy(helper->dev, modeset->mode);
666 modeset->mode = NULL;
667
668 /* FIXME should hold a ref? */
669 modeset->fb = NULL;
670}
671
Dave Airlie785b93e2009-08-28 15:46:53 +1000672static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
673{
674 int i;
675
Dave Airlie6e86d582016-04-27 11:24:51 +1000676 for (i = 0; i < helper->connector_count; i++) {
Thierry Redingad093602017-02-28 15:46:39 +0100677 drm_connector_put(helper->connector_info[i]->connector);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000678 kfree(helper->connector_info[i]);
Dave Airlie6e86d582016-04-27 11:24:51 +1000679 }
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000680 kfree(helper->connector_info);
Ville Syrjäläa2889602016-10-26 17:41:18 +0300681
Sascha Hauera1b77362012-02-01 11:38:22 +0100682 for (i = 0; i < helper->crtc_count; i++) {
Ville Syrjäläa2889602016-10-26 17:41:18 +0300683 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
684
685 drm_fb_helper_modeset_release(helper, modeset);
686 kfree(modeset->connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100687 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000688 kfree(helper->crtc_info);
689}
690
Noralf Trønnescfe63422016-08-23 13:54:06 +0200691static void drm_fb_helper_resume_worker(struct work_struct *work)
692{
693 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
694 resume_work);
695
696 console_lock();
697 fb_set_suspend(helper->fbdev, 0);
698 console_unlock();
699}
700
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200701static void drm_fb_helper_dirty_work(struct work_struct *work)
702{
703 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
704 dirty_work);
705 struct drm_clip_rect *clip = &helper->dirty_clip;
706 struct drm_clip_rect clip_copy;
707 unsigned long flags;
708
709 spin_lock_irqsave(&helper->dirty_lock, flags);
710 clip_copy = *clip;
711 clip->x1 = clip->y1 = ~0;
712 clip->x2 = clip->y2 = 0;
713 spin_unlock_irqrestore(&helper->dirty_lock, flags);
714
Takashi Iwai87d3b652016-10-20 17:05:30 +0200715 /* call dirty callback only when it has been really touched */
716 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
717 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200718}
719
Daniel Vetter207fd322013-01-20 22:13:14 +0100720/**
Thierry Reding10a23102014-06-27 17:19:24 +0200721 * drm_fb_helper_prepare - setup a drm_fb_helper structure
722 * @dev: DRM device
723 * @helper: driver-allocated fbdev helper structure to set up
724 * @funcs: pointer to structure of functions associate with this helper
725 *
726 * Sets up the bare minimum to make the framebuffer helper usable. This is
727 * useful to implement race-free initialization of the polling helpers.
728 */
729void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
730 const struct drm_fb_helper_funcs *funcs)
731{
732 INIT_LIST_HEAD(&helper->kernel_fb_list);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200733 spin_lock_init(&helper->dirty_lock);
Noralf Trønnescfe63422016-08-23 13:54:06 +0200734 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200735 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
736 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
Thierry Reding10a23102014-06-27 17:19:24 +0200737 helper->funcs = funcs;
738 helper->dev = dev;
739}
740EXPORT_SYMBOL(drm_fb_helper_prepare);
741
742/**
Daniel Vettered84e252017-02-07 15:10:49 +0100743 * drm_fb_helper_init - initialize a &struct drm_fb_helper
Daniel Vetter207fd322013-01-20 22:13:14 +0100744 * @dev: drm device
745 * @fb_helper: driver-allocated fbdev helper structure to initialize
Daniel Vetter207fd322013-01-20 22:13:14 +0100746 * @max_conn_count: max connector count
747 *
748 * This allocates the structures for the fbdev helper with the given limits.
749 * Note that this won't yet touch the hardware (through the driver interfaces)
750 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
751 * to allow driver writes more control over the exact init sequence.
752 *
Thierry Reding10a23102014-06-27 17:19:24 +0200753 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100754 *
755 * RETURNS:
756 * Zero if everything went ok, nonzero otherwise.
757 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000758int drm_fb_helper_init(struct drm_device *dev,
759 struct drm_fb_helper *fb_helper,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200760 int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000761{
Dave Airlie785b93e2009-08-28 15:46:53 +1000762 struct drm_crtc *crtc;
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200763 struct drm_mode_config *config = &dev->mode_config;
Dave Airlie785b93e2009-08-28 15:46:53 +1000764 int i;
765
Daniel Vetterf64c5572015-08-25 15:45:13 +0200766 if (!drm_fbdev_emulation)
767 return 0;
768
Xiubo Li04cfe972014-03-10 09:33:58 +0800769 if (!max_conn_count)
770 return -EINVAL;
771
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200772 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
Dave Airlie4abe3522010-03-30 05:34:18 +0000773 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000774 return -ENOMEM;
775
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200776 fb_helper->crtc_count = config->num_crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +0000777 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
778 if (!fb_helper->connector_info) {
779 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000780 return -ENOMEM;
781 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000782 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000783 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000784
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200785 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000786 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000787 kcalloc(max_conn_count,
788 sizeof(struct drm_connector *),
789 GFP_KERNEL);
790
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200791 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000792 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000793 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000794 }
795
796 i = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200797 drm_for_each_crtc(crtc, dev) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000798 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000799 i++;
800 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100801
Dave Airlie785b93e2009-08-28 15:46:53 +1000802 return 0;
803out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000804 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000805 return -ENOMEM;
806}
Dave Airlie4abe3522010-03-30 05:34:18 +0000807EXPORT_SYMBOL(drm_fb_helper_init);
808
Archit Tanejab8017d62015-07-22 14:57:56 +0530809/**
810 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
811 * @fb_helper: driver-allocated fbdev helper
812 *
813 * A helper to alloc fb_info and the members cmap and apertures. Called
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100814 * by the driver within the fb_probe fb_helper callback function. Drivers do not
815 * need to release the allocated fb_info structure themselves, this is
816 * automatically done when calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530817 *
818 * RETURNS:
819 * fb_info pointer if things went okay, pointer containing error code
820 * otherwise
821 */
822struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
823{
824 struct device *dev = fb_helper->dev->dev;
825 struct fb_info *info;
826 int ret;
827
828 info = framebuffer_alloc(0, dev);
829 if (!info)
830 return ERR_PTR(-ENOMEM);
831
832 ret = fb_alloc_cmap(&info->cmap, 256, 0);
833 if (ret)
834 goto err_release;
835
836 info->apertures = alloc_apertures(1);
837 if (!info->apertures) {
838 ret = -ENOMEM;
839 goto err_free_cmap;
840 }
841
842 fb_helper->fbdev = info;
843
844 return info;
845
846err_free_cmap:
847 fb_dealloc_cmap(&info->cmap);
848err_release:
849 framebuffer_release(info);
850 return ERR_PTR(ret);
851}
852EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
853
854/**
855 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
856 * @fb_helper: driver-allocated fbdev helper
857 *
858 * A wrapper around unregister_framebuffer, to release the fb_info
Daniel Vettered84e252017-02-07 15:10:49 +0100859 * framebuffer device. This must be called before releasing all resources for
860 * @fb_helper by calling drm_fb_helper_fini().
Archit Tanejab8017d62015-07-22 14:57:56 +0530861 */
862void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
863{
864 if (fb_helper && fb_helper->fbdev)
865 unregister_framebuffer(fb_helper->fbdev);
866}
867EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
868
869/**
Daniel Vettered84e252017-02-07 15:10:49 +0100870 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
871 * @fb_helper: driver-allocated fbdev helper
872 *
873 * This cleans up all remaining resources associated with @fb_helper. Must be
874 * called after drm_fb_helper_unlink_fbi() was called.
875 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000876void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
877{
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100878 struct fb_info *info;
879
880 if (!drm_fbdev_emulation || !fb_helper)
Daniel Vetterf64c5572015-08-25 15:45:13 +0200881 return;
882
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100883 info = fb_helper->fbdev;
884 if (info) {
885 if (info->cmap.len)
886 fb_dealloc_cmap(&info->cmap);
887 framebuffer_release(info);
888 }
889 fb_helper->fbdev = NULL;
890
Chris Wilson24f76b22017-02-07 12:49:56 +0000891 cancel_work_sync(&fb_helper->resume_work);
Chris Wilsonf21b9a92017-02-07 12:49:55 +0000892 cancel_work_sync(&fb_helper->dirty_work);
893
Chris Wilsona53ca632016-11-29 12:02:17 +0000894 mutex_lock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000895 if (!list_empty(&fb_helper->kernel_fb_list)) {
896 list_del(&fb_helper->kernel_fb_list);
Thierry Reding4b4f99f2017-03-29 16:43:51 +0200897 if (list_empty(&kernel_fb_helper_list))
Dave Airlie4abe3522010-03-30 05:34:18 +0000898 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
Dave Airlie4abe3522010-03-30 05:34:18 +0000899 }
Chris Wilsona53ca632016-11-29 12:02:17 +0000900 mutex_unlock(&kernel_fb_helper_lock);
Dave Airlie4abe3522010-03-30 05:34:18 +0000901
902 drm_fb_helper_crtc_free(fb_helper);
903
Dave Airlie4abe3522010-03-30 05:34:18 +0000904}
905EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000906
Archit Taneja47074ab2015-07-22 14:57:57 +0530907/**
908 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
909 * @fb_helper: driver-allocated fbdev helper
910 *
911 * A wrapper around unlink_framebuffer implemented by fbdev core
912 */
913void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
914{
915 if (fb_helper && fb_helper->fbdev)
916 unlink_framebuffer(fb_helper->fbdev);
917}
918EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
919
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200920static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
921 u32 width, u32 height)
922{
923 struct drm_fb_helper *helper = info->par;
924 struct drm_clip_rect *clip = &helper->dirty_clip;
925 unsigned long flags;
926
927 if (!helper->fb->funcs->dirty)
928 return;
929
930 spin_lock_irqsave(&helper->dirty_lock, flags);
931 clip->x1 = min_t(u32, clip->x1, x);
932 clip->y1 = min_t(u32, clip->y1, y);
933 clip->x2 = max_t(u32, clip->x2, x + width);
934 clip->y2 = max_t(u32, clip->y2, y + height);
935 spin_unlock_irqrestore(&helper->dirty_lock, flags);
936
937 schedule_work(&helper->dirty_work);
938}
939
940/**
941 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
942 * @info: fb_info struct pointer
943 * @pagelist: list of dirty mmap framebuffer pages
944 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100945 * This function is used as the &fb_deferred_io.deferred_io
Noralf Trønneseaa434d2016-04-28 17:18:33 +0200946 * callback function for flushing the fbdev mmap writes.
947 */
948void drm_fb_helper_deferred_io(struct fb_info *info,
949 struct list_head *pagelist)
950{
951 unsigned long start, end, min, max;
952 struct page *page;
953 u32 y1, y2;
954
955 min = ULONG_MAX;
956 max = 0;
957 list_for_each_entry(page, pagelist, lru) {
958 start = page->index << PAGE_SHIFT;
959 end = start + PAGE_SIZE - 1;
960 min = min(min, start);
961 max = max(max, end);
962 }
963
964 if (min < max) {
965 y1 = min / info->fix.line_length;
966 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
967 info->var.yres);
968 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
969 }
970}
971EXPORT_SYMBOL(drm_fb_helper_deferred_io);
972
Archit Tanejacbb1a822015-07-31 16:21:41 +0530973/**
974 * drm_fb_helper_sys_read - wrapper around fb_sys_read
975 * @info: fb_info struct pointer
976 * @buf: userspace buffer to read from framebuffer memory
977 * @count: number of bytes to read from framebuffer memory
978 * @ppos: read offset within framebuffer memory
979 *
980 * A wrapper around fb_sys_read implemented by fbdev core
981 */
982ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
983 size_t count, loff_t *ppos)
984{
985 return fb_sys_read(info, buf, count, ppos);
986}
987EXPORT_SYMBOL(drm_fb_helper_sys_read);
988
989/**
990 * drm_fb_helper_sys_write - wrapper around fb_sys_write
991 * @info: fb_info struct pointer
992 * @buf: userspace buffer to write to framebuffer memory
993 * @count: number of bytes to write to framebuffer memory
994 * @ppos: write offset within framebuffer memory
995 *
996 * A wrapper around fb_sys_write implemented by fbdev core
997 */
998ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
999 size_t count, loff_t *ppos)
1000{
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001001 ssize_t ret;
1002
1003 ret = fb_sys_write(info, buf, count, ppos);
1004 if (ret > 0)
1005 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1006 info->var.yres);
1007
1008 return ret;
Archit Tanejacbb1a822015-07-31 16:21:41 +05301009}
1010EXPORT_SYMBOL(drm_fb_helper_sys_write);
1011
Archit Taneja742547b2015-07-31 16:21:42 +05301012/**
1013 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1014 * @info: fbdev registered by the helper
1015 * @rect: info about rectangle to fill
1016 *
1017 * A wrapper around sys_fillrect implemented by fbdev core
1018 */
1019void drm_fb_helper_sys_fillrect(struct fb_info *info,
1020 const struct fb_fillrect *rect)
1021{
1022 sys_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001023 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1024 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301025}
1026EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1027
1028/**
1029 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1030 * @info: fbdev registered by the helper
1031 * @area: info about area to copy
1032 *
1033 * A wrapper around sys_copyarea implemented by fbdev core
1034 */
1035void drm_fb_helper_sys_copyarea(struct fb_info *info,
1036 const struct fb_copyarea *area)
1037{
1038 sys_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001039 drm_fb_helper_dirty(info, area->dx, area->dy,
1040 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301041}
1042EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1043
1044/**
1045 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1046 * @info: fbdev registered by the helper
1047 * @image: info about image to blit
1048 *
1049 * A wrapper around sys_imageblit implemented by fbdev core
1050 */
1051void drm_fb_helper_sys_imageblit(struct fb_info *info,
1052 const struct fb_image *image)
1053{
1054 sys_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001055 drm_fb_helper_dirty(info, image->dx, image->dy,
1056 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301057}
1058EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1059
1060/**
1061 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1062 * @info: fbdev registered by the helper
1063 * @rect: info about rectangle to fill
1064 *
1065 * A wrapper around cfb_imageblit implemented by fbdev core
1066 */
1067void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1068 const struct fb_fillrect *rect)
1069{
1070 cfb_fillrect(info, rect);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001071 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1072 rect->width, rect->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301073}
1074EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1075
1076/**
1077 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1078 * @info: fbdev registered by the helper
1079 * @area: info about area to copy
1080 *
1081 * A wrapper around cfb_copyarea implemented by fbdev core
1082 */
1083void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1084 const struct fb_copyarea *area)
1085{
1086 cfb_copyarea(info, area);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001087 drm_fb_helper_dirty(info, area->dx, area->dy,
1088 area->width, area->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301089}
1090EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1091
1092/**
1093 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1094 * @info: fbdev registered by the helper
1095 * @image: info about image to blit
1096 *
1097 * A wrapper around cfb_imageblit implemented by fbdev core
1098 */
1099void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1100 const struct fb_image *image)
1101{
1102 cfb_imageblit(info, image);
Noralf Trønneseaa434d2016-04-28 17:18:33 +02001103 drm_fb_helper_dirty(info, image->dx, image->dy,
1104 image->width, image->height);
Archit Taneja742547b2015-07-31 16:21:42 +05301105}
1106EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1107
Archit Tanejafdefa582015-07-31 16:21:43 +05301108/**
1109 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1110 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001111 * @suspend: whether to suspend or resume
Archit Tanejafdefa582015-07-31 16:21:43 +05301112 *
Noralf Trønnescfe63422016-08-23 13:54:06 +02001113 * A wrapper around fb_set_suspend implemented by fbdev core.
1114 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1115 * the lock yourself
Archit Tanejafdefa582015-07-31 16:21:43 +05301116 */
Daniel Vetter28579f32016-08-23 17:27:27 +02001117void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
Archit Tanejafdefa582015-07-31 16:21:43 +05301118{
1119 if (fb_helper && fb_helper->fbdev)
Daniel Vetter28579f32016-08-23 17:27:27 +02001120 fb_set_suspend(fb_helper->fbdev, suspend);
Archit Tanejafdefa582015-07-31 16:21:43 +05301121}
1122EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1123
Noralf Trønnescfe63422016-08-23 13:54:06 +02001124/**
1125 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1126 * takes the console lock
1127 * @fb_helper: driver-allocated fbdev helper
Daniel Vetter28579f32016-08-23 17:27:27 +02001128 * @suspend: whether to suspend or resume
Noralf Trønnescfe63422016-08-23 13:54:06 +02001129 *
1130 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1131 * isn't available on resume, a worker is tasked with waiting for the lock
1132 * to become available. The console lock can be pretty contented on resume
1133 * due to all the printk activity.
1134 *
1135 * This function can be called multiple times with the same state since
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001136 * &fb_info.state is checked to see if fbdev is running or not before locking.
Noralf Trønnescfe63422016-08-23 13:54:06 +02001137 *
1138 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1139 */
1140void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
Daniel Vetter28579f32016-08-23 17:27:27 +02001141 bool suspend)
Noralf Trønnescfe63422016-08-23 13:54:06 +02001142{
1143 if (!fb_helper || !fb_helper->fbdev)
1144 return;
1145
1146 /* make sure there's no pending/ongoing resume */
1147 flush_work(&fb_helper->resume_work);
1148
1149 if (suspend) {
1150 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1151 return;
1152
1153 console_lock();
1154
1155 } else {
1156 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1157 return;
1158
1159 if (!console_trylock()) {
1160 schedule_work(&fb_helper->resume_work);
1161 return;
1162 }
1163 }
1164
1165 fb_set_suspend(fb_helper->fbdev, suspend);
1166 console_unlock();
1167}
1168EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1169
Dave Airliec850cb72009-10-23 18:49:03 +10001170static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001171 u16 blue, u16 regno, struct fb_info *info)
1172{
1173 struct drm_fb_helper *fb_helper = info->par;
1174 struct drm_framebuffer *fb = fb_helper->fb;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001175
Dave Airliec850cb72009-10-23 18:49:03 +10001176 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
1177 u32 *palette;
1178 u32 value;
1179 /* place color in psuedopalette */
1180 if (regno > 16)
1181 return -EINVAL;
1182 palette = (u32 *)info->pseudo_palette;
1183 red >>= (16 - info->var.red.length);
1184 green >>= (16 - info->var.green.length);
1185 blue >>= (16 - info->var.blue.length);
1186 value = (red << info->var.red.offset) |
1187 (green << info->var.green.offset) |
1188 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +00001189 if (info->var.transp.length > 0) {
1190 u32 mask = (1 << info->var.transp.length) - 1;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001191
Rob Clark9da12b6a2011-02-16 02:45:51 +00001192 mask <<= info->var.transp.offset;
1193 value |= mask;
1194 }
Dave Airliec850cb72009-10-23 18:49:03 +10001195 palette[regno] = value;
1196 return 0;
1197 }
1198
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001199 /*
1200 * The driver really shouldn't advertise pseudo/directcolor
1201 * visuals if it can't deal with the palette.
1202 */
1203 if (WARN_ON(!fb_helper->funcs->gamma_set ||
1204 !fb_helper->funcs->gamma_get))
1205 return -EINVAL;
1206
Ville Syrjälä272725c2016-12-14 23:32:20 +02001207 WARN_ON(fb->format->cpp[0] != 1);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001208
Daniel Vetterfef14802016-03-30 11:51:17 +02001209 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001210
Dave Airliec850cb72009-10-23 18:49:03 +10001211 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001212}
1213
Daniel Vetter207fd322013-01-20 22:13:14 +01001214/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001215 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
Daniel Vetter207fd322013-01-20 22:13:14 +01001216 * @cmap: cmap to set
1217 * @info: fbdev registered by the helper
1218 */
Dave Airlie068143d2009-10-05 09:58:02 +10001219int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1220{
1221 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001222 struct drm_device *dev = fb_helper->dev;
Jani Nikulabe26a662015-03-11 11:51:06 +02001223 const struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +10001224 u16 *red, *green, *blue, *transp;
1225 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +01001226 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +10001227 int start;
1228
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001229 if (oops_in_progress)
Rui Wang9aa609e2014-12-15 11:28:26 -08001230 return -EBUSY;
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001231
1232 drm_modeset_lock_all(dev);
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001233 if (!drm_fb_helper_is_bound(fb_helper)) {
1234 drm_modeset_unlock_all(dev);
1235 return -EBUSY;
1236 }
1237
Dave Airlie8be48d92010-03-30 05:34:14 +00001238 for (i = 0; i < fb_helper->crtc_count; i++) {
1239 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1240 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +10001241
1242 red = cmap->red;
1243 green = cmap->green;
1244 blue = cmap->blue;
1245 transp = cmap->transp;
1246 start = cmap->start;
1247
roel062ac622011-03-07 18:00:34 +01001248 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +10001249 u16 hred, hgreen, hblue, htransp = 0xffff;
1250
1251 hred = *red++;
1252 hgreen = *green++;
1253 hblue = *blue++;
1254
1255 if (transp)
1256 htransp = *transp++;
1257
Dave Airliec850cb72009-10-23 18:49:03 +10001258 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1259 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001260 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +10001261 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +03001262 if (crtc_funcs->load_lut)
1263 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +10001264 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +03001265 out:
1266 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +10001267 return rc;
1268}
1269EXPORT_SYMBOL(drm_fb_helper_setcmap);
1270
Daniel Vetter207fd322013-01-20 22:13:14 +01001271/**
Maxime Ripard0f3bbe02017-02-28 16:36:51 +01001272 * drm_fb_helper_ioctl - legacy ioctl implementation
1273 * @info: fbdev registered by the helper
1274 * @cmd: ioctl command
1275 * @arg: ioctl argument
1276 *
1277 * A helper to implement the standard fbdev ioctl. Only
1278 * FBIO_WAITFORVSYNC is implemented for now.
1279 */
1280int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1281 unsigned long arg)
1282{
1283 struct drm_fb_helper *fb_helper = info->par;
1284 struct drm_device *dev = fb_helper->dev;
1285 struct drm_mode_set *mode_set;
1286 struct drm_crtc *crtc;
1287 int ret = 0;
1288
1289 mutex_lock(&dev->mode_config.mutex);
1290 if (!drm_fb_helper_is_bound(fb_helper)) {
1291 ret = -EBUSY;
1292 goto unlock;
1293 }
1294
1295 switch (cmd) {
1296 case FBIO_WAITFORVSYNC:
1297 /*
1298 * Only consider the first CRTC.
1299 *
1300 * This ioctl is supposed to take the CRTC number as
1301 * an argument, but in fbdev times, what that number
1302 * was supposed to be was quite unclear, different
1303 * drivers were passing that argument differently
1304 * (some by reference, some by value), and most of the
1305 * userspace applications were just hardcoding 0 as an
1306 * argument.
1307 *
1308 * The first CRTC should be the integrated panel on
1309 * most drivers, so this is the best choice we can
1310 * make. If we're not smart enough here, one should
1311 * just consider switch the userspace to KMS.
1312 */
1313 mode_set = &fb_helper->crtc_info[0].mode_set;
1314 crtc = mode_set->crtc;
1315
1316 /*
1317 * Only wait for a vblank event if the CRTC is
1318 * enabled, otherwise just don't do anythintg,
1319 * not even report an error.
1320 */
1321 ret = drm_crtc_vblank_get(crtc);
1322 if (!ret) {
1323 drm_crtc_wait_one_vblank(crtc);
1324 drm_crtc_vblank_put(crtc);
1325 }
1326
1327 ret = 0;
1328 goto unlock;
1329 default:
1330 ret = -ENOTTY;
1331 }
1332
1333unlock:
1334 mutex_unlock(&dev->mode_config.mutex);
1335 return ret;
1336}
1337EXPORT_SYMBOL(drm_fb_helper_ioctl);
1338
1339/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001340 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
Daniel Vetter207fd322013-01-20 22:13:14 +01001341 * @var: screeninfo to check
1342 * @info: fbdev registered by the helper
1343 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001344int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1345 struct fb_info *info)
1346{
1347 struct drm_fb_helper *fb_helper = info->par;
1348 struct drm_framebuffer *fb = fb_helper->fb;
1349 int depth;
1350
Jason Wesself90ebd92010-08-05 09:22:32 -05001351 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +10001352 return -EINVAL;
1353
Stefan Agner865afb12016-10-11 16:15:04 -07001354 /*
1355 * Changes struct fb_var_screeninfo are currently not pushed back
1356 * to KMS, hence fail if different settings are requested.
1357 */
Ville Syrjälä272725c2016-12-14 23:32:20 +02001358 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
Michel Dänzer12ffed92017-03-23 17:53:26 +09001359 var->xres > fb->width || var->yres > fb->height ||
1360 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1361 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +01001362 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1363 var->xres, var->yres, var->bits_per_pixel,
1364 var->xres_virtual, var->yres_virtual,
Ville Syrjälä272725c2016-12-14 23:32:20 +02001365 fb->width, fb->height, fb->format->cpp[0] * 8);
Dave Airlie785b93e2009-08-28 15:46:53 +10001366 return -EINVAL;
1367 }
1368
1369 switch (var->bits_per_pixel) {
1370 case 16:
1371 depth = (var->green.length == 6) ? 16 : 15;
1372 break;
1373 case 32:
1374 depth = (var->transp.length > 0) ? 32 : 24;
1375 break;
1376 default:
1377 depth = var->bits_per_pixel;
1378 break;
1379 }
1380
1381 switch (depth) {
1382 case 8:
1383 var->red.offset = 0;
1384 var->green.offset = 0;
1385 var->blue.offset = 0;
1386 var->red.length = 8;
1387 var->green.length = 8;
1388 var->blue.length = 8;
1389 var->transp.length = 0;
1390 var->transp.offset = 0;
1391 break;
1392 case 15:
1393 var->red.offset = 10;
1394 var->green.offset = 5;
1395 var->blue.offset = 0;
1396 var->red.length = 5;
1397 var->green.length = 5;
1398 var->blue.length = 5;
1399 var->transp.length = 1;
1400 var->transp.offset = 15;
1401 break;
1402 case 16:
1403 var->red.offset = 11;
1404 var->green.offset = 5;
1405 var->blue.offset = 0;
1406 var->red.length = 5;
1407 var->green.length = 6;
1408 var->blue.length = 5;
1409 var->transp.length = 0;
1410 var->transp.offset = 0;
1411 break;
1412 case 24:
1413 var->red.offset = 16;
1414 var->green.offset = 8;
1415 var->blue.offset = 0;
1416 var->red.length = 8;
1417 var->green.length = 8;
1418 var->blue.length = 8;
1419 var->transp.length = 0;
1420 var->transp.offset = 0;
1421 break;
1422 case 32:
1423 var->red.offset = 16;
1424 var->green.offset = 8;
1425 var->blue.offset = 0;
1426 var->red.length = 8;
1427 var->green.length = 8;
1428 var->blue.length = 8;
1429 var->transp.length = 8;
1430 var->transp.offset = 24;
1431 break;
1432 default:
1433 return -EINVAL;
1434 }
1435 return 0;
1436}
1437EXPORT_SYMBOL(drm_fb_helper_check_var);
1438
Daniel Vetter207fd322013-01-20 22:13:14 +01001439/**
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001440 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
Daniel Vetter207fd322013-01-20 22:13:14 +01001441 * @info: fbdev registered by the helper
1442 *
1443 * This will let fbcon do the mode init and is called at initialization time by
1444 * the fbdev core when registering the driver, and later on through the hotplug
1445 * callback.
1446 */
Dave Airlie785b93e2009-08-28 15:46:53 +10001447int drm_fb_helper_set_par(struct fb_info *info)
1448{
1449 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001450 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +10001451
Daniel Vetterc50bfd02015-07-28 13:18:40 +02001452 if (oops_in_progress)
1453 return -EBUSY;
1454
Clemens Ladisch5349ef32009-11-04 09:42:52 +01001455 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +10001456 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001457 return -EINVAL;
1458 }
1459
Rob Clark5ea1f752014-05-30 12:29:48 -04001460 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +00001461
Dave Airlie785b93e2009-08-28 15:46:53 +10001462 return 0;
1463}
1464EXPORT_SYMBOL(drm_fb_helper_set_par);
1465
Rob Clark1edf0262015-08-25 15:35:59 -04001466static int pan_display_atomic(struct fb_var_screeninfo *var,
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001467 struct fb_info *info)
Rob Clark1edf0262015-08-25 15:35:59 -04001468{
1469 struct drm_fb_helper *fb_helper = info->par;
1470 struct drm_device *dev = fb_helper->dev;
1471 struct drm_atomic_state *state;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001472 struct drm_plane *plane;
Rob Clark1edf0262015-08-25 15:35:59 -04001473 int i, ret;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001474 unsigned int plane_mask;
Rob Clark1edf0262015-08-25 15:35:59 -04001475
1476 state = drm_atomic_state_alloc(dev);
1477 if (!state)
1478 return -ENOMEM;
1479
1480 state->acquire_ctx = dev->mode_config.acquire_ctx;
1481retry:
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001482 plane_mask = 0;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001483 for (i = 0; i < fb_helper->crtc_count; i++) {
Rob Clark1edf0262015-08-25 15:35:59 -04001484 struct drm_mode_set *mode_set;
1485
1486 mode_set = &fb_helper->crtc_info[i].mode_set;
1487
1488 mode_set->x = var->xoffset;
1489 mode_set->y = var->yoffset;
1490
1491 ret = __drm_atomic_helper_set_config(mode_set, state);
1492 if (ret != 0)
1493 goto fail;
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001494
1495 plane = mode_set->crtc->primary;
Matt Roper7118fd92015-12-18 17:27:01 -08001496 plane_mask |= (1 << drm_plane_index(plane));
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001497 plane->old_fb = plane->fb;
Rob Clark1edf0262015-08-25 15:35:59 -04001498 }
1499
1500 ret = drm_atomic_commit(state);
1501 if (ret != 0)
1502 goto fail;
1503
1504 info->var.xoffset = var->xoffset;
1505 info->var.yoffset = var->yoffset;
1506
Rob Clark1edf0262015-08-25 15:35:59 -04001507fail:
Maarten Lankhorst07d3bad2015-11-11 11:29:11 +01001508 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Daniel Vettera0fb6ad2015-10-16 19:11:30 +02001509
Rob Clark1edf0262015-08-25 15:35:59 -04001510 if (ret == -EDEADLK)
1511 goto backoff;
1512
Chris Wilson08536952016-10-14 13:18:18 +01001513 drm_atomic_state_put(state);
Rob Clark1edf0262015-08-25 15:35:59 -04001514 return ret;
1515
1516backoff:
1517 drm_atomic_state_clear(state);
1518 drm_atomic_legacy_backoff(state);
1519
1520 goto retry;
1521}
1522
Daniel Vetter71286452017-04-03 10:33:04 +02001523static int pan_display_legacy(struct fb_var_screeninfo *var,
Dave Airlie785b93e2009-08-28 15:46:53 +10001524 struct fb_info *info)
1525{
1526 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +10001527 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +10001528 int ret = 0;
1529 int i;
1530
Dave Airlie8be48d92010-03-30 05:34:14 +00001531 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001532 modeset = &fb_helper->crtc_info[i].mode_set;
1533
1534 modeset->x = var->xoffset;
1535 modeset->y = var->yoffset;
1536
1537 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +01001538 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +10001539 if (!ret) {
1540 info->var.xoffset = var->xoffset;
1541 info->var.yoffset = var->yoffset;
1542 }
1543 }
1544 }
Daniel Vetter71286452017-04-03 10:33:04 +02001545
1546 return ret;
1547}
1548
1549/**
1550 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1551 * @var: updated screen information
1552 * @info: fbdev registered by the helper
1553 */
1554int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1555 struct fb_info *info)
1556{
1557 struct drm_fb_helper *fb_helper = info->par;
1558 struct drm_device *dev = fb_helper->dev;
1559 int ret;
1560
1561 if (oops_in_progress)
1562 return -EBUSY;
1563
1564 drm_modeset_lock_all(dev);
1565 if (!drm_fb_helper_is_bound(fb_helper)) {
1566 drm_modeset_unlock_all(dev);
1567 return -EBUSY;
1568 }
1569
1570 if (drm_drv_uses_atomic_modeset(dev))
1571 ret = pan_display_atomic(var, info);
1572 else
1573 ret = pan_display_legacy(var, info);
Daniel Vetter84849902012-12-02 00:28:11 +01001574 drm_modeset_unlock_all(dev);
Daniel Vetter71286452017-04-03 10:33:04 +02001575
Dave Airlie785b93e2009-08-28 15:46:53 +10001576 return ret;
1577}
1578EXPORT_SYMBOL(drm_fb_helper_pan_display);
1579
Daniel Vetter8acf6582013-01-21 23:38:37 +01001580/*
Daniel Vetter207fd322013-01-20 22:13:14 +01001581 * Allocates the backing storage and sets up the fbdev info structure through
1582 * the ->fb_probe callback and then registers the fbdev and sets up the panic
1583 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +01001584 */
Daniel Vetterde1ace52013-01-20 21:50:49 +01001585static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1586 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +10001587{
Daniel Vetter8acf6582013-01-21 23:38:37 +01001588 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +10001589 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001590 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001591 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001592 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +00001593
1594 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1595 sizes.surface_depth = 24;
1596 sizes.surface_bpp = 32;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001597 sizes.fb_width = (u32)-1;
1598 sizes.fb_height = (u32)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001599
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001600 /* if driver picks 8 or 16 by default use that for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001601 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001602 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001603
Dave Airlie785b93e2009-08-28 15:46:53 +10001604 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Chris Wilson966a6a12016-11-29 12:02:15 +00001605 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001606 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001607 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001608
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001609 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001610
1611 if (cmdline_mode->bpp_specified) {
1612 switch (cmdline_mode->bpp) {
1613 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001614 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001615 break;
1616 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001617 sizes.surface_depth = 15;
1618 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001619 break;
1620 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001621 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001622 break;
1623 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001624 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001625 break;
1626 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001627 sizes.surface_depth = 24;
1628 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001629 break;
1630 }
1631 break;
1632 }
1633 }
1634
Dave Airlie8be48d92010-03-30 05:34:14 +00001635 crtc_count = 0;
1636 for (i = 0; i < fb_helper->crtc_count; i++) {
1637 struct drm_display_mode *desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001638 struct drm_mode_set *mode_set;
1639 int x, y, j;
1640 /* in case of tile group, are we the last tile vert or horiz?
1641 * If no tile group you are always the last one both vertically
1642 * and horizontally
1643 */
1644 bool lastv = true, lasth = true;
Rob Clark675c8322015-03-11 10:23:13 -04001645
Dave Airlie8be48d92010-03-30 05:34:14 +00001646 desired_mode = fb_helper->crtc_info[i].desired_mode;
Rob Clark0e3704c2015-03-11 10:23:14 -04001647 mode_set = &fb_helper->crtc_info[i].mode_set;
Rob Clark675c8322015-03-11 10:23:13 -04001648
1649 if (!desired_mode)
1650 continue;
1651
1652 crtc_count++;
1653
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001654 x = fb_helper->crtc_info[i].x;
1655 y = fb_helper->crtc_info[i].y;
Rob Clark675c8322015-03-11 10:23:13 -04001656
1657 if (gamma_size == 0)
1658 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1659
1660 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1661 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
Rob Clark0e3704c2015-03-11 10:23:14 -04001662
1663 for (j = 0; j < mode_set->num_connectors; j++) {
1664 struct drm_connector *connector = mode_set->connectors[j];
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001665
Rob Clark0e3704c2015-03-11 10:23:14 -04001666 if (connector->has_tile) {
1667 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1668 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1669 /* cloning to multiple tiles is just crazy-talk, so: */
1670 break;
1671 }
1672 }
1673
1674 if (lasth)
1675 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1676 if (lastv)
1677 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
Dave Airlie785b93e2009-08-28 15:46:53 +10001678 }
1679
Dave Airlie38651672010-03-30 05:34:13 +00001680 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001681 /*
1682 * hmm everyone went away - assume VGA cable just fell out
1683 * and will come back later.
1684 */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001685 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001686 sizes.fb_width = sizes.surface_width = 1024;
1687 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001688 }
1689
Xinliang Liu5f152572017-02-15 17:19:08 +01001690 /* Handle our overallocation */
1691 sizes.surface_height *= drm_fbdev_overalloc;
1692 sizes.surface_height /= 100;
1693
Dave Airlie38651672010-03-30 05:34:13 +00001694 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001695 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1696 if (ret < 0)
1697 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001698
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001699 /*
1700 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1701 * events, but at init time drm_setup_crtcs needs to be called before
1702 * the fb is allocated (since we need to figure out the desired size of
1703 * the fb before we can allocate it ...). Hence we need to fix things up
1704 * here again.
1705 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001706 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001707 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1708 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1709
Dave Airlie785b93e2009-08-28 15:46:53 +10001710 return 0;
1711}
Dave Airlie785b93e2009-08-28 15:46:53 +10001712
Daniel Vetter207fd322013-01-20 22:13:14 +01001713/**
1714 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1715 * @info: fbdev registered by the helper
1716 * @pitch: desired pitch
1717 * @depth: desired depth
1718 *
1719 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1720 * fbdev emulations. Drivers which support acceleration methods which impose
1721 * additional constraints need to set up their own limits.
1722 *
1723 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001724 * &drm_fb_helper_funcs.fb_probe callback.
Daniel Vetter207fd322013-01-20 22:13:14 +01001725 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001726void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1727 uint32_t depth)
1728{
1729 info->fix.type = FB_TYPE_PACKED_PIXELS;
1730 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1731 FB_VISUAL_TRUECOLOR;
1732 info->fix.mmio_start = 0;
1733 info->fix.mmio_len = 0;
1734 info->fix.type_aux = 0;
1735 info->fix.xpanstep = 1; /* doing it in hw */
1736 info->fix.ypanstep = 1; /* doing it in hw */
1737 info->fix.ywrapstep = 0;
1738 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001739
1740 info->fix.line_length = pitch;
Dave Airlie3632ef82011-01-15 09:27:00 +10001741}
1742EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1743
Daniel Vetter207fd322013-01-20 22:13:14 +01001744/**
1745 * drm_fb_helper_fill_var - initalizes variable fbdev information
1746 * @info: fbdev instance to set up
1747 * @fb_helper: fb helper instance to use as template
1748 * @fb_width: desired fb width
1749 * @fb_height: desired fb height
1750 *
1751 * Sets up the variable fbdev metainformation from the given fb helper instance
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001752 * and the drm framebuffer allocated in &drm_fb_helper.fb.
Daniel Vetter207fd322013-01-20 22:13:14 +01001753 *
1754 * Drivers should call this (or their equivalent setup code) from their
Daniel Vetter6806cdf2017-01-25 07:26:43 +01001755 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1756 * backing storage framebuffer.
Daniel Vetter207fd322013-01-20 22:13:14 +01001757 */
Dave Airlie38651672010-03-30 05:34:13 +00001758void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001759 uint32_t fb_width, uint32_t fb_height)
1760{
Dave Airlie38651672010-03-30 05:34:13 +00001761 struct drm_framebuffer *fb = fb_helper->fb;
Thierry Reding4b4f99f2017-03-29 16:43:51 +02001762
Dave Airlie38651672010-03-30 05:34:13 +00001763 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001764 info->var.xres_virtual = fb->width;
1765 info->var.yres_virtual = fb->height;
Ville Syrjälä272725c2016-12-14 23:32:20 +02001766 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
James Simmons57084d02010-12-20 19:10:39 +00001767 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001768 info->var.xoffset = 0;
1769 info->var.yoffset = 0;
1770 info->var.activate = FB_ACTIVATE_NOW;
1771 info->var.height = -1;
1772 info->var.width = -1;
1773
Ville Syrjäläb00c6002016-12-14 23:31:35 +02001774 switch (fb->format->depth) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001775 case 8:
1776 info->var.red.offset = 0;
1777 info->var.green.offset = 0;
1778 info->var.blue.offset = 0;
1779 info->var.red.length = 8; /* 8bit DAC */
1780 info->var.green.length = 8;
1781 info->var.blue.length = 8;
1782 info->var.transp.offset = 0;
1783 info->var.transp.length = 0;
1784 break;
1785 case 15:
1786 info->var.red.offset = 10;
1787 info->var.green.offset = 5;
1788 info->var.blue.offset = 0;
1789 info->var.red.length = 5;
1790 info->var.green.length = 5;
1791 info->var.blue.length = 5;
1792 info->var.transp.offset = 15;
1793 info->var.transp.length = 1;
1794 break;
1795 case 16:
1796 info->var.red.offset = 11;
1797 info->var.green.offset = 5;
1798 info->var.blue.offset = 0;
1799 info->var.red.length = 5;
1800 info->var.green.length = 6;
1801 info->var.blue.length = 5;
1802 info->var.transp.offset = 0;
1803 break;
1804 case 24:
1805 info->var.red.offset = 16;
1806 info->var.green.offset = 8;
1807 info->var.blue.offset = 0;
1808 info->var.red.length = 8;
1809 info->var.green.length = 8;
1810 info->var.blue.length = 8;
1811 info->var.transp.offset = 0;
1812 info->var.transp.length = 0;
1813 break;
1814 case 32:
1815 info->var.red.offset = 16;
1816 info->var.green.offset = 8;
1817 info->var.blue.offset = 0;
1818 info->var.red.length = 8;
1819 info->var.green.length = 8;
1820 info->var.blue.length = 8;
1821 info->var.transp.offset = 24;
1822 info->var.transp.length = 8;
1823 break;
1824 default:
1825 break;
1826 }
1827
1828 info->var.xres = fb_width;
1829 info->var.yres = fb_height;
1830}
1831EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001832
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001833static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1834 uint32_t maxX,
1835 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001836{
1837 struct drm_connector *connector;
1838 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001839 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001840
Chris Wilson966a6a12016-11-29 12:02:15 +00001841 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001842 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001843 count += connector->funcs->fill_modes(connector, maxX, maxY);
1844 }
1845
1846 return count;
1847}
1848
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001849struct 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 +00001850{
1851 struct drm_display_mode *mode;
1852
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001853 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001854 if (mode->hdisplay > width ||
1855 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001856 continue;
1857 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1858 return mode;
1859 }
1860 return NULL;
1861}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001862EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001863
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001864static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001865{
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001866 return fb_connector->connector->cmdline_mode.specified;
Dave Airlie38651672010-03-30 05:34:13 +00001867}
1868
Vincent Abrioua09759e2017-01-06 17:44:43 +01001869struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
Dave Airlie38651672010-03-30 05:34:13 +00001870{
Chris Wilson1794d252011-04-17 07:43:32 +01001871 struct drm_cmdline_mode *cmdline_mode;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001872 struct drm_display_mode *mode;
Takashi Iwaic683f422014-03-19 14:53:13 +01001873 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001874
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001875 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001876 if (cmdline_mode->specified == false)
Daniel Stonef3af5c72015-03-19 04:33:01 +00001877 return NULL;
Dave Airlie38651672010-03-30 05:34:13 +00001878
1879 /* attempt to find a matching mode in the list of modes
1880 * we have gotten so far, if not add a CVT mode that conforms
1881 */
1882 if (cmdline_mode->rb || cmdline_mode->margins)
1883 goto create_mode;
1884
Takashi Iwaic683f422014-03-19 14:53:13 +01001885 prefer_non_interlace = !cmdline_mode->interlace;
Daniel Stonef3af5c72015-03-19 04:33:01 +00001886again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001887 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001888 /* check width/height */
1889 if (mode->hdisplay != cmdline_mode->xres ||
1890 mode->vdisplay != cmdline_mode->yres)
1891 continue;
1892
1893 if (cmdline_mode->refresh_specified) {
1894 if (mode->vrefresh != cmdline_mode->refresh)
1895 continue;
1896 }
1897
1898 if (cmdline_mode->interlace) {
1899 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1900 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001901 } else if (prefer_non_interlace) {
1902 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1903 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001904 }
1905 return mode;
1906 }
1907
Takashi Iwaic683f422014-03-19 14:53:13 +01001908 if (prefer_non_interlace) {
1909 prefer_non_interlace = false;
1910 goto again;
1911 }
1912
Dave Airlie38651672010-03-30 05:34:13 +00001913create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001914 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1915 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001916 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001917 return mode;
1918}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001919EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001920
1921static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1922{
1923 bool enable;
1924
Sachin Kamat96081cd2012-11-15 03:43:30 +00001925 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001926 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001927 else
Dave Airlie38651672010-03-30 05:34:13 +00001928 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001929
Dave Airlie38651672010-03-30 05:34:13 +00001930 return enable;
1931}
1932
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001933static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1934 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001935{
1936 bool any_enabled = false;
1937 struct drm_connector *connector;
1938 int i = 0;
1939
Chris Wilson966a6a12016-11-29 12:02:15 +00001940 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001941 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001942 enabled[i] = drm_connector_enabled(connector, true);
1943 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1944 enabled[i] ? "yes" : "no");
1945 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001946 }
1947
1948 if (any_enabled)
1949 return;
1950
Chris Wilson966a6a12016-11-29 12:02:15 +00001951 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001952 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001953 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001954 }
1955}
1956
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001957static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1958 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10001959 struct drm_fb_offset *offsets,
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001960 bool *enabled, int width, int height)
1961{
1962 int count, i, j;
1963 bool can_clone = false;
1964 struct drm_fb_helper_connector *fb_helper_conn;
1965 struct drm_display_mode *dmt_mode, *mode;
1966
1967 /* only contemplate cloning in the single crtc case */
1968 if (fb_helper->crtc_count > 1)
1969 return false;
1970
1971 count = 0;
Chris Wilson966a6a12016-11-29 12:02:15 +00001972 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001973 if (enabled[i])
1974 count++;
1975 }
1976
1977 /* only contemplate cloning if more than one connector is enabled */
1978 if (count <= 1)
1979 return false;
1980
1981 /* check the command line or if nothing common pick 1024x768 */
1982 can_clone = true;
Chris Wilson966a6a12016-11-29 12:02:15 +00001983 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001984 if (!enabled[i])
1985 continue;
1986 fb_helper_conn = fb_helper->connector_info[i];
Vincent Abrioua09759e2017-01-06 17:44:43 +01001987 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001988 if (!modes[i]) {
1989 can_clone = false;
1990 break;
1991 }
1992 for (j = 0; j < i; j++) {
1993 if (!enabled[j])
1994 continue;
1995 if (!drm_mode_equal(modes[j], modes[i]))
1996 can_clone = false;
1997 }
1998 }
1999
2000 if (can_clone) {
2001 DRM_DEBUG_KMS("can clone using command line\n");
2002 return true;
2003 }
2004
2005 /* try and find a 1024x768 mode on each connector */
2006 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04002007 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002008
Chris Wilson966a6a12016-11-29 12:02:15 +00002009 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002010 if (!enabled[i])
2011 continue;
2012
2013 fb_helper_conn = fb_helper->connector_info[i];
2014 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2015 if (drm_mode_equal(mode, dmt_mode))
2016 modes[i] = mode;
2017 }
2018 if (!modes[i])
2019 can_clone = false;
2020 }
2021
2022 if (can_clone) {
2023 DRM_DEBUG_KMS("can clone using 1024x768\n");
2024 return true;
2025 }
2026 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2027 return false;
2028}
2029
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002030static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2031 struct drm_display_mode **modes,
2032 struct drm_fb_offset *offsets,
2033 int idx,
2034 int h_idx, int v_idx)
2035{
2036 struct drm_fb_helper_connector *fb_helper_conn;
2037 int i;
2038 int hoffset = 0, voffset = 0;
2039
Chris Wilson966a6a12016-11-29 12:02:15 +00002040 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002041 fb_helper_conn = fb_helper->connector_info[i];
2042 if (!fb_helper_conn->connector->has_tile)
2043 continue;
2044
2045 if (!modes[i] && (h_idx || v_idx)) {
2046 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2047 fb_helper_conn->connector->base.id);
2048 continue;
2049 }
2050 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2051 hoffset += modes[i]->hdisplay;
2052
2053 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2054 voffset += modes[i]->vdisplay;
2055 }
2056 offsets[idx].x = hoffset;
2057 offsets[idx].y = voffset;
2058 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2059 return 0;
2060}
2061
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002062static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00002063 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002064 struct drm_fb_offset *offsets,
Dave Airlie38651672010-03-30 05:34:13 +00002065 bool *enabled, int width, int height)
2066{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002067 struct drm_fb_helper_connector *fb_helper_conn;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002068 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2069 u64 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002070 int tile_pass = 0;
Chris Wilsonc96521e2016-11-27 17:09:10 +00002071 int i;
2072
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002073retry:
Chris Wilson966a6a12016-11-29 12:02:15 +00002074 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002075 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00002076
Chris Wilsonc96521e2016-11-27 17:09:10 +00002077 if (conn_configured & BIT_ULL(i))
Dave Airlie38651672010-03-30 05:34:13 +00002078 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002079
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002080 if (enabled[i] == false) {
Chris Wilsonc96521e2016-11-27 17:09:10 +00002081 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002082 continue;
2083 }
2084
2085 /* first pass over all the untiled connectors */
2086 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2087 continue;
2088
2089 if (tile_pass == 1) {
2090 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2091 fb_helper_conn->connector->tile_v_loc != 0)
2092 continue;
2093
2094 } else {
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002095 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002096 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2097 /* if this tile_pass doesn't cover any of the tiles - keep going */
2098 continue;
2099
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002100 /*
2101 * find the tile offsets for this pass - need to find
2102 * all tiles left and above
2103 */
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002104 drm_get_tile_offsets(fb_helper, modes, offsets,
2105 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2106 }
Dave Airlie38651672010-03-30 05:34:13 +00002107 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002108 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00002109
2110 /* got for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +01002111 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
Dave Airlie38651672010-03-30 05:34:13 +00002112 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002113 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2114 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 +00002115 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002116 }
2117 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002118 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2119 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00002120 break;
2121 }
2122 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2123 "none");
Chris Wilsonc96521e2016-11-27 17:09:10 +00002124 conn_configured |= BIT_ULL(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002125 }
2126
2127 if ((conn_configured & mask) != mask) {
2128 tile_pass++;
2129 goto retry;
Dave Airlie38651672010-03-30 05:34:13 +00002130 }
2131 return true;
2132}
2133
Dave Airlie8be48d92010-03-30 05:34:14 +00002134static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2135 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00002136 struct drm_display_mode **modes,
2137 int n, int width, int height)
2138{
2139 int c, o;
2140 struct drm_connector *connector;
Jani Nikulabe26a662015-03-11 11:51:06 +02002141 const struct drm_connector_helper_funcs *connector_funcs;
Dave Airlie38651672010-03-30 05:34:13 +00002142 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00002143 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00002144 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002145 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00002146
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002147 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00002148 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002149
2150 fb_helper_conn = fb_helper->connector_info[n];
2151 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00002152
2153 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00002154 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00002155 if (modes[n] == NULL)
2156 return best_score;
2157
Lyude255f0e72016-05-12 10:56:59 -04002158 crtcs = kzalloc(fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002159 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00002160 if (!crtcs)
2161 return best_score;
2162
2163 my_score = 1;
2164 if (connector->status == connector_status_connected)
2165 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002166 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00002167 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002168 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00002169 my_score++;
2170
2171 connector_funcs = connector->helper_private;
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002172
2173 /*
2174 * If the DRM device implements atomic hooks and ->best_encoder() is
2175 * NULL we fallback to the default drm_atomic_helper_best_encoder()
2176 * helper.
2177 */
Dhinakaran Pandiyana743d752016-12-22 00:50:42 -08002178 if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
Boris Brezillonc61b93f2016-06-07 13:47:56 +02002179 !connector_funcs->best_encoder)
2180 encoder = drm_atomic_helper_best_encoder(connector);
2181 else
2182 encoder = connector_funcs->best_encoder(connector);
2183
Dave Airlie38651672010-03-30 05:34:13 +00002184 if (!encoder)
2185 goto out;
2186
Thierry Reding4b4f99f2017-03-29 16:43:51 +02002187 /*
2188 * select a crtc for this connector and then attempt to configure
2189 * remaining connectors
2190 */
Dave Airlie8be48d92010-03-30 05:34:14 +00002191 for (c = 0; c < fb_helper->crtc_count; c++) {
2192 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00002193
Sachin Kamat96081cd2012-11-15 03:43:30 +00002194 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00002195 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002196
2197 for (o = 0; o < n; o++)
2198 if (best_crtcs[o] == crtc)
2199 break;
2200
2201 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002202 /* ignore cloning unless only a single crtc */
2203 if (fb_helper->crtc_count > 1)
2204 continue;
2205
2206 if (!drm_mode_equal(modes[o], modes[n]))
2207 continue;
Dave Airlie38651672010-03-30 05:34:13 +00002208 }
2209
2210 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00002211 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2212 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00002213 width, height);
2214 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00002215 best_score = score;
2216 memcpy(best_crtcs, crtcs,
Lyude255f0e72016-05-12 10:56:59 -04002217 fb_helper->connector_count *
Dave Airlie8be48d92010-03-30 05:34:14 +00002218 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00002219 }
Dave Airlie38651672010-03-30 05:34:13 +00002220 }
2221out:
2222 kfree(crtcs);
2223 return best_score;
2224}
2225
Chris Wilson64e94402016-11-29 12:02:16 +00002226static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2227 u32 width, u32 height)
Dave Airlie38651672010-03-30 05:34:13 +00002228{
Dave Airlie8be48d92010-03-30 05:34:14 +00002229 struct drm_device *dev = fb_helper->dev;
2230 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00002231 struct drm_display_mode **modes;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002232 struct drm_fb_offset *offsets;
Dave Airlie38651672010-03-30 05:34:13 +00002233 bool *enabled;
Jesse Barnes11e17a02013-02-19 13:31:39 -08002234 int i;
Dave Airlie38651672010-03-30 05:34:13 +00002235
2236 DRM_DEBUG_KMS("\n");
Chris Wilson64e94402016-11-29 12:02:16 +00002237 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2238 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
Dave Airlie38651672010-03-30 05:34:13 +00002239
Chris Wilson966a6a12016-11-29 12:02:15 +00002240 /* prevent concurrent modification of connector_count by hotplug */
2241 lockdep_assert_held(&fb_helper->dev->mode_config.mutex);
2242
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002243 crtcs = kcalloc(fb_helper->connector_count,
Dave Airlie8be48d92010-03-30 05:34:14 +00002244 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002245 modes = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002246 sizeof(struct drm_display_mode *), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002247 offsets = kcalloc(fb_helper->connector_count,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002248 sizeof(struct drm_fb_offset), GFP_KERNEL);
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002249 enabled = kcalloc(fb_helper->connector_count,
Dave Airlie38651672010-03-30 05:34:13 +00002250 sizeof(bool), GFP_KERNEL);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002251 if (!crtcs || !modes || !enabled || !offsets) {
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002252 DRM_ERROR("Memory allocation failed\n");
2253 goto out;
2254 }
2255
Dave Airlie0b4c0f32010-03-30 05:34:15 +00002256 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00002257
Jesse Barnes11e17a02013-02-19 13:31:39 -08002258 if (!(fb_helper->funcs->initial_config &&
2259 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002260 offsets,
Jesse Barnes11e17a02013-02-19 13:31:39 -08002261 enabled, width, height))) {
Maarten Lankhorst383b2e52016-02-15 13:45:15 +01002262 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2263 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2264 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
Jesse Barnes11e17a02013-02-19 13:31:39 -08002265
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002266 if (!drm_target_cloned(fb_helper, modes, offsets,
2267 enabled, width, height) &&
2268 !drm_target_preferred(fb_helper, modes, offsets,
2269 enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002270 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08002271
2272 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2273 width, height);
2274
2275 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00002276 }
Dave Airlie38651672010-03-30 05:34:13 +00002277
Dave Airlie8be48d92010-03-30 05:34:14 +00002278 /* need to set the modesets up here for use later */
2279 /* fill out the connector<->crtc mappings into the modesets */
Ville Syrjäläa2889602016-10-26 17:41:18 +03002280 for (i = 0; i < fb_helper->crtc_count; i++)
2281 drm_fb_helper_modeset_release(fb_helper,
2282 &fb_helper->crtc_info[i].mode_set);
Dave Airlie38651672010-03-30 05:34:13 +00002283
Chris Wilson966a6a12016-11-29 12:02:15 +00002284 drm_fb_helper_for_each_connector(fb_helper, i) {
Dave Airlie38651672010-03-30 05:34:13 +00002285 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00002286 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002287 struct drm_fb_offset *offset = &offsets[i];
Ville Syrjäläa2889602016-10-26 17:41:18 +03002288 struct drm_mode_set *modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00002289
Dave Airlie8be48d92010-03-30 05:34:14 +00002290 if (mode && fb_crtc) {
Ville Syrjäläa2889602016-10-26 17:41:18 +03002291 struct drm_connector *connector =
2292 fb_helper->connector_info[i]->connector;
2293
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002294 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2295 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002296
Dave Airlie8be48d92010-03-30 05:34:14 +00002297 fb_crtc->desired_mode = mode;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002298 fb_crtc->x = offset->x;
2299 fb_crtc->y = offset->y;
Dave Airlie8be48d92010-03-30 05:34:14 +00002300 modeset->mode = drm_mode_duplicate(dev,
2301 fb_crtc->desired_mode);
Thierry Redingad093602017-02-28 15:46:39 +01002302 drm_connector_get(connector);
Ville Syrjäläa2889602016-10-26 17:41:18 +03002303 modeset->connectors[modeset->num_connectors++] = connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01002304 modeset->fb = fb_helper->fb;
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002305 modeset->x = offset->x;
2306 modeset->y = offset->y;
Dave Airlie38651672010-03-30 05:34:13 +00002307 }
Dave Airlie38651672010-03-30 05:34:13 +00002308 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00002309out:
Dave Airlie38651672010-03-30 05:34:13 +00002310 kfree(crtcs);
2311 kfree(modes);
Dave Airlieb0ee9e72014-10-20 16:31:53 +10002312 kfree(offsets);
Dave Airlie38651672010-03-30 05:34:13 +00002313 kfree(enabled);
2314}
2315
2316/**
Daniel Vetter207fd322013-01-20 22:13:14 +01002317 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002318 * @fb_helper: fb_helper device struct
2319 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00002320 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002321 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00002322 * At the moment, this is a cloned configuration across all heads with
2323 * a new framebuffer object as the backing store.
2324 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002325 * Note that this also registers the fbdev and so allows userspace to call into
2326 * the driver through the fbdev interfaces.
2327 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +01002328 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2329 * to let the driver allocate and initialize the fbdev info structure and the
2330 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
Daniel Vetter207fd322013-01-20 22:13:14 +01002331 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2332 * values for the fbdev info structure.
2333 *
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002334 * HANG DEBUGGING:
2335 *
2336 * When you have fbcon support built-in or already loaded, this function will do
2337 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2338 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2339 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2340 * to consoles, not even serial console. This means when your driver crashes,
2341 * you will see absolutely nothing else but a system stuck in this function,
2342 * with no further output. Any kind of printk() you place within your own driver
2343 * or in the drm core modeset code will also never show up.
2344 *
2345 * Standard debug practice is to run the fbcon setup without taking the
2346 * console_lock as a hack, to be able to see backtraces and crashes on the
2347 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2348 * cmdline option.
2349 *
2350 * The other option is to just disable fbdev emulation since very likely the
Lyudeaf509d32016-05-04 11:28:53 -04002351 * first modeset from userspace will crash in the same way, and is even easier
2352 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
Daniel Vetter40f8cf42016-01-22 08:53:45 +01002353 * kernel cmdline option.
2354 *
Dave Airlie38651672010-03-30 05:34:13 +00002355 * RETURNS:
2356 * Zero if everything went ok, nonzero otherwise.
2357 */
Thierry Reding01934c22014-12-19 11:21:32 +01002358int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00002359{
Dave Airlie8be48d92010-03-30 05:34:14 +00002360 struct drm_device *dev = fb_helper->dev;
Chris Wilson966a6a12016-11-29 12:02:15 +00002361 struct fb_info *info;
Chris Wilson966a6a12016-11-29 12:02:15 +00002362 int ret;
Dave Airlie38651672010-03-30 05:34:13 +00002363
Daniel Vetterf64c5572015-08-25 15:45:13 +02002364 if (!drm_fbdev_emulation)
2365 return 0;
2366
Daniel Vetter53f19042014-03-20 14:26:35 +01002367 mutex_lock(&dev->mode_config.mutex);
Chris Wilson64e94402016-11-29 12:02:16 +00002368 drm_setup_crtcs(fb_helper,
2369 dev->mode_config.max_width,
2370 dev->mode_config.max_height);
Chris Wilson966a6a12016-11-29 12:02:15 +00002371 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2372 mutex_unlock(&dev->mode_config.mutex);
2373 if (ret)
2374 return ret;
Dave Airlie38651672010-03-30 05:34:13 +00002375
Chris Wilson966a6a12016-11-29 12:02:15 +00002376 info = fb_helper->fbdev;
2377 info->var.pixclock = 0;
2378 ret = register_framebuffer(info);
2379 if (ret < 0)
2380 return ret;
2381
2382 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2383 info->node, info->fix.id);
2384
Chris Wilsona53ca632016-11-29 12:02:17 +00002385 mutex_lock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002386 if (list_empty(&kernel_fb_helper_list))
2387 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2388
2389 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Chris Wilsona53ca632016-11-29 12:02:17 +00002390 mutex_unlock(&kernel_fb_helper_lock);
Chris Wilson966a6a12016-11-29 12:02:15 +00002391
2392 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002393}
Dave Airlie8be48d92010-03-30 05:34:14 +00002394EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00002395
Chris Wilson73943712011-04-22 11:03:57 +01002396/**
2397 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01002398 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01002399 * @fb_helper: the drm_fb_helper
2400 *
Chris Wilson73943712011-04-22 11:03:57 +01002401 * Scan the connectors attached to the fb_helper and try to put together a
Daniel Vetter62cacc72016-08-12 22:48:37 +02002402 * setup after notification of a change in output configuration.
Chris Wilson73943712011-04-22 11:03:57 +01002403 *
Daniel Vetter207fd322013-01-20 22:13:14 +01002404 * Called at runtime, takes the mode config locks to be able to check/change the
2405 * modeset configuration. Must be run from process context (which usually means
2406 * either the output polling work or a work item launched from the driver's
2407 * hotplug interrupt).
2408 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002409 * Note that drivers may call this even before calling
Lyudeaf509d32016-05-04 11:28:53 -04002410 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002411 * for a race-free fbcon setup and will make sure that the fbdev emulation will
2412 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01002413 *
Chris Wilson73943712011-04-22 11:03:57 +01002414 * RETURNS:
2415 * 0 on success and a non-zero error code otherwise.
2416 */
2417int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00002418{
Chris Wilson73943712011-04-22 11:03:57 +01002419 struct drm_device *dev = fb_helper->dev;
Dave Airlie4abe3522010-03-30 05:34:18 +00002420
Daniel Vetterf64c5572015-08-25 15:45:13 +02002421 if (!drm_fbdev_emulation)
2422 return 0;
2423
Chris Wilson64e94402016-11-29 12:02:16 +00002424 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02002425 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002426 fb_helper->delayed_hotplug = true;
Chris Wilson64e94402016-11-29 12:02:16 +00002427 mutex_unlock(&dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01002428 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002429 }
Dave Airlie38651672010-03-30 05:34:13 +00002430 DRM_DEBUG_KMS("\n");
2431
Chris Wilson64e94402016-11-29 12:02:16 +00002432 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
Dave Airlie4abe3522010-03-30 05:34:18 +00002433
Chris Wilson64e94402016-11-29 12:02:16 +00002434 mutex_unlock(&dev->mode_config.mutex);
Daniel Vetter89ced122013-04-11 14:26:55 +00002435
Daniel Vetter2180c3c2013-01-21 23:12:36 +01002436 drm_fb_helper_set_par(fb_helper->fbdev);
2437
2438 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00002439}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002440EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00002441
David Rientjes6a108a12011-01-20 14:44:16 -08002442/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002443 * but the module doesn't depend on any fb console symbols. At least
2444 * attempt to load fbcon to avoid leaving the system without a usable console.
2445 */
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002446int __init drm_fb_helper_modinit(void)
David Fries3ce05162010-12-12 12:39:22 -06002447{
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002448#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06002449 const char *name = "fbcon";
2450 struct module *fbcon;
2451
2452 mutex_lock(&module_mutex);
2453 fbcon = find_module(name);
2454 mutex_unlock(&module_mutex);
2455
2456 if (!fbcon)
2457 request_module_nowait(name);
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002458#endif
David Fries3ce05162010-12-12 12:39:22 -06002459 return 0;
2460}
Rafael Antognolli70412cf2016-01-21 15:10:18 -08002461EXPORT_SYMBOL(drm_fb_helper_modinit);