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