Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006-2008 Intel Corporation |
| 3 | * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> |
| 4 | * Copyright (c) 2008 Red Hat Inc. |
| 5 | * |
| 6 | * DRM core CRTC related 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 | * Keith Packard |
| 28 | * Eric Anholt <eric@anholt.net> |
| 29 | * Dave Airlie <airlied@linux.ie> |
| 30 | * Jesse Barnes <jesse.barnes@intel.com> |
| 31 | */ |
Ville Syrjälä | 6ba6d03 | 2013-06-10 11:15:10 +0300 | [diff] [blame] | 32 | #include <linux/ctype.h> |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 33 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Paul Gortmaker | 2d1a8a4 | 2011-08-30 18:16:33 -0400 | [diff] [blame] | 35 | #include <linux/export.h> |
Gustavo Padovan | 2a38015 | 2016-11-15 23:37:08 +0900 | [diff] [blame] | 36 | #include <linux/fence.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_edid.h> |
| 40 | #include <drm/drm_fourcc.h> |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 41 | #include <drm/drm_modeset_lock.h> |
Rob Clark | 88a48e2 | 2014-12-18 16:01:50 -0500 | [diff] [blame] | 42 | #include <drm/drm_atomic.h> |
Daniel Vetter | 3b96a0b | 2016-06-21 10:54:22 +0200 | [diff] [blame] | 43 | #include <drm/drm_auth.h> |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 44 | #include <drm/drm_framebuffer.h> |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 45 | |
Daniel Vetter | 8bd441b | 2014-01-23 15:35:24 +0100 | [diff] [blame] | 46 | #include "drm_crtc_internal.h" |
Daniel Vetter | 67d0ec4 | 2014-09-10 12:43:53 +0200 | [diff] [blame] | 47 | #include "drm_internal.h" |
Daniel Vetter | 8bd441b | 2014-01-23 15:35:24 +0100 | [diff] [blame] | 48 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 49 | /* |
| 50 | * Global properties |
| 51 | */ |
Thierry Reding | 4dfd909 | 2014-12-10 13:03:34 +0100 | [diff] [blame] | 52 | static const struct drm_prop_enum_list drm_plane_type_enum_list[] = { |
Rob Clark | 9922ab5 | 2014-04-01 20:16:57 -0400 | [diff] [blame] | 53 | { DRM_PLANE_TYPE_OVERLAY, "Overlay" }, |
| 54 | { DRM_PLANE_TYPE_PRIMARY, "Primary" }, |
| 55 | { DRM_PLANE_TYPE_CURSOR, "Cursor" }, |
| 56 | }; |
| 57 | |
Daniel Vetter | 5221719 | 2016-08-12 22:48:50 +0200 | [diff] [blame] | 58 | /* |
| 59 | * Optional properties |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 60 | */ |
Lukas Wunner | 6a0d952 | 2016-06-08 18:47:27 +0200 | [diff] [blame] | 61 | /** |
| 62 | * drm_crtc_force_disable - Forcibly turn off a CRTC |
| 63 | * @crtc: CRTC to turn off |
| 64 | * |
| 65 | * Returns: |
| 66 | * Zero on success, error code on failure. |
| 67 | */ |
| 68 | int drm_crtc_force_disable(struct drm_crtc *crtc) |
| 69 | { |
| 70 | struct drm_mode_set set = { |
| 71 | .crtc = crtc, |
| 72 | }; |
| 73 | |
| 74 | return drm_mode_set_config_internal(&set); |
| 75 | } |
| 76 | EXPORT_SYMBOL(drm_crtc_force_disable); |
| 77 | |
| 78 | /** |
| 79 | * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs |
| 80 | * @dev: DRM device whose CRTCs to turn off |
| 81 | * |
| 82 | * Drivers may want to call this on unload to ensure that all displays are |
| 83 | * unlit and the GPU is in a consistent, low power state. Takes modeset locks. |
| 84 | * |
| 85 | * Returns: |
| 86 | * Zero on success, error code on failure. |
| 87 | */ |
| 88 | int drm_crtc_force_disable_all(struct drm_device *dev) |
| 89 | { |
| 90 | struct drm_crtc *crtc; |
| 91 | int ret = 0; |
| 92 | |
| 93 | drm_modeset_lock_all(dev); |
| 94 | drm_for_each_crtc(crtc, dev) |
| 95 | if (crtc->enabled) { |
| 96 | ret = drm_crtc_force_disable(crtc); |
| 97 | if (ret) |
| 98 | goto out; |
| 99 | } |
| 100 | out: |
| 101 | drm_modeset_unlock_all(dev); |
| 102 | return ret; |
| 103 | } |
| 104 | EXPORT_SYMBOL(drm_crtc_force_disable_all); |
| 105 | |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 106 | DEFINE_WW_CLASS(crtc_ww_class); |
| 107 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 108 | static unsigned int drm_num_crtcs(struct drm_device *dev) |
| 109 | { |
| 110 | unsigned int num = 0; |
| 111 | struct drm_crtc *tmp; |
| 112 | |
| 113 | drm_for_each_crtc(tmp, dev) { |
| 114 | num++; |
| 115 | } |
| 116 | |
| 117 | return num; |
| 118 | } |
| 119 | |
Benjamin Gaignard | 79190ea | 2016-06-21 16:37:09 +0200 | [diff] [blame] | 120 | static int drm_crtc_register_all(struct drm_device *dev) |
| 121 | { |
| 122 | struct drm_crtc *crtc; |
| 123 | int ret = 0; |
| 124 | |
| 125 | drm_for_each_crtc(crtc, dev) { |
| 126 | if (crtc->funcs->late_register) |
| 127 | ret = crtc->funcs->late_register(crtc); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static void drm_crtc_unregister_all(struct drm_device *dev) |
| 136 | { |
| 137 | struct drm_crtc *crtc; |
| 138 | |
| 139 | drm_for_each_crtc(crtc, dev) { |
| 140 | if (crtc->funcs->early_unregister) |
| 141 | crtc->funcs->early_unregister(crtc); |
| 142 | } |
| 143 | } |
| 144 | |
Gustavo Padovan | 34e0b7d | 2016-12-06 15:47:17 -0200 | [diff] [blame] | 145 | static const struct fence_ops drm_crtc_fence_ops; |
| 146 | |
Gustavo Padovan | 2a38015 | 2016-11-15 23:37:08 +0900 | [diff] [blame] | 147 | static struct drm_crtc *fence_to_crtc(struct fence *fence) |
| 148 | { |
| 149 | BUG_ON(fence->ops != &drm_crtc_fence_ops); |
| 150 | return container_of(fence->lock, struct drm_crtc, fence_lock); |
| 151 | } |
| 152 | |
| 153 | static const char *drm_crtc_fence_get_driver_name(struct fence *fence) |
| 154 | { |
| 155 | struct drm_crtc *crtc = fence_to_crtc(fence); |
| 156 | |
| 157 | return crtc->dev->driver->name; |
| 158 | } |
| 159 | |
| 160 | static const char *drm_crtc_fence_get_timeline_name(struct fence *fence) |
| 161 | { |
| 162 | struct drm_crtc *crtc = fence_to_crtc(fence); |
| 163 | |
| 164 | return crtc->timeline_name; |
| 165 | } |
| 166 | |
| 167 | static bool drm_crtc_fence_enable_signaling(struct fence *fence) |
| 168 | { |
| 169 | return true; |
| 170 | } |
| 171 | |
Gustavo Padovan | 34e0b7d | 2016-12-06 15:47:17 -0200 | [diff] [blame] | 172 | static const struct fence_ops drm_crtc_fence_ops = { |
Gustavo Padovan | 2a38015 | 2016-11-15 23:37:08 +0900 | [diff] [blame] | 173 | .get_driver_name = drm_crtc_fence_get_driver_name, |
| 174 | .get_timeline_name = drm_crtc_fence_get_timeline_name, |
| 175 | .enable_signaling = drm_crtc_fence_enable_signaling, |
| 176 | .wait = fence_default_wait, |
| 177 | }; |
| 178 | |
Gustavo Padovan | 34e0b7d | 2016-12-06 15:47:17 -0200 | [diff] [blame] | 179 | struct fence *drm_crtc_create_fence(struct drm_crtc *crtc) |
| 180 | { |
| 181 | struct fence *fence; |
| 182 | |
| 183 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
| 184 | if (!fence) |
| 185 | return NULL; |
| 186 | |
| 187 | fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock, |
| 188 | crtc->fence_context, ++crtc->fence_seqno); |
| 189 | |
| 190 | return fence; |
| 191 | } |
| 192 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 193 | /** |
Matt Roper | e13161a | 2014-04-01 15:22:38 -0700 | [diff] [blame] | 194 | * drm_crtc_init_with_planes - Initialise a new CRTC object with |
| 195 | * specified primary and cursor planes. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 196 | * @dev: DRM device |
| 197 | * @crtc: CRTC object to init |
Matt Roper | e13161a | 2014-04-01 15:22:38 -0700 | [diff] [blame] | 198 | * @primary: Primary plane for CRTC |
| 199 | * @cursor: Cursor plane for CRTC |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 200 | * @funcs: callbacks for the new CRTC |
Ville Syrjälä | f988287 | 2015-12-09 16:19:31 +0200 | [diff] [blame] | 201 | * @name: printf style format string for the CRTC name, or NULL for default name |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 202 | * |
Daniel Vetter | 532b367 | 2016-09-21 10:59:25 +0200 | [diff] [blame] | 203 | * Inits a new object created as base part of a driver crtc object. Drivers |
| 204 | * should use this function instead of drm_crtc_init(), which is only provided |
| 205 | * for backwards compatibility with drivers which do not yet support universal |
| 206 | * planes). For really simple hardware which has only 1 plane look at |
| 207 | * drm_simple_display_pipe_init() instead. |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 208 | * |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 209 | * Returns: |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 210 | * Zero on success, error code on failure. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 211 | */ |
Matt Roper | e13161a | 2014-04-01 15:22:38 -0700 | [diff] [blame] | 212 | int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc, |
| 213 | struct drm_plane *primary, |
Matt Roper | fc1d3e4 | 2014-06-10 08:28:11 -0700 | [diff] [blame] | 214 | struct drm_plane *cursor, |
Ville Syrjälä | f988287 | 2015-12-09 16:19:31 +0200 | [diff] [blame] | 215 | const struct drm_crtc_funcs *funcs, |
| 216 | const char *name, ...) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 217 | { |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 218 | struct drm_mode_config *config = &dev->mode_config; |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 219 | int ret; |
| 220 | |
Benjamin Gaignard | 522cf91 | 2015-03-17 12:05:29 +0100 | [diff] [blame] | 221 | WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY); |
| 222 | WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR); |
| 223 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 224 | crtc->dev = dev; |
| 225 | crtc->funcs = funcs; |
| 226 | |
Daniel Vetter | 3b24f7d | 2016-06-08 14:19:00 +0200 | [diff] [blame] | 227 | INIT_LIST_HEAD(&crtc->commit_list); |
| 228 | spin_lock_init(&crtc->commit_lock); |
| 229 | |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 230 | drm_modeset_lock_init(&crtc->mutex); |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 231 | ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC); |
| 232 | if (ret) |
Daniel Vetter | baf698b | 2014-11-12 11:59:47 +0100 | [diff] [blame] | 233 | return ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 234 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 235 | if (name) { |
| 236 | va_list ap; |
| 237 | |
| 238 | va_start(ap, name); |
| 239 | crtc->name = kvasprintf(GFP_KERNEL, name, ap); |
| 240 | va_end(ap); |
| 241 | } else { |
| 242 | crtc->name = kasprintf(GFP_KERNEL, "crtc-%d", |
| 243 | drm_num_crtcs(dev)); |
| 244 | } |
| 245 | if (!crtc->name) { |
Dave Airlie | 7c8f6d2 | 2016-04-15 15:10:32 +1000 | [diff] [blame] | 246 | drm_mode_object_unregister(dev, &crtc->base); |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 247 | return -ENOMEM; |
| 248 | } |
| 249 | |
Gustavo Padovan | 2a38015 | 2016-11-15 23:37:08 +0900 | [diff] [blame] | 250 | crtc->fence_context = fence_context_alloc(1); |
| 251 | spin_lock_init(&crtc->fence_lock); |
| 252 | snprintf(crtc->timeline_name, sizeof(crtc->timeline_name), |
| 253 | "CRTC:%d-%s", crtc->base.id, crtc->name); |
| 254 | |
Paulo Zanoni | bffd9de0 | 2012-05-15 18:09:05 -0300 | [diff] [blame] | 255 | crtc->base.properties = &crtc->properties; |
| 256 | |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 257 | list_add_tail(&crtc->head, &config->crtc_list); |
Chris Wilson | 490d3d1 | 2016-05-27 20:05:00 +0100 | [diff] [blame] | 258 | crtc->index = config->num_crtc++; |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 259 | |
Matt Roper | e13161a | 2014-04-01 15:22:38 -0700 | [diff] [blame] | 260 | crtc->primary = primary; |
Matt Roper | fc1d3e4 | 2014-06-10 08:28:11 -0700 | [diff] [blame] | 261 | crtc->cursor = cursor; |
Matt Roper | e13161a | 2014-04-01 15:22:38 -0700 | [diff] [blame] | 262 | if (primary) |
| 263 | primary->possible_crtcs = 1 << drm_crtc_index(crtc); |
Matt Roper | fc1d3e4 | 2014-06-10 08:28:11 -0700 | [diff] [blame] | 264 | if (cursor) |
| 265 | cursor->possible_crtcs = 1 << drm_crtc_index(crtc); |
Matt Roper | e13161a | 2014-04-01 15:22:38 -0700 | [diff] [blame] | 266 | |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 267 | if (drm_core_check_feature(dev, DRIVER_ATOMIC)) { |
| 268 | drm_object_attach_property(&crtc->base, config->prop_active, 0); |
Daniel Stone | 955f3c3 | 2015-05-25 19:11:52 +0100 | [diff] [blame] | 269 | drm_object_attach_property(&crtc->base, config->prop_mode_id, 0); |
Gustavo Padovan | 710c1ed | 2016-11-16 22:00:21 +0900 | [diff] [blame] | 270 | drm_object_attach_property(&crtc->base, |
| 271 | config->prop_out_fence_ptr, 0); |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Daniel Vetter | baf698b | 2014-11-12 11:59:47 +0100 | [diff] [blame] | 274 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 275 | } |
Matt Roper | e13161a | 2014-04-01 15:22:38 -0700 | [diff] [blame] | 276 | EXPORT_SYMBOL(drm_crtc_init_with_planes); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 277 | |
| 278 | /** |
Ville Syrjälä | ad6f5c3 | 2013-06-05 12:39:55 +0000 | [diff] [blame] | 279 | * drm_crtc_cleanup - Clean up the core crtc usage |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 280 | * @crtc: CRTC to cleanup |
| 281 | * |
Ville Syrjälä | ad6f5c3 | 2013-06-05 12:39:55 +0000 | [diff] [blame] | 282 | * This function cleans up @crtc and removes it from the DRM mode setting |
| 283 | * core. Note that the function does *not* free the crtc structure itself, |
| 284 | * this is the responsibility of the caller. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 285 | */ |
| 286 | void drm_crtc_cleanup(struct drm_crtc *crtc) |
| 287 | { |
| 288 | struct drm_device *dev = crtc->dev; |
| 289 | |
Chris Wilson | 490d3d1 | 2016-05-27 20:05:00 +0100 | [diff] [blame] | 290 | /* Note that the crtc_list is considered to be static; should we |
| 291 | * remove the drm_crtc at runtime we would have to decrement all |
| 292 | * the indices on the drm_crtc after us in the crtc_list. |
| 293 | */ |
| 294 | |
Sachin Kamat | 9e1c156 | 2012-11-19 09:44:56 +0000 | [diff] [blame] | 295 | kfree(crtc->gamma_store); |
| 296 | crtc->gamma_store = NULL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 297 | |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 298 | drm_modeset_lock_fini(&crtc->mutex); |
| 299 | |
Dave Airlie | 7c8f6d2 | 2016-04-15 15:10:32 +1000 | [diff] [blame] | 300 | drm_mode_object_unregister(dev, &crtc->base); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 301 | list_del(&crtc->head); |
| 302 | dev->mode_config.num_crtc--; |
Thierry Reding | 3009c03 | 2014-11-25 12:09:49 +0100 | [diff] [blame] | 303 | |
| 304 | WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state); |
| 305 | if (crtc->state && crtc->funcs->atomic_destroy_state) |
| 306 | crtc->funcs->atomic_destroy_state(crtc, crtc->state); |
Thierry Reding | a18c0af | 2014-12-10 11:38:49 +0100 | [diff] [blame] | 307 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 308 | kfree(crtc->name); |
| 309 | |
Thierry Reding | a18c0af | 2014-12-10 11:38:49 +0100 | [diff] [blame] | 310 | memset(crtc, 0, sizeof(*crtc)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 311 | } |
| 312 | EXPORT_SYMBOL(drm_crtc_cleanup); |
| 313 | |
Benjamin Gaignard | 79190ea | 2016-06-21 16:37:09 +0200 | [diff] [blame] | 314 | int drm_modeset_register_all(struct drm_device *dev) |
| 315 | { |
| 316 | int ret; |
| 317 | |
| 318 | ret = drm_plane_register_all(dev); |
| 319 | if (ret) |
| 320 | goto err_plane; |
| 321 | |
| 322 | ret = drm_crtc_register_all(dev); |
| 323 | if (ret) |
| 324 | goto err_crtc; |
| 325 | |
| 326 | ret = drm_encoder_register_all(dev); |
| 327 | if (ret) |
| 328 | goto err_encoder; |
| 329 | |
| 330 | ret = drm_connector_register_all(dev); |
| 331 | if (ret) |
| 332 | goto err_connector; |
| 333 | |
| 334 | return 0; |
| 335 | |
| 336 | err_connector: |
| 337 | drm_encoder_unregister_all(dev); |
| 338 | err_encoder: |
| 339 | drm_crtc_unregister_all(dev); |
| 340 | err_crtc: |
| 341 | drm_plane_unregister_all(dev); |
| 342 | err_plane: |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | void drm_modeset_unregister_all(struct drm_device *dev) |
| 347 | { |
| 348 | drm_connector_unregister_all(dev); |
| 349 | drm_encoder_unregister_all(dev); |
| 350 | drm_crtc_unregister_all(dev); |
| 351 | drm_plane_unregister_all(dev); |
| 352 | } |
| 353 | |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 354 | static int drm_mode_create_standard_properties(struct drm_device *dev) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 355 | { |
Rob Clark | 356af0e | 2014-12-18 16:01:52 -0500 | [diff] [blame] | 356 | struct drm_property *prop; |
Daniel Vetter | 5221719 | 2016-08-12 22:48:50 +0200 | [diff] [blame] | 357 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 358 | |
Daniel Vetter | 5221719 | 2016-08-12 22:48:50 +0200 | [diff] [blame] | 359 | ret = drm_connector_create_standard_properties(dev); |
| 360 | if (ret) |
| 361 | return ret; |
Dave Airlie | 6f134d7 | 2014-10-20 16:30:50 +1000 | [diff] [blame] | 362 | |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 363 | prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, |
Rob Clark | 9922ab5 | 2014-04-01 20:16:57 -0400 | [diff] [blame] | 364 | "type", drm_plane_type_enum_list, |
| 365 | ARRAY_SIZE(drm_plane_type_enum_list)); |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 366 | if (!prop) |
| 367 | return -ENOMEM; |
| 368 | dev->mode_config.plane_type_property = prop; |
| 369 | |
| 370 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, |
| 371 | "SRC_X", 0, UINT_MAX); |
| 372 | if (!prop) |
| 373 | return -ENOMEM; |
| 374 | dev->mode_config.prop_src_x = prop; |
| 375 | |
| 376 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, |
| 377 | "SRC_Y", 0, UINT_MAX); |
| 378 | if (!prop) |
| 379 | return -ENOMEM; |
| 380 | dev->mode_config.prop_src_y = prop; |
| 381 | |
| 382 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, |
| 383 | "SRC_W", 0, UINT_MAX); |
| 384 | if (!prop) |
| 385 | return -ENOMEM; |
| 386 | dev->mode_config.prop_src_w = prop; |
| 387 | |
| 388 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, |
| 389 | "SRC_H", 0, UINT_MAX); |
| 390 | if (!prop) |
| 391 | return -ENOMEM; |
| 392 | dev->mode_config.prop_src_h = prop; |
| 393 | |
| 394 | prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC, |
| 395 | "CRTC_X", INT_MIN, INT_MAX); |
| 396 | if (!prop) |
| 397 | return -ENOMEM; |
| 398 | dev->mode_config.prop_crtc_x = prop; |
| 399 | |
| 400 | prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC, |
| 401 | "CRTC_Y", INT_MIN, INT_MAX); |
| 402 | if (!prop) |
| 403 | return -ENOMEM; |
| 404 | dev->mode_config.prop_crtc_y = prop; |
| 405 | |
| 406 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, |
| 407 | "CRTC_W", 0, INT_MAX); |
| 408 | if (!prop) |
| 409 | return -ENOMEM; |
| 410 | dev->mode_config.prop_crtc_w = prop; |
| 411 | |
| 412 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, |
| 413 | "CRTC_H", 0, INT_MAX); |
| 414 | if (!prop) |
| 415 | return -ENOMEM; |
| 416 | dev->mode_config.prop_crtc_h = prop; |
| 417 | |
| 418 | prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC, |
| 419 | "FB_ID", DRM_MODE_OBJECT_FB); |
| 420 | if (!prop) |
| 421 | return -ENOMEM; |
| 422 | dev->mode_config.prop_fb_id = prop; |
| 423 | |
Gustavo Padovan | 96e02f4 | 2016-11-15 22:06:39 +0900 | [diff] [blame] | 424 | prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC, |
| 425 | "IN_FENCE_FD", -1, INT_MAX); |
| 426 | if (!prop) |
| 427 | return -ENOMEM; |
| 428 | dev->mode_config.prop_in_fence_fd = prop; |
| 429 | |
Gustavo Padovan | 710c1ed | 2016-11-16 22:00:21 +0900 | [diff] [blame] | 430 | prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, |
| 431 | "OUT_FENCE_PTR", 0, U64_MAX); |
| 432 | if (!prop) |
| 433 | return -ENOMEM; |
| 434 | dev->mode_config.prop_out_fence_ptr = prop; |
| 435 | |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 436 | prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC, |
| 437 | "CRTC_ID", DRM_MODE_OBJECT_CRTC); |
| 438 | if (!prop) |
| 439 | return -ENOMEM; |
| 440 | dev->mode_config.prop_crtc_id = prop; |
Rob Clark | 9922ab5 | 2014-04-01 20:16:57 -0400 | [diff] [blame] | 441 | |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 442 | prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC, |
| 443 | "ACTIVE"); |
| 444 | if (!prop) |
| 445 | return -ENOMEM; |
| 446 | dev->mode_config.prop_active = prop; |
| 447 | |
Daniel Stone | 955f3c3 | 2015-05-25 19:11:52 +0100 | [diff] [blame] | 448 | prop = drm_property_create(dev, |
| 449 | DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB, |
| 450 | "MODE_ID", 0); |
| 451 | if (!prop) |
| 452 | return -ENOMEM; |
| 453 | dev->mode_config.prop_mode_id = prop; |
| 454 | |
Lionel Landwerlin | 5488dc1 | 2016-02-26 17:05:00 +0000 | [diff] [blame] | 455 | prop = drm_property_create(dev, |
| 456 | DRM_MODE_PROP_BLOB, |
| 457 | "DEGAMMA_LUT", 0); |
| 458 | if (!prop) |
| 459 | return -ENOMEM; |
| 460 | dev->mode_config.degamma_lut_property = prop; |
| 461 | |
| 462 | prop = drm_property_create_range(dev, |
| 463 | DRM_MODE_PROP_IMMUTABLE, |
| 464 | "DEGAMMA_LUT_SIZE", 0, UINT_MAX); |
| 465 | if (!prop) |
| 466 | return -ENOMEM; |
| 467 | dev->mode_config.degamma_lut_size_property = prop; |
| 468 | |
| 469 | prop = drm_property_create(dev, |
| 470 | DRM_MODE_PROP_BLOB, |
| 471 | "CTM", 0); |
| 472 | if (!prop) |
| 473 | return -ENOMEM; |
| 474 | dev->mode_config.ctm_property = prop; |
| 475 | |
| 476 | prop = drm_property_create(dev, |
| 477 | DRM_MODE_PROP_BLOB, |
| 478 | "GAMMA_LUT", 0); |
| 479 | if (!prop) |
| 480 | return -ENOMEM; |
| 481 | dev->mode_config.gamma_lut_property = prop; |
| 482 | |
| 483 | prop = drm_property_create_range(dev, |
| 484 | DRM_MODE_PROP_IMMUTABLE, |
| 485 | "GAMMA_LUT_SIZE", 0, UINT_MAX); |
| 486 | if (!prop) |
| 487 | return -ENOMEM; |
| 488 | dev->mode_config.gamma_lut_size_property = prop; |
| 489 | |
Rob Clark | 9922ab5 | 2014-04-01 20:16:57 -0400 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 493 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 494 | * drm_mode_getresources - get graphics configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 495 | * @dev: drm device for the ioctl |
| 496 | * @data: data pointer for the ioctl |
| 497 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 498 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 499 | * Construct a set of configuration description structures and return |
| 500 | * them to the user, including CRTC, connector and framebuffer configuration. |
| 501 | * |
| 502 | * Called by the user via ioctl. |
| 503 | * |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 504 | * Returns: |
Daniel Vetter | 1a49863 | 2014-11-19 18:38:09 +0100 | [diff] [blame] | 505 | * Zero on success, negative errno on failure. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 506 | */ |
| 507 | int drm_mode_getresources(struct drm_device *dev, void *data, |
| 508 | struct drm_file *file_priv) |
| 509 | { |
| 510 | struct drm_mode_card_res *card_res = data; |
| 511 | struct list_head *lh; |
| 512 | struct drm_framebuffer *fb; |
| 513 | struct drm_connector *connector; |
| 514 | struct drm_crtc *crtc; |
| 515 | struct drm_encoder *encoder; |
| 516 | int ret = 0; |
| 517 | int connector_count = 0; |
| 518 | int crtc_count = 0; |
| 519 | int fb_count = 0; |
| 520 | int encoder_count = 0; |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 521 | int copied = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 522 | uint32_t __user *fb_id; |
| 523 | uint32_t __user *crtc_id; |
| 524 | uint32_t __user *connector_id; |
| 525 | uint32_t __user *encoder_id; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 526 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 527 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 528 | return -EINVAL; |
| 529 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 530 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 531 | mutex_lock(&file_priv->fbs_lock); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 532 | /* |
| 533 | * For the non-control nodes we need to limit the list of resources |
| 534 | * by IDs in the group list for this node |
| 535 | */ |
| 536 | list_for_each(lh, &file_priv->fbs) |
| 537 | fb_count++; |
| 538 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 539 | /* handle this in 4 parts */ |
| 540 | /* FBs */ |
| 541 | if (card_res->count_fbs >= fb_count) { |
| 542 | copied = 0; |
| 543 | fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr; |
| 544 | list_for_each_entry(fb, &file_priv->fbs, filp_head) { |
| 545 | if (put_user(fb->base.id, fb_id + copied)) { |
| 546 | mutex_unlock(&file_priv->fbs_lock); |
| 547 | return -EFAULT; |
| 548 | } |
| 549 | copied++; |
| 550 | } |
| 551 | } |
| 552 | card_res->count_fbs = fb_count; |
| 553 | mutex_unlock(&file_priv->fbs_lock); |
| 554 | |
Daniel Vetter | fcf93f6 | 2014-11-12 08:45:01 +0100 | [diff] [blame] | 555 | /* mode_config.mutex protects the connector list against e.g. DP MST |
| 556 | * connector hot-adding. CRTC/Plane lists are invariant. */ |
| 557 | mutex_lock(&dev->mode_config.mutex); |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 558 | drm_for_each_crtc(crtc, dev) |
| 559 | crtc_count++; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 560 | |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 561 | drm_for_each_connector(connector, dev) |
| 562 | connector_count++; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 563 | |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 564 | drm_for_each_encoder(encoder, dev) |
| 565 | encoder_count++; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 566 | |
| 567 | card_res->max_height = dev->mode_config.max_height; |
| 568 | card_res->min_height = dev->mode_config.min_height; |
| 569 | card_res->max_width = dev->mode_config.max_width; |
| 570 | card_res->min_width = dev->mode_config.min_width; |
| 571 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 572 | /* CRTCs */ |
| 573 | if (card_res->count_crtcs >= crtc_count) { |
| 574 | copied = 0; |
| 575 | crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr; |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 576 | drm_for_each_crtc(crtc, dev) { |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 577 | if (put_user(crtc->base.id, crtc_id + copied)) { |
| 578 | ret = -EFAULT; |
| 579 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 580 | } |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 581 | copied++; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | card_res->count_crtcs = crtc_count; |
| 585 | |
| 586 | /* Encoders */ |
| 587 | if (card_res->count_encoders >= encoder_count) { |
| 588 | copied = 0; |
| 589 | encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr; |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 590 | drm_for_each_encoder(encoder, dev) { |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 591 | if (put_user(encoder->base.id, encoder_id + |
| 592 | copied)) { |
| 593 | ret = -EFAULT; |
| 594 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 595 | } |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 596 | copied++; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | card_res->count_encoders = encoder_count; |
| 600 | |
| 601 | /* Connectors */ |
| 602 | if (card_res->count_connectors >= connector_count) { |
| 603 | copied = 0; |
| 604 | connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr; |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 605 | drm_for_each_connector(connector, dev) { |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 606 | if (put_user(connector->base.id, |
| 607 | connector_id + copied)) { |
| 608 | ret = -EFAULT; |
| 609 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 610 | } |
Daniel Vetter | 9c7060f | 2015-07-09 23:44:36 +0200 | [diff] [blame] | 611 | copied++; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | card_res->count_connectors = connector_count; |
| 615 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 616 | out: |
Daniel Vetter | fcf93f6 | 2014-11-12 08:45:01 +0100 | [diff] [blame] | 617 | mutex_unlock(&dev->mode_config.mutex); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * drm_mode_getcrtc - get CRTC configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 623 | * @dev: drm device for the ioctl |
| 624 | * @data: data pointer for the ioctl |
| 625 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 626 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 627 | * Construct a CRTC configuration structure to return to the user. |
| 628 | * |
| 629 | * Called by the user via ioctl. |
| 630 | * |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 631 | * Returns: |
Daniel Vetter | 1a49863 | 2014-11-19 18:38:09 +0100 | [diff] [blame] | 632 | * Zero on success, negative errno on failure. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 633 | */ |
| 634 | int drm_mode_getcrtc(struct drm_device *dev, |
| 635 | void *data, struct drm_file *file_priv) |
| 636 | { |
| 637 | struct drm_mode_crtc *crtc_resp = data; |
| 638 | struct drm_crtc *crtc; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 639 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 640 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 641 | return -EINVAL; |
| 642 | |
Rob Clark | a2b34e2 | 2013-10-05 16:36:52 -0400 | [diff] [blame] | 643 | crtc = drm_crtc_find(dev, crtc_resp->crtc_id); |
Daniel Vetter | fcf93f6 | 2014-11-12 08:45:01 +0100 | [diff] [blame] | 644 | if (!crtc) |
| 645 | return -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 646 | |
Daniel Vetter | fcf93f6 | 2014-11-12 08:45:01 +0100 | [diff] [blame] | 647 | drm_modeset_lock_crtc(crtc, crtc->primary); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 648 | crtc_resp->gamma_size = crtc->gamma_size; |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 649 | if (crtc->primary->fb) |
| 650 | crtc_resp->fb_id = crtc->primary->fb->base.id; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 651 | else |
| 652 | crtc_resp->fb_id = 0; |
| 653 | |
Daniel Vetter | 31c946e | 2015-02-22 12:24:17 +0100 | [diff] [blame] | 654 | if (crtc->state) { |
| 655 | crtc_resp->x = crtc->primary->state->src_x >> 16; |
| 656 | crtc_resp->y = crtc->primary->state->src_y >> 16; |
| 657 | if (crtc->state->enable) { |
Daniel Stone | 934a8a8 | 2015-05-22 13:34:48 +0100 | [diff] [blame] | 658 | drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode); |
Daniel Vetter | 31c946e | 2015-02-22 12:24:17 +0100 | [diff] [blame] | 659 | crtc_resp->mode_valid = 1; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 660 | |
Daniel Vetter | 31c946e | 2015-02-22 12:24:17 +0100 | [diff] [blame] | 661 | } else { |
| 662 | crtc_resp->mode_valid = 0; |
| 663 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 664 | } else { |
Daniel Vetter | 31c946e | 2015-02-22 12:24:17 +0100 | [diff] [blame] | 665 | crtc_resp->x = crtc->x; |
| 666 | crtc_resp->y = crtc->y; |
| 667 | if (crtc->enabled) { |
Daniel Stone | 934a8a8 | 2015-05-22 13:34:48 +0100 | [diff] [blame] | 668 | drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode); |
Daniel Vetter | 31c946e | 2015-02-22 12:24:17 +0100 | [diff] [blame] | 669 | crtc_resp->mode_valid = 1; |
| 670 | |
| 671 | } else { |
| 672 | crtc_resp->mode_valid = 0; |
| 673 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 674 | } |
Daniel Vetter | fcf93f6 | 2014-11-12 08:45:01 +0100 | [diff] [blame] | 675 | drm_modeset_unlock_crtc(crtc); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 676 | |
Daniel Vetter | baf698b | 2014-11-12 11:59:47 +0100 | [diff] [blame] | 677 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 680 | /** |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 681 | * drm_mode_set_config_internal - helper to call ->set_config |
| 682 | * @set: modeset config to set |
| 683 | * |
| 684 | * This is a little helper to wrap internal calls to the ->set_config driver |
| 685 | * interface. The only thing it adds is correct refcounting dance. |
Thierry Reding | 4dfd909 | 2014-12-10 13:03:34 +0100 | [diff] [blame] | 686 | * |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 687 | * Returns: |
Daniel Vetter | 1a49863 | 2014-11-19 18:38:09 +0100 | [diff] [blame] | 688 | * Zero on success, negative errno on failure. |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 689 | */ |
| 690 | int drm_mode_set_config_internal(struct drm_mode_set *set) |
| 691 | { |
| 692 | struct drm_crtc *crtc = set->crtc; |
Daniel Vetter | 5cef29a | 2013-06-15 00:13:16 +0200 | [diff] [blame] | 693 | struct drm_framebuffer *fb; |
| 694 | struct drm_crtc *tmp; |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 695 | int ret; |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 696 | |
Daniel Vetter | 5cef29a | 2013-06-15 00:13:16 +0200 | [diff] [blame] | 697 | /* |
| 698 | * NOTE: ->set_config can also disable other crtcs (if we steal all |
| 699 | * connectors from it), hence we need to refcount the fbs across all |
| 700 | * crtcs. Atomic modeset will have saner semantics ... |
| 701 | */ |
Daniel Vetter | e4f6254 | 2015-07-09 23:44:35 +0200 | [diff] [blame] | 702 | drm_for_each_crtc(tmp, crtc->dev) |
Daniel Vetter | 3d30a59 | 2014-07-27 13:42:42 +0200 | [diff] [blame] | 703 | tmp->primary->old_fb = tmp->primary->fb; |
Daniel Vetter | 5cef29a | 2013-06-15 00:13:16 +0200 | [diff] [blame] | 704 | |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 705 | fb = set->fb; |
| 706 | |
| 707 | ret = crtc->funcs->set_config(set); |
| 708 | if (ret == 0) { |
Matt Roper | e13161a | 2014-04-01 15:22:38 -0700 | [diff] [blame] | 709 | crtc->primary->crtc = crtc; |
Daniel Vetter | 0fe27f0 | 2014-04-23 17:34:06 +0200 | [diff] [blame] | 710 | crtc->primary->fb = fb; |
Daniel Vetter | 5cef29a | 2013-06-15 00:13:16 +0200 | [diff] [blame] | 711 | } |
Daniel Vetter | cc85e12 | 2013-06-15 00:13:15 +0200 | [diff] [blame] | 712 | |
Daniel Vetter | e4f6254 | 2015-07-09 23:44:35 +0200 | [diff] [blame] | 713 | drm_for_each_crtc(tmp, crtc->dev) { |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 714 | if (tmp->primary->fb) |
| 715 | drm_framebuffer_reference(tmp->primary->fb); |
Daniel Vetter | 3d30a59 | 2014-07-27 13:42:42 +0200 | [diff] [blame] | 716 | if (tmp->primary->old_fb) |
| 717 | drm_framebuffer_unreference(tmp->primary->old_fb); |
| 718 | tmp->primary->old_fb = NULL; |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return ret; |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 722 | } |
| 723 | EXPORT_SYMBOL(drm_mode_set_config_internal); |
| 724 | |
Matt Roper | af93629 | 2014-04-01 15:22:34 -0700 | [diff] [blame] | 725 | /** |
Gustavo Padovan | ecb7e16 | 2014-12-01 15:40:09 -0800 | [diff] [blame] | 726 | * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode |
| 727 | * @mode: mode to query |
| 728 | * @hdisplay: hdisplay value to fill in |
| 729 | * @vdisplay: vdisplay value to fill in |
| 730 | * |
| 731 | * The vdisplay value will be doubled if the specified mode is a stereo mode of |
| 732 | * the appropriate layout. |
| 733 | */ |
| 734 | void drm_crtc_get_hv_timing(const struct drm_display_mode *mode, |
| 735 | int *hdisplay, int *vdisplay) |
| 736 | { |
| 737 | struct drm_display_mode adjusted; |
| 738 | |
| 739 | drm_mode_copy(&adjusted, mode); |
| 740 | drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY); |
| 741 | *hdisplay = adjusted.crtc_hdisplay; |
| 742 | *vdisplay = adjusted.crtc_vdisplay; |
| 743 | } |
| 744 | EXPORT_SYMBOL(drm_crtc_get_hv_timing); |
| 745 | |
| 746 | /** |
Matt Roper | af93629 | 2014-04-01 15:22:34 -0700 | [diff] [blame] | 747 | * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the |
| 748 | * CRTC viewport |
| 749 | * @crtc: CRTC that framebuffer will be displayed on |
| 750 | * @x: x panning |
| 751 | * @y: y panning |
| 752 | * @mode: mode that framebuffer will be displayed under |
| 753 | * @fb: framebuffer to check size of |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 754 | */ |
Matt Roper | af93629 | 2014-04-01 15:22:34 -0700 | [diff] [blame] | 755 | int drm_crtc_check_viewport(const struct drm_crtc *crtc, |
| 756 | int x, int y, |
| 757 | const struct drm_display_mode *mode, |
| 758 | const struct drm_framebuffer *fb) |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 759 | |
| 760 | { |
| 761 | int hdisplay, vdisplay; |
| 762 | |
Gustavo Padovan | ecb7e16 | 2014-12-01 15:40:09 -0800 | [diff] [blame] | 763 | drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay); |
Damien Lespiau | a0c1bbb | 2013-09-25 16:45:31 +0100 | [diff] [blame] | 764 | |
Ville Syrjälä | 33e0be6 | 2015-10-16 18:38:39 +0300 | [diff] [blame] | 765 | if (crtc->state && |
Joonas Lahtinen | 31ad61e | 2016-07-29 08:50:05 +0300 | [diff] [blame] | 766 | crtc->primary->state->rotation & (DRM_ROTATE_90 | |
| 767 | DRM_ROTATE_270)) |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 768 | swap(hdisplay, vdisplay); |
| 769 | |
Daniel Vetter | 43968d7 | 2016-09-21 10:59:24 +0200 | [diff] [blame] | 770 | return drm_framebuffer_check_src_coords(x << 16, y << 16, |
| 771 | hdisplay << 16, vdisplay << 16, |
| 772 | fb); |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 773 | } |
Matt Roper | af93629 | 2014-04-01 15:22:34 -0700 | [diff] [blame] | 774 | EXPORT_SYMBOL(drm_crtc_check_viewport); |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 775 | |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 776 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 777 | * drm_mode_setcrtc - set CRTC configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 778 | * @dev: drm device for the ioctl |
| 779 | * @data: data pointer for the ioctl |
| 780 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 781 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 782 | * Build a new CRTC configuration based on user request. |
| 783 | * |
| 784 | * Called by the user via ioctl. |
| 785 | * |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 786 | * Returns: |
Daniel Vetter | 1a49863 | 2014-11-19 18:38:09 +0100 | [diff] [blame] | 787 | * Zero on success, negative errno on failure. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 788 | */ |
| 789 | int drm_mode_setcrtc(struct drm_device *dev, void *data, |
| 790 | struct drm_file *file_priv) |
| 791 | { |
| 792 | struct drm_mode_config *config = &dev->mode_config; |
| 793 | struct drm_mode_crtc *crtc_req = data; |
Ville Syrjälä | 6653cc8 | 2012-03-13 12:35:38 +0200 | [diff] [blame] | 794 | struct drm_crtc *crtc; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 795 | struct drm_connector **connector_set = NULL, *connector; |
| 796 | struct drm_framebuffer *fb = NULL; |
| 797 | struct drm_display_mode *mode = NULL; |
| 798 | struct drm_mode_set set; |
| 799 | uint32_t __user *set_connectors_ptr; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 800 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 801 | int i; |
| 802 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 803 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 804 | return -EINVAL; |
| 805 | |
Zhao Junwang | 01447e9 | 2015-07-07 17:08:35 +0800 | [diff] [blame] | 806 | /* |
| 807 | * Universal plane src offsets are only 16.16, prevent havoc for |
| 808 | * drivers using universal plane code internally. |
| 809 | */ |
| 810 | if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000) |
Ville Syrjälä | 1d97e91 | 2012-03-13 12:35:41 +0200 | [diff] [blame] | 811 | return -ERANGE; |
| 812 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 813 | drm_modeset_lock_all(dev); |
Rob Clark | a2b34e2 | 2013-10-05 16:36:52 -0400 | [diff] [blame] | 814 | crtc = drm_crtc_find(dev, crtc_req->crtc_id); |
| 815 | if (!crtc) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 816 | DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id); |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 817 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 818 | goto out; |
| 819 | } |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 820 | DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 821 | |
| 822 | if (crtc_req->mode_valid) { |
| 823 | /* If we have a mode we need a framebuffer. */ |
| 824 | /* If we pass -1, set the mode with the currently bound fb */ |
| 825 | if (crtc_req->fb_id == -1) { |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 826 | if (!crtc->primary->fb) { |
Ville Syrjälä | 6653cc8 | 2012-03-13 12:35:38 +0200 | [diff] [blame] | 827 | DRM_DEBUG_KMS("CRTC doesn't have current FB\n"); |
| 828 | ret = -EINVAL; |
| 829 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 830 | } |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 831 | fb = crtc->primary->fb; |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 832 | /* Make refcounting symmetric with the lookup path. */ |
| 833 | drm_framebuffer_reference(fb); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 834 | } else { |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 835 | fb = drm_framebuffer_lookup(dev, crtc_req->fb_id); |
| 836 | if (!fb) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 837 | DRM_DEBUG_KMS("Unknown FB ID%d\n", |
| 838 | crtc_req->fb_id); |
Ville Syrjälä | 37c4e70 | 2013-10-17 13:35:01 +0300 | [diff] [blame] | 839 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 840 | goto out; |
| 841 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | mode = drm_mode_create(dev); |
Ville Syrjälä | ee34ab5 | 2012-03-13 12:35:43 +0200 | [diff] [blame] | 845 | if (!mode) { |
| 846 | ret = -ENOMEM; |
| 847 | goto out; |
| 848 | } |
| 849 | |
Daniel Stone | 934a8a8 | 2015-05-22 13:34:48 +0100 | [diff] [blame] | 850 | ret = drm_mode_convert_umode(mode, &crtc_req->mode); |
Ville Syrjälä | 90367bf | 2012-03-13 12:35:44 +0200 | [diff] [blame] | 851 | if (ret) { |
| 852 | DRM_DEBUG_KMS("Invalid mode\n"); |
| 853 | goto out; |
| 854 | } |
| 855 | |
Laurent Pinchart | 7eb5f30 | 2015-03-09 10:41:07 +0200 | [diff] [blame] | 856 | /* |
| 857 | * Check whether the primary plane supports the fb pixel format. |
| 858 | * Drivers not implementing the universal planes API use a |
| 859 | * default formats list provided by the DRM core which doesn't |
| 860 | * match real hardware capabilities. Skip the check in that |
| 861 | * case. |
| 862 | */ |
| 863 | if (!crtc->primary->format_default) { |
| 864 | ret = drm_plane_check_pixel_format(crtc->primary, |
| 865 | fb->pixel_format); |
| 866 | if (ret) { |
Eric Engestrom | d382814 | 2016-08-15 16:29:55 +0100 | [diff] [blame] | 867 | char *format_name = drm_get_format_name(fb->pixel_format); |
Eric Engestrom | 90844f0 | 2016-08-15 01:02:38 +0100 | [diff] [blame] | 868 | DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name); |
| 869 | kfree(format_name); |
Laurent Pinchart | 7eb5f30 | 2015-03-09 10:41:07 +0200 | [diff] [blame] | 870 | goto out; |
| 871 | } |
| 872 | } |
| 873 | |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 874 | ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y, |
| 875 | mode, fb); |
| 876 | if (ret) |
Ville Syrjälä | 5f61bb4 | 2012-03-13 12:35:45 +0200 | [diff] [blame] | 877 | goto out; |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 878 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | if (crtc_req->count_connectors == 0 && mode) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 882 | DRM_DEBUG_KMS("Count connectors is 0 but mode set\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 883 | ret = -EINVAL; |
| 884 | goto out; |
| 885 | } |
| 886 | |
Jakob Bornecrantz | 7781de7 | 2009-08-03 13:43:58 +0100 | [diff] [blame] | 887 | if (crtc_req->count_connectors > 0 && (!mode || !fb)) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 888 | DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 889 | crtc_req->count_connectors); |
| 890 | ret = -EINVAL; |
| 891 | goto out; |
| 892 | } |
| 893 | |
| 894 | if (crtc_req->count_connectors > 0) { |
| 895 | u32 out_id; |
| 896 | |
| 897 | /* Avoid unbounded kernel memory allocation */ |
| 898 | if (crtc_req->count_connectors > config->num_connector) { |
| 899 | ret = -EINVAL; |
| 900 | goto out; |
| 901 | } |
| 902 | |
Thierry Reding | 2f6c538 | 2014-12-10 13:03:36 +0100 | [diff] [blame] | 903 | connector_set = kmalloc_array(crtc_req->count_connectors, |
| 904 | sizeof(struct drm_connector *), |
| 905 | GFP_KERNEL); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 906 | if (!connector_set) { |
| 907 | ret = -ENOMEM; |
| 908 | goto out; |
| 909 | } |
| 910 | |
| 911 | for (i = 0; i < crtc_req->count_connectors; i++) { |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 912 | connector_set[i] = NULL; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 913 | set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 914 | if (get_user(out_id, &set_connectors_ptr[i])) { |
| 915 | ret = -EFAULT; |
| 916 | goto out; |
| 917 | } |
| 918 | |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 919 | connector = drm_connector_lookup(dev, out_id); |
Rob Clark | a2b34e2 | 2013-10-05 16:36:52 -0400 | [diff] [blame] | 920 | if (!connector) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 921 | DRM_DEBUG_KMS("Connector id %d unknown\n", |
| 922 | out_id); |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 923 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 924 | goto out; |
| 925 | } |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 926 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", |
| 927 | connector->base.id, |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 928 | connector->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 929 | |
| 930 | connector_set[i] = connector; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | set.crtc = crtc; |
| 935 | set.x = crtc_req->x; |
| 936 | set.y = crtc_req->y; |
| 937 | set.mode = mode; |
| 938 | set.connectors = connector_set; |
| 939 | set.num_connectors = crtc_req->count_connectors; |
Dave Airlie | 5ef5f72 | 2009-08-17 13:11:23 +1000 | [diff] [blame] | 940 | set.fb = fb; |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 941 | ret = drm_mode_set_config_internal(&set); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 942 | |
| 943 | out: |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 944 | if (fb) |
| 945 | drm_framebuffer_unreference(fb); |
| 946 | |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 947 | if (connector_set) { |
| 948 | for (i = 0; i < crtc_req->count_connectors; i++) { |
| 949 | if (connector_set[i]) |
| 950 | drm_connector_unreference(connector_set[i]); |
| 951 | } |
| 952 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 953 | kfree(connector_set); |
Ville Syrjälä | ee34ab5 | 2012-03-13 12:35:43 +0200 | [diff] [blame] | 954 | drm_mode_destroy(dev, mode); |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 955 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 956 | return ret; |
| 957 | } |
| 958 | |
Daniel Vetter | 949619f | 2016-08-29 10:27:51 +0200 | [diff] [blame] | 959 | int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj, |
| 960 | struct drm_property *property, |
| 961 | uint64_t value) |
Paulo Zanoni | bffd9de0 | 2012-05-15 18:09:05 -0300 | [diff] [blame] | 962 | { |
| 963 | int ret = -EINVAL; |
| 964 | struct drm_crtc *crtc = obj_to_crtc(obj); |
| 965 | |
| 966 | if (crtc->funcs->set_property) |
| 967 | ret = crtc->funcs->set_property(crtc, property, value); |
| 968 | if (!ret) |
| 969 | drm_object_property_set_value(obj, property, value); |
| 970 | |
| 971 | return ret; |
| 972 | } |
| 973 | |
Thomas Wood | 3a5f87c | 2014-08-20 14:45:00 +0100 | [diff] [blame] | 974 | /** |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 975 | * drm_mode_config_reset - call ->reset callbacks |
| 976 | * @dev: drm device |
| 977 | * |
| 978 | * This functions calls all the crtc's, encoder's and connector's ->reset |
| 979 | * callback. Drivers can use this in e.g. their driver load or resume code to |
| 980 | * reset hardware and software state. |
| 981 | */ |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 982 | void drm_mode_config_reset(struct drm_device *dev) |
| 983 | { |
| 984 | struct drm_crtc *crtc; |
Daniel Vetter | 2a0d7cf | 2014-07-29 15:32:37 +0200 | [diff] [blame] | 985 | struct drm_plane *plane; |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 986 | struct drm_encoder *encoder; |
| 987 | struct drm_connector *connector; |
| 988 | |
Daniel Vetter | e4f6254 | 2015-07-09 23:44:35 +0200 | [diff] [blame] | 989 | drm_for_each_plane(plane, dev) |
Daniel Vetter | 2a0d7cf | 2014-07-29 15:32:37 +0200 | [diff] [blame] | 990 | if (plane->funcs->reset) |
| 991 | plane->funcs->reset(plane); |
| 992 | |
Daniel Vetter | e4f6254 | 2015-07-09 23:44:35 +0200 | [diff] [blame] | 993 | drm_for_each_crtc(crtc, dev) |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 994 | if (crtc->funcs->reset) |
| 995 | crtc->funcs->reset(crtc); |
| 996 | |
Daniel Vetter | e4f6254 | 2015-07-09 23:44:35 +0200 | [diff] [blame] | 997 | drm_for_each_encoder(encoder, dev) |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 998 | if (encoder->funcs->reset) |
| 999 | encoder->funcs->reset(encoder); |
| 1000 | |
Daniel Vetter | f8c2ba3 | 2015-07-29 08:32:43 +0200 | [diff] [blame] | 1001 | mutex_lock(&dev->mode_config.mutex); |
Dave Airlie | 4eebf60 | 2015-08-17 14:13:53 +1000 | [diff] [blame] | 1002 | drm_for_each_connector(connector, dev) |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 1003 | if (connector->funcs->reset) |
| 1004 | connector->funcs->reset(connector); |
Daniel Vetter | f8c2ba3 | 2015-07-29 08:32:43 +0200 | [diff] [blame] | 1005 | mutex_unlock(&dev->mode_config.mutex); |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 1006 | } |
| 1007 | EXPORT_SYMBOL(drm_mode_config_reset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1008 | |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 1009 | /** |
| 1010 | * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer |
| 1011 | * @dev: DRM device |
| 1012 | * @data: ioctl data |
| 1013 | * @file_priv: DRM file info |
| 1014 | * |
| 1015 | * This creates a new dumb buffer in the driver's backing storage manager (GEM, |
| 1016 | * TTM or something else entirely) and returns the resulting buffer handle. This |
| 1017 | * handle can then be wrapped up into a framebuffer modeset object. |
| 1018 | * |
| 1019 | * Note that userspace is not allowed to use such objects for render |
| 1020 | * acceleration - drivers must create their own private ioctls for such a use |
| 1021 | * case. |
| 1022 | * |
| 1023 | * Called by the user via ioctl. |
| 1024 | * |
| 1025 | * Returns: |
Daniel Vetter | 1a49863 | 2014-11-19 18:38:09 +0100 | [diff] [blame] | 1026 | * Zero on success, negative errno on failure. |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 1027 | */ |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1028 | int drm_mode_create_dumb_ioctl(struct drm_device *dev, |
| 1029 | void *data, struct drm_file *file_priv) |
| 1030 | { |
| 1031 | struct drm_mode_create_dumb *args = data; |
David Herrmann | b28cd41 | 2014-01-20 20:09:55 +0100 | [diff] [blame] | 1032 | u32 cpp, stride, size; |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1033 | |
| 1034 | if (!dev->driver->dumb_create) |
| 1035 | return -ENOSYS; |
David Herrmann | b28cd41 | 2014-01-20 20:09:55 +0100 | [diff] [blame] | 1036 | if (!args->width || !args->height || !args->bpp) |
| 1037 | return -EINVAL; |
| 1038 | |
| 1039 | /* overflow checks for 32bit size calculations */ |
David Herrmann | 00e7208 | 2014-08-24 19:23:26 +0200 | [diff] [blame] | 1040 | /* NOTE: DIV_ROUND_UP() can overflow */ |
David Herrmann | b28cd41 | 2014-01-20 20:09:55 +0100 | [diff] [blame] | 1041 | cpp = DIV_ROUND_UP(args->bpp, 8); |
David Herrmann | 00e7208 | 2014-08-24 19:23:26 +0200 | [diff] [blame] | 1042 | if (!cpp || cpp > 0xffffffffU / args->width) |
David Herrmann | b28cd41 | 2014-01-20 20:09:55 +0100 | [diff] [blame] | 1043 | return -EINVAL; |
| 1044 | stride = cpp * args->width; |
| 1045 | if (args->height > 0xffffffffU / stride) |
| 1046 | return -EINVAL; |
| 1047 | |
| 1048 | /* test for wrap-around */ |
| 1049 | size = args->height * stride; |
| 1050 | if (PAGE_ALIGN(size) == 0) |
| 1051 | return -EINVAL; |
| 1052 | |
Thierry Reding | f608595 | 2014-11-03 11:14:14 +0100 | [diff] [blame] | 1053 | /* |
| 1054 | * handle, pitch and size are output parameters. Zero them out to |
| 1055 | * prevent drivers from accidentally using uninitialized data. Since |
| 1056 | * not all existing userspace is clearing these fields properly we |
| 1057 | * cannot reject IOCTL with garbage in them. |
| 1058 | */ |
| 1059 | args->handle = 0; |
| 1060 | args->pitch = 0; |
| 1061 | args->size = 0; |
| 1062 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1063 | return dev->driver->dumb_create(file_priv, dev, args); |
| 1064 | } |
| 1065 | |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 1066 | /** |
| 1067 | * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer |
| 1068 | * @dev: DRM device |
| 1069 | * @data: ioctl data |
| 1070 | * @file_priv: DRM file info |
| 1071 | * |
| 1072 | * Allocate an offset in the drm device node's address space to be able to |
| 1073 | * memory map a dumb buffer. |
| 1074 | * |
| 1075 | * Called by the user via ioctl. |
| 1076 | * |
| 1077 | * Returns: |
Daniel Vetter | 1a49863 | 2014-11-19 18:38:09 +0100 | [diff] [blame] | 1078 | * Zero on success, negative errno on failure. |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 1079 | */ |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1080 | int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, |
| 1081 | void *data, struct drm_file *file_priv) |
| 1082 | { |
| 1083 | struct drm_mode_map_dumb *args = data; |
| 1084 | |
| 1085 | /* call driver ioctl to get mmap offset */ |
| 1086 | if (!dev->driver->dumb_map_offset) |
| 1087 | return -ENOSYS; |
| 1088 | |
| 1089 | return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset); |
| 1090 | } |
| 1091 | |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 1092 | /** |
| 1093 | * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer |
| 1094 | * @dev: DRM device |
| 1095 | * @data: ioctl data |
| 1096 | * @file_priv: DRM file info |
| 1097 | * |
| 1098 | * This destroys the userspace handle for the given dumb backing storage buffer. |
| 1099 | * Since buffer objects must be reference counted in the kernel a buffer object |
| 1100 | * won't be immediately freed if a framebuffer modeset object still uses it. |
| 1101 | * |
| 1102 | * Called by the user via ioctl. |
| 1103 | * |
| 1104 | * Returns: |
Daniel Vetter | 1a49863 | 2014-11-19 18:38:09 +0100 | [diff] [blame] | 1105 | * Zero on success, negative errno on failure. |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 1106 | */ |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1107 | int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, |
| 1108 | void *data, struct drm_file *file_priv) |
| 1109 | { |
| 1110 | struct drm_mode_destroy_dumb *args = data; |
| 1111 | |
| 1112 | if (!dev->driver->dumb_destroy) |
| 1113 | return -ENOSYS; |
| 1114 | |
| 1115 | return dev->driver->dumb_destroy(file_priv, dev, args->handle); |
| 1116 | } |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 1117 | |
Daniel Vetter | c8e32cc | 2014-03-10 21:33:02 +0100 | [diff] [blame] | 1118 | /** |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1119 | * drm_mode_config_init - initialize DRM mode_configuration structure |
| 1120 | * @dev: DRM device |
| 1121 | * |
| 1122 | * Initialize @dev's mode_config structure, used for tracking the graphics |
| 1123 | * configuration of @dev. |
| 1124 | * |
| 1125 | * Since this initializes the modeset locks, no locking is possible. Which is no |
| 1126 | * problem, since this should happen single threaded at init time. It is the |
| 1127 | * driver's problem to ensure this guarantee. |
| 1128 | * |
| 1129 | */ |
| 1130 | void drm_mode_config_init(struct drm_device *dev) |
| 1131 | { |
| 1132 | mutex_init(&dev->mode_config.mutex); |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 1133 | drm_modeset_lock_init(&dev->mode_config.connection_mutex); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1134 | mutex_init(&dev->mode_config.idr_mutex); |
| 1135 | mutex_init(&dev->mode_config.fb_lock); |
Daniel Stone | 8fb6e7a | 2015-04-20 19:22:54 +0100 | [diff] [blame] | 1136 | mutex_init(&dev->mode_config.blob_lock); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1137 | INIT_LIST_HEAD(&dev->mode_config.fb_list); |
| 1138 | INIT_LIST_HEAD(&dev->mode_config.crtc_list); |
| 1139 | INIT_LIST_HEAD(&dev->mode_config.connector_list); |
| 1140 | INIT_LIST_HEAD(&dev->mode_config.encoder_list); |
| 1141 | INIT_LIST_HEAD(&dev->mode_config.property_list); |
| 1142 | INIT_LIST_HEAD(&dev->mode_config.property_blob_list); |
| 1143 | INIT_LIST_HEAD(&dev->mode_config.plane_list); |
| 1144 | idr_init(&dev->mode_config.crtc_idr); |
Dave Airlie | 138f9eb | 2014-10-20 16:17:17 +1000 | [diff] [blame] | 1145 | idr_init(&dev->mode_config.tile_idr); |
Maarten Lankhorst | 5fff80b | 2016-02-17 08:32:05 +0100 | [diff] [blame] | 1146 | ida_init(&dev->mode_config.connector_ida); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1147 | |
| 1148 | drm_modeset_lock_all(dev); |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 1149 | drm_mode_create_standard_properties(dev); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1150 | drm_modeset_unlock_all(dev); |
| 1151 | |
| 1152 | /* Just to be sure */ |
| 1153 | dev->mode_config.num_fb = 0; |
| 1154 | dev->mode_config.num_connector = 0; |
| 1155 | dev->mode_config.num_crtc = 0; |
| 1156 | dev->mode_config.num_encoder = 0; |
Matt Roper | e27dde3 | 2014-04-01 15:22:30 -0700 | [diff] [blame] | 1157 | dev->mode_config.num_overlay_plane = 0; |
| 1158 | dev->mode_config.num_total_plane = 0; |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1159 | } |
| 1160 | EXPORT_SYMBOL(drm_mode_config_init); |
| 1161 | |
| 1162 | /** |
| 1163 | * drm_mode_config_cleanup - free up DRM mode_config info |
| 1164 | * @dev: DRM device |
| 1165 | * |
| 1166 | * Free up all the connectors and CRTCs associated with this DRM device, then |
| 1167 | * free up the framebuffers and associated buffer objects. |
| 1168 | * |
| 1169 | * Note that since this /should/ happen single-threaded at driver/device |
| 1170 | * teardown time, no locking is required. It's the driver's job to ensure that |
| 1171 | * this guarantee actually holds true. |
| 1172 | * |
| 1173 | * FIXME: cleanup any dangling user buffer objects too |
| 1174 | */ |
| 1175 | void drm_mode_config_cleanup(struct drm_device *dev) |
| 1176 | { |
| 1177 | struct drm_connector *connector, *ot; |
| 1178 | struct drm_crtc *crtc, *ct; |
| 1179 | struct drm_encoder *encoder, *enct; |
| 1180 | struct drm_framebuffer *fb, *fbt; |
| 1181 | struct drm_property *property, *pt; |
| 1182 | struct drm_property_blob *blob, *bt; |
| 1183 | struct drm_plane *plane, *plt; |
| 1184 | |
| 1185 | list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, |
| 1186 | head) { |
| 1187 | encoder->funcs->destroy(encoder); |
| 1188 | } |
| 1189 | |
| 1190 | list_for_each_entry_safe(connector, ot, |
| 1191 | &dev->mode_config.connector_list, head) { |
| 1192 | connector->funcs->destroy(connector); |
| 1193 | } |
| 1194 | |
| 1195 | list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, |
| 1196 | head) { |
| 1197 | drm_property_destroy(dev, property); |
| 1198 | } |
| 1199 | |
Maarten Lankhorst | f35034f | 2016-03-22 15:42:14 +0100 | [diff] [blame] | 1200 | list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list, |
| 1201 | head) { |
| 1202 | plane->funcs->destroy(plane); |
| 1203 | } |
| 1204 | |
| 1205 | list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) { |
| 1206 | crtc->funcs->destroy(crtc); |
| 1207 | } |
| 1208 | |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1209 | list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list, |
Daniel Stone | e2f5d2e | 2015-05-22 13:34:51 +0100 | [diff] [blame] | 1210 | head_global) { |
Daniel Stone | 6bcacf5 | 2015-04-20 19:22:55 +0100 | [diff] [blame] | 1211 | drm_property_unreference_blob(blob); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Single-threaded teardown context, so it's not required to grab the |
| 1216 | * fb_lock to protect against concurrent fb_list access. Contrary, it |
| 1217 | * would actually deadlock with the drm_framebuffer_cleanup function. |
| 1218 | * |
| 1219 | * Also, if there are any framebuffers left, that's a driver leak now, |
| 1220 | * so politely WARN about this. |
| 1221 | */ |
| 1222 | WARN_ON(!list_empty(&dev->mode_config.fb_list)); |
| 1223 | list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) { |
Dave Airlie | d0f37cf6 | 2016-04-15 15:10:36 +1000 | [diff] [blame] | 1224 | drm_framebuffer_free(&fb->base.refcount); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
Maarten Lankhorst | 5fff80b | 2016-02-17 08:32:05 +0100 | [diff] [blame] | 1227 | ida_destroy(&dev->mode_config.connector_ida); |
Dave Airlie | 138f9eb | 2014-10-20 16:17:17 +1000 | [diff] [blame] | 1228 | idr_destroy(&dev->mode_config.tile_idr); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1229 | idr_destroy(&dev->mode_config.crtc_idr); |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 1230 | drm_modeset_lock_fini(&dev->mode_config.connection_mutex); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 1231 | } |
| 1232 | EXPORT_SYMBOL(drm_mode_config_cleanup); |
Ville Syrjälä | c1df5f3 | 2014-07-08 10:31:53 +0530 | [diff] [blame] | 1233 | |
Dave Airlie | 138f9eb | 2014-10-20 16:17:17 +1000 | [diff] [blame] | 1234 | /** |
| 1235 | * DOC: Tile group |
| 1236 | * |
| 1237 | * Tile groups are used to represent tiled monitors with a unique |
| 1238 | * integer identifier. Tiled monitors using DisplayID v1.3 have |
| 1239 | * a unique 8-byte handle, we store this in a tile group, so we |
| 1240 | * have a common identifier for all tiles in a monitor group. |
| 1241 | */ |
| 1242 | static void drm_tile_group_free(struct kref *kref) |
| 1243 | { |
| 1244 | struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount); |
| 1245 | struct drm_device *dev = tg->dev; |
| 1246 | mutex_lock(&dev->mode_config.idr_mutex); |
| 1247 | idr_remove(&dev->mode_config.tile_idr, tg->id); |
| 1248 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 1249 | kfree(tg); |
| 1250 | } |
| 1251 | |
| 1252 | /** |
| 1253 | * drm_mode_put_tile_group - drop a reference to a tile group. |
| 1254 | * @dev: DRM device |
| 1255 | * @tg: tile group to drop reference to. |
| 1256 | * |
| 1257 | * drop reference to tile group and free if 0. |
| 1258 | */ |
| 1259 | void drm_mode_put_tile_group(struct drm_device *dev, |
| 1260 | struct drm_tile_group *tg) |
| 1261 | { |
| 1262 | kref_put(&tg->refcount, drm_tile_group_free); |
| 1263 | } |
| 1264 | |
| 1265 | /** |
| 1266 | * drm_mode_get_tile_group - get a reference to an existing tile group |
| 1267 | * @dev: DRM device |
| 1268 | * @topology: 8-bytes unique per monitor. |
| 1269 | * |
| 1270 | * Use the unique bytes to get a reference to an existing tile group. |
| 1271 | * |
| 1272 | * RETURNS: |
| 1273 | * tile group or NULL if not found. |
| 1274 | */ |
| 1275 | struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev, |
| 1276 | char topology[8]) |
| 1277 | { |
| 1278 | struct drm_tile_group *tg; |
| 1279 | int id; |
| 1280 | mutex_lock(&dev->mode_config.idr_mutex); |
| 1281 | idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) { |
| 1282 | if (!memcmp(tg->group_data, topology, 8)) { |
| 1283 | if (!kref_get_unless_zero(&tg->refcount)) |
| 1284 | tg = NULL; |
| 1285 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 1286 | return tg; |
| 1287 | } |
| 1288 | } |
| 1289 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 1290 | return NULL; |
| 1291 | } |
Rob Clark | 81ddd1b | 2015-03-27 13:01:52 -0400 | [diff] [blame] | 1292 | EXPORT_SYMBOL(drm_mode_get_tile_group); |
Dave Airlie | 138f9eb | 2014-10-20 16:17:17 +1000 | [diff] [blame] | 1293 | |
| 1294 | /** |
| 1295 | * drm_mode_create_tile_group - create a tile group from a displayid description |
| 1296 | * @dev: DRM device |
| 1297 | * @topology: 8-bytes unique per monitor. |
| 1298 | * |
| 1299 | * Create a tile group for the unique monitor, and get a unique |
| 1300 | * identifier for the tile group. |
| 1301 | * |
| 1302 | * RETURNS: |
| 1303 | * new tile group or error. |
| 1304 | */ |
| 1305 | struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev, |
| 1306 | char topology[8]) |
| 1307 | { |
| 1308 | struct drm_tile_group *tg; |
| 1309 | int ret; |
| 1310 | |
| 1311 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); |
| 1312 | if (!tg) |
| 1313 | return ERR_PTR(-ENOMEM); |
| 1314 | |
| 1315 | kref_init(&tg->refcount); |
| 1316 | memcpy(tg->group_data, topology, 8); |
| 1317 | tg->dev = dev; |
| 1318 | |
| 1319 | mutex_lock(&dev->mode_config.idr_mutex); |
| 1320 | ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL); |
| 1321 | if (ret >= 0) { |
| 1322 | tg->id = ret; |
| 1323 | } else { |
| 1324 | kfree(tg); |
| 1325 | tg = ERR_PTR(ret); |
| 1326 | } |
| 1327 | |
| 1328 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 1329 | return tg; |
| 1330 | } |
Rob Clark | 81ddd1b | 2015-03-27 13:01:52 -0400 | [diff] [blame] | 1331 | EXPORT_SYMBOL(drm_mode_create_tile_group); |