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