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