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