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> |
Noralf Trønnes | d536540 | 2018-07-03 18:03:48 +0200 | [diff] [blame] | 33 | #include <linux/dma-buf.h> |
Andy Shevchenko | 3b40a44 | 2010-02-02 14:40:32 -0800 | [diff] [blame] | 34 | #include <linux/kernel.h> |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 35 | #include <linux/sysrq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Paul Gortmaker | e0cd360 | 2011-08-30 11:04:30 -0400 | [diff] [blame] | 37 | #include <linux/module.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 38 | #include <drm/drmP.h> |
| 39 | #include <drm/drm_crtc.h> |
| 40 | #include <drm/drm_fb_helper.h> |
| 41 | #include <drm/drm_crtc_helper.h> |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 42 | #include <drm/drm_atomic.h> |
| 43 | #include <drm/drm_atomic_helper.h> |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 44 | |
Hans de Goede | 8f0cb41 | 2017-11-25 20:35:50 +0100 | [diff] [blame] | 45 | #include "drm_crtc_internal.h" |
Ville Syrjälä | 699fbee | 2016-09-19 16:33:44 +0300 | [diff] [blame] | 46 | #include "drm_crtc_helper_internal.h" |
| 47 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 48 | static bool drm_fbdev_emulation = true; |
| 49 | module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); |
| 50 | MODULE_PARM_DESC(fbdev_emulation, |
| 51 | "Enable legacy fbdev emulation [default=true]"); |
| 52 | |
Xinliang Liu | 5f15257 | 2017-02-15 17:19:08 +0100 | [diff] [blame] | 53 | static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; |
| 54 | module_param(drm_fbdev_overalloc, int, 0444); |
| 55 | MODULE_PARM_DESC(drm_fbdev_overalloc, |
| 56 | "Overallocation of the fbdev buffer (%) [default=" |
| 57 | __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); |
| 58 | |
Neil Armstrong | 4be9bd1 | 2018-09-28 14:05:55 +0200 | [diff] [blame] | 59 | /* |
| 60 | * In order to keep user-space compatibility, we want in certain use-cases |
| 61 | * to keep leaking the fbdev physical address to the user-space program |
| 62 | * handling the fbdev buffer. |
| 63 | * This is a bad habit essentially kept into closed source opengl driver |
| 64 | * that should really be moved into open-source upstream projects instead |
| 65 | * of using legacy physical addresses in user space to communicate with |
| 66 | * other out-of-tree kernel modules. |
| 67 | * |
| 68 | * This module_param *should* be removed as soon as possible and be |
| 69 | * considered as a broken and legacy behaviour from a modern fbdev device. |
| 70 | */ |
| 71 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) |
| 72 | static bool drm_leak_fbdev_smem = false; |
| 73 | module_param_unsafe(drm_leak_fbdev_smem, bool, 0600); |
Wei Yongjun | b31a3ca | 2018-12-04 06:32:15 +0000 | [diff] [blame] | 74 | MODULE_PARM_DESC(drm_leak_fbdev_smem, |
Neil Armstrong | 4be9bd1 | 2018-09-28 14:05:55 +0200 | [diff] [blame] | 75 | "Allow unsafe leaking fbdev physical smem address [default=false]"); |
| 76 | #endif |
| 77 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 78 | static LIST_HEAD(kernel_fb_helper_list); |
Chris Wilson | a53ca63 | 2016-11-29 12:02:17 +0000 | [diff] [blame] | 79 | static DEFINE_MUTEX(kernel_fb_helper_lock); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 80 | |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 81 | /** |
| 82 | * DOC: fbdev helpers |
| 83 | * |
| 84 | * 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] | 85 | * mode setting driver. They can be used mostly independently from the crtc |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 86 | * helper functions used by many drivers to implement the kernel mode setting |
| 87 | * interfaces. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 88 | * |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 89 | * Drivers that support a dumb buffer with a virtual address and mmap support, |
| 90 | * should try out the generic fbdev emulation using drm_fbdev_generic_setup(). |
| 91 | * |
Noralf Trønnes | 95b0137 | 2017-12-15 18:51:16 +0100 | [diff] [blame] | 92 | * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it |
| 93 | * down by calling drm_fb_helper_fbdev_teardown(). |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 94 | * |
Noralf Trønnes | 95b0137 | 2017-12-15 18:51:16 +0100 | [diff] [blame] | 95 | * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use |
| 96 | * the setup helper and will need to do the whole four-step setup process with |
| 97 | * drm_fb_helper_prepare(), drm_fb_helper_init(), |
| 98 | * drm_fb_helper_single_add_all_connectors(), enable hotplugging and |
| 99 | * drm_fb_helper_initial_config() to avoid a possible race window. |
| 100 | * |
| 101 | * At runtime drivers should restore the fbdev console by using |
| 102 | * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback. |
| 103 | * They should also notify the fb helper code from updates to the output |
| 104 | * configuration by using drm_fb_helper_output_poll_changed() as their |
| 105 | * &drm_mode_config_funcs.output_poll_changed callback. |
| 106 | * |
| 107 | * For suspend/resume consider using drm_mode_config_helper_suspend() and |
| 108 | * drm_mode_config_helper_resume() which takes care of fbdev as well. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 109 | * |
| 110 | * All other functions exported by the fb helper library can be used to |
| 111 | * implement the fbdev driver interface by the driver. |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 112 | * |
| 113 | * It is possible, though perhaps somewhat tricky, to implement race-free |
| 114 | * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() |
| 115 | * helper must be called first to initialize the minimum required to make |
| 116 | * 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] | 117 | * 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] | 118 | * it is safe to enable interrupts and start processing hotplug events. At the |
| 119 | * same time, drivers should initialize all modeset objects such as CRTCs, |
| 120 | * encoders and connectors. To finish up the fbdev helper initialization, the |
| 121 | * drm_fb_helper_init() function is called. To probe for all attached displays |
| 122 | * and set up an initial configuration using the detected hardware, drivers |
| 123 | * should call drm_fb_helper_single_add_all_connectors() followed by |
| 124 | * drm_fb_helper_initial_config(). |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 125 | * |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 126 | * If &drm_framebuffer_funcs.dirty is set, the |
Noralf Trønnes | 2dad551 | 2016-05-11 18:09:17 +0200 | [diff] [blame] | 127 | * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 128 | * 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] | 129 | * away. This worker then calls the dirty() function ensuring that it will |
| 130 | * always run in process context since the fb_*() function could be running in |
| 131 | * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io |
| 132 | * callback it will also schedule dirty_work with the damage collected from the |
Noralf Trønnes | 48c9571 | 2017-12-15 18:51:17 +0100 | [diff] [blame] | 133 | * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup |
| 134 | * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()). |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 135 | */ |
| 136 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 137 | #define drm_fb_helper_for_each_connector(fbh, i__) \ |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 138 | for (({ lockdep_assert_held(&(fbh)->lock); }), \ |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 139 | i__ = 0; i__ < (fbh)->connector_count; i__++) |
| 140 | |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 141 | static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, |
| 142 | struct drm_connector *connector) |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 143 | { |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 144 | struct drm_fb_helper_connector *fb_conn; |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 145 | struct drm_fb_helper_connector **temp; |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 146 | unsigned int count; |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 147 | |
| 148 | if (!drm_fbdev_emulation) |
| 149 | return 0; |
| 150 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 151 | lockdep_assert_held(&fb_helper->lock); |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 152 | |
| 153 | count = fb_helper->connector_count + 1; |
| 154 | |
| 155 | if (count > fb_helper->connector_info_alloc_count) { |
| 156 | size_t size = count * sizeof(fb_conn); |
| 157 | |
| 158 | temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL); |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 159 | if (!temp) |
| 160 | return -ENOMEM; |
| 161 | |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 162 | fb_helper->connector_info_alloc_count = count; |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 163 | fb_helper->connector_info = temp; |
| 164 | } |
| 165 | |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 166 | fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL); |
| 167 | if (!fb_conn) |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 168 | return -ENOMEM; |
| 169 | |
| 170 | drm_connector_get(connector); |
Thierry Reding | 50021ff | 2017-03-29 16:43:53 +0200 | [diff] [blame] | 171 | fb_conn->connector = connector; |
| 172 | fb_helper->connector_info[fb_helper->connector_count++] = fb_conn; |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 173 | |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 174 | return 0; |
| 175 | } |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 176 | |
| 177 | int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, |
| 178 | struct drm_connector *connector) |
| 179 | { |
| 180 | int err; |
| 181 | |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 182 | if (!fb_helper) |
| 183 | return 0; |
| 184 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 185 | mutex_lock(&fb_helper->lock); |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 186 | err = __drm_fb_helper_add_one_connector(fb_helper, connector); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 187 | mutex_unlock(&fb_helper->lock); |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 188 | |
| 189 | return err; |
| 190 | } |
Thierry Reding | 39b8b2e | 2017-03-29 16:43:52 +0200 | [diff] [blame] | 191 | EXPORT_SYMBOL(drm_fb_helper_add_one_connector); |
| 192 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 193 | /** |
| 194 | * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev |
| 195 | * emulation helper |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 196 | * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 197 | * |
| 198 | * This functions adds all the available connectors for use with the given |
| 199 | * fb_helper. This is a separate step to allow drivers to freely assign |
| 200 | * connectors to the fbdev, e.g. if some are reserved for special purposes or |
| 201 | * not adequate to be used for the fbcon. |
| 202 | * |
Daniel Vetter | 169faec | 2015-07-09 23:44:27 +0200 | [diff] [blame] | 203 | * This function is protected against concurrent connector hotadds/removals |
| 204 | * using drm_fb_helper_add_one_connector() and |
| 205 | * drm_fb_helper_remove_one_connector(). |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 206 | */ |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 207 | 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] | 208 | { |
Gustavo A. R. Silva | 89f3f35 | 2017-12-05 11:46:28 -0600 | [diff] [blame] | 209 | struct drm_device *dev; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 210 | struct drm_connector *connector; |
Daniel Vetter | c36a325 | 2016-12-15 16:58:43 +0100 | [diff] [blame] | 211 | struct drm_connector_list_iter conn_iter; |
| 212 | int i, ret = 0; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 213 | |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 214 | if (!drm_fbdev_emulation || !fb_helper) |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 215 | return 0; |
| 216 | |
Gustavo A. R. Silva | 89f3f35 | 2017-12-05 11:46:28 -0600 | [diff] [blame] | 217 | dev = fb_helper->dev; |
| 218 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 219 | mutex_lock(&fb_helper->lock); |
Thierry Reding | b982dab | 2017-02-28 15:46:43 +0100 | [diff] [blame] | 220 | drm_connector_list_iter_begin(dev, &conn_iter); |
Daniel Vetter | c36a325 | 2016-12-15 16:58:43 +0100 | [diff] [blame] | 221 | drm_for_each_connector_iter(connector, &conn_iter) { |
Paul Kocialkowski | 8fd3b90 | 2018-11-15 17:32:48 +0100 | [diff] [blame] | 222 | if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) |
| 223 | continue; |
| 224 | |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 225 | ret = __drm_fb_helper_add_one_connector(fb_helper, connector); |
Maarten Lankhorst | 15fce29 | 2016-02-15 13:45:16 +0100 | [diff] [blame] | 226 | if (ret) |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 227 | goto fail; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 228 | } |
Daniel Vetter | c36a325 | 2016-12-15 16:58:43 +0100 | [diff] [blame] | 229 | goto out; |
| 230 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 231 | fail: |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 232 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Ville Syrjälä | 7dfcb36 | 2016-10-26 12:05:52 +0300 | [diff] [blame] | 233 | struct drm_fb_helper_connector *fb_helper_connector = |
| 234 | fb_helper->connector_info[i]; |
| 235 | |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 236 | drm_connector_put(fb_helper_connector->connector); |
Ville Syrjälä | 7dfcb36 | 2016-10-26 12:05:52 +0300 | [diff] [blame] | 237 | |
| 238 | kfree(fb_helper_connector); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 239 | fb_helper->connector_info[i] = NULL; |
| 240 | } |
| 241 | fb_helper->connector_count = 0; |
Daniel Vetter | c36a325 | 2016-12-15 16:58:43 +0100 | [diff] [blame] | 242 | out: |
Thierry Reding | b982dab | 2017-02-28 15:46:43 +0100 | [diff] [blame] | 243 | drm_connector_list_iter_end(&conn_iter); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 244 | mutex_unlock(&fb_helper->lock); |
Daniel Vetter | 169faec | 2015-07-09 23:44:27 +0200 | [diff] [blame] | 245 | |
Maarten Lankhorst | 15fce29 | 2016-02-15 13:45:16 +0100 | [diff] [blame] | 246 | return ret; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 247 | } |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 248 | EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 249 | |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 250 | static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, |
| 251 | struct drm_connector *connector) |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 252 | { |
| 253 | struct drm_fb_helper_connector *fb_helper_connector; |
| 254 | int i, j; |
| 255 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 256 | if (!drm_fbdev_emulation) |
| 257 | return 0; |
| 258 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 259 | lockdep_assert_held(&fb_helper->lock); |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 260 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 261 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 262 | if (fb_helper->connector_info[i]->connector == connector) |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | if (i == fb_helper->connector_count) |
| 267 | return -EINVAL; |
| 268 | fb_helper_connector = fb_helper->connector_info[i]; |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 269 | drm_connector_put(fb_helper_connector->connector); |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 270 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 271 | for (j = i + 1; j < fb_helper->connector_count; j++) |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 272 | fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 273 | |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 274 | fb_helper->connector_count--; |
| 275 | kfree(fb_helper_connector); |
Rob Clark | 2148f18 | 2015-01-26 10:11:08 -0500 | [diff] [blame] | 276 | |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 277 | return 0; |
| 278 | } |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 279 | |
| 280 | int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, |
| 281 | struct drm_connector *connector) |
| 282 | { |
| 283 | int err; |
| 284 | |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 285 | if (!fb_helper) |
| 286 | return 0; |
| 287 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 288 | mutex_lock(&fb_helper->lock); |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 289 | err = __drm_fb_helper_remove_one_connector(fb_helper, connector); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 290 | mutex_unlock(&fb_helper->lock); |
Thierry Reding | af2405a | 2017-07-04 17:18:21 +0200 | [diff] [blame] | 291 | |
| 292 | return err; |
| 293 | } |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 294 | EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); |
| 295 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 296 | static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) |
| 297 | { |
| 298 | uint16_t *r_base, *g_base, *b_base; |
| 299 | |
Laurent Pinchart | ebe0f24 | 2012-05-17 13:27:24 +0200 | [diff] [blame] | 300 | if (crtc->funcs->gamma_set == NULL) |
| 301 | return; |
| 302 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 303 | r_base = crtc->gamma_store; |
| 304 | g_base = r_base + crtc->gamma_size; |
| 305 | b_base = g_base + crtc->gamma_size; |
| 306 | |
Daniel Vetter | 6d124ff | 2017-04-03 10:33:01 +0200 | [diff] [blame] | 307 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, |
| 308 | crtc->gamma_size, NULL); |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 311 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 312 | * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 313 | * @info: fbdev registered by the helper |
| 314 | */ |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 315 | int drm_fb_helper_debug_enter(struct fb_info *info) |
| 316 | { |
| 317 | struct drm_fb_helper *helper = info->par; |
Jani Nikula | be26a66 | 2015-03-11 11:51:06 +0200 | [diff] [blame] | 318 | const struct drm_crtc_helper_funcs *funcs; |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 319 | int i; |
| 320 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 321 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
| 322 | for (i = 0; i < helper->crtc_count; i++) { |
| 323 | struct drm_mode_set *mode_set = |
| 324 | &helper->crtc_info[i].mode_set; |
| 325 | |
| 326 | if (!mode_set->crtc->enabled) |
| 327 | continue; |
| 328 | |
| 329 | funcs = mode_set->crtc->helper_private; |
Stefan Christ | 1b99b72 | 2016-11-14 00:03:11 +0100 | [diff] [blame] | 330 | if (funcs->mode_set_base_atomic == NULL) |
| 331 | continue; |
| 332 | |
Daniel Vetter | 9c79e0b | 2017-04-03 10:32:59 +0200 | [diff] [blame] | 333 | if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) |
| 334 | continue; |
| 335 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 336 | funcs->mode_set_base_atomic(mode_set->crtc, |
| 337 | mode_set->fb, |
| 338 | mode_set->x, |
Jason Wessel | 413d45d | 2010-09-26 06:47:25 -0500 | [diff] [blame] | 339 | mode_set->y, |
Jason Wessel | 21c74a8 | 2010-10-13 14:09:44 -0500 | [diff] [blame] | 340 | ENTER_ATOMIC_MODE_SET); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | EXPORT_SYMBOL(drm_fb_helper_debug_enter); |
| 347 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 348 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 349 | * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 350 | * @info: fbdev registered by the helper |
| 351 | */ |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 352 | int drm_fb_helper_debug_leave(struct fb_info *info) |
| 353 | { |
| 354 | struct drm_fb_helper *helper = info->par; |
| 355 | struct drm_crtc *crtc; |
Jani Nikula | be26a66 | 2015-03-11 11:51:06 +0200 | [diff] [blame] | 356 | const struct drm_crtc_helper_funcs *funcs; |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 357 | struct drm_framebuffer *fb; |
| 358 | int i; |
| 359 | |
| 360 | for (i = 0; i < helper->crtc_count; i++) { |
| 361 | struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 362 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 363 | crtc = mode_set->crtc; |
Maarten Lankhorst | 7114d2e | 2017-07-03 13:51:06 +0200 | [diff] [blame] | 364 | if (drm_drv_uses_atomic_modeset(crtc->dev)) |
| 365 | continue; |
| 366 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 367 | funcs = crtc->helper_private; |
Maarten Lankhorst | 7114d2e | 2017-07-03 13:51:06 +0200 | [diff] [blame] | 368 | fb = crtc->primary->fb; |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 369 | |
| 370 | if (!crtc->enabled) |
| 371 | continue; |
| 372 | |
| 373 | if (!fb) { |
| 374 | DRM_ERROR("no fb to restore??\n"); |
| 375 | continue; |
| 376 | } |
| 377 | |
Stefan Christ | 1b99b72 | 2016-11-14 00:03:11 +0100 | [diff] [blame] | 378 | if (funcs->mode_set_base_atomic == NULL) |
| 379 | continue; |
| 380 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 381 | drm_fb_helper_restore_lut_atomic(mode_set->crtc); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 382 | funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, |
Jason Wessel | 21c74a8 | 2010-10-13 14:09:44 -0500 | [diff] [blame] | 383 | crtc->y, LEAVE_ATOMIC_MODE_SET); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | EXPORT_SYMBOL(drm_fb_helper_debug_leave); |
| 389 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 390 | 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] | 391 | { |
| 392 | struct drm_device *dev = fb_helper->dev; |
Hans de Goede | 8f0cb41 | 2017-11-25 20:35:50 +0100 | [diff] [blame] | 393 | struct drm_plane_state *plane_state; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 394 | struct drm_plane *plane; |
| 395 | struct drm_atomic_state *state; |
| 396 | int i, ret; |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 397 | struct drm_modeset_acquire_ctx ctx; |
| 398 | |
| 399 | drm_modeset_acquire_init(&ctx, 0); |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 400 | |
| 401 | state = drm_atomic_state_alloc(dev); |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 402 | if (!state) { |
| 403 | ret = -ENOMEM; |
| 404 | goto out_ctx; |
| 405 | } |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 406 | |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 407 | state->acquire_ctx = &ctx; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 408 | retry: |
| 409 | drm_for_each_plane(plane, dev) { |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 410 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 411 | if (IS_ERR(plane_state)) { |
| 412 | ret = PTR_ERR(plane_state); |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 413 | goto out_state; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Robert Foss | c2c446a | 2017-05-19 16:50:17 -0400 | [diff] [blame] | 416 | plane_state->rotation = DRM_MODE_ROTATE_0; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 417 | |
| 418 | /* disable non-primary: */ |
| 419 | if (plane->type == DRM_PLANE_TYPE_PRIMARY) |
| 420 | continue; |
| 421 | |
| 422 | ret = __drm_atomic_helper_disable_plane(plane, plane_state); |
| 423 | if (ret != 0) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 424 | goto out_state; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 427 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 428 | struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; |
Hans de Goede | 8f0cb41 | 2017-11-25 20:35:50 +0100 | [diff] [blame] | 429 | struct drm_plane *primary = mode_set->crtc->primary; |
| 430 | |
| 431 | /* Cannot fail as we've already gotten the plane state above */ |
| 432 | plane_state = drm_atomic_get_new_plane_state(state, primary); |
| 433 | plane_state->rotation = fb_helper->crtc_info[i].rotation; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 434 | |
| 435 | ret = __drm_atomic_helper_set_config(mode_set, state); |
| 436 | if (ret != 0) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 437 | goto out_state; |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 438 | |
| 439 | /* |
| 440 | * __drm_atomic_helper_set_config() sets active when a |
| 441 | * mode is set, unconditionally clear it if we force DPMS off |
| 442 | */ |
| 443 | if (!active) { |
| 444 | struct drm_crtc *crtc = mode_set->crtc; |
| 445 | struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); |
| 446 | |
| 447 | crtc_state->active = false; |
| 448 | } |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | ret = drm_atomic_commit(state); |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 452 | |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 453 | out_state: |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 454 | if (ret == -EDEADLK) |
| 455 | goto backoff; |
| 456 | |
Chris Wilson | 0853695 | 2016-10-14 13:18:18 +0100 | [diff] [blame] | 457 | drm_atomic_state_put(state); |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 458 | out_ctx: |
| 459 | drm_modeset_drop_locks(&ctx); |
| 460 | drm_modeset_acquire_fini(&ctx); |
| 461 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 462 | return ret; |
| 463 | |
| 464 | backoff: |
| 465 | drm_atomic_state_clear(state); |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 466 | drm_modeset_backoff(&ctx); |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 467 | |
| 468 | goto retry; |
| 469 | } |
| 470 | |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 471 | static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper) |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 472 | { |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 473 | struct drm_device *dev = fb_helper->dev; |
| 474 | struct drm_plane *plane; |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 475 | int i, ret = 0; |
Daniel Vetter | 6aed8ec | 2013-01-20 17:32:21 +0100 | [diff] [blame] | 476 | |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 477 | drm_modeset_lock_all(fb_helper->dev); |
Daniel Vetter | 6295d60 | 2015-07-09 23:44:25 +0200 | [diff] [blame] | 478 | drm_for_each_plane(plane, dev) { |
Matt Roper | e27dde3 | 2014-04-01 15:22:30 -0700 | [diff] [blame] | 479 | if (plane->type != DRM_PLANE_TYPE_PRIMARY) |
| 480 | drm_plane_force_disable(plane); |
Daniel Vetter | 6aed8ec | 2013-01-20 17:32:21 +0100 | [diff] [blame] | 481 | |
Ville Syrjälä | 6686df8 | 2016-10-21 22:22:45 +0300 | [diff] [blame] | 482 | if (plane->rotation_property) |
Ville Syrjälä | d138dd3 | 2016-09-26 19:30:48 +0300 | [diff] [blame] | 483 | drm_mode_plane_set_obj_prop(plane, |
| 484 | plane->rotation_property, |
Robert Foss | c2c446a | 2017-05-19 16:50:17 -0400 | [diff] [blame] | 485 | DRM_MODE_ROTATE_0); |
Sonika Jindal | 9783de2 | 2014-08-05 11:26:57 +0530 | [diff] [blame] | 486 | } |
| 487 | |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 488 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 489 | 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] | 490 | struct drm_crtc *crtc = mode_set->crtc; |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 491 | |
Alex Deucher | 03f9abb | 2015-09-30 14:47:37 -0400 | [diff] [blame] | 492 | if (crtc->funcs->cursor_set2) { |
| 493 | ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0); |
| 494 | if (ret) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 495 | goto out; |
Alex Deucher | 03f9abb | 2015-09-30 14:47:37 -0400 | [diff] [blame] | 496 | } else if (crtc->funcs->cursor_set) { |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 497 | ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); |
| 498 | if (ret) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 499 | goto out; |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 500 | } |
| 501 | |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 502 | ret = drm_mode_set_config_internal(mode_set); |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 503 | if (ret) |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 504 | goto out; |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 505 | } |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 506 | out: |
| 507 | drm_modeset_unlock_all(fb_helper->dev); |
Daniel Vetter | b7bdf0a8 | 2015-08-25 17:20:28 +0200 | [diff] [blame] | 508 | |
Daniel Vetter | 1d0c64109 | 2017-07-04 17:18:27 +0200 | [diff] [blame] | 509 | return ret; |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 510 | } |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 511 | |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 512 | static int restore_fbdev_mode(struct drm_fb_helper *fb_helper) |
| 513 | { |
| 514 | struct drm_device *dev = fb_helper->dev; |
| 515 | |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 516 | if (drm_drv_uses_atomic_modeset(dev)) |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 517 | return restore_fbdev_mode_atomic(fb_helper, true); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 518 | else |
| 519 | return restore_fbdev_mode_legacy(fb_helper); |
| 520 | } |
| 521 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 522 | /** |
| 523 | * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 524 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 525 | * |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 526 | * This should be called from driver's drm &drm_driver.lastclose callback |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 527 | * when implementing an fbcon on top of kms using this helper. This ensures that |
| 528 | * 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] | 529 | * |
| 530 | * RETURNS: |
| 531 | * Zero if everything went ok, negative error code otherwise. |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 532 | */ |
Daniel Vetter | b7bdf0a8 | 2015-08-25 17:20:28 +0200 | [diff] [blame] | 533 | 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] | 534 | { |
Daniel Vetter | b7bdf0a8 | 2015-08-25 17:20:28 +0200 | [diff] [blame] | 535 | bool do_delayed; |
| 536 | int ret; |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 537 | |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 538 | if (!drm_fbdev_emulation || !fb_helper) |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 539 | return -ENODEV; |
| 540 | |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 541 | if (READ_ONCE(fb_helper->deferred_setup)) |
| 542 | return 0; |
| 543 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 544 | mutex_lock(&fb_helper->lock); |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 545 | ret = restore_fbdev_mode(fb_helper); |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 546 | |
| 547 | do_delayed = fb_helper->delayed_hotplug; |
| 548 | if (do_delayed) |
| 549 | fb_helper->delayed_hotplug = false; |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 550 | mutex_unlock(&fb_helper->lock); |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 551 | |
| 552 | if (do_delayed) |
| 553 | drm_fb_helper_hotplug_event(fb_helper); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 554 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 555 | return ret; |
| 556 | } |
| 557 | EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 558 | |
Geert Uytterhoeven | 2c4124f | 2015-08-04 15:22:11 +0200 | [diff] [blame] | 559 | static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) |
| 560 | { |
| 561 | struct drm_device *dev = fb_helper->dev; |
| 562 | struct drm_crtc *crtc; |
| 563 | int bound = 0, crtcs_bound = 0; |
| 564 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 565 | /* |
| 566 | * Sometimes user space wants everything disabled, so don't steal the |
| 567 | * display if there's a master. |
| 568 | */ |
Johannes Berg | f17b3ea | 2016-08-11 11:50:21 +0200 | [diff] [blame] | 569 | if (READ_ONCE(dev->master)) |
Geert Uytterhoeven | 2c4124f | 2015-08-04 15:22:11 +0200 | [diff] [blame] | 570 | return false; |
| 571 | |
| 572 | drm_for_each_crtc(crtc, dev) { |
Daniel Vetter | bdac4a0 | 2017-07-04 17:18:24 +0200 | [diff] [blame] | 573 | drm_modeset_lock(&crtc->mutex, NULL); |
Geert Uytterhoeven | 2c4124f | 2015-08-04 15:22:11 +0200 | [diff] [blame] | 574 | if (crtc->primary->fb) |
| 575 | crtcs_bound++; |
| 576 | if (crtc->primary->fb == fb_helper->fb) |
| 577 | bound++; |
Daniel Vetter | bdac4a0 | 2017-07-04 17:18:24 +0200 | [diff] [blame] | 578 | drm_modeset_unlock(&crtc->mutex); |
Geert Uytterhoeven | 2c4124f | 2015-08-04 15:22:11 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | if (bound < crtcs_bound) |
| 582 | return false; |
| 583 | |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | #ifdef CONFIG_MAGIC_SYSRQ |
Daniel Vetter | d21bf46 | 2013-01-20 18:09:52 +0100 | [diff] [blame] | 588 | /* |
| 589 | * restore fbcon display for all kms driver's using this helper, used for sysrq |
| 590 | * and panic handling. |
| 591 | */ |
Sachin Kamat | 78b9c35 | 2012-08-01 17:15:32 +0530 | [diff] [blame] | 592 | static bool drm_fb_helper_force_kernel_mode(void) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 593 | { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 594 | bool ret, error = false; |
| 595 | struct drm_fb_helper *helper; |
| 596 | |
| 597 | if (list_empty(&kernel_fb_helper_list)) |
| 598 | return false; |
| 599 | |
| 600 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
Thierry Reding | b77f076 | 2014-04-29 11:44:32 +0200 | [diff] [blame] | 601 | struct drm_device *dev = helper->dev; |
| 602 | |
| 603 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 604 | continue; |
| 605 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 606 | mutex_lock(&helper->lock); |
Geert Uytterhoeven | 3d9e35a | 2015-08-04 15:22:10 +0200 | [diff] [blame] | 607 | ret = restore_fbdev_mode(helper); |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 608 | if (ret) |
| 609 | error = true; |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 610 | mutex_unlock(&helper->lock); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 611 | } |
| 612 | return error; |
| 613 | } |
| 614 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 615 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
| 616 | { |
Daniel Vetter | d21bf46 | 2013-01-20 18:09:52 +0100 | [diff] [blame] | 617 | bool ret; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 618 | |
Daniel Vetter | d21bf46 | 2013-01-20 18:09:52 +0100 | [diff] [blame] | 619 | ret = drm_fb_helper_force_kernel_mode(); |
| 620 | if (ret == true) |
| 621 | DRM_ERROR("Failed to restore crtc configuration\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 622 | } |
| 623 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); |
| 624 | |
Dmitry Torokhov | 1495cc9 | 2010-08-17 21:15:46 -0700 | [diff] [blame] | 625 | static void drm_fb_helper_sysrq(int dummy1) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 626 | { |
| 627 | schedule_work(&drm_fb_helper_restore_work); |
| 628 | } |
| 629 | |
| 630 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { |
| 631 | .handler = drm_fb_helper_sysrq, |
| 632 | .help_msg = "force-fb(V)", |
| 633 | .action_msg = "Restore framebuffer console", |
| 634 | }; |
Randy Dunlap | b8c40d6 | 2010-03-25 18:29:05 +0000 | [diff] [blame] | 635 | #else |
| 636 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; |
Mikael Pettersson | bea1d35 | 2009-09-28 18:26:25 +0200 | [diff] [blame] | 637 | #endif |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 638 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 639 | 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] | 640 | { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 641 | struct drm_device *dev = fb_helper->dev; |
| 642 | struct drm_crtc *crtc; |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 643 | struct drm_connector *connector; |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 644 | int i, j; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 645 | |
Daniel Vetter | bdac4a0 | 2017-07-04 17:18:24 +0200 | [diff] [blame] | 646 | drm_modeset_lock_all(dev); |
Jesse Barnes | e87b2c4 | 2009-09-17 18:14:41 -0700 | [diff] [blame] | 647 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 648 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 649 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 650 | if (!crtc->enabled) |
| 651 | continue; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 652 | |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 653 | /* Walk the connectors & encoders on this fb turning them on/off */ |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 654 | drm_fb_helper_for_each_connector(fb_helper, j) { |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 655 | connector = fb_helper->connector_info[j]->connector; |
Daniel Vetter | e04190e | 2012-09-07 10:14:52 +0200 | [diff] [blame] | 656 | connector->funcs->dpms(connector, dpms_mode); |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 657 | drm_object_property_set_value(&connector->base, |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 658 | dev->mode_config.dpms_property, dpms_mode); |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 659 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 660 | } |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 661 | drm_modeset_unlock_all(dev); |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) |
| 665 | { |
| 666 | struct drm_fb_helper *fb_helper = info->par; |
| 667 | |
| 668 | /* |
| 669 | * For each CRTC in this fb, turn the connectors on/off. |
| 670 | */ |
| 671 | mutex_lock(&fb_helper->lock); |
| 672 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 673 | mutex_unlock(&fb_helper->lock); |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | if (drm_drv_uses_atomic_modeset(fb_helper->dev)) |
| 678 | restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON); |
| 679 | else |
| 680 | dpms_legacy(fb_helper, dpms_mode); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 681 | mutex_unlock(&fb_helper->lock); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 682 | } |
| 683 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 684 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 685 | * drm_fb_helper_blank - implementation for &fb_ops.fb_blank |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 686 | * @blank: desired blanking state |
| 687 | * @info: fbdev registered by the helper |
| 688 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 689 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
| 690 | { |
Daniel Vetter | c50bfd0 | 2015-07-28 13:18:40 +0200 | [diff] [blame] | 691 | if (oops_in_progress) |
| 692 | return -EBUSY; |
| 693 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 694 | switch (blank) { |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 695 | /* Display: On; HSync: On, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 696 | case FB_BLANK_UNBLANK: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 697 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 698 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 699 | /* Display: Off; HSync: On, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 700 | case FB_BLANK_NORMAL: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 701 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 702 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 703 | /* Display: Off; HSync: Off, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 704 | case FB_BLANK_HSYNC_SUSPEND: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 705 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 706 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 707 | /* Display: Off; HSync: On, VSync: Off */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 708 | case FB_BLANK_VSYNC_SUSPEND: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 709 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 710 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 711 | /* Display: Off; HSync: Off, VSync: Off */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 712 | case FB_BLANK_POWERDOWN: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 713 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 714 | break; |
| 715 | } |
| 716 | return 0; |
| 717 | } |
| 718 | EXPORT_SYMBOL(drm_fb_helper_blank); |
| 719 | |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 720 | static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper, |
| 721 | struct drm_mode_set *modeset) |
| 722 | { |
| 723 | int i; |
| 724 | |
| 725 | for (i = 0; i < modeset->num_connectors; i++) { |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 726 | drm_connector_put(modeset->connectors[i]); |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 727 | modeset->connectors[i] = NULL; |
| 728 | } |
| 729 | modeset->num_connectors = 0; |
| 730 | |
| 731 | drm_mode_destroy(helper->dev, modeset->mode); |
| 732 | modeset->mode = NULL; |
| 733 | |
| 734 | /* FIXME should hold a ref? */ |
| 735 | modeset->fb = NULL; |
| 736 | } |
| 737 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 738 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) |
| 739 | { |
| 740 | int i; |
| 741 | |
Dave Airlie | 6e86d58 | 2016-04-27 11:24:51 +1000 | [diff] [blame] | 742 | for (i = 0; i < helper->connector_count; i++) { |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 743 | drm_connector_put(helper->connector_info[i]->connector); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 744 | kfree(helper->connector_info[i]); |
Dave Airlie | 6e86d58 | 2016-04-27 11:24:51 +1000 | [diff] [blame] | 745 | } |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 746 | kfree(helper->connector_info); |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 747 | |
Sascha Hauer | a1b7736 | 2012-02-01 11:38:22 +0100 | [diff] [blame] | 748 | for (i = 0; i < helper->crtc_count; i++) { |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 749 | struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set; |
| 750 | |
| 751 | drm_fb_helper_modeset_release(helper, modeset); |
| 752 | kfree(modeset->connectors); |
Sascha Hauer | a1b7736 | 2012-02-01 11:38:22 +0100 | [diff] [blame] | 753 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 754 | kfree(helper->crtc_info); |
| 755 | } |
| 756 | |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 757 | static void drm_fb_helper_resume_worker(struct work_struct *work) |
| 758 | { |
| 759 | struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, |
| 760 | resume_work); |
| 761 | |
| 762 | console_lock(); |
| 763 | fb_set_suspend(helper->fbdev, 0); |
| 764 | console_unlock(); |
| 765 | } |
| 766 | |
Noralf Trønnes | d536540 | 2018-07-03 18:03:48 +0200 | [diff] [blame] | 767 | static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper, |
| 768 | struct drm_clip_rect *clip) |
| 769 | { |
| 770 | struct drm_framebuffer *fb = fb_helper->fb; |
| 771 | unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0); |
| 772 | size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp; |
| 773 | void *src = fb_helper->fbdev->screen_buffer + offset; |
| 774 | void *dst = fb_helper->buffer->vaddr + offset; |
| 775 | size_t len = (clip->x2 - clip->x1) * cpp; |
| 776 | unsigned int y; |
| 777 | |
| 778 | for (y = clip->y1; y < clip->y2; y++) { |
| 779 | memcpy(dst, src, len); |
| 780 | src += fb->pitches[0]; |
| 781 | dst += fb->pitches[0]; |
| 782 | } |
| 783 | } |
| 784 | |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 785 | static void drm_fb_helper_dirty_work(struct work_struct *work) |
| 786 | { |
| 787 | struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, |
| 788 | dirty_work); |
| 789 | struct drm_clip_rect *clip = &helper->dirty_clip; |
| 790 | struct drm_clip_rect clip_copy; |
| 791 | unsigned long flags; |
| 792 | |
| 793 | spin_lock_irqsave(&helper->dirty_lock, flags); |
| 794 | clip_copy = *clip; |
| 795 | clip->x1 = clip->y1 = ~0; |
| 796 | clip->x2 = clip->y2 = 0; |
| 797 | spin_unlock_irqrestore(&helper->dirty_lock, flags); |
| 798 | |
Takashi Iwai | 87d3b65 | 2016-10-20 17:05:30 +0200 | [diff] [blame] | 799 | /* call dirty callback only when it has been really touched */ |
Noralf Trønnes | d536540 | 2018-07-03 18:03:48 +0200 | [diff] [blame] | 800 | if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) { |
| 801 | /* Generic fbdev uses a shadow buffer */ |
| 802 | if (helper->buffer) |
| 803 | drm_fb_helper_dirty_blit_real(helper, &clip_copy); |
Takashi Iwai | 87d3b65 | 2016-10-20 17:05:30 +0200 | [diff] [blame] | 804 | helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1); |
Noralf Trønnes | d536540 | 2018-07-03 18:03:48 +0200 | [diff] [blame] | 805 | } |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 806 | } |
| 807 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 808 | /** |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 809 | * drm_fb_helper_prepare - setup a drm_fb_helper structure |
| 810 | * @dev: DRM device |
| 811 | * @helper: driver-allocated fbdev helper structure to set up |
| 812 | * @funcs: pointer to structure of functions associate with this helper |
| 813 | * |
| 814 | * Sets up the bare minimum to make the framebuffer helper usable. This is |
| 815 | * useful to implement race-free initialization of the polling helpers. |
| 816 | */ |
| 817 | void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, |
| 818 | const struct drm_fb_helper_funcs *funcs) |
| 819 | { |
| 820 | INIT_LIST_HEAD(&helper->kernel_fb_list); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 821 | spin_lock_init(&helper->dirty_lock); |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 822 | INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 823 | INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); |
| 824 | helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 825 | mutex_init(&helper->lock); |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 826 | helper->funcs = funcs; |
| 827 | helper->dev = dev; |
| 828 | } |
| 829 | EXPORT_SYMBOL(drm_fb_helper_prepare); |
| 830 | |
| 831 | /** |
Daniel Vetter | ed84e25 | 2017-02-07 15:10:49 +0100 | [diff] [blame] | 832 | * drm_fb_helper_init - initialize a &struct drm_fb_helper |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 833 | * @dev: drm device |
| 834 | * @fb_helper: driver-allocated fbdev helper structure to initialize |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 835 | * @max_conn_count: max connector count |
| 836 | * |
| 837 | * This allocates the structures for the fbdev helper with the given limits. |
| 838 | * Note that this won't yet touch the hardware (through the driver interfaces) |
| 839 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() |
| 840 | * to allow driver writes more control over the exact init sequence. |
| 841 | * |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 842 | * Drivers must call drm_fb_helper_prepare() before calling this function. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 843 | * |
| 844 | * RETURNS: |
| 845 | * Zero if everything went ok, nonzero otherwise. |
| 846 | */ |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 847 | int drm_fb_helper_init(struct drm_device *dev, |
| 848 | struct drm_fb_helper *fb_helper, |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 849 | int max_conn_count) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 850 | { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 851 | struct drm_crtc *crtc; |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 852 | struct drm_mode_config *config = &dev->mode_config; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 853 | int i; |
| 854 | |
Noralf Trønnes | 29ad20b22 | 2017-10-30 16:39:38 +0100 | [diff] [blame] | 855 | if (!drm_fbdev_emulation) { |
| 856 | dev->fb_helper = fb_helper; |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 857 | return 0; |
Noralf Trønnes | 29ad20b22 | 2017-10-30 16:39:38 +0100 | [diff] [blame] | 858 | } |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 859 | |
Xiubo Li | 04cfe97 | 2014-03-10 09:33:58 +0800 | [diff] [blame] | 860 | if (!max_conn_count) |
| 861 | return -EINVAL; |
| 862 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 863 | 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] | 864 | if (!fb_helper->crtc_info) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 865 | return -ENOMEM; |
| 866 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 867 | fb_helper->crtc_count = config->num_crtc; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 868 | fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); |
| 869 | if (!fb_helper->connector_info) { |
| 870 | kfree(fb_helper->crtc_info); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 871 | return -ENOMEM; |
| 872 | } |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 873 | fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 874 | fb_helper->connector_count = 0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 875 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 876 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 877 | fb_helper->crtc_info[i].mode_set.connectors = |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 878 | kcalloc(max_conn_count, |
| 879 | sizeof(struct drm_connector *), |
| 880 | GFP_KERNEL); |
| 881 | |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 882 | if (!fb_helper->crtc_info[i].mode_set.connectors) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 883 | goto out_free; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 884 | fb_helper->crtc_info[i].mode_set.num_connectors = 0; |
Hans de Goede | 8f0cb41 | 2017-11-25 20:35:50 +0100 | [diff] [blame] | 885 | fb_helper->crtc_info[i].rotation = DRM_MODE_ROTATE_0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | i = 0; |
Daniel Vetter | 6295d60 | 2015-07-09 23:44:25 +0200 | [diff] [blame] | 889 | drm_for_each_crtc(crtc, dev) { |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 890 | fb_helper->crtc_info[i].mode_set.crtc = crtc; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 891 | i++; |
| 892 | } |
Sascha Hauer | e9ad318 | 2012-02-01 11:38:25 +0100 | [diff] [blame] | 893 | |
Noralf Trønnes | 29ad20b22 | 2017-10-30 16:39:38 +0100 | [diff] [blame] | 894 | dev->fb_helper = fb_helper; |
| 895 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 896 | return 0; |
| 897 | out_free: |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 898 | drm_fb_helper_crtc_free(fb_helper); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 899 | return -ENOMEM; |
| 900 | } |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 901 | EXPORT_SYMBOL(drm_fb_helper_init); |
| 902 | |
Archit Taneja | b8017d6 | 2015-07-22 14:57:56 +0530 | [diff] [blame] | 903 | /** |
| 904 | * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members |
| 905 | * @fb_helper: driver-allocated fbdev helper |
| 906 | * |
| 907 | * 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] | 908 | * by the driver within the fb_probe fb_helper callback function. Drivers do not |
| 909 | * need to release the allocated fb_info structure themselves, this is |
| 910 | * automatically done when calling drm_fb_helper_fini(). |
Archit Taneja | b8017d6 | 2015-07-22 14:57:56 +0530 | [diff] [blame] | 911 | * |
| 912 | * RETURNS: |
| 913 | * fb_info pointer if things went okay, pointer containing error code |
| 914 | * otherwise |
| 915 | */ |
| 916 | struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) |
| 917 | { |
| 918 | struct device *dev = fb_helper->dev->dev; |
| 919 | struct fb_info *info; |
| 920 | int ret; |
| 921 | |
| 922 | info = framebuffer_alloc(0, dev); |
| 923 | if (!info) |
| 924 | return ERR_PTR(-ENOMEM); |
| 925 | |
| 926 | ret = fb_alloc_cmap(&info->cmap, 256, 0); |
| 927 | if (ret) |
| 928 | goto err_release; |
| 929 | |
| 930 | info->apertures = alloc_apertures(1); |
| 931 | if (!info->apertures) { |
| 932 | ret = -ENOMEM; |
| 933 | goto err_free_cmap; |
| 934 | } |
| 935 | |
| 936 | fb_helper->fbdev = info; |
| 937 | |
| 938 | return info; |
| 939 | |
| 940 | err_free_cmap: |
| 941 | fb_dealloc_cmap(&info->cmap); |
| 942 | err_release: |
| 943 | framebuffer_release(info); |
| 944 | return ERR_PTR(ret); |
| 945 | } |
| 946 | EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); |
| 947 | |
| 948 | /** |
| 949 | * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 950 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
Archit Taneja | b8017d6 | 2015-07-22 14:57:56 +0530 | [diff] [blame] | 951 | * |
| 952 | * A wrapper around unregister_framebuffer, to release the fb_info |
Daniel Vetter | ed84e25 | 2017-02-07 15:10:49 +0100 | [diff] [blame] | 953 | * framebuffer device. This must be called before releasing all resources for |
| 954 | * @fb_helper by calling drm_fb_helper_fini(). |
Archit Taneja | b8017d6 | 2015-07-22 14:57:56 +0530 | [diff] [blame] | 955 | */ |
| 956 | void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) |
| 957 | { |
| 958 | if (fb_helper && fb_helper->fbdev) |
| 959 | unregister_framebuffer(fb_helper->fbdev); |
| 960 | } |
| 961 | EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); |
| 962 | |
| 963 | /** |
Daniel Vetter | ed84e25 | 2017-02-07 15:10:49 +0100 | [diff] [blame] | 964 | * drm_fb_helper_fini - finialize a &struct drm_fb_helper |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 965 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
Daniel Vetter | ed84e25 | 2017-02-07 15:10:49 +0100 | [diff] [blame] | 966 | * |
| 967 | * This cleans up all remaining resources associated with @fb_helper. Must be |
| 968 | * called after drm_fb_helper_unlink_fbi() was called. |
| 969 | */ |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 970 | void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) |
| 971 | { |
Daniel Vetter | da7bdda | 2017-02-07 17:16:03 +0100 | [diff] [blame] | 972 | struct fb_info *info; |
| 973 | |
Noralf Trønnes | 29ad20b22 | 2017-10-30 16:39:38 +0100 | [diff] [blame] | 974 | if (!fb_helper) |
| 975 | return; |
| 976 | |
| 977 | fb_helper->dev->fb_helper = NULL; |
| 978 | |
| 979 | if (!drm_fbdev_emulation) |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 980 | return; |
| 981 | |
Noralf Trønnes | b52f09c | 2017-08-28 19:17:43 +0200 | [diff] [blame] | 982 | cancel_work_sync(&fb_helper->resume_work); |
| 983 | cancel_work_sync(&fb_helper->dirty_work); |
| 984 | |
Daniel Vetter | da7bdda | 2017-02-07 17:16:03 +0100 | [diff] [blame] | 985 | info = fb_helper->fbdev; |
| 986 | if (info) { |
| 987 | if (info->cmap.len) |
| 988 | fb_dealloc_cmap(&info->cmap); |
| 989 | framebuffer_release(info); |
| 990 | } |
| 991 | fb_helper->fbdev = NULL; |
| 992 | |
Chris Wilson | a53ca63 | 2016-11-29 12:02:17 +0000 | [diff] [blame] | 993 | mutex_lock(&kernel_fb_helper_lock); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 994 | if (!list_empty(&fb_helper->kernel_fb_list)) { |
| 995 | list_del(&fb_helper->kernel_fb_list); |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 996 | if (list_empty(&kernel_fb_helper_list)) |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 997 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 998 | } |
Chris Wilson | a53ca63 | 2016-11-29 12:02:17 +0000 | [diff] [blame] | 999 | mutex_unlock(&kernel_fb_helper_lock); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1000 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1001 | mutex_destroy(&fb_helper->lock); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1002 | drm_fb_helper_crtc_free(fb_helper); |
| 1003 | |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1004 | } |
| 1005 | EXPORT_SYMBOL(drm_fb_helper_fini); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1006 | |
Archit Taneja | 47074ab | 2015-07-22 14:57:57 +0530 | [diff] [blame] | 1007 | /** |
| 1008 | * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 1009 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
Archit Taneja | 47074ab | 2015-07-22 14:57:57 +0530 | [diff] [blame] | 1010 | * |
| 1011 | * A wrapper around unlink_framebuffer implemented by fbdev core |
| 1012 | */ |
| 1013 | void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) |
| 1014 | { |
| 1015 | if (fb_helper && fb_helper->fbdev) |
| 1016 | unlink_framebuffer(fb_helper->fbdev); |
| 1017 | } |
| 1018 | EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); |
| 1019 | |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1020 | static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, |
| 1021 | u32 width, u32 height) |
| 1022 | { |
| 1023 | struct drm_fb_helper *helper = info->par; |
| 1024 | struct drm_clip_rect *clip = &helper->dirty_clip; |
| 1025 | unsigned long flags; |
| 1026 | |
| 1027 | if (!helper->fb->funcs->dirty) |
| 1028 | return; |
| 1029 | |
| 1030 | spin_lock_irqsave(&helper->dirty_lock, flags); |
| 1031 | clip->x1 = min_t(u32, clip->x1, x); |
| 1032 | clip->y1 = min_t(u32, clip->y1, y); |
| 1033 | clip->x2 = max_t(u32, clip->x2, x + width); |
| 1034 | clip->y2 = max_t(u32, clip->y2, y + height); |
| 1035 | spin_unlock_irqrestore(&helper->dirty_lock, flags); |
| 1036 | |
| 1037 | schedule_work(&helper->dirty_work); |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * drm_fb_helper_deferred_io() - fbdev deferred_io callback function |
| 1042 | * @info: fb_info struct pointer |
| 1043 | * @pagelist: list of dirty mmap framebuffer pages |
| 1044 | * |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1045 | * This function is used as the &fb_deferred_io.deferred_io |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1046 | * callback function for flushing the fbdev mmap writes. |
| 1047 | */ |
| 1048 | void drm_fb_helper_deferred_io(struct fb_info *info, |
| 1049 | struct list_head *pagelist) |
| 1050 | { |
| 1051 | unsigned long start, end, min, max; |
| 1052 | struct page *page; |
| 1053 | u32 y1, y2; |
| 1054 | |
| 1055 | min = ULONG_MAX; |
| 1056 | max = 0; |
| 1057 | list_for_each_entry(page, pagelist, lru) { |
| 1058 | start = page->index << PAGE_SHIFT; |
| 1059 | end = start + PAGE_SIZE - 1; |
| 1060 | min = min(min, start); |
| 1061 | max = max(max, end); |
| 1062 | } |
| 1063 | |
| 1064 | if (min < max) { |
| 1065 | y1 = min / info->fix.line_length; |
| 1066 | y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), |
| 1067 | info->var.yres); |
| 1068 | drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); |
| 1069 | } |
| 1070 | } |
| 1071 | EXPORT_SYMBOL(drm_fb_helper_deferred_io); |
| 1072 | |
Archit Taneja | cbb1a82 | 2015-07-31 16:21:41 +0530 | [diff] [blame] | 1073 | /** |
Noralf Trønnes | 48c9571 | 2017-12-15 18:51:17 +0100 | [diff] [blame] | 1074 | * drm_fb_helper_defio_init - fbdev deferred I/O initialization |
| 1075 | * @fb_helper: driver-allocated fbdev helper |
| 1076 | * |
| 1077 | * This function allocates &fb_deferred_io, sets callback to |
| 1078 | * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init(). |
| 1079 | * It should be called from the &drm_fb_helper_funcs->fb_probe callback. |
| 1080 | * drm_fb_helper_fbdev_teardown() cleans up deferred I/O. |
| 1081 | * |
| 1082 | * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done |
| 1083 | * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby |
| 1084 | * affect other instances of that &fb_ops. |
| 1085 | * |
| 1086 | * Returns: |
| 1087 | * 0 on success or a negative error code on failure. |
| 1088 | */ |
| 1089 | int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper) |
| 1090 | { |
| 1091 | struct fb_info *info = fb_helper->fbdev; |
| 1092 | struct fb_deferred_io *fbdefio; |
| 1093 | struct fb_ops *fbops; |
| 1094 | |
| 1095 | fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL); |
| 1096 | fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); |
| 1097 | if (!fbdefio || !fbops) { |
| 1098 | kfree(fbdefio); |
| 1099 | kfree(fbops); |
| 1100 | return -ENOMEM; |
| 1101 | } |
| 1102 | |
| 1103 | info->fbdefio = fbdefio; |
| 1104 | fbdefio->delay = msecs_to_jiffies(50); |
| 1105 | fbdefio->deferred_io = drm_fb_helper_deferred_io; |
| 1106 | |
| 1107 | *fbops = *info->fbops; |
| 1108 | info->fbops = fbops; |
| 1109 | |
| 1110 | fb_deferred_io_init(info); |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | EXPORT_SYMBOL(drm_fb_helper_defio_init); |
| 1115 | |
| 1116 | /** |
Archit Taneja | cbb1a82 | 2015-07-31 16:21:41 +0530 | [diff] [blame] | 1117 | * drm_fb_helper_sys_read - wrapper around fb_sys_read |
| 1118 | * @info: fb_info struct pointer |
| 1119 | * @buf: userspace buffer to read from framebuffer memory |
| 1120 | * @count: number of bytes to read from framebuffer memory |
| 1121 | * @ppos: read offset within framebuffer memory |
| 1122 | * |
| 1123 | * A wrapper around fb_sys_read implemented by fbdev core |
| 1124 | */ |
| 1125 | ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, |
| 1126 | size_t count, loff_t *ppos) |
| 1127 | { |
| 1128 | return fb_sys_read(info, buf, count, ppos); |
| 1129 | } |
| 1130 | EXPORT_SYMBOL(drm_fb_helper_sys_read); |
| 1131 | |
| 1132 | /** |
| 1133 | * drm_fb_helper_sys_write - wrapper around fb_sys_write |
| 1134 | * @info: fb_info struct pointer |
| 1135 | * @buf: userspace buffer to write to framebuffer memory |
| 1136 | * @count: number of bytes to write to framebuffer memory |
| 1137 | * @ppos: write offset within framebuffer memory |
| 1138 | * |
| 1139 | * A wrapper around fb_sys_write implemented by fbdev core |
| 1140 | */ |
| 1141 | ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, |
| 1142 | size_t count, loff_t *ppos) |
| 1143 | { |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1144 | ssize_t ret; |
| 1145 | |
| 1146 | ret = fb_sys_write(info, buf, count, ppos); |
| 1147 | if (ret > 0) |
| 1148 | drm_fb_helper_dirty(info, 0, 0, info->var.xres, |
| 1149 | info->var.yres); |
| 1150 | |
| 1151 | return ret; |
Archit Taneja | cbb1a82 | 2015-07-31 16:21:41 +0530 | [diff] [blame] | 1152 | } |
| 1153 | EXPORT_SYMBOL(drm_fb_helper_sys_write); |
| 1154 | |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1155 | /** |
| 1156 | * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect |
| 1157 | * @info: fbdev registered by the helper |
| 1158 | * @rect: info about rectangle to fill |
| 1159 | * |
| 1160 | * A wrapper around sys_fillrect implemented by fbdev core |
| 1161 | */ |
| 1162 | void drm_fb_helper_sys_fillrect(struct fb_info *info, |
| 1163 | const struct fb_fillrect *rect) |
| 1164 | { |
| 1165 | sys_fillrect(info, rect); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1166 | drm_fb_helper_dirty(info, rect->dx, rect->dy, |
| 1167 | rect->width, rect->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1168 | } |
| 1169 | EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); |
| 1170 | |
| 1171 | /** |
| 1172 | * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea |
| 1173 | * @info: fbdev registered by the helper |
| 1174 | * @area: info about area to copy |
| 1175 | * |
| 1176 | * A wrapper around sys_copyarea implemented by fbdev core |
| 1177 | */ |
| 1178 | void drm_fb_helper_sys_copyarea(struct fb_info *info, |
| 1179 | const struct fb_copyarea *area) |
| 1180 | { |
| 1181 | sys_copyarea(info, area); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1182 | drm_fb_helper_dirty(info, area->dx, area->dy, |
| 1183 | area->width, area->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1184 | } |
| 1185 | EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); |
| 1186 | |
| 1187 | /** |
| 1188 | * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit |
| 1189 | * @info: fbdev registered by the helper |
| 1190 | * @image: info about image to blit |
| 1191 | * |
| 1192 | * A wrapper around sys_imageblit implemented by fbdev core |
| 1193 | */ |
| 1194 | void drm_fb_helper_sys_imageblit(struct fb_info *info, |
| 1195 | const struct fb_image *image) |
| 1196 | { |
| 1197 | sys_imageblit(info, image); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1198 | drm_fb_helper_dirty(info, image->dx, image->dy, |
| 1199 | image->width, image->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1200 | } |
| 1201 | EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); |
| 1202 | |
| 1203 | /** |
| 1204 | * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect |
| 1205 | * @info: fbdev registered by the helper |
| 1206 | * @rect: info about rectangle to fill |
| 1207 | * |
Daniel Vetter | d104328 | 2018-05-24 11:01:05 +0200 | [diff] [blame] | 1208 | * A wrapper around cfb_fillrect implemented by fbdev core |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1209 | */ |
| 1210 | void drm_fb_helper_cfb_fillrect(struct fb_info *info, |
| 1211 | const struct fb_fillrect *rect) |
| 1212 | { |
| 1213 | cfb_fillrect(info, rect); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1214 | drm_fb_helper_dirty(info, rect->dx, rect->dy, |
| 1215 | rect->width, rect->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1216 | } |
| 1217 | EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); |
| 1218 | |
| 1219 | /** |
| 1220 | * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea |
| 1221 | * @info: fbdev registered by the helper |
| 1222 | * @area: info about area to copy |
| 1223 | * |
| 1224 | * A wrapper around cfb_copyarea implemented by fbdev core |
| 1225 | */ |
| 1226 | void drm_fb_helper_cfb_copyarea(struct fb_info *info, |
| 1227 | const struct fb_copyarea *area) |
| 1228 | { |
| 1229 | cfb_copyarea(info, area); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1230 | drm_fb_helper_dirty(info, area->dx, area->dy, |
| 1231 | area->width, area->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1232 | } |
| 1233 | EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); |
| 1234 | |
| 1235 | /** |
| 1236 | * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit |
| 1237 | * @info: fbdev registered by the helper |
| 1238 | * @image: info about image to blit |
| 1239 | * |
| 1240 | * A wrapper around cfb_imageblit implemented by fbdev core |
| 1241 | */ |
| 1242 | void drm_fb_helper_cfb_imageblit(struct fb_info *info, |
| 1243 | const struct fb_image *image) |
| 1244 | { |
| 1245 | cfb_imageblit(info, image); |
Noralf Trønnes | eaa434d | 2016-04-28 17:18:33 +0200 | [diff] [blame] | 1246 | drm_fb_helper_dirty(info, image->dx, image->dy, |
| 1247 | image->width, image->height); |
Archit Taneja | 742547b | 2015-07-31 16:21:42 +0530 | [diff] [blame] | 1248 | } |
| 1249 | EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); |
| 1250 | |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1251 | /** |
| 1252 | * drm_fb_helper_set_suspend - wrapper around fb_set_suspend |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 1253 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1254 | * @suspend: whether to suspend or resume |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1255 | * |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1256 | * A wrapper around fb_set_suspend implemented by fbdev core. |
| 1257 | * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take |
| 1258 | * the lock yourself |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1259 | */ |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1260 | 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] | 1261 | { |
| 1262 | if (fb_helper && fb_helper->fbdev) |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1263 | fb_set_suspend(fb_helper->fbdev, suspend); |
Archit Taneja | fdefa58 | 2015-07-31 16:21:43 +0530 | [diff] [blame] | 1264 | } |
| 1265 | EXPORT_SYMBOL(drm_fb_helper_set_suspend); |
| 1266 | |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1267 | /** |
| 1268 | * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also |
| 1269 | * takes the console lock |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 1270 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
Daniel Vetter | 28579f3 | 2016-08-23 17:27:27 +0200 | [diff] [blame] | 1271 | * @suspend: whether to suspend or resume |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1272 | * |
| 1273 | * A wrapper around fb_set_suspend() that takes the console lock. If the lock |
| 1274 | * isn't available on resume, a worker is tasked with waiting for the lock |
| 1275 | * to become available. The console lock can be pretty contented on resume |
| 1276 | * due to all the printk activity. |
| 1277 | * |
| 1278 | * This function can be called multiple times with the same state since |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1279 | * &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] | 1280 | * |
| 1281 | * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. |
| 1282 | */ |
| 1283 | 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] | 1284 | bool suspend) |
Noralf Trønnes | cfe6342 | 2016-08-23 13:54:06 +0200 | [diff] [blame] | 1285 | { |
| 1286 | if (!fb_helper || !fb_helper->fbdev) |
| 1287 | return; |
| 1288 | |
| 1289 | /* make sure there's no pending/ongoing resume */ |
| 1290 | flush_work(&fb_helper->resume_work); |
| 1291 | |
| 1292 | if (suspend) { |
| 1293 | if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) |
| 1294 | return; |
| 1295 | |
| 1296 | console_lock(); |
| 1297 | |
| 1298 | } else { |
| 1299 | if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) |
| 1300 | return; |
| 1301 | |
| 1302 | if (!console_trylock()) { |
| 1303 | schedule_work(&fb_helper->resume_work); |
| 1304 | return; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | fb_set_suspend(fb_helper->fbdev, suspend); |
| 1309 | console_unlock(); |
| 1310 | } |
| 1311 | EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); |
| 1312 | |
Peter Rosin | b8e2b01 | 2017-07-04 12:36:57 +0200 | [diff] [blame] | 1313 | static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info) |
| 1314 | { |
| 1315 | u32 *palette = (u32 *)info->pseudo_palette; |
| 1316 | int i; |
| 1317 | |
| 1318 | if (cmap->start + cmap->len > 16) |
| 1319 | return -EINVAL; |
| 1320 | |
| 1321 | for (i = 0; i < cmap->len; ++i) { |
| 1322 | u16 red = cmap->red[i]; |
| 1323 | u16 green = cmap->green[i]; |
| 1324 | u16 blue = cmap->blue[i]; |
| 1325 | u32 value; |
| 1326 | |
| 1327 | red >>= 16 - info->var.red.length; |
| 1328 | green >>= 16 - info->var.green.length; |
| 1329 | blue >>= 16 - info->var.blue.length; |
| 1330 | value = (red << info->var.red.offset) | |
| 1331 | (green << info->var.green.offset) | |
| 1332 | (blue << info->var.blue.offset); |
| 1333 | if (info->var.transp.length > 0) { |
| 1334 | u32 mask = (1 << info->var.transp.length) - 1; |
| 1335 | |
| 1336 | mask <<= info->var.transp.offset; |
| 1337 | value |= mask; |
| 1338 | } |
| 1339 | palette[cmap->start + i] = value; |
| 1340 | } |
| 1341 | |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1345 | static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info) |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1346 | { |
| 1347 | struct drm_fb_helper *fb_helper = info->par; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1348 | struct drm_crtc *crtc; |
Peter Rosin | a3562a0 | 2017-07-04 12:36:58 +0200 | [diff] [blame] | 1349 | u16 *r, *g, *b; |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1350 | int i, ret = 0; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1351 | |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1352 | drm_modeset_lock_all(fb_helper->dev); |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1353 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1354 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1355 | if (!crtc->funcs->gamma_set || !crtc->gamma_size) |
| 1356 | return -EINVAL; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1357 | |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1358 | if (cmap->start + cmap->len > crtc->gamma_size) |
| 1359 | return -EINVAL; |
Peter Rosin | a3562a0 | 2017-07-04 12:36:58 +0200 | [diff] [blame] | 1360 | |
| 1361 | r = crtc->gamma_store; |
| 1362 | g = r + crtc->gamma_size; |
| 1363 | b = g + crtc->gamma_size; |
| 1364 | |
| 1365 | memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); |
| 1366 | memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); |
| 1367 | memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); |
| 1368 | |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1369 | ret = crtc->funcs->gamma_set(crtc, r, g, b, |
| 1370 | crtc->gamma_size, NULL); |
| 1371 | if (ret) |
| 1372 | return ret; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1373 | } |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1374 | drm_modeset_unlock_all(fb_helper->dev); |
| 1375 | |
| 1376 | return ret; |
| 1377 | } |
| 1378 | |
| 1379 | static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc, |
| 1380 | struct fb_cmap *cmap) |
| 1381 | { |
| 1382 | struct drm_device *dev = crtc->dev; |
| 1383 | struct drm_property_blob *gamma_lut; |
| 1384 | struct drm_color_lut *lut; |
| 1385 | int size = crtc->gamma_size; |
| 1386 | int i; |
| 1387 | |
| 1388 | if (!size || cmap->start + cmap->len > size) |
| 1389 | return ERR_PTR(-EINVAL); |
| 1390 | |
| 1391 | gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL); |
| 1392 | if (IS_ERR(gamma_lut)) |
| 1393 | return gamma_lut; |
| 1394 | |
Ville Syrjälä | 11b83e3 | 2018-02-23 21:25:02 +0200 | [diff] [blame] | 1395 | lut = gamma_lut->data; |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1396 | if (cmap->start || cmap->len != size) { |
| 1397 | u16 *r = crtc->gamma_store; |
| 1398 | u16 *g = r + crtc->gamma_size; |
| 1399 | u16 *b = g + crtc->gamma_size; |
| 1400 | |
| 1401 | for (i = 0; i < cmap->start; i++) { |
| 1402 | lut[i].red = r[i]; |
| 1403 | lut[i].green = g[i]; |
| 1404 | lut[i].blue = b[i]; |
| 1405 | } |
| 1406 | for (i = cmap->start + cmap->len; i < size; i++) { |
| 1407 | lut[i].red = r[i]; |
| 1408 | lut[i].green = g[i]; |
| 1409 | lut[i].blue = b[i]; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | for (i = 0; i < cmap->len; i++) { |
| 1414 | lut[cmap->start + i].red = cmap->red[i]; |
| 1415 | lut[cmap->start + i].green = cmap->green[i]; |
| 1416 | lut[cmap->start + i].blue = cmap->blue[i]; |
| 1417 | } |
| 1418 | |
| 1419 | return gamma_lut; |
| 1420 | } |
| 1421 | |
| 1422 | static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info) |
| 1423 | { |
| 1424 | struct drm_fb_helper *fb_helper = info->par; |
| 1425 | struct drm_device *dev = fb_helper->dev; |
| 1426 | struct drm_property_blob *gamma_lut = NULL; |
| 1427 | struct drm_modeset_acquire_ctx ctx; |
| 1428 | struct drm_crtc_state *crtc_state; |
| 1429 | struct drm_atomic_state *state; |
| 1430 | struct drm_crtc *crtc; |
| 1431 | u16 *r, *g, *b; |
| 1432 | int i, ret = 0; |
| 1433 | bool replaced; |
| 1434 | |
| 1435 | drm_modeset_acquire_init(&ctx, 0); |
| 1436 | |
| 1437 | state = drm_atomic_state_alloc(dev); |
| 1438 | if (!state) { |
| 1439 | ret = -ENOMEM; |
| 1440 | goto out_ctx; |
| 1441 | } |
| 1442 | |
| 1443 | state->acquire_ctx = &ctx; |
| 1444 | retry: |
| 1445 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1446 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
| 1447 | |
| 1448 | if (!gamma_lut) |
| 1449 | gamma_lut = setcmap_new_gamma_lut(crtc, cmap); |
| 1450 | if (IS_ERR(gamma_lut)) { |
| 1451 | ret = PTR_ERR(gamma_lut); |
| 1452 | gamma_lut = NULL; |
| 1453 | goto out_state; |
| 1454 | } |
| 1455 | |
| 1456 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 1457 | if (IS_ERR(crtc_state)) { |
| 1458 | ret = PTR_ERR(crtc_state); |
| 1459 | goto out_state; |
| 1460 | } |
| 1461 | |
| 1462 | replaced = drm_property_replace_blob(&crtc_state->degamma_lut, |
| 1463 | NULL); |
| 1464 | replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL); |
| 1465 | replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, |
| 1466 | gamma_lut); |
| 1467 | crtc_state->color_mgmt_changed |= replaced; |
| 1468 | } |
| 1469 | |
| 1470 | ret = drm_atomic_commit(state); |
| 1471 | if (ret) |
| 1472 | goto out_state; |
| 1473 | |
| 1474 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1475 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
| 1476 | |
| 1477 | r = crtc->gamma_store; |
| 1478 | g = r + crtc->gamma_size; |
| 1479 | b = g + crtc->gamma_size; |
| 1480 | |
| 1481 | memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); |
| 1482 | memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); |
| 1483 | memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); |
| 1484 | } |
| 1485 | |
| 1486 | out_state: |
| 1487 | if (ret == -EDEADLK) |
| 1488 | goto backoff; |
| 1489 | |
| 1490 | drm_property_blob_put(gamma_lut); |
| 1491 | drm_atomic_state_put(state); |
| 1492 | out_ctx: |
| 1493 | drm_modeset_drop_locks(&ctx); |
| 1494 | drm_modeset_acquire_fini(&ctx); |
| 1495 | |
| 1496 | return ret; |
| 1497 | |
| 1498 | backoff: |
| 1499 | drm_atomic_state_clear(state); |
| 1500 | drm_modeset_backoff(&ctx); |
| 1501 | goto retry; |
| 1502 | } |
| 1503 | |
| 1504 | /** |
| 1505 | * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap |
| 1506 | * @cmap: cmap to set |
| 1507 | * @info: fbdev registered by the helper |
| 1508 | */ |
| 1509 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) |
| 1510 | { |
| 1511 | struct drm_fb_helper *fb_helper = info->par; |
| 1512 | int ret; |
| 1513 | |
| 1514 | if (oops_in_progress) |
| 1515 | return -EBUSY; |
| 1516 | |
| 1517 | mutex_lock(&fb_helper->lock); |
| 1518 | |
| 1519 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 1520 | ret = -EBUSY; |
| 1521 | goto out; |
| 1522 | } |
| 1523 | |
| 1524 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) |
| 1525 | ret = setcmap_pseudo_palette(cmap, info); |
| 1526 | else if (drm_drv_uses_atomic_modeset(fb_helper->dev)) |
| 1527 | ret = setcmap_atomic(cmap, info); |
| 1528 | else |
| 1529 | ret = setcmap_legacy(cmap, info); |
| 1530 | |
| 1531 | out: |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1532 | mutex_unlock(&fb_helper->lock); |
Peter Rosin | 964c600 | 2017-07-13 18:25:27 +0200 | [diff] [blame] | 1533 | |
| 1534 | return ret; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 1535 | } |
| 1536 | EXPORT_SYMBOL(drm_fb_helper_setcmap); |
| 1537 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1538 | /** |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1539 | * drm_fb_helper_ioctl - legacy ioctl implementation |
| 1540 | * @info: fbdev registered by the helper |
| 1541 | * @cmd: ioctl command |
| 1542 | * @arg: ioctl argument |
| 1543 | * |
| 1544 | * A helper to implement the standard fbdev ioctl. Only |
| 1545 | * FBIO_WAITFORVSYNC is implemented for now. |
| 1546 | */ |
| 1547 | int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, |
| 1548 | unsigned long arg) |
| 1549 | { |
| 1550 | struct drm_fb_helper *fb_helper = info->par; |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1551 | struct drm_mode_set *mode_set; |
| 1552 | struct drm_crtc *crtc; |
| 1553 | int ret = 0; |
| 1554 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1555 | mutex_lock(&fb_helper->lock); |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1556 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 1557 | ret = -EBUSY; |
| 1558 | goto unlock; |
| 1559 | } |
| 1560 | |
| 1561 | switch (cmd) { |
| 1562 | case FBIO_WAITFORVSYNC: |
| 1563 | /* |
| 1564 | * Only consider the first CRTC. |
| 1565 | * |
| 1566 | * This ioctl is supposed to take the CRTC number as |
| 1567 | * an argument, but in fbdev times, what that number |
| 1568 | * was supposed to be was quite unclear, different |
| 1569 | * drivers were passing that argument differently |
| 1570 | * (some by reference, some by value), and most of the |
| 1571 | * userspace applications were just hardcoding 0 as an |
| 1572 | * argument. |
| 1573 | * |
| 1574 | * The first CRTC should be the integrated panel on |
| 1575 | * most drivers, so this is the best choice we can |
| 1576 | * make. If we're not smart enough here, one should |
| 1577 | * just consider switch the userspace to KMS. |
| 1578 | */ |
| 1579 | mode_set = &fb_helper->crtc_info[0].mode_set; |
| 1580 | crtc = mode_set->crtc; |
| 1581 | |
| 1582 | /* |
| 1583 | * Only wait for a vblank event if the CRTC is |
| 1584 | * enabled, otherwise just don't do anythintg, |
| 1585 | * not even report an error. |
| 1586 | */ |
| 1587 | ret = drm_crtc_vblank_get(crtc); |
| 1588 | if (!ret) { |
| 1589 | drm_crtc_wait_one_vblank(crtc); |
| 1590 | drm_crtc_vblank_put(crtc); |
| 1591 | } |
| 1592 | |
| 1593 | ret = 0; |
| 1594 | goto unlock; |
| 1595 | default: |
| 1596 | ret = -ENOTTY; |
| 1597 | } |
| 1598 | |
| 1599 | unlock: |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1600 | mutex_unlock(&fb_helper->lock); |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1601 | return ret; |
| 1602 | } |
| 1603 | EXPORT_SYMBOL(drm_fb_helper_ioctl); |
| 1604 | |
Eugeniy Paltsev | db05c48 | 2018-10-03 19:45:38 +0300 | [diff] [blame] | 1605 | static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1, |
| 1606 | const struct fb_var_screeninfo *var_2) |
| 1607 | { |
| 1608 | return var_1->bits_per_pixel == var_2->bits_per_pixel && |
| 1609 | var_1->grayscale == var_2->grayscale && |
| 1610 | var_1->red.offset == var_2->red.offset && |
| 1611 | var_1->red.length == var_2->red.length && |
| 1612 | var_1->red.msb_right == var_2->red.msb_right && |
| 1613 | var_1->green.offset == var_2->green.offset && |
| 1614 | var_1->green.length == var_2->green.length && |
| 1615 | var_1->green.msb_right == var_2->green.msb_right && |
| 1616 | var_1->blue.offset == var_2->blue.offset && |
| 1617 | var_1->blue.length == var_2->blue.length && |
| 1618 | var_1->blue.msb_right == var_2->blue.msb_right && |
| 1619 | var_1->transp.offset == var_2->transp.offset && |
| 1620 | var_1->transp.length == var_2->transp.length && |
| 1621 | var_1->transp.msb_right == var_2->transp.msb_right; |
| 1622 | } |
| 1623 | |
Maxime Ripard | 0f3bbe0 | 2017-02-28 16:36:51 +0100 | [diff] [blame] | 1624 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1625 | * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1626 | * @var: screeninfo to check |
| 1627 | * @info: fbdev registered by the helper |
| 1628 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1629 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
| 1630 | struct fb_info *info) |
| 1631 | { |
| 1632 | struct drm_fb_helper *fb_helper = info->par; |
| 1633 | struct drm_framebuffer *fb = fb_helper->fb; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1634 | |
Jason Wessel | f90ebd9 | 2010-08-05 09:22:32 -0500 | [diff] [blame] | 1635 | if (var->pixclock != 0 || in_dbg_master()) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1636 | return -EINVAL; |
| 1637 | |
Alexandru Gheorghe | 042bf75 | 2018-11-01 17:02:05 +0000 | [diff] [blame] | 1638 | if ((drm_format_info_block_width(fb->format, 0) > 1) || |
| 1639 | (drm_format_info_block_height(fb->format, 0) > 1)) |
| 1640 | return -EINVAL; |
| 1641 | |
Stefan Agner | 865afb1 | 2016-10-11 16:15:04 -0700 | [diff] [blame] | 1642 | /* |
| 1643 | * Changes struct fb_var_screeninfo are currently not pushed back |
| 1644 | * to KMS, hence fail if different settings are requested. |
| 1645 | */ |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1646 | if (var->bits_per_pixel != fb->format->cpp[0] * 8 || |
Michel Dänzer | 12ffed9 | 2017-03-23 17:53:26 +0900 | [diff] [blame] | 1647 | var->xres > fb->width || var->yres > fb->height || |
| 1648 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { |
| 1649 | 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] | 1650 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
| 1651 | var->xres, var->yres, var->bits_per_pixel, |
| 1652 | var->xres_virtual, var->yres_virtual, |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1653 | fb->width, fb->height, fb->format->cpp[0] * 8); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1654 | return -EINVAL; |
| 1655 | } |
| 1656 | |
Eugeniy Paltsev | db05c48 | 2018-10-03 19:45:38 +0300 | [diff] [blame] | 1657 | /* |
| 1658 | * drm fbdev emulation doesn't support changing the pixel format at all, |
| 1659 | * so reject all pixel format changing requests. |
| 1660 | */ |
| 1661 | if (!drm_fb_pixel_format_equal(var, &info->var)) { |
| 1662 | DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1663 | return -EINVAL; |
| 1664 | } |
Eugeniy Paltsev | db05c48 | 2018-10-03 19:45:38 +0300 | [diff] [blame] | 1665 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1666 | return 0; |
| 1667 | } |
| 1668 | EXPORT_SYMBOL(drm_fb_helper_check_var); |
| 1669 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1670 | /** |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1671 | * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1672 | * @info: fbdev registered by the helper |
| 1673 | * |
| 1674 | * This will let fbcon do the mode init and is called at initialization time by |
| 1675 | * the fbdev core when registering the driver, and later on through the hotplug |
| 1676 | * callback. |
| 1677 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1678 | int drm_fb_helper_set_par(struct fb_info *info) |
| 1679 | { |
| 1680 | struct drm_fb_helper *fb_helper = info->par; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1681 | struct fb_var_screeninfo *var = &info->var; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1682 | |
Daniel Vetter | c50bfd0 | 2015-07-28 13:18:40 +0200 | [diff] [blame] | 1683 | if (oops_in_progress) |
| 1684 | return -EBUSY; |
| 1685 | |
Clemens Ladisch | 5349ef3 | 2009-11-04 09:42:52 +0100 | [diff] [blame] | 1686 | if (var->pixclock != 0) { |
Pavel Roskin | 172e91f | 2010-02-11 14:31:32 +1000 | [diff] [blame] | 1687 | DRM_ERROR("PIXEL CLOCK SET\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1688 | return -EINVAL; |
| 1689 | } |
| 1690 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 1691 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1692 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1693 | return 0; |
| 1694 | } |
| 1695 | EXPORT_SYMBOL(drm_fb_helper_set_par); |
| 1696 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1697 | 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] | 1698 | { |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1699 | int i; |
Daniel Vetter | 5c2e344 | 2017-07-04 17:18:26 +0200 | [diff] [blame] | 1700 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1701 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1702 | struct drm_mode_set *mode_set; |
| 1703 | |
| 1704 | mode_set = &fb_helper->crtc_info[i].mode_set; |
| 1705 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1706 | mode_set->x = x; |
| 1707 | mode_set->y = y; |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1708 | } |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1709 | } |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1710 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1711 | static int pan_display_atomic(struct fb_var_screeninfo *var, |
| 1712 | struct fb_info *info) |
| 1713 | { |
| 1714 | struct drm_fb_helper *fb_helper = info->par; |
| 1715 | int ret; |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1716 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1717 | pan_set(fb_helper, var->xoffset, var->yoffset); |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1718 | |
Daniel Vetter | 6b7dc6e | 2017-07-04 17:18:29 +0200 | [diff] [blame] | 1719 | ret = restore_fbdev_mode_atomic(fb_helper, true); |
| 1720 | if (!ret) { |
| 1721 | info->var.xoffset = var->xoffset; |
| 1722 | info->var.yoffset = var->yoffset; |
| 1723 | } else |
| 1724 | pan_set(fb_helper, info->var.xoffset, info->var.yoffset); |
Daniel Vetter | 5c2e344 | 2017-07-04 17:18:26 +0200 | [diff] [blame] | 1725 | |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1726 | return ret; |
Rob Clark | 1edf026 | 2015-08-25 15:35:59 -0400 | [diff] [blame] | 1727 | } |
| 1728 | |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1729 | static int pan_display_legacy(struct fb_var_screeninfo *var, |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1730 | struct fb_info *info) |
| 1731 | { |
| 1732 | struct drm_fb_helper *fb_helper = info->par; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1733 | struct drm_mode_set *modeset; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1734 | int ret = 0; |
| 1735 | int i; |
| 1736 | |
Daniel Vetter | 5c2e344 | 2017-07-04 17:18:26 +0200 | [diff] [blame] | 1737 | drm_modeset_lock_all(fb_helper->dev); |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1738 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1739 | modeset = &fb_helper->crtc_info[i].mode_set; |
| 1740 | |
| 1741 | modeset->x = var->xoffset; |
| 1742 | modeset->y = var->yoffset; |
| 1743 | |
| 1744 | if (modeset->num_connectors) { |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 1745 | ret = drm_mode_set_config_internal(modeset); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1746 | if (!ret) { |
| 1747 | info->var.xoffset = var->xoffset; |
| 1748 | info->var.yoffset = var->yoffset; |
| 1749 | } |
| 1750 | } |
| 1751 | } |
Daniel Vetter | 5c2e344 | 2017-07-04 17:18:26 +0200 | [diff] [blame] | 1752 | drm_modeset_unlock_all(fb_helper->dev); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1753 | |
| 1754 | return ret; |
| 1755 | } |
| 1756 | |
| 1757 | /** |
| 1758 | * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display |
| 1759 | * @var: updated screen information |
| 1760 | * @info: fbdev registered by the helper |
| 1761 | */ |
| 1762 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, |
| 1763 | struct fb_info *info) |
| 1764 | { |
| 1765 | struct drm_fb_helper *fb_helper = info->par; |
| 1766 | struct drm_device *dev = fb_helper->dev; |
| 1767 | int ret; |
| 1768 | |
| 1769 | if (oops_in_progress) |
| 1770 | return -EBUSY; |
| 1771 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1772 | mutex_lock(&fb_helper->lock); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1773 | if (!drm_fb_helper_is_bound(fb_helper)) { |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1774 | mutex_unlock(&fb_helper->lock); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1775 | return -EBUSY; |
| 1776 | } |
| 1777 | |
| 1778 | if (drm_drv_uses_atomic_modeset(dev)) |
| 1779 | ret = pan_display_atomic(var, info); |
| 1780 | else |
| 1781 | ret = pan_display_legacy(var, info); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 1782 | mutex_unlock(&fb_helper->lock); |
Daniel Vetter | 7128645 | 2017-04-03 10:33:04 +0200 | [diff] [blame] | 1783 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1784 | return ret; |
| 1785 | } |
| 1786 | EXPORT_SYMBOL(drm_fb_helper_pan_display); |
| 1787 | |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1788 | /* |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1789 | * Allocates the backing storage and sets up the fbdev info structure through |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 1790 | * the ->fb_probe callback. |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1791 | */ |
Daniel Vetter | de1ace5 | 2013-01-20 21:50:49 +0100 | [diff] [blame] | 1792 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
| 1793 | int preferred_bpp) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1794 | { |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1795 | int ret = 0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1796 | int crtc_count = 0; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1797 | int i; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1798 | struct drm_fb_helper_surface_size sizes; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1799 | int gamma_size = 0; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1800 | |
| 1801 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); |
| 1802 | sizes.surface_depth = 24; |
| 1803 | sizes.surface_bpp = 32; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1804 | sizes.fb_width = (u32)-1; |
| 1805 | sizes.fb_height = (u32)-1; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1806 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1807 | /* 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] | 1808 | if (preferred_bpp != sizes.surface_bpp) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1809 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1810 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1811 | /* 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] | 1812 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1813 | 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] | 1814 | struct drm_cmdline_mode *cmdline_mode; |
Dave Airlie | 8ef8678 | 2009-09-26 06:39:00 +1000 | [diff] [blame] | 1815 | |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1816 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1817 | |
| 1818 | if (cmdline_mode->bpp_specified) { |
| 1819 | switch (cmdline_mode->bpp) { |
| 1820 | case 8: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1821 | sizes.surface_depth = sizes.surface_bpp = 8; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1822 | break; |
| 1823 | case 15: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1824 | sizes.surface_depth = 15; |
| 1825 | sizes.surface_bpp = 16; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1826 | break; |
| 1827 | case 16: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1828 | sizes.surface_depth = sizes.surface_bpp = 16; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1829 | break; |
| 1830 | case 24: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1831 | sizes.surface_depth = sizes.surface_bpp = 24; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1832 | break; |
| 1833 | case 32: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1834 | sizes.surface_depth = 24; |
| 1835 | sizes.surface_bpp = 32; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1836 | break; |
| 1837 | } |
| 1838 | break; |
| 1839 | } |
| 1840 | } |
| 1841 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1842 | crtc_count = 0; |
| 1843 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1844 | struct drm_display_mode *desired_mode; |
Rob Clark | 0e3704c | 2015-03-11 10:23:14 -0400 | [diff] [blame] | 1845 | struct drm_mode_set *mode_set; |
| 1846 | int x, y, j; |
| 1847 | /* in case of tile group, are we the last tile vert or horiz? |
| 1848 | * If no tile group you are always the last one both vertically |
| 1849 | * and horizontally |
| 1850 | */ |
| 1851 | bool lastv = true, lasth = true; |
Rob Clark | 675c832 | 2015-03-11 10:23:13 -0400 | [diff] [blame] | 1852 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1853 | desired_mode = fb_helper->crtc_info[i].desired_mode; |
Rob Clark | 0e3704c | 2015-03-11 10:23:14 -0400 | [diff] [blame] | 1854 | mode_set = &fb_helper->crtc_info[i].mode_set; |
Rob Clark | 675c832 | 2015-03-11 10:23:13 -0400 | [diff] [blame] | 1855 | |
| 1856 | if (!desired_mode) |
| 1857 | continue; |
| 1858 | |
| 1859 | crtc_count++; |
| 1860 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1861 | x = fb_helper->crtc_info[i].x; |
| 1862 | y = fb_helper->crtc_info[i].y; |
Rob Clark | 675c832 | 2015-03-11 10:23:13 -0400 | [diff] [blame] | 1863 | |
| 1864 | if (gamma_size == 0) |
| 1865 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; |
| 1866 | |
| 1867 | sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); |
| 1868 | 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] | 1869 | |
| 1870 | for (j = 0; j < mode_set->num_connectors; j++) { |
| 1871 | struct drm_connector *connector = mode_set->connectors[j]; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1872 | |
Rob Clark | 0e3704c | 2015-03-11 10:23:14 -0400 | [diff] [blame] | 1873 | if (connector->has_tile) { |
| 1874 | lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); |
| 1875 | lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); |
| 1876 | /* cloning to multiple tiles is just crazy-talk, so: */ |
| 1877 | break; |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | if (lasth) |
| 1882 | sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); |
| 1883 | if (lastv) |
| 1884 | 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] | 1885 | } |
| 1886 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1887 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 1888 | DRM_INFO("Cannot find any crtc or sizes\n"); |
Maarten Lankhorst | 52dd065 | 2017-11-28 12:16:03 +0100 | [diff] [blame] | 1889 | |
| 1890 | /* First time: disable all crtc's.. */ |
| 1891 | if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master)) |
| 1892 | restore_fbdev_mode(fb_helper); |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 1893 | return -EAGAIN; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1894 | } |
| 1895 | |
Xinliang Liu | 5f15257 | 2017-02-15 17:19:08 +0100 | [diff] [blame] | 1896 | /* Handle our overallocation */ |
| 1897 | sizes.surface_height *= drm_fbdev_overalloc; |
| 1898 | sizes.surface_height /= 100; |
| 1899 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1900 | /* push down into drivers */ |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1901 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
| 1902 | if (ret < 0) |
| 1903 | return ret; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1904 | |
Maarten Lankhorst | 8d44e9e | 2017-12-20 10:35:44 +0100 | [diff] [blame] | 1905 | strcpy(fb_helper->fb->comm, "[fbcon]"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1906 | return 0; |
| 1907 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1908 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1909 | /** |
| 1910 | * drm_fb_helper_fill_fix - initializes fixed fbdev information |
| 1911 | * @info: fbdev registered by the helper |
| 1912 | * @pitch: desired pitch |
| 1913 | * @depth: desired depth |
| 1914 | * |
| 1915 | * Helper to fill in the fixed fbdev information useful for a non-accelerated |
| 1916 | * fbdev emulations. Drivers which support acceleration methods which impose |
| 1917 | * additional constraints need to set up their own limits. |
| 1918 | * |
| 1919 | * Drivers should call this (or their equivalent setup code) from their |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1920 | * &drm_fb_helper_funcs.fb_probe callback. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1921 | */ |
Dave Airlie | 3632ef8 | 2011-01-15 09:27:00 +1000 | [diff] [blame] | 1922 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
| 1923 | uint32_t depth) |
| 1924 | { |
| 1925 | info->fix.type = FB_TYPE_PACKED_PIXELS; |
| 1926 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : |
| 1927 | FB_VISUAL_TRUECOLOR; |
| 1928 | info->fix.mmio_start = 0; |
| 1929 | info->fix.mmio_len = 0; |
| 1930 | info->fix.type_aux = 0; |
| 1931 | info->fix.xpanstep = 1; /* doing it in hw */ |
| 1932 | info->fix.ypanstep = 1; /* doing it in hw */ |
| 1933 | info->fix.ywrapstep = 0; |
| 1934 | info->fix.accel = FB_ACCEL_NONE; |
Dave Airlie | 3632ef8 | 2011-01-15 09:27:00 +1000 | [diff] [blame] | 1935 | |
| 1936 | info->fix.line_length = pitch; |
Dave Airlie | 3632ef8 | 2011-01-15 09:27:00 +1000 | [diff] [blame] | 1937 | } |
| 1938 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); |
| 1939 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1940 | /** |
| 1941 | * drm_fb_helper_fill_var - initalizes variable fbdev information |
| 1942 | * @info: fbdev instance to set up |
| 1943 | * @fb_helper: fb helper instance to use as template |
| 1944 | * @fb_width: desired fb width |
| 1945 | * @fb_height: desired fb height |
| 1946 | * |
| 1947 | * Sets up the variable fbdev metainformation from the given fb helper instance |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1948 | * and the drm framebuffer allocated in &drm_fb_helper.fb. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1949 | * |
| 1950 | * Drivers should call this (or their equivalent setup code) from their |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 1951 | * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev |
| 1952 | * backing storage framebuffer. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1953 | */ |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1954 | 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] | 1955 | uint32_t fb_width, uint32_t fb_height) |
| 1956 | { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1957 | struct drm_framebuffer *fb = fb_helper->fb; |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 1958 | |
Alexandru Gheorghe | 042bf75 | 2018-11-01 17:02:05 +0000 | [diff] [blame] | 1959 | WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) || |
| 1960 | (drm_format_info_block_height(fb->format, 0) > 1)); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1961 | info->pseudo_palette = fb_helper->pseudo_palette; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1962 | info->var.xres_virtual = fb->width; |
| 1963 | info->var.yres_virtual = fb->height; |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 1964 | info->var.bits_per_pixel = fb->format->cpp[0] * 8; |
James Simmons | 57084d0 | 2010-12-20 19:10:39 +0000 | [diff] [blame] | 1965 | info->var.accel_flags = FB_ACCELF_TEXT; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1966 | info->var.xoffset = 0; |
| 1967 | info->var.yoffset = 0; |
| 1968 | info->var.activate = FB_ACTIVATE_NOW; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1969 | |
Ville Syrjälä | b00c600 | 2016-12-14 23:31:35 +0200 | [diff] [blame] | 1970 | switch (fb->format->depth) { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1971 | case 8: |
| 1972 | info->var.red.offset = 0; |
| 1973 | info->var.green.offset = 0; |
| 1974 | info->var.blue.offset = 0; |
| 1975 | info->var.red.length = 8; /* 8bit DAC */ |
| 1976 | info->var.green.length = 8; |
| 1977 | info->var.blue.length = 8; |
| 1978 | info->var.transp.offset = 0; |
| 1979 | info->var.transp.length = 0; |
| 1980 | break; |
| 1981 | case 15: |
| 1982 | info->var.red.offset = 10; |
| 1983 | info->var.green.offset = 5; |
| 1984 | info->var.blue.offset = 0; |
| 1985 | info->var.red.length = 5; |
| 1986 | info->var.green.length = 5; |
| 1987 | info->var.blue.length = 5; |
| 1988 | info->var.transp.offset = 15; |
| 1989 | info->var.transp.length = 1; |
| 1990 | break; |
| 1991 | case 16: |
| 1992 | info->var.red.offset = 11; |
| 1993 | info->var.green.offset = 5; |
| 1994 | info->var.blue.offset = 0; |
| 1995 | info->var.red.length = 5; |
| 1996 | info->var.green.length = 6; |
| 1997 | info->var.blue.length = 5; |
| 1998 | info->var.transp.offset = 0; |
| 1999 | break; |
| 2000 | case 24: |
| 2001 | info->var.red.offset = 16; |
| 2002 | info->var.green.offset = 8; |
| 2003 | info->var.blue.offset = 0; |
| 2004 | info->var.red.length = 8; |
| 2005 | info->var.green.length = 8; |
| 2006 | info->var.blue.length = 8; |
| 2007 | info->var.transp.offset = 0; |
| 2008 | info->var.transp.length = 0; |
| 2009 | break; |
| 2010 | case 32: |
| 2011 | info->var.red.offset = 16; |
| 2012 | info->var.green.offset = 8; |
| 2013 | info->var.blue.offset = 0; |
| 2014 | info->var.red.length = 8; |
| 2015 | info->var.green.length = 8; |
| 2016 | info->var.blue.length = 8; |
| 2017 | info->var.transp.offset = 24; |
| 2018 | info->var.transp.length = 8; |
| 2019 | break; |
| 2020 | default: |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| 2024 | info->var.xres = fb_width; |
| 2025 | info->var.yres = fb_height; |
| 2026 | } |
| 2027 | EXPORT_SYMBOL(drm_fb_helper_fill_var); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2028 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2029 | 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] | 2030 | uint32_t maxX, |
| 2031 | uint32_t maxY) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2032 | { |
| 2033 | struct drm_connector *connector; |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 2034 | int i, count = 0; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2035 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2036 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2037 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2038 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
| 2039 | } |
| 2040 | |
| 2041 | return count; |
| 2042 | } |
| 2043 | |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 2044 | 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] | 2045 | { |
| 2046 | struct drm_display_mode *mode; |
| 2047 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2048 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
Daniel Vetter | 9d3de13 | 2014-01-23 16:27:56 +0100 | [diff] [blame] | 2049 | if (mode->hdisplay > width || |
| 2050 | mode->vdisplay > height) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2051 | continue; |
| 2052 | if (mode->type & DRM_MODE_TYPE_PREFERRED) |
| 2053 | return mode; |
| 2054 | } |
| 2055 | return NULL; |
| 2056 | } |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 2057 | EXPORT_SYMBOL(drm_has_preferred_mode); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2058 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2059 | 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] | 2060 | { |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 2061 | return fb_connector->connector->cmdline_mode.specified; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Vincent Abriou | a09759e | 2017-01-06 17:44:43 +0100 | [diff] [blame] | 2064 | 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] | 2065 | { |
Chris Wilson | 1794d25 | 2011-04-17 07:43:32 +0100 | [diff] [blame] | 2066 | struct drm_cmdline_mode *cmdline_mode; |
Daniel Stone | f3af5c7 | 2015-03-19 04:33:01 +0000 | [diff] [blame] | 2067 | struct drm_display_mode *mode; |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 2068 | bool prefer_non_interlace; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2069 | |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 2070 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2071 | if (cmdline_mode->specified == false) |
Daniel Stone | f3af5c7 | 2015-03-19 04:33:01 +0000 | [diff] [blame] | 2072 | return NULL; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2073 | |
| 2074 | /* attempt to find a matching mode in the list of modes |
| 2075 | * we have gotten so far, if not add a CVT mode that conforms |
| 2076 | */ |
| 2077 | if (cmdline_mode->rb || cmdline_mode->margins) |
| 2078 | goto create_mode; |
| 2079 | |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 2080 | prefer_non_interlace = !cmdline_mode->interlace; |
Daniel Stone | f3af5c7 | 2015-03-19 04:33:01 +0000 | [diff] [blame] | 2081 | again: |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2082 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2083 | /* check width/height */ |
| 2084 | if (mode->hdisplay != cmdline_mode->xres || |
| 2085 | mode->vdisplay != cmdline_mode->yres) |
| 2086 | continue; |
| 2087 | |
| 2088 | if (cmdline_mode->refresh_specified) { |
| 2089 | if (mode->vrefresh != cmdline_mode->refresh) |
| 2090 | continue; |
| 2091 | } |
| 2092 | |
| 2093 | if (cmdline_mode->interlace) { |
| 2094 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) |
| 2095 | continue; |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 2096 | } else if (prefer_non_interlace) { |
| 2097 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) |
| 2098 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2099 | } |
| 2100 | return mode; |
| 2101 | } |
| 2102 | |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 2103 | if (prefer_non_interlace) { |
| 2104 | prefer_non_interlace = false; |
| 2105 | goto again; |
| 2106 | } |
| 2107 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2108 | create_mode: |
Chris Wilson | 1794d25 | 2011-04-17 07:43:32 +0100 | [diff] [blame] | 2109 | mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, |
| 2110 | cmdline_mode); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2111 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2112 | return mode; |
| 2113 | } |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 2114 | EXPORT_SYMBOL(drm_pick_cmdline_mode); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2115 | |
| 2116 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) |
| 2117 | { |
| 2118 | bool enable; |
| 2119 | |
Dave Airlie | b5f0538 | 2017-10-16 05:08:39 +0100 | [diff] [blame] | 2120 | if (connector->display_info.non_desktop) |
| 2121 | return false; |
| 2122 | |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 2123 | if (strict) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2124 | enable = connector->status == connector_status_connected; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 2125 | else |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2126 | enable = connector->status != connector_status_disconnected; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 2127 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2128 | return enable; |
| 2129 | } |
| 2130 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2131 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
| 2132 | bool *enabled) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2133 | { |
| 2134 | bool any_enabled = false; |
| 2135 | struct drm_connector *connector; |
| 2136 | int i = 0; |
| 2137 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2138 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2139 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2140 | enabled[i] = drm_connector_enabled(connector, true); |
| 2141 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, |
Dave Airlie | b5f0538 | 2017-10-16 05:08:39 +0100 | [diff] [blame] | 2142 | connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no"); |
| 2143 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2144 | any_enabled |= enabled[i]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | if (any_enabled) |
| 2148 | return; |
| 2149 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2150 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2151 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2152 | enabled[i] = drm_connector_enabled(connector, false); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2153 | } |
| 2154 | } |
| 2155 | |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2156 | static bool drm_target_cloned(struct drm_fb_helper *fb_helper, |
| 2157 | struct drm_display_mode **modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2158 | struct drm_fb_offset *offsets, |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2159 | bool *enabled, int width, int height) |
| 2160 | { |
| 2161 | int count, i, j; |
| 2162 | bool can_clone = false; |
| 2163 | struct drm_fb_helper_connector *fb_helper_conn; |
| 2164 | struct drm_display_mode *dmt_mode, *mode; |
| 2165 | |
| 2166 | /* only contemplate cloning in the single crtc case */ |
| 2167 | if (fb_helper->crtc_count > 1) |
| 2168 | return false; |
| 2169 | |
| 2170 | count = 0; |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2171 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2172 | if (enabled[i]) |
| 2173 | count++; |
| 2174 | } |
| 2175 | |
| 2176 | /* only contemplate cloning if more than one connector is enabled */ |
| 2177 | if (count <= 1) |
| 2178 | return false; |
| 2179 | |
| 2180 | /* check the command line or if nothing common pick 1024x768 */ |
| 2181 | can_clone = true; |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2182 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2183 | if (!enabled[i]) |
| 2184 | continue; |
| 2185 | fb_helper_conn = fb_helper->connector_info[i]; |
Vincent Abriou | a09759e | 2017-01-06 17:44:43 +0100 | [diff] [blame] | 2186 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn); |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2187 | if (!modes[i]) { |
| 2188 | can_clone = false; |
| 2189 | break; |
| 2190 | } |
| 2191 | for (j = 0; j < i; j++) { |
| 2192 | if (!enabled[j]) |
| 2193 | continue; |
Shashank Sharma | 222ec16 | 2018-05-08 16:39:44 +0530 | [diff] [blame] | 2194 | if (!drm_mode_match(modes[j], modes[i], |
| 2195 | DRM_MODE_MATCH_TIMINGS | |
| 2196 | DRM_MODE_MATCH_CLOCK | |
| 2197 | DRM_MODE_MATCH_FLAGS | |
| 2198 | DRM_MODE_MATCH_3D_FLAGS)) |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2199 | can_clone = false; |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | if (can_clone) { |
| 2204 | DRM_DEBUG_KMS("can clone using command line\n"); |
| 2205 | return true; |
| 2206 | } |
| 2207 | |
| 2208 | /* try and find a 1024x768 mode on each connector */ |
| 2209 | can_clone = true; |
Adam Jackson | f6e252b | 2012-04-13 16:33:31 -0400 | [diff] [blame] | 2210 | 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] | 2211 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2212 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2213 | if (!enabled[i]) |
| 2214 | continue; |
| 2215 | |
| 2216 | fb_helper_conn = fb_helper->connector_info[i]; |
| 2217 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
Shashank Sharma | 222ec16 | 2018-05-08 16:39:44 +0530 | [diff] [blame] | 2218 | if (drm_mode_match(mode, dmt_mode, |
| 2219 | DRM_MODE_MATCH_TIMINGS | |
| 2220 | DRM_MODE_MATCH_CLOCK | |
| 2221 | DRM_MODE_MATCH_FLAGS | |
| 2222 | DRM_MODE_MATCH_3D_FLAGS)) |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2223 | modes[i] = mode; |
| 2224 | } |
| 2225 | if (!modes[i]) |
| 2226 | can_clone = false; |
| 2227 | } |
| 2228 | |
| 2229 | if (can_clone) { |
| 2230 | DRM_DEBUG_KMS("can clone using 1024x768\n"); |
| 2231 | return true; |
| 2232 | } |
| 2233 | DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); |
| 2234 | return false; |
| 2235 | } |
| 2236 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2237 | static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, |
| 2238 | struct drm_display_mode **modes, |
| 2239 | struct drm_fb_offset *offsets, |
| 2240 | int idx, |
| 2241 | int h_idx, int v_idx) |
| 2242 | { |
| 2243 | struct drm_fb_helper_connector *fb_helper_conn; |
| 2244 | int i; |
| 2245 | int hoffset = 0, voffset = 0; |
| 2246 | |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2247 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2248 | fb_helper_conn = fb_helper->connector_info[i]; |
| 2249 | if (!fb_helper_conn->connector->has_tile) |
| 2250 | continue; |
| 2251 | |
| 2252 | if (!modes[i] && (h_idx || v_idx)) { |
| 2253 | DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, |
| 2254 | fb_helper_conn->connector->base.id); |
| 2255 | continue; |
| 2256 | } |
| 2257 | if (fb_helper_conn->connector->tile_h_loc < h_idx) |
| 2258 | hoffset += modes[i]->hdisplay; |
| 2259 | |
| 2260 | if (fb_helper_conn->connector->tile_v_loc < v_idx) |
| 2261 | voffset += modes[i]->vdisplay; |
| 2262 | } |
| 2263 | offsets[idx].x = hoffset; |
| 2264 | offsets[idx].y = voffset; |
| 2265 | DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2269 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2270 | struct drm_display_mode **modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2271 | struct drm_fb_offset *offsets, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2272 | bool *enabled, int width, int height) |
| 2273 | { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2274 | struct drm_fb_helper_connector *fb_helper_conn; |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2275 | const u64 mask = BIT_ULL(fb_helper->connector_count) - 1; |
| 2276 | u64 conn_configured = 0; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2277 | int tile_pass = 0; |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2278 | int i; |
| 2279 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2280 | retry: |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2281 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2282 | fb_helper_conn = fb_helper->connector_info[i]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2283 | |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2284 | if (conn_configured & BIT_ULL(i)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2285 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2286 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2287 | if (enabled[i] == false) { |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2288 | conn_configured |= BIT_ULL(i); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2289 | continue; |
| 2290 | } |
| 2291 | |
| 2292 | /* first pass over all the untiled connectors */ |
| 2293 | if (tile_pass == 0 && fb_helper_conn->connector->has_tile) |
| 2294 | continue; |
| 2295 | |
| 2296 | if (tile_pass == 1) { |
| 2297 | if (fb_helper_conn->connector->tile_h_loc != 0 || |
| 2298 | fb_helper_conn->connector->tile_v_loc != 0) |
| 2299 | continue; |
| 2300 | |
| 2301 | } else { |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 2302 | if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 && |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2303 | fb_helper_conn->connector->tile_v_loc != tile_pass - 1) |
| 2304 | /* if this tile_pass doesn't cover any of the tiles - keep going */ |
| 2305 | continue; |
| 2306 | |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 2307 | /* |
| 2308 | * find the tile offsets for this pass - need to find |
| 2309 | * all tiles left and above |
| 2310 | */ |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2311 | drm_get_tile_offsets(fb_helper, modes, offsets, |
| 2312 | i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); |
| 2313 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2314 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2315 | fb_helper_conn->connector->base.id); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2316 | |
| 2317 | /* got for command line mode first */ |
Vincent Abriou | a09759e | 2017-01-06 17:44:43 +0100 | [diff] [blame] | 2318 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2319 | if (!modes[i]) { |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2320 | DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", |
| 2321 | 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] | 2322 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2323 | } |
| 2324 | /* No preferred modes, pick one off the list */ |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2325 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
| 2326 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2327 | break; |
| 2328 | } |
| 2329 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : |
| 2330 | "none"); |
Chris Wilson | c96521e | 2016-11-27 17:09:10 +0000 | [diff] [blame] | 2331 | conn_configured |= BIT_ULL(i); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | if ((conn_configured & mask) != mask) { |
| 2335 | tile_pass++; |
| 2336 | goto retry; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2337 | } |
| 2338 | return true; |
| 2339 | } |
| 2340 | |
Ville Syrjälä | 0d998891 | 2018-06-28 16:13:07 +0300 | [diff] [blame] | 2341 | static bool connector_has_possible_crtc(struct drm_connector *connector, |
| 2342 | struct drm_crtc *crtc) |
| 2343 | { |
Ville Syrjälä | 83aefbb | 2018-06-28 16:13:09 +0300 | [diff] [blame] | 2344 | struct drm_encoder *encoder; |
Ville Syrjälä | 0d998891 | 2018-06-28 16:13:07 +0300 | [diff] [blame] | 2345 | int i; |
| 2346 | |
Ville Syrjälä | 83aefbb | 2018-06-28 16:13:09 +0300 | [diff] [blame] | 2347 | drm_connector_for_each_possible_encoder(connector, encoder, i) { |
Ville Syrjälä | 0d998891 | 2018-06-28 16:13:07 +0300 | [diff] [blame] | 2348 | if (encoder->possible_crtcs & drm_crtc_mask(crtc)) |
| 2349 | return true; |
| 2350 | } |
| 2351 | |
| 2352 | return false; |
| 2353 | } |
| 2354 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2355 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
| 2356 | struct drm_fb_helper_crtc **best_crtcs, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2357 | struct drm_display_mode **modes, |
| 2358 | int n, int width, int height) |
| 2359 | { |
| 2360 | int c, o; |
| 2361 | struct drm_connector *connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2362 | int my_score, best_score, score; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2363 | struct drm_fb_helper_crtc **crtcs, *crtc; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2364 | struct drm_fb_helper_connector *fb_helper_conn; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2365 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2366 | if (n == fb_helper->connector_count) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2367 | return 0; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2368 | |
| 2369 | fb_helper_conn = fb_helper->connector_info[n]; |
| 2370 | connector = fb_helper_conn->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2371 | |
| 2372 | best_crtcs[n] = NULL; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2373 | 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] | 2374 | if (modes[n] == NULL) |
| 2375 | return best_score; |
| 2376 | |
Harsha Sharma | 4b947b1c | 2017-10-13 13:07:47 +0530 | [diff] [blame] | 2377 | crtcs = kcalloc(fb_helper->connector_count, |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2378 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2379 | if (!crtcs) |
| 2380 | return best_score; |
| 2381 | |
| 2382 | my_score = 1; |
| 2383 | if (connector->status == connector_status_connected) |
| 2384 | my_score++; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2385 | if (drm_has_cmdline_mode(fb_helper_conn)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2386 | my_score++; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2387 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2388 | my_score++; |
| 2389 | |
Boris Brezillon | c61b93f | 2016-06-07 13:47:56 +0200 | [diff] [blame] | 2390 | /* |
Thierry Reding | 4b4f99f | 2017-03-29 16:43:51 +0200 | [diff] [blame] | 2391 | * select a crtc for this connector and then attempt to configure |
| 2392 | * remaining connectors |
| 2393 | */ |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2394 | for (c = 0; c < fb_helper->crtc_count; c++) { |
| 2395 | crtc = &fb_helper->crtc_info[c]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2396 | |
Ville Syrjälä | 0d998891 | 2018-06-28 16:13:07 +0300 | [diff] [blame] | 2397 | if (!connector_has_possible_crtc(connector, |
| 2398 | crtc->mode_set.crtc)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2399 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2400 | |
| 2401 | for (o = 0; o < n; o++) |
| 2402 | if (best_crtcs[o] == crtc) |
| 2403 | break; |
| 2404 | |
| 2405 | if (o < n) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2406 | /* ignore cloning unless only a single crtc */ |
| 2407 | if (fb_helper->crtc_count > 1) |
| 2408 | continue; |
| 2409 | |
| 2410 | if (!drm_mode_equal(modes[o], modes[n])) |
| 2411 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | crtcs[n] = crtc; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2415 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
| 2416 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2417 | width, height); |
| 2418 | if (score > best_score) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2419 | best_score = score; |
| 2420 | memcpy(best_crtcs, crtcs, |
Lyude | 255f0e7 | 2016-05-12 10:56:59 -0400 | [diff] [blame] | 2421 | fb_helper->connector_count * |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2422 | sizeof(struct drm_fb_helper_crtc *)); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2423 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2424 | } |
Ville Syrjälä | 0d998891 | 2018-06-28 16:13:07 +0300 | [diff] [blame] | 2425 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2426 | kfree(crtcs); |
| 2427 | return best_score; |
| 2428 | } |
| 2429 | |
Hans de Goede | 8f0cb41 | 2017-11-25 20:35:50 +0100 | [diff] [blame] | 2430 | /* |
| 2431 | * This function checks if rotation is necessary because of panel orientation |
| 2432 | * and if it is, if it is supported. |
| 2433 | * If rotation is necessary and supported, its gets set in fb_crtc.rotation. |
| 2434 | * If rotation is necessary but not supported, a DRM_MODE_ROTATE_* flag gets |
| 2435 | * or-ed into fb_helper->sw_rotations. In drm_setup_crtcs_fb() we check if only |
| 2436 | * one bit is set and then we set fb_info.fbcon_rotate_hint to make fbcon do |
| 2437 | * the unsupported rotation. |
| 2438 | */ |
| 2439 | static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper, |
| 2440 | struct drm_fb_helper_crtc *fb_crtc, |
| 2441 | struct drm_connector *connector) |
| 2442 | { |
| 2443 | struct drm_plane *plane = fb_crtc->mode_set.crtc->primary; |
| 2444 | uint64_t valid_mask = 0; |
| 2445 | int i, rotation; |
| 2446 | |
| 2447 | fb_crtc->rotation = DRM_MODE_ROTATE_0; |
| 2448 | |
| 2449 | switch (connector->display_info.panel_orientation) { |
| 2450 | case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP: |
| 2451 | rotation = DRM_MODE_ROTATE_180; |
| 2452 | break; |
| 2453 | case DRM_MODE_PANEL_ORIENTATION_LEFT_UP: |
| 2454 | rotation = DRM_MODE_ROTATE_90; |
| 2455 | break; |
| 2456 | case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP: |
| 2457 | rotation = DRM_MODE_ROTATE_270; |
| 2458 | break; |
| 2459 | default: |
| 2460 | rotation = DRM_MODE_ROTATE_0; |
| 2461 | } |
| 2462 | |
| 2463 | /* |
| 2464 | * TODO: support 90 / 270 degree hardware rotation, |
| 2465 | * depending on the hardware this may require the framebuffer |
| 2466 | * to be in a specific tiling format. |
| 2467 | */ |
| 2468 | if (rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property) { |
| 2469 | fb_helper->sw_rotations |= rotation; |
| 2470 | return; |
| 2471 | } |
| 2472 | |
| 2473 | for (i = 0; i < plane->rotation_property->num_values; i++) |
| 2474 | valid_mask |= (1ULL << plane->rotation_property->values[i]); |
| 2475 | |
| 2476 | if (!(rotation & valid_mask)) { |
| 2477 | fb_helper->sw_rotations |= rotation; |
| 2478 | return; |
| 2479 | } |
| 2480 | |
| 2481 | fb_crtc->rotation = rotation; |
| 2482 | /* Rotating in hardware, fbcon should not rotate */ |
| 2483 | fb_helper->sw_rotations |= DRM_MODE_ROTATE_0; |
| 2484 | } |
| 2485 | |
Chris Wilson | 64e9440 | 2016-11-29 12:02:16 +0000 | [diff] [blame] | 2486 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper, |
| 2487 | u32 width, u32 height) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2488 | { |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2489 | struct drm_device *dev = fb_helper->dev; |
| 2490 | struct drm_fb_helper_crtc **crtcs; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2491 | struct drm_display_mode **modes; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2492 | struct drm_fb_offset *offsets; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2493 | bool *enabled; |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2494 | int i; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2495 | |
| 2496 | DRM_DEBUG_KMS("\n"); |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2497 | /* prevent concurrent modification of connector_count by hotplug */ |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2498 | lockdep_assert_held(&fb_helper->lock); |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2499 | |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2500 | crtcs = kcalloc(fb_helper->connector_count, |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2501 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2502 | modes = kcalloc(fb_helper->connector_count, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2503 | sizeof(struct drm_display_mode *), GFP_KERNEL); |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2504 | offsets = kcalloc(fb_helper->connector_count, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2505 | sizeof(struct drm_fb_offset), GFP_KERNEL); |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2506 | enabled = kcalloc(fb_helper->connector_count, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2507 | sizeof(bool), GFP_KERNEL); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2508 | if (!crtcs || !modes || !enabled || !offsets) { |
Sachin Kamat | 8c5eaca | 2012-11-19 09:44:58 +0000 | [diff] [blame] | 2509 | DRM_ERROR("Memory allocation failed\n"); |
| 2510 | goto out; |
| 2511 | } |
| 2512 | |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 2513 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
| 2514 | if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0) |
| 2515 | DRM_DEBUG_KMS("No connectors reported connected with modes\n"); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 2516 | drm_enable_connectors(fb_helper, enabled); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2517 | |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2518 | if (!(fb_helper->funcs->initial_config && |
| 2519 | fb_helper->funcs->initial_config(fb_helper, crtcs, modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2520 | offsets, |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2521 | enabled, width, height))) { |
Maarten Lankhorst | 383b2e5 | 2016-02-15 13:45:15 +0100 | [diff] [blame] | 2522 | memset(modes, 0, fb_helper->connector_count*sizeof(modes[0])); |
| 2523 | memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0])); |
| 2524 | memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0])); |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2525 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2526 | if (!drm_target_cloned(fb_helper, modes, offsets, |
| 2527 | enabled, width, height) && |
| 2528 | !drm_target_preferred(fb_helper, modes, offsets, |
| 2529 | enabled, width, height)) |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2530 | DRM_ERROR("Unable to find initial modes\n"); |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 2531 | |
| 2532 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", |
| 2533 | width, height); |
| 2534 | |
| 2535 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 2536 | } |
Daniel Vetter | e13a058 | 2017-07-05 06:56:29 +0200 | [diff] [blame] | 2537 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2538 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2539 | /* need to set the modesets up here for use later */ |
| 2540 | /* fill out the connector<->crtc mappings into the modesets */ |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 2541 | for (i = 0; i < fb_helper->crtc_count; i++) |
| 2542 | drm_fb_helper_modeset_release(fb_helper, |
| 2543 | &fb_helper->crtc_info[i].mode_set); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2544 | |
Hans de Goede | 8f0cb41 | 2017-11-25 20:35:50 +0100 | [diff] [blame] | 2545 | fb_helper->sw_rotations = 0; |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2546 | drm_fb_helper_for_each_connector(fb_helper, i) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2547 | struct drm_display_mode *mode = modes[i]; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2548 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2549 | struct drm_fb_offset *offset = &offsets[i]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2550 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2551 | if (mode && fb_crtc) { |
David Lechner | 27a061f | 2017-08-02 13:00:13 -0500 | [diff] [blame] | 2552 | struct drm_mode_set *modeset = &fb_crtc->mode_set; |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 2553 | struct drm_connector *connector = |
| 2554 | fb_helper->connector_info[i]->connector; |
| 2555 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2556 | DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", |
| 2557 | 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] | 2558 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2559 | fb_crtc->desired_mode = mode; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2560 | fb_crtc->x = offset->x; |
| 2561 | fb_crtc->y = offset->y; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2562 | modeset->mode = drm_mode_duplicate(dev, |
| 2563 | fb_crtc->desired_mode); |
Thierry Reding | ad09360 | 2017-02-28 15:46:39 +0100 | [diff] [blame] | 2564 | drm_connector_get(connector); |
Hans de Goede | 8f0cb41 | 2017-11-25 20:35:50 +0100 | [diff] [blame] | 2565 | drm_setup_crtc_rotation(fb_helper, fb_crtc, connector); |
Ville Syrjälä | a288960 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 2566 | modeset->connectors[modeset->num_connectors++] = connector; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2567 | modeset->x = offset->x; |
| 2568 | modeset->y = offset->y; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2569 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2570 | } |
Sachin Kamat | 8c5eaca | 2012-11-19 09:44:58 +0000 | [diff] [blame] | 2571 | out: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2572 | kfree(crtcs); |
| 2573 | kfree(modes); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 2574 | kfree(offsets); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2575 | kfree(enabled); |
| 2576 | } |
| 2577 | |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2578 | /* |
| 2579 | * This is a continuation of drm_setup_crtcs() that sets up anything related |
| 2580 | * to the framebuffer. During initialization, drm_setup_crtcs() is called before |
| 2581 | * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev). |
| 2582 | * So, any setup that touches those fields needs to be done here instead of in |
| 2583 | * drm_setup_crtcs(). |
| 2584 | */ |
| 2585 | static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) |
| 2586 | { |
David Lechner | 991a399 | 2017-08-04 11:25:24 -0500 | [diff] [blame] | 2587 | struct fb_info *info = fb_helper->fbdev; |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2588 | int i; |
| 2589 | |
| 2590 | for (i = 0; i < fb_helper->crtc_count; i++) |
| 2591 | if (fb_helper->crtc_info[i].mode_set.num_connectors) |
| 2592 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; |
David Lechner | 991a399 | 2017-08-04 11:25:24 -0500 | [diff] [blame] | 2593 | |
| 2594 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
| 2595 | drm_fb_helper_for_each_connector(fb_helper, i) { |
| 2596 | struct drm_connector *connector = |
| 2597 | fb_helper->connector_info[i]->connector; |
| 2598 | |
| 2599 | /* use first connected connector for the physical dimensions */ |
| 2600 | if (connector->status == connector_status_connected) { |
| 2601 | info->var.width = connector->display_info.width_mm; |
| 2602 | info->var.height = connector->display_info.height_mm; |
| 2603 | break; |
| 2604 | } |
| 2605 | } |
| 2606 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
Hans de Goede | 8f0cb41 | 2017-11-25 20:35:50 +0100 | [diff] [blame] | 2607 | |
| 2608 | switch (fb_helper->sw_rotations) { |
| 2609 | case DRM_MODE_ROTATE_0: |
| 2610 | info->fbcon_rotate_hint = FB_ROTATE_UR; |
| 2611 | break; |
| 2612 | case DRM_MODE_ROTATE_90: |
| 2613 | info->fbcon_rotate_hint = FB_ROTATE_CCW; |
| 2614 | break; |
| 2615 | case DRM_MODE_ROTATE_180: |
| 2616 | info->fbcon_rotate_hint = FB_ROTATE_UD; |
| 2617 | break; |
| 2618 | case DRM_MODE_ROTATE_270: |
| 2619 | info->fbcon_rotate_hint = FB_ROTATE_CW; |
| 2620 | break; |
| 2621 | default: |
| 2622 | /* |
| 2623 | * Multiple bits are set / multiple rotations requested |
| 2624 | * fbcon cannot handle separate rotation settings per |
| 2625 | * output, so fallback to unrotated. |
| 2626 | */ |
| 2627 | info->fbcon_rotate_hint = FB_ROTATE_UR; |
| 2628 | } |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2629 | } |
| 2630 | |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2631 | /* Note: Drops fb_helper->lock before returning. */ |
| 2632 | static int |
| 2633 | __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper, |
| 2634 | int bpp_sel) |
| 2635 | { |
| 2636 | struct drm_device *dev = fb_helper->dev; |
| 2637 | struct fb_info *info; |
| 2638 | unsigned int width, height; |
| 2639 | int ret; |
| 2640 | |
| 2641 | width = dev->mode_config.max_width; |
| 2642 | height = dev->mode_config.max_height; |
| 2643 | |
| 2644 | drm_setup_crtcs(fb_helper, width, height); |
| 2645 | ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); |
| 2646 | if (ret < 0) { |
| 2647 | if (ret == -EAGAIN) { |
| 2648 | fb_helper->preferred_bpp = bpp_sel; |
| 2649 | fb_helper->deferred_setup = true; |
| 2650 | ret = 0; |
| 2651 | } |
| 2652 | mutex_unlock(&fb_helper->lock); |
| 2653 | |
| 2654 | return ret; |
| 2655 | } |
David Lechner | f461bd2 | 2017-08-03 11:19:08 -0500 | [diff] [blame] | 2656 | drm_setup_crtcs_fb(fb_helper); |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2657 | |
| 2658 | fb_helper->deferred_setup = false; |
| 2659 | |
| 2660 | info = fb_helper->fbdev; |
| 2661 | info->var.pixclock = 0; |
Neil Armstrong | 4be9bd1 | 2018-09-28 14:05:55 +0200 | [diff] [blame] | 2662 | /* Shamelessly allow physical address leaking to userspace */ |
| 2663 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) |
| 2664 | if (!drm_leak_fbdev_smem) |
| 2665 | #endif |
| 2666 | /* don't leak any physical addresses to userspace */ |
| 2667 | info->flags |= FBINFO_HIDE_SMEM_START; |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2668 | |
| 2669 | /* Need to drop locks to avoid recursive deadlock in |
| 2670 | * register_framebuffer. This is ok because the only thing left to do is |
| 2671 | * register the fbdev emulation instance in kernel_fb_helper_list. */ |
| 2672 | mutex_unlock(&fb_helper->lock); |
| 2673 | |
| 2674 | ret = register_framebuffer(info); |
| 2675 | if (ret < 0) |
| 2676 | return ret; |
| 2677 | |
| 2678 | dev_info(dev->dev, "fb%d: %s frame buffer device\n", |
| 2679 | info->node, info->fix.id); |
| 2680 | |
| 2681 | mutex_lock(&kernel_fb_helper_lock); |
| 2682 | if (list_empty(&kernel_fb_helper_list)) |
| 2683 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
| 2684 | |
| 2685 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); |
| 2686 | mutex_unlock(&kernel_fb_helper_lock); |
| 2687 | |
| 2688 | return 0; |
| 2689 | } |
| 2690 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2691 | /** |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2692 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 2693 | * @fb_helper: fb_helper device struct |
| 2694 | * @bpp_sel: bpp value to use for the framebuffer configuration |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2695 | * |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 2696 | * 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] | 2697 | * At the moment, this is a cloned configuration across all heads with |
| 2698 | * a new framebuffer object as the backing store. |
| 2699 | * |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2700 | * Note that this also registers the fbdev and so allows userspace to call into |
| 2701 | * the driver through the fbdev interfaces. |
| 2702 | * |
Daniel Vetter | 6806cdf | 2017-01-25 07:26:43 +0100 | [diff] [blame] | 2703 | * This function will call down into the &drm_fb_helper_funcs.fb_probe callback |
| 2704 | * to let the driver allocate and initialize the fbdev info structure and the |
| 2705 | * 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] | 2706 | * drm_fb_helper_fill_fix() are provided as helpers to setup simple default |
| 2707 | * values for the fbdev info structure. |
| 2708 | * |
Daniel Vetter | 40f8cf4 | 2016-01-22 08:53:45 +0100 | [diff] [blame] | 2709 | * HANG DEBUGGING: |
| 2710 | * |
| 2711 | * When you have fbcon support built-in or already loaded, this function will do |
| 2712 | * a full modeset to setup the fbdev console. Due to locking misdesign in the |
| 2713 | * VT/fbdev subsystem that entire modeset sequence has to be done while holding |
| 2714 | * console_lock. Until console_unlock is called no dmesg lines will be sent out |
| 2715 | * to consoles, not even serial console. This means when your driver crashes, |
| 2716 | * you will see absolutely nothing else but a system stuck in this function, |
| 2717 | * with no further output. Any kind of printk() you place within your own driver |
| 2718 | * or in the drm core modeset code will also never show up. |
| 2719 | * |
| 2720 | * Standard debug practice is to run the fbcon setup without taking the |
| 2721 | * console_lock as a hack, to be able to see backtraces and crashes on the |
| 2722 | * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel |
| 2723 | * cmdline option. |
| 2724 | * |
| 2725 | * The other option is to just disable fbdev emulation since very likely the |
Lyude | af509d3 | 2016-05-04 11:28:53 -0400 | [diff] [blame] | 2726 | * first modeset from userspace will crash in the same way, and is even easier |
| 2727 | * 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] | 2728 | * kernel cmdline option. |
| 2729 | * |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2730 | * RETURNS: |
| 2731 | * Zero if everything went ok, nonzero otherwise. |
| 2732 | */ |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 2733 | 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] | 2734 | { |
Chris Wilson | 966a6a1 | 2016-11-29 12:02:15 +0000 | [diff] [blame] | 2735 | int ret; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2736 | |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 2737 | if (!drm_fbdev_emulation) |
| 2738 | return 0; |
| 2739 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2740 | mutex_lock(&fb_helper->lock); |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2741 | ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2742 | |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2743 | return ret; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2744 | } |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 2745 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2746 | |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2747 | /** |
| 2748 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 2749 | * probing all the outputs attached to the fb |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 2750 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2751 | * |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2752 | * 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] | 2753 | * setup after notification of a change in output configuration. |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2754 | * |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2755 | * Called at runtime, takes the mode config locks to be able to check/change the |
| 2756 | * modeset configuration. Must be run from process context (which usually means |
| 2757 | * either the output polling work or a work item launched from the driver's |
| 2758 | * hotplug interrupt). |
| 2759 | * |
Daniel Vetter | 50c3dc9 | 2014-06-27 17:19:22 +0200 | [diff] [blame] | 2760 | * Note that drivers may call this even before calling |
Lyude | af509d3 | 2016-05-04 11:28:53 -0400 | [diff] [blame] | 2761 | * 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] | 2762 | * for a race-free fbcon setup and will make sure that the fbdev emulation will |
| 2763 | * not miss any hotplug events. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 2764 | * |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 2765 | * RETURNS: |
| 2766 | * 0 on success and a non-zero error code otherwise. |
| 2767 | */ |
| 2768 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2769 | { |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2770 | int err = 0; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 2771 | |
Noralf Trønnes | c777990 | 2017-10-30 16:39:37 +0100 | [diff] [blame] | 2772 | if (!drm_fbdev_emulation || !fb_helper) |
Daniel Vetter | f64c557 | 2015-08-25 15:45:13 +0200 | [diff] [blame] | 2773 | return 0; |
| 2774 | |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2775 | mutex_lock(&fb_helper->lock); |
Daniel Vetter | ca91a27 | 2017-07-06 15:00:21 +0200 | [diff] [blame] | 2776 | if (fb_helper->deferred_setup) { |
| 2777 | err = __drm_fb_helper_initial_config_and_unlock(fb_helper, |
| 2778 | fb_helper->preferred_bpp); |
| 2779 | return err; |
| 2780 | } |
| 2781 | |
Daniel Vetter | 50c3dc9 | 2014-06-27 17:19:22 +0200 | [diff] [blame] | 2782 | if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 2783 | fb_helper->delayed_hotplug = true; |
Daniel Vetter | bdac4a0 | 2017-07-04 17:18:24 +0200 | [diff] [blame] | 2784 | mutex_unlock(&fb_helper->lock); |
| 2785 | return err; |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 2786 | } |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2787 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2788 | DRM_DEBUG_KMS("\n"); |
| 2789 | |
Chris Wilson | 64e9440 | 2016-11-29 12:02:16 +0000 | [diff] [blame] | 2790 | 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] | 2791 | drm_setup_crtcs_fb(fb_helper); |
Thierry Reding | e9827d8 | 2017-07-04 17:18:23 +0200 | [diff] [blame] | 2792 | mutex_unlock(&fb_helper->lock); |
Daniel Vetter | 89ced12 | 2013-04-11 14:26:55 +0000 | [diff] [blame] | 2793 | |
Daniel Vetter | 2180c3c | 2013-01-21 23:12:36 +0100 | [diff] [blame] | 2794 | drm_fb_helper_set_par(fb_helper->fbdev); |
| 2795 | |
| 2796 | return 0; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 2797 | } |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 2798 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
Dave Airlie | 5c4426a | 2010-03-30 05:34:17 +0000 | [diff] [blame] | 2799 | |
Noralf Trønnes | 304a4f6 | 2017-10-30 16:39:39 +0100 | [diff] [blame] | 2800 | /** |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2801 | * drm_fb_helper_fbdev_setup() - Setup fbdev emulation |
| 2802 | * @dev: DRM device |
| 2803 | * @fb_helper: fbdev helper structure to set up |
| 2804 | * @funcs: fbdev helper functions |
| 2805 | * @preferred_bpp: Preferred bits per pixel for the device. |
| 2806 | * @dev->mode_config.preferred_depth is used if this is zero. |
| 2807 | * @max_conn_count: Maximum number of connectors. |
| 2808 | * @dev->mode_config.num_connector is used if this is zero. |
| 2809 | * |
| 2810 | * This function sets up fbdev emulation and registers fbdev for access by |
| 2811 | * userspace. If all connectors are disconnected, setup is deferred to the next |
| 2812 | * time drm_fb_helper_hotplug_event() is called. |
| 2813 | * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback |
| 2814 | * function. |
| 2815 | * |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 2816 | * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev. |
| 2817 | * |
| 2818 | * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup(). |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2819 | * |
| 2820 | * Returns: |
| 2821 | * Zero on success or negative error code on failure. |
| 2822 | */ |
| 2823 | int drm_fb_helper_fbdev_setup(struct drm_device *dev, |
| 2824 | struct drm_fb_helper *fb_helper, |
| 2825 | const struct drm_fb_helper_funcs *funcs, |
| 2826 | unsigned int preferred_bpp, |
| 2827 | unsigned int max_conn_count) |
| 2828 | { |
| 2829 | int ret; |
| 2830 | |
| 2831 | if (!preferred_bpp) |
| 2832 | preferred_bpp = dev->mode_config.preferred_depth; |
| 2833 | if (!preferred_bpp) |
| 2834 | preferred_bpp = 32; |
| 2835 | |
| 2836 | if (!max_conn_count) |
| 2837 | max_conn_count = dev->mode_config.num_connector; |
| 2838 | if (!max_conn_count) { |
Noralf Trønnes | 6129369 | 2018-09-08 15:46:29 +0200 | [diff] [blame] | 2839 | DRM_DEV_ERROR(dev->dev, "fbdev: No connectors\n"); |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2840 | return -EINVAL; |
| 2841 | } |
| 2842 | |
| 2843 | drm_fb_helper_prepare(dev, fb_helper, funcs); |
| 2844 | |
| 2845 | ret = drm_fb_helper_init(dev, fb_helper, max_conn_count); |
| 2846 | if (ret < 0) { |
Noralf Trønnes | 6129369 | 2018-09-08 15:46:29 +0200 | [diff] [blame] | 2847 | DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret); |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2848 | return ret; |
| 2849 | } |
| 2850 | |
| 2851 | ret = drm_fb_helper_single_add_all_connectors(fb_helper); |
| 2852 | if (ret < 0) { |
Noralf Trønnes | 6129369 | 2018-09-08 15:46:29 +0200 | [diff] [blame] | 2853 | DRM_DEV_ERROR(dev->dev, "fbdev: Failed to add connectors (ret=%d)\n", ret); |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2854 | goto err_drm_fb_helper_fini; |
| 2855 | } |
| 2856 | |
| 2857 | if (!drm_drv_uses_atomic_modeset(dev)) |
| 2858 | drm_helper_disable_unused_functions(dev); |
| 2859 | |
| 2860 | ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp); |
| 2861 | if (ret < 0) { |
Noralf Trønnes | 6129369 | 2018-09-08 15:46:29 +0200 | [diff] [blame] | 2862 | DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret); |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2863 | goto err_drm_fb_helper_fini; |
| 2864 | } |
| 2865 | |
| 2866 | return 0; |
| 2867 | |
| 2868 | err_drm_fb_helper_fini: |
| 2869 | drm_fb_helper_fini(fb_helper); |
| 2870 | |
| 2871 | return ret; |
| 2872 | } |
| 2873 | EXPORT_SYMBOL(drm_fb_helper_fbdev_setup); |
| 2874 | |
| 2875 | /** |
| 2876 | * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation |
| 2877 | * @dev: DRM device |
| 2878 | * |
| 2879 | * This function unregisters fbdev if not already done and cleans up the |
| 2880 | * associated resources including the &drm_framebuffer. |
| 2881 | * The driver is responsible for freeing the &drm_fb_helper structure which is |
| 2882 | * stored in &drm_device->fb_helper. Do note that this pointer has been cleared |
| 2883 | * when this function returns. |
| 2884 | * |
| 2885 | * In order to support device removal/unplug while file handles are still open, |
| 2886 | * drm_fb_helper_unregister_fbi() should be called on device removal and |
| 2887 | * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when |
| 2888 | * file handles are closed. |
| 2889 | */ |
| 2890 | void drm_fb_helper_fbdev_teardown(struct drm_device *dev) |
| 2891 | { |
| 2892 | struct drm_fb_helper *fb_helper = dev->fb_helper; |
Noralf Trønnes | 48c9571 | 2017-12-15 18:51:17 +0100 | [diff] [blame] | 2893 | struct fb_ops *fbops = NULL; |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2894 | |
| 2895 | if (!fb_helper) |
| 2896 | return; |
| 2897 | |
| 2898 | /* Unregister if it hasn't been done already */ |
| 2899 | if (fb_helper->fbdev && fb_helper->fbdev->dev) |
| 2900 | drm_fb_helper_unregister_fbi(fb_helper); |
| 2901 | |
Noralf Trønnes | 48c9571 | 2017-12-15 18:51:17 +0100 | [diff] [blame] | 2902 | if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) { |
| 2903 | fb_deferred_io_cleanup(fb_helper->fbdev); |
| 2904 | kfree(fb_helper->fbdev->fbdefio); |
| 2905 | fbops = fb_helper->fbdev->fbops; |
| 2906 | } |
| 2907 | |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2908 | drm_fb_helper_fini(fb_helper); |
Noralf Trønnes | 48c9571 | 2017-12-15 18:51:17 +0100 | [diff] [blame] | 2909 | kfree(fbops); |
Noralf Trønnes | 8741216 | 2017-12-15 18:51:14 +0100 | [diff] [blame] | 2910 | |
| 2911 | if (fb_helper->fb) |
| 2912 | drm_framebuffer_remove(fb_helper->fb); |
| 2913 | } |
| 2914 | EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown); |
| 2915 | |
| 2916 | /** |
Noralf Trønnes | 304a4f6 | 2017-10-30 16:39:39 +0100 | [diff] [blame] | 2917 | * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation |
| 2918 | * @dev: DRM device |
| 2919 | * |
| 2920 | * This function can be used as the &drm_driver->lastclose callback for drivers |
| 2921 | * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked(). |
| 2922 | */ |
| 2923 | void drm_fb_helper_lastclose(struct drm_device *dev) |
| 2924 | { |
| 2925 | drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper); |
| 2926 | } |
| 2927 | EXPORT_SYMBOL(drm_fb_helper_lastclose); |
| 2928 | |
| 2929 | /** |
| 2930 | * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed |
| 2931 | * helper for fbdev emulation |
| 2932 | * @dev: DRM device |
| 2933 | * |
| 2934 | * This function can be used as the |
| 2935 | * &drm_mode_config_funcs.output_poll_changed callback for drivers that only |
| 2936 | * need to call drm_fb_helper_hotplug_event(). |
| 2937 | */ |
| 2938 | void drm_fb_helper_output_poll_changed(struct drm_device *dev) |
| 2939 | { |
| 2940 | drm_fb_helper_hotplug_event(dev->fb_helper); |
| 2941 | } |
| 2942 | EXPORT_SYMBOL(drm_fb_helper_output_poll_changed); |
| 2943 | |
Noralf Trønnes | d536540 | 2018-07-03 18:03:48 +0200 | [diff] [blame] | 2944 | /* @user: 1=userspace, 0=fbcon */ |
| 2945 | static int drm_fbdev_fb_open(struct fb_info *info, int user) |
| 2946 | { |
| 2947 | struct drm_fb_helper *fb_helper = info->par; |
| 2948 | |
| 2949 | if (!try_module_get(fb_helper->dev->driver->fops->owner)) |
| 2950 | return -ENODEV; |
| 2951 | |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
| 2955 | static int drm_fbdev_fb_release(struct fb_info *info, int user) |
| 2956 | { |
| 2957 | struct drm_fb_helper *fb_helper = info->par; |
| 2958 | |
| 2959 | module_put(fb_helper->dev->driver->fops->owner); |
| 2960 | |
| 2961 | return 0; |
| 2962 | } |
| 2963 | |
| 2964 | /* |
| 2965 | * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of |
| 2966 | * unregister_framebuffer() or fb_release(). |
| 2967 | */ |
| 2968 | static void drm_fbdev_fb_destroy(struct fb_info *info) |
| 2969 | { |
| 2970 | struct drm_fb_helper *fb_helper = info->par; |
| 2971 | struct fb_info *fbi = fb_helper->fbdev; |
| 2972 | struct fb_ops *fbops = NULL; |
| 2973 | void *shadow = NULL; |
| 2974 | |
| 2975 | if (fbi->fbdefio) { |
| 2976 | fb_deferred_io_cleanup(fbi); |
| 2977 | shadow = fbi->screen_buffer; |
| 2978 | fbops = fbi->fbops; |
| 2979 | } |
| 2980 | |
| 2981 | drm_fb_helper_fini(fb_helper); |
| 2982 | |
| 2983 | if (shadow) { |
| 2984 | vfree(shadow); |
| 2985 | kfree(fbops); |
| 2986 | } |
| 2987 | |
| 2988 | drm_client_framebuffer_delete(fb_helper->buffer); |
| 2989 | /* |
| 2990 | * FIXME: |
| 2991 | * Remove conditional when all CMA drivers have been moved over to using |
| 2992 | * drm_fbdev_generic_setup(). |
| 2993 | */ |
| 2994 | if (fb_helper->client.funcs) { |
| 2995 | drm_client_release(&fb_helper->client); |
| 2996 | kfree(fb_helper); |
| 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) |
| 3001 | { |
| 3002 | struct drm_fb_helper *fb_helper = info->par; |
| 3003 | |
| 3004 | if (fb_helper->dev->driver->gem_prime_mmap) |
| 3005 | return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma); |
| 3006 | else |
| 3007 | return -ENODEV; |
| 3008 | } |
| 3009 | |
| 3010 | static struct fb_ops drm_fbdev_fb_ops = { |
| 3011 | .owner = THIS_MODULE, |
| 3012 | DRM_FB_HELPER_DEFAULT_OPS, |
| 3013 | .fb_open = drm_fbdev_fb_open, |
| 3014 | .fb_release = drm_fbdev_fb_release, |
| 3015 | .fb_destroy = drm_fbdev_fb_destroy, |
| 3016 | .fb_mmap = drm_fbdev_fb_mmap, |
| 3017 | .fb_read = drm_fb_helper_sys_read, |
| 3018 | .fb_write = drm_fb_helper_sys_write, |
| 3019 | .fb_fillrect = drm_fb_helper_sys_fillrect, |
| 3020 | .fb_copyarea = drm_fb_helper_sys_copyarea, |
| 3021 | .fb_imageblit = drm_fb_helper_sys_imageblit, |
| 3022 | }; |
| 3023 | |
| 3024 | static struct fb_deferred_io drm_fbdev_defio = { |
| 3025 | .delay = HZ / 20, |
| 3026 | .deferred_io = drm_fb_helper_deferred_io, |
| 3027 | }; |
| 3028 | |
| 3029 | /** |
| 3030 | * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper |
| 3031 | * @fb_helper: fbdev helper structure |
| 3032 | * @sizes: describes fbdev size and scanout surface size |
| 3033 | * |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3034 | * This function uses the client API to create a framebuffer backed by a dumb buffer. |
Noralf Trønnes | d536540 | 2018-07-03 18:03:48 +0200 | [diff] [blame] | 3035 | * |
| 3036 | * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect, |
| 3037 | * fb_copyarea, fb_imageblit. |
| 3038 | * |
| 3039 | * Returns: |
| 3040 | * Zero on success or negative error code on failure. |
| 3041 | */ |
| 3042 | int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, |
| 3043 | struct drm_fb_helper_surface_size *sizes) |
| 3044 | { |
| 3045 | struct drm_client_dev *client = &fb_helper->client; |
| 3046 | struct drm_client_buffer *buffer; |
| 3047 | struct drm_framebuffer *fb; |
| 3048 | struct fb_info *fbi; |
| 3049 | u32 format; |
| 3050 | int ret; |
| 3051 | |
| 3052 | DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n", |
| 3053 | sizes->surface_width, sizes->surface_height, |
| 3054 | sizes->surface_bpp); |
| 3055 | |
| 3056 | format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); |
| 3057 | buffer = drm_client_framebuffer_create(client, sizes->surface_width, |
| 3058 | sizes->surface_height, format); |
| 3059 | if (IS_ERR(buffer)) |
| 3060 | return PTR_ERR(buffer); |
| 3061 | |
| 3062 | fb_helper->buffer = buffer; |
| 3063 | fb_helper->fb = buffer->fb; |
| 3064 | fb = buffer->fb; |
| 3065 | |
| 3066 | fbi = drm_fb_helper_alloc_fbi(fb_helper); |
| 3067 | if (IS_ERR(fbi)) { |
| 3068 | ret = PTR_ERR(fbi); |
| 3069 | goto err_free_buffer; |
| 3070 | } |
| 3071 | |
| 3072 | fbi->par = fb_helper; |
| 3073 | fbi->fbops = &drm_fbdev_fb_ops; |
| 3074 | fbi->screen_size = fb->height * fb->pitches[0]; |
| 3075 | fbi->fix.smem_len = fbi->screen_size; |
| 3076 | fbi->screen_buffer = buffer->vaddr; |
Neil Armstrong | 4be9bd1 | 2018-09-28 14:05:55 +0200 | [diff] [blame] | 3077 | /* Shamelessly leak the physical address to user-space */ |
| 3078 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) |
| 3079 | if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0) |
| 3080 | fbi->fix.smem_start = |
| 3081 | page_to_phys(virt_to_page(fbi->screen_buffer)); |
| 3082 | #endif |
Noralf Trønnes | d536540 | 2018-07-03 18:03:48 +0200 | [diff] [blame] | 3083 | strcpy(fbi->fix.id, "DRM emulated"); |
| 3084 | |
| 3085 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth); |
| 3086 | drm_fb_helper_fill_var(fbi, fb_helper, sizes->fb_width, sizes->fb_height); |
| 3087 | |
| 3088 | if (fb->funcs->dirty) { |
| 3089 | struct fb_ops *fbops; |
| 3090 | void *shadow; |
| 3091 | |
| 3092 | /* |
| 3093 | * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per |
| 3094 | * instance version is necessary. |
| 3095 | */ |
| 3096 | fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); |
| 3097 | shadow = vzalloc(fbi->screen_size); |
| 3098 | if (!fbops || !shadow) { |
| 3099 | kfree(fbops); |
| 3100 | vfree(shadow); |
| 3101 | ret = -ENOMEM; |
| 3102 | goto err_fb_info_destroy; |
| 3103 | } |
| 3104 | |
| 3105 | *fbops = *fbi->fbops; |
| 3106 | fbi->fbops = fbops; |
| 3107 | fbi->screen_buffer = shadow; |
| 3108 | fbi->fbdefio = &drm_fbdev_defio; |
| 3109 | |
| 3110 | fb_deferred_io_init(fbi); |
| 3111 | } |
| 3112 | |
| 3113 | return 0; |
| 3114 | |
| 3115 | err_fb_info_destroy: |
| 3116 | drm_fb_helper_fini(fb_helper); |
| 3117 | err_free_buffer: |
| 3118 | drm_client_framebuffer_delete(buffer); |
| 3119 | |
| 3120 | return ret; |
| 3121 | } |
| 3122 | EXPORT_SYMBOL(drm_fb_helper_generic_probe); |
| 3123 | |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3124 | static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = { |
| 3125 | .fb_probe = drm_fb_helper_generic_probe, |
| 3126 | }; |
| 3127 | |
| 3128 | static void drm_fbdev_client_unregister(struct drm_client_dev *client) |
| 3129 | { |
| 3130 | struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); |
| 3131 | |
| 3132 | if (fb_helper->fbdev) { |
| 3133 | drm_fb_helper_unregister_fbi(fb_helper); |
| 3134 | /* drm_fbdev_fb_destroy() takes care of cleanup */ |
| 3135 | return; |
| 3136 | } |
| 3137 | |
| 3138 | /* Did drm_fb_helper_fbdev_setup() run? */ |
| 3139 | if (fb_helper->dev) |
| 3140 | drm_fb_helper_fini(fb_helper); |
| 3141 | |
| 3142 | drm_client_release(client); |
| 3143 | kfree(fb_helper); |
| 3144 | } |
| 3145 | |
| 3146 | static int drm_fbdev_client_restore(struct drm_client_dev *client) |
| 3147 | { |
| 3148 | struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); |
| 3149 | |
| 3150 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
| 3151 | |
| 3152 | return 0; |
| 3153 | } |
| 3154 | |
| 3155 | static int drm_fbdev_client_hotplug(struct drm_client_dev *client) |
| 3156 | { |
| 3157 | struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); |
| 3158 | struct drm_device *dev = client->dev; |
| 3159 | int ret; |
| 3160 | |
| 3161 | /* If drm_fb_helper_fbdev_setup() failed, we only try once */ |
| 3162 | if (!fb_helper->dev && fb_helper->funcs) |
| 3163 | return 0; |
| 3164 | |
| 3165 | if (dev->fb_helper) |
| 3166 | return drm_fb_helper_hotplug_event(dev->fb_helper); |
| 3167 | |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3168 | if (!dev->mode_config.num_connector) { |
| 3169 | DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n"); |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3170 | return 0; |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3171 | } |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3172 | |
| 3173 | ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_helper_generic_funcs, |
| 3174 | fb_helper->preferred_bpp, 0); |
| 3175 | if (ret) { |
| 3176 | fb_helper->dev = NULL; |
| 3177 | fb_helper->fbdev = NULL; |
| 3178 | return ret; |
| 3179 | } |
| 3180 | |
| 3181 | return 0; |
| 3182 | } |
| 3183 | |
| 3184 | static const struct drm_client_funcs drm_fbdev_client_funcs = { |
| 3185 | .owner = THIS_MODULE, |
| 3186 | .unregister = drm_fbdev_client_unregister, |
| 3187 | .restore = drm_fbdev_client_restore, |
| 3188 | .hotplug = drm_fbdev_client_hotplug, |
| 3189 | }; |
| 3190 | |
| 3191 | /** |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3192 | * drm_fbdev_generic_setup() - Setup generic fbdev emulation |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3193 | * @dev: DRM device |
| 3194 | * @preferred_bpp: Preferred bits per pixel for the device. |
| 3195 | * @dev->mode_config.preferred_depth is used if this is zero. |
| 3196 | * |
| 3197 | * This function sets up generic fbdev emulation for drivers that supports |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3198 | * dumb buffers with a virtual address and that can be mmap'ed. If the driver |
| 3199 | * does not support these functions, it could use drm_fb_helper_fbdev_setup(). |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3200 | * |
| 3201 | * Restore, hotplug events and teardown are all taken care of. Drivers that do |
| 3202 | * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. |
| 3203 | * Simple drivers might use drm_mode_config_helper_suspend(). |
| 3204 | * |
| 3205 | * Drivers that set the dirty callback on their framebuffer will get a shadow |
| 3206 | * fbdev buffer that is blitted onto the real buffer. This is done in order to |
| 3207 | * make deferred I/O work with all kinds of buffers. |
| 3208 | * |
| 3209 | * This function is safe to call even when there are no connectors present. |
| 3210 | * Setup will be retried on the next hotplug event. |
| 3211 | * |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3212 | * The fbdev is destroyed by drm_dev_unregister(). |
| 3213 | * |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3214 | * Returns: |
| 3215 | * Zero on success or negative error code on failure. |
| 3216 | */ |
| 3217 | int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) |
| 3218 | { |
| 3219 | struct drm_fb_helper *fb_helper; |
| 3220 | int ret; |
| 3221 | |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3222 | WARN(dev->fb_helper, "fb_helper is already set!\n"); |
| 3223 | |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3224 | if (!drm_fbdev_emulation) |
| 3225 | return 0; |
| 3226 | |
| 3227 | fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); |
| 3228 | if (!fb_helper) |
| 3229 | return -ENOMEM; |
| 3230 | |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 3231 | ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs); |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3232 | if (ret) { |
| 3233 | kfree(fb_helper); |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3234 | DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret); |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3235 | return ret; |
| 3236 | } |
| 3237 | |
Noralf Trønnes | 4d4c2d8 | 2018-10-01 21:45:36 +0200 | [diff] [blame] | 3238 | drm_client_add(&fb_helper->client); |
| 3239 | |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3240 | fb_helper->preferred_bpp = preferred_bpp; |
| 3241 | |
Peter Wu | c10802b | 2018-09-07 00:18:10 +0200 | [diff] [blame] | 3242 | ret = drm_fbdev_client_hotplug(&fb_helper->client); |
| 3243 | if (ret) |
| 3244 | DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret); |
Noralf Trønnes | 9060d7f | 2018-07-03 18:03:52 +0200 | [diff] [blame] | 3245 | |
| 3246 | return 0; |
| 3247 | } |
| 3248 | EXPORT_SYMBOL(drm_fbdev_generic_setup); |
| 3249 | |
David Rientjes | 6a108a1 | 2011-01-20 14:44:16 -0800 | [diff] [blame] | 3250 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 3251 | * but the module doesn't depend on any fb console symbols. At least |
| 3252 | * attempt to load fbcon to avoid leaving the system without a usable console. |
| 3253 | */ |
Rafael Antognolli | 70412cf | 2016-01-21 15:10:18 -0800 | [diff] [blame] | 3254 | int __init drm_fb_helper_modinit(void) |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 3255 | { |
Rafael Antognolli | 70412cf | 2016-01-21 15:10:18 -0800 | [diff] [blame] | 3256 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
Kees Cook | 0632466 | 2017-05-08 15:59:05 -0700 | [diff] [blame] | 3257 | const char name[] = "fbcon"; |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 3258 | struct module *fbcon; |
| 3259 | |
| 3260 | mutex_lock(&module_mutex); |
| 3261 | fbcon = find_module(name); |
| 3262 | mutex_unlock(&module_mutex); |
| 3263 | |
| 3264 | if (!fbcon) |
| 3265 | request_module_nowait(name); |
Rafael Antognolli | 70412cf | 2016-01-21 15:10:18 -0800 | [diff] [blame] | 3266 | #endif |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 3267 | return 0; |
| 3268 | } |
Rafael Antognolli | 70412cf | 2016-01-21 15:10:18 -0800 | [diff] [blame] | 3269 | EXPORT_SYMBOL(drm_fb_helper_modinit); |