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 | |
Andy Shevchenko | 3b40a44 | 2010-02-02 14:40:32 -0800 | [diff] [blame] | 32 | #include <linux/kernel.h> |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 33 | #include <linux/sysrq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 35 | #include <linux/fb.h> |
Paul Gortmaker | e0cd360 | 2011-08-30 11:04:30 -0400 | [diff] [blame] | 36 | #include <linux/module.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 37 | #include <drm/drmP.h> |
| 38 | #include <drm/drm_crtc.h> |
| 39 | #include <drm/drm_fb_helper.h> |
| 40 | #include <drm/drm_crtc_helper.h> |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 41 | |
| 42 | static LIST_HEAD(kernel_fb_helper_list); |
| 43 | |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 44 | /** |
| 45 | * DOC: fbdev helpers |
| 46 | * |
| 47 | * 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] | 48 | * mode setting driver. They can be used mostly independently from the crtc |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 49 | * helper functions used by many drivers to implement the kernel mode setting |
| 50 | * interfaces. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 51 | * |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 52 | * Initialization is done as a four-step process with drm_fb_helper_prepare(), |
| 53 | * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and |
| 54 | * drm_fb_helper_initial_config(). Drivers with fancier requirements than the |
| 55 | * default behaviour can override the third step with their own code. |
| 56 | * Teardown is done with drm_fb_helper_fini(). |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 57 | * |
| 58 | * At runtime drivers should restore the fbdev console by calling |
| 59 | * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They |
| 60 | * should also notify the fb helper code from updates to the output |
| 61 | * configuration by calling drm_fb_helper_hotplug_event(). For easier |
| 62 | * integration with the output polling code in drm_crtc_helper.c the modeset |
Thierry Reding | 83c617c | 2014-04-29 11:44:35 +0200 | [diff] [blame] | 63 | * code provides a ->output_poll_changed callback. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 64 | * |
| 65 | * All other functions exported by the fb helper library can be used to |
| 66 | * implement the fbdev driver interface by the driver. |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 67 | * |
| 68 | * It is possible, though perhaps somewhat tricky, to implement race-free |
| 69 | * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() |
| 70 | * helper must be called first to initialize the minimum required to make |
| 71 | * hotplug detection work. Drivers also need to make sure to properly set up |
| 72 | * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init() |
| 73 | * it is safe to enable interrupts and start processing hotplug events. At the |
| 74 | * same time, drivers should initialize all modeset objects such as CRTCs, |
| 75 | * encoders and connectors. To finish up the fbdev helper initialization, the |
| 76 | * drm_fb_helper_init() function is called. To probe for all attached displays |
| 77 | * and set up an initial configuration using the detected hardware, drivers |
| 78 | * should call drm_fb_helper_single_add_all_connectors() followed by |
| 79 | * drm_fb_helper_initial_config(). |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 80 | */ |
| 81 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 82 | /** |
| 83 | * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev |
| 84 | * emulation helper |
| 85 | * @fb_helper: fbdev initialized with drm_fb_helper_init |
| 86 | * |
| 87 | * This functions adds all the available connectors for use with the given |
| 88 | * fb_helper. This is a separate step to allow drivers to freely assign |
| 89 | * connectors to the fbdev, e.g. if some are reserved for special purposes or |
| 90 | * not adequate to be used for the fbcon. |
| 91 | * |
| 92 | * Since this is part of the initial setup before the fbdev is published, no |
| 93 | * locking is required. |
| 94 | */ |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 95 | 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] | 96 | { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 97 | struct drm_device *dev = fb_helper->dev; |
| 98 | struct drm_connector *connector; |
| 99 | int i; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 100 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 101 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 102 | struct drm_fb_helper_connector *fb_helper_connector; |
| 103 | |
| 104 | fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); |
| 105 | if (!fb_helper_connector) |
| 106 | goto fail; |
| 107 | |
| 108 | fb_helper_connector->connector = connector; |
| 109 | fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; |
| 110 | } |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 111 | return 0; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 112 | fail: |
| 113 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 114 | kfree(fb_helper->connector_info[i]); |
| 115 | fb_helper->connector_info[i] = NULL; |
| 116 | } |
| 117 | fb_helper->connector_count = 0; |
| 118 | return -ENOMEM; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 119 | } |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 120 | EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 121 | |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 122 | int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector) |
| 123 | { |
| 124 | struct drm_fb_helper_connector **temp; |
| 125 | struct drm_fb_helper_connector *fb_helper_connector; |
| 126 | |
| 127 | WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); |
| 128 | if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) { |
Damien Lespiau | 14f476f | 2014-08-08 19:15:20 +0100 | [diff] [blame] | 129 | temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL); |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 130 | if (!temp) |
| 131 | return -ENOMEM; |
| 132 | |
| 133 | fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1; |
| 134 | fb_helper->connector_info = temp; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); |
| 139 | if (!fb_helper_connector) |
| 140 | return -ENOMEM; |
| 141 | |
| 142 | fb_helper_connector->connector = connector; |
| 143 | fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; |
| 144 | return 0; |
| 145 | } |
| 146 | EXPORT_SYMBOL(drm_fb_helper_add_one_connector); |
| 147 | |
Rob Clark | 2148f18 | 2015-01-26 10:11:08 -0500 | [diff] [blame] | 148 | static void remove_from_modeset(struct drm_mode_set *set, |
| 149 | struct drm_connector *connector) |
| 150 | { |
| 151 | int i, j; |
| 152 | |
| 153 | for (i = 0; i < set->num_connectors; i++) { |
| 154 | if (set->connectors[i] == connector) |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | if (i == set->num_connectors) |
| 159 | return; |
| 160 | |
| 161 | for (j = i + 1; j < set->num_connectors; j++) { |
| 162 | set->connectors[j - 1] = set->connectors[j]; |
| 163 | } |
| 164 | set->num_connectors--; |
| 165 | |
| 166 | /* because i915 is pissy about this.. |
| 167 | * TODO maybe need to makes sure we set it back to !=NULL somewhere? |
| 168 | */ |
| 169 | if (set->num_connectors == 0) |
| 170 | set->fb = NULL; |
| 171 | } |
| 172 | |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 173 | int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, |
| 174 | struct drm_connector *connector) |
| 175 | { |
| 176 | struct drm_fb_helper_connector *fb_helper_connector; |
| 177 | int i, j; |
| 178 | |
| 179 | WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); |
| 180 | |
| 181 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 182 | if (fb_helper->connector_info[i]->connector == connector) |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | if (i == fb_helper->connector_count) |
| 187 | return -EINVAL; |
| 188 | fb_helper_connector = fb_helper->connector_info[i]; |
| 189 | |
| 190 | for (j = i + 1; j < fb_helper->connector_count; j++) { |
| 191 | fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; |
| 192 | } |
| 193 | fb_helper->connector_count--; |
| 194 | kfree(fb_helper_connector); |
Rob Clark | 2148f18 | 2015-01-26 10:11:08 -0500 | [diff] [blame] | 195 | |
| 196 | /* also cleanup dangling references to the connector: */ |
| 197 | for (i = 0; i < fb_helper->crtc_count; i++) |
| 198 | remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector); |
| 199 | |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); |
| 203 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 204 | static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper) |
| 205 | { |
| 206 | uint16_t *r_base, *g_base, *b_base; |
| 207 | int i; |
| 208 | |
Ville Syrjälä | 04c0c56 | 2013-05-27 20:19:57 +0300 | [diff] [blame] | 209 | if (helper->funcs->gamma_get == NULL) |
| 210 | return; |
| 211 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 212 | r_base = crtc->gamma_store; |
| 213 | g_base = r_base + crtc->gamma_size; |
| 214 | b_base = g_base + crtc->gamma_size; |
| 215 | |
| 216 | for (i = 0; i < crtc->gamma_size; i++) |
| 217 | helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i); |
| 218 | } |
| 219 | |
| 220 | static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) |
| 221 | { |
| 222 | uint16_t *r_base, *g_base, *b_base; |
| 223 | |
Laurent Pinchart | ebe0f24 | 2012-05-17 13:27:24 +0200 | [diff] [blame] | 224 | if (crtc->funcs->gamma_set == NULL) |
| 225 | return; |
| 226 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 227 | r_base = crtc->gamma_store; |
| 228 | g_base = r_base + crtc->gamma_size; |
| 229 | b_base = g_base + crtc->gamma_size; |
| 230 | |
| 231 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); |
| 232 | } |
| 233 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 234 | /** |
| 235 | * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter |
| 236 | * @info: fbdev registered by the helper |
| 237 | */ |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 238 | int drm_fb_helper_debug_enter(struct fb_info *info) |
| 239 | { |
| 240 | struct drm_fb_helper *helper = info->par; |
| 241 | struct drm_crtc_helper_funcs *funcs; |
| 242 | int i; |
| 243 | |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 244 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
| 245 | for (i = 0; i < helper->crtc_count; i++) { |
| 246 | struct drm_mode_set *mode_set = |
| 247 | &helper->crtc_info[i].mode_set; |
| 248 | |
| 249 | if (!mode_set->crtc->enabled) |
| 250 | continue; |
| 251 | |
| 252 | funcs = mode_set->crtc->helper_private; |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 253 | drm_fb_helper_save_lut_atomic(mode_set->crtc, helper); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 254 | funcs->mode_set_base_atomic(mode_set->crtc, |
| 255 | mode_set->fb, |
| 256 | mode_set->x, |
Jason Wessel | 413d45d | 2010-09-26 06:47:25 -0500 | [diff] [blame] | 257 | mode_set->y, |
Jason Wessel | 21c74a8 | 2010-10-13 14:09:44 -0500 | [diff] [blame] | 258 | ENTER_ATOMIC_MODE_SET); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | EXPORT_SYMBOL(drm_fb_helper_debug_enter); |
| 265 | |
| 266 | /* Find the real fb for a given fb helper CRTC */ |
| 267 | static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc) |
| 268 | { |
| 269 | struct drm_device *dev = crtc->dev; |
| 270 | struct drm_crtc *c; |
| 271 | |
| 272 | list_for_each_entry(c, &dev->mode_config.crtc_list, head) { |
| 273 | if (crtc->base.id == c->base.id) |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 274 | return c->primary->fb; |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | return NULL; |
| 278 | } |
| 279 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 280 | /** |
| 281 | * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave |
| 282 | * @info: fbdev registered by the helper |
| 283 | */ |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 284 | int drm_fb_helper_debug_leave(struct fb_info *info) |
| 285 | { |
| 286 | struct drm_fb_helper *helper = info->par; |
| 287 | struct drm_crtc *crtc; |
| 288 | struct drm_crtc_helper_funcs *funcs; |
| 289 | struct drm_framebuffer *fb; |
| 290 | int i; |
| 291 | |
| 292 | for (i = 0; i < helper->crtc_count; i++) { |
| 293 | struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; |
| 294 | crtc = mode_set->crtc; |
| 295 | funcs = crtc->helper_private; |
| 296 | fb = drm_mode_config_fb(crtc); |
| 297 | |
| 298 | if (!crtc->enabled) |
| 299 | continue; |
| 300 | |
| 301 | if (!fb) { |
| 302 | DRM_ERROR("no fb to restore??\n"); |
| 303 | continue; |
| 304 | } |
| 305 | |
Jason Wessel | 9923102 | 2010-10-13 14:09:43 -0500 | [diff] [blame] | 306 | drm_fb_helper_restore_lut_atomic(mode_set->crtc); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 307 | funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, |
Jason Wessel | 21c74a8 | 2010-10-13 14:09:44 -0500 | [diff] [blame] | 308 | crtc->y, LEAVE_ATOMIC_MODE_SET); |
Jesse Barnes | 1a7aba7 | 2010-08-05 09:22:31 -0500 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | EXPORT_SYMBOL(drm_fb_helper_debug_leave); |
| 314 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 315 | static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper) |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 316 | { |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 317 | struct drm_device *dev = fb_helper->dev; |
| 318 | struct drm_plane *plane; |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 319 | bool error = false; |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 320 | int i; |
Daniel Vetter | 6aed8ec | 2013-01-20 17:32:21 +0100 | [diff] [blame] | 321 | |
Ville Syrjälä | 3858bc5 | 2013-06-03 16:10:42 +0300 | [diff] [blame] | 322 | drm_warn_on_modeset_not_all_locked(dev); |
| 323 | |
Sonika Jindal | 9783de2 | 2014-08-05 11:26:57 +0530 | [diff] [blame] | 324 | list_for_each_entry(plane, &dev->mode_config.plane_list, head) { |
Matt Roper | e27dde3 | 2014-04-01 15:22:30 -0700 | [diff] [blame] | 325 | if (plane->type != DRM_PLANE_TYPE_PRIMARY) |
| 326 | drm_plane_force_disable(plane); |
Daniel Vetter | 6aed8ec | 2013-01-20 17:32:21 +0100 | [diff] [blame] | 327 | |
Sonika Jindal | 9783de2 | 2014-08-05 11:26:57 +0530 | [diff] [blame] | 328 | if (dev->mode_config.rotation_property) { |
Thomas Wood | 3a5f87c | 2014-08-20 14:45:00 +0100 | [diff] [blame] | 329 | drm_mode_plane_set_obj_prop(plane, |
| 330 | dev->mode_config.rotation_property, |
| 331 | BIT(DRM_ROTATE_0)); |
Sonika Jindal | 9783de2 | 2014-08-05 11:26:57 +0530 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 335 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 336 | 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] | 337 | struct drm_crtc *crtc = mode_set->crtc; |
| 338 | int ret; |
| 339 | |
| 340 | if (crtc->funcs->cursor_set) { |
| 341 | ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); |
| 342 | if (ret) |
| 343 | error = true; |
| 344 | } |
| 345 | |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 346 | ret = drm_mode_set_config_internal(mode_set); |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 347 | if (ret) |
| 348 | error = true; |
| 349 | } |
| 350 | return error; |
| 351 | } |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 352 | /** |
| 353 | * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration |
| 354 | * @fb_helper: fbcon to restore |
| 355 | * |
| 356 | * This should be called from driver's drm ->lastclose callback |
| 357 | * when implementing an fbcon on top of kms using this helper. This ensures that |
| 358 | * the user isn't greeted with a black screen when e.g. X dies. |
| 359 | * |
| 360 | * Use this variant if you need to bypass locking (panic), or already |
| 361 | * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked() |
| 362 | */ |
| 363 | static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper) |
| 364 | { |
| 365 | return restore_fbdev_mode(fb_helper); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration |
| 370 | * @fb_helper: fbcon to restore |
| 371 | * |
| 372 | * This should be called from driver's drm ->lastclose callback |
| 373 | * when implementing an fbcon on top of kms using this helper. This ensures that |
| 374 | * the user isn't greeted with a black screen when e.g. X dies. |
| 375 | */ |
| 376 | bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) |
| 377 | { |
| 378 | struct drm_device *dev = fb_helper->dev; |
| 379 | bool ret; |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 380 | bool do_delayed = false; |
| 381 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 382 | drm_modeset_lock_all(dev); |
| 383 | ret = restore_fbdev_mode(fb_helper); |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 384 | |
| 385 | do_delayed = fb_helper->delayed_hotplug; |
| 386 | if (do_delayed) |
| 387 | fb_helper->delayed_hotplug = false; |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 388 | drm_modeset_unlock_all(dev); |
Dave Airlie | e2809c7 | 2014-11-26 13:15:24 +1000 | [diff] [blame] | 389 | |
| 390 | if (do_delayed) |
| 391 | drm_fb_helper_hotplug_event(fb_helper); |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 395 | |
Daniel Vetter | d21bf46 | 2013-01-20 18:09:52 +0100 | [diff] [blame] | 396 | /* |
| 397 | * restore fbcon display for all kms driver's using this helper, used for sysrq |
| 398 | * and panic handling. |
| 399 | */ |
Sachin Kamat | 78b9c35 | 2012-08-01 17:15:32 +0530 | [diff] [blame] | 400 | static bool drm_fb_helper_force_kernel_mode(void) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 401 | { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 402 | bool ret, error = false; |
| 403 | struct drm_fb_helper *helper; |
| 404 | |
| 405 | if (list_empty(&kernel_fb_helper_list)) |
| 406 | return false; |
| 407 | |
| 408 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
Thierry Reding | b77f076 | 2014-04-29 11:44:32 +0200 | [diff] [blame] | 409 | struct drm_device *dev = helper->dev; |
| 410 | |
| 411 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 412 | continue; |
| 413 | |
Daniel Vetter | cb597bb | 2014-07-27 19:09:33 +0200 | [diff] [blame] | 414 | /* |
| 415 | * NOTE: Use trylock mode to avoid deadlocks and sleeping in |
| 416 | * panic context. |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 417 | */ |
Daniel Vetter | cb597bb | 2014-07-27 19:09:33 +0200 | [diff] [blame] | 418 | if (__drm_modeset_lock_all(dev, true) != 0) { |
Thierry Reding | b77f076 | 2014-04-29 11:44:32 +0200 | [diff] [blame] | 419 | error = true; |
| 420 | continue; |
| 421 | } |
| 422 | |
Dave Airlie | e8e7a2b | 2011-04-21 22:18:32 +0100 | [diff] [blame] | 423 | ret = drm_fb_helper_restore_fbdev_mode(helper); |
| 424 | if (ret) |
| 425 | error = true; |
Thierry Reding | b77f076 | 2014-04-29 11:44:32 +0200 | [diff] [blame] | 426 | |
Daniel Vetter | cb597bb | 2014-07-27 19:09:33 +0200 | [diff] [blame] | 427 | drm_modeset_unlock_all(dev); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 428 | } |
| 429 | return error; |
| 430 | } |
| 431 | |
Daniel Vetter | 43c8a84 | 2013-01-20 18:18:07 +0100 | [diff] [blame] | 432 | static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed, |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 433 | void *panic_str) |
| 434 | { |
Hugh Dickins | d68752c | 2011-10-17 17:06:40 -0700 | [diff] [blame] | 435 | /* |
| 436 | * It's a waste of time and effort to switch back to text console |
| 437 | * if the kernel should reboot before panic messages can be seen. |
| 438 | */ |
| 439 | if (panic_timeout < 0) |
| 440 | return 0; |
| 441 | |
Sachin Kamat | d56b1b9 | 2012-11-15 03:43:29 +0000 | [diff] [blame] | 442 | pr_err("panic occurred, switching back to text console\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 443 | return drm_fb_helper_force_kernel_mode(); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 444 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 445 | |
| 446 | static struct notifier_block paniced = { |
| 447 | .notifier_call = drm_fb_helper_panic, |
| 448 | }; |
| 449 | |
Daniel Vetter | 20c60c3 | 2012-12-17 12:13:23 +0100 | [diff] [blame] | 450 | static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) |
| 451 | { |
| 452 | struct drm_device *dev = fb_helper->dev; |
| 453 | struct drm_crtc *crtc; |
| 454 | int bound = 0, crtcs_bound = 0; |
| 455 | |
Paulo Zanoni | 520edd1 | 2013-11-27 18:24:08 -0200 | [diff] [blame] | 456 | /* Sometimes user space wants everything disabled, so don't steal the |
| 457 | * display if there's a master. */ |
| 458 | if (dev->primary->master) |
| 459 | return false; |
| 460 | |
Daniel Vetter | 20c60c3 | 2012-12-17 12:13:23 +0100 | [diff] [blame] | 461 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 462 | if (crtc->primary->fb) |
Daniel Vetter | 20c60c3 | 2012-12-17 12:13:23 +0100 | [diff] [blame] | 463 | crtcs_bound++; |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 464 | if (crtc->primary->fb == fb_helper->fb) |
Daniel Vetter | 20c60c3 | 2012-12-17 12:13:23 +0100 | [diff] [blame] | 465 | bound++; |
| 466 | } |
| 467 | |
| 468 | if (bound < crtcs_bound) |
| 469 | return false; |
Paulo Zanoni | 520edd1 | 2013-11-27 18:24:08 -0200 | [diff] [blame] | 470 | |
Daniel Vetter | 20c60c3 | 2012-12-17 12:13:23 +0100 | [diff] [blame] | 471 | return true; |
| 472 | } |
| 473 | |
Mikael Pettersson | bea1d35 | 2009-09-28 18:26:25 +0200 | [diff] [blame] | 474 | #ifdef CONFIG_MAGIC_SYSRQ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 475 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
| 476 | { |
Daniel Vetter | d21bf46 | 2013-01-20 18:09:52 +0100 | [diff] [blame] | 477 | bool ret; |
| 478 | ret = drm_fb_helper_force_kernel_mode(); |
| 479 | if (ret == true) |
| 480 | DRM_ERROR("Failed to restore crtc configuration\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 481 | } |
| 482 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); |
| 483 | |
Dmitry Torokhov | 1495cc9 | 2010-08-17 21:15:46 -0700 | [diff] [blame] | 484 | static void drm_fb_helper_sysrq(int dummy1) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 485 | { |
| 486 | schedule_work(&drm_fb_helper_restore_work); |
| 487 | } |
| 488 | |
| 489 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { |
| 490 | .handler = drm_fb_helper_sysrq, |
| 491 | .help_msg = "force-fb(V)", |
| 492 | .action_msg = "Restore framebuffer console", |
| 493 | }; |
Randy Dunlap | b8c40d6 | 2010-03-25 18:29:05 +0000 | [diff] [blame] | 494 | #else |
| 495 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; |
Mikael Pettersson | bea1d35 | 2009-09-28 18:26:25 +0200 | [diff] [blame] | 496 | #endif |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 497 | |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 498 | static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 499 | { |
| 500 | struct drm_fb_helper *fb_helper = info->par; |
| 501 | struct drm_device *dev = fb_helper->dev; |
| 502 | struct drm_crtc *crtc; |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 503 | struct drm_connector *connector; |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 504 | int i, j; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 505 | |
| 506 | /* |
Daniel Vetter | 1b1d539 | 2013-01-24 16:42:07 +0100 | [diff] [blame] | 507 | * fbdev->blank can be called from irq context in case of a panic. |
| 508 | * Since we already have our own special panic handler which will |
| 509 | * restore the fbdev console mode completely, just bail out early. |
| 510 | */ |
| 511 | if (oops_in_progress) |
| 512 | return; |
| 513 | |
| 514 | /* |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 515 | * For each CRTC in this fb, turn the connectors on/off. |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 516 | */ |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 517 | drm_modeset_lock_all(dev); |
Daniel Vetter | 20c60c3 | 2012-12-17 12:13:23 +0100 | [diff] [blame] | 518 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 519 | drm_modeset_unlock_all(dev); |
| 520 | return; |
| 521 | } |
| 522 | |
Jesse Barnes | e87b2c4 | 2009-09-17 18:14:41 -0700 | [diff] [blame] | 523 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 524 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 525 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 526 | if (!crtc->enabled) |
| 527 | continue; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 528 | |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 529 | /* Walk the connectors & encoders on this fb turning them on/off */ |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 530 | for (j = 0; j < fb_helper->connector_count; j++) { |
| 531 | connector = fb_helper->connector_info[j]->connector; |
Daniel Vetter | e04190e | 2012-09-07 10:14:52 +0200 | [diff] [blame] | 532 | connector->funcs->dpms(connector, dpms_mode); |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 533 | drm_object_property_set_value(&connector->base, |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 534 | dev->mode_config.dpms_property, dpms_mode); |
Jesse Barnes | 023eb57 | 2010-07-02 10:48:08 -0700 | [diff] [blame] | 535 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 536 | } |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 537 | drm_modeset_unlock_all(dev); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 538 | } |
| 539 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 540 | /** |
| 541 | * drm_fb_helper_blank - implementation for ->fb_blank |
| 542 | * @blank: desired blanking state |
| 543 | * @info: fbdev registered by the helper |
| 544 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 545 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
| 546 | { |
| 547 | switch (blank) { |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 548 | /* Display: On; HSync: On, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 549 | case FB_BLANK_UNBLANK: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 550 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 551 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 552 | /* Display: Off; HSync: On, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 553 | case FB_BLANK_NORMAL: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 554 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 555 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 556 | /* Display: Off; HSync: Off, VSync: On */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 557 | case FB_BLANK_HSYNC_SUSPEND: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 558 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 559 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 560 | /* Display: Off; HSync: On, VSync: Off */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 561 | case FB_BLANK_VSYNC_SUSPEND: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 562 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 563 | break; |
James Simmons | 731b5a1 | 2009-10-29 20:39:07 +0000 | [diff] [blame] | 564 | /* Display: Off; HSync: Off, VSync: Off */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 565 | case FB_BLANK_POWERDOWN: |
Sascha Hauer | 3a8148c | 2012-02-01 11:38:24 +0100 | [diff] [blame] | 566 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | EXPORT_SYMBOL(drm_fb_helper_blank); |
| 572 | |
| 573 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) |
| 574 | { |
| 575 | int i; |
| 576 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 577 | for (i = 0; i < helper->connector_count; i++) |
| 578 | kfree(helper->connector_info[i]); |
| 579 | kfree(helper->connector_info); |
Sascha Hauer | a1b7736 | 2012-02-01 11:38:22 +0100 | [diff] [blame] | 580 | for (i = 0; i < helper->crtc_count; i++) { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 581 | kfree(helper->crtc_info[i].mode_set.connectors); |
Sascha Hauer | a1b7736 | 2012-02-01 11:38:22 +0100 | [diff] [blame] | 582 | if (helper->crtc_info[i].mode_set.mode) |
| 583 | drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode); |
| 584 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 585 | kfree(helper->crtc_info); |
| 586 | } |
| 587 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 588 | /** |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 589 | * drm_fb_helper_prepare - setup a drm_fb_helper structure |
| 590 | * @dev: DRM device |
| 591 | * @helper: driver-allocated fbdev helper structure to set up |
| 592 | * @funcs: pointer to structure of functions associate with this helper |
| 593 | * |
| 594 | * Sets up the bare minimum to make the framebuffer helper usable. This is |
| 595 | * useful to implement race-free initialization of the polling helpers. |
| 596 | */ |
| 597 | void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, |
| 598 | const struct drm_fb_helper_funcs *funcs) |
| 599 | { |
| 600 | INIT_LIST_HEAD(&helper->kernel_fb_list); |
| 601 | helper->funcs = funcs; |
| 602 | helper->dev = dev; |
| 603 | } |
| 604 | EXPORT_SYMBOL(drm_fb_helper_prepare); |
| 605 | |
| 606 | /** |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 607 | * drm_fb_helper_init - initialize a drm_fb_helper structure |
| 608 | * @dev: drm device |
| 609 | * @fb_helper: driver-allocated fbdev helper structure to initialize |
| 610 | * @crtc_count: maximum number of crtcs to support in this fbdev emulation |
| 611 | * @max_conn_count: max connector count |
| 612 | * |
| 613 | * This allocates the structures for the fbdev helper with the given limits. |
| 614 | * Note that this won't yet touch the hardware (through the driver interfaces) |
| 615 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() |
| 616 | * to allow driver writes more control over the exact init sequence. |
| 617 | * |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 618 | * Drivers must call drm_fb_helper_prepare() before calling this function. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 619 | * |
| 620 | * RETURNS: |
| 621 | * Zero if everything went ok, nonzero otherwise. |
| 622 | */ |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 623 | int drm_fb_helper_init(struct drm_device *dev, |
| 624 | struct drm_fb_helper *fb_helper, |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 625 | int crtc_count, int max_conn_count) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 626 | { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 627 | struct drm_crtc *crtc; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 628 | int i; |
| 629 | |
Xiubo Li | 04cfe97 | 2014-03-10 09:33:58 +0800 | [diff] [blame] | 630 | if (!max_conn_count) |
| 631 | return -EINVAL; |
| 632 | |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 633 | fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); |
| 634 | if (!fb_helper->crtc_info) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 635 | return -ENOMEM; |
| 636 | |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 637 | fb_helper->crtc_count = crtc_count; |
| 638 | fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); |
| 639 | if (!fb_helper->connector_info) { |
| 640 | kfree(fb_helper->crtc_info); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 641 | return -ENOMEM; |
| 642 | } |
Dave Airlie | 65c2a89 | 2014-06-05 14:01:30 +1000 | [diff] [blame] | 643 | fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 644 | fb_helper->connector_count = 0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 645 | |
| 646 | for (i = 0; i < crtc_count; i++) { |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 647 | fb_helper->crtc_info[i].mode_set.connectors = |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 648 | kcalloc(max_conn_count, |
| 649 | sizeof(struct drm_connector *), |
| 650 | GFP_KERNEL); |
| 651 | |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 652 | if (!fb_helper->crtc_info[i].mode_set.connectors) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 653 | goto out_free; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 654 | fb_helper->crtc_info[i].mode_set.num_connectors = 0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | i = 0; |
| 658 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 659 | fb_helper->crtc_info[i].mode_set.crtc = crtc; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 660 | i++; |
| 661 | } |
Sascha Hauer | e9ad318 | 2012-02-01 11:38:25 +0100 | [diff] [blame] | 662 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 663 | return 0; |
| 664 | out_free: |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 665 | drm_fb_helper_crtc_free(fb_helper); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 666 | return -ENOMEM; |
| 667 | } |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 668 | EXPORT_SYMBOL(drm_fb_helper_init); |
| 669 | |
| 670 | void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) |
| 671 | { |
| 672 | if (!list_empty(&fb_helper->kernel_fb_list)) { |
| 673 | list_del(&fb_helper->kernel_fb_list); |
| 674 | if (list_empty(&kernel_fb_helper_list)) { |
Sachin Kamat | d56b1b9 | 2012-11-15 03:43:29 +0000 | [diff] [blame] | 675 | pr_info("drm: unregistered panic notifier\n"); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 676 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 677 | &paniced); |
| 678 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | drm_fb_helper_crtc_free(fb_helper); |
| 683 | |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 684 | } |
| 685 | EXPORT_SYMBOL(drm_fb_helper_fini); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 686 | |
Dave Airlie | c850cb7 | 2009-10-23 18:49:03 +1000 | [diff] [blame] | 687 | static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 688 | u16 blue, u16 regno, struct fb_info *info) |
| 689 | { |
| 690 | struct drm_fb_helper *fb_helper = info->par; |
| 691 | struct drm_framebuffer *fb = fb_helper->fb; |
| 692 | int pindex; |
| 693 | |
Dave Airlie | c850cb7 | 2009-10-23 18:49:03 +1000 | [diff] [blame] | 694 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) { |
| 695 | u32 *palette; |
| 696 | u32 value; |
| 697 | /* place color in psuedopalette */ |
| 698 | if (regno > 16) |
| 699 | return -EINVAL; |
| 700 | palette = (u32 *)info->pseudo_palette; |
| 701 | red >>= (16 - info->var.red.length); |
| 702 | green >>= (16 - info->var.green.length); |
| 703 | blue >>= (16 - info->var.blue.length); |
| 704 | value = (red << info->var.red.offset) | |
| 705 | (green << info->var.green.offset) | |
| 706 | (blue << info->var.blue.offset); |
Rob Clark | 9da12b6a | 2011-02-16 02:45:51 +0000 | [diff] [blame] | 707 | if (info->var.transp.length > 0) { |
| 708 | u32 mask = (1 << info->var.transp.length) - 1; |
| 709 | mask <<= info->var.transp.offset; |
| 710 | value |= mask; |
| 711 | } |
Dave Airlie | c850cb7 | 2009-10-23 18:49:03 +1000 | [diff] [blame] | 712 | palette[regno] = value; |
| 713 | return 0; |
| 714 | } |
| 715 | |
Ville Syrjälä | 04c0c56 | 2013-05-27 20:19:57 +0300 | [diff] [blame] | 716 | /* |
| 717 | * The driver really shouldn't advertise pseudo/directcolor |
| 718 | * visuals if it can't deal with the palette. |
| 719 | */ |
| 720 | if (WARN_ON(!fb_helper->funcs->gamma_set || |
| 721 | !fb_helper->funcs->gamma_get)) |
| 722 | return -EINVAL; |
| 723 | |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 724 | pindex = regno; |
| 725 | |
| 726 | if (fb->bits_per_pixel == 16) { |
| 727 | pindex = regno << 3; |
| 728 | |
| 729 | if (fb->depth == 16 && regno > 63) |
Dave Airlie | c850cb7 | 2009-10-23 18:49:03 +1000 | [diff] [blame] | 730 | return -EINVAL; |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 731 | if (fb->depth == 15 && regno > 31) |
Dave Airlie | c850cb7 | 2009-10-23 18:49:03 +1000 | [diff] [blame] | 732 | return -EINVAL; |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 733 | |
| 734 | if (fb->depth == 16) { |
| 735 | u16 r, g, b; |
| 736 | int i; |
| 737 | if (regno < 32) { |
| 738 | for (i = 0; i < 8; i++) |
| 739 | fb_helper->funcs->gamma_set(crtc, red, |
| 740 | green, blue, pindex + i); |
| 741 | } |
| 742 | |
| 743 | fb_helper->funcs->gamma_get(crtc, &r, |
| 744 | &g, &b, |
| 745 | pindex >> 1); |
| 746 | |
| 747 | for (i = 0; i < 4; i++) |
| 748 | fb_helper->funcs->gamma_set(crtc, r, |
| 749 | green, b, |
| 750 | (pindex >> 1) + i); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if (fb->depth != 16) |
| 755 | fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); |
Dave Airlie | c850cb7 | 2009-10-23 18:49:03 +1000 | [diff] [blame] | 756 | return 0; |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 757 | } |
| 758 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 759 | /** |
| 760 | * drm_fb_helper_setcmap - implementation for ->fb_setcmap |
| 761 | * @cmap: cmap to set |
| 762 | * @info: fbdev registered by the helper |
| 763 | */ |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 764 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) |
| 765 | { |
| 766 | struct drm_fb_helper *fb_helper = info->par; |
Ville Syrjälä | 8391a3d | 2013-05-27 20:19:56 +0300 | [diff] [blame] | 767 | struct drm_device *dev = fb_helper->dev; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 768 | struct drm_crtc_helper_funcs *crtc_funcs; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 769 | u16 *red, *green, *blue, *transp; |
| 770 | struct drm_crtc *crtc; |
roel | 062ac62 | 2011-03-07 18:00:34 +0100 | [diff] [blame] | 771 | int i, j, rc = 0; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 772 | int start; |
| 773 | |
Rui Wang | 9aa609e | 2014-12-15 11:28:26 -0800 | [diff] [blame] | 774 | if (__drm_modeset_lock_all(dev, !!oops_in_progress)) { |
| 775 | return -EBUSY; |
| 776 | } |
Ville Syrjälä | 8391a3d | 2013-05-27 20:19:56 +0300 | [diff] [blame] | 777 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 778 | drm_modeset_unlock_all(dev); |
| 779 | return -EBUSY; |
| 780 | } |
| 781 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 782 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 783 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
| 784 | crtc_funcs = crtc->helper_private; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 785 | |
| 786 | red = cmap->red; |
| 787 | green = cmap->green; |
| 788 | blue = cmap->blue; |
| 789 | transp = cmap->transp; |
| 790 | start = cmap->start; |
| 791 | |
roel | 062ac62 | 2011-03-07 18:00:34 +0100 | [diff] [blame] | 792 | for (j = 0; j < cmap->len; j++) { |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 793 | u16 hred, hgreen, hblue, htransp = 0xffff; |
| 794 | |
| 795 | hred = *red++; |
| 796 | hgreen = *green++; |
| 797 | hblue = *blue++; |
| 798 | |
| 799 | if (transp) |
| 800 | htransp = *transp++; |
| 801 | |
Dave Airlie | c850cb7 | 2009-10-23 18:49:03 +1000 | [diff] [blame] | 802 | rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); |
| 803 | if (rc) |
Ville Syrjälä | 8391a3d | 2013-05-27 20:19:56 +0300 | [diff] [blame] | 804 | goto out; |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 805 | } |
Ville Syrjälä | 04c0c56 | 2013-05-27 20:19:57 +0300 | [diff] [blame] | 806 | if (crtc_funcs->load_lut) |
| 807 | crtc_funcs->load_lut(crtc); |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 808 | } |
Ville Syrjälä | 8391a3d | 2013-05-27 20:19:56 +0300 | [diff] [blame] | 809 | out: |
| 810 | drm_modeset_unlock_all(dev); |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 811 | return rc; |
| 812 | } |
| 813 | EXPORT_SYMBOL(drm_fb_helper_setcmap); |
| 814 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 815 | /** |
| 816 | * drm_fb_helper_check_var - implementation for ->fb_check_var |
| 817 | * @var: screeninfo to check |
| 818 | * @info: fbdev registered by the helper |
| 819 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 820 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
| 821 | struct fb_info *info) |
| 822 | { |
| 823 | struct drm_fb_helper *fb_helper = info->par; |
| 824 | struct drm_framebuffer *fb = fb_helper->fb; |
| 825 | int depth; |
| 826 | |
Jason Wessel | f90ebd9 | 2010-08-05 09:22:32 -0500 | [diff] [blame] | 827 | if (var->pixclock != 0 || in_dbg_master()) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 828 | return -EINVAL; |
| 829 | |
| 830 | /* Need to resize the fb object !!! */ |
Chris Wilson | 62fb376 | 2012-03-26 21:15:53 +0100 | [diff] [blame] | 831 | if (var->bits_per_pixel > fb->bits_per_pixel || |
| 832 | var->xres > fb->width || var->yres > fb->height || |
| 833 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { |
Dave Airlie | 509c7d8 | 2010-01-08 09:27:08 +1000 | [diff] [blame] | 834 | DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " |
Chris Wilson | 62fb376 | 2012-03-26 21:15:53 +0100 | [diff] [blame] | 835 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
| 836 | var->xres, var->yres, var->bits_per_pixel, |
| 837 | var->xres_virtual, var->yres_virtual, |
Dave Airlie | 509c7d8 | 2010-01-08 09:27:08 +1000 | [diff] [blame] | 838 | fb->width, fb->height, fb->bits_per_pixel); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 839 | return -EINVAL; |
| 840 | } |
| 841 | |
| 842 | switch (var->bits_per_pixel) { |
| 843 | case 16: |
| 844 | depth = (var->green.length == 6) ? 16 : 15; |
| 845 | break; |
| 846 | case 32: |
| 847 | depth = (var->transp.length > 0) ? 32 : 24; |
| 848 | break; |
| 849 | default: |
| 850 | depth = var->bits_per_pixel; |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | switch (depth) { |
| 855 | case 8: |
| 856 | var->red.offset = 0; |
| 857 | var->green.offset = 0; |
| 858 | var->blue.offset = 0; |
| 859 | var->red.length = 8; |
| 860 | var->green.length = 8; |
| 861 | var->blue.length = 8; |
| 862 | var->transp.length = 0; |
| 863 | var->transp.offset = 0; |
| 864 | break; |
| 865 | case 15: |
| 866 | var->red.offset = 10; |
| 867 | var->green.offset = 5; |
| 868 | var->blue.offset = 0; |
| 869 | var->red.length = 5; |
| 870 | var->green.length = 5; |
| 871 | var->blue.length = 5; |
| 872 | var->transp.length = 1; |
| 873 | var->transp.offset = 15; |
| 874 | break; |
| 875 | case 16: |
| 876 | var->red.offset = 11; |
| 877 | var->green.offset = 5; |
| 878 | var->blue.offset = 0; |
| 879 | var->red.length = 5; |
| 880 | var->green.length = 6; |
| 881 | var->blue.length = 5; |
| 882 | var->transp.length = 0; |
| 883 | var->transp.offset = 0; |
| 884 | break; |
| 885 | case 24: |
| 886 | var->red.offset = 16; |
| 887 | var->green.offset = 8; |
| 888 | var->blue.offset = 0; |
| 889 | var->red.length = 8; |
| 890 | var->green.length = 8; |
| 891 | var->blue.length = 8; |
| 892 | var->transp.length = 0; |
| 893 | var->transp.offset = 0; |
| 894 | break; |
| 895 | case 32: |
| 896 | var->red.offset = 16; |
| 897 | var->green.offset = 8; |
| 898 | var->blue.offset = 0; |
| 899 | var->red.length = 8; |
| 900 | var->green.length = 8; |
| 901 | var->blue.length = 8; |
| 902 | var->transp.length = 8; |
| 903 | var->transp.offset = 24; |
| 904 | break; |
| 905 | default: |
| 906 | return -EINVAL; |
| 907 | } |
| 908 | return 0; |
| 909 | } |
| 910 | EXPORT_SYMBOL(drm_fb_helper_check_var); |
| 911 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 912 | /** |
| 913 | * drm_fb_helper_set_par - implementation for ->fb_set_par |
| 914 | * @info: fbdev registered by the helper |
| 915 | * |
| 916 | * This will let fbcon do the mode init and is called at initialization time by |
| 917 | * the fbdev core when registering the driver, and later on through the hotplug |
| 918 | * callback. |
| 919 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 920 | int drm_fb_helper_set_par(struct fb_info *info) |
| 921 | { |
| 922 | struct drm_fb_helper *fb_helper = info->par; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 923 | struct fb_var_screeninfo *var = &info->var; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 924 | |
Clemens Ladisch | 5349ef3 | 2009-11-04 09:42:52 +0100 | [diff] [blame] | 925 | if (var->pixclock != 0) { |
Pavel Roskin | 172e91f | 2010-02-11 14:31:32 +1000 | [diff] [blame] | 926 | DRM_ERROR("PIXEL CLOCK SET\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 927 | return -EINVAL; |
| 928 | } |
| 929 | |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 930 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 931 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 932 | return 0; |
| 933 | } |
| 934 | EXPORT_SYMBOL(drm_fb_helper_set_par); |
| 935 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 936 | /** |
| 937 | * drm_fb_helper_pan_display - implementation for ->fb_pan_display |
| 938 | * @var: updated screen information |
| 939 | * @info: fbdev registered by the helper |
| 940 | */ |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 941 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, |
| 942 | struct fb_info *info) |
| 943 | { |
| 944 | struct drm_fb_helper *fb_helper = info->par; |
| 945 | struct drm_device *dev = fb_helper->dev; |
| 946 | struct drm_mode_set *modeset; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 947 | int ret = 0; |
| 948 | int i; |
| 949 | |
Rui Wang | 9aa609e | 2014-12-15 11:28:26 -0800 | [diff] [blame] | 950 | if (__drm_modeset_lock_all(dev, !!oops_in_progress)) { |
| 951 | return -EBUSY; |
| 952 | } |
Daniel Vetter | 20c60c3 | 2012-12-17 12:13:23 +0100 | [diff] [blame] | 953 | if (!drm_fb_helper_is_bound(fb_helper)) { |
| 954 | drm_modeset_unlock_all(dev); |
| 955 | return -EBUSY; |
| 956 | } |
| 957 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 958 | for (i = 0; i < fb_helper->crtc_count; i++) { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 959 | modeset = &fb_helper->crtc_info[i].mode_set; |
| 960 | |
| 961 | modeset->x = var->xoffset; |
| 962 | modeset->y = var->yoffset; |
| 963 | |
| 964 | if (modeset->num_connectors) { |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 965 | ret = drm_mode_set_config_internal(modeset); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 966 | if (!ret) { |
| 967 | info->var.xoffset = var->xoffset; |
| 968 | info->var.yoffset = var->yoffset; |
| 969 | } |
| 970 | } |
| 971 | } |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 972 | drm_modeset_unlock_all(dev); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 973 | return ret; |
| 974 | } |
| 975 | EXPORT_SYMBOL(drm_fb_helper_pan_display); |
| 976 | |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 977 | /* |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 978 | * Allocates the backing storage and sets up the fbdev info structure through |
| 979 | * the ->fb_probe callback and then registers the fbdev and sets up the panic |
| 980 | * notifier. |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 981 | */ |
Daniel Vetter | de1ace5 | 2013-01-20 21:50:49 +0100 | [diff] [blame] | 982 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
| 983 | int preferred_bpp) |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 984 | { |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 985 | int ret = 0; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 986 | int crtc_count = 0; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 987 | int i; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 988 | struct fb_info *info; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 989 | struct drm_fb_helper_surface_size sizes; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 990 | int gamma_size = 0; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 991 | |
| 992 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); |
| 993 | sizes.surface_depth = 24; |
| 994 | sizes.surface_bpp = 32; |
| 995 | sizes.fb_width = (unsigned)-1; |
| 996 | sizes.fb_height = (unsigned)-1; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 997 | |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 998 | /* if driver picks 8 or 16 by default use that |
| 999 | for both depth/bpp */ |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1000 | if (preferred_bpp != sizes.surface_bpp) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1001 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1002 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1003 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1004 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1005 | 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] | 1006 | struct drm_cmdline_mode *cmdline_mode; |
Dave Airlie | 8ef8678 | 2009-09-26 06:39:00 +1000 | [diff] [blame] | 1007 | |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1008 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1009 | |
| 1010 | if (cmdline_mode->bpp_specified) { |
| 1011 | switch (cmdline_mode->bpp) { |
| 1012 | case 8: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1013 | sizes.surface_depth = sizes.surface_bpp = 8; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1014 | break; |
| 1015 | case 15: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1016 | sizes.surface_depth = 15; |
| 1017 | sizes.surface_bpp = 16; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1018 | break; |
| 1019 | case 16: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1020 | sizes.surface_depth = sizes.surface_bpp = 16; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1021 | break; |
| 1022 | case 24: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1023 | sizes.surface_depth = sizes.surface_bpp = 24; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1024 | break; |
| 1025 | case 32: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1026 | sizes.surface_depth = 24; |
| 1027 | sizes.surface_bpp = 32; |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 1028 | break; |
| 1029 | } |
| 1030 | break; |
| 1031 | } |
| 1032 | } |
| 1033 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1034 | crtc_count = 0; |
| 1035 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1036 | struct drm_display_mode *desired_mode; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1037 | int x, y; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1038 | desired_mode = fb_helper->crtc_info[i].desired_mode; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1039 | x = fb_helper->crtc_info[i].x; |
| 1040 | y = fb_helper->crtc_info[i].y; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1041 | if (desired_mode) { |
| 1042 | if (gamma_size == 0) |
| 1043 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1044 | if (desired_mode->hdisplay + x < sizes.fb_width) |
| 1045 | sizes.fb_width = desired_mode->hdisplay + x; |
| 1046 | if (desired_mode->vdisplay + y < sizes.fb_height) |
| 1047 | sizes.fb_height = desired_mode->vdisplay + y; |
| 1048 | if (desired_mode->hdisplay + x > sizes.surface_width) |
| 1049 | sizes.surface_width = desired_mode->hdisplay + x; |
| 1050 | if (desired_mode->vdisplay + y > sizes.surface_height) |
| 1051 | sizes.surface_height = desired_mode->vdisplay + y; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1052 | crtc_count++; |
| 1053 | } |
| 1054 | } |
| 1055 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1056 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1057 | /* hmm everyone went away - assume VGA cable just fell out |
| 1058 | and will come back later. */ |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 1059 | DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); |
Dave Airlie | 19b4b44 | 2010-03-30 05:34:16 +0000 | [diff] [blame] | 1060 | sizes.fb_width = sizes.surface_width = 1024; |
| 1061 | sizes.fb_height = sizes.surface_height = 768; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1062 | } |
| 1063 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1064 | /* push down into drivers */ |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1065 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
| 1066 | if (ret < 0) |
| 1067 | return ret; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1068 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1069 | info = fb_helper->fbdev; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1070 | |
Daniel Vetter | 7e53f3a | 2013-01-21 10:52:17 +0100 | [diff] [blame] | 1071 | /* |
| 1072 | * Set the fb pointer - usually drm_setup_crtcs does this for hotplug |
| 1073 | * events, but at init time drm_setup_crtcs needs to be called before |
| 1074 | * the fb is allocated (since we need to figure out the desired size of |
| 1075 | * the fb before we can allocate it ...). Hence we need to fix things up |
| 1076 | * here again. |
| 1077 | */ |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1078 | for (i = 0; i < fb_helper->crtc_count; i++) |
Daniel Vetter | 7e53f3a | 2013-01-21 10:52:17 +0100 | [diff] [blame] | 1079 | if (fb_helper->crtc_info[i].mode_set.num_connectors) |
| 1080 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; |
| 1081 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1082 | |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1083 | info->var.pixclock = 0; |
| 1084 | if (register_framebuffer(info) < 0) |
| 1085 | return -EINVAL; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1086 | |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1087 | dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", |
| 1088 | info->node, info->fix.id); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1089 | |
| 1090 | /* Switch back to kernel console on panic */ |
| 1091 | /* multi card linked list maybe */ |
| 1092 | if (list_empty(&kernel_fb_helper_list)) { |
Sachin Kamat | d56b1b9 | 2012-11-15 03:43:29 +0000 | [diff] [blame] | 1093 | dev_info(fb_helper->dev->dev, "registered panic notifier\n"); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1094 | atomic_notifier_chain_register(&panic_notifier_list, |
| 1095 | &paniced); |
| 1096 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
| 1097 | } |
Daniel Vetter | 8acf658 | 2013-01-21 23:38:37 +0100 | [diff] [blame] | 1098 | |
| 1099 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1100 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1101 | return 0; |
| 1102 | } |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1103 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1104 | /** |
| 1105 | * drm_fb_helper_fill_fix - initializes fixed fbdev information |
| 1106 | * @info: fbdev registered by the helper |
| 1107 | * @pitch: desired pitch |
| 1108 | * @depth: desired depth |
| 1109 | * |
| 1110 | * Helper to fill in the fixed fbdev information useful for a non-accelerated |
| 1111 | * fbdev emulations. Drivers which support acceleration methods which impose |
| 1112 | * additional constraints need to set up their own limits. |
| 1113 | * |
| 1114 | * Drivers should call this (or their equivalent setup code) from their |
| 1115 | * ->fb_probe callback. |
| 1116 | */ |
Dave Airlie | 3632ef8 | 2011-01-15 09:27:00 +1000 | [diff] [blame] | 1117 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
| 1118 | uint32_t depth) |
| 1119 | { |
| 1120 | info->fix.type = FB_TYPE_PACKED_PIXELS; |
| 1121 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : |
| 1122 | FB_VISUAL_TRUECOLOR; |
| 1123 | info->fix.mmio_start = 0; |
| 1124 | info->fix.mmio_len = 0; |
| 1125 | info->fix.type_aux = 0; |
| 1126 | info->fix.xpanstep = 1; /* doing it in hw */ |
| 1127 | info->fix.ypanstep = 1; /* doing it in hw */ |
| 1128 | info->fix.ywrapstep = 0; |
| 1129 | info->fix.accel = FB_ACCEL_NONE; |
Dave Airlie | 3632ef8 | 2011-01-15 09:27:00 +1000 | [diff] [blame] | 1130 | |
| 1131 | info->fix.line_length = pitch; |
| 1132 | return; |
| 1133 | } |
| 1134 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); |
| 1135 | |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1136 | /** |
| 1137 | * drm_fb_helper_fill_var - initalizes variable fbdev information |
| 1138 | * @info: fbdev instance to set up |
| 1139 | * @fb_helper: fb helper instance to use as template |
| 1140 | * @fb_width: desired fb width |
| 1141 | * @fb_height: desired fb height |
| 1142 | * |
| 1143 | * Sets up the variable fbdev metainformation from the given fb helper instance |
| 1144 | * and the drm framebuffer allocated in fb_helper->fb. |
| 1145 | * |
| 1146 | * Drivers should call this (or their equivalent setup code) from their |
| 1147 | * ->fb_probe callback after having allocated the fbdev backing |
| 1148 | * storage framebuffer. |
| 1149 | */ |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1150 | 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] | 1151 | uint32_t fb_width, uint32_t fb_height) |
| 1152 | { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1153 | struct drm_framebuffer *fb = fb_helper->fb; |
| 1154 | info->pseudo_palette = fb_helper->pseudo_palette; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1155 | info->var.xres_virtual = fb->width; |
| 1156 | info->var.yres_virtual = fb->height; |
| 1157 | info->var.bits_per_pixel = fb->bits_per_pixel; |
James Simmons | 57084d0 | 2010-12-20 19:10:39 +0000 | [diff] [blame] | 1158 | info->var.accel_flags = FB_ACCELF_TEXT; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 1159 | info->var.xoffset = 0; |
| 1160 | info->var.yoffset = 0; |
| 1161 | info->var.activate = FB_ACTIVATE_NOW; |
| 1162 | info->var.height = -1; |
| 1163 | info->var.width = -1; |
| 1164 | |
| 1165 | switch (fb->depth) { |
| 1166 | case 8: |
| 1167 | info->var.red.offset = 0; |
| 1168 | info->var.green.offset = 0; |
| 1169 | info->var.blue.offset = 0; |
| 1170 | info->var.red.length = 8; /* 8bit DAC */ |
| 1171 | info->var.green.length = 8; |
| 1172 | info->var.blue.length = 8; |
| 1173 | info->var.transp.offset = 0; |
| 1174 | info->var.transp.length = 0; |
| 1175 | break; |
| 1176 | case 15: |
| 1177 | info->var.red.offset = 10; |
| 1178 | info->var.green.offset = 5; |
| 1179 | info->var.blue.offset = 0; |
| 1180 | info->var.red.length = 5; |
| 1181 | info->var.green.length = 5; |
| 1182 | info->var.blue.length = 5; |
| 1183 | info->var.transp.offset = 15; |
| 1184 | info->var.transp.length = 1; |
| 1185 | break; |
| 1186 | case 16: |
| 1187 | info->var.red.offset = 11; |
| 1188 | info->var.green.offset = 5; |
| 1189 | info->var.blue.offset = 0; |
| 1190 | info->var.red.length = 5; |
| 1191 | info->var.green.length = 6; |
| 1192 | info->var.blue.length = 5; |
| 1193 | info->var.transp.offset = 0; |
| 1194 | break; |
| 1195 | case 24: |
| 1196 | info->var.red.offset = 16; |
| 1197 | info->var.green.offset = 8; |
| 1198 | info->var.blue.offset = 0; |
| 1199 | info->var.red.length = 8; |
| 1200 | info->var.green.length = 8; |
| 1201 | info->var.blue.length = 8; |
| 1202 | info->var.transp.offset = 0; |
| 1203 | info->var.transp.length = 0; |
| 1204 | break; |
| 1205 | case 32: |
| 1206 | info->var.red.offset = 16; |
| 1207 | info->var.green.offset = 8; |
| 1208 | info->var.blue.offset = 0; |
| 1209 | info->var.red.length = 8; |
| 1210 | info->var.green.length = 8; |
| 1211 | info->var.blue.length = 8; |
| 1212 | info->var.transp.offset = 24; |
| 1213 | info->var.transp.length = 8; |
| 1214 | break; |
| 1215 | default: |
| 1216 | break; |
| 1217 | } |
| 1218 | |
| 1219 | info->var.xres = fb_width; |
| 1220 | info->var.yres = fb_height; |
| 1221 | } |
| 1222 | EXPORT_SYMBOL(drm_fb_helper_fill_var); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1223 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1224 | static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, |
| 1225 | uint32_t maxX, |
| 1226 | uint32_t maxY) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1227 | { |
| 1228 | struct drm_connector *connector; |
| 1229 | int count = 0; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1230 | int i; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1231 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1232 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1233 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1234 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
| 1235 | } |
| 1236 | |
| 1237 | return count; |
| 1238 | } |
| 1239 | |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 1240 | 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] | 1241 | { |
| 1242 | struct drm_display_mode *mode; |
| 1243 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1244 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
Daniel Vetter | 9d3de13 | 2014-01-23 16:27:56 +0100 | [diff] [blame] | 1245 | if (mode->hdisplay > width || |
| 1246 | mode->vdisplay > height) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1247 | continue; |
| 1248 | if (mode->type & DRM_MODE_TYPE_PREFERRED) |
| 1249 | return mode; |
| 1250 | } |
| 1251 | return NULL; |
| 1252 | } |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 1253 | EXPORT_SYMBOL(drm_has_preferred_mode); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1254 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1255 | 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] | 1256 | { |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1257 | return fb_connector->connector->cmdline_mode.specified; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 1260 | struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1261 | int width, int height) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1262 | { |
Chris Wilson | 1794d25 | 2011-04-17 07:43:32 +0100 | [diff] [blame] | 1263 | struct drm_cmdline_mode *cmdline_mode; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1264 | struct drm_display_mode *mode = NULL; |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 1265 | bool prefer_non_interlace; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1266 | |
Chris Wilson | eaf99c7 | 2014-08-06 10:08:32 +0200 | [diff] [blame] | 1267 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1268 | if (cmdline_mode->specified == false) |
| 1269 | return mode; |
| 1270 | |
| 1271 | /* attempt to find a matching mode in the list of modes |
| 1272 | * we have gotten so far, if not add a CVT mode that conforms |
| 1273 | */ |
| 1274 | if (cmdline_mode->rb || cmdline_mode->margins) |
| 1275 | goto create_mode; |
| 1276 | |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 1277 | prefer_non_interlace = !cmdline_mode->interlace; |
| 1278 | again: |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1279 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1280 | /* check width/height */ |
| 1281 | if (mode->hdisplay != cmdline_mode->xres || |
| 1282 | mode->vdisplay != cmdline_mode->yres) |
| 1283 | continue; |
| 1284 | |
| 1285 | if (cmdline_mode->refresh_specified) { |
| 1286 | if (mode->vrefresh != cmdline_mode->refresh) |
| 1287 | continue; |
| 1288 | } |
| 1289 | |
| 1290 | if (cmdline_mode->interlace) { |
| 1291 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) |
| 1292 | continue; |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 1293 | } else if (prefer_non_interlace) { |
| 1294 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) |
| 1295 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1296 | } |
| 1297 | return mode; |
| 1298 | } |
| 1299 | |
Takashi Iwai | c683f42 | 2014-03-19 14:53:13 +0100 | [diff] [blame] | 1300 | if (prefer_non_interlace) { |
| 1301 | prefer_non_interlace = false; |
| 1302 | goto again; |
| 1303 | } |
| 1304 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1305 | create_mode: |
Chris Wilson | 1794d25 | 2011-04-17 07:43:32 +0100 | [diff] [blame] | 1306 | mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, |
| 1307 | cmdline_mode); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1308 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1309 | return mode; |
| 1310 | } |
Jesse Barnes | 2f1046f | 2014-02-12 12:26:24 -0800 | [diff] [blame] | 1311 | EXPORT_SYMBOL(drm_pick_cmdline_mode); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1312 | |
| 1313 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) |
| 1314 | { |
| 1315 | bool enable; |
| 1316 | |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1317 | if (strict) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1318 | enable = connector->status == connector_status_connected; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1319 | else |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1320 | enable = connector->status != connector_status_disconnected; |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1321 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1322 | return enable; |
| 1323 | } |
| 1324 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1325 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
| 1326 | bool *enabled) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1327 | { |
| 1328 | bool any_enabled = false; |
| 1329 | struct drm_connector *connector; |
| 1330 | int i = 0; |
| 1331 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1332 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1333 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1334 | enabled[i] = drm_connector_enabled(connector, true); |
| 1335 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, |
| 1336 | enabled[i] ? "yes" : "no"); |
| 1337 | any_enabled |= enabled[i]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | if (any_enabled) |
| 1341 | return; |
| 1342 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1343 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1344 | connector = fb_helper->connector_info[i]->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1345 | enabled[i] = drm_connector_enabled(connector, false); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 1349 | static bool drm_target_cloned(struct drm_fb_helper *fb_helper, |
| 1350 | struct drm_display_mode **modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1351 | struct drm_fb_offset *offsets, |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 1352 | bool *enabled, int width, int height) |
| 1353 | { |
| 1354 | int count, i, j; |
| 1355 | bool can_clone = false; |
| 1356 | struct drm_fb_helper_connector *fb_helper_conn; |
| 1357 | struct drm_display_mode *dmt_mode, *mode; |
| 1358 | |
| 1359 | /* only contemplate cloning in the single crtc case */ |
| 1360 | if (fb_helper->crtc_count > 1) |
| 1361 | return false; |
| 1362 | |
| 1363 | count = 0; |
| 1364 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1365 | if (enabled[i]) |
| 1366 | count++; |
| 1367 | } |
| 1368 | |
| 1369 | /* only contemplate cloning if more than one connector is enabled */ |
| 1370 | if (count <= 1) |
| 1371 | return false; |
| 1372 | |
| 1373 | /* check the command line or if nothing common pick 1024x768 */ |
| 1374 | can_clone = true; |
| 1375 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1376 | if (!enabled[i]) |
| 1377 | continue; |
| 1378 | fb_helper_conn = fb_helper->connector_info[i]; |
| 1379 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); |
| 1380 | if (!modes[i]) { |
| 1381 | can_clone = false; |
| 1382 | break; |
| 1383 | } |
| 1384 | for (j = 0; j < i; j++) { |
| 1385 | if (!enabled[j]) |
| 1386 | continue; |
| 1387 | if (!drm_mode_equal(modes[j], modes[i])) |
| 1388 | can_clone = false; |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | if (can_clone) { |
| 1393 | DRM_DEBUG_KMS("can clone using command line\n"); |
| 1394 | return true; |
| 1395 | } |
| 1396 | |
| 1397 | /* try and find a 1024x768 mode on each connector */ |
| 1398 | can_clone = true; |
Adam Jackson | f6e252b | 2012-04-13 16:33:31 -0400 | [diff] [blame] | 1399 | 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] | 1400 | |
| 1401 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1402 | |
| 1403 | if (!enabled[i]) |
| 1404 | continue; |
| 1405 | |
| 1406 | fb_helper_conn = fb_helper->connector_info[i]; |
| 1407 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
| 1408 | if (drm_mode_equal(mode, dmt_mode)) |
| 1409 | modes[i] = mode; |
| 1410 | } |
| 1411 | if (!modes[i]) |
| 1412 | can_clone = false; |
| 1413 | } |
| 1414 | |
| 1415 | if (can_clone) { |
| 1416 | DRM_DEBUG_KMS("can clone using 1024x768\n"); |
| 1417 | return true; |
| 1418 | } |
| 1419 | DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); |
| 1420 | return false; |
| 1421 | } |
| 1422 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1423 | static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, |
| 1424 | struct drm_display_mode **modes, |
| 1425 | struct drm_fb_offset *offsets, |
| 1426 | int idx, |
| 1427 | int h_idx, int v_idx) |
| 1428 | { |
| 1429 | struct drm_fb_helper_connector *fb_helper_conn; |
| 1430 | int i; |
| 1431 | int hoffset = 0, voffset = 0; |
| 1432 | |
| 1433 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1434 | fb_helper_conn = fb_helper->connector_info[i]; |
| 1435 | if (!fb_helper_conn->connector->has_tile) |
| 1436 | continue; |
| 1437 | |
| 1438 | if (!modes[i] && (h_idx || v_idx)) { |
| 1439 | DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, |
| 1440 | fb_helper_conn->connector->base.id); |
| 1441 | continue; |
| 1442 | } |
| 1443 | if (fb_helper_conn->connector->tile_h_loc < h_idx) |
| 1444 | hoffset += modes[i]->hdisplay; |
| 1445 | |
| 1446 | if (fb_helper_conn->connector->tile_v_loc < v_idx) |
| 1447 | voffset += modes[i]->vdisplay; |
| 1448 | } |
| 1449 | offsets[idx].x = hoffset; |
| 1450 | offsets[idx].y = voffset; |
| 1451 | DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1455 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1456 | struct drm_display_mode **modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1457 | struct drm_fb_offset *offsets, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1458 | bool *enabled, int width, int height) |
| 1459 | { |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1460 | struct drm_fb_helper_connector *fb_helper_conn; |
| 1461 | int i; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1462 | uint64_t conn_configured = 0, mask; |
| 1463 | int tile_pass = 0; |
| 1464 | mask = (1 << fb_helper->connector_count) - 1; |
| 1465 | retry: |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1466 | for (i = 0; i < fb_helper->connector_count; i++) { |
| 1467 | fb_helper_conn = fb_helper->connector_info[i]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1468 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1469 | if (conn_configured & (1 << i)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1470 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1471 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1472 | if (enabled[i] == false) { |
| 1473 | conn_configured |= (1 << i); |
| 1474 | continue; |
| 1475 | } |
| 1476 | |
| 1477 | /* first pass over all the untiled connectors */ |
| 1478 | if (tile_pass == 0 && fb_helper_conn->connector->has_tile) |
| 1479 | continue; |
| 1480 | |
| 1481 | if (tile_pass == 1) { |
| 1482 | if (fb_helper_conn->connector->tile_h_loc != 0 || |
| 1483 | fb_helper_conn->connector->tile_v_loc != 0) |
| 1484 | continue; |
| 1485 | |
| 1486 | } else { |
| 1487 | if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 && |
| 1488 | fb_helper_conn->connector->tile_v_loc != tile_pass - 1) |
| 1489 | /* if this tile_pass doesn't cover any of the tiles - keep going */ |
| 1490 | continue; |
| 1491 | |
| 1492 | /* find the tile offsets for this pass - need |
| 1493 | to find all tiles left and above */ |
| 1494 | drm_get_tile_offsets(fb_helper, modes, offsets, |
| 1495 | i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); |
| 1496 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1497 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1498 | fb_helper_conn->connector->base.id); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1499 | |
| 1500 | /* got for command line mode first */ |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1501 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1502 | if (!modes[i]) { |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1503 | DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", |
| 1504 | 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] | 1505 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1506 | } |
| 1507 | /* No preferred modes, pick one off the list */ |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1508 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
| 1509 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1510 | break; |
| 1511 | } |
| 1512 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : |
| 1513 | "none"); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1514 | conn_configured |= (1 << i); |
| 1515 | } |
| 1516 | |
| 1517 | if ((conn_configured & mask) != mask) { |
| 1518 | tile_pass++; |
| 1519 | goto retry; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1520 | } |
| 1521 | return true; |
| 1522 | } |
| 1523 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1524 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
| 1525 | struct drm_fb_helper_crtc **best_crtcs, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1526 | struct drm_display_mode **modes, |
| 1527 | int n, int width, int height) |
| 1528 | { |
| 1529 | int c, o; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1530 | struct drm_device *dev = fb_helper->dev; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1531 | struct drm_connector *connector; |
| 1532 | struct drm_connector_helper_funcs *connector_funcs; |
| 1533 | struct drm_encoder *encoder; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1534 | int my_score, best_score, score; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1535 | struct drm_fb_helper_crtc **crtcs, *crtc; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1536 | struct drm_fb_helper_connector *fb_helper_conn; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1537 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1538 | if (n == fb_helper->connector_count) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1539 | return 0; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1540 | |
| 1541 | fb_helper_conn = fb_helper->connector_info[n]; |
| 1542 | connector = fb_helper_conn->connector; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1543 | |
| 1544 | best_crtcs[n] = NULL; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1545 | 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] | 1546 | if (modes[n] == NULL) |
| 1547 | return best_score; |
| 1548 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1549 | crtcs = kzalloc(dev->mode_config.num_connector * |
| 1550 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1551 | if (!crtcs) |
| 1552 | return best_score; |
| 1553 | |
| 1554 | my_score = 1; |
| 1555 | if (connector->status == connector_status_connected) |
| 1556 | my_score++; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1557 | if (drm_has_cmdline_mode(fb_helper_conn)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1558 | my_score++; |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1559 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1560 | my_score++; |
| 1561 | |
| 1562 | connector_funcs = connector->helper_private; |
| 1563 | encoder = connector_funcs->best_encoder(connector); |
| 1564 | if (!encoder) |
| 1565 | goto out; |
| 1566 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1567 | /* select a crtc for this connector and then attempt to configure |
| 1568 | remaining connectors */ |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1569 | for (c = 0; c < fb_helper->crtc_count; c++) { |
| 1570 | crtc = &fb_helper->crtc_info[c]; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1571 | |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1572 | if ((encoder->possible_crtcs & (1 << c)) == 0) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1573 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1574 | |
| 1575 | for (o = 0; o < n; o++) |
| 1576 | if (best_crtcs[o] == crtc) |
| 1577 | break; |
| 1578 | |
| 1579 | if (o < n) { |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 1580 | /* ignore cloning unless only a single crtc */ |
| 1581 | if (fb_helper->crtc_count > 1) |
| 1582 | continue; |
| 1583 | |
| 1584 | if (!drm_mode_equal(modes[o], modes[n])) |
| 1585 | continue; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | crtcs[n] = crtc; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1589 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
| 1590 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1591 | width, height); |
| 1592 | if (score > best_score) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1593 | best_score = score; |
| 1594 | memcpy(best_crtcs, crtcs, |
| 1595 | dev->mode_config.num_connector * |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1596 | sizeof(struct drm_fb_helper_crtc *)); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1597 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1598 | } |
| 1599 | out: |
| 1600 | kfree(crtcs); |
| 1601 | return best_score; |
| 1602 | } |
| 1603 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1604 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1605 | { |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1606 | struct drm_device *dev = fb_helper->dev; |
| 1607 | struct drm_fb_helper_crtc **crtcs; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1608 | struct drm_display_mode **modes; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1609 | struct drm_fb_offset *offsets; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1610 | struct drm_mode_set *modeset; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1611 | bool *enabled; |
| 1612 | int width, height; |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 1613 | int i; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1614 | |
| 1615 | DRM_DEBUG_KMS("\n"); |
| 1616 | |
| 1617 | width = dev->mode_config.max_width; |
| 1618 | height = dev->mode_config.max_height; |
| 1619 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1620 | crtcs = kcalloc(dev->mode_config.num_connector, |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1621 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1622 | modes = kcalloc(dev->mode_config.num_connector, |
| 1623 | sizeof(struct drm_display_mode *), GFP_KERNEL); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1624 | offsets = kcalloc(dev->mode_config.num_connector, |
| 1625 | sizeof(struct drm_fb_offset), GFP_KERNEL); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1626 | enabled = kcalloc(dev->mode_config.num_connector, |
| 1627 | sizeof(bool), GFP_KERNEL); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1628 | if (!crtcs || !modes || !enabled || !offsets) { |
Sachin Kamat | 8c5eaca | 2012-11-19 09:44:58 +0000 | [diff] [blame] | 1629 | DRM_ERROR("Memory allocation failed\n"); |
| 1630 | goto out; |
| 1631 | } |
| 1632 | |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1633 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1634 | drm_enable_connectors(fb_helper, enabled); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1635 | |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 1636 | if (!(fb_helper->funcs->initial_config && |
| 1637 | fb_helper->funcs->initial_config(fb_helper, crtcs, modes, |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1638 | offsets, |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 1639 | enabled, width, height))) { |
| 1640 | memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0])); |
| 1641 | memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0])); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1642 | memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0])); |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 1643 | |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1644 | if (!drm_target_cloned(fb_helper, modes, offsets, |
| 1645 | enabled, width, height) && |
| 1646 | !drm_target_preferred(fb_helper, modes, offsets, |
| 1647 | enabled, width, height)) |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 1648 | DRM_ERROR("Unable to find initial modes\n"); |
Jesse Barnes | 11e17a0 | 2013-02-19 13:31:39 -0800 | [diff] [blame] | 1649 | |
| 1650 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", |
| 1651 | width, height); |
| 1652 | |
| 1653 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
Dave Airlie | 1d42bbc | 2010-05-07 05:02:30 +0000 | [diff] [blame] | 1654 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1655 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1656 | /* need to set the modesets up here for use later */ |
| 1657 | /* fill out the connector<->crtc mappings into the modesets */ |
| 1658 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1659 | modeset = &fb_helper->crtc_info[i].mode_set; |
| 1660 | modeset->num_connectors = 0; |
Daniel Vetter | 7e53f3a | 2013-01-21 10:52:17 +0100 | [diff] [blame] | 1661 | modeset->fb = NULL; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1662 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1663 | |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1664 | for (i = 0; i < fb_helper->connector_count; i++) { |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1665 | struct drm_display_mode *mode = modes[i]; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1666 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1667 | struct drm_fb_offset *offset = &offsets[i]; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1668 | modeset = &fb_crtc->mode_set; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1669 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1670 | if (mode && fb_crtc) { |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1671 | DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", |
| 1672 | mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1673 | fb_crtc->desired_mode = mode; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1674 | fb_crtc->x = offset->x; |
| 1675 | fb_crtc->y = offset->y; |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1676 | if (modeset->mode) |
| 1677 | drm_mode_destroy(dev, modeset->mode); |
| 1678 | modeset->mode = drm_mode_duplicate(dev, |
| 1679 | fb_crtc->desired_mode); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1680 | modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; |
Daniel Vetter | 7e53f3a | 2013-01-21 10:52:17 +0100 | [diff] [blame] | 1681 | modeset->fb = fb_helper->fb; |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1682 | modeset->x = offset->x; |
| 1683 | modeset->y = offset->y; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1684 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Daniel Vetter | 7e53f3a | 2013-01-21 10:52:17 +0100 | [diff] [blame] | 1687 | /* Clear out any old modes if there are no more connected outputs. */ |
| 1688 | for (i = 0; i < fb_helper->crtc_count; i++) { |
| 1689 | modeset = &fb_helper->crtc_info[i].mode_set; |
| 1690 | if (modeset->num_connectors == 0) { |
| 1691 | BUG_ON(modeset->fb); |
Daniel Vetter | 7e53f3a | 2013-01-21 10:52:17 +0100 | [diff] [blame] | 1692 | if (modeset->mode) |
| 1693 | drm_mode_destroy(dev, modeset->mode); |
| 1694 | modeset->mode = NULL; |
| 1695 | } |
| 1696 | } |
Sachin Kamat | 8c5eaca | 2012-11-19 09:44:58 +0000 | [diff] [blame] | 1697 | out: |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1698 | kfree(crtcs); |
| 1699 | kfree(modes); |
Dave Airlie | b0ee9e7 | 2014-10-20 16:31:53 +1000 | [diff] [blame] | 1700 | kfree(offsets); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1701 | kfree(enabled); |
| 1702 | } |
| 1703 | |
| 1704 | /** |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1705 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 1706 | * @fb_helper: fb_helper device struct |
| 1707 | * @bpp_sel: bpp value to use for the framebuffer configuration |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1708 | * |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 1709 | * 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] | 1710 | * At the moment, this is a cloned configuration across all heads with |
| 1711 | * a new framebuffer object as the backing store. |
| 1712 | * |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1713 | * Note that this also registers the fbdev and so allows userspace to call into |
| 1714 | * the driver through the fbdev interfaces. |
| 1715 | * |
| 1716 | * This function will call down into the ->fb_probe callback to let |
| 1717 | * the driver allocate and initialize the fbdev info structure and the drm |
| 1718 | * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and |
| 1719 | * drm_fb_helper_fill_fix() are provided as helpers to setup simple default |
| 1720 | * values for the fbdev info structure. |
| 1721 | * |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1722 | * RETURNS: |
| 1723 | * Zero if everything went ok, nonzero otherwise. |
| 1724 | */ |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 1725 | 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] | 1726 | { |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1727 | struct drm_device *dev = fb_helper->dev; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1728 | int count = 0; |
| 1729 | |
Daniel Vetter | 53f1904 | 2014-03-20 14:26:35 +0100 | [diff] [blame] | 1730 | mutex_lock(&dev->mode_config.mutex); |
Dave Airlie | 0b4c0f3 | 2010-03-30 05:34:15 +0000 | [diff] [blame] | 1731 | count = drm_fb_helper_probe_connector_modes(fb_helper, |
| 1732 | dev->mode_config.max_width, |
| 1733 | dev->mode_config.max_height); |
Daniel Vetter | 53f1904 | 2014-03-20 14:26:35 +0100 | [diff] [blame] | 1734 | mutex_unlock(&dev->mode_config.mutex); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1735 | /* |
| 1736 | * we shouldn't end up with no modes here. |
| 1737 | */ |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1738 | if (count == 0) |
Sachin Kamat | d56b1b9 | 2012-11-15 03:43:29 +0000 | [diff] [blame] | 1739 | dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); |
Sachin Kamat | 96081cd | 2012-11-15 03:43:30 +0000 | [diff] [blame] | 1740 | |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1741 | drm_setup_crtcs(fb_helper); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1742 | |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1743 | return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1744 | } |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1745 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1746 | |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 1747 | /** |
| 1748 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by |
Daniel Vetter | d0ddc033 | 2012-11-01 14:45:17 +0100 | [diff] [blame] | 1749 | * probing all the outputs attached to the fb |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 1750 | * @fb_helper: the drm_fb_helper |
| 1751 | * |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 1752 | * Scan the connectors attached to the fb_helper and try to put together a |
| 1753 | * setup after *notification of a change in output configuration. |
| 1754 | * |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1755 | * Called at runtime, takes the mode config locks to be able to check/change the |
| 1756 | * modeset configuration. Must be run from process context (which usually means |
| 1757 | * either the output polling work or a work item launched from the driver's |
| 1758 | * hotplug interrupt). |
| 1759 | * |
Daniel Vetter | 50c3dc9 | 2014-06-27 17:19:22 +0200 | [diff] [blame] | 1760 | * Note that drivers may call this even before calling |
| 1761 | * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows |
| 1762 | * for a race-free fbcon setup and will make sure that the fbdev emulation will |
| 1763 | * not miss any hotplug events. |
Daniel Vetter | 207fd32 | 2013-01-20 22:13:14 +0100 | [diff] [blame] | 1764 | * |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 1765 | * RETURNS: |
| 1766 | * 0 on success and a non-zero error code otherwise. |
| 1767 | */ |
| 1768 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1769 | { |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 1770 | struct drm_device *dev = fb_helper->dev; |
Lespiau, Damien | 51bbd27 | 2013-09-28 16:24:05 +0100 | [diff] [blame] | 1771 | u32 max_width, max_height; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1772 | |
Daniel Vetter | 89ced12 | 2013-04-11 14:26:55 +0000 | [diff] [blame] | 1773 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
Daniel Vetter | 50c3dc9 | 2014-06-27 17:19:22 +0200 | [diff] [blame] | 1774 | if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 1775 | fb_helper->delayed_hotplug = true; |
Daniel Vetter | 89ced12 | 2013-04-11 14:26:55 +0000 | [diff] [blame] | 1776 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
Chris Wilson | 7394371 | 2011-04-22 11:03:57 +0100 | [diff] [blame] | 1777 | return 0; |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 1778 | } |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1779 | DRM_DEBUG_KMS("\n"); |
| 1780 | |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1781 | max_width = fb_helper->fb->width; |
| 1782 | max_height = fb_helper->fb->height; |
Dave Airlie | 4abe352 | 2010-03-30 05:34:18 +0000 | [diff] [blame] | 1783 | |
Lespiau, Damien | 51bbd27 | 2013-09-28 16:24:05 +0100 | [diff] [blame] | 1784 | drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); |
Daniel Vetter | 89ced12 | 2013-04-11 14:26:55 +0000 | [diff] [blame] | 1785 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
| 1786 | |
| 1787 | drm_modeset_lock_all(dev); |
Dave Airlie | 8be48d9 | 2010-03-30 05:34:14 +0000 | [diff] [blame] | 1788 | drm_setup_crtcs(fb_helper); |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1789 | drm_modeset_unlock_all(dev); |
Daniel Vetter | 2180c3c | 2013-01-21 23:12:36 +0100 | [diff] [blame] | 1790 | drm_fb_helper_set_par(fb_helper->fbdev); |
| 1791 | |
| 1792 | return 0; |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 1793 | } |
Dave Airlie | eb1f8e4 | 2010-05-07 06:42:51 +0000 | [diff] [blame] | 1794 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
Dave Airlie | 5c4426a | 2010-03-30 05:34:17 +0000 | [diff] [blame] | 1795 | |
David Rientjes | 6a108a1 | 2011-01-20 14:44:16 -0800 | [diff] [blame] | 1796 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 1797 | * but the module doesn't depend on any fb console symbols. At least |
| 1798 | * attempt to load fbcon to avoid leaving the system without a usable console. |
| 1799 | */ |
David Rientjes | 6a108a1 | 2011-01-20 14:44:16 -0800 | [diff] [blame] | 1800 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
David Fries | 3ce0516 | 2010-12-12 12:39:22 -0600 | [diff] [blame] | 1801 | static int __init drm_fb_helper_modinit(void) |
| 1802 | { |
| 1803 | const char *name = "fbcon"; |
| 1804 | struct module *fbcon; |
| 1805 | |
| 1806 | mutex_lock(&module_mutex); |
| 1807 | fbcon = find_module(name); |
| 1808 | mutex_unlock(&module_mutex); |
| 1809 | |
| 1810 | if (!fbcon) |
| 1811 | request_module_nowait(name); |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
| 1815 | module_init(drm_fb_helper_modinit); |
| 1816 | #endif |