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