Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1 | /* |
| 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 Kamat | d56b1b9 | 2012-11-15 03:43:29 +0000 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 32 | #include <linux/console.h> |
Andy Shevchenko | 3b40a44 | 2010-02-02 14:40:32 -0800 | [diff] [blame] | 33 | #include <linux/kernel.h> |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 34 | #include <linux/sysrq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Paul Gortmaker | e0cd360 | 2011-08-30 11:04:30 -0400 | [diff] [blame] | 36 | #include <linux/module.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 37 | #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 Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 41 | #include <drm/drm_atomic.h> |
| 42 | #include <drm/drm_atomic_helper.h> |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 43 | |
Ville Syrjälä | 699fbee | 2016-09-19 16:33:44 +0300 | [diff] [blame] | 44 | #include "drm_crtc_helper_internal.h" |
| 45 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 46 | static bool drm_fbdev_emulation = true; |
| 47 | module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); |
| 48 | MODULE_PARM_DESC(fbdev_emulation, |
| 49 | "Enable legacy fbdev emulation [default=true]"); |
| 50 | |
Xinliang Liu | 5f15257 | 2017-02-15 17:19:08 +0100 | [diff] [blame] | 51 | static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; |
| 52 | module_param(drm_fbdev_overalloc, int, 0444); |
| 53 | MODULE_PARM_DESC(drm_fbdev_overalloc, |
| 54 | "Overallocation of the fbdev buffer (%) [default=" |
| 55 | __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); |
| 56 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 57 | static LIST_HEAD(kernel_fb_helper_list); |
Chris Wilson | a53ca63 | 2016-11-29 12:02:17 +0000 | [diff] [blame] | 58 | static DEFINE_MUTEX(kernel_fb_helper_lock); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 59 | |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 60 | /** |
| 61 | * DOC: fbdev helpers |
| 62 | * |
| 63 | * The fb helper functions are useful to provide an fbdev on top of a drm kernel |
Thierry Reding | 83c617c | 2014-04-29 11:44:35 +0200 | [diff] [blame] | 64 | * mode setting driver. They can be used mostly independently from the crtc |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 65 | * helper functions used by many drivers to implement the kernel mode setting |
| 66 | * interfaces. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 67 | * |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 68 | * 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 Vetter | ed84e25 | 2017-02-07 15:10:49 +0100 | [diff] [blame] | 72 | * Teardown is done with drm_fb_helper_fini() after the fbdev device is |
| 73 | * unregisters using drm_fb_helper_unregister_fbi(). |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 74 | * |
| 75 | * At runtime drivers should restore the fbdev console by calling |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 76 | * 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 Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 79 | * integration with the output polling code in drm_crtc_helper.c the modeset |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 80 | * code provides a &drm_mode_config_funcs.output_poll_changed callback. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 81 | * |
| 82 | * All other functions exported by the fb helper library can be used to |
| 83 | * implement the fbdev driver interface by the driver. |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 84 | * |
| 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 Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 89 | * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init() |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 90 | * 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ønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 97 | * |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 98 | * If &drm_framebuffer_funcs.dirty is set, the |
Noralf Trønnes | 2dad551 | 2016-05-11 18:09:17 +0200 | [diff] [blame] | 99 | * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 100 | * accumulate changes and schedule &drm_fb_helper.dirty_work to run right |
Noralf Trønnes | 2dad551 | 2016-05-11 18:09:17 +0200 | [diff] [blame] | 101 | * 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 Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 106 | */ |
| 107 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 108 | #define drm_fb_helper_for_each_connector(fbh, i__) \ |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 109 | for (({ lockdep_assert_held(&(fbh)->lock); }), \ |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 110 | i__ = 0; i__ < (fbh)->connector_count; i__++) |
| 111 | |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 112 | static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, |
| 113 | struct drm_connector *connector) |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 114 | { |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 115 | struct drm_fb_helper_connector *fb_conn; |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 116 | struct drm_fb_helper_connector **temp; |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 117 | unsigned int count; |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 118 | |
| 119 | if (!drm_fbdev_emulation) |
| 120 | return 0; |
| 121 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 122 | lockdep_assert_held(&fb_helper->lock); |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 123 | |
| 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 Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 130 | if (!temp) |
| 131 | return -ENOMEM; |
| 132 | |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 133 | fb_helper->connector_info_alloc_count = count; |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 134 | fb_helper->connector_info = temp; |
| 135 | } |
| 136 | |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 137 | fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL); |
| 138 | if (!fb_conn) |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 139 | return -ENOMEM; |
| 140 | |
| 141 | drm_connector_get(connector); |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 142 | fb_conn->connector = connector; |
| 143 | fb_helper->connector_info[fb_helper->connector_count++] = fb_conn; |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 144 | |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | } |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 147 | |
| 148 | int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, |
| 149 | struct drm_connector *connector) |
| 150 | { |
| 151 | int err; |
| 152 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 153 | mutex_lock(&fb_helper->lock); |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 154 | err = __drm_fb_helper_add_one_connector(fb_helper, connector); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 155 | mutex_unlock(&fb_helper->lock); |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 156 | |
| 157 | return err; |
| 158 | } |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 159 | EXPORT_SYMBOL(drm_fb_helper_add_one_connector); |
| 160 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 161 | /** |
| 162 | * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev |
| 163 | * emulation helper |
| 164 | * @fb_helper: fbdev initialized with drm_fb_helper_init |
| 165 | * |
| 166 | * This functions adds all the available connectors for use with the given |
| 167 | * fb_helper. This is a separate step to allow drivers to freely assign |
| 168 | * connectors to the fbdev, e.g. if some are reserved for special purposes or |
| 169 | * not adequate to be used for the fbcon. |
| 170 | * |
Daniel Vetter | 169faec | 2015-07-09 23:44:27 +0200 | [diff] [blame] | 171 | * This function is protected against concurrent connector hotadds/removals |
| 172 | * using drm_fb_helper_add_one_connector() and |
| 173 | * drm_fb_helper_remove_one_connector(). |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 174 | */ |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 175 | int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 176 | { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 177 | struct drm_device *dev = fb_helper->dev; |
| 178 | struct drm_connector *connector; |
Daniel Vetter | c36a325 | 2016-12-15 16:58:43 +0100 | [diff] [blame] | 179 | struct drm_connector_list_iter conn_iter; |
| 180 | int i, ret = 0; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 181 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 182 | if (!drm_fbdev_emulation) |
| 183 | return 0; |
| 184 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 185 | mutex_lock(&fb_helper->lock); |
Thierry Reding | b982dab | 2017-02-28 15:46:43 +0100 | [diff] [blame] | 186 | drm_connector_list_iter_begin(dev, &conn_iter); |
Daniel Vetter | c36a325 | 2016-12-15 16:58:43 +0100 | [diff] [blame] | 187 | drm_for_each_connector_iter(connector, &conn_iter) { |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 188 | ret = __drm_fb_helper_add_one_connector(fb_helper, connector); |
Maarten Lankhorst | 15fce29 | 2016-02-15 13:45:16 +0100 | [diff] [blame] | 189 | if (ret) |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 190 | goto fail; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 191 | } |
Daniel Vetter | c36a325 | 2016-12-15 16:58:43 +0100 | [diff] [blame] | 192 | goto out; |
| 193 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 194 | fail: |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 195 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Ville Syrjälä | 7dfcb36 | 2016-10-26 12:05:52 +0300 | [diff] [blame] | 196 | struct drm_fb_helper_connector *fb_helper_connector = |
| 197 | fb_helper->connector_info[i]; |
| 198 | |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 199 | drm_connector_put(fb_helper_connector->connector); |
Ville Syrjälä | 7dfcb36 | 2016-10-26 12:05:52 +0300 | [diff] [blame] | 200 | |
| 201 | kfree(fb_helper_connector); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 202 | fb_helper->connector_info[i] = NULL; |
| 203 | } |
| 204 | fb_helper->connector_count = 0; |
Daniel Vetter | c36a325 | 2016-12-15 16:58:43 +0100 | [diff] [blame] | 205 | out: |
Thierry Reding | b982dab | 2017-02-28 15:46:43 +0100 | [diff] [blame] | 206 | drm_connector_list_iter_end(&conn_iter); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 207 | mutex_unlock(&fb_helper->lock); |
Daniel Vetter | 169faec | 2015-07-09 23:44:27 +0200 | [diff] [blame] | 208 | |
Maarten Lankhorst | 15fce29 | 2016-02-15 13:45:16 +0100 | [diff] [blame] | 209 | return ret; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 210 | } |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 211 | EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 212 | |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 213 | static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, |
| 214 | struct drm_connector *connector) |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 215 | { |
| 216 | struct drm_fb_helper_connector *fb_helper_connector; |
| 217 | int i, j; |
| 218 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 219 | if (!drm_fbdev_emulation) |
| 220 | return 0; |
| 221 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 222 | lockdep_assert_held(&fb_helper->lock); |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 223 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 224 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 225 | if (fb_helper->connector_info[i]->connector == connector) |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | if (i == fb_helper->connector_count) |
| 230 | return -EINVAL; |
| 231 | fb_helper_connector = fb_helper->connector_info[i]; |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 232 | drm_connector_put(fb_helper_connector->connector); |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 233 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 234 | for (j = i + 1; j < fb_helper->connector_count; j++) |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 235 | fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 236 | |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 237 | fb_helper->connector_count--; |
| 238 | kfree(fb_helper_connector); |
Rob Clark | 2148f18 | 2015-01-26 10:11:08 -0500 | [diff] [blame] | 239 | |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 240 | return 0; |
| 241 | } |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 242 | |
| 243 | int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, |
| 244 | struct drm_connector *connector) |
| 245 | { |
| 246 | int err; |
| 247 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 248 | mutex_lock(&fb_helper->lock); |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 249 | err = __drm_fb_helper_remove_one_connector(fb_helper, connector); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 250 | mutex_unlock(&fb_helper->lock); |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 251 | |
| 252 | return err; |
| 253 | } |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 254 | EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); |
| 255 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 256 | static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) |
| 257 | { |
| 258 | uint16_t *r_base, *g_base, *b_base; |
| 259 | |
Laurent Pinchart | ebe0f24 | 2012-05-17 13:27:24 +0200 | [diff] [blame] | 260 | if (crtc->funcs->gamma_set == NULL) |
| 261 | return; |
| 262 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 263 | r_base = crtc->gamma_store; |
| 264 | g_base = r_base + crtc->gamma_size; |
| 265 | b_base = g_base + crtc->gamma_size; |
| 266 | |
Daniel Vetter | 6d124ff | 2017-04-03 10:33:01 +0200 | [diff] [blame] | 267 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, |
| 268 | crtc->gamma_size, NULL); |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 271 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 272 | * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 273 | * @info: fbdev registered by the helper |
| 274 | */ |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 275 | int drm_fb_helper_debug_enter(struct fb_info *info) |
| 276 | { |
| 277 | struct drm_fb_helper *helper = info->par; |
Jani Nikula | be26a66 | 2015-03-11 11:51:06 +0200 | [diff] [blame] | 278 | const struct drm_crtc_helper_funcs *funcs; |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 279 | int i; |
| 280 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 281 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
| 282 | for (i = 0; i < helper->crtc_count; i++) { |
| 283 | struct drm_mode_set *mode_set = |
| 284 | &helper->crtc_info[i].mode_set; |
| 285 | |
| 286 | if (!mode_set->crtc->enabled) |
| 287 | continue; |
| 288 | |
| 289 | funcs = mode_set->crtc->helper_private; |
Stefan Christ | 1b99b72 | 2016-11-14 00:03:11 +0100 | [diff] [blame] | 290 | if (funcs->mode_set_base_atomic == NULL) |
| 291 | continue; |
| 292 | |
Daniel Vetter | 9c79e0b | 2017-04-03 10:32:59 +0200 | [diff] [blame] | 293 | if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) |
| 294 | continue; |
| 295 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 296 | funcs->mode_set_base_atomic(mode_set->crtc, |
| 297 | mode_set->fb, |
| 298 | mode_set->x, |
Jason Wessel | 413d45d | 2010-09-26 06:47:25 -0500 | [diff] [blame] | 299 | mode_set->y, |
Jason Wessel | 21c74a8 | 2010-10-13 14:09:44 -0500 | [diff] [blame] | 300 | ENTER_ATOMIC_MODE_SET); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | EXPORT_SYMBOL(drm_fb_helper_debug_enter); |
| 307 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 308 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 309 | * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 310 | * @info: fbdev registered by the helper |
| 311 | */ |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 312 | int drm_fb_helper_debug_leave(struct fb_info *info) |
| 313 | { |
| 314 | struct drm_fb_helper *helper = info->par; |
| 315 | struct drm_crtc *crtc; |
Jani Nikula | be26a66 | 2015-03-11 11:51:06 +0200 | [diff] [blame] | 316 | const struct drm_crtc_helper_funcs *funcs; |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 317 | struct drm_framebuffer *fb; |
| 318 | int i; |
| 319 | |
| 320 | for (i = 0; i < helper->crtc_count; i++) { |
| 321 | struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 322 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 323 | crtc = mode_set->crtc; |
Maarten Lankhorst | 7114d2e | 2017-07-03 13:51:06 +0200 | [diff] [blame] | 324 | if (drm_drv_uses_atomic_modeset(crtc->dev)) |
| 325 | continue; |
| 326 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 327 | funcs = crtc->helper_private; |
Maarten Lankhorst | 7114d2e | 2017-07-03 13:51:06 +0200 | [diff] [blame] | 328 | fb = crtc->primary->fb; |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 329 | |
| 330 | if (!crtc->enabled) |
| 331 | continue; |
| 332 | |
| 333 | if (!fb) { |
| 334 | DRM_ERROR("no fb to restore??\n"); |
| 335 | continue; |
| 336 | } |
| 337 | |
Stefan Christ | 1b99b72 | 2016-11-14 00:03:11 +0100 | [diff] [blame] | 338 | if (funcs->mode_set_base_atomic == NULL) |
| 339 | continue; |
| 340 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 341 | drm_fb_helper_restore_lut_atomic(mode_set->crtc); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 342 | funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, |
Jason Wessel | 21c74a8 | 2010-10-13 14:09:44 -0500 | [diff] [blame] | 343 | crtc->y, LEAVE_ATOMIC_MODE_SET); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | EXPORT_SYMBOL(drm_fb_helper_debug_leave); |
| 349 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 350 | static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active) |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 351 | { |
| 352 | struct drm_device *dev = fb_helper->dev; |
| 353 | struct drm_plane *plane; |
| 354 | struct drm_atomic_state *state; |
| 355 | int i, ret; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 356 | unsigned int plane_mask; |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 357 | struct drm_modeset_acquire_ctx ctx; |
| 358 | |
| 359 | drm_modeset_acquire_init(&ctx, 0); |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 360 | |
| 361 | state = drm_atomic_state_alloc(dev); |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 362 | if (!state) { |
| 363 | ret = -ENOMEM; |
| 364 | goto out_ctx; |
| 365 | } |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 366 | |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 367 | state->acquire_ctx = &ctx; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 368 | retry: |
Maarten Lankhorst | f72c6b3 | 2015-11-11 11:29:10 +0100 | [diff] [blame] | 369 | plane_mask = 0; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 370 | drm_for_each_plane(plane, dev) { |
| 371 | struct drm_plane_state *plane_state; |
| 372 | |
| 373 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 374 | if (IS_ERR(plane_state)) { |
| 375 | ret = PTR_ERR(plane_state); |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 376 | goto out_state; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 377 | } |
| 378 | |
Robert Foss | c2c446a | 2017-05-19 16:50:17 -0400 | [diff] [blame] | 379 | plane_state->rotation = DRM_MODE_ROTATE_0; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 380 | |
Maarten Lankhorst | f72c6b3 | 2015-11-11 11:29:10 +0100 | [diff] [blame] | 381 | plane->old_fb = plane->fb; |
| 382 | plane_mask |= 1 << drm_plane_index(plane); |
| 383 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 384 | /* disable non-primary: */ |
| 385 | if (plane->type == DRM_PLANE_TYPE_PRIMARY) |
| 386 | continue; |
| 387 | |
| 388 | ret = __drm_atomic_helper_disable_plane(plane, plane_state); |
| 389 | if (ret != 0) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 390 | goto out_state; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 393 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 394 | struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; |
| 395 | |
| 396 | ret = __drm_atomic_helper_set_config(mode_set, state); |
| 397 | if (ret != 0) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 398 | goto out_state; |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 399 | |
| 400 | /* |
| 401 | * __drm_atomic_helper_set_config() sets active when a |
| 402 | * mode is set, unconditionally clear it if we force DPMS off |
| 403 | */ |
| 404 | if (!active) { |
| 405 | struct drm_crtc *crtc = mode_set->crtc; |
| 406 | struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); |
| 407 | |
| 408 | crtc_state->active = false; |
| 409 | } |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | ret = drm_atomic_commit(state); |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 413 | |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 414 | out_state: |
Maarten Lankhorst | f72c6b3 | 2015-11-11 11:29:10 +0100 | [diff] [blame] | 415 | drm_atomic_clean_old_fb(dev, plane_mask, ret); |
Matt Roper | 9428403 | 2015-09-21 17:21:48 -0700 | [diff] [blame] | 416 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 417 | if (ret == -EDEADLK) |
| 418 | goto backoff; |
| 419 | |
Chris Wilson | 0853695 | 2016-10-14 13:18:18 +0100 | [diff] [blame] | 420 | drm_atomic_state_put(state); |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 421 | out_ctx: |
| 422 | drm_modeset_drop_locks(&ctx); |
| 423 | drm_modeset_acquire_fini(&ctx); |
| 424 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 425 | return ret; |
| 426 | |
| 427 | backoff: |
| 428 | drm_atomic_state_clear(state); |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 429 | drm_modeset_backoff(&ctx); |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 430 | |
| 431 | goto retry; |
| 432 | } |
| 433 | |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 434 | static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper) |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 435 | { |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 436 | struct drm_device *dev = fb_helper->dev; |
| 437 | struct drm_plane *plane; |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 438 | int i, ret = 0; |
Daniel Vetter | 6aed8ec | 2013-01-20 17:32:21 +0100 | [diff] [blame] | 439 | |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 440 | drm_modeset_lock_all(fb_helper->dev); |
Daniel Vetter | 6295d60 | 2015-07-09 23:44:25 +0200 | [diff] [blame] | 441 | drm_for_each_plane(plane, dev) { |
Matt Roper | e27dde3 | 2014-04-01 15:22:30 -0700 | [diff] [blame] | 442 | if (plane->type != DRM_PLANE_TYPE_PRIMARY) |
| 443 | drm_plane_force_disable(plane); |
Daniel Vetter | 6aed8ec | 2013-01-20 17:32:21 +0100 | [diff] [blame] | 444 | |
Ville Syrjälä | 6686df8 | 2016-10-21 22:22:45 +0300 | [diff] [blame] | 445 | if (plane->rotation_property) |
Ville Syrjälä | d138dd3 | 2016-09-26 19:30:48 +0300 | [diff] [blame] | 446 | drm_mode_plane_set_obj_prop(plane, |
| 447 | plane->rotation_property, |
Robert Foss | c2c446a | 2017-05-19 16:50:17 -0400 | [diff] [blame] | 448 | DRM_MODE_ROTATE_0); |
Sonika Jindal | 9783de2 | 2014-08-05 11:26:57 +0530 | [diff] [blame] | 449 | } |
| 450 | |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 451 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 452 | struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 453 | struct drm_crtc *crtc = mode_set->crtc; |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 454 | |
Alex Deucher | 03f9abb | 2015-09-30 14:47:37 -0400 | [diff] [blame] | 455 | if (crtc->funcs->cursor_set2) { |
| 456 | ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0); |
| 457 | if (ret) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 458 | goto out; |
Alex Deucher | 03f9abb | 2015-09-30 14:47:37 -0400 | [diff] [blame] | 459 | } else if (crtc->funcs->cursor_set) { |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 460 | ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); |
| 461 | if (ret) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 462 | goto out; |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 463 | } |
| 464 | |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 465 | ret = drm_mode_set_config_internal(mode_set); |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 466 | if (ret) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 467 | goto out; |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 468 | } |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 469 | out: |
| 470 | drm_modeset_unlock_all(fb_helper->dev); |
Daniel Vetter | b7bdf0a8 | 2015-08-25 17:20:28 +0200 | [diff] [blame] | 471 | |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 472 | return ret; |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 473 | } |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 474 | |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 475 | static int restore_fbdev_mode(struct drm_fb_helper *fb_helper) |
| 476 | { |
| 477 | struct drm_device *dev = fb_helper->dev; |
| 478 | |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 479 | if (drm_drv_uses_atomic_modeset(dev)) |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 480 | return restore_fbdev_mode_atomic(fb_helper, true); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 481 | else |
| 482 | return restore_fbdev_mode_legacy(fb_helper); |
| 483 | } |
| 484 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 485 | /** |
| 486 | * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration |
| 487 | * @fb_helper: fbcon to restore |
| 488 | * |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 489 | * This should be called from driver's drm &drm_driver.lastclose callback |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 490 | * when implementing an fbcon on top of kms using this helper. This ensures that |
| 491 | * the user isn't greeted with a black screen when e.g. X dies. |
Daniel Vetter | b7bdf0a8 | 2015-08-25 17:20:28 +0200 | [diff] [blame] | 492 | * |
| 493 | * RETURNS: |
| 494 | * Zero if everything went ok, negative error code otherwise. |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 495 | */ |
Daniel Vetter | b7bdf0a8 | 2015-08-25 17:20:28 +0200 | [diff] [blame] | 496 | int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 497 | { |
Daniel Vetter | b7bdf0a8 | 2015-08-25 17:20:28 +0200 | [diff] [blame] | 498 | bool do_delayed; |
| 499 | int ret; |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 500 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 501 | if (!drm_fbdev_emulation) |
| 502 | return -ENODEV; |
| 503 | |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 504 | if (READ_ONCE(fb_helper->deferred_setup)) |
| 505 | return 0; |
| 506 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 507 | mutex_lock(&fb_helper->lock); |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 508 | ret = restore_fbdev_mode(fb_helper); |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 509 | |
| 510 | do_delayed = fb_helper->delayed_hotplug; |
| 511 | if (do_delayed) |
| 512 | fb_helper->delayed_hotplug = false; |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 513 | mutex_unlock(&fb_helper->lock); |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 514 | |
| 515 | if (do_delayed) |
| 516 | drm_fb_helper_hotplug_event(fb_helper); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 517 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 518 | return ret; |
| 519 | } |
| 520 | EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 521 | |
Geert Uytterhoeven | 2c4124f | 2015-08-04 15:22:11 +0200 | [diff] [blame] | 522 | static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) |
| 523 | { |
| 524 | struct drm_device *dev = fb_helper->dev; |
| 525 | struct drm_crtc *crtc; |
| 526 | int bound = 0, crtcs_bound = 0; |
| 527 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 528 | /* |
| 529 | * Sometimes user space wants everything disabled, so don't steal the |
| 530 | * display if there's a master. |
| 531 | */ |
Johannes Berg | f17b3ea | 2016-08-11 11:50:21 +0200 | [diff] [blame] | 532 | if (READ_ONCE(dev->master)) |
Geert Uytterhoeven | 2c4124f | 2015-08-04 15:22:11 +0200 | [diff] [blame] | 533 | return false; |
| 534 | |
| 535 | drm_for_each_crtc(crtc, dev) { |
Daniel Vetter | bdac4a0 | 2017-07-04 17:18:24 +0200 | [diff] [blame] | 536 | drm_modeset_lock(&crtc->mutex, NULL); |
Geert Uytterhoeven | 2c4124f | 2015-08-04 15:22:11 +0200 | [diff] [blame] | 537 | if (crtc->primary->fb) |
| 538 | crtcs_bound++; |
| 539 | if (crtc->primary->fb == fb_helper->fb) |
| 540 | bound++; |
Daniel Vetter | bdac4a0 | 2017-07-04 17:18:24 +0200 | [diff] [blame] | 541 | drm_modeset_unlock(&crtc->mutex); |
Geert Uytterhoeven | 2c4124f | 2015-08-04 15:22:11 +0200 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | if (bound < crtcs_bound) |
| 545 | return false; |
| 546 | |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | #ifdef CONFIG_MAGIC_SYSRQ |
Daniel Vetter | d21bf46 | 2013-01-20 18:09:52 +0100 | [diff] [blame] | 551 | /* |
| 552 | * restore fbcon display for all kms driver's using this helper, used for sysrq |
| 553 | * and panic handling. |
| 554 | */ |
Sachin Kamat | 78b9c35 | 2012-08-01 17:15:32 +0530 | [diff] [blame] | 555 | static bool drm_fb_helper_force_kernel_mode(void) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 556 | { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 557 | bool ret, error = false; |
| 558 | struct drm_fb_helper *helper; |
| 559 | |
| 560 | if (list_empty(&kernel_fb_helper_list)) |
| 561 | return false; |
| 562 | |
| 563 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
Thierry Reding | b77f076 | 2014-04-29 11:44:32 +0200 | [diff] [blame] | 564 | struct drm_device *dev = helper->dev; |
| 565 | |
| 566 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 567 | continue; |
| 568 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 569 | mutex_lock(&helper->lock); |
Geert Uytterhoeven | 3d9e35a | 2015-08-04 15:22:10 +0200 | [diff] [blame] | 570 | ret = restore_fbdev_mode(helper); |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 571 | if (ret) |
| 572 | error = true; |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 573 | mutex_unlock(&helper->lock); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 574 | } |
| 575 | return error; |
| 576 | } |
| 577 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 578 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
| 579 | { |
Daniel Vetter | d21bf46 | 2013-01-20 18:09:52 +0100 | [diff] [blame] | 580 | bool ret; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 581 | |
Daniel Vetter | d21bf46 | 2013-01-20 18:09:52 +0100 | [diff] [blame] | 582 | ret = drm_fb_helper_force_kernel_mode(); |
| 583 | if (ret == true) |
| 584 | DRM_ERROR("Failed to restore crtc configuration\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 585 | } |
| 586 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); |
| 587 | |
Dmitry Torokhov | 1495cc9 | 2010-08-17 21:15:46 -0700 | [diff] [blame] | 588 | static void drm_fb_helper_sysrq(int dummy1) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 589 | { |
| 590 | schedule_work(&drm_fb_helper_restore_work); |
| 591 | } |
| 592 | |
| 593 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { |
| 594 | .handler = drm_fb_helper_sysrq, |
| 595 | .help_msg = "force-fb(V)", |
| 596 | .action_msg = "Restore framebuffer console", |
| 597 | }; |
Randy Dunlap | b8c40d6 | 2010-03-25 18:29:05 +0000 | [diff] [blame] | 598 | #else |
| 599 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; |
Mikael Pettersson | bea1d35 | 2009-09-28 18:26:25 +0200 | [diff] [blame] | 600 | #endif |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 601 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 602 | static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 603 | { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 604 | struct drm_device *dev = fb_helper->dev; |
| 605 | struct drm_crtc *crtc; |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 606 | struct drm_connector *connector; |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 607 | int i, j; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 608 | |
Daniel Vetter | bdac4a0 | 2017-07-04 17:18:24 +0200 | [diff] [blame] | 609 | drm_modeset_lock_all(dev); |
Jesse Barnes | e87b2c4 | 2009-09-17 18:14:41 -0700 | [diff] [blame] | 610 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 611 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 612 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 613 | if (!crtc->enabled) |
| 614 | continue; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 615 | |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 616 | /* Walk the connectors & encoders on this fb turning them on/off */ |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 617 | drm_fb_helper_for_each_connector(fb_helper, j) { |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 618 | connector = fb_helper->connector_info[j]->connector; |
Daniel Vetter | e04190e | 2012-09-07 10:14:52 +0200 | [diff] [blame] | 619 | connector->funcs->dpms(connector, dpms_mode); |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 620 | drm_object_property_set_value(&connector->base, |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 621 | dev->mode_config.dpms_property, dpms_mode); |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 622 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 623 | } |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 624 | drm_modeset_unlock_all(dev); |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) |
| 628 | { |
| 629 | struct drm_fb_helper *fb_helper = info->par; |
| 630 | |
| 631 | /* |
| 632 | * For each CRTC in this fb, turn the connectors on/off. |
| 633 | */ |
| 634 | mutex_lock(&fb_helper->lock); |
| 635 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 636 | mutex_unlock(&fb_helper->lock); |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | if (drm_drv_uses_atomic_modeset(fb_helper->dev)) |
| 641 | restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON); |
| 642 | else |
| 643 | dpms_legacy(fb_helper, dpms_mode); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 644 | mutex_unlock(&fb_helper->lock); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 645 | } |
| 646 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 647 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 648 | * drm_fb_helper_blank - implementation for &fb_ops.fb_blank |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 649 | * @blank: desired blanking state |
| 650 | * @info: fbdev registered by the helper |
| 651 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 652 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
| 653 | { |
Daniel Vetter | c50bfd0 | 2015-07-28 13:18:40 +0200 | [diff] [blame] | 654 | if (oops_in_progress) |
| 655 | return -EBUSY; |
| 656 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 657 | switch (blank) { |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 658 | /* Display: On; HSync: On, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 659 | case FB_BLANK_UNBLANK: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 660 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 661 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 662 | /* Display: Off; HSync: On, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 663 | case FB_BLANK_NORMAL: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 664 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 665 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 666 | /* Display: Off; HSync: Off, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 667 | case FB_BLANK_HSYNC_SUSPEND: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 668 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 669 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 670 | /* Display: Off; HSync: On, VSync: Off */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 671 | case FB_BLANK_VSYNC_SUSPEND: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 672 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 673 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 674 | /* Display: Off; HSync: Off, VSync: Off */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 675 | case FB_BLANK_POWERDOWN: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 676 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 677 | break; |
| 678 | } |
| 679 | return 0; |
| 680 | } |
| 681 | EXPORT_SYMBOL(drm_fb_helper_blank); |
| 682 | |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 683 | static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper, |
| 684 | struct drm_mode_set *modeset) |
| 685 | { |
| 686 | int i; |
| 687 | |
| 688 | for (i = 0; i < modeset->num_connectors; i++) { |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 689 | drm_connector_put(modeset->connectors[i]); |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 690 | modeset->connectors[i] = NULL; |
| 691 | } |
| 692 | modeset->num_connectors = 0; |
| 693 | |
| 694 | drm_mode_destroy(helper->dev, modeset->mode); |
| 695 | modeset->mode = NULL; |
| 696 | |
| 697 | /* FIXME should hold a ref? */ |
| 698 | modeset->fb = NULL; |
| 699 | } |
| 700 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 701 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) |
| 702 | { |
| 703 | int i; |
| 704 | |
Dave Airlie | 6e86d58 | 2016-04-27 11:24:51 +1000 | [diff] [blame] | 705 | for (i = 0; i < helper->connector_count; i++) { |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 706 | drm_connector_put(helper->connector_info[i]->connector); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 707 | kfree(helper->connector_info[i]); |
Dave Airlie | 6e86d58 | 2016-04-27 11:24:51 +1000 | [diff] [blame] | 708 | } |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 709 | kfree(helper->connector_info); |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 710 | |
Sascha Hauer | a1b7736 | 2012-02-01 11:38:22 +0100 | [diff] [blame] | 711 | for (i = 0; i < helper->crtc_count; i++) { |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 712 | struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set; |
| 713 | |
| 714 | drm_fb_helper_modeset_release(helper, modeset); |
| 715 | kfree(modeset->connectors); |
Sascha Hauer | a1b7736 | 2012-02-01 11:38:22 +0100 | [diff] [blame] | 716 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 717 | kfree(helper->crtc_info); |
| 718 | } |
| 719 | |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 720 | static void drm_fb_helper_resume_worker(struct work_struct *work) |
| 721 | { |
| 722 | struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, |
| 723 | resume_work); |
| 724 | |
| 725 | console_lock(); |
| 726 | fb_set_suspend(helper->fbdev, 0); |
| 727 | console_unlock(); |
| 728 | } |
| 729 | |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 730 | static void drm_fb_helper_dirty_work(struct work_struct *work) |
| 731 | { |
| 732 | struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, |
| 733 | dirty_work); |
| 734 | struct drm_clip_rect *clip = &helper->dirty_clip; |
| 735 | struct drm_clip_rect clip_copy; |
| 736 | unsigned long flags; |
| 737 | |
| 738 | spin_lock_irqsave(&helper->dirty_lock, flags); |
| 739 | clip_copy = *clip; |
| 740 | clip->x1 = clip->y1 = ~0; |
| 741 | clip->x2 = clip->y2 = 0; |
| 742 | spin_unlock_irqrestore(&helper->dirty_lock, flags); |
| 743 | |
Takashi Iwai | 87d3b65 | 2016-10-20 17:05:30 +0200 | [diff] [blame] | 744 | /* call dirty callback only when it has been really touched */ |
| 745 | if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) |
| 746 | helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 747 | } |
| 748 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 749 | /** |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 750 | * drm_fb_helper_prepare - setup a drm_fb_helper structure |
| 751 | * @dev: DRM device |
| 752 | * @helper: driver-allocated fbdev helper structure to set up |
| 753 | * @funcs: pointer to structure of functions associate with this helper |
| 754 | * |
| 755 | * Sets up the bare minimum to make the framebuffer helper usable. This is |
| 756 | * useful to implement race-free initialization of the polling helpers. |
| 757 | */ |
| 758 | void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, |
| 759 | const struct drm_fb_helper_funcs *funcs) |
| 760 | { |
| 761 | INIT_LIST_HEAD(&helper->kernel_fb_list); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 762 | spin_lock_init(&helper->dirty_lock); |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 763 | INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 764 | INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); |
| 765 | helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 766 | mutex_init(&helper->lock); |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 767 | helper->funcs = funcs; |
| 768 | helper->dev = dev; |
| 769 | } |
| 770 | EXPORT_SYMBOL(drm_fb_helper_prepare); |
| 771 | |
| 772 | /** |
Daniel Vetter | ed84e25 | 2017-02-07 15:10:49 +0100 | [diff] [blame] | 773 | * drm_fb_helper_init - initialize a &struct drm_fb_helper |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 774 | * @dev: drm device |
| 775 | * @fb_helper: driver-allocated fbdev helper structure to initialize |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 776 | * @max_conn_count: max connector count |
| 777 | * |
| 778 | * This allocates the structures for the fbdev helper with the given limits. |
| 779 | * Note that this won't yet touch the hardware (through the driver interfaces) |
| 780 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() |
| 781 | * to allow driver writes more control over the exact init sequence. |
| 782 | * |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 783 | * Drivers must call drm_fb_helper_prepare() before calling this function. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 784 | * |
| 785 | * RETURNS: |
| 786 | * Zero if everything went ok, nonzero otherwise. |
| 787 | */ |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 788 | int drm_fb_helper_init(struct drm_device *dev, |
| 789 | struct drm_fb_helper *fb_helper, |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 790 | int max_conn_count) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 791 | { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 792 | struct drm_crtc *crtc; |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 793 | struct drm_mode_config *config = &dev->mode_config; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 794 | int i; |
| 795 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 796 | if (!drm_fbdev_emulation) |
| 797 | return 0; |
| 798 | |
Xiubo Li | 04cfe97 | 2014-03-10 09:33:58 +0800 | [diff] [blame] | 799 | if (!max_conn_count) |
| 800 | return -EINVAL; |
| 801 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 802 | fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 803 | if (!fb_helper->crtc_info) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 804 | return -ENOMEM; |
| 805 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 806 | fb_helper->crtc_count = config->num_crtc; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 807 | fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); |
| 808 | if (!fb_helper->connector_info) { |
| 809 | kfree(fb_helper->crtc_info); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 810 | return -ENOMEM; |
| 811 | } |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 812 | fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 813 | fb_helper->connector_count = 0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 814 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 815 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 816 | fb_helper->crtc_info[i].mode_set.connectors = |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 817 | kcalloc(max_conn_count, |
| 818 | sizeof(struct drm_connector *), |
| 819 | GFP_KERNEL); |
| 820 | |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 821 | if (!fb_helper->crtc_info[i].mode_set.connectors) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 822 | goto out_free; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 823 | fb_helper->crtc_info[i].mode_set.num_connectors = 0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | i = 0; |
Daniel Vetter | 6295d60 | 2015-07-09 23:44:25 +0200 | [diff] [blame] | 827 | drm_for_each_crtc(crtc, dev) { |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 828 | fb_helper->crtc_info[i].mode_set.crtc = crtc; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 829 | i++; |
| 830 | } |
Sascha Hauer | e9ad318 | 2012-02-01 11:38:25 +0100 | [diff] [blame] | 831 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 832 | return 0; |
| 833 | out_free: |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 834 | drm_fb_helper_crtc_free(fb_helper); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 835 | return -ENOMEM; |
| 836 | } |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 837 | EXPORT_SYMBOL(drm_fb_helper_init); |
| 838 | |
Archit Taneja | b8017d6 | 2015-07-22 14:57:56 +0530 | [diff] [blame] | 839 | /** |
| 840 | * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members |
| 841 | * @fb_helper: driver-allocated fbdev helper |
| 842 | * |
| 843 | * A helper to alloc fb_info and the members cmap and apertures. Called |
Daniel Vetter | da7bdda | 2017-02-07 17:16:03 +0100 | [diff] [blame] | 844 | * by the driver within the fb_probe fb_helper callback function. Drivers do not |
| 845 | * need to release the allocated fb_info structure themselves, this is |
| 846 | * automatically done when calling drm_fb_helper_fini(). |
Archit Taneja | b8017d6 | 2015-07-22 14:57:56 +0530 | [diff] [blame] | 847 | * |
| 848 | * RETURNS: |
| 849 | * fb_info pointer if things went okay, pointer containing error code |
| 850 | * otherwise |
| 851 | */ |
| 852 | struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) |
| 853 | { |
| 854 | struct device *dev = fb_helper->dev->dev; |
| 855 | struct fb_info *info; |
| 856 | int ret; |
| 857 | |
| 858 | info = framebuffer_alloc(0, dev); |
| 859 | if (!info) |
| 860 | return ERR_PTR(-ENOMEM); |
| 861 | |
| 862 | ret = fb_alloc_cmap(&info->cmap, 256, 0); |
| 863 | if (ret) |
| 864 | goto err_release; |
| 865 | |
| 866 | info->apertures = alloc_apertures(1); |
| 867 | if (!info->apertures) { |
| 868 | ret = -ENOMEM; |
| 869 | goto err_free_cmap; |
| 870 | } |
| 871 | |
| 872 | fb_helper->fbdev = info; |
| 873 | |
| 874 | return info; |
| 875 | |
| 876 | err_free_cmap: |
| 877 | fb_dealloc_cmap(&info->cmap); |
| 878 | err_release: |
| 879 | framebuffer_release(info); |
| 880 | return ERR_PTR(ret); |
| 881 | } |
| 882 | EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); |
| 883 | |
| 884 | /** |
| 885 | * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device |
| 886 | * @fb_helper: driver-allocated fbdev helper |
| 887 | * |
| 888 | * A wrapper around unregister_framebuffer, to release the fb_info |
Daniel Vetter | ed84e25 | 2017-02-07 15:10:49 +0100 | [diff] [blame] | 889 | * framebuffer device. This must be called before releasing all resources for |
| 890 | * @fb_helper by calling drm_fb_helper_fini(). |
Archit Taneja | b8017d6 | 2015-07-22 14:57:56 +0530 | [diff] [blame] | 891 | */ |
| 892 | void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) |
| 893 | { |
| 894 | if (fb_helper && fb_helper->fbdev) |
| 895 | unregister_framebuffer(fb_helper->fbdev); |
| 896 | } |
| 897 | EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); |
| 898 | |
| 899 | /** |
Daniel Vetter | ed84e25 | 2017-02-07 15:10:49 +0100 | [diff] [blame] | 900 | * drm_fb_helper_fini - finialize a &struct drm_fb_helper |
| 901 | * @fb_helper: driver-allocated fbdev helper |
| 902 | * |
| 903 | * This cleans up all remaining resources associated with @fb_helper. Must be |
| 904 | * called after drm_fb_helper_unlink_fbi() was called. |
| 905 | */ |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 906 | void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) |
| 907 | { |
Daniel Vetter | da7bdda | 2017-02-07 17:16:03 +0100 | [diff] [blame] | 908 | struct fb_info *info; |
| 909 | |
| 910 | if (!drm_fbdev_emulation || !fb_helper) |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 911 | return; |
| 912 | |
Noralf Trønnes | b52f09c | 2017-08-28 19:17:43 +0200 | [diff] [blame] | 913 | cancel_work_sync(&fb_helper->resume_work); |
| 914 | cancel_work_sync(&fb_helper->dirty_work); |
| 915 | |
Daniel Vetter | da7bdda | 2017-02-07 17:16:03 +0100 | [diff] [blame] | 916 | info = fb_helper->fbdev; |
| 917 | if (info) { |
| 918 | if (info->cmap.len) |
| 919 | fb_dealloc_cmap(&info->cmap); |
| 920 | framebuffer_release(info); |
| 921 | } |
| 922 | fb_helper->fbdev = NULL; |
| 923 | |
Chris Wilson | a53ca63 | 2016-11-29 12:02:17 +0000 | [diff] [blame] | 924 | mutex_lock(&kernel_fb_helper_lock); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 925 | if (!list_empty(&fb_helper->kernel_fb_list)) { |
| 926 | list_del(&fb_helper->kernel_fb_list); |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 927 | if (list_empty(&kernel_fb_helper_list)) |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 928 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 929 | } |
Chris Wilson | a53ca63 | 2016-11-29 12:02:17 +0000 | [diff] [blame] | 930 | mutex_unlock(&kernel_fb_helper_lock); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 931 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 932 | mutex_destroy(&fb_helper->lock); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 933 | drm_fb_helper_crtc_free(fb_helper); |
| 934 | |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 935 | } |
| 936 | EXPORT_SYMBOL(drm_fb_helper_fini); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 937 | |
Archit Taneja | 47074ab | 2015-07-22 14:57:57 +0530 | [diff] [blame] | 938 | /** |
| 939 | * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer |
| 940 | * @fb_helper: driver-allocated fbdev helper |
| 941 | * |
| 942 | * A wrapper around unlink_framebuffer implemented by fbdev core |
| 943 | */ |
| 944 | void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) |
| 945 | { |
| 946 | if (fb_helper && fb_helper->fbdev) |
| 947 | unlink_framebuffer(fb_helper->fbdev); |
| 948 | } |
| 949 | EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); |
| 950 | |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 951 | static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, |
| 952 | u32 width, u32 height) |
| 953 | { |
| 954 | struct drm_fb_helper *helper = info->par; |
| 955 | struct drm_clip_rect *clip = &helper->dirty_clip; |
| 956 | unsigned long flags; |
| 957 | |
| 958 | if (!helper->fb->funcs->dirty) |
| 959 | return; |
| 960 | |
| 961 | spin_lock_irqsave(&helper->dirty_lock, flags); |
| 962 | clip->x1 = min_t(u32, clip->x1, x); |
| 963 | clip->y1 = min_t(u32, clip->y1, y); |
| 964 | clip->x2 = max_t(u32, clip->x2, x + width); |
| 965 | clip->y2 = max_t(u32, clip->y2, y + height); |
| 966 | spin_unlock_irqrestore(&helper->dirty_lock, flags); |
| 967 | |
| 968 | schedule_work(&helper->dirty_work); |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * drm_fb_helper_deferred_io() - fbdev deferred_io callback function |
| 973 | * @info: fb_info struct pointer |
| 974 | * @pagelist: list of dirty mmap framebuffer pages |
| 975 | * |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 976 | * This function is used as the &fb_deferred_io.deferred_io |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 977 | * callback function for flushing the fbdev mmap writes. |
| 978 | */ |
| 979 | void drm_fb_helper_deferred_io(struct fb_info *info, |
| 980 | struct list_head *pagelist) |
| 981 | { |
| 982 | unsigned long start, end, min, max; |
| 983 | struct page *page; |
| 984 | u32 y1, y2; |
| 985 | |
| 986 | min = ULONG_MAX; |
| 987 | max = 0; |
| 988 | list_for_each_entry(page, pagelist, lru) { |
| 989 | start = page->index << PAGE_SHIFT; |
| 990 | end = start + PAGE_SIZE - 1; |
| 991 | min = min(min, start); |
| 992 | max = max(max, end); |
| 993 | } |
| 994 | |
| 995 | if (min < max) { |
| 996 | y1 = min / info->fix.line_length; |
| 997 | y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), |
| 998 | info->var.yres); |
| 999 | drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); |
| 1000 | } |
| 1001 | } |
| 1002 | EXPORT_SYMBOL(drm_fb_helper_deferred_io); |
| 1003 | |
Archit Taneja | cbb1a82 | 2015-07-31 16:21:41 +0530 | [diff] [blame] | 1004 | /** |
| 1005 | * drm_fb_helper_sys_read - wrapper around fb_sys_read |
| 1006 | * @info: fb_info struct pointer |
| 1007 | * @buf: userspace buffer to read from framebuffer memory |
| 1008 | * @count: number of bytes to read from framebuffer memory |
| 1009 | * @ppos: read offset within framebuffer memory |
| 1010 | * |
| 1011 | * A wrapper around fb_sys_read implemented by fbdev core |
| 1012 | */ |
| 1013 | ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, |
| 1014 | size_t count, loff_t *ppos) |
| 1015 | { |
| 1016 | return fb_sys_read(info, buf, count, ppos); |
| 1017 | } |
| 1018 | EXPORT_SYMBOL(drm_fb_helper_sys_read); |
| 1019 | |
| 1020 | /** |
| 1021 | * drm_fb_helper_sys_write - wrapper around fb_sys_write |
| 1022 | * @info: fb_info struct pointer |
| 1023 | * @buf: userspace buffer to write to framebuffer memory |
| 1024 | * @count: number of bytes to write to framebuffer memory |
| 1025 | * @ppos: write offset within framebuffer memory |
| 1026 | * |
| 1027 | * A wrapper around fb_sys_write implemented by fbdev core |
| 1028 | */ |
| 1029 | ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, |
| 1030 | size_t count, loff_t *ppos) |
| 1031 | { |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1032 | ssize_t ret; |
| 1033 | |
| 1034 | ret = fb_sys_write(info, buf, count, ppos); |
| 1035 | if (ret > 0) |
| 1036 | drm_fb_helper_dirty(info, 0, 0, info->var.xres, |
| 1037 | info->var.yres); |
| 1038 | |
| 1039 | return ret; |
Archit Taneja | cbb1a82 | 2015-07-31 16:21:41 +0530 | [diff] [blame] | 1040 | } |
| 1041 | EXPORT_SYMBOL(drm_fb_helper_sys_write); |
| 1042 | |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1043 | /** |
| 1044 | * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect |
| 1045 | * @info: fbdev registered by the helper |
| 1046 | * @rect: info about rectangle to fill |
| 1047 | * |
| 1048 | * A wrapper around sys_fillrect implemented by fbdev core |
| 1049 | */ |
| 1050 | void drm_fb_helper_sys_fillrect(struct fb_info *info, |
| 1051 | const struct fb_fillrect *rect) |
| 1052 | { |
| 1053 | sys_fillrect(info, rect); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1054 | drm_fb_helper_dirty(info, rect->dx, rect->dy, |
| 1055 | rect->width, rect->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1056 | } |
| 1057 | EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); |
| 1058 | |
| 1059 | /** |
| 1060 | * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea |
| 1061 | * @info: fbdev registered by the helper |
| 1062 | * @area: info about area to copy |
| 1063 | * |
| 1064 | * A wrapper around sys_copyarea implemented by fbdev core |
| 1065 | */ |
| 1066 | void drm_fb_helper_sys_copyarea(struct fb_info *info, |
| 1067 | const struct fb_copyarea *area) |
| 1068 | { |
| 1069 | sys_copyarea(info, area); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1070 | drm_fb_helper_dirty(info, area->dx, area->dy, |
| 1071 | area->width, area->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1072 | } |
| 1073 | EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); |
| 1074 | |
| 1075 | /** |
| 1076 | * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit |
| 1077 | * @info: fbdev registered by the helper |
| 1078 | * @image: info about image to blit |
| 1079 | * |
| 1080 | * A wrapper around sys_imageblit implemented by fbdev core |
| 1081 | */ |
| 1082 | void drm_fb_helper_sys_imageblit(struct fb_info *info, |
| 1083 | const struct fb_image *image) |
| 1084 | { |
| 1085 | sys_imageblit(info, image); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1086 | drm_fb_helper_dirty(info, image->dx, image->dy, |
| 1087 | image->width, image->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1088 | } |
| 1089 | EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); |
| 1090 | |
| 1091 | /** |
| 1092 | * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect |
| 1093 | * @info: fbdev registered by the helper |
| 1094 | * @rect: info about rectangle to fill |
| 1095 | * |
| 1096 | * A wrapper around cfb_imageblit implemented by fbdev core |
| 1097 | */ |
| 1098 | void drm_fb_helper_cfb_fillrect(struct fb_info *info, |
| 1099 | const struct fb_fillrect *rect) |
| 1100 | { |
| 1101 | cfb_fillrect(info, rect); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1102 | drm_fb_helper_dirty(info, rect->dx, rect->dy, |
| 1103 | rect->width, rect->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1104 | } |
| 1105 | EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); |
| 1106 | |
| 1107 | /** |
| 1108 | * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea |
| 1109 | * @info: fbdev registered by the helper |
| 1110 | * @area: info about area to copy |
| 1111 | * |
| 1112 | * A wrapper around cfb_copyarea implemented by fbdev core |
| 1113 | */ |
| 1114 | void drm_fb_helper_cfb_copyarea(struct fb_info *info, |
| 1115 | const struct fb_copyarea *area) |
| 1116 | { |
| 1117 | cfb_copyarea(info, area); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1118 | drm_fb_helper_dirty(info, area->dx, area->dy, |
| 1119 | area->width, area->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1120 | } |
| 1121 | EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); |
| 1122 | |
| 1123 | /** |
| 1124 | * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit |
| 1125 | * @info: fbdev registered by the helper |
| 1126 | * @image: info about image to blit |
| 1127 | * |
| 1128 | * A wrapper around cfb_imageblit implemented by fbdev core |
| 1129 | */ |
| 1130 | void drm_fb_helper_cfb_imageblit(struct fb_info *info, |
| 1131 | const struct fb_image *image) |
| 1132 | { |
| 1133 | cfb_imageblit(info, image); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1134 | drm_fb_helper_dirty(info, image->dx, image->dy, |
| 1135 | image->width, image->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1136 | } |
| 1137 | EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); |
| 1138 | |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1139 | /** |
| 1140 | * drm_fb_helper_set_suspend - wrapper around fb_set_suspend |
| 1141 | * @fb_helper: driver-allocated fbdev helper |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1142 | * @suspend: whether to suspend or resume |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1143 | * |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1144 | * A wrapper around fb_set_suspend implemented by fbdev core. |
| 1145 | * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take |
| 1146 | * the lock yourself |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1147 | */ |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1148 | void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1149 | { |
| 1150 | if (fb_helper && fb_helper->fbdev) |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1151 | fb_set_suspend(fb_helper->fbdev, suspend); |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1152 | } |
| 1153 | EXPORT_SYMBOL(drm_fb_helper_set_suspend); |
| 1154 | |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1155 | /** |
| 1156 | * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also |
| 1157 | * takes the console lock |
| 1158 | * @fb_helper: driver-allocated fbdev helper |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1159 | * @suspend: whether to suspend or resume |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1160 | * |
| 1161 | * A wrapper around fb_set_suspend() that takes the console lock. If the lock |
| 1162 | * isn't available on resume, a worker is tasked with waiting for the lock |
| 1163 | * to become available. The console lock can be pretty contented on resume |
| 1164 | * due to all the printk activity. |
| 1165 | * |
| 1166 | * This function can be called multiple times with the same state since |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1167 | * &fb_info.state is checked to see if fbdev is running or not before locking. |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1168 | * |
| 1169 | * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. |
| 1170 | */ |
| 1171 | void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1172 | bool suspend) |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1173 | { |
| 1174 | if (!fb_helper || !fb_helper->fbdev) |
| 1175 | return; |
| 1176 | |
| 1177 | /* make sure there's no pending/ongoing resume */ |
| 1178 | flush_work(&fb_helper->resume_work); |
| 1179 | |
| 1180 | if (suspend) { |
| 1181 | if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) |
| 1182 | return; |
| 1183 | |
| 1184 | console_lock(); |
| 1185 | |
| 1186 | } else { |
| 1187 | if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) |
| 1188 | return; |
| 1189 | |
| 1190 | if (!console_trylock()) { |
| 1191 | schedule_work(&fb_helper->resume_work); |
| 1192 | return; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | fb_set_suspend(fb_helper->fbdev, suspend); |
| 1197 | console_unlock(); |
| 1198 | } |
| 1199 | EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); |
| 1200 | |
Peter Rosin | b8e2b01 | 2017-07-04 12:36:57 +0200 | [diff] [blame] | 1201 | static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info) |
| 1202 | { |
| 1203 | u32 *palette = (u32 *)info->pseudo_palette; |
| 1204 | int i; |
| 1205 | |
| 1206 | if (cmap->start + cmap->len > 16) |
| 1207 | return -EINVAL; |
| 1208 | |
| 1209 | for (i = 0; i < cmap->len; ++i) { |
| 1210 | u16 red = cmap->red[i]; |
| 1211 | u16 green = cmap->green[i]; |
| 1212 | u16 blue = cmap->blue[i]; |
| 1213 | u32 value; |
| 1214 | |
| 1215 | red >>= 16 - info->var.red.length; |
| 1216 | green >>= 16 - info->var.green.length; |
| 1217 | blue >>= 16 - info->var.blue.length; |
| 1218 | value = (red << info->var.red.offset) | |
| 1219 | (green << info->var.green.offset) | |
| 1220 | (blue << info->var.blue.offset); |
| 1221 | if (info->var.transp.length > 0) { |
| 1222 | u32 mask = (1 << info->var.transp.length) - 1; |
| 1223 | |
| 1224 | mask <<= info->var.transp.offset; |
| 1225 | value |= mask; |
| 1226 | } |
| 1227 | palette[cmap->start + i] = value; |
| 1228 | } |
| 1229 | |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1233 | static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info) |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1234 | { |
| 1235 | struct drm_fb_helper *fb_helper = info->par; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1236 | struct drm_crtc *crtc; |
Peter Rosin | a3562a0 | 2017-07-04 12:36:58 +0200 | [diff] [blame] | 1237 | u16 *r, *g, *b; |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1238 | int i, ret = 0; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1239 | |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1240 | drm_modeset_lock_all(fb_helper->dev); |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1241 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1242 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1243 | if (!crtc->funcs->gamma_set || !crtc->gamma_size) |
| 1244 | return -EINVAL; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1245 | |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1246 | if (cmap->start + cmap->len > crtc->gamma_size) |
| 1247 | return -EINVAL; |
Peter Rosin | a3562a0 | 2017-07-04 12:36:58 +0200 | [diff] [blame] | 1248 | |
| 1249 | r = crtc->gamma_store; |
| 1250 | g = r + crtc->gamma_size; |
| 1251 | b = g + crtc->gamma_size; |
| 1252 | |
| 1253 | memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); |
| 1254 | memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); |
| 1255 | memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); |
| 1256 | |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1257 | ret = crtc->funcs->gamma_set(crtc, r, g, b, |
| 1258 | crtc->gamma_size, NULL); |
| 1259 | if (ret) |
| 1260 | return ret; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1261 | } |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1262 | drm_modeset_unlock_all(fb_helper->dev); |
| 1263 | |
| 1264 | return ret; |
| 1265 | } |
| 1266 | |
| 1267 | static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc, |
| 1268 | struct fb_cmap *cmap) |
| 1269 | { |
| 1270 | struct drm_device *dev = crtc->dev; |
| 1271 | struct drm_property_blob *gamma_lut; |
| 1272 | struct drm_color_lut *lut; |
| 1273 | int size = crtc->gamma_size; |
| 1274 | int i; |
| 1275 | |
| 1276 | if (!size || cmap->start + cmap->len > size) |
| 1277 | return ERR_PTR(-EINVAL); |
| 1278 | |
| 1279 | gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL); |
| 1280 | if (IS_ERR(gamma_lut)) |
| 1281 | return gamma_lut; |
| 1282 | |
| 1283 | lut = (struct drm_color_lut *)gamma_lut->data; |
| 1284 | if (cmap->start || cmap->len != size) { |
| 1285 | u16 *r = crtc->gamma_store; |
| 1286 | u16 *g = r + crtc->gamma_size; |
| 1287 | u16 *b = g + crtc->gamma_size; |
| 1288 | |
| 1289 | for (i = 0; i < cmap->start; i++) { |
| 1290 | lut[i].red = r[i]; |
| 1291 | lut[i].green = g[i]; |
| 1292 | lut[i].blue = b[i]; |
| 1293 | } |
| 1294 | for (i = cmap->start + cmap->len; i < size; i++) { |
| 1295 | lut[i].red = r[i]; |
| 1296 | lut[i].green = g[i]; |
| 1297 | lut[i].blue = b[i]; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | for (i = 0; i < cmap->len; i++) { |
| 1302 | lut[cmap->start + i].red = cmap->red[i]; |
| 1303 | lut[cmap->start + i].green = cmap->green[i]; |
| 1304 | lut[cmap->start + i].blue = cmap->blue[i]; |
| 1305 | } |
| 1306 | |
| 1307 | return gamma_lut; |
| 1308 | } |
| 1309 | |
| 1310 | static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info) |
| 1311 | { |
| 1312 | struct drm_fb_helper *fb_helper = info->par; |
| 1313 | struct drm_device *dev = fb_helper->dev; |
| 1314 | struct drm_property_blob *gamma_lut = NULL; |
| 1315 | struct drm_modeset_acquire_ctx ctx; |
| 1316 | struct drm_crtc_state *crtc_state; |
| 1317 | struct drm_atomic_state *state; |
| 1318 | struct drm_crtc *crtc; |
| 1319 | u16 *r, *g, *b; |
| 1320 | int i, ret = 0; |
| 1321 | bool replaced; |
| 1322 | |
| 1323 | drm_modeset_acquire_init(&ctx, 0); |
| 1324 | |
| 1325 | state = drm_atomic_state_alloc(dev); |
| 1326 | if (!state) { |
| 1327 | ret = -ENOMEM; |
| 1328 | goto out_ctx; |
| 1329 | } |
| 1330 | |
| 1331 | state->acquire_ctx = &ctx; |
| 1332 | retry: |
| 1333 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1334 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
| 1335 | |
| 1336 | if (!gamma_lut) |
| 1337 | gamma_lut = setcmap_new_gamma_lut(crtc, cmap); |
| 1338 | if (IS_ERR(gamma_lut)) { |
| 1339 | ret = PTR_ERR(gamma_lut); |
| 1340 | gamma_lut = NULL; |
| 1341 | goto out_state; |
| 1342 | } |
| 1343 | |
| 1344 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 1345 | if (IS_ERR(crtc_state)) { |
| 1346 | ret = PTR_ERR(crtc_state); |
| 1347 | goto out_state; |
| 1348 | } |
| 1349 | |
| 1350 | replaced = drm_property_replace_blob(&crtc_state->degamma_lut, |
| 1351 | NULL); |
| 1352 | replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL); |
| 1353 | replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, |
| 1354 | gamma_lut); |
| 1355 | crtc_state->color_mgmt_changed |= replaced; |
| 1356 | } |
| 1357 | |
| 1358 | ret = drm_atomic_commit(state); |
| 1359 | if (ret) |
| 1360 | goto out_state; |
| 1361 | |
| 1362 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1363 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
| 1364 | |
| 1365 | r = crtc->gamma_store; |
| 1366 | g = r + crtc->gamma_size; |
| 1367 | b = g + crtc->gamma_size; |
| 1368 | |
| 1369 | memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); |
| 1370 | memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); |
| 1371 | memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); |
| 1372 | } |
| 1373 | |
| 1374 | out_state: |
| 1375 | if (ret == -EDEADLK) |
| 1376 | goto backoff; |
| 1377 | |
| 1378 | drm_property_blob_put(gamma_lut); |
| 1379 | drm_atomic_state_put(state); |
| 1380 | out_ctx: |
| 1381 | drm_modeset_drop_locks(&ctx); |
| 1382 | drm_modeset_acquire_fini(&ctx); |
| 1383 | |
| 1384 | return ret; |
| 1385 | |
| 1386 | backoff: |
| 1387 | drm_atomic_state_clear(state); |
| 1388 | drm_modeset_backoff(&ctx); |
| 1389 | goto retry; |
| 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap |
| 1394 | * @cmap: cmap to set |
| 1395 | * @info: fbdev registered by the helper |
| 1396 | */ |
| 1397 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) |
| 1398 | { |
| 1399 | struct drm_fb_helper *fb_helper = info->par; |
| 1400 | int ret; |
| 1401 | |
| 1402 | if (oops_in_progress) |
| 1403 | return -EBUSY; |
| 1404 | |
| 1405 | mutex_lock(&fb_helper->lock); |
| 1406 | |
| 1407 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 1408 | ret = -EBUSY; |
| 1409 | goto out; |
| 1410 | } |
| 1411 | |
| 1412 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) |
| 1413 | ret = setcmap_pseudo_palette(cmap, info); |
| 1414 | else if (drm_drv_uses_atomic_modeset(fb_helper->dev)) |
| 1415 | ret = setcmap_atomic(cmap, info); |
| 1416 | else |
| 1417 | ret = setcmap_legacy(cmap, info); |
| 1418 | |
| 1419 | out: |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1420 | mutex_unlock(&fb_helper->lock); |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1421 | |
| 1422 | return ret; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1423 | } |
| 1424 | EXPORT_SYMBOL(drm_fb_helper_setcmap); |
| 1425 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1426 | /** |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1427 | * drm_fb_helper_ioctl - legacy ioctl implementation |
| 1428 | * @info: fbdev registered by the helper |
| 1429 | * @cmd: ioctl command |
| 1430 | * @arg: ioctl argument |
| 1431 | * |
| 1432 | * A helper to implement the standard fbdev ioctl. Only |
| 1433 | * FBIO_WAITFORVSYNC is implemented for now. |
| 1434 | */ |
| 1435 | int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, |
| 1436 | unsigned long arg) |
| 1437 | { |
| 1438 | struct drm_fb_helper *fb_helper = info->par; |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1439 | struct drm_mode_set *mode_set; |
| 1440 | struct drm_crtc *crtc; |
| 1441 | int ret = 0; |
| 1442 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1443 | mutex_lock(&fb_helper->lock); |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1444 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 1445 | ret = -EBUSY; |
| 1446 | goto unlock; |
| 1447 | } |
| 1448 | |
| 1449 | switch (cmd) { |
| 1450 | case FBIO_WAITFORVSYNC: |
| 1451 | /* |
| 1452 | * Only consider the first CRTC. |
| 1453 | * |
| 1454 | * This ioctl is supposed to take the CRTC number as |
| 1455 | * an argument, but in fbdev times, what that number |
| 1456 | * was supposed to be was quite unclear, different |
| 1457 | * drivers were passing that argument differently |
| 1458 | * (some by reference, some by value), and most of the |
| 1459 | * userspace applications were just hardcoding 0 as an |
| 1460 | * argument. |
| 1461 | * |
| 1462 | * The first CRTC should be the integrated panel on |
| 1463 | * most drivers, so this is the best choice we can |
| 1464 | * make. If we're not smart enough here, one should |
| 1465 | * just consider switch the userspace to KMS. |
| 1466 | */ |
| 1467 | mode_set = &fb_helper->crtc_info[0].mode_set; |
| 1468 | crtc = mode_set->crtc; |
| 1469 | |
| 1470 | /* |
| 1471 | * Only wait for a vblank event if the CRTC is |
| 1472 | * enabled, otherwise just don't do anythintg, |
| 1473 | * not even report an error. |
| 1474 | */ |
| 1475 | ret = drm_crtc_vblank_get(crtc); |
| 1476 | if (!ret) { |
| 1477 | drm_crtc_wait_one_vblank(crtc); |
| 1478 | drm_crtc_vblank_put(crtc); |
| 1479 | } |
| 1480 | |
| 1481 | ret = 0; |
| 1482 | goto unlock; |
| 1483 | default: |
| 1484 | ret = -ENOTTY; |
| 1485 | } |
| 1486 | |
| 1487 | unlock: |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1488 | mutex_unlock(&fb_helper->lock); |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1489 | return ret; |
| 1490 | } |
| 1491 | EXPORT_SYMBOL(drm_fb_helper_ioctl); |
| 1492 | |
| 1493 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1494 | * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1495 | * @var: screeninfo to check |
| 1496 | * @info: fbdev registered by the helper |
| 1497 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1498 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
| 1499 | struct fb_info *info) |
| 1500 | { |
| 1501 | struct drm_fb_helper *fb_helper = info->par; |
| 1502 | struct drm_framebuffer *fb = fb_helper->fb; |
| 1503 | int depth; |
| 1504 | |
Jason Wessel | f90ebd9 | 2010-08-05 09:22:32 -0500 | [diff] [blame] | 1505 | if (var->pixclock != 0 || in_dbg_master()) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1506 | return -EINVAL; |
| 1507 | |
Stefan Agner | 865afb1 | 2016-10-11 16:15:04 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * Changes struct fb_var_screeninfo are currently not pushed back |
| 1510 | * to KMS, hence fail if different settings are requested. |
| 1511 | */ |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1512 | if (var->bits_per_pixel != fb->format->cpp[0] * 8 || |
Michel Dänzer | 12ffed9 | 2017-03-23 17:53:26 +0900 | [diff] [blame] | 1513 | var->xres > fb->width || var->yres > fb->height || |
| 1514 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { |
| 1515 | DRM_DEBUG("fb requested width/height/bpp can't fit in current fb " |
Chris Wilson | 62fb376 | 2012-03-26 21:15:53 +0100 | [diff] [blame] | 1516 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
| 1517 | var->xres, var->yres, var->bits_per_pixel, |
| 1518 | var->xres_virtual, var->yres_virtual, |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1519 | fb->width, fb->height, fb->format->cpp[0] * 8); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1520 | return -EINVAL; |
| 1521 | } |
| 1522 | |
| 1523 | switch (var->bits_per_pixel) { |
| 1524 | case 16: |
| 1525 | depth = (var->green.length == 6) ? 16 : 15; |
| 1526 | break; |
| 1527 | case 32: |
| 1528 | depth = (var->transp.length > 0) ? 32 : 24; |
| 1529 | break; |
| 1530 | default: |
| 1531 | depth = var->bits_per_pixel; |
| 1532 | break; |
| 1533 | } |
| 1534 | |
| 1535 | switch (depth) { |
| 1536 | case 8: |
| 1537 | var->red.offset = 0; |
| 1538 | var->green.offset = 0; |
| 1539 | var->blue.offset = 0; |
| 1540 | var->red.length = 8; |
| 1541 | var->green.length = 8; |
| 1542 | var->blue.length = 8; |
| 1543 | var->transp.length = 0; |
| 1544 | var->transp.offset = 0; |
| 1545 | break; |
| 1546 | case 15: |
| 1547 | var->red.offset = 10; |
| 1548 | var->green.offset = 5; |
| 1549 | var->blue.offset = 0; |
| 1550 | var->red.length = 5; |
| 1551 | var->green.length = 5; |
| 1552 | var->blue.length = 5; |
| 1553 | var->transp.length = 1; |
| 1554 | var->transp.offset = 15; |
| 1555 | break; |
| 1556 | case 16: |
| 1557 | var->red.offset = 11; |
| 1558 | var->green.offset = 5; |
| 1559 | var->blue.offset = 0; |
| 1560 | var->red.length = 5; |
| 1561 | var->green.length = 6; |
| 1562 | var->blue.length = 5; |
| 1563 | var->transp.length = 0; |
| 1564 | var->transp.offset = 0; |
| 1565 | break; |
| 1566 | case 24: |
| 1567 | var->red.offset = 16; |
| 1568 | var->green.offset = 8; |
| 1569 | var->blue.offset = 0; |
| 1570 | var->red.length = 8; |
| 1571 | var->green.length = 8; |
| 1572 | var->blue.length = 8; |
| 1573 | var->transp.length = 0; |
| 1574 | var->transp.offset = 0; |
| 1575 | break; |
| 1576 | case 32: |
| 1577 | var->red.offset = 16; |
| 1578 | var->green.offset = 8; |
| 1579 | var->blue.offset = 0; |
| 1580 | var->red.length = 8; |
| 1581 | var->green.length = 8; |
| 1582 | var->blue.length = 8; |
| 1583 | var->transp.length = 8; |
| 1584 | var->transp.offset = 24; |
| 1585 | break; |
| 1586 | default: |
| 1587 | return -EINVAL; |
| 1588 | } |
| 1589 | return 0; |
| 1590 | } |
| 1591 | EXPORT_SYMBOL(drm_fb_helper_check_var); |
| 1592 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1593 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1594 | * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1595 | * @info: fbdev registered by the helper |
| 1596 | * |
| 1597 | * This will let fbcon do the mode init and is called at initialization time by |
| 1598 | * the fbdev core when registering the driver, and later on through the hotplug |
| 1599 | * callback. |
| 1600 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1601 | int drm_fb_helper_set_par(struct fb_info *info) |
| 1602 | { |
| 1603 | struct drm_fb_helper *fb_helper = info->par; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1604 | struct fb_var_screeninfo *var = &info->var; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1605 | |
Daniel Vetter | c50bfd0 | 2015-07-28 13:18:40 +0200 | [diff] [blame] | 1606 | if (oops_in_progress) |
| 1607 | return -EBUSY; |
| 1608 | |
Clemens Ladisch | 5349ef3 | 2009-11-04 09:42:52 +0100 | [diff] [blame] | 1609 | if (var->pixclock != 0) { |
Pavel Roskin | 172e91f | 2010-02-11 14:31:32 +1000 | [diff] [blame] | 1610 | DRM_ERROR("PIXEL CLOCK SET\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1611 | return -EINVAL; |
| 1612 | } |
| 1613 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 1614 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1615 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1616 | return 0; |
| 1617 | } |
| 1618 | EXPORT_SYMBOL(drm_fb_helper_set_par); |
| 1619 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1620 | static void pan_set(struct drm_fb_helper *fb_helper, int x, int y) |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1621 | { |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1622 | int i; |
Daniel Vetter | 5c2e344 | 2017-07-04 17:18:26 +0200 | [diff] [blame] | 1623 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1624 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1625 | struct drm_mode_set *mode_set; |
| 1626 | |
| 1627 | mode_set = &fb_helper->crtc_info[i].mode_set; |
| 1628 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1629 | mode_set->x = x; |
| 1630 | mode_set->y = y; |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1631 | } |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1632 | } |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1633 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1634 | static int pan_display_atomic(struct fb_var_screeninfo *var, |
| 1635 | struct fb_info *info) |
| 1636 | { |
| 1637 | struct drm_fb_helper *fb_helper = info->par; |
| 1638 | int ret; |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1639 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1640 | pan_set(fb_helper, var->xoffset, var->yoffset); |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1641 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1642 | ret = restore_fbdev_mode_atomic(fb_helper, true); |
| 1643 | if (!ret) { |
| 1644 | info->var.xoffset = var->xoffset; |
| 1645 | info->var.yoffset = var->yoffset; |
| 1646 | } else |
| 1647 | pan_set(fb_helper, info->var.xoffset, info->var.yoffset); |
Daniel Vetter | 5c2e344 | 2017-07-04 17:18:26 +0200 | [diff] [blame] | 1648 | |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1649 | return ret; |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1650 | } |
| 1651 | |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1652 | static int pan_display_legacy(struct fb_var_screeninfo *var, |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1653 | struct fb_info *info) |
| 1654 | { |
| 1655 | struct drm_fb_helper *fb_helper = info->par; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1656 | struct drm_mode_set *modeset; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1657 | int ret = 0; |
| 1658 | int i; |
| 1659 | |
Daniel Vetter | 5c2e344 | 2017-07-04 17:18:26 +0200 | [diff] [blame] | 1660 | drm_modeset_lock_all(fb_helper->dev); |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1661 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1662 | modeset = &fb_helper->crtc_info[i].mode_set; |
| 1663 | |
| 1664 | modeset->x = var->xoffset; |
| 1665 | modeset->y = var->yoffset; |
| 1666 | |
| 1667 | if (modeset->num_connectors) { |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 1668 | ret = drm_mode_set_config_internal(modeset); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1669 | if (!ret) { |
| 1670 | info->var.xoffset = var->xoffset; |
| 1671 | info->var.yoffset = var->yoffset; |
| 1672 | } |
| 1673 | } |
| 1674 | } |
Daniel Vetter | 5c2e344 | 2017-07-04 17:18:26 +0200 | [diff] [blame] | 1675 | drm_modeset_unlock_all(fb_helper->dev); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1676 | |
| 1677 | return ret; |
| 1678 | } |
| 1679 | |
| 1680 | /** |
| 1681 | * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display |
| 1682 | * @var: updated screen information |
| 1683 | * @info: fbdev registered by the helper |
| 1684 | */ |
| 1685 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, |
| 1686 | struct fb_info *info) |
| 1687 | { |
| 1688 | struct drm_fb_helper *fb_helper = info->par; |
| 1689 | struct drm_device *dev = fb_helper->dev; |
| 1690 | int ret; |
| 1691 | |
| 1692 | if (oops_in_progress) |
| 1693 | return -EBUSY; |
| 1694 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1695 | mutex_lock(&fb_helper->lock); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1696 | if (!drm_fb_helper_is_bound(fb_helper)) { |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1697 | mutex_unlock(&fb_helper->lock); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1698 | return -EBUSY; |
| 1699 | } |
| 1700 | |
| 1701 | if (drm_drv_uses_atomic_modeset(dev)) |
| 1702 | ret = pan_display_atomic(var, info); |
| 1703 | else |
| 1704 | ret = pan_display_legacy(var, info); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1705 | mutex_unlock(&fb_helper->lock); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1706 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1707 | return ret; |
| 1708 | } |
| 1709 | EXPORT_SYMBOL(drm_fb_helper_pan_display); |
| 1710 | |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1711 | /* |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1712 | * Allocates the backing storage and sets up the fbdev info structure through |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 1713 | * the ->fb_probe callback. |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1714 | */ |
Daniel Vetter | de1ace5 | 2013-01-20 21:50:49 +0100 | [diff] [blame] | 1715 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
| 1716 | int preferred_bpp) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1717 | { |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1718 | int ret = 0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1719 | int crtc_count = 0; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1720 | int i; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1721 | struct drm_fb_helper_surface_size sizes; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1722 | int gamma_size = 0; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1723 | |
| 1724 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); |
| 1725 | sizes.surface_depth = 24; |
| 1726 | sizes.surface_bpp = 32; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1727 | sizes.fb_width = (u32)-1; |
| 1728 | sizes.fb_height = (u32)-1; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1729 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1730 | /* if driver picks 8 or 16 by default use that for both depth/bpp */ |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1731 | if (preferred_bpp != sizes.surface_bpp) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1732 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1733 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1734 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 1735 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1736 | struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; |
Chris Wilson | 1794d25 | 2011-04-17 07:43:32 +0100 | [diff] [blame] | 1737 | struct drm_cmdline_mode *cmdline_mode; |
Dave Airlie | 8ef8678 | 2009-09-26 06:39:00 +1000 | [diff] [blame] | 1738 | |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1739 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1740 | |
| 1741 | if (cmdline_mode->bpp_specified) { |
| 1742 | switch (cmdline_mode->bpp) { |
| 1743 | case 8: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1744 | sizes.surface_depth = sizes.surface_bpp = 8; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1745 | break; |
| 1746 | case 15: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1747 | sizes.surface_depth = 15; |
| 1748 | sizes.surface_bpp = 16; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1749 | break; |
| 1750 | case 16: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1751 | sizes.surface_depth = sizes.surface_bpp = 16; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1752 | break; |
| 1753 | case 24: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1754 | sizes.surface_depth = sizes.surface_bpp = 24; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1755 | break; |
| 1756 | case 32: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1757 | sizes.surface_depth = 24; |
| 1758 | sizes.surface_bpp = 32; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1759 | break; |
| 1760 | } |
| 1761 | break; |
| 1762 | } |
| 1763 | } |
| 1764 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1765 | crtc_count = 0; |
| 1766 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1767 | struct drm_display_mode *desired_mode; |
Rob Clark | 0e3704c | 2015-03-11 10:23:14 -0400 | [diff] [blame] | 1768 | struct drm_mode_set *mode_set; |
| 1769 | int x, y, j; |
| 1770 | /* in case of tile group, are we the last tile vert or horiz? |
| 1771 | * If no tile group you are always the last one both vertically |
| 1772 | * and horizontally |
| 1773 | */ |
| 1774 | bool lastv = true, lasth = true; |
Rob Clark | 675c832 | 2015-03-11 10:23:13 -0400 | [diff] [blame] | 1775 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1776 | desired_mode = fb_helper->crtc_info[i].desired_mode; |
Rob Clark | 0e3704c | 2015-03-11 10:23:14 -0400 | [diff] [blame] | 1777 | mode_set = &fb_helper->crtc_info[i].mode_set; |
Rob Clark | 675c832 | 2015-03-11 10:23:13 -0400 | [diff] [blame] | 1778 | |
| 1779 | if (!desired_mode) |
| 1780 | continue; |
| 1781 | |
| 1782 | crtc_count++; |
| 1783 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1784 | x = fb_helper->crtc_info[i].x; |
| 1785 | y = fb_helper->crtc_info[i].y; |
Rob Clark | 675c832 | 2015-03-11 10:23:13 -0400 | [diff] [blame] | 1786 | |
| 1787 | if (gamma_size == 0) |
| 1788 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; |
| 1789 | |
| 1790 | sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); |
| 1791 | sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); |
Rob Clark | 0e3704c | 2015-03-11 10:23:14 -0400 | [diff] [blame] | 1792 | |
| 1793 | for (j = 0; j < mode_set->num_connectors; j++) { |
| 1794 | struct drm_connector *connector = mode_set->connectors[j]; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1795 | |
Rob Clark | 0e3704c | 2015-03-11 10:23:14 -0400 | [diff] [blame] | 1796 | if (connector->has_tile) { |
| 1797 | lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); |
| 1798 | lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); |
| 1799 | /* cloning to multiple tiles is just crazy-talk, so: */ |
| 1800 | break; |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | if (lasth) |
| 1805 | sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); |
| 1806 | if (lastv) |
| 1807 | sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1808 | } |
| 1809 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1810 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 1811 | DRM_INFO("Cannot find any crtc or sizes\n"); |
| 1812 | return -EAGAIN; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1813 | } |
| 1814 | |
Xinliang Liu | 5f15257 | 2017-02-15 17:19:08 +0100 | [diff] [blame] | 1815 | /* Handle our overallocation */ |
| 1816 | sizes.surface_height *= drm_fbdev_overalloc; |
| 1817 | sizes.surface_height /= 100; |
| 1818 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1819 | /* push down into drivers */ |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1820 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
| 1821 | if (ret < 0) |
| 1822 | return ret; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1823 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1824 | return 0; |
| 1825 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1826 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1827 | /** |
| 1828 | * drm_fb_helper_fill_fix - initializes fixed fbdev information |
| 1829 | * @info: fbdev registered by the helper |
| 1830 | * @pitch: desired pitch |
| 1831 | * @depth: desired depth |
| 1832 | * |
| 1833 | * Helper to fill in the fixed fbdev information useful for a non-accelerated |
| 1834 | * fbdev emulations. Drivers which support acceleration methods which impose |
| 1835 | * additional constraints need to set up their own limits. |
| 1836 | * |
| 1837 | * Drivers should call this (or their equivalent setup code) from their |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1838 | * &drm_fb_helper_funcs.fb_probe callback. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1839 | */ |
Dave Airlie | 3632ef8 | 2011-01-15 09:27:00 +1000 | [diff] [blame] | 1840 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
| 1841 | uint32_t depth) |
| 1842 | { |
| 1843 | info->fix.type = FB_TYPE_PACKED_PIXELS; |
| 1844 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : |
| 1845 | FB_VISUAL_TRUECOLOR; |
| 1846 | info->fix.mmio_start = 0; |
| 1847 | info->fix.mmio_len = 0; |
| 1848 | info->fix.type_aux = 0; |
| 1849 | info->fix.xpanstep = 1; /* doing it in hw */ |
| 1850 | info->fix.ypanstep = 1; /* doing it in hw */ |
| 1851 | info->fix.ywrapstep = 0; |
| 1852 | info->fix.accel = FB_ACCEL_NONE; |
Dave Airlie | 3632ef8 | 2011-01-15 09:27:00 +1000 | [diff] [blame] | 1853 | |
| 1854 | info->fix.line_length = pitch; |
Dave Airlie | 3632ef8 | 2011-01-15 09:27:00 +1000 | [diff] [blame] | 1855 | } |
| 1856 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); |
| 1857 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1858 | /** |
| 1859 | * drm_fb_helper_fill_var - initalizes variable fbdev information |
| 1860 | * @info: fbdev instance to set up |
| 1861 | * @fb_helper: fb helper instance to use as template |
| 1862 | * @fb_width: desired fb width |
| 1863 | * @fb_height: desired fb height |
| 1864 | * |
| 1865 | * Sets up the variable fbdev metainformation from the given fb helper instance |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1866 | * and the drm framebuffer allocated in &drm_fb_helper.fb. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1867 | * |
| 1868 | * Drivers should call this (or their equivalent setup code) from their |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1869 | * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev |
| 1870 | * backing storage framebuffer. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1871 | */ |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1872 | void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1873 | uint32_t fb_width, uint32_t fb_height) |
| 1874 | { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1875 | struct drm_framebuffer *fb = fb_helper->fb; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1876 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1877 | info->pseudo_palette = fb_helper->pseudo_palette; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1878 | info->var.xres_virtual = fb->width; |
| 1879 | info->var.yres_virtual = fb->height; |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1880 | info->var.bits_per_pixel = fb->format->cpp[0] * 8; |
James Simmons | 57084d0 | 2010-12-20 19:10:39 +0000 | [diff] [blame] | 1881 | info->var.accel_flags = FB_ACCELF_TEXT; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1882 | info->var.xoffset = 0; |
| 1883 | info->var.yoffset = 0; |
| 1884 | info->var.activate = FB_ACTIVATE_NOW; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1885 | |
Ville Syrjälä | b00c600 | 2016-12-14 23:31:35 +0200 | [diff] [blame] | 1886 | switch (fb->format->depth) { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1887 | case 8: |
| 1888 | info->var.red.offset = 0; |
| 1889 | info->var.green.offset = 0; |
| 1890 | info->var.blue.offset = 0; |
| 1891 | info->var.red.length = 8; /* 8bit DAC */ |
| 1892 | info->var.green.length = 8; |
| 1893 | info->var.blue.length = 8; |
| 1894 | info->var.transp.offset = 0; |
| 1895 | info->var.transp.length = 0; |
| 1896 | break; |
| 1897 | case 15: |
| 1898 | info->var.red.offset = 10; |
| 1899 | info->var.green.offset = 5; |
| 1900 | info->var.blue.offset = 0; |
| 1901 | info->var.red.length = 5; |
| 1902 | info->var.green.length = 5; |
| 1903 | info->var.blue.length = 5; |
| 1904 | info->var.transp.offset = 15; |
| 1905 | info->var.transp.length = 1; |
| 1906 | break; |
| 1907 | case 16: |
| 1908 | info->var.red.offset = 11; |
| 1909 | info->var.green.offset = 5; |
| 1910 | info->var.blue.offset = 0; |
| 1911 | info->var.red.length = 5; |
| 1912 | info->var.green.length = 6; |
| 1913 | info->var.blue.length = 5; |
| 1914 | info->var.transp.offset = 0; |
| 1915 | break; |
| 1916 | case 24: |
| 1917 | info->var.red.offset = 16; |
| 1918 | info->var.green.offset = 8; |
| 1919 | info->var.blue.offset = 0; |
| 1920 | info->var.red.length = 8; |
| 1921 | info->var.green.length = 8; |
| 1922 | info->var.blue.length = 8; |
| 1923 | info->var.transp.offset = 0; |
| 1924 | info->var.transp.length = 0; |
| 1925 | break; |
| 1926 | case 32: |
| 1927 | info->var.red.offset = 16; |
| 1928 | info->var.green.offset = 8; |
| 1929 | info->var.blue.offset = 0; |
| 1930 | info->var.red.length = 8; |
| 1931 | info->var.green.length = 8; |
| 1932 | info->var.blue.length = 8; |
| 1933 | info->var.transp.offset = 24; |
| 1934 | info->var.transp.length = 8; |
| 1935 | break; |
| 1936 | default: |
| 1937 | break; |
| 1938 | } |
| 1939 | |
| 1940 | info->var.xres = fb_width; |
| 1941 | info->var.yres = fb_height; |
| 1942 | } |
| 1943 | EXPORT_SYMBOL(drm_fb_helper_fill_var); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1944 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1945 | static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 1946 | uint32_t maxX, |
| 1947 | uint32_t maxY) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1948 | { |
| 1949 | struct drm_connector *connector; |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 1950 | int i, count = 0; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1951 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 1952 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1953 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1954 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
| 1955 | } |
| 1956 | |
| 1957 | return count; |
| 1958 | } |
| 1959 | |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 1960 | struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1961 | { |
| 1962 | struct drm_display_mode *mode; |
| 1963 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1964 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
Daniel Vetter | 9d3de13 | 2014-01-23 16:27:56 +0100 | [diff] [blame] | 1965 | if (mode->hdisplay > width || |
| 1966 | mode->vdisplay > height) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1967 | continue; |
| 1968 | if (mode->type & DRM_MODE_TYPE_PREFERRED) |
| 1969 | return mode; |
| 1970 | } |
| 1971 | return NULL; |
| 1972 | } |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 1973 | EXPORT_SYMBOL(drm_has_preferred_mode); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1974 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1975 | static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1976 | { |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1977 | return fb_connector->connector->cmdline_mode.specified; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Vincent Abriou | a09759e | 2017-01-06 17:44:43 +0100 | [diff] [blame] | 1980 | struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1981 | { |
Chris Wilson | 1794d25 | 2011-04-17 07:43:32 +0100 | [diff] [blame] | 1982 | struct drm_cmdline_mode *cmdline_mode; |
Daniel Stone | f3af5c7 | 2015-03-19 04:33:01 +0000 | [diff] [blame] | 1983 | struct drm_display_mode *mode; |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 1984 | bool prefer_non_interlace; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1985 | |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1986 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1987 | if (cmdline_mode->specified == false) |
Daniel Stone | f3af5c7 | 2015-03-19 04:33:01 +0000 | [diff] [blame] | 1988 | return NULL; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1989 | |
| 1990 | /* attempt to find a matching mode in the list of modes |
| 1991 | * we have gotten so far, if not add a CVT mode that conforms |
| 1992 | */ |
| 1993 | if (cmdline_mode->rb || cmdline_mode->margins) |
| 1994 | goto create_mode; |
| 1995 | |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 1996 | prefer_non_interlace = !cmdline_mode->interlace; |
Daniel Stone | f3af5c7 | 2015-03-19 04:33:01 +0000 | [diff] [blame] | 1997 | again: |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1998 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1999 | /* check width/height */ |
| 2000 | if (mode->hdisplay != cmdline_mode->xres || |
| 2001 | mode->vdisplay != cmdline_mode->yres) |
| 2002 | continue; |
| 2003 | |
| 2004 | if (cmdline_mode->refresh_specified) { |
| 2005 | if (mode->vrefresh != cmdline_mode->refresh) |
| 2006 | continue; |
| 2007 | } |
| 2008 | |
| 2009 | if (cmdline_mode->interlace) { |
| 2010 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) |
| 2011 | continue; |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 2012 | } else if (prefer_non_interlace) { |
| 2013 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) |
| 2014 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2015 | } |
| 2016 | return mode; |
| 2017 | } |
| 2018 | |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 2019 | if (prefer_non_interlace) { |
| 2020 | prefer_non_interlace = false; |
| 2021 | goto again; |
| 2022 | } |
| 2023 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2024 | create_mode: |
Chris Wilson | 1794d25 | 2011-04-17 07:43:32 +0100 | [diff] [blame] | 2025 | mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, |
| 2026 | cmdline_mode); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2027 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2028 | return mode; |
| 2029 | } |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 2030 | EXPORT_SYMBOL(drm_pick_cmdline_mode); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2031 | |
| 2032 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) |
| 2033 | { |
| 2034 | bool enable; |
| 2035 | |
Dave Airlie | b5f0538 | 2017-10-16 05:08:39 +0100 | [diff] [blame^] | 2036 | if (connector->display_info.non_desktop) |
| 2037 | return false; |
| 2038 | |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 2039 | if (strict) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2040 | enable = connector->status == connector_status_connected; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 2041 | else |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2042 | enable = connector->status != connector_status_disconnected; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 2043 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2044 | return enable; |
| 2045 | } |
| 2046 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2047 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
| 2048 | bool *enabled) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2049 | { |
| 2050 | bool any_enabled = false; |
| 2051 | struct drm_connector *connector; |
| 2052 | int i = 0; |
| 2053 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2054 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2055 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2056 | enabled[i] = drm_connector_enabled(connector, true); |
| 2057 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, |
Dave Airlie | b5f0538 | 2017-10-16 05:08:39 +0100 | [diff] [blame^] | 2058 | connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no"); |
| 2059 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2060 | any_enabled |= enabled[i]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | if (any_enabled) |
| 2064 | return; |
| 2065 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2066 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2067 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2068 | enabled[i] = drm_connector_enabled(connector, false); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2072 | static bool drm_target_cloned(struct drm_fb_helper *fb_helper, |
| 2073 | struct drm_display_mode **modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2074 | struct drm_fb_offset *offsets, |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2075 | bool *enabled, int width, int height) |
| 2076 | { |
| 2077 | int count, i, j; |
| 2078 | bool can_clone = false; |
| 2079 | struct drm_fb_helper_connector *fb_helper_conn; |
| 2080 | struct drm_display_mode *dmt_mode, *mode; |
| 2081 | |
| 2082 | /* only contemplate cloning in the single crtc case */ |
| 2083 | if (fb_helper->crtc_count > 1) |
| 2084 | return false; |
| 2085 | |
| 2086 | count = 0; |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2087 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2088 | if (enabled[i]) |
| 2089 | count++; |
| 2090 | } |
| 2091 | |
| 2092 | /* only contemplate cloning if more than one connector is enabled */ |
| 2093 | if (count <= 1) |
| 2094 | return false; |
| 2095 | |
| 2096 | /* check the command line or if nothing common pick 1024x768 */ |
| 2097 | can_clone = true; |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2098 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2099 | if (!enabled[i]) |
| 2100 | continue; |
| 2101 | fb_helper_conn = fb_helper->connector_info[i]; |
Vincent Abriou | a09759e | 2017-01-06 17:44:43 +0100 | [diff] [blame] | 2102 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn); |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2103 | if (!modes[i]) { |
| 2104 | can_clone = false; |
| 2105 | break; |
| 2106 | } |
| 2107 | for (j = 0; j < i; j++) { |
| 2108 | if (!enabled[j]) |
| 2109 | continue; |
| 2110 | if (!drm_mode_equal(modes[j], modes[i])) |
| 2111 | can_clone = false; |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | if (can_clone) { |
| 2116 | DRM_DEBUG_KMS("can clone using command line\n"); |
| 2117 | return true; |
| 2118 | } |
| 2119 | |
| 2120 | /* try and find a 1024x768 mode on each connector */ |
| 2121 | can_clone = true; |
Adam Jackson | f6e252b | 2012-04-13 16:33:31 -0400 | [diff] [blame] | 2122 | dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2123 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2124 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2125 | if (!enabled[i]) |
| 2126 | continue; |
| 2127 | |
| 2128 | fb_helper_conn = fb_helper->connector_info[i]; |
| 2129 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
| 2130 | if (drm_mode_equal(mode, dmt_mode)) |
| 2131 | modes[i] = mode; |
| 2132 | } |
| 2133 | if (!modes[i]) |
| 2134 | can_clone = false; |
| 2135 | } |
| 2136 | |
| 2137 | if (can_clone) { |
| 2138 | DRM_DEBUG_KMS("can clone using 1024x768\n"); |
| 2139 | return true; |
| 2140 | } |
| 2141 | DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); |
| 2142 | return false; |
| 2143 | } |
| 2144 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2145 | static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, |
| 2146 | struct drm_display_mode **modes, |
| 2147 | struct drm_fb_offset *offsets, |
| 2148 | int idx, |
| 2149 | int h_idx, int v_idx) |
| 2150 | { |
| 2151 | struct drm_fb_helper_connector *fb_helper_conn; |
| 2152 | int i; |
| 2153 | int hoffset = 0, voffset = 0; |
| 2154 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2155 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2156 | fb_helper_conn = fb_helper->connector_info[i]; |
| 2157 | if (!fb_helper_conn->connector->has_tile) |
| 2158 | continue; |
| 2159 | |
| 2160 | if (!modes[i] && (h_idx || v_idx)) { |
| 2161 | DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, |
| 2162 | fb_helper_conn->connector->base.id); |
| 2163 | continue; |
| 2164 | } |
| 2165 | if (fb_helper_conn->connector->tile_h_loc < h_idx) |
| 2166 | hoffset += modes[i]->hdisplay; |
| 2167 | |
| 2168 | if (fb_helper_conn->connector->tile_v_loc < v_idx) |
| 2169 | voffset += modes[i]->vdisplay; |
| 2170 | } |
| 2171 | offsets[idx].x = hoffset; |
| 2172 | offsets[idx].y = voffset; |
| 2173 | DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); |
| 2174 | return 0; |
| 2175 | } |
| 2176 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2177 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2178 | struct drm_display_mode **modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2179 | struct drm_fb_offset *offsets, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2180 | bool *enabled, int width, int height) |
| 2181 | { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2182 | struct drm_fb_helper_connector *fb_helper_conn; |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2183 | const u64 mask = BIT_ULL(fb_helper->connector_count) - 1; |
| 2184 | u64 conn_configured = 0; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2185 | int tile_pass = 0; |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2186 | int i; |
| 2187 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2188 | retry: |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2189 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2190 | fb_helper_conn = fb_helper->connector_info[i]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2191 | |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2192 | if (conn_configured & BIT_ULL(i)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2193 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2194 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2195 | if (enabled[i] == false) { |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2196 | conn_configured |= BIT_ULL(i); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2197 | continue; |
| 2198 | } |
| 2199 | |
| 2200 | /* first pass over all the untiled connectors */ |
| 2201 | if (tile_pass == 0 && fb_helper_conn->connector->has_tile) |
| 2202 | continue; |
| 2203 | |
| 2204 | if (tile_pass == 1) { |
| 2205 | if (fb_helper_conn->connector->tile_h_loc != 0 || |
| 2206 | fb_helper_conn->connector->tile_v_loc != 0) |
| 2207 | continue; |
| 2208 | |
| 2209 | } else { |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 2210 | if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 && |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2211 | fb_helper_conn->connector->tile_v_loc != tile_pass - 1) |
| 2212 | /* if this tile_pass doesn't cover any of the tiles - keep going */ |
| 2213 | continue; |
| 2214 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 2215 | /* |
| 2216 | * find the tile offsets for this pass - need to find |
| 2217 | * all tiles left and above |
| 2218 | */ |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2219 | drm_get_tile_offsets(fb_helper, modes, offsets, |
| 2220 | i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); |
| 2221 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2222 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2223 | fb_helper_conn->connector->base.id); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2224 | |
| 2225 | /* got for command line mode first */ |
Vincent Abriou | a09759e | 2017-01-06 17:44:43 +0100 | [diff] [blame] | 2226 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2227 | if (!modes[i]) { |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2228 | DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", |
| 2229 | fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2230 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2231 | } |
| 2232 | /* No preferred modes, pick one off the list */ |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2233 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
| 2234 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2235 | break; |
| 2236 | } |
| 2237 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : |
| 2238 | "none"); |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2239 | conn_configured |= BIT_ULL(i); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | if ((conn_configured & mask) != mask) { |
| 2243 | tile_pass++; |
| 2244 | goto retry; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2245 | } |
| 2246 | return true; |
| 2247 | } |
| 2248 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2249 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
| 2250 | struct drm_fb_helper_crtc **best_crtcs, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2251 | struct drm_display_mode **modes, |
| 2252 | int n, int width, int height) |
| 2253 | { |
| 2254 | int c, o; |
| 2255 | struct drm_connector *connector; |
Jani Nikula | be26a66 | 2015-03-11 11:51:06 +0200 | [diff] [blame] | 2256 | const struct drm_connector_helper_funcs *connector_funcs; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2257 | struct drm_encoder *encoder; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2258 | int my_score, best_score, score; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2259 | struct drm_fb_helper_crtc **crtcs, *crtc; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2260 | struct drm_fb_helper_connector *fb_helper_conn; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2261 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2262 | if (n == fb_helper->connector_count) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2263 | return 0; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2264 | |
| 2265 | fb_helper_conn = fb_helper->connector_info[n]; |
| 2266 | connector = fb_helper_conn->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2267 | |
| 2268 | best_crtcs[n] = NULL; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2269 | best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2270 | if (modes[n] == NULL) |
| 2271 | return best_score; |
| 2272 | |
Harsha Sharma | 4b947b1c | 2017-10-13 13:07:47 +0530 | [diff] [blame] | 2273 | crtcs = kcalloc(fb_helper->connector_count, |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2274 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2275 | if (!crtcs) |
| 2276 | return best_score; |
| 2277 | |
| 2278 | my_score = 1; |
| 2279 | if (connector->status == connector_status_connected) |
| 2280 | my_score++; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2281 | if (drm_has_cmdline_mode(fb_helper_conn)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2282 | my_score++; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2283 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2284 | my_score++; |
| 2285 | |
| 2286 | connector_funcs = connector->helper_private; |
Boris Brezillon | c61b93f | 2016-06-07 13:47:56 +0200 | [diff] [blame] | 2287 | |
| 2288 | /* |
| 2289 | * If the DRM device implements atomic hooks and ->best_encoder() is |
| 2290 | * NULL we fallback to the default drm_atomic_helper_best_encoder() |
| 2291 | * helper. |
| 2292 | */ |
Dhinakaran Pandiyan | a743d75 | 2016-12-22 00:50:42 -0800 | [diff] [blame] | 2293 | if (drm_drv_uses_atomic_modeset(fb_helper->dev) && |
Boris Brezillon | c61b93f | 2016-06-07 13:47:56 +0200 | [diff] [blame] | 2294 | !connector_funcs->best_encoder) |
| 2295 | encoder = drm_atomic_helper_best_encoder(connector); |
| 2296 | else |
| 2297 | encoder = connector_funcs->best_encoder(connector); |
| 2298 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2299 | if (!encoder) |
| 2300 | goto out; |
| 2301 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 2302 | /* |
| 2303 | * select a crtc for this connector and then attempt to configure |
| 2304 | * remaining connectors |
| 2305 | */ |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2306 | for (c = 0; c < fb_helper->crtc_count; c++) { |
| 2307 | crtc = &fb_helper->crtc_info[c]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2308 | |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 2309 | if ((encoder->possible_crtcs & (1 << c)) == 0) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2310 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2311 | |
| 2312 | for (o = 0; o < n; o++) |
| 2313 | if (best_crtcs[o] == crtc) |
| 2314 | break; |
| 2315 | |
| 2316 | if (o < n) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2317 | /* ignore cloning unless only a single crtc */ |
| 2318 | if (fb_helper->crtc_count > 1) |
| 2319 | continue; |
| 2320 | |
| 2321 | if (!drm_mode_equal(modes[o], modes[n])) |
| 2322 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | crtcs[n] = crtc; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2326 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
| 2327 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2328 | width, height); |
| 2329 | if (score > best_score) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2330 | best_score = score; |
| 2331 | memcpy(best_crtcs, crtcs, |
Lyude | 255f0e7 | 2016-05-12 10:56:59 -0400 | [diff] [blame] | 2332 | fb_helper->connector_count * |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2333 | sizeof(struct drm_fb_helper_crtc *)); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2334 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2335 | } |
| 2336 | out: |
| 2337 | kfree(crtcs); |
| 2338 | return best_score; |
| 2339 | } |
| 2340 | |
Chris Wilson | 64e9440 | 2016-11-29 12:02:16 +0000 | [diff] [blame] | 2341 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper, |
| 2342 | u32 width, u32 height) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2343 | { |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2344 | struct drm_device *dev = fb_helper->dev; |
| 2345 | struct drm_fb_helper_crtc **crtcs; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2346 | struct drm_display_mode **modes; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2347 | struct drm_fb_offset *offsets; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2348 | bool *enabled; |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2349 | int i; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2350 | |
| 2351 | DRM_DEBUG_KMS("\n"); |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2352 | /* prevent concurrent modification of connector_count by hotplug */ |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2353 | lockdep_assert_held(&fb_helper->lock); |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2354 | |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2355 | crtcs = kcalloc(fb_helper->connector_count, |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2356 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2357 | modes = kcalloc(fb_helper->connector_count, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2358 | sizeof(struct drm_display_mode *), GFP_KERNEL); |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2359 | offsets = kcalloc(fb_helper->connector_count, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2360 | sizeof(struct drm_fb_offset), GFP_KERNEL); |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2361 | enabled = kcalloc(fb_helper->connector_count, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2362 | sizeof(bool), GFP_KERNEL); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2363 | if (!crtcs || !modes || !enabled || !offsets) { |
Sachin Kamat | 8c5eaca | 2012-11-19 09:44:58 +0000 | [diff] [blame] | 2364 | DRM_ERROR("Memory allocation failed\n"); |
| 2365 | goto out; |
| 2366 | } |
| 2367 | |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 2368 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
| 2369 | if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0) |
| 2370 | DRM_DEBUG_KMS("No connectors reported connected with modes\n"); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2371 | drm_enable_connectors(fb_helper, enabled); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2372 | |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2373 | if (!(fb_helper->funcs->initial_config && |
| 2374 | fb_helper->funcs->initial_config(fb_helper, crtcs, modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2375 | offsets, |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2376 | enabled, width, height))) { |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2377 | memset(modes, 0, fb_helper->connector_count*sizeof(modes[0])); |
| 2378 | memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0])); |
| 2379 | memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0])); |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2380 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2381 | if (!drm_target_cloned(fb_helper, modes, offsets, |
| 2382 | enabled, width, height) && |
| 2383 | !drm_target_preferred(fb_helper, modes, offsets, |
| 2384 | enabled, width, height)) |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2385 | DRM_ERROR("Unable to find initial modes\n"); |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2386 | |
| 2387 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", |
| 2388 | width, height); |
| 2389 | |
| 2390 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2391 | } |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 2392 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2393 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2394 | /* need to set the modesets up here for use later */ |
| 2395 | /* fill out the connector<->crtc mappings into the modesets */ |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 2396 | for (i = 0; i < fb_helper->crtc_count; i++) |
| 2397 | drm_fb_helper_modeset_release(fb_helper, |
| 2398 | &fb_helper->crtc_info[i].mode_set); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2399 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2400 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2401 | struct drm_display_mode *mode = modes[i]; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2402 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2403 | struct drm_fb_offset *offset = &offsets[i]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2404 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2405 | if (mode && fb_crtc) { |
David Lechner | 27a061f | 2017-08-02 13:00:13 -0500 | [diff] [blame] | 2406 | struct drm_mode_set *modeset = &fb_crtc->mode_set; |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 2407 | struct drm_connector *connector = |
| 2408 | fb_helper->connector_info[i]->connector; |
| 2409 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2410 | DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", |
| 2411 | mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 2412 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2413 | fb_crtc->desired_mode = mode; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2414 | fb_crtc->x = offset->x; |
| 2415 | fb_crtc->y = offset->y; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2416 | modeset->mode = drm_mode_duplicate(dev, |
| 2417 | fb_crtc->desired_mode); |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 2418 | drm_connector_get(connector); |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 2419 | modeset->connectors[modeset->num_connectors++] = connector; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2420 | modeset->x = offset->x; |
| 2421 | modeset->y = offset->y; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2422 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2423 | } |
Sachin Kamat | 8c5eaca | 2012-11-19 09:44:58 +0000 | [diff] [blame] | 2424 | out: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2425 | kfree(crtcs); |
| 2426 | kfree(modes); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2427 | kfree(offsets); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2428 | kfree(enabled); |
| 2429 | } |
| 2430 | |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2431 | /* |
| 2432 | * This is a continuation of drm_setup_crtcs() that sets up anything related |
| 2433 | * to the framebuffer. During initialization, drm_setup_crtcs() is called before |
| 2434 | * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev). |
| 2435 | * So, any setup that touches those fields needs to be done here instead of in |
| 2436 | * drm_setup_crtcs(). |
| 2437 | */ |
| 2438 | static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) |
| 2439 | { |
David Lechner | 991a399 | 2017-08-04 11:25:24 -0500 | [diff] [blame] | 2440 | struct fb_info *info = fb_helper->fbdev; |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2441 | int i; |
| 2442 | |
| 2443 | for (i = 0; i < fb_helper->crtc_count; i++) |
| 2444 | if (fb_helper->crtc_info[i].mode_set.num_connectors) |
| 2445 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; |
David Lechner | 991a399 | 2017-08-04 11:25:24 -0500 | [diff] [blame] | 2446 | |
| 2447 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
| 2448 | drm_fb_helper_for_each_connector(fb_helper, i) { |
| 2449 | struct drm_connector *connector = |
| 2450 | fb_helper->connector_info[i]->connector; |
| 2451 | |
| 2452 | /* use first connected connector for the physical dimensions */ |
| 2453 | if (connector->status == connector_status_connected) { |
| 2454 | info->var.width = connector->display_info.width_mm; |
| 2455 | info->var.height = connector->display_info.height_mm; |
| 2456 | break; |
| 2457 | } |
| 2458 | } |
| 2459 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2460 | } |
| 2461 | |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2462 | /* Note: Drops fb_helper->lock before returning. */ |
| 2463 | static int |
| 2464 | __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper, |
| 2465 | int bpp_sel) |
| 2466 | { |
| 2467 | struct drm_device *dev = fb_helper->dev; |
| 2468 | struct fb_info *info; |
| 2469 | unsigned int width, height; |
| 2470 | int ret; |
| 2471 | |
| 2472 | width = dev->mode_config.max_width; |
| 2473 | height = dev->mode_config.max_height; |
| 2474 | |
| 2475 | drm_setup_crtcs(fb_helper, width, height); |
| 2476 | ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); |
| 2477 | if (ret < 0) { |
| 2478 | if (ret == -EAGAIN) { |
| 2479 | fb_helper->preferred_bpp = bpp_sel; |
| 2480 | fb_helper->deferred_setup = true; |
| 2481 | ret = 0; |
| 2482 | } |
| 2483 | mutex_unlock(&fb_helper->lock); |
| 2484 | |
| 2485 | return ret; |
| 2486 | } |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2487 | drm_setup_crtcs_fb(fb_helper); |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2488 | |
| 2489 | fb_helper->deferred_setup = false; |
| 2490 | |
| 2491 | info = fb_helper->fbdev; |
| 2492 | info->var.pixclock = 0; |
| 2493 | |
| 2494 | /* Need to drop locks to avoid recursive deadlock in |
| 2495 | * register_framebuffer. This is ok because the only thing left to do is |
| 2496 | * register the fbdev emulation instance in kernel_fb_helper_list. */ |
| 2497 | mutex_unlock(&fb_helper->lock); |
| 2498 | |
| 2499 | ret = register_framebuffer(info); |
| 2500 | if (ret < 0) |
| 2501 | return ret; |
| 2502 | |
| 2503 | dev_info(dev->dev, "fb%d: %s frame buffer device\n", |
| 2504 | info->node, info->fix.id); |
| 2505 | |
| 2506 | mutex_lock(&kernel_fb_helper_lock); |
| 2507 | if (list_empty(&kernel_fb_helper_list)) |
| 2508 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
| 2509 | |
| 2510 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); |
| 2511 | mutex_unlock(&kernel_fb_helper_lock); |
| 2512 | |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2516 | /** |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2517 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 2518 | * @fb_helper: fb_helper device struct |
| 2519 | * @bpp_sel: bpp value to use for the framebuffer configuration |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2520 | * |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 2521 | * Scans the CRTCs and connectors and tries to put together an initial setup. |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2522 | * At the moment, this is a cloned configuration across all heads with |
| 2523 | * a new framebuffer object as the backing store. |
| 2524 | * |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2525 | * Note that this also registers the fbdev and so allows userspace to call into |
| 2526 | * the driver through the fbdev interfaces. |
| 2527 | * |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 2528 | * This function will call down into the &drm_fb_helper_funcs.fb_probe callback |
| 2529 | * to let the driver allocate and initialize the fbdev info structure and the |
| 2530 | * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2531 | * drm_fb_helper_fill_fix() are provided as helpers to setup simple default |
| 2532 | * values for the fbdev info structure. |
| 2533 | * |
Daniel Vetter | 40f8cf4 | 2016-01-22 08:53:45 +0100 | [diff] [blame] | 2534 | * HANG DEBUGGING: |
| 2535 | * |
| 2536 | * When you have fbcon support built-in or already loaded, this function will do |
| 2537 | * a full modeset to setup the fbdev console. Due to locking misdesign in the |
| 2538 | * VT/fbdev subsystem that entire modeset sequence has to be done while holding |
| 2539 | * console_lock. Until console_unlock is called no dmesg lines will be sent out |
| 2540 | * to consoles, not even serial console. This means when your driver crashes, |
| 2541 | * you will see absolutely nothing else but a system stuck in this function, |
| 2542 | * with no further output. Any kind of printk() you place within your own driver |
| 2543 | * or in the drm core modeset code will also never show up. |
| 2544 | * |
| 2545 | * Standard debug practice is to run the fbcon setup without taking the |
| 2546 | * console_lock as a hack, to be able to see backtraces and crashes on the |
| 2547 | * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel |
| 2548 | * cmdline option. |
| 2549 | * |
| 2550 | * The other option is to just disable fbdev emulation since very likely the |
Lyude | af509d3 | 2016-05-04 11:28:53 -0400 | [diff] [blame] | 2551 | * first modeset from userspace will crash in the same way, and is even easier |
| 2552 | * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 |
Daniel Vetter | 40f8cf4 | 2016-01-22 08:53:45 +0100 | [diff] [blame] | 2553 | * kernel cmdline option. |
| 2554 | * |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2555 | * RETURNS: |
| 2556 | * Zero if everything went ok, nonzero otherwise. |
| 2557 | */ |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 2558 | int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2559 | { |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2560 | int ret; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2561 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 2562 | if (!drm_fbdev_emulation) |
| 2563 | return 0; |
| 2564 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2565 | mutex_lock(&fb_helper->lock); |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2566 | ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2567 | |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2568 | return ret; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2569 | } |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2570 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2571 | |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2572 | /** |
| 2573 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 2574 | * probing all the outputs attached to the fb |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2575 | * @fb_helper: the drm_fb_helper |
| 2576 | * |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2577 | * Scan the connectors attached to the fb_helper and try to put together a |
Daniel Vetter | 62cacc7 | 2016-08-12 22:48:37 +0200 | [diff] [blame] | 2578 | * setup after notification of a change in output configuration. |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2579 | * |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2580 | * Called at runtime, takes the mode config locks to be able to check/change the |
| 2581 | * modeset configuration. Must be run from process context (which usually means |
| 2582 | * either the output polling work or a work item launched from the driver's |
| 2583 | * hotplug interrupt). |
| 2584 | * |
Daniel Vetter | 50c3dc9 | 2014-06-27 17:19:22 +0200 | [diff] [blame] | 2585 | * Note that drivers may call this even before calling |
Lyude | af509d3 | 2016-05-04 11:28:53 -0400 | [diff] [blame] | 2586 | * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows |
Daniel Vetter | 50c3dc9 | 2014-06-27 17:19:22 +0200 | [diff] [blame] | 2587 | * for a race-free fbcon setup and will make sure that the fbdev emulation will |
| 2588 | * not miss any hotplug events. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2589 | * |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2590 | * RETURNS: |
| 2591 | * 0 on success and a non-zero error code otherwise. |
| 2592 | */ |
| 2593 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2594 | { |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2595 | int err = 0; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 2596 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 2597 | if (!drm_fbdev_emulation) |
| 2598 | return 0; |
| 2599 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2600 | mutex_lock(&fb_helper->lock); |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2601 | if (fb_helper->deferred_setup) { |
| 2602 | err = __drm_fb_helper_initial_config_and_unlock(fb_helper, |
| 2603 | fb_helper->preferred_bpp); |
| 2604 | return err; |
| 2605 | } |
| 2606 | |
Daniel Vetter | 50c3dc9 | 2014-06-27 17:19:22 +0200 | [diff] [blame] | 2607 | if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 2608 | fb_helper->delayed_hotplug = true; |
Daniel Vetter | bdac4a0 | 2017-07-04 17:18:24 +0200 | [diff] [blame] | 2609 | mutex_unlock(&fb_helper->lock); |
| 2610 | return err; |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 2611 | } |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2612 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2613 | DRM_DEBUG_KMS("\n"); |
| 2614 | |
Chris Wilson | 64e9440 | 2016-11-29 12:02:16 +0000 | [diff] [blame] | 2615 | drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height); |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2616 | drm_setup_crtcs_fb(fb_helper); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2617 | mutex_unlock(&fb_helper->lock); |
Daniel Vetter | 89ced12 | 2013-04-11 14:26:55 +0000 | [diff] [blame] | 2618 | |
Daniel Vetter | 2180c3c | 2013-01-21 23:12:36 +0100 | [diff] [blame] | 2619 | drm_fb_helper_set_par(fb_helper->fbdev); |
| 2620 | |
| 2621 | return 0; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2622 | } |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 2623 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
Dave Airlie | 5c4426a | 2010-03-30 05:34:17 +0000 | [diff] [blame] | 2624 | |
David Rientjes | 6a108a1 | 2011-01-20 14:44:16 -0800 | [diff] [blame] | 2625 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 2626 | * but the module doesn't depend on any fb console symbols. At least |
| 2627 | * attempt to load fbcon to avoid leaving the system without a usable console. |
| 2628 | */ |
Rafael Antognolli | 70412cf | 2016-01-21 15:10:18 -0800 | [diff] [blame] | 2629 | int __init drm_fb_helper_modinit(void) |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 2630 | { |
Rafael Antognolli | 70412cf | 2016-01-21 15:10:18 -0800 | [diff] [blame] | 2631 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
Kees Cook | 0632466 | 2017-05-08 15:59:05 -0700 | [diff] [blame] | 2632 | const char name[] = "fbcon"; |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 2633 | struct module *fbcon; |
| 2634 | |
| 2635 | mutex_lock(&module_mutex); |
| 2636 | fbcon = find_module(name); |
| 2637 | mutex_unlock(&module_mutex); |
| 2638 | |
| 2639 | if (!fbcon) |
| 2640 | request_module_nowait(name); |
Rafael Antognolli | 70412cf | 2016-01-21 15:10:18 -0800 | [diff] [blame] | 2641 | #endif |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 2642 | return 0; |
| 2643 | } |
Rafael Antognolli | 70412cf | 2016-01-21 15:10:18 -0800 | [diff] [blame] | 2644 | EXPORT_SYMBOL(drm_fb_helper_modinit); |