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