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> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 36 | #include <drm/drmP.h> |
| 37 | #include <drm/drm_crtc.h> |
| 38 | #include <drm/drm_edid.h> |
| 39 | #include <drm/drm_fourcc.h> |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 40 | |
Daniel Vetter | 8bd441b | 2014-01-23 15:35:24 +0100 | [diff] [blame^] | 41 | #include "drm_crtc_internal.h" |
| 42 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 43 | /** |
| 44 | * drm_modeset_lock_all - take all modeset locks |
| 45 | * @dev: drm device |
| 46 | * |
| 47 | * This function takes all modeset locks, suitable where a more fine-grained |
| 48 | * scheme isn't (yet) implemented. |
| 49 | */ |
| 50 | void drm_modeset_lock_all(struct drm_device *dev) |
| 51 | { |
Daniel Vetter | 29494c1 | 2012-12-02 02:18:25 +0100 | [diff] [blame] | 52 | struct drm_crtc *crtc; |
| 53 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 54 | mutex_lock(&dev->mode_config.mutex); |
Daniel Vetter | 29494c1 | 2012-12-02 02:18:25 +0100 | [diff] [blame] | 55 | |
| 56 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 57 | mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex); |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 58 | } |
| 59 | EXPORT_SYMBOL(drm_modeset_lock_all); |
| 60 | |
| 61 | /** |
| 62 | * drm_modeset_unlock_all - drop all modeset locks |
| 63 | * @dev: device |
| 64 | */ |
| 65 | void drm_modeset_unlock_all(struct drm_device *dev) |
| 66 | { |
Daniel Vetter | 29494c1 | 2012-12-02 02:18:25 +0100 | [diff] [blame] | 67 | struct drm_crtc *crtc; |
| 68 | |
| 69 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 70 | mutex_unlock(&crtc->mutex); |
| 71 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 72 | mutex_unlock(&dev->mode_config.mutex); |
| 73 | } |
| 74 | EXPORT_SYMBOL(drm_modeset_unlock_all); |
| 75 | |
Daniel Vetter | 6aed8ec | 2013-01-20 17:32:21 +0100 | [diff] [blame] | 76 | /** |
| 77 | * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked |
| 78 | * @dev: device |
| 79 | */ |
| 80 | void drm_warn_on_modeset_not_all_locked(struct drm_device *dev) |
| 81 | { |
| 82 | struct drm_crtc *crtc; |
| 83 | |
Daniel Vetter | a9b054e | 2013-05-02 09:43:05 +0200 | [diff] [blame] | 84 | /* Locking is currently fubar in the panic handler. */ |
| 85 | if (oops_in_progress) |
| 86 | return; |
| 87 | |
Daniel Vetter | 6aed8ec | 2013-01-20 17:32:21 +0100 | [diff] [blame] | 88 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 89 | WARN_ON(!mutex_is_locked(&crtc->mutex)); |
| 90 | |
| 91 | WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); |
| 92 | } |
| 93 | EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked); |
| 94 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 95 | /* Avoid boilerplate. I'm tired of typing. */ |
| 96 | #define DRM_ENUM_NAME_FN(fnname, list) \ |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 97 | const char *fnname(int val) \ |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 98 | { \ |
| 99 | int i; \ |
| 100 | for (i = 0; i < ARRAY_SIZE(list); i++) { \ |
| 101 | if (list[i].type == val) \ |
| 102 | return list[i].name; \ |
| 103 | } \ |
| 104 | return "(unknown)"; \ |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Global properties |
| 109 | */ |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 110 | static const struct drm_prop_enum_list drm_dpms_enum_list[] = |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 111 | { { DRM_MODE_DPMS_ON, "On" }, |
| 112 | { DRM_MODE_DPMS_STANDBY, "Standby" }, |
| 113 | { DRM_MODE_DPMS_SUSPEND, "Suspend" }, |
| 114 | { DRM_MODE_DPMS_OFF, "Off" } |
| 115 | }; |
| 116 | |
| 117 | DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list) |
| 118 | |
| 119 | /* |
| 120 | * Optional properties |
| 121 | */ |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 122 | static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 123 | { |
Jesse Barnes | 53bd838 | 2009-07-01 10:04:40 -0700 | [diff] [blame] | 124 | { DRM_MODE_SCALE_NONE, "None" }, |
| 125 | { DRM_MODE_SCALE_FULLSCREEN, "Full" }, |
| 126 | { DRM_MODE_SCALE_CENTER, "Center" }, |
| 127 | { DRM_MODE_SCALE_ASPECT, "Full aspect" }, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 130 | /* |
| 131 | * Non-global properties, but "required" for certain connectors. |
| 132 | */ |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 133 | static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 134 | { |
| 135 | { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ |
| 136 | { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */ |
| 137 | { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */ |
| 138 | }; |
| 139 | |
| 140 | DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list) |
| 141 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 142 | static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 143 | { |
| 144 | { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */ |
| 145 | { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */ |
| 146 | { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */ |
| 147 | }; |
| 148 | |
| 149 | DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name, |
| 150 | drm_dvi_i_subconnector_enum_list) |
| 151 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 152 | static const struct drm_prop_enum_list drm_tv_select_enum_list[] = |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 153 | { |
| 154 | { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ |
| 155 | { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ |
| 156 | { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */ |
| 157 | { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */ |
Francisco Jerez | aeaa1ad | 2009-08-02 04:19:19 +0200 | [diff] [blame] | 158 | { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */ |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list) |
| 162 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 163 | static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 164 | { |
| 165 | { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */ |
| 166 | { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ |
| 167 | { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */ |
| 168 | { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */ |
Francisco Jerez | aeaa1ad | 2009-08-02 04:19:19 +0200 | [diff] [blame] | 169 | { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */ |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name, |
| 173 | drm_tv_subconnector_enum_list) |
| 174 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 175 | static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = { |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 176 | { DRM_MODE_DIRTY_OFF, "Off" }, |
| 177 | { DRM_MODE_DIRTY_ON, "On" }, |
| 178 | { DRM_MODE_DIRTY_ANNOTATE, "Annotate" }, |
| 179 | }; |
| 180 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 181 | struct drm_conn_prop_enum_list { |
| 182 | int type; |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 183 | const char *name; |
Ilia Mirkin | b21e3af | 2013-08-07 22:34:48 -0400 | [diff] [blame] | 184 | struct ida ida; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /* |
| 188 | * Connector and encoder types. |
| 189 | */ |
| 190 | static struct drm_conn_prop_enum_list drm_connector_enum_list[] = |
Ilia Mirkin | b21e3af | 2013-08-07 22:34:48 -0400 | [diff] [blame] | 191 | { { DRM_MODE_CONNECTOR_Unknown, "Unknown" }, |
| 192 | { DRM_MODE_CONNECTOR_VGA, "VGA" }, |
| 193 | { DRM_MODE_CONNECTOR_DVII, "DVI-I" }, |
| 194 | { DRM_MODE_CONNECTOR_DVID, "DVI-D" }, |
| 195 | { DRM_MODE_CONNECTOR_DVIA, "DVI-A" }, |
| 196 | { DRM_MODE_CONNECTOR_Composite, "Composite" }, |
| 197 | { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" }, |
| 198 | { DRM_MODE_CONNECTOR_LVDS, "LVDS" }, |
| 199 | { DRM_MODE_CONNECTOR_Component, "Component" }, |
| 200 | { DRM_MODE_CONNECTOR_9PinDIN, "DIN" }, |
| 201 | { DRM_MODE_CONNECTOR_DisplayPort, "DP" }, |
| 202 | { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" }, |
| 203 | { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" }, |
| 204 | { DRM_MODE_CONNECTOR_TV, "TV" }, |
| 205 | { DRM_MODE_CONNECTOR_eDP, "eDP" }, |
| 206 | { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" }, |
Shobhit Kumar | b892327 | 2013-08-27 15:12:13 +0300 | [diff] [blame] | 207 | { DRM_MODE_CONNECTOR_DSI, "DSI" }, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 208 | }; |
| 209 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 210 | static const struct drm_prop_enum_list drm_encoder_enum_list[] = |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 211 | { { DRM_MODE_ENCODER_NONE, "None" }, |
| 212 | { DRM_MODE_ENCODER_DAC, "DAC" }, |
| 213 | { DRM_MODE_ENCODER_TMDS, "TMDS" }, |
| 214 | { DRM_MODE_ENCODER_LVDS, "LVDS" }, |
| 215 | { DRM_MODE_ENCODER_TVDAC, "TV" }, |
Thomas Hellstrom | a7331e5 | 2011-10-22 10:36:19 +0200 | [diff] [blame] | 216 | { DRM_MODE_ENCODER_VIRTUAL, "Virtual" }, |
Shobhit Kumar | b892327 | 2013-08-27 15:12:13 +0300 | [diff] [blame] | 217 | { DRM_MODE_ENCODER_DSI, "DSI" }, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
Jesse Barnes | ac1bb36 | 2014-02-10 15:32:44 -0800 | [diff] [blame] | 220 | static const struct drm_prop_enum_list drm_subpixel_enum_list[] = |
| 221 | { |
| 222 | { SubPixelUnknown, "Unknown" }, |
| 223 | { SubPixelHorizontalRGB, "Horizontal RGB" }, |
| 224 | { SubPixelHorizontalBGR, "Horizontal BGR" }, |
| 225 | { SubPixelVerticalRGB, "Vertical RGB" }, |
| 226 | { SubPixelVerticalBGR, "Vertical BGR" }, |
| 227 | { SubPixelNone, "None" }, |
| 228 | }; |
| 229 | |
Ilia Mirkin | b21e3af | 2013-08-07 22:34:48 -0400 | [diff] [blame] | 230 | void drm_connector_ida_init(void) |
| 231 | { |
| 232 | int i; |
| 233 | |
| 234 | for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++) |
| 235 | ida_init(&drm_connector_enum_list[i].ida); |
| 236 | } |
| 237 | |
| 238 | void drm_connector_ida_destroy(void) |
| 239 | { |
| 240 | int i; |
| 241 | |
| 242 | for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++) |
| 243 | ida_destroy(&drm_connector_enum_list[i].ida); |
| 244 | } |
| 245 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 246 | const char *drm_get_encoder_name(const struct drm_encoder *encoder) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 247 | { |
| 248 | static char buf[32]; |
| 249 | |
| 250 | snprintf(buf, 32, "%s-%d", |
| 251 | drm_encoder_enum_list[encoder->encoder_type].name, |
| 252 | encoder->base.id); |
| 253 | return buf; |
| 254 | } |
Dave Airlie | 13a8195 | 2009-09-07 15:45:33 +1000 | [diff] [blame] | 255 | EXPORT_SYMBOL(drm_get_encoder_name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 256 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 257 | const char *drm_get_connector_name(const struct drm_connector *connector) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 258 | { |
| 259 | static char buf[32]; |
| 260 | |
| 261 | snprintf(buf, 32, "%s-%d", |
| 262 | drm_connector_enum_list[connector->connector_type].name, |
| 263 | connector->connector_type_id); |
| 264 | return buf; |
| 265 | } |
| 266 | EXPORT_SYMBOL(drm_get_connector_name); |
| 267 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 268 | const char *drm_get_connector_status_name(enum drm_connector_status status) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 269 | { |
| 270 | if (status == connector_status_connected) |
| 271 | return "connected"; |
| 272 | else if (status == connector_status_disconnected) |
| 273 | return "disconnected"; |
| 274 | else |
| 275 | return "unknown"; |
| 276 | } |
Lespiau, Damien | ed7951d | 2013-05-10 12:36:42 +0000 | [diff] [blame] | 277 | EXPORT_SYMBOL(drm_get_connector_status_name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 278 | |
Jesse Barnes | ac1bb36 | 2014-02-10 15:32:44 -0800 | [diff] [blame] | 279 | /** |
| 280 | * drm_get_subpixel_order_name - return a string for a given subpixel enum |
| 281 | * @order: enum of subpixel_order |
| 282 | * |
| 283 | * Note you could abuse this and return something out of bounds, but that |
| 284 | * would be a caller error. No unscrubbed user data should make it here. |
| 285 | */ |
| 286 | const char *drm_get_subpixel_order_name(enum subpixel_order order) |
| 287 | { |
| 288 | return drm_subpixel_enum_list[order].name; |
| 289 | } |
| 290 | EXPORT_SYMBOL(drm_get_subpixel_order_name); |
| 291 | |
Ville Syrjälä | 6ba6d03 | 2013-06-10 11:15:10 +0300 | [diff] [blame] | 292 | static char printable_char(int c) |
| 293 | { |
| 294 | return isascii(c) && isprint(c) ? c : '?'; |
| 295 | } |
| 296 | |
Ville Syrjälä | d20d317 | 2013-06-07 15:43:07 +0000 | [diff] [blame] | 297 | const char *drm_get_format_name(uint32_t format) |
Ville Syrjälä | 6ba6d03 | 2013-06-10 11:15:10 +0300 | [diff] [blame] | 298 | { |
| 299 | static char buf[32]; |
| 300 | |
| 301 | snprintf(buf, sizeof(buf), |
| 302 | "%c%c%c%c %s-endian (0x%08x)", |
| 303 | printable_char(format & 0xff), |
| 304 | printable_char((format >> 8) & 0xff), |
| 305 | printable_char((format >> 16) & 0xff), |
| 306 | printable_char((format >> 24) & 0x7f), |
| 307 | format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little", |
| 308 | format); |
| 309 | |
| 310 | return buf; |
| 311 | } |
| 312 | EXPORT_SYMBOL(drm_get_format_name); |
| 313 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 314 | /** |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 315 | * drm_mode_object_get - allocate a new modeset identifier |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 316 | * @dev: DRM device |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 317 | * @obj: object pointer, used to generate unique ID |
| 318 | * @obj_type: object type |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 319 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 320 | * Create a unique identifier based on @ptr in @dev's identifier space. Used |
| 321 | * for tracking modes, CRTCs and connectors. |
| 322 | * |
| 323 | * RETURNS: |
| 324 | * New unique (relative to other objects in @dev) integer identifier for the |
| 325 | * object. |
| 326 | */ |
Daniel Vetter | 8bd441b | 2014-01-23 15:35:24 +0100 | [diff] [blame^] | 327 | int drm_mode_object_get(struct drm_device *dev, |
| 328 | struct drm_mode_object *obj, uint32_t obj_type) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 329 | { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 330 | int ret; |
| 331 | |
Jesse Barnes | ad2563c | 2009-01-19 17:21:45 +1000 | [diff] [blame] | 332 | mutex_lock(&dev->mode_config.idr_mutex); |
Tejun Heo | 2e92881 | 2013-02-27 17:04:08 -0800 | [diff] [blame] | 333 | ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL); |
| 334 | if (ret >= 0) { |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 335 | /* |
| 336 | * Set up the object linking under the protection of the idr |
| 337 | * lock so that other users can't see inconsistent state. |
| 338 | */ |
Tejun Heo | 2e92881 | 2013-02-27 17:04:08 -0800 | [diff] [blame] | 339 | obj->id = ret; |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 340 | obj->type = obj_type; |
| 341 | } |
Jesse Barnes | ad2563c | 2009-01-19 17:21:45 +1000 | [diff] [blame] | 342 | mutex_unlock(&dev->mode_config.idr_mutex); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 343 | |
Tejun Heo | 2e92881 | 2013-02-27 17:04:08 -0800 | [diff] [blame] | 344 | return ret < 0 ? ret : 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 348 | * drm_mode_object_put - free a modeset identifer |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 349 | * @dev: DRM device |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 350 | * @object: object to free |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 351 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 352 | * Free @id from @dev's unique identifier pool. |
| 353 | */ |
Daniel Vetter | 8bd441b | 2014-01-23 15:35:24 +0100 | [diff] [blame^] | 354 | void drm_mode_object_put(struct drm_device *dev, |
| 355 | struct drm_mode_object *object) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 356 | { |
Jesse Barnes | ad2563c | 2009-01-19 17:21:45 +1000 | [diff] [blame] | 357 | mutex_lock(&dev->mode_config.idr_mutex); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 358 | idr_remove(&dev->mode_config.crtc_idr, object->id); |
Jesse Barnes | ad2563c | 2009-01-19 17:21:45 +1000 | [diff] [blame] | 359 | mutex_unlock(&dev->mode_config.idr_mutex); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 362 | /** |
| 363 | * drm_mode_object_find - look up a drm object with static lifetime |
| 364 | * @dev: drm device |
| 365 | * @id: id of the mode object |
| 366 | * @type: type of the mode object |
| 367 | * |
| 368 | * Note that framebuffers cannot be looked up with this functions - since those |
| 369 | * are reference counted, they need special treatment. |
| 370 | */ |
Daniel Vetter | 7a9c906 | 2009-09-15 22:57:31 +0200 | [diff] [blame] | 371 | struct drm_mode_object *drm_mode_object_find(struct drm_device *dev, |
| 372 | uint32_t id, uint32_t type) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 373 | { |
Jesse Barnes | ad2563c | 2009-01-19 17:21:45 +1000 | [diff] [blame] | 374 | struct drm_mode_object *obj = NULL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 375 | |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 376 | /* Framebuffers are reference counted and need their own lookup |
| 377 | * function.*/ |
| 378 | WARN_ON(type == DRM_MODE_OBJECT_FB); |
| 379 | |
Jesse Barnes | ad2563c | 2009-01-19 17:21:45 +1000 | [diff] [blame] | 380 | mutex_lock(&dev->mode_config.idr_mutex); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 381 | obj = idr_find(&dev->mode_config.crtc_idr, id); |
| 382 | if (!obj || (obj->type != type) || (obj->id != id)) |
Jesse Barnes | ad2563c | 2009-01-19 17:21:45 +1000 | [diff] [blame] | 383 | obj = NULL; |
| 384 | mutex_unlock(&dev->mode_config.idr_mutex); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 385 | |
| 386 | return obj; |
| 387 | } |
| 388 | EXPORT_SYMBOL(drm_mode_object_find); |
| 389 | |
| 390 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 391 | * drm_framebuffer_init - initialize a framebuffer |
| 392 | * @dev: DRM device |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 393 | * @fb: framebuffer to be initialized |
| 394 | * @funcs: ... with these functions |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 395 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 396 | * Allocates an ID for the framebuffer's parent mode object, sets its mode |
| 397 | * functions & device file and adds it to the master fd list. |
| 398 | * |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 399 | * IMPORTANT: |
| 400 | * This functions publishes the fb and makes it available for concurrent access |
| 401 | * by other users. Which means by this point the fb _must_ be fully set up - |
| 402 | * since all the fb attributes are invariant over its lifetime, no further |
| 403 | * locking but only correct reference counting is required. |
| 404 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 405 | * RETURNS: |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 406 | * Zero on success, error code on failure. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 407 | */ |
| 408 | int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, |
| 409 | const struct drm_framebuffer_funcs *funcs) |
| 410 | { |
| 411 | int ret; |
| 412 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 413 | mutex_lock(&dev->mode_config.fb_lock); |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 414 | kref_init(&fb->refcount); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 415 | INIT_LIST_HEAD(&fb->filp_head); |
| 416 | fb->dev = dev; |
| 417 | fb->funcs = funcs; |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 418 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 419 | ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB); |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 420 | if (ret) |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 421 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 422 | |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 423 | /* Grab the idr reference. */ |
| 424 | drm_framebuffer_reference(fb); |
| 425 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 426 | dev->mode_config.num_fb++; |
| 427 | list_add(&fb->head, &dev->mode_config.fb_list); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 428 | out: |
| 429 | mutex_unlock(&dev->mode_config.fb_lock); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | EXPORT_SYMBOL(drm_framebuffer_init); |
| 434 | |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 435 | static void drm_framebuffer_free(struct kref *kref) |
| 436 | { |
| 437 | struct drm_framebuffer *fb = |
| 438 | container_of(kref, struct drm_framebuffer, refcount); |
| 439 | fb->funcs->destroy(fb); |
| 440 | } |
| 441 | |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 442 | static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev, |
| 443 | uint32_t id) |
| 444 | { |
| 445 | struct drm_mode_object *obj = NULL; |
| 446 | struct drm_framebuffer *fb; |
| 447 | |
| 448 | mutex_lock(&dev->mode_config.idr_mutex); |
| 449 | obj = idr_find(&dev->mode_config.crtc_idr, id); |
| 450 | if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id)) |
| 451 | fb = NULL; |
| 452 | else |
| 453 | fb = obj_to_fb(obj); |
| 454 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 455 | |
| 456 | return fb; |
| 457 | } |
| 458 | |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 459 | /** |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 460 | * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference |
| 461 | * @dev: drm device |
| 462 | * @id: id of the fb object |
| 463 | * |
| 464 | * If successful, this grabs an additional reference to the framebuffer - |
| 465 | * callers need to make sure to eventually unreference the returned framebuffer |
| 466 | * again. |
| 467 | */ |
| 468 | struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, |
| 469 | uint32_t id) |
| 470 | { |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 471 | struct drm_framebuffer *fb; |
| 472 | |
| 473 | mutex_lock(&dev->mode_config.fb_lock); |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 474 | fb = __drm_framebuffer_lookup(dev, id); |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 475 | if (fb) |
archit taneja | 9131d3d | 2013-04-10 08:59:39 +0000 | [diff] [blame] | 476 | drm_framebuffer_reference(fb); |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 477 | mutex_unlock(&dev->mode_config.fb_lock); |
| 478 | |
| 479 | return fb; |
| 480 | } |
| 481 | EXPORT_SYMBOL(drm_framebuffer_lookup); |
| 482 | |
| 483 | /** |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 484 | * drm_framebuffer_unreference - unref a framebuffer |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 485 | * @fb: framebuffer to unref |
| 486 | * |
| 487 | * This functions decrements the fb's refcount and frees it if it drops to zero. |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 488 | */ |
| 489 | void drm_framebuffer_unreference(struct drm_framebuffer *fb) |
| 490 | { |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 491 | DRM_DEBUG("FB ID: %d\n", fb->base.id); |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 492 | kref_put(&fb->refcount, drm_framebuffer_free); |
| 493 | } |
| 494 | EXPORT_SYMBOL(drm_framebuffer_unreference); |
| 495 | |
| 496 | /** |
| 497 | * drm_framebuffer_reference - incr the fb refcnt |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 498 | * @fb: framebuffer |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 499 | */ |
| 500 | void drm_framebuffer_reference(struct drm_framebuffer *fb) |
| 501 | { |
| 502 | DRM_DEBUG("FB ID: %d\n", fb->base.id); |
| 503 | kref_get(&fb->refcount); |
| 504 | } |
| 505 | EXPORT_SYMBOL(drm_framebuffer_reference); |
| 506 | |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 507 | static void drm_framebuffer_free_bug(struct kref *kref) |
| 508 | { |
| 509 | BUG(); |
| 510 | } |
| 511 | |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 512 | static void __drm_framebuffer_unreference(struct drm_framebuffer *fb) |
| 513 | { |
| 514 | DRM_DEBUG("FB ID: %d\n", fb->base.id); |
| 515 | kref_put(&fb->refcount, drm_framebuffer_free_bug); |
| 516 | } |
| 517 | |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 518 | /* dev->mode_config.fb_lock must be held! */ |
| 519 | static void __drm_framebuffer_unregister(struct drm_device *dev, |
| 520 | struct drm_framebuffer *fb) |
| 521 | { |
| 522 | mutex_lock(&dev->mode_config.idr_mutex); |
| 523 | idr_remove(&dev->mode_config.crtc_idr, fb->base.id); |
| 524 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 525 | |
| 526 | fb->base.id = 0; |
| 527 | |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 528 | __drm_framebuffer_unreference(fb); |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 529 | } |
| 530 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 531 | /** |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 532 | * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr |
| 533 | * @fb: fb to unregister |
| 534 | * |
| 535 | * Drivers need to call this when cleaning up driver-private framebuffers, e.g. |
| 536 | * those used for fbdev. Note that the caller must hold a reference of it's own, |
| 537 | * i.e. the object may not be destroyed through this call (since it'll lead to a |
| 538 | * locking inversion). |
| 539 | */ |
| 540 | void drm_framebuffer_unregister_private(struct drm_framebuffer *fb) |
| 541 | { |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 542 | struct drm_device *dev = fb->dev; |
| 543 | |
| 544 | mutex_lock(&dev->mode_config.fb_lock); |
| 545 | /* Mark fb as reaped and drop idr ref. */ |
| 546 | __drm_framebuffer_unregister(dev, fb); |
| 547 | mutex_unlock(&dev->mode_config.fb_lock); |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 548 | } |
| 549 | EXPORT_SYMBOL(drm_framebuffer_unregister_private); |
| 550 | |
| 551 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 552 | * drm_framebuffer_cleanup - remove a framebuffer object |
| 553 | * @fb: framebuffer to remove |
| 554 | * |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 555 | * Cleanup references to a user-created framebuffer. This function is intended |
| 556 | * to be used from the drivers ->destroy callback. |
| 557 | * |
| 558 | * Note that this function does not remove the fb from active usuage - if it is |
| 559 | * still used anywhere, hilarity can ensue since userspace could call getfb on |
| 560 | * the id and get back -EINVAL. Obviously no concern at driver unload time. |
| 561 | * |
| 562 | * Also, the framebuffer will not be removed from the lookup idr - for |
| 563 | * user-created framebuffers this will happen in in the rmfb ioctl. For |
| 564 | * driver-private objects (e.g. for fbdev) drivers need to explicitly call |
| 565 | * drm_framebuffer_unregister_private. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 566 | */ |
| 567 | void drm_framebuffer_cleanup(struct drm_framebuffer *fb) |
| 568 | { |
| 569 | struct drm_device *dev = fb->dev; |
Daniel Vetter | 8faf6b1 | 2012-12-01 23:43:11 +0100 | [diff] [blame] | 570 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 571 | mutex_lock(&dev->mode_config.fb_lock); |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 572 | list_del(&fb->head); |
| 573 | dev->mode_config.num_fb--; |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 574 | mutex_unlock(&dev->mode_config.fb_lock); |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 575 | } |
| 576 | EXPORT_SYMBOL(drm_framebuffer_cleanup); |
| 577 | |
| 578 | /** |
| 579 | * drm_framebuffer_remove - remove and unreference a framebuffer object |
| 580 | * @fb: framebuffer to remove |
| 581 | * |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 582 | * Scans all the CRTCs and planes in @dev's mode_config. If they're |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 583 | * using @fb, removes it, setting it to NULL. Then drops the reference to the |
Daniel Vetter | b62584e | 2012-12-11 16:51:35 +0100 | [diff] [blame] | 584 | * passed-in framebuffer. Might take the modeset locks. |
| 585 | * |
| 586 | * Note that this function optimizes the cleanup away if the caller holds the |
| 587 | * last reference to the framebuffer. It is also guaranteed to not take the |
| 588 | * modeset locks in this case. |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 589 | */ |
| 590 | void drm_framebuffer_remove(struct drm_framebuffer *fb) |
| 591 | { |
| 592 | struct drm_device *dev = fb->dev; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 593 | struct drm_crtc *crtc; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 594 | struct drm_plane *plane; |
Dave Airlie | 5ef5f72 | 2009-08-17 13:11:23 +1000 | [diff] [blame] | 595 | struct drm_mode_set set; |
| 596 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 597 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 598 | WARN_ON(!list_empty(&fb->filp_head)); |
Daniel Vetter | 8faf6b1 | 2012-12-01 23:43:11 +0100 | [diff] [blame] | 599 | |
Daniel Vetter | b62584e | 2012-12-11 16:51:35 +0100 | [diff] [blame] | 600 | /* |
| 601 | * drm ABI mandates that we remove any deleted framebuffers from active |
| 602 | * useage. But since most sane clients only remove framebuffers they no |
| 603 | * longer need, try to optimize this away. |
| 604 | * |
| 605 | * Since we're holding a reference ourselves, observing a refcount of 1 |
| 606 | * means that we're the last holder and can skip it. Also, the refcount |
| 607 | * can never increase from 1 again, so we don't need any barriers or |
| 608 | * locks. |
| 609 | * |
| 610 | * Note that userspace could try to race with use and instate a new |
| 611 | * usage _after_ we've cleared all current ones. End result will be an |
| 612 | * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot |
| 613 | * in this manner. |
| 614 | */ |
| 615 | if (atomic_read(&fb->refcount.refcount) > 1) { |
| 616 | drm_modeset_lock_all(dev); |
| 617 | /* remove from any CRTC */ |
| 618 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 619 | if (crtc->fb == fb) { |
| 620 | /* should turn off the crtc */ |
| 621 | memset(&set, 0, sizeof(struct drm_mode_set)); |
| 622 | set.crtc = crtc; |
| 623 | set.fb = NULL; |
| 624 | ret = drm_mode_set_config_internal(&set); |
| 625 | if (ret) |
| 626 | DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc); |
| 627 | } |
Dave Airlie | 5ef5f72 | 2009-08-17 13:11:23 +1000 | [diff] [blame] | 628 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 629 | |
Daniel Vetter | b62584e | 2012-12-11 16:51:35 +0100 | [diff] [blame] | 630 | list_for_each_entry(plane, &dev->mode_config.plane_list, head) { |
Ville Syrjälä | 9125e61 | 2013-06-03 16:10:40 +0300 | [diff] [blame] | 631 | if (plane->fb == fb) |
| 632 | drm_plane_force_disable(plane); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 633 | } |
Daniel Vetter | b62584e | 2012-12-11 16:51:35 +0100 | [diff] [blame] | 634 | drm_modeset_unlock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 637 | drm_framebuffer_unreference(fb); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 638 | } |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 639 | EXPORT_SYMBOL(drm_framebuffer_remove); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 640 | |
| 641 | /** |
| 642 | * drm_crtc_init - Initialise a new CRTC object |
| 643 | * @dev: DRM device |
| 644 | * @crtc: CRTC object to init |
| 645 | * @funcs: callbacks for the new CRTC |
| 646 | * |
Ville Syrjälä | ad6f5c3 | 2013-06-05 12:39:55 +0000 | [diff] [blame] | 647 | * Inits a new object created as base part of a driver crtc object. |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 648 | * |
| 649 | * RETURNS: |
| 650 | * Zero on success, error code on failure. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 651 | */ |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 652 | int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 653 | const struct drm_crtc_funcs *funcs) |
| 654 | { |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 655 | int ret; |
| 656 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 657 | crtc->dev = dev; |
| 658 | crtc->funcs = funcs; |
Rob Clark | 7c80e12 | 2012-09-04 16:35:56 +0000 | [diff] [blame] | 659 | crtc->invert_dimensions = false; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 660 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 661 | drm_modeset_lock_all(dev); |
Daniel Vetter | 29494c1 | 2012-12-02 02:18:25 +0100 | [diff] [blame] | 662 | mutex_init(&crtc->mutex); |
| 663 | mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex); |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 664 | |
| 665 | ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC); |
| 666 | if (ret) |
| 667 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 668 | |
Paulo Zanoni | bffd9de0 | 2012-05-15 18:09:05 -0300 | [diff] [blame] | 669 | crtc->base.properties = &crtc->properties; |
| 670 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 671 | list_add_tail(&crtc->head, &dev->mode_config.crtc_list); |
| 672 | dev->mode_config.num_crtc++; |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 673 | |
| 674 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 675 | drm_modeset_unlock_all(dev); |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 676 | |
| 677 | return ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 678 | } |
| 679 | EXPORT_SYMBOL(drm_crtc_init); |
| 680 | |
| 681 | /** |
Ville Syrjälä | ad6f5c3 | 2013-06-05 12:39:55 +0000 | [diff] [blame] | 682 | * drm_crtc_cleanup - Clean up the core crtc usage |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 683 | * @crtc: CRTC to cleanup |
| 684 | * |
Ville Syrjälä | ad6f5c3 | 2013-06-05 12:39:55 +0000 | [diff] [blame] | 685 | * This function cleans up @crtc and removes it from the DRM mode setting |
| 686 | * core. Note that the function does *not* free the crtc structure itself, |
| 687 | * this is the responsibility of the caller. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 688 | */ |
| 689 | void drm_crtc_cleanup(struct drm_crtc *crtc) |
| 690 | { |
| 691 | struct drm_device *dev = crtc->dev; |
| 692 | |
Sachin Kamat | 9e1c156 | 2012-11-19 09:44:56 +0000 | [diff] [blame] | 693 | kfree(crtc->gamma_store); |
| 694 | crtc->gamma_store = NULL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 695 | |
| 696 | drm_mode_object_put(dev, &crtc->base); |
| 697 | list_del(&crtc->head); |
| 698 | dev->mode_config.num_crtc--; |
| 699 | } |
| 700 | EXPORT_SYMBOL(drm_crtc_cleanup); |
| 701 | |
| 702 | /** |
Russell King | db5f7a6 | 2014-01-02 21:27:33 +0000 | [diff] [blame] | 703 | * drm_crtc_index - find the index of a registered CRTC |
| 704 | * @crtc: CRTC to find index for |
| 705 | * |
| 706 | * Given a registered CRTC, return the index of that CRTC within a DRM |
| 707 | * device's list of CRTCs. |
| 708 | */ |
| 709 | unsigned int drm_crtc_index(struct drm_crtc *crtc) |
| 710 | { |
| 711 | unsigned int index = 0; |
| 712 | struct drm_crtc *tmp; |
| 713 | |
| 714 | list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) { |
| 715 | if (tmp == crtc) |
| 716 | return index; |
| 717 | |
| 718 | index++; |
| 719 | } |
| 720 | |
| 721 | BUG(); |
| 722 | } |
| 723 | EXPORT_SYMBOL(drm_crtc_index); |
| 724 | |
Lespiau, Damien | 86f422d | 2013-08-20 00:53:06 +0100 | [diff] [blame] | 725 | /* |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 726 | * drm_mode_remove - remove and free a mode |
| 727 | * @connector: connector list to modify |
| 728 | * @mode: mode to remove |
| 729 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 730 | * Remove @mode from @connector's mode list, then free it. |
| 731 | */ |
Lespiau, Damien | 86f422d | 2013-08-20 00:53:06 +0100 | [diff] [blame] | 732 | static void drm_mode_remove(struct drm_connector *connector, |
| 733 | struct drm_display_mode *mode) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 734 | { |
| 735 | list_del(&mode->head); |
Sascha Hauer | 554f1d7 | 2012-02-01 11:38:19 +0100 | [diff] [blame] | 736 | drm_mode_destroy(connector->dev, mode); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 737 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 738 | |
| 739 | /** |
| 740 | * drm_connector_init - Init a preallocated connector |
| 741 | * @dev: DRM device |
| 742 | * @connector: the connector to init |
| 743 | * @funcs: callbacks for this connector |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 744 | * @connector_type: user visible type of the connector |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 745 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 746 | * Initialises a preallocated connector. Connectors should be |
| 747 | * subclassed as part of driver connector objects. |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 748 | * |
| 749 | * RETURNS: |
| 750 | * Zero on success, error code on failure. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 751 | */ |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 752 | int drm_connector_init(struct drm_device *dev, |
| 753 | struct drm_connector *connector, |
| 754 | const struct drm_connector_funcs *funcs, |
| 755 | int connector_type) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 756 | { |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 757 | int ret; |
Ilia Mirkin | b21e3af | 2013-08-07 22:34:48 -0400 | [diff] [blame] | 758 | struct ida *connector_ida = |
| 759 | &drm_connector_enum_list[connector_type].ida; |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 760 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 761 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 762 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 763 | ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR); |
| 764 | if (ret) |
| 765 | goto out; |
| 766 | |
Paulo Zanoni | 7e3bdf4 | 2012-05-15 18:09:01 -0300 | [diff] [blame] | 767 | connector->base.properties = &connector->properties; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 768 | connector->dev = dev; |
| 769 | connector->funcs = funcs; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 770 | connector->connector_type = connector_type; |
| 771 | connector->connector_type_id = |
Ilia Mirkin | b21e3af | 2013-08-07 22:34:48 -0400 | [diff] [blame] | 772 | ida_simple_get(connector_ida, 1, 0, GFP_KERNEL); |
| 773 | if (connector->connector_type_id < 0) { |
| 774 | ret = connector->connector_type_id; |
| 775 | drm_mode_object_put(dev, &connector->base); |
| 776 | goto out; |
| 777 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 778 | INIT_LIST_HEAD(&connector->probed_modes); |
| 779 | INIT_LIST_HEAD(&connector->modes); |
| 780 | connector->edid_blob_ptr = NULL; |
Daniel Vetter | 5e2cb2f | 2012-10-23 18:23:35 +0000 | [diff] [blame] | 781 | connector->status = connector_status_unknown; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 782 | |
| 783 | list_add_tail(&connector->head, &dev->mode_config.connector_list); |
| 784 | dev->mode_config.num_connector++; |
| 785 | |
Thomas Hellstrom | a7331e5 | 2011-10-22 10:36:19 +0200 | [diff] [blame] | 786 | if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL) |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 787 | drm_object_attach_property(&connector->base, |
Thomas Hellstrom | a7331e5 | 2011-10-22 10:36:19 +0200 | [diff] [blame] | 788 | dev->mode_config.edid_property, |
| 789 | 0); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 790 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 791 | drm_object_attach_property(&connector->base, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 792 | dev->mode_config.dpms_property, 0); |
| 793 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 794 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 795 | drm_modeset_unlock_all(dev); |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 796 | |
| 797 | return ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 798 | } |
| 799 | EXPORT_SYMBOL(drm_connector_init); |
| 800 | |
| 801 | /** |
| 802 | * drm_connector_cleanup - cleans up an initialised connector |
| 803 | * @connector: connector to cleanup |
| 804 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 805 | * Cleans up the connector but doesn't free the object. |
| 806 | */ |
| 807 | void drm_connector_cleanup(struct drm_connector *connector) |
| 808 | { |
| 809 | struct drm_device *dev = connector->dev; |
| 810 | struct drm_display_mode *mode, *t; |
| 811 | |
| 812 | list_for_each_entry_safe(mode, t, &connector->probed_modes, head) |
| 813 | drm_mode_remove(connector, mode); |
| 814 | |
| 815 | list_for_each_entry_safe(mode, t, &connector->modes, head) |
| 816 | drm_mode_remove(connector, mode); |
| 817 | |
Ilia Mirkin | b21e3af | 2013-08-07 22:34:48 -0400 | [diff] [blame] | 818 | ida_remove(&drm_connector_enum_list[connector->connector_type].ida, |
| 819 | connector->connector_type_id); |
| 820 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 821 | drm_mode_object_put(dev, &connector->base); |
| 822 | list_del(&connector->head); |
Joonyoung Shim | 6380c50 | 2011-08-27 02:06:21 +0000 | [diff] [blame] | 823 | dev->mode_config.num_connector--; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 824 | } |
| 825 | EXPORT_SYMBOL(drm_connector_cleanup); |
| 826 | |
Dave Airlie | cbc7e22 | 2012-02-20 14:16:40 +0000 | [diff] [blame] | 827 | void drm_connector_unplug_all(struct drm_device *dev) |
| 828 | { |
| 829 | struct drm_connector *connector; |
| 830 | |
| 831 | /* taking the mode config mutex ends up in a clash with sysfs */ |
| 832 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 833 | drm_sysfs_connector_remove(connector); |
| 834 | |
| 835 | } |
| 836 | EXPORT_SYMBOL(drm_connector_unplug_all); |
| 837 | |
Sean Paul | 3b336ec | 2013-08-14 16:47:37 -0400 | [diff] [blame] | 838 | int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge, |
| 839 | const struct drm_bridge_funcs *funcs) |
| 840 | { |
| 841 | int ret; |
| 842 | |
| 843 | drm_modeset_lock_all(dev); |
| 844 | |
| 845 | ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE); |
| 846 | if (ret) |
| 847 | goto out; |
| 848 | |
| 849 | bridge->dev = dev; |
| 850 | bridge->funcs = funcs; |
| 851 | |
| 852 | list_add_tail(&bridge->head, &dev->mode_config.bridge_list); |
| 853 | dev->mode_config.num_bridge++; |
| 854 | |
| 855 | out: |
| 856 | drm_modeset_unlock_all(dev); |
| 857 | return ret; |
| 858 | } |
| 859 | EXPORT_SYMBOL(drm_bridge_init); |
| 860 | |
| 861 | void drm_bridge_cleanup(struct drm_bridge *bridge) |
| 862 | { |
| 863 | struct drm_device *dev = bridge->dev; |
| 864 | |
| 865 | drm_modeset_lock_all(dev); |
| 866 | drm_mode_object_put(dev, &bridge->base); |
| 867 | list_del(&bridge->head); |
| 868 | dev->mode_config.num_bridge--; |
| 869 | drm_modeset_unlock_all(dev); |
| 870 | } |
| 871 | EXPORT_SYMBOL(drm_bridge_cleanup); |
| 872 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 873 | int drm_encoder_init(struct drm_device *dev, |
Dave Airlie | cbc7e22 | 2012-02-20 14:16:40 +0000 | [diff] [blame] | 874 | struct drm_encoder *encoder, |
| 875 | const struct drm_encoder_funcs *funcs, |
| 876 | int encoder_type) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 877 | { |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 878 | int ret; |
| 879 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 880 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 881 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 882 | ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER); |
| 883 | if (ret) |
| 884 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 885 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 886 | encoder->dev = dev; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 887 | encoder->encoder_type = encoder_type; |
| 888 | encoder->funcs = funcs; |
| 889 | |
| 890 | list_add_tail(&encoder->head, &dev->mode_config.encoder_list); |
| 891 | dev->mode_config.num_encoder++; |
| 892 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 893 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 894 | drm_modeset_unlock_all(dev); |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 895 | |
| 896 | return ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 897 | } |
| 898 | EXPORT_SYMBOL(drm_encoder_init); |
| 899 | |
| 900 | void drm_encoder_cleanup(struct drm_encoder *encoder) |
| 901 | { |
| 902 | struct drm_device *dev = encoder->dev; |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 903 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 904 | drm_mode_object_put(dev, &encoder->base); |
| 905 | list_del(&encoder->head); |
Joonyoung Shim | 6380c50 | 2011-08-27 02:06:21 +0000 | [diff] [blame] | 906 | dev->mode_config.num_encoder--; |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 907 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 908 | } |
| 909 | EXPORT_SYMBOL(drm_encoder_cleanup); |
| 910 | |
Ville Syrjälä | 35f2c3a | 2013-06-05 15:39:56 +0300 | [diff] [blame] | 911 | /** |
| 912 | * drm_plane_init - Initialise a new plane object |
| 913 | * @dev: DRM device |
| 914 | * @plane: plane object to init |
| 915 | * @possible_crtcs: bitmask of possible CRTCs |
| 916 | * @funcs: callbacks for the new plane |
| 917 | * @formats: array of supported formats (%DRM_FORMAT_*) |
| 918 | * @format_count: number of elements in @formats |
| 919 | * @priv: plane is private (hidden from userspace)? |
| 920 | * |
| 921 | * Inits a new object created as base part of a driver plane object. |
| 922 | * |
| 923 | * RETURNS: |
| 924 | * Zero on success, error code on failure. |
| 925 | */ |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 926 | int drm_plane_init(struct drm_device *dev, struct drm_plane *plane, |
| 927 | unsigned long possible_crtcs, |
| 928 | const struct drm_plane_funcs *funcs, |
Rob Clark | 0a7eb24 | 2011-12-13 20:19:36 -0600 | [diff] [blame] | 929 | const uint32_t *formats, uint32_t format_count, |
| 930 | bool priv) |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 931 | { |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 932 | int ret; |
| 933 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 934 | drm_modeset_lock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 935 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 936 | ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE); |
| 937 | if (ret) |
| 938 | goto out; |
| 939 | |
Rob Clark | 4d93914 | 2012-05-17 02:23:27 -0600 | [diff] [blame] | 940 | plane->base.properties = &plane->properties; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 941 | plane->dev = dev; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 942 | plane->funcs = funcs; |
| 943 | plane->format_types = kmalloc(sizeof(uint32_t) * format_count, |
| 944 | GFP_KERNEL); |
| 945 | if (!plane->format_types) { |
| 946 | DRM_DEBUG_KMS("out of memory when allocating plane\n"); |
| 947 | drm_mode_object_put(dev, &plane->base); |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 948 | ret = -ENOMEM; |
| 949 | goto out; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 952 | memcpy(plane->format_types, formats, format_count * sizeof(uint32_t)); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 953 | plane->format_count = format_count; |
| 954 | plane->possible_crtcs = possible_crtcs; |
| 955 | |
Rob Clark | 0a7eb24 | 2011-12-13 20:19:36 -0600 | [diff] [blame] | 956 | /* private planes are not exposed to userspace, but depending on |
| 957 | * display hardware, might be convenient to allow sharing programming |
| 958 | * for the scanout engine with the crtc implementation. |
| 959 | */ |
| 960 | if (!priv) { |
| 961 | list_add_tail(&plane->head, &dev->mode_config.plane_list); |
| 962 | dev->mode_config.num_plane++; |
| 963 | } else { |
| 964 | INIT_LIST_HEAD(&plane->head); |
| 965 | } |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 966 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 967 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 968 | drm_modeset_unlock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 969 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 970 | return ret; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 971 | } |
| 972 | EXPORT_SYMBOL(drm_plane_init); |
| 973 | |
Ville Syrjälä | 35f2c3a | 2013-06-05 15:39:56 +0300 | [diff] [blame] | 974 | /** |
| 975 | * drm_plane_cleanup - Clean up the core plane usage |
| 976 | * @plane: plane to cleanup |
| 977 | * |
| 978 | * This function cleans up @plane and removes it from the DRM mode setting |
| 979 | * core. Note that the function does *not* free the plane structure itself, |
| 980 | * this is the responsibility of the caller. |
| 981 | */ |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 982 | void drm_plane_cleanup(struct drm_plane *plane) |
| 983 | { |
| 984 | struct drm_device *dev = plane->dev; |
| 985 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 986 | drm_modeset_lock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 987 | kfree(plane->format_types); |
| 988 | drm_mode_object_put(dev, &plane->base); |
Rob Clark | 0a7eb24 | 2011-12-13 20:19:36 -0600 | [diff] [blame] | 989 | /* if not added to a list, it must be a private plane */ |
| 990 | if (!list_empty(&plane->head)) { |
| 991 | list_del(&plane->head); |
| 992 | dev->mode_config.num_plane--; |
| 993 | } |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 994 | drm_modeset_unlock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 995 | } |
| 996 | EXPORT_SYMBOL(drm_plane_cleanup); |
| 997 | |
Ville Syrjälä | 35f2c3a | 2013-06-05 15:39:56 +0300 | [diff] [blame] | 998 | /** |
| 999 | * drm_plane_force_disable - Forcibly disable a plane |
| 1000 | * @plane: plane to disable |
| 1001 | * |
| 1002 | * Forces the plane to be disabled. |
| 1003 | * |
| 1004 | * Used when the plane's current framebuffer is destroyed, |
| 1005 | * and when restoring fbdev mode. |
| 1006 | */ |
Ville Syrjälä | 9125e61 | 2013-06-03 16:10:40 +0300 | [diff] [blame] | 1007 | void drm_plane_force_disable(struct drm_plane *plane) |
| 1008 | { |
| 1009 | int ret; |
| 1010 | |
| 1011 | if (!plane->fb) |
| 1012 | return; |
| 1013 | |
| 1014 | ret = plane->funcs->disable_plane(plane); |
| 1015 | if (ret) |
| 1016 | DRM_ERROR("failed to disable plane with busy fb\n"); |
| 1017 | /* disconnect the plane from the fb and crtc: */ |
| 1018 | __drm_framebuffer_unreference(plane->fb); |
| 1019 | plane->fb = NULL; |
| 1020 | plane->crtc = NULL; |
| 1021 | } |
| 1022 | EXPORT_SYMBOL(drm_plane_force_disable); |
| 1023 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1024 | static int drm_mode_create_standard_connector_properties(struct drm_device *dev) |
| 1025 | { |
| 1026 | struct drm_property *edid; |
| 1027 | struct drm_property *dpms; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1028 | |
| 1029 | /* |
| 1030 | * Standard properties (apply to all connectors) |
| 1031 | */ |
| 1032 | edid = drm_property_create(dev, DRM_MODE_PROP_BLOB | |
| 1033 | DRM_MODE_PROP_IMMUTABLE, |
| 1034 | "EDID", 0); |
| 1035 | dev->mode_config.edid_property = edid; |
| 1036 | |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1037 | dpms = drm_property_create_enum(dev, 0, |
| 1038 | "DPMS", drm_dpms_enum_list, |
| 1039 | ARRAY_SIZE(drm_dpms_enum_list)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1040 | dev->mode_config.dpms_property = dpms; |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties |
| 1047 | * @dev: DRM device |
| 1048 | * |
| 1049 | * Called by a driver the first time a DVI-I connector is made. |
| 1050 | */ |
| 1051 | int drm_mode_create_dvi_i_properties(struct drm_device *dev) |
| 1052 | { |
| 1053 | struct drm_property *dvi_i_selector; |
| 1054 | struct drm_property *dvi_i_subconnector; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1055 | |
| 1056 | if (dev->mode_config.dvi_i_select_subconnector_property) |
| 1057 | return 0; |
| 1058 | |
| 1059 | dvi_i_selector = |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1060 | drm_property_create_enum(dev, 0, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1061 | "select subconnector", |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1062 | drm_dvi_i_select_enum_list, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1063 | ARRAY_SIZE(drm_dvi_i_select_enum_list)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1064 | dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector; |
| 1065 | |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1066 | dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1067 | "subconnector", |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1068 | drm_dvi_i_subconnector_enum_list, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1069 | ARRAY_SIZE(drm_dvi_i_subconnector_enum_list)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1070 | dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector; |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | EXPORT_SYMBOL(drm_mode_create_dvi_i_properties); |
| 1075 | |
| 1076 | /** |
| 1077 | * drm_create_tv_properties - create TV specific connector properties |
| 1078 | * @dev: DRM device |
| 1079 | * @num_modes: number of different TV formats (modes) supported |
| 1080 | * @modes: array of pointers to strings containing name of each format |
| 1081 | * |
| 1082 | * Called by a driver's TV initialization routine, this function creates |
| 1083 | * the TV specific connector properties for a given device. Caller is |
| 1084 | * responsible for allocating a list of format names and passing them to |
| 1085 | * this routine. |
| 1086 | */ |
| 1087 | int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes, |
| 1088 | char *modes[]) |
| 1089 | { |
| 1090 | struct drm_property *tv_selector; |
| 1091 | struct drm_property *tv_subconnector; |
| 1092 | int i; |
| 1093 | |
| 1094 | if (dev->mode_config.tv_select_subconnector_property) |
| 1095 | return 0; |
| 1096 | |
| 1097 | /* |
| 1098 | * Basic connector properties |
| 1099 | */ |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1100 | tv_selector = drm_property_create_enum(dev, 0, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1101 | "select subconnector", |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1102 | drm_tv_select_enum_list, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1103 | ARRAY_SIZE(drm_tv_select_enum_list)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1104 | dev->mode_config.tv_select_subconnector_property = tv_selector; |
| 1105 | |
| 1106 | tv_subconnector = |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1107 | drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, |
| 1108 | "subconnector", |
| 1109 | drm_tv_subconnector_enum_list, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1110 | ARRAY_SIZE(drm_tv_subconnector_enum_list)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1111 | dev->mode_config.tv_subconnector_property = tv_subconnector; |
| 1112 | |
| 1113 | /* |
| 1114 | * Other, TV specific properties: margins & TV modes. |
| 1115 | */ |
| 1116 | dev->mode_config.tv_left_margin_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1117 | drm_property_create_range(dev, 0, "left margin", 0, 100); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1118 | |
| 1119 | dev->mode_config.tv_right_margin_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1120 | drm_property_create_range(dev, 0, "right margin", 0, 100); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1121 | |
| 1122 | dev->mode_config.tv_top_margin_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1123 | drm_property_create_range(dev, 0, "top margin", 0, 100); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1124 | |
| 1125 | dev->mode_config.tv_bottom_margin_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1126 | drm_property_create_range(dev, 0, "bottom margin", 0, 100); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1127 | |
| 1128 | dev->mode_config.tv_mode_property = |
| 1129 | drm_property_create(dev, DRM_MODE_PROP_ENUM, |
| 1130 | "mode", num_modes); |
| 1131 | for (i = 0; i < num_modes; i++) |
| 1132 | drm_property_add_enum(dev->mode_config.tv_mode_property, i, |
| 1133 | i, modes[i]); |
| 1134 | |
Francisco Jerez | b6b7902 | 2009-08-02 04:19:20 +0200 | [diff] [blame] | 1135 | dev->mode_config.tv_brightness_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1136 | drm_property_create_range(dev, 0, "brightness", 0, 100); |
Francisco Jerez | b6b7902 | 2009-08-02 04:19:20 +0200 | [diff] [blame] | 1137 | |
| 1138 | dev->mode_config.tv_contrast_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1139 | drm_property_create_range(dev, 0, "contrast", 0, 100); |
Francisco Jerez | b6b7902 | 2009-08-02 04:19:20 +0200 | [diff] [blame] | 1140 | |
| 1141 | dev->mode_config.tv_flicker_reduction_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1142 | drm_property_create_range(dev, 0, "flicker reduction", 0, 100); |
Francisco Jerez | b6b7902 | 2009-08-02 04:19:20 +0200 | [diff] [blame] | 1143 | |
Francisco Jerez | a75f023 | 2009-08-12 02:30:10 +0200 | [diff] [blame] | 1144 | dev->mode_config.tv_overscan_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1145 | drm_property_create_range(dev, 0, "overscan", 0, 100); |
Francisco Jerez | a75f023 | 2009-08-12 02:30:10 +0200 | [diff] [blame] | 1146 | |
| 1147 | dev->mode_config.tv_saturation_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1148 | drm_property_create_range(dev, 0, "saturation", 0, 100); |
Francisco Jerez | a75f023 | 2009-08-12 02:30:10 +0200 | [diff] [blame] | 1149 | |
| 1150 | dev->mode_config.tv_hue_property = |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 1151 | drm_property_create_range(dev, 0, "hue", 0, 100); |
Francisco Jerez | a75f023 | 2009-08-12 02:30:10 +0200 | [diff] [blame] | 1152 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
| 1155 | EXPORT_SYMBOL(drm_mode_create_tv_properties); |
| 1156 | |
| 1157 | /** |
| 1158 | * drm_mode_create_scaling_mode_property - create scaling mode property |
| 1159 | * @dev: DRM device |
| 1160 | * |
| 1161 | * Called by a driver the first time it's needed, must be attached to desired |
| 1162 | * connectors. |
| 1163 | */ |
| 1164 | int drm_mode_create_scaling_mode_property(struct drm_device *dev) |
| 1165 | { |
| 1166 | struct drm_property *scaling_mode; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1167 | |
| 1168 | if (dev->mode_config.scaling_mode_property) |
| 1169 | return 0; |
| 1170 | |
| 1171 | scaling_mode = |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1172 | drm_property_create_enum(dev, 0, "scaling mode", |
| 1173 | drm_scaling_mode_enum_list, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1174 | ARRAY_SIZE(drm_scaling_mode_enum_list)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1175 | |
| 1176 | dev->mode_config.scaling_mode_property = scaling_mode; |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | EXPORT_SYMBOL(drm_mode_create_scaling_mode_property); |
| 1181 | |
| 1182 | /** |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 1183 | * drm_mode_create_dirty_property - create dirty property |
| 1184 | * @dev: DRM device |
| 1185 | * |
| 1186 | * Called by a driver the first time it's needed, must be attached to desired |
| 1187 | * connectors. |
| 1188 | */ |
| 1189 | int drm_mode_create_dirty_info_property(struct drm_device *dev) |
| 1190 | { |
| 1191 | struct drm_property *dirty_info; |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 1192 | |
| 1193 | if (dev->mode_config.dirty_info_property) |
| 1194 | return 0; |
| 1195 | |
| 1196 | dirty_info = |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1197 | drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 1198 | "dirty", |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 1199 | drm_dirty_info_enum_list, |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 1200 | ARRAY_SIZE(drm_dirty_info_enum_list)); |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 1201 | dev->mode_config.dirty_info_property = dirty_info; |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
| 1205 | EXPORT_SYMBOL(drm_mode_create_dirty_info_property); |
| 1206 | |
Ville Syrjälä | ea9cbb0 | 2013-04-25 20:09:20 +0300 | [diff] [blame] | 1207 | static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1208 | { |
| 1209 | uint32_t total_objects = 0; |
| 1210 | |
| 1211 | total_objects += dev->mode_config.num_crtc; |
| 1212 | total_objects += dev->mode_config.num_connector; |
| 1213 | total_objects += dev->mode_config.num_encoder; |
Sean Paul | 3b336ec | 2013-08-14 16:47:37 -0400 | [diff] [blame] | 1214 | total_objects += dev->mode_config.num_bridge; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1215 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1216 | group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL); |
| 1217 | if (!group->id_list) |
| 1218 | return -ENOMEM; |
| 1219 | |
| 1220 | group->num_crtcs = 0; |
| 1221 | group->num_connectors = 0; |
| 1222 | group->num_encoders = 0; |
Sean Paul | 3b336ec | 2013-08-14 16:47:37 -0400 | [diff] [blame] | 1223 | group->num_bridges = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | int drm_mode_group_init_legacy_group(struct drm_device *dev, |
| 1228 | struct drm_mode_group *group) |
| 1229 | { |
| 1230 | struct drm_crtc *crtc; |
| 1231 | struct drm_encoder *encoder; |
| 1232 | struct drm_connector *connector; |
Sean Paul | 3b336ec | 2013-08-14 16:47:37 -0400 | [diff] [blame] | 1233 | struct drm_bridge *bridge; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1234 | int ret; |
| 1235 | |
| 1236 | if ((ret = drm_mode_group_init(dev, group))) |
| 1237 | return ret; |
| 1238 | |
| 1239 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 1240 | group->id_list[group->num_crtcs++] = crtc->base.id; |
| 1241 | |
| 1242 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) |
| 1243 | group->id_list[group->num_crtcs + group->num_encoders++] = |
| 1244 | encoder->base.id; |
| 1245 | |
| 1246 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 1247 | group->id_list[group->num_crtcs + group->num_encoders + |
| 1248 | group->num_connectors++] = connector->base.id; |
| 1249 | |
Sean Paul | 3b336ec | 2013-08-14 16:47:37 -0400 | [diff] [blame] | 1250 | list_for_each_entry(bridge, &dev->mode_config.bridge_list, head) |
| 1251 | group->id_list[group->num_crtcs + group->num_encoders + |
| 1252 | group->num_connectors + group->num_bridges++] = |
| 1253 | bridge->base.id; |
| 1254 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1255 | return 0; |
| 1256 | } |
Dave Airlie | 9c1dfc5 | 2012-03-20 06:59:29 +0000 | [diff] [blame] | 1257 | EXPORT_SYMBOL(drm_mode_group_init_legacy_group); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1258 | |
| 1259 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1260 | * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo |
| 1261 | * @out: drm_mode_modeinfo struct to return to the user |
| 1262 | * @in: drm_display_mode to use |
| 1263 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1264 | * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to |
| 1265 | * the user. |
| 1266 | */ |
Ville Syrjälä | 93bbf6d | 2012-03-13 12:35:47 +0200 | [diff] [blame] | 1267 | static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, |
| 1268 | const struct drm_display_mode *in) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1269 | { |
Ville Syrjälä | e36fae3 | 2012-03-13 12:35:40 +0200 | [diff] [blame] | 1270 | WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX || |
| 1271 | in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX || |
| 1272 | in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX || |
| 1273 | in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX || |
| 1274 | in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX, |
| 1275 | "timing values too large for mode info\n"); |
| 1276 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1277 | out->clock = in->clock; |
| 1278 | out->hdisplay = in->hdisplay; |
| 1279 | out->hsync_start = in->hsync_start; |
| 1280 | out->hsync_end = in->hsync_end; |
| 1281 | out->htotal = in->htotal; |
| 1282 | out->hskew = in->hskew; |
| 1283 | out->vdisplay = in->vdisplay; |
| 1284 | out->vsync_start = in->vsync_start; |
| 1285 | out->vsync_end = in->vsync_end; |
| 1286 | out->vtotal = in->vtotal; |
| 1287 | out->vscan = in->vscan; |
| 1288 | out->vrefresh = in->vrefresh; |
| 1289 | out->flags = in->flags; |
| 1290 | out->type = in->type; |
| 1291 | strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); |
| 1292 | out->name[DRM_DISPLAY_MODE_LEN-1] = 0; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
Marc-André Lureau | 74afee7 | 2013-10-18 16:11:27 +0200 | [diff] [blame] | 1296 | * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1297 | * @out: drm_display_mode to return to the user |
| 1298 | * @in: drm_mode_modeinfo to use |
| 1299 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1300 | * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to |
| 1301 | * the caller. |
Ville Syrjälä | 90367bf | 2012-03-13 12:35:44 +0200 | [diff] [blame] | 1302 | * |
| 1303 | * RETURNS: |
| 1304 | * Zero on success, errno on failure. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1305 | */ |
Ville Syrjälä | 93bbf6d | 2012-03-13 12:35:47 +0200 | [diff] [blame] | 1306 | static int drm_crtc_convert_umode(struct drm_display_mode *out, |
| 1307 | const struct drm_mode_modeinfo *in) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1308 | { |
Ville Syrjälä | 90367bf | 2012-03-13 12:35:44 +0200 | [diff] [blame] | 1309 | if (in->clock > INT_MAX || in->vrefresh > INT_MAX) |
| 1310 | return -ERANGE; |
| 1311 | |
Damien Lespiau | 5848ad4 | 2013-09-27 12:11:50 +0100 | [diff] [blame] | 1312 | if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX) |
| 1313 | return -EINVAL; |
| 1314 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1315 | out->clock = in->clock; |
| 1316 | out->hdisplay = in->hdisplay; |
| 1317 | out->hsync_start = in->hsync_start; |
| 1318 | out->hsync_end = in->hsync_end; |
| 1319 | out->htotal = in->htotal; |
| 1320 | out->hskew = in->hskew; |
| 1321 | out->vdisplay = in->vdisplay; |
| 1322 | out->vsync_start = in->vsync_start; |
| 1323 | out->vsync_end = in->vsync_end; |
| 1324 | out->vtotal = in->vtotal; |
| 1325 | out->vscan = in->vscan; |
| 1326 | out->vrefresh = in->vrefresh; |
| 1327 | out->flags = in->flags; |
| 1328 | out->type = in->type; |
| 1329 | strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); |
| 1330 | out->name[DRM_DISPLAY_MODE_LEN-1] = 0; |
Ville Syrjälä | 90367bf | 2012-03-13 12:35:44 +0200 | [diff] [blame] | 1331 | |
| 1332 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * drm_mode_getresources - get graphics configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 1337 | * @dev: drm device for the ioctl |
| 1338 | * @data: data pointer for the ioctl |
| 1339 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1340 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1341 | * Construct a set of configuration description structures and return |
| 1342 | * them to the user, including CRTC, connector and framebuffer configuration. |
| 1343 | * |
| 1344 | * Called by the user via ioctl. |
| 1345 | * |
| 1346 | * RETURNS: |
| 1347 | * Zero on success, errno on failure. |
| 1348 | */ |
| 1349 | int drm_mode_getresources(struct drm_device *dev, void *data, |
| 1350 | struct drm_file *file_priv) |
| 1351 | { |
| 1352 | struct drm_mode_card_res *card_res = data; |
| 1353 | struct list_head *lh; |
| 1354 | struct drm_framebuffer *fb; |
| 1355 | struct drm_connector *connector; |
| 1356 | struct drm_crtc *crtc; |
| 1357 | struct drm_encoder *encoder; |
| 1358 | int ret = 0; |
| 1359 | int connector_count = 0; |
| 1360 | int crtc_count = 0; |
| 1361 | int fb_count = 0; |
| 1362 | int encoder_count = 0; |
| 1363 | int copied = 0, i; |
| 1364 | uint32_t __user *fb_id; |
| 1365 | uint32_t __user *crtc_id; |
| 1366 | uint32_t __user *connector_id; |
| 1367 | uint32_t __user *encoder_id; |
| 1368 | struct drm_mode_group *mode_group; |
| 1369 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 1370 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1371 | return -EINVAL; |
| 1372 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1373 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 1374 | mutex_lock(&file_priv->fbs_lock); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1375 | /* |
| 1376 | * For the non-control nodes we need to limit the list of resources |
| 1377 | * by IDs in the group list for this node |
| 1378 | */ |
| 1379 | list_for_each(lh, &file_priv->fbs) |
| 1380 | fb_count++; |
| 1381 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 1382 | /* handle this in 4 parts */ |
| 1383 | /* FBs */ |
| 1384 | if (card_res->count_fbs >= fb_count) { |
| 1385 | copied = 0; |
| 1386 | fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr; |
| 1387 | list_for_each_entry(fb, &file_priv->fbs, filp_head) { |
| 1388 | if (put_user(fb->base.id, fb_id + copied)) { |
| 1389 | mutex_unlock(&file_priv->fbs_lock); |
| 1390 | return -EFAULT; |
| 1391 | } |
| 1392 | copied++; |
| 1393 | } |
| 1394 | } |
| 1395 | card_res->count_fbs = fb_count; |
| 1396 | mutex_unlock(&file_priv->fbs_lock); |
| 1397 | |
| 1398 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1399 | mode_group = &file_priv->master->minor->mode_group; |
| 1400 | if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { |
| 1401 | |
| 1402 | list_for_each(lh, &dev->mode_config.crtc_list) |
| 1403 | crtc_count++; |
| 1404 | |
| 1405 | list_for_each(lh, &dev->mode_config.connector_list) |
| 1406 | connector_count++; |
| 1407 | |
| 1408 | list_for_each(lh, &dev->mode_config.encoder_list) |
| 1409 | encoder_count++; |
| 1410 | } else { |
| 1411 | |
| 1412 | crtc_count = mode_group->num_crtcs; |
| 1413 | connector_count = mode_group->num_connectors; |
| 1414 | encoder_count = mode_group->num_encoders; |
| 1415 | } |
| 1416 | |
| 1417 | card_res->max_height = dev->mode_config.max_height; |
| 1418 | card_res->min_height = dev->mode_config.min_height; |
| 1419 | card_res->max_width = dev->mode_config.max_width; |
| 1420 | card_res->min_width = dev->mode_config.min_width; |
| 1421 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1422 | /* CRTCs */ |
| 1423 | if (card_res->count_crtcs >= crtc_count) { |
| 1424 | copied = 0; |
| 1425 | crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr; |
| 1426 | if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { |
| 1427 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, |
| 1428 | head) { |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 1429 | DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1430 | if (put_user(crtc->base.id, crtc_id + copied)) { |
| 1431 | ret = -EFAULT; |
| 1432 | goto out; |
| 1433 | } |
| 1434 | copied++; |
| 1435 | } |
| 1436 | } else { |
| 1437 | for (i = 0; i < mode_group->num_crtcs; i++) { |
| 1438 | if (put_user(mode_group->id_list[i], |
| 1439 | crtc_id + copied)) { |
| 1440 | ret = -EFAULT; |
| 1441 | goto out; |
| 1442 | } |
| 1443 | copied++; |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | card_res->count_crtcs = crtc_count; |
| 1448 | |
| 1449 | /* Encoders */ |
| 1450 | if (card_res->count_encoders >= encoder_count) { |
| 1451 | copied = 0; |
| 1452 | encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr; |
| 1453 | if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { |
| 1454 | list_for_each_entry(encoder, |
| 1455 | &dev->mode_config.encoder_list, |
| 1456 | head) { |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 1457 | DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id, |
| 1458 | drm_get_encoder_name(encoder)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1459 | if (put_user(encoder->base.id, encoder_id + |
| 1460 | copied)) { |
| 1461 | ret = -EFAULT; |
| 1462 | goto out; |
| 1463 | } |
| 1464 | copied++; |
| 1465 | } |
| 1466 | } else { |
| 1467 | for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) { |
| 1468 | if (put_user(mode_group->id_list[i], |
| 1469 | encoder_id + copied)) { |
| 1470 | ret = -EFAULT; |
| 1471 | goto out; |
| 1472 | } |
| 1473 | copied++; |
| 1474 | } |
| 1475 | |
| 1476 | } |
| 1477 | } |
| 1478 | card_res->count_encoders = encoder_count; |
| 1479 | |
| 1480 | /* Connectors */ |
| 1481 | if (card_res->count_connectors >= connector_count) { |
| 1482 | copied = 0; |
| 1483 | connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr; |
| 1484 | if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { |
| 1485 | list_for_each_entry(connector, |
| 1486 | &dev->mode_config.connector_list, |
| 1487 | head) { |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 1488 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", |
| 1489 | connector->base.id, |
| 1490 | drm_get_connector_name(connector)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1491 | if (put_user(connector->base.id, |
| 1492 | connector_id + copied)) { |
| 1493 | ret = -EFAULT; |
| 1494 | goto out; |
| 1495 | } |
| 1496 | copied++; |
| 1497 | } |
| 1498 | } else { |
| 1499 | int start = mode_group->num_crtcs + |
| 1500 | mode_group->num_encoders; |
| 1501 | for (i = start; i < start + mode_group->num_connectors; i++) { |
| 1502 | if (put_user(mode_group->id_list[i], |
| 1503 | connector_id + copied)) { |
| 1504 | ret = -EFAULT; |
| 1505 | goto out; |
| 1506 | } |
| 1507 | copied++; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | card_res->count_connectors = connector_count; |
| 1512 | |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 1513 | DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1514 | card_res->count_connectors, card_res->count_encoders); |
| 1515 | |
| 1516 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1517 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1518 | return ret; |
| 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * drm_mode_getcrtc - get CRTC configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 1523 | * @dev: drm device for the ioctl |
| 1524 | * @data: data pointer for the ioctl |
| 1525 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1526 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1527 | * Construct a CRTC configuration structure to return to the user. |
| 1528 | * |
| 1529 | * Called by the user via ioctl. |
| 1530 | * |
| 1531 | * RETURNS: |
| 1532 | * Zero on success, errno on failure. |
| 1533 | */ |
| 1534 | int drm_mode_getcrtc(struct drm_device *dev, |
| 1535 | void *data, struct drm_file *file_priv) |
| 1536 | { |
| 1537 | struct drm_mode_crtc *crtc_resp = data; |
| 1538 | struct drm_crtc *crtc; |
| 1539 | struct drm_mode_object *obj; |
| 1540 | int ret = 0; |
| 1541 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 1542 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1543 | return -EINVAL; |
| 1544 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1545 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1546 | |
| 1547 | obj = drm_mode_object_find(dev, crtc_resp->crtc_id, |
| 1548 | DRM_MODE_OBJECT_CRTC); |
| 1549 | if (!obj) { |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 1550 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1551 | goto out; |
| 1552 | } |
| 1553 | crtc = obj_to_crtc(obj); |
| 1554 | |
| 1555 | crtc_resp->x = crtc->x; |
| 1556 | crtc_resp->y = crtc->y; |
| 1557 | crtc_resp->gamma_size = crtc->gamma_size; |
| 1558 | if (crtc->fb) |
| 1559 | crtc_resp->fb_id = crtc->fb->base.id; |
| 1560 | else |
| 1561 | crtc_resp->fb_id = 0; |
| 1562 | |
| 1563 | if (crtc->enabled) { |
| 1564 | |
| 1565 | drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode); |
| 1566 | crtc_resp->mode_valid = 1; |
| 1567 | |
| 1568 | } else { |
| 1569 | crtc_resp->mode_valid = 0; |
| 1570 | } |
| 1571 | |
| 1572 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1573 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1574 | return ret; |
| 1575 | } |
| 1576 | |
Damien Lespiau | 61d8e32 | 2013-09-25 16:45:22 +0100 | [diff] [blame] | 1577 | static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode, |
| 1578 | const struct drm_file *file_priv) |
| 1579 | { |
| 1580 | /* |
| 1581 | * If user-space hasn't configured the driver to expose the stereo 3D |
| 1582 | * modes, don't expose them. |
| 1583 | */ |
| 1584 | if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode)) |
| 1585 | return false; |
| 1586 | |
| 1587 | return true; |
| 1588 | } |
| 1589 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1590 | /** |
| 1591 | * drm_mode_getconnector - get connector configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 1592 | * @dev: drm device for the ioctl |
| 1593 | * @data: data pointer for the ioctl |
| 1594 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1595 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1596 | * Construct a connector configuration structure to return to the user. |
| 1597 | * |
| 1598 | * Called by the user via ioctl. |
| 1599 | * |
| 1600 | * RETURNS: |
| 1601 | * Zero on success, errno on failure. |
| 1602 | */ |
| 1603 | int drm_mode_getconnector(struct drm_device *dev, void *data, |
| 1604 | struct drm_file *file_priv) |
| 1605 | { |
| 1606 | struct drm_mode_get_connector *out_resp = data; |
| 1607 | struct drm_mode_object *obj; |
| 1608 | struct drm_connector *connector; |
| 1609 | struct drm_display_mode *mode; |
| 1610 | int mode_count = 0; |
| 1611 | int props_count = 0; |
| 1612 | int encoders_count = 0; |
| 1613 | int ret = 0; |
| 1614 | int copied = 0; |
| 1615 | int i; |
| 1616 | struct drm_mode_modeinfo u_mode; |
| 1617 | struct drm_mode_modeinfo __user *mode_ptr; |
| 1618 | uint32_t __user *prop_ptr; |
| 1619 | uint64_t __user *prop_values; |
| 1620 | uint32_t __user *encoder_ptr; |
| 1621 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 1622 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1623 | return -EINVAL; |
| 1624 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1625 | memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo)); |
| 1626 | |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 1627 | DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1628 | |
Daniel Vetter | 7b24056 | 2012-12-12 00:35:33 +0100 | [diff] [blame] | 1629 | mutex_lock(&dev->mode_config.mutex); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1630 | |
| 1631 | obj = drm_mode_object_find(dev, out_resp->connector_id, |
| 1632 | DRM_MODE_OBJECT_CONNECTOR); |
| 1633 | if (!obj) { |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 1634 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1635 | goto out; |
| 1636 | } |
| 1637 | connector = obj_to_connector(obj); |
| 1638 | |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 1639 | props_count = connector->properties.count; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1640 | |
| 1641 | for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { |
| 1642 | if (connector->encoder_ids[i] != 0) { |
| 1643 | encoders_count++; |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | if (out_resp->count_modes == 0) { |
| 1648 | connector->funcs->fill_modes(connector, |
| 1649 | dev->mode_config.max_width, |
| 1650 | dev->mode_config.max_height); |
| 1651 | } |
| 1652 | |
| 1653 | /* delayed so we get modes regardless of pre-fill_modes state */ |
| 1654 | list_for_each_entry(mode, &connector->modes, head) |
Damien Lespiau | 61d8e32 | 2013-09-25 16:45:22 +0100 | [diff] [blame] | 1655 | if (drm_mode_expose_to_userspace(mode, file_priv)) |
| 1656 | mode_count++; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1657 | |
| 1658 | out_resp->connector_id = connector->base.id; |
| 1659 | out_resp->connector_type = connector->connector_type; |
| 1660 | out_resp->connector_type_id = connector->connector_type_id; |
| 1661 | out_resp->mm_width = connector->display_info.width_mm; |
| 1662 | out_resp->mm_height = connector->display_info.height_mm; |
| 1663 | out_resp->subpixel = connector->display_info.subpixel_order; |
| 1664 | out_resp->connection = connector->status; |
| 1665 | if (connector->encoder) |
| 1666 | out_resp->encoder_id = connector->encoder->base.id; |
| 1667 | else |
| 1668 | out_resp->encoder_id = 0; |
| 1669 | |
| 1670 | /* |
| 1671 | * This ioctl is called twice, once to determine how much space is |
| 1672 | * needed, and the 2nd time to fill it. |
| 1673 | */ |
| 1674 | if ((out_resp->count_modes >= mode_count) && mode_count) { |
| 1675 | copied = 0; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 1676 | mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1677 | list_for_each_entry(mode, &connector->modes, head) { |
Damien Lespiau | 61d8e32 | 2013-09-25 16:45:22 +0100 | [diff] [blame] | 1678 | if (!drm_mode_expose_to_userspace(mode, file_priv)) |
| 1679 | continue; |
| 1680 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1681 | drm_crtc_convert_to_umode(&u_mode, mode); |
| 1682 | if (copy_to_user(mode_ptr + copied, |
| 1683 | &u_mode, sizeof(u_mode))) { |
| 1684 | ret = -EFAULT; |
| 1685 | goto out; |
| 1686 | } |
| 1687 | copied++; |
| 1688 | } |
| 1689 | } |
| 1690 | out_resp->count_modes = mode_count; |
| 1691 | |
| 1692 | if ((out_resp->count_props >= props_count) && props_count) { |
| 1693 | copied = 0; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 1694 | prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr); |
| 1695 | prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr); |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 1696 | for (i = 0; i < connector->properties.count; i++) { |
| 1697 | if (put_user(connector->properties.ids[i], |
| 1698 | prop_ptr + copied)) { |
| 1699 | ret = -EFAULT; |
| 1700 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1701 | } |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 1702 | |
| 1703 | if (put_user(connector->properties.values[i], |
| 1704 | prop_values + copied)) { |
| 1705 | ret = -EFAULT; |
| 1706 | goto out; |
| 1707 | } |
| 1708 | copied++; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | out_resp->count_props = props_count; |
| 1712 | |
| 1713 | if ((out_resp->count_encoders >= encoders_count) && encoders_count) { |
| 1714 | copied = 0; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 1715 | encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1716 | for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { |
| 1717 | if (connector->encoder_ids[i] != 0) { |
| 1718 | if (put_user(connector->encoder_ids[i], |
| 1719 | encoder_ptr + copied)) { |
| 1720 | ret = -EFAULT; |
| 1721 | goto out; |
| 1722 | } |
| 1723 | copied++; |
| 1724 | } |
| 1725 | } |
| 1726 | } |
| 1727 | out_resp->count_encoders = encoders_count; |
| 1728 | |
| 1729 | out: |
Daniel Vetter | 7b24056 | 2012-12-12 00:35:33 +0100 | [diff] [blame] | 1730 | mutex_unlock(&dev->mode_config.mutex); |
| 1731 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1732 | return ret; |
| 1733 | } |
| 1734 | |
| 1735 | int drm_mode_getencoder(struct drm_device *dev, void *data, |
| 1736 | struct drm_file *file_priv) |
| 1737 | { |
| 1738 | struct drm_mode_get_encoder *enc_resp = data; |
| 1739 | struct drm_mode_object *obj; |
| 1740 | struct drm_encoder *encoder; |
| 1741 | int ret = 0; |
| 1742 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 1743 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1744 | return -EINVAL; |
| 1745 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1746 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1747 | obj = drm_mode_object_find(dev, enc_resp->encoder_id, |
| 1748 | DRM_MODE_OBJECT_ENCODER); |
| 1749 | if (!obj) { |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 1750 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1751 | goto out; |
| 1752 | } |
| 1753 | encoder = obj_to_encoder(obj); |
| 1754 | |
| 1755 | if (encoder->crtc) |
| 1756 | enc_resp->crtc_id = encoder->crtc->base.id; |
| 1757 | else |
| 1758 | enc_resp->crtc_id = 0; |
| 1759 | enc_resp->encoder_type = encoder->encoder_type; |
| 1760 | enc_resp->encoder_id = encoder->base.id; |
| 1761 | enc_resp->possible_crtcs = encoder->possible_crtcs; |
| 1762 | enc_resp->possible_clones = encoder->possible_clones; |
| 1763 | |
| 1764 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1765 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1766 | return ret; |
| 1767 | } |
| 1768 | |
| 1769 | /** |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1770 | * drm_mode_getplane_res - get plane info |
| 1771 | * @dev: DRM device |
| 1772 | * @data: ioctl data |
| 1773 | * @file_priv: DRM file info |
| 1774 | * |
| 1775 | * Return an plane count and set of IDs. |
| 1776 | */ |
| 1777 | int drm_mode_getplane_res(struct drm_device *dev, void *data, |
| 1778 | struct drm_file *file_priv) |
| 1779 | { |
| 1780 | struct drm_mode_get_plane_res *plane_resp = data; |
| 1781 | struct drm_mode_config *config; |
| 1782 | struct drm_plane *plane; |
| 1783 | uint32_t __user *plane_ptr; |
| 1784 | int copied = 0, ret = 0; |
| 1785 | |
| 1786 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1787 | return -EINVAL; |
| 1788 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1789 | drm_modeset_lock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1790 | config = &dev->mode_config; |
| 1791 | |
| 1792 | /* |
| 1793 | * This ioctl is called twice, once to determine how much space is |
| 1794 | * needed, and the 2nd time to fill it. |
| 1795 | */ |
| 1796 | if (config->num_plane && |
| 1797 | (plane_resp->count_planes >= config->num_plane)) { |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 1798 | plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1799 | |
| 1800 | list_for_each_entry(plane, &config->plane_list, head) { |
| 1801 | if (put_user(plane->base.id, plane_ptr + copied)) { |
| 1802 | ret = -EFAULT; |
| 1803 | goto out; |
| 1804 | } |
| 1805 | copied++; |
| 1806 | } |
| 1807 | } |
| 1808 | plane_resp->count_planes = config->num_plane; |
| 1809 | |
| 1810 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1811 | drm_modeset_unlock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1812 | return ret; |
| 1813 | } |
| 1814 | |
| 1815 | /** |
| 1816 | * drm_mode_getplane - get plane info |
| 1817 | * @dev: DRM device |
| 1818 | * @data: ioctl data |
| 1819 | * @file_priv: DRM file info |
| 1820 | * |
| 1821 | * Return plane info, including formats supported, gamma size, any |
| 1822 | * current fb, etc. |
| 1823 | */ |
| 1824 | int drm_mode_getplane(struct drm_device *dev, void *data, |
| 1825 | struct drm_file *file_priv) |
| 1826 | { |
| 1827 | struct drm_mode_get_plane *plane_resp = data; |
| 1828 | struct drm_mode_object *obj; |
| 1829 | struct drm_plane *plane; |
| 1830 | uint32_t __user *format_ptr; |
| 1831 | int ret = 0; |
| 1832 | |
| 1833 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1834 | return -EINVAL; |
| 1835 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1836 | drm_modeset_lock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1837 | obj = drm_mode_object_find(dev, plane_resp->plane_id, |
| 1838 | DRM_MODE_OBJECT_PLANE); |
| 1839 | if (!obj) { |
| 1840 | ret = -ENOENT; |
| 1841 | goto out; |
| 1842 | } |
| 1843 | plane = obj_to_plane(obj); |
| 1844 | |
| 1845 | if (plane->crtc) |
| 1846 | plane_resp->crtc_id = plane->crtc->base.id; |
| 1847 | else |
| 1848 | plane_resp->crtc_id = 0; |
| 1849 | |
| 1850 | if (plane->fb) |
| 1851 | plane_resp->fb_id = plane->fb->base.id; |
| 1852 | else |
| 1853 | plane_resp->fb_id = 0; |
| 1854 | |
| 1855 | plane_resp->plane_id = plane->base.id; |
| 1856 | plane_resp->possible_crtcs = plane->possible_crtcs; |
Ville Syrjälä | 778ad90 | 2013-06-03 16:11:42 +0300 | [diff] [blame] | 1857 | plane_resp->gamma_size = 0; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1858 | |
| 1859 | /* |
| 1860 | * This ioctl is called twice, once to determine how much space is |
| 1861 | * needed, and the 2nd time to fill it. |
| 1862 | */ |
| 1863 | if (plane->format_count && |
| 1864 | (plane_resp->count_format_types >= plane->format_count)) { |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 1865 | format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1866 | if (copy_to_user(format_ptr, |
| 1867 | plane->format_types, |
| 1868 | sizeof(uint32_t) * plane->format_count)) { |
| 1869 | ret = -EFAULT; |
| 1870 | goto out; |
| 1871 | } |
| 1872 | } |
| 1873 | plane_resp->count_format_types = plane->format_count; |
| 1874 | |
| 1875 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 1876 | drm_modeset_unlock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1877 | return ret; |
| 1878 | } |
| 1879 | |
| 1880 | /** |
| 1881 | * drm_mode_setplane - set up or tear down an plane |
| 1882 | * @dev: DRM device |
| 1883 | * @data: ioctl data* |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 1884 | * @file_priv: DRM file info |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1885 | * |
| 1886 | * Set plane info, including placement, fb, scaling, and other factors. |
| 1887 | * Or pass a NULL fb to disable. |
| 1888 | */ |
| 1889 | int drm_mode_setplane(struct drm_device *dev, void *data, |
| 1890 | struct drm_file *file_priv) |
| 1891 | { |
| 1892 | struct drm_mode_set_plane *plane_req = data; |
| 1893 | struct drm_mode_object *obj; |
| 1894 | struct drm_plane *plane; |
| 1895 | struct drm_crtc *crtc; |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 1896 | struct drm_framebuffer *fb = NULL, *old_fb = NULL; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1897 | int ret = 0; |
Ville Syrjälä | 42ef878 | 2011-12-20 00:06:44 +0200 | [diff] [blame] | 1898 | unsigned int fb_width, fb_height; |
Ville Syrjälä | 62443be | 2011-12-20 00:06:47 +0200 | [diff] [blame] | 1899 | int i; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1900 | |
| 1901 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1902 | return -EINVAL; |
| 1903 | |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1904 | /* |
| 1905 | * First, find the plane, crtc, and fb objects. If not available, |
| 1906 | * we don't bother to call the driver. |
| 1907 | */ |
| 1908 | obj = drm_mode_object_find(dev, plane_req->plane_id, |
| 1909 | DRM_MODE_OBJECT_PLANE); |
| 1910 | if (!obj) { |
| 1911 | DRM_DEBUG_KMS("Unknown plane ID %d\n", |
| 1912 | plane_req->plane_id); |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 1913 | return -ENOENT; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1914 | } |
| 1915 | plane = obj_to_plane(obj); |
| 1916 | |
| 1917 | /* No fb means shut it down */ |
| 1918 | if (!plane_req->fb_id) { |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 1919 | drm_modeset_lock_all(dev); |
| 1920 | old_fb = plane->fb; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1921 | plane->funcs->disable_plane(plane); |
Ville Syrjälä | e5e3b44 | 2011-12-20 00:06:43 +0200 | [diff] [blame] | 1922 | plane->crtc = NULL; |
| 1923 | plane->fb = NULL; |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 1924 | drm_modeset_unlock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1925 | goto out; |
| 1926 | } |
| 1927 | |
| 1928 | obj = drm_mode_object_find(dev, plane_req->crtc_id, |
| 1929 | DRM_MODE_OBJECT_CRTC); |
| 1930 | if (!obj) { |
| 1931 | DRM_DEBUG_KMS("Unknown crtc ID %d\n", |
| 1932 | plane_req->crtc_id); |
| 1933 | ret = -ENOENT; |
| 1934 | goto out; |
| 1935 | } |
| 1936 | crtc = obj_to_crtc(obj); |
| 1937 | |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 1938 | fb = drm_framebuffer_lookup(dev, plane_req->fb_id); |
| 1939 | if (!fb) { |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1940 | DRM_DEBUG_KMS("Unknown framebuffer ID %d\n", |
| 1941 | plane_req->fb_id); |
| 1942 | ret = -ENOENT; |
| 1943 | goto out; |
| 1944 | } |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1945 | |
Ville Syrjälä | 62443be | 2011-12-20 00:06:47 +0200 | [diff] [blame] | 1946 | /* Check whether this plane supports the fb pixel format. */ |
| 1947 | for (i = 0; i < plane->format_count; i++) |
| 1948 | if (fb->pixel_format == plane->format_types[i]) |
| 1949 | break; |
| 1950 | if (i == plane->format_count) { |
Ville Syrjälä | 6ba6d03 | 2013-06-10 11:15:10 +0300 | [diff] [blame] | 1951 | DRM_DEBUG_KMS("Invalid pixel format %s\n", |
| 1952 | drm_get_format_name(fb->pixel_format)); |
Ville Syrjälä | 62443be | 2011-12-20 00:06:47 +0200 | [diff] [blame] | 1953 | ret = -EINVAL; |
| 1954 | goto out; |
| 1955 | } |
| 1956 | |
Ville Syrjälä | 42ef878 | 2011-12-20 00:06:44 +0200 | [diff] [blame] | 1957 | fb_width = fb->width << 16; |
| 1958 | fb_height = fb->height << 16; |
| 1959 | |
| 1960 | /* Make sure source coordinates are inside the fb. */ |
| 1961 | if (plane_req->src_w > fb_width || |
| 1962 | plane_req->src_x > fb_width - plane_req->src_w || |
| 1963 | plane_req->src_h > fb_height || |
| 1964 | plane_req->src_y > fb_height - plane_req->src_h) { |
| 1965 | DRM_DEBUG_KMS("Invalid source coordinates " |
| 1966 | "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", |
| 1967 | plane_req->src_w >> 16, |
| 1968 | ((plane_req->src_w & 0xffff) * 15625) >> 10, |
| 1969 | plane_req->src_h >> 16, |
| 1970 | ((plane_req->src_h & 0xffff) * 15625) >> 10, |
| 1971 | plane_req->src_x >> 16, |
| 1972 | ((plane_req->src_x & 0xffff) * 15625) >> 10, |
| 1973 | plane_req->src_y >> 16, |
| 1974 | ((plane_req->src_y & 0xffff) * 15625) >> 10); |
| 1975 | ret = -ENOSPC; |
| 1976 | goto out; |
| 1977 | } |
| 1978 | |
Ville Syrjälä | 687a040 | 2011-12-20 00:06:45 +0200 | [diff] [blame] | 1979 | /* Give drivers some help against integer overflows */ |
| 1980 | if (plane_req->crtc_w > INT_MAX || |
| 1981 | plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w || |
| 1982 | plane_req->crtc_h > INT_MAX || |
| 1983 | plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) { |
| 1984 | DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n", |
| 1985 | plane_req->crtc_w, plane_req->crtc_h, |
| 1986 | plane_req->crtc_x, plane_req->crtc_y); |
| 1987 | ret = -ERANGE; |
| 1988 | goto out; |
| 1989 | } |
| 1990 | |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 1991 | drm_modeset_lock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1992 | ret = plane->funcs->update_plane(plane, crtc, fb, |
| 1993 | plane_req->crtc_x, plane_req->crtc_y, |
| 1994 | plane_req->crtc_w, plane_req->crtc_h, |
| 1995 | plane_req->src_x, plane_req->src_y, |
| 1996 | plane_req->src_w, plane_req->src_h); |
| 1997 | if (!ret) { |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 1998 | old_fb = plane->fb; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 1999 | plane->crtc = crtc; |
| 2000 | plane->fb = fb; |
Daniel Vetter | 35f8bad | 2013-02-15 21:21:37 +0100 | [diff] [blame] | 2001 | fb = NULL; |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 2002 | } |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 2003 | drm_modeset_unlock_all(dev); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 2004 | |
| 2005 | out: |
Daniel Vetter | 6c2a753 | 2012-12-11 00:59:24 +0100 | [diff] [blame] | 2006 | if (fb) |
| 2007 | drm_framebuffer_unreference(fb); |
| 2008 | if (old_fb) |
| 2009 | drm_framebuffer_unreference(old_fb); |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 2010 | |
| 2011 | return ret; |
| 2012 | } |
| 2013 | |
| 2014 | /** |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 2015 | * drm_mode_set_config_internal - helper to call ->set_config |
| 2016 | * @set: modeset config to set |
| 2017 | * |
| 2018 | * This is a little helper to wrap internal calls to the ->set_config driver |
| 2019 | * interface. The only thing it adds is correct refcounting dance. |
| 2020 | */ |
| 2021 | int drm_mode_set_config_internal(struct drm_mode_set *set) |
| 2022 | { |
| 2023 | struct drm_crtc *crtc = set->crtc; |
Daniel Vetter | 5cef29a | 2013-06-15 00:13:16 +0200 | [diff] [blame] | 2024 | struct drm_framebuffer *fb; |
| 2025 | struct drm_crtc *tmp; |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 2026 | int ret; |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 2027 | |
Daniel Vetter | 5cef29a | 2013-06-15 00:13:16 +0200 | [diff] [blame] | 2028 | /* |
| 2029 | * NOTE: ->set_config can also disable other crtcs (if we steal all |
| 2030 | * connectors from it), hence we need to refcount the fbs across all |
| 2031 | * crtcs. Atomic modeset will have saner semantics ... |
| 2032 | */ |
| 2033 | list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) |
| 2034 | tmp->old_fb = tmp->fb; |
| 2035 | |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 2036 | fb = set->fb; |
| 2037 | |
| 2038 | ret = crtc->funcs->set_config(set); |
| 2039 | if (ret == 0) { |
Daniel Vetter | cc85e12 | 2013-06-15 00:13:15 +0200 | [diff] [blame] | 2040 | /* crtc->fb must be updated by ->set_config, enforces this. */ |
| 2041 | WARN_ON(fb != crtc->fb); |
Daniel Vetter | 5cef29a | 2013-06-15 00:13:16 +0200 | [diff] [blame] | 2042 | } |
Daniel Vetter | cc85e12 | 2013-06-15 00:13:15 +0200 | [diff] [blame] | 2043 | |
Daniel Vetter | 5cef29a | 2013-06-15 00:13:16 +0200 | [diff] [blame] | 2044 | list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) { |
| 2045 | if (tmp->fb) |
| 2046 | drm_framebuffer_reference(tmp->fb); |
| 2047 | if (tmp->old_fb) |
| 2048 | drm_framebuffer_unreference(tmp->old_fb); |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | return ret; |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 2052 | } |
| 2053 | EXPORT_SYMBOL(drm_mode_set_config_internal); |
| 2054 | |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 2055 | /* |
| 2056 | * Checks that the framebuffer is big enough for the CRTC viewport |
| 2057 | * (x, y, hdisplay, vdisplay) |
| 2058 | */ |
| 2059 | static int drm_crtc_check_viewport(const struct drm_crtc *crtc, |
| 2060 | int x, int y, |
| 2061 | const struct drm_display_mode *mode, |
| 2062 | const struct drm_framebuffer *fb) |
| 2063 | |
| 2064 | { |
| 2065 | int hdisplay, vdisplay; |
| 2066 | |
| 2067 | hdisplay = mode->hdisplay; |
| 2068 | vdisplay = mode->vdisplay; |
| 2069 | |
Damien Lespiau | a0c1bbb | 2013-09-25 16:45:31 +0100 | [diff] [blame] | 2070 | if (drm_mode_is_stereo(mode)) { |
| 2071 | struct drm_display_mode adjusted = *mode; |
| 2072 | |
| 2073 | drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE); |
| 2074 | hdisplay = adjusted.crtc_hdisplay; |
| 2075 | vdisplay = adjusted.crtc_vdisplay; |
| 2076 | } |
| 2077 | |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 2078 | if (crtc->invert_dimensions) |
| 2079 | swap(hdisplay, vdisplay); |
| 2080 | |
| 2081 | if (hdisplay > fb->width || |
| 2082 | vdisplay > fb->height || |
| 2083 | x > fb->width - hdisplay || |
| 2084 | y > fb->height - vdisplay) { |
| 2085 | DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n", |
| 2086 | fb->width, fb->height, hdisplay, vdisplay, x, y, |
| 2087 | crtc->invert_dimensions ? " (inverted)" : ""); |
| 2088 | return -ENOSPC; |
| 2089 | } |
| 2090 | |
| 2091 | return 0; |
| 2092 | } |
| 2093 | |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 2094 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2095 | * drm_mode_setcrtc - set CRTC configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 2096 | * @dev: drm device for the ioctl |
| 2097 | * @data: data pointer for the ioctl |
| 2098 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2099 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2100 | * Build a new CRTC configuration based on user request. |
| 2101 | * |
| 2102 | * Called by the user via ioctl. |
| 2103 | * |
| 2104 | * RETURNS: |
| 2105 | * Zero on success, errno on failure. |
| 2106 | */ |
| 2107 | int drm_mode_setcrtc(struct drm_device *dev, void *data, |
| 2108 | struct drm_file *file_priv) |
| 2109 | { |
| 2110 | struct drm_mode_config *config = &dev->mode_config; |
| 2111 | struct drm_mode_crtc *crtc_req = data; |
| 2112 | struct drm_mode_object *obj; |
Ville Syrjälä | 6653cc8 | 2012-03-13 12:35:38 +0200 | [diff] [blame] | 2113 | struct drm_crtc *crtc; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2114 | struct drm_connector **connector_set = NULL, *connector; |
| 2115 | struct drm_framebuffer *fb = NULL; |
| 2116 | struct drm_display_mode *mode = NULL; |
| 2117 | struct drm_mode_set set; |
| 2118 | uint32_t __user *set_connectors_ptr; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 2119 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2120 | int i; |
| 2121 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 2122 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 2123 | return -EINVAL; |
| 2124 | |
Ville Syrjälä | 1d97e91 | 2012-03-13 12:35:41 +0200 | [diff] [blame] | 2125 | /* For some reason crtc x/y offsets are signed internally. */ |
| 2126 | if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX) |
| 2127 | return -ERANGE; |
| 2128 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 2129 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2130 | obj = drm_mode_object_find(dev, crtc_req->crtc_id, |
| 2131 | DRM_MODE_OBJECT_CRTC); |
| 2132 | if (!obj) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 2133 | 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] | 2134 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2135 | goto out; |
| 2136 | } |
| 2137 | crtc = obj_to_crtc(obj); |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 2138 | DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2139 | |
| 2140 | if (crtc_req->mode_valid) { |
| 2141 | /* If we have a mode we need a framebuffer. */ |
| 2142 | /* If we pass -1, set the mode with the currently bound fb */ |
| 2143 | if (crtc_req->fb_id == -1) { |
Ville Syrjälä | 6653cc8 | 2012-03-13 12:35:38 +0200 | [diff] [blame] | 2144 | if (!crtc->fb) { |
| 2145 | DRM_DEBUG_KMS("CRTC doesn't have current FB\n"); |
| 2146 | ret = -EINVAL; |
| 2147 | goto out; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2148 | } |
Ville Syrjälä | 6653cc8 | 2012-03-13 12:35:38 +0200 | [diff] [blame] | 2149 | fb = crtc->fb; |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 2150 | /* Make refcounting symmetric with the lookup path. */ |
| 2151 | drm_framebuffer_reference(fb); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2152 | } else { |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 2153 | fb = drm_framebuffer_lookup(dev, crtc_req->fb_id); |
| 2154 | if (!fb) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 2155 | DRM_DEBUG_KMS("Unknown FB ID%d\n", |
| 2156 | crtc_req->fb_id); |
Ville Syrjälä | 37c4e70 | 2013-10-17 13:35:01 +0300 | [diff] [blame] | 2157 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2158 | goto out; |
| 2159 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | mode = drm_mode_create(dev); |
Ville Syrjälä | ee34ab5 | 2012-03-13 12:35:43 +0200 | [diff] [blame] | 2163 | if (!mode) { |
| 2164 | ret = -ENOMEM; |
| 2165 | goto out; |
| 2166 | } |
| 2167 | |
Ville Syrjälä | 90367bf | 2012-03-13 12:35:44 +0200 | [diff] [blame] | 2168 | ret = drm_crtc_convert_umode(mode, &crtc_req->mode); |
| 2169 | if (ret) { |
| 2170 | DRM_DEBUG_KMS("Invalid mode\n"); |
| 2171 | goto out; |
| 2172 | } |
| 2173 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2174 | drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); |
Ville Syrjälä | 5f61bb4 | 2012-03-13 12:35:45 +0200 | [diff] [blame] | 2175 | |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 2176 | ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y, |
| 2177 | mode, fb); |
| 2178 | if (ret) |
Ville Syrjälä | 5f61bb4 | 2012-03-13 12:35:45 +0200 | [diff] [blame] | 2179 | goto out; |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 2180 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | if (crtc_req->count_connectors == 0 && mode) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 2184 | DRM_DEBUG_KMS("Count connectors is 0 but mode set\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2185 | ret = -EINVAL; |
| 2186 | goto out; |
| 2187 | } |
| 2188 | |
Jakob Bornecrantz | 7781de7 | 2009-08-03 13:43:58 +0100 | [diff] [blame] | 2189 | if (crtc_req->count_connectors > 0 && (!mode || !fb)) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 2190 | 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] | 2191 | crtc_req->count_connectors); |
| 2192 | ret = -EINVAL; |
| 2193 | goto out; |
| 2194 | } |
| 2195 | |
| 2196 | if (crtc_req->count_connectors > 0) { |
| 2197 | u32 out_id; |
| 2198 | |
| 2199 | /* Avoid unbounded kernel memory allocation */ |
| 2200 | if (crtc_req->count_connectors > config->num_connector) { |
| 2201 | ret = -EINVAL; |
| 2202 | goto out; |
| 2203 | } |
| 2204 | |
| 2205 | connector_set = kmalloc(crtc_req->count_connectors * |
| 2206 | sizeof(struct drm_connector *), |
| 2207 | GFP_KERNEL); |
| 2208 | if (!connector_set) { |
| 2209 | ret = -ENOMEM; |
| 2210 | goto out; |
| 2211 | } |
| 2212 | |
| 2213 | for (i = 0; i < crtc_req->count_connectors; i++) { |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 2214 | 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] | 2215 | if (get_user(out_id, &set_connectors_ptr[i])) { |
| 2216 | ret = -EFAULT; |
| 2217 | goto out; |
| 2218 | } |
| 2219 | |
| 2220 | obj = drm_mode_object_find(dev, out_id, |
| 2221 | DRM_MODE_OBJECT_CONNECTOR); |
| 2222 | if (!obj) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 2223 | DRM_DEBUG_KMS("Connector id %d unknown\n", |
| 2224 | out_id); |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 2225 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2226 | goto out; |
| 2227 | } |
| 2228 | connector = obj_to_connector(obj); |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 2229 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", |
| 2230 | connector->base.id, |
| 2231 | drm_get_connector_name(connector)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2232 | |
| 2233 | connector_set[i] = connector; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | set.crtc = crtc; |
| 2238 | set.x = crtc_req->x; |
| 2239 | set.y = crtc_req->y; |
| 2240 | set.mode = mode; |
| 2241 | set.connectors = connector_set; |
| 2242 | set.num_connectors = crtc_req->count_connectors; |
Dave Airlie | 5ef5f72 | 2009-08-17 13:11:23 +1000 | [diff] [blame] | 2243 | set.fb = fb; |
Daniel Vetter | 2d13b67 | 2012-12-11 13:47:23 +0100 | [diff] [blame] | 2244 | ret = drm_mode_set_config_internal(&set); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2245 | |
| 2246 | out: |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 2247 | if (fb) |
| 2248 | drm_framebuffer_unreference(fb); |
| 2249 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2250 | kfree(connector_set); |
Ville Syrjälä | ee34ab5 | 2012-03-13 12:35:43 +0200 | [diff] [blame] | 2251 | drm_mode_destroy(dev, mode); |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 2252 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2253 | return ret; |
| 2254 | } |
| 2255 | |
Dave Airlie | 4c813d4 | 2013-06-20 11:48:52 +1000 | [diff] [blame] | 2256 | static int drm_mode_cursor_common(struct drm_device *dev, |
| 2257 | struct drm_mode_cursor2 *req, |
| 2258 | struct drm_file *file_priv) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2259 | { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2260 | struct drm_mode_object *obj; |
| 2261 | struct drm_crtc *crtc; |
| 2262 | int ret = 0; |
| 2263 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 2264 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 2265 | return -EINVAL; |
| 2266 | |
Jakob Bornecrantz | 7c4eaca | 2012-08-16 08:29:03 +0000 | [diff] [blame] | 2267 | if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags)) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2268 | return -EINVAL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2269 | |
Jakob Bornecrantz | e0c8463 | 2008-12-19 14:50:50 +1000 | [diff] [blame] | 2270 | obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2271 | if (!obj) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 2272 | DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id); |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 2273 | return -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2274 | } |
| 2275 | crtc = obj_to_crtc(obj); |
| 2276 | |
Daniel Vetter | dac3566 | 2012-12-02 15:24:10 +0100 | [diff] [blame] | 2277 | mutex_lock(&crtc->mutex); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2278 | if (req->flags & DRM_MODE_CURSOR_BO) { |
Dave Airlie | 4c813d4 | 2013-06-20 11:48:52 +1000 | [diff] [blame] | 2279 | if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2280 | ret = -ENXIO; |
| 2281 | goto out; |
| 2282 | } |
| 2283 | /* Turns off the cursor if handle is 0 */ |
Dave Airlie | 4c813d4 | 2013-06-20 11:48:52 +1000 | [diff] [blame] | 2284 | if (crtc->funcs->cursor_set2) |
| 2285 | ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle, |
| 2286 | req->width, req->height, req->hot_x, req->hot_y); |
| 2287 | else |
| 2288 | ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle, |
| 2289 | req->width, req->height); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | if (req->flags & DRM_MODE_CURSOR_MOVE) { |
| 2293 | if (crtc->funcs->cursor_move) { |
| 2294 | ret = crtc->funcs->cursor_move(crtc, req->x, req->y); |
| 2295 | } else { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2296 | ret = -EFAULT; |
| 2297 | goto out; |
| 2298 | } |
| 2299 | } |
| 2300 | out: |
Daniel Vetter | dac3566 | 2012-12-02 15:24:10 +0100 | [diff] [blame] | 2301 | mutex_unlock(&crtc->mutex); |
| 2302 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2303 | return ret; |
Dave Airlie | 4c813d4 | 2013-06-20 11:48:52 +1000 | [diff] [blame] | 2304 | |
| 2305 | } |
| 2306 | int drm_mode_cursor_ioctl(struct drm_device *dev, |
| 2307 | void *data, struct drm_file *file_priv) |
| 2308 | { |
| 2309 | struct drm_mode_cursor *req = data; |
| 2310 | struct drm_mode_cursor2 new_req; |
| 2311 | |
| 2312 | memcpy(&new_req, req, sizeof(struct drm_mode_cursor)); |
| 2313 | new_req.hot_x = new_req.hot_y = 0; |
| 2314 | |
| 2315 | return drm_mode_cursor_common(dev, &new_req, file_priv); |
| 2316 | } |
| 2317 | |
| 2318 | int drm_mode_cursor2_ioctl(struct drm_device *dev, |
| 2319 | void *data, struct drm_file *file_priv) |
| 2320 | { |
| 2321 | struct drm_mode_cursor2 *req = data; |
| 2322 | return drm_mode_cursor_common(dev, req, file_priv); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2323 | } |
| 2324 | |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2325 | /* Original addfb only supported RGB formats, so figure out which one */ |
| 2326 | uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth) |
| 2327 | { |
| 2328 | uint32_t fmt; |
| 2329 | |
| 2330 | switch (bpp) { |
| 2331 | case 8: |
Ville Syrjälä | d84f031 | 2013-01-31 19:43:38 +0200 | [diff] [blame] | 2332 | fmt = DRM_FORMAT_C8; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2333 | break; |
| 2334 | case 16: |
| 2335 | if (depth == 15) |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 2336 | fmt = DRM_FORMAT_XRGB1555; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2337 | else |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 2338 | fmt = DRM_FORMAT_RGB565; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2339 | break; |
| 2340 | case 24: |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 2341 | fmt = DRM_FORMAT_RGB888; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2342 | break; |
| 2343 | case 32: |
| 2344 | if (depth == 24) |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 2345 | fmt = DRM_FORMAT_XRGB8888; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2346 | else if (depth == 30) |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 2347 | fmt = DRM_FORMAT_XRGB2101010; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2348 | else |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 2349 | fmt = DRM_FORMAT_ARGB8888; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2350 | break; |
| 2351 | default: |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 2352 | DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n"); |
| 2353 | fmt = DRM_FORMAT_XRGB8888; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2354 | break; |
| 2355 | } |
| 2356 | |
| 2357 | return fmt; |
| 2358 | } |
| 2359 | EXPORT_SYMBOL(drm_mode_legacy_fb_format); |
| 2360 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2361 | /** |
| 2362 | * drm_mode_addfb - add an FB to the graphics configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 2363 | * @dev: drm device for the ioctl |
| 2364 | * @data: data pointer for the ioctl |
| 2365 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2366 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2367 | * Add a new FB to the specified CRTC, given a user request. |
| 2368 | * |
| 2369 | * Called by the user via ioctl. |
| 2370 | * |
| 2371 | * RETURNS: |
| 2372 | * Zero on success, errno on failure. |
| 2373 | */ |
| 2374 | int drm_mode_addfb(struct drm_device *dev, |
| 2375 | void *data, struct drm_file *file_priv) |
| 2376 | { |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2377 | struct drm_mode_fb_cmd *or = data; |
| 2378 | struct drm_mode_fb_cmd2 r = {}; |
| 2379 | struct drm_mode_config *config = &dev->mode_config; |
| 2380 | struct drm_framebuffer *fb; |
| 2381 | int ret = 0; |
| 2382 | |
| 2383 | /* Use new struct with format internally */ |
| 2384 | r.fb_id = or->fb_id; |
| 2385 | r.width = or->width; |
| 2386 | r.height = or->height; |
| 2387 | r.pitches[0] = or->pitch; |
| 2388 | r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth); |
| 2389 | r.handles[0] = or->handle; |
| 2390 | |
| 2391 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 2392 | return -EINVAL; |
| 2393 | |
Jesse Barnes | acb4b99 | 2011-11-07 12:03:23 -0800 | [diff] [blame] | 2394 | if ((config->min_width > r.width) || (r.width > config->max_width)) |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2395 | return -EINVAL; |
Jesse Barnes | acb4b99 | 2011-11-07 12:03:23 -0800 | [diff] [blame] | 2396 | |
| 2397 | if ((config->min_height > r.height) || (r.height > config->max_height)) |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2398 | return -EINVAL; |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2399 | |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2400 | fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r); |
| 2401 | if (IS_ERR(fb)) { |
Dave Airlie | 1aa1b11 | 2012-05-01 17:38:35 +0100 | [diff] [blame] | 2402 | DRM_DEBUG_KMS("could not create framebuffer\n"); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2403 | return PTR_ERR(fb); |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2404 | } |
| 2405 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2406 | mutex_lock(&file_priv->fbs_lock); |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2407 | or->fb_id = fb->base.id; |
| 2408 | list_add(&fb->filp_head, &file_priv->fbs); |
| 2409 | DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2410 | mutex_unlock(&file_priv->fbs_lock); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2411 | |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2412 | return ret; |
| 2413 | } |
| 2414 | |
Ville Syrjälä | cff91b6 | 2012-05-24 20:54:00 +0300 | [diff] [blame] | 2415 | static int format_check(const struct drm_mode_fb_cmd2 *r) |
Ville Syrjälä | 935b597 | 2011-12-20 00:06:48 +0200 | [diff] [blame] | 2416 | { |
| 2417 | uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN; |
| 2418 | |
| 2419 | switch (format) { |
| 2420 | case DRM_FORMAT_C8: |
| 2421 | case DRM_FORMAT_RGB332: |
| 2422 | case DRM_FORMAT_BGR233: |
| 2423 | case DRM_FORMAT_XRGB4444: |
| 2424 | case DRM_FORMAT_XBGR4444: |
| 2425 | case DRM_FORMAT_RGBX4444: |
| 2426 | case DRM_FORMAT_BGRX4444: |
| 2427 | case DRM_FORMAT_ARGB4444: |
| 2428 | case DRM_FORMAT_ABGR4444: |
| 2429 | case DRM_FORMAT_RGBA4444: |
| 2430 | case DRM_FORMAT_BGRA4444: |
| 2431 | case DRM_FORMAT_XRGB1555: |
| 2432 | case DRM_FORMAT_XBGR1555: |
| 2433 | case DRM_FORMAT_RGBX5551: |
| 2434 | case DRM_FORMAT_BGRX5551: |
| 2435 | case DRM_FORMAT_ARGB1555: |
| 2436 | case DRM_FORMAT_ABGR1555: |
| 2437 | case DRM_FORMAT_RGBA5551: |
| 2438 | case DRM_FORMAT_BGRA5551: |
| 2439 | case DRM_FORMAT_RGB565: |
| 2440 | case DRM_FORMAT_BGR565: |
| 2441 | case DRM_FORMAT_RGB888: |
| 2442 | case DRM_FORMAT_BGR888: |
| 2443 | case DRM_FORMAT_XRGB8888: |
| 2444 | case DRM_FORMAT_XBGR8888: |
| 2445 | case DRM_FORMAT_RGBX8888: |
| 2446 | case DRM_FORMAT_BGRX8888: |
| 2447 | case DRM_FORMAT_ARGB8888: |
| 2448 | case DRM_FORMAT_ABGR8888: |
| 2449 | case DRM_FORMAT_RGBA8888: |
| 2450 | case DRM_FORMAT_BGRA8888: |
| 2451 | case DRM_FORMAT_XRGB2101010: |
| 2452 | case DRM_FORMAT_XBGR2101010: |
| 2453 | case DRM_FORMAT_RGBX1010102: |
| 2454 | case DRM_FORMAT_BGRX1010102: |
| 2455 | case DRM_FORMAT_ARGB2101010: |
| 2456 | case DRM_FORMAT_ABGR2101010: |
| 2457 | case DRM_FORMAT_RGBA1010102: |
| 2458 | case DRM_FORMAT_BGRA1010102: |
| 2459 | case DRM_FORMAT_YUYV: |
| 2460 | case DRM_FORMAT_YVYU: |
| 2461 | case DRM_FORMAT_UYVY: |
| 2462 | case DRM_FORMAT_VYUY: |
| 2463 | case DRM_FORMAT_AYUV: |
| 2464 | case DRM_FORMAT_NV12: |
| 2465 | case DRM_FORMAT_NV21: |
| 2466 | case DRM_FORMAT_NV16: |
| 2467 | case DRM_FORMAT_NV61: |
Laurent Pinchart | ba623f6 | 2012-05-18 23:47:40 +0200 | [diff] [blame] | 2468 | case DRM_FORMAT_NV24: |
| 2469 | case DRM_FORMAT_NV42: |
Ville Syrjälä | 935b597 | 2011-12-20 00:06:48 +0200 | [diff] [blame] | 2470 | case DRM_FORMAT_YUV410: |
| 2471 | case DRM_FORMAT_YVU410: |
| 2472 | case DRM_FORMAT_YUV411: |
| 2473 | case DRM_FORMAT_YVU411: |
| 2474 | case DRM_FORMAT_YUV420: |
| 2475 | case DRM_FORMAT_YVU420: |
| 2476 | case DRM_FORMAT_YUV422: |
| 2477 | case DRM_FORMAT_YVU422: |
| 2478 | case DRM_FORMAT_YUV444: |
| 2479 | case DRM_FORMAT_YVU444: |
| 2480 | return 0; |
| 2481 | default: |
Ville Syrjälä | 23c453a | 2013-10-15 21:06:51 +0300 | [diff] [blame] | 2482 | DRM_DEBUG_KMS("invalid pixel format %s\n", |
| 2483 | drm_get_format_name(r->pixel_format)); |
Ville Syrjälä | 935b597 | 2011-12-20 00:06:48 +0200 | [diff] [blame] | 2484 | return -EINVAL; |
| 2485 | } |
| 2486 | } |
| 2487 | |
Ville Syrjälä | cff91b6 | 2012-05-24 20:54:00 +0300 | [diff] [blame] | 2488 | static int framebuffer_check(const struct drm_mode_fb_cmd2 *r) |
Ville Syrjälä | d1b45d5 | 2012-04-05 21:35:18 +0300 | [diff] [blame] | 2489 | { |
| 2490 | int ret, hsub, vsub, num_planes, i; |
| 2491 | |
| 2492 | ret = format_check(r); |
| 2493 | if (ret) { |
Ville Syrjälä | 6ba6d03 | 2013-06-10 11:15:10 +0300 | [diff] [blame] | 2494 | DRM_DEBUG_KMS("bad framebuffer format %s\n", |
| 2495 | drm_get_format_name(r->pixel_format)); |
Ville Syrjälä | d1b45d5 | 2012-04-05 21:35:18 +0300 | [diff] [blame] | 2496 | return ret; |
| 2497 | } |
| 2498 | |
| 2499 | hsub = drm_format_horz_chroma_subsampling(r->pixel_format); |
| 2500 | vsub = drm_format_vert_chroma_subsampling(r->pixel_format); |
| 2501 | num_planes = drm_format_num_planes(r->pixel_format); |
| 2502 | |
| 2503 | if (r->width == 0 || r->width % hsub) { |
Dave Airlie | 1aa1b11 | 2012-05-01 17:38:35 +0100 | [diff] [blame] | 2504 | DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height); |
Ville Syrjälä | d1b45d5 | 2012-04-05 21:35:18 +0300 | [diff] [blame] | 2505 | return -EINVAL; |
| 2506 | } |
| 2507 | |
| 2508 | if (r->height == 0 || r->height % vsub) { |
Dave Airlie | 1aa1b11 | 2012-05-01 17:38:35 +0100 | [diff] [blame] | 2509 | DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height); |
Ville Syrjälä | d1b45d5 | 2012-04-05 21:35:18 +0300 | [diff] [blame] | 2510 | return -EINVAL; |
| 2511 | } |
| 2512 | |
| 2513 | for (i = 0; i < num_planes; i++) { |
| 2514 | unsigned int width = r->width / (i != 0 ? hsub : 1); |
Ville Syrjälä | b180b5d | 2012-10-25 18:05:04 +0000 | [diff] [blame] | 2515 | unsigned int height = r->height / (i != 0 ? vsub : 1); |
| 2516 | unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i); |
Ville Syrjälä | d1b45d5 | 2012-04-05 21:35:18 +0300 | [diff] [blame] | 2517 | |
| 2518 | if (!r->handles[i]) { |
Dave Airlie | 1aa1b11 | 2012-05-01 17:38:35 +0100 | [diff] [blame] | 2519 | DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i); |
Ville Syrjälä | d1b45d5 | 2012-04-05 21:35:18 +0300 | [diff] [blame] | 2520 | return -EINVAL; |
| 2521 | } |
| 2522 | |
Ville Syrjälä | b180b5d | 2012-10-25 18:05:04 +0000 | [diff] [blame] | 2523 | if ((uint64_t) width * cpp > UINT_MAX) |
| 2524 | return -ERANGE; |
| 2525 | |
| 2526 | if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX) |
| 2527 | return -ERANGE; |
| 2528 | |
| 2529 | if (r->pitches[i] < width * cpp) { |
Dave Airlie | 1aa1b11 | 2012-05-01 17:38:35 +0100 | [diff] [blame] | 2530 | DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i); |
Ville Syrjälä | d1b45d5 | 2012-04-05 21:35:18 +0300 | [diff] [blame] | 2531 | return -EINVAL; |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2538 | /** |
| 2539 | * drm_mode_addfb2 - add an FB to the graphics configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 2540 | * @dev: drm device for the ioctl |
| 2541 | * @data: data pointer for the ioctl |
| 2542 | * @file_priv: drm file for the ioctl call |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2543 | * |
Jesse Barnes | 308e5bc | 2011-11-14 14:51:28 -0800 | [diff] [blame] | 2544 | * Add a new FB to the specified CRTC, given a user request with format. |
| 2545 | * |
| 2546 | * Called by the user via ioctl. |
| 2547 | * |
| 2548 | * RETURNS: |
| 2549 | * Zero on success, errno on failure. |
| 2550 | */ |
| 2551 | int drm_mode_addfb2(struct drm_device *dev, |
| 2552 | void *data, struct drm_file *file_priv) |
| 2553 | { |
| 2554 | struct drm_mode_fb_cmd2 *r = data; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2555 | struct drm_mode_config *config = &dev->mode_config; |
| 2556 | struct drm_framebuffer *fb; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 2557 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2558 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 2559 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 2560 | return -EINVAL; |
| 2561 | |
Ville Syrjälä | e3cc352 | 2012-11-08 09:09:42 +0000 | [diff] [blame] | 2562 | if (r->flags & ~DRM_MODE_FB_INTERLACED) { |
| 2563 | DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags); |
| 2564 | return -EINVAL; |
| 2565 | } |
| 2566 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2567 | if ((config->min_width > r->width) || (r->width > config->max_width)) { |
Dave Airlie | 1aa1b11 | 2012-05-01 17:38:35 +0100 | [diff] [blame] | 2568 | DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n", |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 2569 | r->width, config->min_width, config->max_width); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2570 | return -EINVAL; |
| 2571 | } |
| 2572 | if ((config->min_height > r->height) || (r->height > config->max_height)) { |
Dave Airlie | 1aa1b11 | 2012-05-01 17:38:35 +0100 | [diff] [blame] | 2573 | DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n", |
Jesse Barnes | 8cf5c91 | 2011-11-14 14:51:27 -0800 | [diff] [blame] | 2574 | r->height, config->min_height, config->max_height); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2575 | return -EINVAL; |
| 2576 | } |
| 2577 | |
Ville Syrjälä | d1b45d5 | 2012-04-05 21:35:18 +0300 | [diff] [blame] | 2578 | ret = framebuffer_check(r); |
| 2579 | if (ret) |
Ville Syrjälä | 935b597 | 2011-12-20 00:06:48 +0200 | [diff] [blame] | 2580 | return ret; |
Ville Syrjälä | 935b597 | 2011-12-20 00:06:48 +0200 | [diff] [blame] | 2581 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2582 | fb = dev->mode_config.funcs->fb_create(dev, file_priv, r); |
Chris Wilson | cce13ff | 2010-08-08 13:36:38 +0100 | [diff] [blame] | 2583 | if (IS_ERR(fb)) { |
Dave Airlie | 1aa1b11 | 2012-05-01 17:38:35 +0100 | [diff] [blame] | 2584 | DRM_DEBUG_KMS("could not create framebuffer\n"); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2585 | return PTR_ERR(fb); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2586 | } |
| 2587 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2588 | mutex_lock(&file_priv->fbs_lock); |
Jakob Bornecrantz | e0c8463 | 2008-12-19 14:50:50 +1000 | [diff] [blame] | 2589 | r->fb_id = fb->base.id; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2590 | list_add(&fb->filp_head, &file_priv->fbs); |
Jerome Glisse | 9440106 | 2010-07-15 15:43:25 -0400 | [diff] [blame] | 2591 | DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2592 | mutex_unlock(&file_priv->fbs_lock); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2593 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2594 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2595 | return ret; |
| 2596 | } |
| 2597 | |
| 2598 | /** |
| 2599 | * drm_mode_rmfb - remove an FB from the configuration |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 2600 | * @dev: drm device for the ioctl |
| 2601 | * @data: data pointer for the ioctl |
| 2602 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2603 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2604 | * Remove the FB specified by the user. |
| 2605 | * |
| 2606 | * Called by the user via ioctl. |
| 2607 | * |
| 2608 | * RETURNS: |
| 2609 | * Zero on success, errno on failure. |
| 2610 | */ |
| 2611 | int drm_mode_rmfb(struct drm_device *dev, |
| 2612 | void *data, struct drm_file *file_priv) |
| 2613 | { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2614 | struct drm_framebuffer *fb = NULL; |
| 2615 | struct drm_framebuffer *fbl = NULL; |
| 2616 | uint32_t *id = data; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2617 | int found = 0; |
| 2618 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 2619 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 2620 | return -EINVAL; |
| 2621 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2622 | mutex_lock(&file_priv->fbs_lock); |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 2623 | mutex_lock(&dev->mode_config.fb_lock); |
| 2624 | fb = __drm_framebuffer_lookup(dev, *id); |
| 2625 | if (!fb) |
| 2626 | goto fail_lookup; |
| 2627 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2628 | list_for_each_entry(fbl, &file_priv->fbs, filp_head) |
| 2629 | if (fb == fbl) |
| 2630 | found = 1; |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 2631 | if (!found) |
| 2632 | goto fail_lookup; |
| 2633 | |
| 2634 | /* Mark fb as reaped, we still have a ref from fpriv->fbs. */ |
| 2635 | __drm_framebuffer_unregister(dev, fb); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2636 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2637 | list_del_init(&fb->filp_head); |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 2638 | mutex_unlock(&dev->mode_config.fb_lock); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2639 | mutex_unlock(&file_priv->fbs_lock); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2640 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2641 | drm_framebuffer_remove(fb); |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2642 | |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 2643 | return 0; |
| 2644 | |
| 2645 | fail_lookup: |
| 2646 | mutex_unlock(&dev->mode_config.fb_lock); |
| 2647 | mutex_unlock(&file_priv->fbs_lock); |
| 2648 | |
Ville Syrjälä | 37c4e70 | 2013-10-17 13:35:01 +0300 | [diff] [blame] | 2649 | return -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | /** |
| 2653 | * drm_mode_getfb - get FB info |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 2654 | * @dev: drm device for the ioctl |
| 2655 | * @data: data pointer for the ioctl |
| 2656 | * @file_priv: drm file for the ioctl call |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2657 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2658 | * Lookup the FB given its ID and return info about it. |
| 2659 | * |
| 2660 | * Called by the user via ioctl. |
| 2661 | * |
| 2662 | * RETURNS: |
| 2663 | * Zero on success, errno on failure. |
| 2664 | */ |
| 2665 | int drm_mode_getfb(struct drm_device *dev, |
| 2666 | void *data, struct drm_file *file_priv) |
| 2667 | { |
| 2668 | struct drm_mode_fb_cmd *r = data; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2669 | struct drm_framebuffer *fb; |
Daniel Vetter | 58c0dca | 2012-12-13 23:06:08 +0100 | [diff] [blame] | 2670 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2671 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 2672 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 2673 | return -EINVAL; |
| 2674 | |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 2675 | fb = drm_framebuffer_lookup(dev, r->fb_id); |
Daniel Vetter | 58c0dca | 2012-12-13 23:06:08 +0100 | [diff] [blame] | 2676 | if (!fb) |
Ville Syrjälä | 37c4e70 | 2013-10-17 13:35:01 +0300 | [diff] [blame] | 2677 | return -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2678 | |
| 2679 | r->height = fb->height; |
| 2680 | r->width = fb->width; |
| 2681 | r->depth = fb->depth; |
| 2682 | r->bpp = fb->bits_per_pixel; |
Ville Syrjälä | 01f2c77 | 2011-12-20 00:06:49 +0200 | [diff] [blame] | 2683 | r->pitch = fb->pitches[0]; |
David Herrmann | 101b96f | 2013-08-26 15:16:49 +0200 | [diff] [blame] | 2684 | if (fb->funcs->create_handle) { |
| 2685 | if (file_priv->is_master || capable(CAP_SYS_ADMIN)) { |
| 2686 | ret = fb->funcs->create_handle(fb, file_priv, |
| 2687 | &r->handle); |
| 2688 | } else { |
| 2689 | /* GET_FB() is an unprivileged ioctl so we must not |
| 2690 | * return a buffer-handle to non-master processes! For |
| 2691 | * backwards-compatibility reasons, we cannot make |
| 2692 | * GET_FB() privileged, so just return an invalid handle |
| 2693 | * for non-masters. */ |
| 2694 | r->handle = 0; |
| 2695 | ret = 0; |
| 2696 | } |
| 2697 | } else { |
Daniel Vetter | af26ef3 | 2012-12-13 23:07:50 +0100 | [diff] [blame] | 2698 | ret = -ENODEV; |
David Herrmann | 101b96f | 2013-08-26 15:16:49 +0200 | [diff] [blame] | 2699 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2700 | |
Daniel Vetter | 58c0dca | 2012-12-13 23:06:08 +0100 | [diff] [blame] | 2701 | drm_framebuffer_unreference(fb); |
| 2702 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2703 | return ret; |
| 2704 | } |
| 2705 | |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2706 | int drm_mode_dirtyfb_ioctl(struct drm_device *dev, |
| 2707 | void *data, struct drm_file *file_priv) |
| 2708 | { |
| 2709 | struct drm_clip_rect __user *clips_ptr; |
| 2710 | struct drm_clip_rect *clips = NULL; |
| 2711 | struct drm_mode_fb_dirty_cmd *r = data; |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2712 | struct drm_framebuffer *fb; |
| 2713 | unsigned flags; |
| 2714 | int num_clips; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 2715 | int ret; |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2716 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 2717 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 2718 | return -EINVAL; |
| 2719 | |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 2720 | fb = drm_framebuffer_lookup(dev, r->fb_id); |
Daniel Vetter | 4ccf097 | 2012-12-11 00:38:18 +0100 | [diff] [blame] | 2721 | if (!fb) |
Ville Syrjälä | 37c4e70 | 2013-10-17 13:35:01 +0300 | [diff] [blame] | 2722 | return -ENOENT; |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2723 | |
| 2724 | num_clips = r->num_clips; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 2725 | clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr; |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2726 | |
| 2727 | if (!num_clips != !clips_ptr) { |
| 2728 | ret = -EINVAL; |
| 2729 | goto out_err1; |
| 2730 | } |
| 2731 | |
| 2732 | flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags; |
| 2733 | |
| 2734 | /* If userspace annotates copy, clips must come in pairs */ |
| 2735 | if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) { |
| 2736 | ret = -EINVAL; |
| 2737 | goto out_err1; |
| 2738 | } |
| 2739 | |
| 2740 | if (num_clips && clips_ptr) { |
Xi Wang | a5cd335 | 2011-11-23 01:12:01 -0500 | [diff] [blame] | 2741 | if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) { |
| 2742 | ret = -EINVAL; |
| 2743 | goto out_err1; |
| 2744 | } |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2745 | clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL); |
| 2746 | if (!clips) { |
| 2747 | ret = -ENOMEM; |
| 2748 | goto out_err1; |
| 2749 | } |
| 2750 | |
| 2751 | ret = copy_from_user(clips, clips_ptr, |
| 2752 | num_clips * sizeof(*clips)); |
Dan Carpenter | e902a35 | 2010-06-04 12:23:21 +0200 | [diff] [blame] | 2753 | if (ret) { |
| 2754 | ret = -EFAULT; |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2755 | goto out_err2; |
Dan Carpenter | e902a35 | 2010-06-04 12:23:21 +0200 | [diff] [blame] | 2756 | } |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | if (fb->funcs->dirty) { |
Thomas Hellstrom | 02b0016 | 2010-10-05 12:43:02 +0200 | [diff] [blame] | 2760 | ret = fb->funcs->dirty(fb, file_priv, flags, r->color, |
| 2761 | clips, num_clips); |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2762 | } else { |
| 2763 | ret = -ENOSYS; |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | out_err2: |
| 2767 | kfree(clips); |
| 2768 | out_err1: |
Daniel Vetter | 4ccf097 | 2012-12-11 00:38:18 +0100 | [diff] [blame] | 2769 | drm_framebuffer_unreference(fb); |
| 2770 | |
Jakob Bornecrantz | 884840a | 2009-12-03 23:25:47 +0000 | [diff] [blame] | 2771 | return ret; |
| 2772 | } |
| 2773 | |
| 2774 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2775 | /** |
| 2776 | * drm_fb_release - remove and free the FBs on this file |
Daniel Vetter | 065a50ed | 2012-12-02 00:09:18 +0100 | [diff] [blame] | 2777 | * @priv: drm file for the ioctl |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2778 | * |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2779 | * Destroy all the FBs associated with @filp. |
| 2780 | * |
| 2781 | * Called by the user via ioctl. |
| 2782 | * |
| 2783 | * RETURNS: |
| 2784 | * Zero on success, errno on failure. |
| 2785 | */ |
Kristian Høgsberg | ea39f83 | 2009-02-12 14:37:56 -0500 | [diff] [blame] | 2786 | void drm_fb_release(struct drm_file *priv) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2787 | { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2788 | struct drm_device *dev = priv->minor->dev; |
| 2789 | struct drm_framebuffer *fb, *tfb; |
| 2790 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2791 | mutex_lock(&priv->fbs_lock); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2792 | list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 2793 | |
| 2794 | mutex_lock(&dev->mode_config.fb_lock); |
| 2795 | /* Mark fb as reaped, we still have a ref from fpriv->fbs. */ |
| 2796 | __drm_framebuffer_unregister(dev, fb); |
| 2797 | mutex_unlock(&dev->mode_config.fb_lock); |
| 2798 | |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2799 | list_del_init(&fb->filp_head); |
Daniel Vetter | 2b677e8 | 2012-12-10 21:16:05 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* This will also drop the fpriv->fbs reference. */ |
Rob Clark | f7eff60 | 2012-09-05 21:48:38 +0000 | [diff] [blame] | 2802 | drm_framebuffer_remove(fb); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2803 | } |
Daniel Vetter | 4b096ac | 2012-12-10 21:19:18 +0100 | [diff] [blame] | 2804 | mutex_unlock(&priv->fbs_lock); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2805 | } |
| 2806 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2807 | struct drm_property *drm_property_create(struct drm_device *dev, int flags, |
| 2808 | const char *name, int num_values) |
| 2809 | { |
| 2810 | struct drm_property *property = NULL; |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 2811 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2812 | |
| 2813 | property = kzalloc(sizeof(struct drm_property), GFP_KERNEL); |
| 2814 | if (!property) |
| 2815 | return NULL; |
| 2816 | |
| 2817 | if (num_values) { |
| 2818 | property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL); |
| 2819 | if (!property->values) |
| 2820 | goto fail; |
| 2821 | } |
| 2822 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 2823 | ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY); |
| 2824 | if (ret) |
| 2825 | goto fail; |
| 2826 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2827 | property->flags = flags; |
| 2828 | property->num_values = num_values; |
| 2829 | INIT_LIST_HEAD(&property->enum_blob_list); |
| 2830 | |
Vinson Lee | 471dd2e | 2011-11-10 11:55:40 -0800 | [diff] [blame] | 2831 | if (name) { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2832 | strncpy(property->name, name, DRM_PROP_NAME_LEN); |
Vinson Lee | 471dd2e | 2011-11-10 11:55:40 -0800 | [diff] [blame] | 2833 | property->name[DRM_PROP_NAME_LEN-1] = '\0'; |
| 2834 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2835 | |
| 2836 | list_add_tail(&property->head, &dev->mode_config.property_list); |
| 2837 | return property; |
| 2838 | fail: |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 2839 | kfree(property->values); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2840 | kfree(property); |
| 2841 | return NULL; |
| 2842 | } |
| 2843 | EXPORT_SYMBOL(drm_property_create); |
| 2844 | |
Sascha Hauer | 4a67d39 | 2012-02-06 10:58:17 +0100 | [diff] [blame] | 2845 | struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags, |
| 2846 | const char *name, |
| 2847 | const struct drm_prop_enum_list *props, |
| 2848 | int num_values) |
| 2849 | { |
| 2850 | struct drm_property *property; |
| 2851 | int i, ret; |
| 2852 | |
| 2853 | flags |= DRM_MODE_PROP_ENUM; |
| 2854 | |
| 2855 | property = drm_property_create(dev, flags, name, num_values); |
| 2856 | if (!property) |
| 2857 | return NULL; |
| 2858 | |
| 2859 | for (i = 0; i < num_values; i++) { |
| 2860 | ret = drm_property_add_enum(property, i, |
| 2861 | props[i].type, |
| 2862 | props[i].name); |
| 2863 | if (ret) { |
| 2864 | drm_property_destroy(dev, property); |
| 2865 | return NULL; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | return property; |
| 2870 | } |
| 2871 | EXPORT_SYMBOL(drm_property_create_enum); |
| 2872 | |
Rob Clark | 49e2754 | 2012-05-17 02:23:26 -0600 | [diff] [blame] | 2873 | struct drm_property *drm_property_create_bitmask(struct drm_device *dev, |
| 2874 | int flags, const char *name, |
| 2875 | const struct drm_prop_enum_list *props, |
| 2876 | int num_values) |
| 2877 | { |
| 2878 | struct drm_property *property; |
| 2879 | int i, ret; |
| 2880 | |
| 2881 | flags |= DRM_MODE_PROP_BITMASK; |
| 2882 | |
| 2883 | property = drm_property_create(dev, flags, name, num_values); |
| 2884 | if (!property) |
| 2885 | return NULL; |
| 2886 | |
| 2887 | for (i = 0; i < num_values; i++) { |
| 2888 | ret = drm_property_add_enum(property, i, |
| 2889 | props[i].type, |
| 2890 | props[i].name); |
| 2891 | if (ret) { |
| 2892 | drm_property_destroy(dev, property); |
| 2893 | return NULL; |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | return property; |
| 2898 | } |
| 2899 | EXPORT_SYMBOL(drm_property_create_bitmask); |
| 2900 | |
Sascha Hauer | d9bc3c0 | 2012-02-06 10:58:18 +0100 | [diff] [blame] | 2901 | struct drm_property *drm_property_create_range(struct drm_device *dev, int flags, |
| 2902 | const char *name, |
| 2903 | uint64_t min, uint64_t max) |
| 2904 | { |
| 2905 | struct drm_property *property; |
| 2906 | |
| 2907 | flags |= DRM_MODE_PROP_RANGE; |
| 2908 | |
| 2909 | property = drm_property_create(dev, flags, name, 2); |
| 2910 | if (!property) |
| 2911 | return NULL; |
| 2912 | |
| 2913 | property->values[0] = min; |
| 2914 | property->values[1] = max; |
| 2915 | |
| 2916 | return property; |
| 2917 | } |
| 2918 | EXPORT_SYMBOL(drm_property_create_range); |
| 2919 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2920 | int drm_property_add_enum(struct drm_property *property, int index, |
| 2921 | uint64_t value, const char *name) |
| 2922 | { |
| 2923 | struct drm_property_enum *prop_enum; |
| 2924 | |
Rob Clark | 49e2754 | 2012-05-17 02:23:26 -0600 | [diff] [blame] | 2925 | if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK))) |
| 2926 | return -EINVAL; |
| 2927 | |
| 2928 | /* |
| 2929 | * Bitmask enum properties have the additional constraint of values |
| 2930 | * from 0 to 63 |
| 2931 | */ |
| 2932 | if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63)) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 2933 | return -EINVAL; |
| 2934 | |
| 2935 | if (!list_empty(&property->enum_blob_list)) { |
| 2936 | list_for_each_entry(prop_enum, &property->enum_blob_list, head) { |
| 2937 | if (prop_enum->value == value) { |
| 2938 | strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); |
| 2939 | prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0'; |
| 2940 | return 0; |
| 2941 | } |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL); |
| 2946 | if (!prop_enum) |
| 2947 | return -ENOMEM; |
| 2948 | |
| 2949 | strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); |
| 2950 | prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0'; |
| 2951 | prop_enum->value = value; |
| 2952 | |
| 2953 | property->values[index] = value; |
| 2954 | list_add_tail(&prop_enum->head, &property->enum_blob_list); |
| 2955 | return 0; |
| 2956 | } |
| 2957 | EXPORT_SYMBOL(drm_property_add_enum); |
| 2958 | |
| 2959 | void drm_property_destroy(struct drm_device *dev, struct drm_property *property) |
| 2960 | { |
| 2961 | struct drm_property_enum *prop_enum, *pt; |
| 2962 | |
| 2963 | list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) { |
| 2964 | list_del(&prop_enum->head); |
| 2965 | kfree(prop_enum); |
| 2966 | } |
| 2967 | |
| 2968 | if (property->num_values) |
| 2969 | kfree(property->values); |
| 2970 | drm_mode_object_put(dev, &property->base); |
| 2971 | list_del(&property->head); |
| 2972 | kfree(property); |
| 2973 | } |
| 2974 | EXPORT_SYMBOL(drm_property_destroy); |
| 2975 | |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 2976 | void drm_object_attach_property(struct drm_mode_object *obj, |
| 2977 | struct drm_property *property, |
| 2978 | uint64_t init_val) |
| 2979 | { |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 2980 | int count = obj->properties->count; |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 2981 | |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 2982 | if (count == DRM_OBJECT_MAX_PROPERTY) { |
| 2983 | WARN(1, "Failed to attach object property (type: 0x%x). Please " |
| 2984 | "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time " |
| 2985 | "you see this message on the same object type.\n", |
| 2986 | obj->type); |
| 2987 | return; |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 2988 | } |
| 2989 | |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 2990 | obj->properties->ids[count] = property->base.id; |
| 2991 | obj->properties->values[count] = init_val; |
| 2992 | obj->properties->count++; |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 2993 | } |
| 2994 | EXPORT_SYMBOL(drm_object_attach_property); |
| 2995 | |
| 2996 | int drm_object_property_set_value(struct drm_mode_object *obj, |
| 2997 | struct drm_property *property, uint64_t val) |
| 2998 | { |
| 2999 | int i; |
| 3000 | |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 3001 | for (i = 0; i < obj->properties->count; i++) { |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3002 | if (obj->properties->ids[i] == property->base.id) { |
| 3003 | obj->properties->values[i] = val; |
| 3004 | return 0; |
| 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | return -EINVAL; |
| 3009 | } |
| 3010 | EXPORT_SYMBOL(drm_object_property_set_value); |
| 3011 | |
| 3012 | int drm_object_property_get_value(struct drm_mode_object *obj, |
| 3013 | struct drm_property *property, uint64_t *val) |
| 3014 | { |
| 3015 | int i; |
| 3016 | |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 3017 | for (i = 0; i < obj->properties->count; i++) { |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3018 | if (obj->properties->ids[i] == property->base.id) { |
| 3019 | *val = obj->properties->values[i]; |
| 3020 | return 0; |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | return -EINVAL; |
| 3025 | } |
| 3026 | EXPORT_SYMBOL(drm_object_property_get_value); |
| 3027 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3028 | int drm_mode_getproperty_ioctl(struct drm_device *dev, |
| 3029 | void *data, struct drm_file *file_priv) |
| 3030 | { |
| 3031 | struct drm_mode_object *obj; |
| 3032 | struct drm_mode_get_property *out_resp = data; |
| 3033 | struct drm_property *property; |
| 3034 | int enum_count = 0; |
| 3035 | int blob_count = 0; |
| 3036 | int value_count = 0; |
| 3037 | int ret = 0, i; |
| 3038 | int copied; |
| 3039 | struct drm_property_enum *prop_enum; |
| 3040 | struct drm_mode_property_enum __user *enum_ptr; |
| 3041 | struct drm_property_blob *prop_blob; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 3042 | uint32_t __user *blob_id_ptr; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3043 | uint64_t __user *values_ptr; |
| 3044 | uint32_t __user *blob_length_ptr; |
| 3045 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 3046 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3047 | return -EINVAL; |
| 3048 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3049 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3050 | obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY); |
| 3051 | if (!obj) { |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3052 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3053 | goto done; |
| 3054 | } |
| 3055 | property = obj_to_property(obj); |
| 3056 | |
Rob Clark | 49e2754 | 2012-05-17 02:23:26 -0600 | [diff] [blame] | 3057 | if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3058 | list_for_each_entry(prop_enum, &property->enum_blob_list, head) |
| 3059 | enum_count++; |
| 3060 | } else if (property->flags & DRM_MODE_PROP_BLOB) { |
| 3061 | list_for_each_entry(prop_blob, &property->enum_blob_list, head) |
| 3062 | blob_count++; |
| 3063 | } |
| 3064 | |
| 3065 | value_count = property->num_values; |
| 3066 | |
| 3067 | strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN); |
| 3068 | out_resp->name[DRM_PROP_NAME_LEN-1] = 0; |
| 3069 | out_resp->flags = property->flags; |
| 3070 | |
| 3071 | if ((out_resp->count_values >= value_count) && value_count) { |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 3072 | values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3073 | for (i = 0; i < value_count; i++) { |
| 3074 | if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) { |
| 3075 | ret = -EFAULT; |
| 3076 | goto done; |
| 3077 | } |
| 3078 | } |
| 3079 | } |
| 3080 | out_resp->count_values = value_count; |
| 3081 | |
Rob Clark | 49e2754 | 2012-05-17 02:23:26 -0600 | [diff] [blame] | 3082 | if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3083 | if ((out_resp->count_enum_blobs >= enum_count) && enum_count) { |
| 3084 | copied = 0; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 3085 | enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3086 | list_for_each_entry(prop_enum, &property->enum_blob_list, head) { |
| 3087 | |
| 3088 | if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) { |
| 3089 | ret = -EFAULT; |
| 3090 | goto done; |
| 3091 | } |
| 3092 | |
| 3093 | if (copy_to_user(&enum_ptr[copied].name, |
| 3094 | &prop_enum->name, DRM_PROP_NAME_LEN)) { |
| 3095 | ret = -EFAULT; |
| 3096 | goto done; |
| 3097 | } |
| 3098 | copied++; |
| 3099 | } |
| 3100 | } |
| 3101 | out_resp->count_enum_blobs = enum_count; |
| 3102 | } |
| 3103 | |
| 3104 | if (property->flags & DRM_MODE_PROP_BLOB) { |
| 3105 | if ((out_resp->count_enum_blobs >= blob_count) && blob_count) { |
| 3106 | copied = 0; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 3107 | blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr; |
| 3108 | blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3109 | |
| 3110 | list_for_each_entry(prop_blob, &property->enum_blob_list, head) { |
| 3111 | if (put_user(prop_blob->base.id, blob_id_ptr + copied)) { |
| 3112 | ret = -EFAULT; |
| 3113 | goto done; |
| 3114 | } |
| 3115 | |
| 3116 | if (put_user(prop_blob->length, blob_length_ptr + copied)) { |
| 3117 | ret = -EFAULT; |
| 3118 | goto done; |
| 3119 | } |
| 3120 | |
| 3121 | copied++; |
| 3122 | } |
| 3123 | } |
| 3124 | out_resp->count_enum_blobs = blob_count; |
| 3125 | } |
| 3126 | done: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3127 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3128 | return ret; |
| 3129 | } |
| 3130 | |
| 3131 | static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length, |
| 3132 | void *data) |
| 3133 | { |
| 3134 | struct drm_property_blob *blob; |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 3135 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3136 | |
| 3137 | if (!length || !data) |
| 3138 | return NULL; |
| 3139 | |
| 3140 | blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL); |
| 3141 | if (!blob) |
| 3142 | return NULL; |
| 3143 | |
Ville Syrjälä | 6bfc56a | 2012-03-13 12:35:48 +0200 | [diff] [blame] | 3144 | ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB); |
| 3145 | if (ret) { |
| 3146 | kfree(blob); |
| 3147 | return NULL; |
| 3148 | } |
| 3149 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3150 | blob->length = length; |
| 3151 | |
| 3152 | memcpy(blob->data, data, length); |
| 3153 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3154 | list_add_tail(&blob->head, &dev->mode_config.property_blob_list); |
| 3155 | return blob; |
| 3156 | } |
| 3157 | |
| 3158 | static void drm_property_destroy_blob(struct drm_device *dev, |
| 3159 | struct drm_property_blob *blob) |
| 3160 | { |
| 3161 | drm_mode_object_put(dev, &blob->base); |
| 3162 | list_del(&blob->head); |
| 3163 | kfree(blob); |
| 3164 | } |
| 3165 | |
| 3166 | int drm_mode_getblob_ioctl(struct drm_device *dev, |
| 3167 | void *data, struct drm_file *file_priv) |
| 3168 | { |
| 3169 | struct drm_mode_object *obj; |
| 3170 | struct drm_mode_get_blob *out_resp = data; |
| 3171 | struct drm_property_blob *blob; |
| 3172 | int ret = 0; |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 3173 | void __user *blob_ptr; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3174 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 3175 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3176 | return -EINVAL; |
| 3177 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3178 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3179 | obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB); |
| 3180 | if (!obj) { |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3181 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3182 | goto done; |
| 3183 | } |
| 3184 | blob = obj_to_blob(obj); |
| 3185 | |
| 3186 | if (out_resp->length == blob->length) { |
Ville Syrjälä | 81f6c7f | 2011-12-20 00:06:42 +0200 | [diff] [blame] | 3187 | blob_ptr = (void __user *)(unsigned long)out_resp->data; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3188 | if (copy_to_user(blob_ptr, blob->data, blob->length)){ |
| 3189 | ret = -EFAULT; |
| 3190 | goto done; |
| 3191 | } |
| 3192 | } |
| 3193 | out_resp->length = blob->length; |
| 3194 | |
| 3195 | done: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3196 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3197 | return ret; |
| 3198 | } |
| 3199 | |
| 3200 | int drm_mode_connector_update_edid_property(struct drm_connector *connector, |
| 3201 | struct edid *edid) |
| 3202 | { |
| 3203 | struct drm_device *dev = connector->dev; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 3204 | int ret, size; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3205 | |
| 3206 | if (connector->edid_blob_ptr) |
| 3207 | drm_property_destroy_blob(dev, connector->edid_blob_ptr); |
| 3208 | |
| 3209 | /* Delete edid, when there is none. */ |
| 3210 | if (!edid) { |
| 3211 | connector->edid_blob_ptr = NULL; |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 3212 | ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3213 | return ret; |
| 3214 | } |
| 3215 | |
Adam Jackson | 7466f4c | 2010-03-29 21:43:23 +0000 | [diff] [blame] | 3216 | size = EDID_LENGTH * (1 + edid->extensions); |
| 3217 | connector->edid_blob_ptr = drm_property_create_blob(connector->dev, |
| 3218 | size, edid); |
Sachin Kamat | e655d12 | 2012-11-19 09:44:57 +0000 | [diff] [blame] | 3219 | if (!connector->edid_blob_ptr) |
| 3220 | return -EINVAL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3221 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 3222 | ret = drm_object_property_set_value(&connector->base, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3223 | dev->mode_config.edid_property, |
| 3224 | connector->edid_blob_ptr->base.id); |
| 3225 | |
| 3226 | return ret; |
| 3227 | } |
| 3228 | EXPORT_SYMBOL(drm_mode_connector_update_edid_property); |
| 3229 | |
Paulo Zanoni | 26a3481 | 2012-05-15 18:08:59 -0300 | [diff] [blame] | 3230 | static bool drm_property_change_is_valid(struct drm_property *property, |
Ville Syrjälä | 592c20e | 2012-05-24 20:53:58 +0300 | [diff] [blame] | 3231 | uint64_t value) |
Paulo Zanoni | 26a3481 | 2012-05-15 18:08:59 -0300 | [diff] [blame] | 3232 | { |
| 3233 | if (property->flags & DRM_MODE_PROP_IMMUTABLE) |
| 3234 | return false; |
| 3235 | if (property->flags & DRM_MODE_PROP_RANGE) { |
| 3236 | if (value < property->values[0] || value > property->values[1]) |
| 3237 | return false; |
| 3238 | return true; |
Rob Clark | 49e2754 | 2012-05-17 02:23:26 -0600 | [diff] [blame] | 3239 | } else if (property->flags & DRM_MODE_PROP_BITMASK) { |
| 3240 | int i; |
Ville Syrjälä | 592c20e | 2012-05-24 20:53:58 +0300 | [diff] [blame] | 3241 | uint64_t valid_mask = 0; |
Rob Clark | 49e2754 | 2012-05-17 02:23:26 -0600 | [diff] [blame] | 3242 | for (i = 0; i < property->num_values; i++) |
| 3243 | valid_mask |= (1ULL << property->values[i]); |
| 3244 | return !(value & ~valid_mask); |
Ville Syrjälä | c4a5675 | 2012-10-25 18:05:06 +0000 | [diff] [blame] | 3245 | } else if (property->flags & DRM_MODE_PROP_BLOB) { |
| 3246 | /* Only the driver knows */ |
| 3247 | return true; |
Paulo Zanoni | 26a3481 | 2012-05-15 18:08:59 -0300 | [diff] [blame] | 3248 | } else { |
| 3249 | int i; |
| 3250 | for (i = 0; i < property->num_values; i++) |
| 3251 | if (property->values[i] == value) |
| 3252 | return true; |
| 3253 | return false; |
| 3254 | } |
| 3255 | } |
| 3256 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3257 | int drm_mode_connector_property_set_ioctl(struct drm_device *dev, |
| 3258 | void *data, struct drm_file *file_priv) |
| 3259 | { |
Paulo Zanoni | 0057d8d | 2012-05-15 18:09:03 -0300 | [diff] [blame] | 3260 | struct drm_mode_connector_set_property *conn_set_prop = data; |
| 3261 | struct drm_mode_obj_set_property obj_set_prop = { |
| 3262 | .value = conn_set_prop->value, |
| 3263 | .prop_id = conn_set_prop->prop_id, |
| 3264 | .obj_id = conn_set_prop->connector_id, |
| 3265 | .obj_type = DRM_MODE_OBJECT_CONNECTOR |
| 3266 | }; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3267 | |
Paulo Zanoni | 0057d8d | 2012-05-15 18:09:03 -0300 | [diff] [blame] | 3268 | /* It does all the locking and checking we need */ |
| 3269 | return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3270 | } |
| 3271 | |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3272 | static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj, |
| 3273 | struct drm_property *property, |
| 3274 | uint64_t value) |
| 3275 | { |
| 3276 | int ret = -EINVAL; |
| 3277 | struct drm_connector *connector = obj_to_connector(obj); |
| 3278 | |
| 3279 | /* Do DPMS ourselves */ |
| 3280 | if (property == connector->dev->mode_config.dpms_property) { |
| 3281 | if (connector->funcs->dpms) |
| 3282 | (*connector->funcs->dpms)(connector, (int)value); |
| 3283 | ret = 0; |
| 3284 | } else if (connector->funcs->set_property) |
| 3285 | ret = connector->funcs->set_property(connector, property, value); |
| 3286 | |
| 3287 | /* store the property value if successful */ |
| 3288 | if (!ret) |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 3289 | drm_object_property_set_value(&connector->base, property, value); |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3290 | return ret; |
| 3291 | } |
| 3292 | |
Paulo Zanoni | bffd9de0 | 2012-05-15 18:09:05 -0300 | [diff] [blame] | 3293 | static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj, |
| 3294 | struct drm_property *property, |
| 3295 | uint64_t value) |
| 3296 | { |
| 3297 | int ret = -EINVAL; |
| 3298 | struct drm_crtc *crtc = obj_to_crtc(obj); |
| 3299 | |
| 3300 | if (crtc->funcs->set_property) |
| 3301 | ret = crtc->funcs->set_property(crtc, property, value); |
| 3302 | if (!ret) |
| 3303 | drm_object_property_set_value(obj, property, value); |
| 3304 | |
| 3305 | return ret; |
| 3306 | } |
| 3307 | |
Rob Clark | 4d93914 | 2012-05-17 02:23:27 -0600 | [diff] [blame] | 3308 | static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj, |
| 3309 | struct drm_property *property, |
| 3310 | uint64_t value) |
| 3311 | { |
| 3312 | int ret = -EINVAL; |
| 3313 | struct drm_plane *plane = obj_to_plane(obj); |
| 3314 | |
| 3315 | if (plane->funcs->set_property) |
| 3316 | ret = plane->funcs->set_property(plane, property, value); |
| 3317 | if (!ret) |
| 3318 | drm_object_property_set_value(obj, property, value); |
| 3319 | |
| 3320 | return ret; |
| 3321 | } |
| 3322 | |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3323 | int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data, |
| 3324 | struct drm_file *file_priv) |
| 3325 | { |
| 3326 | struct drm_mode_obj_get_properties *arg = data; |
| 3327 | struct drm_mode_object *obj; |
| 3328 | int ret = 0; |
| 3329 | int i; |
| 3330 | int copied = 0; |
| 3331 | int props_count = 0; |
| 3332 | uint32_t __user *props_ptr; |
| 3333 | uint64_t __user *prop_values_ptr; |
| 3334 | |
| 3335 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3336 | return -EINVAL; |
| 3337 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3338 | drm_modeset_lock_all(dev); |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3339 | |
| 3340 | obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type); |
| 3341 | if (!obj) { |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3342 | ret = -ENOENT; |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3343 | goto out; |
| 3344 | } |
| 3345 | if (!obj->properties) { |
| 3346 | ret = -EINVAL; |
| 3347 | goto out; |
| 3348 | } |
| 3349 | |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 3350 | props_count = obj->properties->count; |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3351 | |
| 3352 | /* This ioctl is called twice, once to determine how much space is |
| 3353 | * needed, and the 2nd time to fill it. */ |
| 3354 | if ((arg->count_props >= props_count) && props_count) { |
| 3355 | copied = 0; |
| 3356 | props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); |
| 3357 | prop_values_ptr = (uint64_t __user *)(unsigned long) |
| 3358 | (arg->prop_values_ptr); |
| 3359 | for (i = 0; i < props_count; i++) { |
| 3360 | if (put_user(obj->properties->ids[i], |
| 3361 | props_ptr + copied)) { |
| 3362 | ret = -EFAULT; |
| 3363 | goto out; |
| 3364 | } |
| 3365 | if (put_user(obj->properties->values[i], |
| 3366 | prop_values_ptr + copied)) { |
| 3367 | ret = -EFAULT; |
| 3368 | goto out; |
| 3369 | } |
| 3370 | copied++; |
| 3371 | } |
| 3372 | } |
| 3373 | arg->count_props = props_count; |
| 3374 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3375 | drm_modeset_unlock_all(dev); |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3376 | return ret; |
| 3377 | } |
| 3378 | |
| 3379 | int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data, |
| 3380 | struct drm_file *file_priv) |
| 3381 | { |
| 3382 | struct drm_mode_obj_set_property *arg = data; |
| 3383 | struct drm_mode_object *arg_obj; |
| 3384 | struct drm_mode_object *prop_obj; |
| 3385 | struct drm_property *property; |
| 3386 | int ret = -EINVAL; |
| 3387 | int i; |
| 3388 | |
| 3389 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3390 | return -EINVAL; |
| 3391 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3392 | drm_modeset_lock_all(dev); |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3393 | |
| 3394 | arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type); |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3395 | if (!arg_obj) { |
| 3396 | ret = -ENOENT; |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3397 | goto out; |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3398 | } |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3399 | if (!arg_obj->properties) |
| 3400 | goto out; |
| 3401 | |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 3402 | for (i = 0; i < arg_obj->properties->count; i++) |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3403 | if (arg_obj->properties->ids[i] == arg->prop_id) |
| 3404 | break; |
| 3405 | |
Paulo Zanoni | 7f88a9b | 2012-05-15 18:09:04 -0300 | [diff] [blame] | 3406 | if (i == arg_obj->properties->count) |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3407 | goto out; |
| 3408 | |
| 3409 | prop_obj = drm_mode_object_find(dev, arg->prop_id, |
| 3410 | DRM_MODE_OBJECT_PROPERTY); |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3411 | if (!prop_obj) { |
| 3412 | ret = -ENOENT; |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3413 | goto out; |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3414 | } |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3415 | property = obj_to_property(prop_obj); |
| 3416 | |
| 3417 | if (!drm_property_change_is_valid(property, arg->value)) |
| 3418 | goto out; |
| 3419 | |
| 3420 | switch (arg_obj->type) { |
| 3421 | case DRM_MODE_OBJECT_CONNECTOR: |
| 3422 | ret = drm_mode_connector_set_obj_prop(arg_obj, property, |
| 3423 | arg->value); |
| 3424 | break; |
Paulo Zanoni | bffd9de0 | 2012-05-15 18:09:05 -0300 | [diff] [blame] | 3425 | case DRM_MODE_OBJECT_CRTC: |
| 3426 | ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value); |
| 3427 | break; |
Rob Clark | 4d93914 | 2012-05-17 02:23:27 -0600 | [diff] [blame] | 3428 | case DRM_MODE_OBJECT_PLANE: |
| 3429 | ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value); |
| 3430 | break; |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3434 | drm_modeset_unlock_all(dev); |
Paulo Zanoni | c543188 | 2012-05-15 18:09:02 -0300 | [diff] [blame] | 3435 | return ret; |
| 3436 | } |
| 3437 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3438 | int drm_mode_connector_attach_encoder(struct drm_connector *connector, |
| 3439 | struct drm_encoder *encoder) |
| 3440 | { |
| 3441 | int i; |
| 3442 | |
| 3443 | for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { |
| 3444 | if (connector->encoder_ids[i] == 0) { |
| 3445 | connector->encoder_ids[i] = encoder->base.id; |
| 3446 | return 0; |
| 3447 | } |
| 3448 | } |
| 3449 | return -ENOMEM; |
| 3450 | } |
| 3451 | EXPORT_SYMBOL(drm_mode_connector_attach_encoder); |
| 3452 | |
Sascha Hauer | 4cae5b8 | 2012-02-01 11:38:23 +0100 | [diff] [blame] | 3453 | int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3454 | int gamma_size) |
| 3455 | { |
| 3456 | crtc->gamma_size = gamma_size; |
| 3457 | |
| 3458 | crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL); |
| 3459 | if (!crtc->gamma_store) { |
| 3460 | crtc->gamma_size = 0; |
Sascha Hauer | 4cae5b8 | 2012-02-01 11:38:23 +0100 | [diff] [blame] | 3461 | return -ENOMEM; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3462 | } |
| 3463 | |
Sascha Hauer | 4cae5b8 | 2012-02-01 11:38:23 +0100 | [diff] [blame] | 3464 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3465 | } |
| 3466 | EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size); |
| 3467 | |
| 3468 | int drm_mode_gamma_set_ioctl(struct drm_device *dev, |
| 3469 | void *data, struct drm_file *file_priv) |
| 3470 | { |
| 3471 | struct drm_mode_crtc_lut *crtc_lut = data; |
| 3472 | struct drm_mode_object *obj; |
| 3473 | struct drm_crtc *crtc; |
| 3474 | void *r_base, *g_base, *b_base; |
| 3475 | int size; |
| 3476 | int ret = 0; |
| 3477 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 3478 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3479 | return -EINVAL; |
| 3480 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3481 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3482 | obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC); |
| 3483 | if (!obj) { |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3484 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3485 | goto out; |
| 3486 | } |
| 3487 | crtc = obj_to_crtc(obj); |
| 3488 | |
Laurent Pinchart | ebe0f24 | 2012-05-17 13:27:24 +0200 | [diff] [blame] | 3489 | if (crtc->funcs->gamma_set == NULL) { |
| 3490 | ret = -ENOSYS; |
| 3491 | goto out; |
| 3492 | } |
| 3493 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3494 | /* memcpy into gamma store */ |
| 3495 | if (crtc_lut->gamma_size != crtc->gamma_size) { |
| 3496 | ret = -EINVAL; |
| 3497 | goto out; |
| 3498 | } |
| 3499 | |
| 3500 | size = crtc_lut->gamma_size * (sizeof(uint16_t)); |
| 3501 | r_base = crtc->gamma_store; |
| 3502 | if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) { |
| 3503 | ret = -EFAULT; |
| 3504 | goto out; |
| 3505 | } |
| 3506 | |
| 3507 | g_base = r_base + size; |
| 3508 | if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) { |
| 3509 | ret = -EFAULT; |
| 3510 | goto out; |
| 3511 | } |
| 3512 | |
| 3513 | b_base = g_base + size; |
| 3514 | if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) { |
| 3515 | ret = -EFAULT; |
| 3516 | goto out; |
| 3517 | } |
| 3518 | |
James Simmons | 7203425 | 2010-08-03 01:33:19 +0100 | [diff] [blame] | 3519 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3520 | |
| 3521 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3522 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3523 | return ret; |
| 3524 | |
| 3525 | } |
| 3526 | |
| 3527 | int drm_mode_gamma_get_ioctl(struct drm_device *dev, |
| 3528 | void *data, struct drm_file *file_priv) |
| 3529 | { |
| 3530 | struct drm_mode_crtc_lut *crtc_lut = data; |
| 3531 | struct drm_mode_object *obj; |
| 3532 | struct drm_crtc *crtc; |
| 3533 | void *r_base, *g_base, *b_base; |
| 3534 | int size; |
| 3535 | int ret = 0; |
| 3536 | |
Dave Airlie | fb3b06c | 2011-02-08 13:55:21 +1000 | [diff] [blame] | 3537 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3538 | return -EINVAL; |
| 3539 | |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3540 | drm_modeset_lock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3541 | obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC); |
| 3542 | if (!obj) { |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3543 | ret = -ENOENT; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3544 | goto out; |
| 3545 | } |
| 3546 | crtc = obj_to_crtc(obj); |
| 3547 | |
| 3548 | /* memcpy into gamma store */ |
| 3549 | if (crtc_lut->gamma_size != crtc->gamma_size) { |
| 3550 | ret = -EINVAL; |
| 3551 | goto out; |
| 3552 | } |
| 3553 | |
| 3554 | size = crtc_lut->gamma_size * (sizeof(uint16_t)); |
| 3555 | r_base = crtc->gamma_store; |
| 3556 | if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) { |
| 3557 | ret = -EFAULT; |
| 3558 | goto out; |
| 3559 | } |
| 3560 | |
| 3561 | g_base = r_base + size; |
| 3562 | if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) { |
| 3563 | ret = -EFAULT; |
| 3564 | goto out; |
| 3565 | } |
| 3566 | |
| 3567 | b_base = g_base + size; |
| 3568 | if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) { |
| 3569 | ret = -EFAULT; |
| 3570 | goto out; |
| 3571 | } |
| 3572 | out: |
Daniel Vetter | 8484990 | 2012-12-02 00:28:11 +0100 | [diff] [blame] | 3573 | drm_modeset_unlock_all(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 3574 | return ret; |
| 3575 | } |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3576 | |
| 3577 | int drm_mode_page_flip_ioctl(struct drm_device *dev, |
| 3578 | void *data, struct drm_file *file_priv) |
| 3579 | { |
| 3580 | struct drm_mode_crtc_page_flip *page_flip = data; |
| 3581 | struct drm_mode_object *obj; |
| 3582 | struct drm_crtc *crtc; |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 3583 | struct drm_framebuffer *fb = NULL, *old_fb = NULL; |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3584 | struct drm_pending_vblank_event *e = NULL; |
| 3585 | unsigned long flags; |
| 3586 | int ret = -EINVAL; |
| 3587 | |
| 3588 | if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS || |
| 3589 | page_flip->reserved != 0) |
| 3590 | return -EINVAL; |
| 3591 | |
Keith Packard | 62f2104 | 2013-07-22 18:50:00 -0700 | [diff] [blame] | 3592 | if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip) |
| 3593 | return -EINVAL; |
| 3594 | |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3595 | obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC); |
| 3596 | if (!obj) |
Ville Syrjälä | f27657f | 2013-10-17 13:35:00 +0300 | [diff] [blame] | 3597 | return -ENOENT; |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3598 | crtc = obj_to_crtc(obj); |
| 3599 | |
Daniel Vetter | b4d5e7d | 2012-12-11 16:59:31 +0100 | [diff] [blame] | 3600 | mutex_lock(&crtc->mutex); |
Chris Wilson | 90c1efd | 2010-07-17 20:23:26 +0100 | [diff] [blame] | 3601 | if (crtc->fb == NULL) { |
| 3602 | /* The framebuffer is currently unbound, presumably |
| 3603 | * due to a hotplug event, that userspace has not |
| 3604 | * yet discovered. |
| 3605 | */ |
| 3606 | ret = -EBUSY; |
| 3607 | goto out; |
| 3608 | } |
| 3609 | |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3610 | if (crtc->funcs->page_flip == NULL) |
| 3611 | goto out; |
| 3612 | |
Daniel Vetter | 786b99e | 2012-12-02 21:53:40 +0100 | [diff] [blame] | 3613 | fb = drm_framebuffer_lookup(dev, page_flip->fb_id); |
Ville Syrjälä | 37c4e70 | 2013-10-17 13:35:01 +0300 | [diff] [blame] | 3614 | if (!fb) { |
| 3615 | ret = -ENOENT; |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3616 | goto out; |
Ville Syrjälä | 37c4e70 | 2013-10-17 13:35:01 +0300 | [diff] [blame] | 3617 | } |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3618 | |
Damien Lespiau | c11e928 | 2013-09-25 16:45:30 +0100 | [diff] [blame] | 3619 | ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb); |
| 3620 | if (ret) |
Ville Syrjälä | 5f61bb4 | 2012-03-13 12:35:45 +0200 | [diff] [blame] | 3621 | goto out; |
Ville Syrjälä | 5f61bb4 | 2012-03-13 12:35:45 +0200 | [diff] [blame] | 3622 | |
Laurent Pinchart | 909d9cd | 2013-04-22 01:38:46 +0200 | [diff] [blame] | 3623 | if (crtc->fb->pixel_format != fb->pixel_format) { |
| 3624 | DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n"); |
| 3625 | ret = -EINVAL; |
| 3626 | goto out; |
| 3627 | } |
| 3628 | |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3629 | if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { |
| 3630 | ret = -ENOMEM; |
| 3631 | spin_lock_irqsave(&dev->event_lock, flags); |
| 3632 | if (file_priv->event_space < sizeof e->event) { |
| 3633 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 3634 | goto out; |
| 3635 | } |
| 3636 | file_priv->event_space -= sizeof e->event; |
| 3637 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 3638 | |
| 3639 | e = kzalloc(sizeof *e, GFP_KERNEL); |
| 3640 | if (e == NULL) { |
| 3641 | spin_lock_irqsave(&dev->event_lock, flags); |
| 3642 | file_priv->event_space += sizeof e->event; |
| 3643 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 3644 | goto out; |
| 3645 | } |
| 3646 | |
Jesse Barnes | 7bd4d7b | 2009-11-19 10:50:22 -0800 | [diff] [blame] | 3647 | e->event.base.type = DRM_EVENT_FLIP_COMPLETE; |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3648 | e->event.base.length = sizeof e->event; |
| 3649 | e->event.user_data = page_flip->user_data; |
| 3650 | e->base.event = &e->event.base; |
| 3651 | e->base.file_priv = file_priv; |
| 3652 | e->base.destroy = |
| 3653 | (void (*) (struct drm_pending_event *)) kfree; |
| 3654 | } |
| 3655 | |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 3656 | old_fb = crtc->fb; |
Keith Packard | ed8d197 | 2013-07-22 18:49:58 -0700 | [diff] [blame] | 3657 | ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags); |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3658 | if (ret) { |
Joonyoung Shim | aef6a7e | 2012-04-18 13:47:02 +0900 | [diff] [blame] | 3659 | if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { |
| 3660 | spin_lock_irqsave(&dev->event_lock, flags); |
| 3661 | file_priv->event_space += sizeof e->event; |
| 3662 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 3663 | kfree(e); |
| 3664 | } |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 3665 | /* Keep the old fb, don't unref it. */ |
| 3666 | old_fb = NULL; |
| 3667 | } else { |
Thierry Reding | 8cf1e98 | 2013-02-13 16:08:33 +0100 | [diff] [blame] | 3668 | /* |
| 3669 | * Warn if the driver hasn't properly updated the crtc->fb |
| 3670 | * field to reflect that the new framebuffer is now used. |
| 3671 | * Failing to do so will screw with the reference counting |
| 3672 | * on framebuffers. |
| 3673 | */ |
| 3674 | WARN_ON(crtc->fb != fb); |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 3675 | /* Unref only the old framebuffer. */ |
| 3676 | fb = NULL; |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | out: |
Daniel Vetter | b0d1232 | 2012-12-11 01:07:12 +0100 | [diff] [blame] | 3680 | if (fb) |
| 3681 | drm_framebuffer_unreference(fb); |
| 3682 | if (old_fb) |
| 3683 | drm_framebuffer_unreference(old_fb); |
Daniel Vetter | b4d5e7d | 2012-12-11 16:59:31 +0100 | [diff] [blame] | 3684 | mutex_unlock(&crtc->mutex); |
| 3685 | |
Kristian Høgsberg | d91d8a3 | 2009-11-17 12:43:55 -0500 | [diff] [blame] | 3686 | return ret; |
| 3687 | } |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 3688 | |
| 3689 | void drm_mode_config_reset(struct drm_device *dev) |
| 3690 | { |
| 3691 | struct drm_crtc *crtc; |
| 3692 | struct drm_encoder *encoder; |
| 3693 | struct drm_connector *connector; |
| 3694 | |
| 3695 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 3696 | if (crtc->funcs->reset) |
| 3697 | crtc->funcs->reset(crtc); |
| 3698 | |
| 3699 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) |
| 3700 | if (encoder->funcs->reset) |
| 3701 | encoder->funcs->reset(encoder); |
| 3702 | |
Daniel Vetter | 5e2cb2f | 2012-10-23 18:23:35 +0000 | [diff] [blame] | 3703 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 3704 | connector->status = connector_status_unknown; |
| 3705 | |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 3706 | if (connector->funcs->reset) |
| 3707 | connector->funcs->reset(connector); |
Daniel Vetter | 5e2cb2f | 2012-10-23 18:23:35 +0000 | [diff] [blame] | 3708 | } |
Chris Wilson | eb03355 | 2011-01-24 15:11:08 +0000 | [diff] [blame] | 3709 | } |
| 3710 | EXPORT_SYMBOL(drm_mode_config_reset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 3711 | |
| 3712 | int drm_mode_create_dumb_ioctl(struct drm_device *dev, |
| 3713 | void *data, struct drm_file *file_priv) |
| 3714 | { |
| 3715 | struct drm_mode_create_dumb *args = data; |
| 3716 | |
| 3717 | if (!dev->driver->dumb_create) |
| 3718 | return -ENOSYS; |
| 3719 | return dev->driver->dumb_create(file_priv, dev, args); |
| 3720 | } |
| 3721 | |
| 3722 | int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, |
| 3723 | void *data, struct drm_file *file_priv) |
| 3724 | { |
| 3725 | struct drm_mode_map_dumb *args = data; |
| 3726 | |
| 3727 | /* call driver ioctl to get mmap offset */ |
| 3728 | if (!dev->driver->dumb_map_offset) |
| 3729 | return -ENOSYS; |
| 3730 | |
| 3731 | return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset); |
| 3732 | } |
| 3733 | |
| 3734 | int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, |
| 3735 | void *data, struct drm_file *file_priv) |
| 3736 | { |
| 3737 | struct drm_mode_destroy_dumb *args = data; |
| 3738 | |
| 3739 | if (!dev->driver->dumb_destroy) |
| 3740 | return -ENOSYS; |
| 3741 | |
| 3742 | return dev->driver->dumb_destroy(file_priv, dev, args->handle); |
| 3743 | } |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 3744 | |
| 3745 | /* |
| 3746 | * Just need to support RGB formats here for compat with code that doesn't |
| 3747 | * use pixel formats directly yet. |
| 3748 | */ |
| 3749 | void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth, |
| 3750 | int *bpp) |
| 3751 | { |
| 3752 | switch (format) { |
Ville Syrjälä | c51a6bc | 2013-01-31 19:43:37 +0200 | [diff] [blame] | 3753 | case DRM_FORMAT_C8: |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 3754 | case DRM_FORMAT_RGB332: |
| 3755 | case DRM_FORMAT_BGR233: |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 3756 | *depth = 8; |
| 3757 | *bpp = 8; |
| 3758 | break; |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 3759 | case DRM_FORMAT_XRGB1555: |
| 3760 | case DRM_FORMAT_XBGR1555: |
| 3761 | case DRM_FORMAT_RGBX5551: |
| 3762 | case DRM_FORMAT_BGRX5551: |
| 3763 | case DRM_FORMAT_ARGB1555: |
| 3764 | case DRM_FORMAT_ABGR1555: |
| 3765 | case DRM_FORMAT_RGBA5551: |
| 3766 | case DRM_FORMAT_BGRA5551: |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 3767 | *depth = 15; |
| 3768 | *bpp = 16; |
| 3769 | break; |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 3770 | case DRM_FORMAT_RGB565: |
| 3771 | case DRM_FORMAT_BGR565: |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 3772 | *depth = 16; |
| 3773 | *bpp = 16; |
| 3774 | break; |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 3775 | case DRM_FORMAT_RGB888: |
| 3776 | case DRM_FORMAT_BGR888: |
| 3777 | *depth = 24; |
| 3778 | *bpp = 24; |
| 3779 | break; |
| 3780 | case DRM_FORMAT_XRGB8888: |
| 3781 | case DRM_FORMAT_XBGR8888: |
| 3782 | case DRM_FORMAT_RGBX8888: |
| 3783 | case DRM_FORMAT_BGRX8888: |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 3784 | *depth = 24; |
| 3785 | *bpp = 32; |
| 3786 | break; |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 3787 | case DRM_FORMAT_XRGB2101010: |
| 3788 | case DRM_FORMAT_XBGR2101010: |
| 3789 | case DRM_FORMAT_RGBX1010102: |
| 3790 | case DRM_FORMAT_BGRX1010102: |
| 3791 | case DRM_FORMAT_ARGB2101010: |
| 3792 | case DRM_FORMAT_ABGR2101010: |
| 3793 | case DRM_FORMAT_RGBA1010102: |
| 3794 | case DRM_FORMAT_BGRA1010102: |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 3795 | *depth = 30; |
| 3796 | *bpp = 32; |
| 3797 | break; |
Ville Syrjälä | 04b3924 | 2011-11-17 18:05:13 +0200 | [diff] [blame] | 3798 | case DRM_FORMAT_ARGB8888: |
| 3799 | case DRM_FORMAT_ABGR8888: |
| 3800 | case DRM_FORMAT_RGBA8888: |
| 3801 | case DRM_FORMAT_BGRA8888: |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 3802 | *depth = 32; |
| 3803 | *bpp = 32; |
| 3804 | break; |
| 3805 | default: |
Ville Syrjälä | 23c453a | 2013-10-15 21:06:51 +0300 | [diff] [blame] | 3806 | DRM_DEBUG_KMS("unsupported pixel format %s\n", |
| 3807 | drm_get_format_name(format)); |
Dave Airlie | 248dbc2 | 2011-11-29 20:02:54 +0000 | [diff] [blame] | 3808 | *depth = 0; |
| 3809 | *bpp = 0; |
| 3810 | break; |
| 3811 | } |
| 3812 | } |
| 3813 | EXPORT_SYMBOL(drm_fb_get_bpp_depth); |
Ville Syrjälä | 141670e | 2012-04-05 21:35:15 +0300 | [diff] [blame] | 3814 | |
| 3815 | /** |
| 3816 | * drm_format_num_planes - get the number of planes for format |
| 3817 | * @format: pixel format (DRM_FORMAT_*) |
| 3818 | * |
| 3819 | * RETURNS: |
| 3820 | * The number of planes used by the specified pixel format. |
| 3821 | */ |
| 3822 | int drm_format_num_planes(uint32_t format) |
| 3823 | { |
| 3824 | switch (format) { |
| 3825 | case DRM_FORMAT_YUV410: |
| 3826 | case DRM_FORMAT_YVU410: |
| 3827 | case DRM_FORMAT_YUV411: |
| 3828 | case DRM_FORMAT_YVU411: |
| 3829 | case DRM_FORMAT_YUV420: |
| 3830 | case DRM_FORMAT_YVU420: |
| 3831 | case DRM_FORMAT_YUV422: |
| 3832 | case DRM_FORMAT_YVU422: |
| 3833 | case DRM_FORMAT_YUV444: |
| 3834 | case DRM_FORMAT_YVU444: |
| 3835 | return 3; |
| 3836 | case DRM_FORMAT_NV12: |
| 3837 | case DRM_FORMAT_NV21: |
| 3838 | case DRM_FORMAT_NV16: |
| 3839 | case DRM_FORMAT_NV61: |
Laurent Pinchart | ba623f6 | 2012-05-18 23:47:40 +0200 | [diff] [blame] | 3840 | case DRM_FORMAT_NV24: |
| 3841 | case DRM_FORMAT_NV42: |
Ville Syrjälä | 141670e | 2012-04-05 21:35:15 +0300 | [diff] [blame] | 3842 | return 2; |
| 3843 | default: |
| 3844 | return 1; |
| 3845 | } |
| 3846 | } |
| 3847 | EXPORT_SYMBOL(drm_format_num_planes); |
Ville Syrjälä | 5a86bd5 | 2012-04-05 21:35:16 +0300 | [diff] [blame] | 3848 | |
| 3849 | /** |
| 3850 | * drm_format_plane_cpp - determine the bytes per pixel value |
| 3851 | * @format: pixel format (DRM_FORMAT_*) |
| 3852 | * @plane: plane index |
| 3853 | * |
| 3854 | * RETURNS: |
| 3855 | * The bytes per pixel value for the specified plane. |
| 3856 | */ |
| 3857 | int drm_format_plane_cpp(uint32_t format, int plane) |
| 3858 | { |
| 3859 | unsigned int depth; |
| 3860 | int bpp; |
| 3861 | |
| 3862 | if (plane >= drm_format_num_planes(format)) |
| 3863 | return 0; |
| 3864 | |
| 3865 | switch (format) { |
| 3866 | case DRM_FORMAT_YUYV: |
| 3867 | case DRM_FORMAT_YVYU: |
| 3868 | case DRM_FORMAT_UYVY: |
| 3869 | case DRM_FORMAT_VYUY: |
| 3870 | return 2; |
| 3871 | case DRM_FORMAT_NV12: |
| 3872 | case DRM_FORMAT_NV21: |
| 3873 | case DRM_FORMAT_NV16: |
| 3874 | case DRM_FORMAT_NV61: |
Laurent Pinchart | ba623f6 | 2012-05-18 23:47:40 +0200 | [diff] [blame] | 3875 | case DRM_FORMAT_NV24: |
| 3876 | case DRM_FORMAT_NV42: |
Ville Syrjälä | 5a86bd5 | 2012-04-05 21:35:16 +0300 | [diff] [blame] | 3877 | return plane ? 2 : 1; |
| 3878 | case DRM_FORMAT_YUV410: |
| 3879 | case DRM_FORMAT_YVU410: |
| 3880 | case DRM_FORMAT_YUV411: |
| 3881 | case DRM_FORMAT_YVU411: |
| 3882 | case DRM_FORMAT_YUV420: |
| 3883 | case DRM_FORMAT_YVU420: |
| 3884 | case DRM_FORMAT_YUV422: |
| 3885 | case DRM_FORMAT_YVU422: |
| 3886 | case DRM_FORMAT_YUV444: |
| 3887 | case DRM_FORMAT_YVU444: |
| 3888 | return 1; |
| 3889 | default: |
| 3890 | drm_fb_get_bpp_depth(format, &depth, &bpp); |
| 3891 | return bpp >> 3; |
| 3892 | } |
| 3893 | } |
| 3894 | EXPORT_SYMBOL(drm_format_plane_cpp); |
Ville Syrjälä | 01b68b0 | 2012-04-05 21:35:17 +0300 | [diff] [blame] | 3895 | |
| 3896 | /** |
| 3897 | * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor |
| 3898 | * @format: pixel format (DRM_FORMAT_*) |
| 3899 | * |
| 3900 | * RETURNS: |
| 3901 | * The horizontal chroma subsampling factor for the |
| 3902 | * specified pixel format. |
| 3903 | */ |
| 3904 | int drm_format_horz_chroma_subsampling(uint32_t format) |
| 3905 | { |
| 3906 | switch (format) { |
| 3907 | case DRM_FORMAT_YUV411: |
| 3908 | case DRM_FORMAT_YVU411: |
| 3909 | case DRM_FORMAT_YUV410: |
| 3910 | case DRM_FORMAT_YVU410: |
| 3911 | return 4; |
| 3912 | case DRM_FORMAT_YUYV: |
| 3913 | case DRM_FORMAT_YVYU: |
| 3914 | case DRM_FORMAT_UYVY: |
| 3915 | case DRM_FORMAT_VYUY: |
| 3916 | case DRM_FORMAT_NV12: |
| 3917 | case DRM_FORMAT_NV21: |
| 3918 | case DRM_FORMAT_NV16: |
| 3919 | case DRM_FORMAT_NV61: |
| 3920 | case DRM_FORMAT_YUV422: |
| 3921 | case DRM_FORMAT_YVU422: |
| 3922 | case DRM_FORMAT_YUV420: |
| 3923 | case DRM_FORMAT_YVU420: |
| 3924 | return 2; |
| 3925 | default: |
| 3926 | return 1; |
| 3927 | } |
| 3928 | } |
| 3929 | EXPORT_SYMBOL(drm_format_horz_chroma_subsampling); |
| 3930 | |
| 3931 | /** |
| 3932 | * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor |
| 3933 | * @format: pixel format (DRM_FORMAT_*) |
| 3934 | * |
| 3935 | * RETURNS: |
| 3936 | * The vertical chroma subsampling factor for the |
| 3937 | * specified pixel format. |
| 3938 | */ |
| 3939 | int drm_format_vert_chroma_subsampling(uint32_t format) |
| 3940 | { |
| 3941 | switch (format) { |
| 3942 | case DRM_FORMAT_YUV410: |
| 3943 | case DRM_FORMAT_YVU410: |
| 3944 | return 4; |
| 3945 | case DRM_FORMAT_YUV420: |
| 3946 | case DRM_FORMAT_YVU420: |
| 3947 | case DRM_FORMAT_NV12: |
| 3948 | case DRM_FORMAT_NV21: |
| 3949 | return 2; |
| 3950 | default: |
| 3951 | return 1; |
| 3952 | } |
| 3953 | } |
| 3954 | EXPORT_SYMBOL(drm_format_vert_chroma_subsampling); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 3955 | |
| 3956 | /** |
| 3957 | * drm_mode_config_init - initialize DRM mode_configuration structure |
| 3958 | * @dev: DRM device |
| 3959 | * |
| 3960 | * Initialize @dev's mode_config structure, used for tracking the graphics |
| 3961 | * configuration of @dev. |
| 3962 | * |
| 3963 | * Since this initializes the modeset locks, no locking is possible. Which is no |
| 3964 | * problem, since this should happen single threaded at init time. It is the |
| 3965 | * driver's problem to ensure this guarantee. |
| 3966 | * |
| 3967 | */ |
| 3968 | void drm_mode_config_init(struct drm_device *dev) |
| 3969 | { |
| 3970 | mutex_init(&dev->mode_config.mutex); |
| 3971 | mutex_init(&dev->mode_config.idr_mutex); |
| 3972 | mutex_init(&dev->mode_config.fb_lock); |
| 3973 | INIT_LIST_HEAD(&dev->mode_config.fb_list); |
| 3974 | INIT_LIST_HEAD(&dev->mode_config.crtc_list); |
| 3975 | INIT_LIST_HEAD(&dev->mode_config.connector_list); |
Sean Paul | 3b336ec | 2013-08-14 16:47:37 -0400 | [diff] [blame] | 3976 | INIT_LIST_HEAD(&dev->mode_config.bridge_list); |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 3977 | INIT_LIST_HEAD(&dev->mode_config.encoder_list); |
| 3978 | INIT_LIST_HEAD(&dev->mode_config.property_list); |
| 3979 | INIT_LIST_HEAD(&dev->mode_config.property_blob_list); |
| 3980 | INIT_LIST_HEAD(&dev->mode_config.plane_list); |
| 3981 | idr_init(&dev->mode_config.crtc_idr); |
| 3982 | |
| 3983 | drm_modeset_lock_all(dev); |
| 3984 | drm_mode_create_standard_connector_properties(dev); |
| 3985 | drm_modeset_unlock_all(dev); |
| 3986 | |
| 3987 | /* Just to be sure */ |
| 3988 | dev->mode_config.num_fb = 0; |
| 3989 | dev->mode_config.num_connector = 0; |
| 3990 | dev->mode_config.num_crtc = 0; |
| 3991 | dev->mode_config.num_encoder = 0; |
| 3992 | } |
| 3993 | EXPORT_SYMBOL(drm_mode_config_init); |
| 3994 | |
| 3995 | /** |
| 3996 | * drm_mode_config_cleanup - free up DRM mode_config info |
| 3997 | * @dev: DRM device |
| 3998 | * |
| 3999 | * Free up all the connectors and CRTCs associated with this DRM device, then |
| 4000 | * free up the framebuffers and associated buffer objects. |
| 4001 | * |
| 4002 | * Note that since this /should/ happen single-threaded at driver/device |
| 4003 | * teardown time, no locking is required. It's the driver's job to ensure that |
| 4004 | * this guarantee actually holds true. |
| 4005 | * |
| 4006 | * FIXME: cleanup any dangling user buffer objects too |
| 4007 | */ |
| 4008 | void drm_mode_config_cleanup(struct drm_device *dev) |
| 4009 | { |
| 4010 | struct drm_connector *connector, *ot; |
| 4011 | struct drm_crtc *crtc, *ct; |
| 4012 | struct drm_encoder *encoder, *enct; |
Sean Paul | 3b336ec | 2013-08-14 16:47:37 -0400 | [diff] [blame] | 4013 | struct drm_bridge *bridge, *brt; |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 4014 | struct drm_framebuffer *fb, *fbt; |
| 4015 | struct drm_property *property, *pt; |
| 4016 | struct drm_property_blob *blob, *bt; |
| 4017 | struct drm_plane *plane, *plt; |
| 4018 | |
| 4019 | list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, |
| 4020 | head) { |
| 4021 | encoder->funcs->destroy(encoder); |
| 4022 | } |
| 4023 | |
Sean Paul | 3b336ec | 2013-08-14 16:47:37 -0400 | [diff] [blame] | 4024 | list_for_each_entry_safe(bridge, brt, |
| 4025 | &dev->mode_config.bridge_list, head) { |
| 4026 | bridge->funcs->destroy(bridge); |
| 4027 | } |
| 4028 | |
Laurent Pinchart | 87d24fc | 2013-04-15 15:37:16 +0200 | [diff] [blame] | 4029 | list_for_each_entry_safe(connector, ot, |
| 4030 | &dev->mode_config.connector_list, head) { |
| 4031 | connector->funcs->destroy(connector); |
| 4032 | } |
| 4033 | |
| 4034 | list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, |
| 4035 | head) { |
| 4036 | drm_property_destroy(dev, property); |
| 4037 | } |
| 4038 | |
| 4039 | list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list, |
| 4040 | head) { |
| 4041 | drm_property_destroy_blob(dev, blob); |
| 4042 | } |
| 4043 | |
| 4044 | /* |
| 4045 | * Single-threaded teardown context, so it's not required to grab the |
| 4046 | * fb_lock to protect against concurrent fb_list access. Contrary, it |
| 4047 | * would actually deadlock with the drm_framebuffer_cleanup function. |
| 4048 | * |
| 4049 | * Also, if there are any framebuffers left, that's a driver leak now, |
| 4050 | * so politely WARN about this. |
| 4051 | */ |
| 4052 | WARN_ON(!list_empty(&dev->mode_config.fb_list)); |
| 4053 | list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) { |
| 4054 | drm_framebuffer_remove(fb); |
| 4055 | } |
| 4056 | |
| 4057 | list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list, |
| 4058 | head) { |
| 4059 | plane->funcs->destroy(plane); |
| 4060 | } |
| 4061 | |
| 4062 | list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) { |
| 4063 | crtc->funcs->destroy(crtc); |
| 4064 | } |
| 4065 | |
| 4066 | idr_destroy(&dev->mode_config.crtc_idr); |
| 4067 | } |
| 4068 | EXPORT_SYMBOL(drm_mode_config_cleanup); |