blob: 4c2367181f3d16e7bce32ac103d0da5217f02dbf [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
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ä6ba6d032013-06-10 11:15:10 +030032#include <linux/ctype.h>
Dave Airlief453ba02008-11-07 14:05:41 -080033#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040035#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
38#include <drm/drm_edid.h>
39#include <drm/drm_fourcc.h>
Dave Airlief453ba02008-11-07 14:05:41 -080040
Daniel Vetter8bd441b2014-01-23 15:35:24 +010041#include "drm_crtc_internal.h"
42
Daniel Vetter84849902012-12-02 00:28:11 +010043/**
44 * drm_modeset_lock_all - take all modeset locks
45 * @dev: drm device
46 *
47 * This function takes all modeset locks, suitable where a more fine-grained
48 * scheme isn't (yet) implemented.
49 */
50void drm_modeset_lock_all(struct drm_device *dev)
51{
Daniel Vetter29494c12012-12-02 02:18:25 +010052 struct drm_crtc *crtc;
53
Daniel Vetter84849902012-12-02 00:28:11 +010054 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter29494c12012-12-02 02:18:25 +010055
56 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
57 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Daniel Vetter84849902012-12-02 00:28:11 +010058}
59EXPORT_SYMBOL(drm_modeset_lock_all);
60
61/**
62 * drm_modeset_unlock_all - drop all modeset locks
63 * @dev: device
64 */
65void drm_modeset_unlock_all(struct drm_device *dev)
66{
Daniel Vetter29494c12012-12-02 02:18:25 +010067 struct drm_crtc *crtc;
68
69 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
70 mutex_unlock(&crtc->mutex);
71
Daniel Vetter84849902012-12-02 00:28:11 +010072 mutex_unlock(&dev->mode_config.mutex);
73}
74EXPORT_SYMBOL(drm_modeset_unlock_all);
75
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010076/**
77 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
78 * @dev: device
79 */
80void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
81{
82 struct drm_crtc *crtc;
83
Daniel Vettera9b054e2013-05-02 09:43:05 +020084 /* Locking is currently fubar in the panic handler. */
85 if (oops_in_progress)
86 return;
87
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010088 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
89 WARN_ON(!mutex_is_locked(&crtc->mutex));
90
91 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
92}
93EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
94
Dave Airlief453ba02008-11-07 14:05:41 -080095/* Avoid boilerplate. I'm tired of typing. */
96#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000097 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080098 { \
99 int i; \
100 for (i = 0; i < ARRAY_SIZE(list); i++) { \
101 if (list[i].type == val) \
102 return list[i].name; \
103 } \
104 return "(unknown)"; \
105 }
106
107/*
108 * Global properties
109 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000110static const struct drm_prop_enum_list drm_dpms_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800111{ { DRM_MODE_DPMS_ON, "On" },
112 { DRM_MODE_DPMS_STANDBY, "Standby" },
113 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
114 { DRM_MODE_DPMS_OFF, "Off" }
115};
116
117DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
118
119/*
120 * Optional properties
121 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000122static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800123{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700124 { DRM_MODE_SCALE_NONE, "None" },
125 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
126 { DRM_MODE_SCALE_CENTER, "Center" },
127 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800128};
129
Dave Airlief453ba02008-11-07 14:05:41 -0800130/*
131 * Non-global properties, but "required" for certain connectors.
132 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000133static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800134{
135 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
136 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
137 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
138};
139
140DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
141
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000142static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800143{
144 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
145 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
146 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
147};
148
149DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
150 drm_dvi_i_subconnector_enum_list)
151
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000152static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800153{
154 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
155 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
156 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
157 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200158 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800159};
160
161DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
162
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000163static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800164{
165 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
166 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
167 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
168 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200169 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800170};
171
172DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
173 drm_tv_subconnector_enum_list)
174
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000175static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000176 { DRM_MODE_DIRTY_OFF, "Off" },
177 { DRM_MODE_DIRTY_ON, "On" },
178 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
179};
180
Dave Airlief453ba02008-11-07 14:05:41 -0800181struct drm_conn_prop_enum_list {
182 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000183 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400184 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800185};
186
187/*
188 * Connector and encoder types.
189 */
190static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400191{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
192 { DRM_MODE_CONNECTOR_VGA, "VGA" },
193 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
194 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
195 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
196 { DRM_MODE_CONNECTOR_Composite, "Composite" },
197 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
198 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
199 { DRM_MODE_CONNECTOR_Component, "Component" },
200 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
201 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
202 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
203 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
204 { DRM_MODE_CONNECTOR_TV, "TV" },
205 { DRM_MODE_CONNECTOR_eDP, "eDP" },
206 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300207 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800208};
209
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000210static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800211{ { DRM_MODE_ENCODER_NONE, "None" },
212 { DRM_MODE_ENCODER_DAC, "DAC" },
213 { DRM_MODE_ENCODER_TMDS, "TMDS" },
214 { DRM_MODE_ENCODER_LVDS, "LVDS" },
215 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200216 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300217 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800218};
219
Jesse Barnesac1bb362014-02-10 15:32:44 -0800220static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
221{
222 { SubPixelUnknown, "Unknown" },
223 { SubPixelHorizontalRGB, "Horizontal RGB" },
224 { SubPixelHorizontalBGR, "Horizontal BGR" },
225 { SubPixelVerticalRGB, "Vertical RGB" },
226 { SubPixelVerticalBGR, "Vertical BGR" },
227 { SubPixelNone, "None" },
228};
229
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400230void drm_connector_ida_init(void)
231{
232 int i;
233
234 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
235 ida_init(&drm_connector_enum_list[i].ida);
236}
237
238void drm_connector_ida_destroy(void)
239{
240 int i;
241
242 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
243 ida_destroy(&drm_connector_enum_list[i].ida);
244}
245
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000246const char *drm_get_encoder_name(const struct drm_encoder *encoder)
Dave Airlief453ba02008-11-07 14:05:41 -0800247{
248 static char buf[32];
249
250 snprintf(buf, 32, "%s-%d",
251 drm_encoder_enum_list[encoder->encoder_type].name,
252 encoder->base.id);
253 return buf;
254}
Dave Airlie13a81952009-09-07 15:45:33 +1000255EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800256
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000257const char *drm_get_connector_name(const struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -0800258{
259 static char buf[32];
260
261 snprintf(buf, 32, "%s-%d",
262 drm_connector_enum_list[connector->connector_type].name,
263 connector->connector_type_id);
264 return buf;
265}
266EXPORT_SYMBOL(drm_get_connector_name);
267
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000268const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800269{
270 if (status == connector_status_connected)
271 return "connected";
272 else if (status == connector_status_disconnected)
273 return "disconnected";
274 else
275 return "unknown";
276}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000277EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800278
Jesse Barnesac1bb362014-02-10 15:32:44 -0800279/**
280 * drm_get_subpixel_order_name - return a string for a given subpixel enum
281 * @order: enum of subpixel_order
282 *
283 * Note you could abuse this and return something out of bounds, but that
284 * would be a caller error. No unscrubbed user data should make it here.
285 */
286const char *drm_get_subpixel_order_name(enum subpixel_order order)
287{
288 return drm_subpixel_enum_list[order].name;
289}
290EXPORT_SYMBOL(drm_get_subpixel_order_name);
291
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300292static char printable_char(int c)
293{
294 return isascii(c) && isprint(c) ? c : '?';
295}
296
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000297const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300298{
299 static char buf[32];
300
301 snprintf(buf, sizeof(buf),
302 "%c%c%c%c %s-endian (0x%08x)",
303 printable_char(format & 0xff),
304 printable_char((format >> 8) & 0xff),
305 printable_char((format >> 16) & 0xff),
306 printable_char((format >> 24) & 0x7f),
307 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
308 format);
309
310 return buf;
311}
312EXPORT_SYMBOL(drm_get_format_name);
313
Dave Airlief453ba02008-11-07 14:05:41 -0800314/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100315 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800316 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100317 * @obj: object pointer, used to generate unique ID
318 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800319 *
Dave Airlief453ba02008-11-07 14:05:41 -0800320 * Create a unique identifier based on @ptr in @dev's identifier space. Used
321 * for tracking modes, CRTCs and connectors.
322 *
323 * RETURNS:
324 * New unique (relative to other objects in @dev) integer identifier for the
325 * object.
326 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100327int drm_mode_object_get(struct drm_device *dev,
328 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800329{
Dave Airlief453ba02008-11-07 14:05:41 -0800330 int ret;
331
Jesse Barnesad2563c2009-01-19 17:21:45 +1000332 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800333 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
334 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100335 /*
336 * Set up the object linking under the protection of the idr
337 * lock so that other users can't see inconsistent state.
338 */
Tejun Heo2e928812013-02-27 17:04:08 -0800339 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100340 obj->type = obj_type;
341 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000342 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100343
Tejun Heo2e928812013-02-27 17:04:08 -0800344 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800345}
346
347/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100348 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800349 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100350 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800351 *
Dave Airlief453ba02008-11-07 14:05:41 -0800352 * Free @id from @dev's unique identifier pool.
353 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100354void drm_mode_object_put(struct drm_device *dev,
355 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800356{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000357 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800358 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000359 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800360}
361
Daniel Vetter786b99e2012-12-02 21:53:40 +0100362/**
363 * drm_mode_object_find - look up a drm object with static lifetime
364 * @dev: drm device
365 * @id: id of the mode object
366 * @type: type of the mode object
367 *
368 * Note that framebuffers cannot be looked up with this functions - since those
369 * are reference counted, they need special treatment.
370 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200371struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
372 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800373{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000374 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800375
Daniel Vetter786b99e2012-12-02 21:53:40 +0100376 /* Framebuffers are reference counted and need their own lookup
377 * function.*/
378 WARN_ON(type == DRM_MODE_OBJECT_FB);
379
Jesse Barnesad2563c2009-01-19 17:21:45 +1000380 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800381 obj = idr_find(&dev->mode_config.crtc_idr, id);
382 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000383 obj = NULL;
384 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800385
386 return obj;
387}
388EXPORT_SYMBOL(drm_mode_object_find);
389
390/**
Dave Airlief453ba02008-11-07 14:05:41 -0800391 * drm_framebuffer_init - initialize a framebuffer
392 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100393 * @fb: framebuffer to be initialized
394 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800395 *
Dave Airlief453ba02008-11-07 14:05:41 -0800396 * Allocates an ID for the framebuffer's parent mode object, sets its mode
397 * functions & device file and adds it to the master fd list.
398 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100399 * IMPORTANT:
400 * This functions publishes the fb and makes it available for concurrent access
401 * by other users. Which means by this point the fb _must_ be fully set up -
402 * since all the fb attributes are invariant over its lifetime, no further
403 * locking but only correct reference counting is required.
404 *
Dave Airlief453ba02008-11-07 14:05:41 -0800405 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200406 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800407 */
408int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
409 const struct drm_framebuffer_funcs *funcs)
410{
411 int ret;
412
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100413 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000414 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100415 INIT_LIST_HEAD(&fb->filp_head);
416 fb->dev = dev;
417 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000418
Dave Airlief453ba02008-11-07 14:05:41 -0800419 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200420 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100421 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800422
Daniel Vetter2b677e82012-12-10 21:16:05 +0100423 /* Grab the idr reference. */
424 drm_framebuffer_reference(fb);
425
Dave Airlief453ba02008-11-07 14:05:41 -0800426 dev->mode_config.num_fb++;
427 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100428out:
429 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800430
431 return 0;
432}
433EXPORT_SYMBOL(drm_framebuffer_init);
434
Rob Clarkf7eff602012-09-05 21:48:38 +0000435static void drm_framebuffer_free(struct kref *kref)
436{
437 struct drm_framebuffer *fb =
438 container_of(kref, struct drm_framebuffer, refcount);
439 fb->funcs->destroy(fb);
440}
441
Daniel Vetter2b677e82012-12-10 21:16:05 +0100442static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
443 uint32_t id)
444{
445 struct drm_mode_object *obj = NULL;
446 struct drm_framebuffer *fb;
447
448 mutex_lock(&dev->mode_config.idr_mutex);
449 obj = idr_find(&dev->mode_config.crtc_idr, id);
450 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
451 fb = NULL;
452 else
453 fb = obj_to_fb(obj);
454 mutex_unlock(&dev->mode_config.idr_mutex);
455
456 return fb;
457}
458
Rob Clarkf7eff602012-09-05 21:48:38 +0000459/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100460 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
461 * @dev: drm device
462 * @id: id of the fb object
463 *
464 * If successful, this grabs an additional reference to the framebuffer -
465 * callers need to make sure to eventually unreference the returned framebuffer
466 * again.
467 */
468struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
469 uint32_t id)
470{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100471 struct drm_framebuffer *fb;
472
473 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100474 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100475 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000476 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100477 mutex_unlock(&dev->mode_config.fb_lock);
478
479 return fb;
480}
481EXPORT_SYMBOL(drm_framebuffer_lookup);
482
483/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000484 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100485 * @fb: framebuffer to unref
486 *
487 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000488 */
489void drm_framebuffer_unreference(struct drm_framebuffer *fb)
490{
Rob Clarkf7eff602012-09-05 21:48:38 +0000491 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000492 kref_put(&fb->refcount, drm_framebuffer_free);
493}
494EXPORT_SYMBOL(drm_framebuffer_unreference);
495
496/**
497 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100498 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000499 */
500void drm_framebuffer_reference(struct drm_framebuffer *fb)
501{
502 DRM_DEBUG("FB ID: %d\n", fb->base.id);
503 kref_get(&fb->refcount);
504}
505EXPORT_SYMBOL(drm_framebuffer_reference);
506
Daniel Vetter2b677e82012-12-10 21:16:05 +0100507static void drm_framebuffer_free_bug(struct kref *kref)
508{
509 BUG();
510}
511
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100512static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
513{
514 DRM_DEBUG("FB ID: %d\n", fb->base.id);
515 kref_put(&fb->refcount, drm_framebuffer_free_bug);
516}
517
Daniel Vetter2b677e82012-12-10 21:16:05 +0100518/* dev->mode_config.fb_lock must be held! */
519static void __drm_framebuffer_unregister(struct drm_device *dev,
520 struct drm_framebuffer *fb)
521{
522 mutex_lock(&dev->mode_config.idr_mutex);
523 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
524 mutex_unlock(&dev->mode_config.idr_mutex);
525
526 fb->base.id = 0;
527
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100528 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100529}
530
Dave Airlief453ba02008-11-07 14:05:41 -0800531/**
Daniel Vetter36206362012-12-10 20:42:17 +0100532 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
533 * @fb: fb to unregister
534 *
535 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
536 * those used for fbdev. Note that the caller must hold a reference of it's own,
537 * i.e. the object may not be destroyed through this call (since it'll lead to a
538 * locking inversion).
539 */
540void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
541{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100542 struct drm_device *dev = fb->dev;
543
544 mutex_lock(&dev->mode_config.fb_lock);
545 /* Mark fb as reaped and drop idr ref. */
546 __drm_framebuffer_unregister(dev, fb);
547 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100548}
549EXPORT_SYMBOL(drm_framebuffer_unregister_private);
550
551/**
Dave Airlief453ba02008-11-07 14:05:41 -0800552 * drm_framebuffer_cleanup - remove a framebuffer object
553 * @fb: framebuffer to remove
554 *
Daniel Vetter36206362012-12-10 20:42:17 +0100555 * Cleanup references to a user-created framebuffer. This function is intended
556 * to be used from the drivers ->destroy callback.
557 *
558 * Note that this function does not remove the fb from active usuage - if it is
559 * still used anywhere, hilarity can ensue since userspace could call getfb on
560 * the id and get back -EINVAL. Obviously no concern at driver unload time.
561 *
562 * Also, the framebuffer will not be removed from the lookup idr - for
563 * user-created framebuffers this will happen in in the rmfb ioctl. For
564 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
565 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800566 */
567void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
568{
569 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100570
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100571 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000572 list_del(&fb->head);
573 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100574 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000575}
576EXPORT_SYMBOL(drm_framebuffer_cleanup);
577
578/**
579 * drm_framebuffer_remove - remove and unreference a framebuffer object
580 * @fb: framebuffer to remove
581 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000582 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100583 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100584 * passed-in framebuffer. Might take the modeset locks.
585 *
586 * Note that this function optimizes the cleanup away if the caller holds the
587 * last reference to the framebuffer. It is also guaranteed to not take the
588 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000589 */
590void drm_framebuffer_remove(struct drm_framebuffer *fb)
591{
592 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800593 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800594 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000595 struct drm_mode_set set;
596 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800597
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100598 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100599
Daniel Vetterb62584e2012-12-11 16:51:35 +0100600 /*
601 * drm ABI mandates that we remove any deleted framebuffers from active
602 * useage. But since most sane clients only remove framebuffers they no
603 * longer need, try to optimize this away.
604 *
605 * Since we're holding a reference ourselves, observing a refcount of 1
606 * means that we're the last holder and can skip it. Also, the refcount
607 * can never increase from 1 again, so we don't need any barriers or
608 * locks.
609 *
610 * Note that userspace could try to race with use and instate a new
611 * usage _after_ we've cleared all current ones. End result will be an
612 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
613 * in this manner.
614 */
615 if (atomic_read(&fb->refcount.refcount) > 1) {
616 drm_modeset_lock_all(dev);
617 /* remove from any CRTC */
618 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
619 if (crtc->fb == fb) {
620 /* should turn off the crtc */
621 memset(&set, 0, sizeof(struct drm_mode_set));
622 set.crtc = crtc;
623 set.fb = NULL;
624 ret = drm_mode_set_config_internal(&set);
625 if (ret)
626 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
627 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000628 }
Dave Airlief453ba02008-11-07 14:05:41 -0800629
Daniel Vetterb62584e2012-12-11 16:51:35 +0100630 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300631 if (plane->fb == fb)
632 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800633 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100634 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800635 }
636
Rob Clarkf7eff602012-09-05 21:48:38 +0000637 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800638}
Rob Clarkf7eff602012-09-05 21:48:38 +0000639EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800640
641/**
642 * drm_crtc_init - Initialise a new CRTC object
643 * @dev: DRM device
644 * @crtc: CRTC object to init
645 * @funcs: callbacks for the new CRTC
646 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000647 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200648 *
649 * RETURNS:
650 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800651 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200652int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800653 const struct drm_crtc_funcs *funcs)
654{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200655 int ret;
656
Dave Airlief453ba02008-11-07 14:05:41 -0800657 crtc->dev = dev;
658 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000659 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800660
Daniel Vetter84849902012-12-02 00:28:11 +0100661 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100662 mutex_init(&crtc->mutex);
663 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200664
665 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
666 if (ret)
667 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800668
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300669 crtc->base.properties = &crtc->properties;
670
Dave Airlief453ba02008-11-07 14:05:41 -0800671 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
672 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200673
674 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100675 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200676
677 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800678}
679EXPORT_SYMBOL(drm_crtc_init);
680
681/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000682 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800683 * @crtc: CRTC to cleanup
684 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000685 * This function cleans up @crtc and removes it from the DRM mode setting
686 * core. Note that the function does *not* free the crtc structure itself,
687 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800688 */
689void drm_crtc_cleanup(struct drm_crtc *crtc)
690{
691 struct drm_device *dev = crtc->dev;
692
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000693 kfree(crtc->gamma_store);
694 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800695
696 drm_mode_object_put(dev, &crtc->base);
697 list_del(&crtc->head);
698 dev->mode_config.num_crtc--;
699}
700EXPORT_SYMBOL(drm_crtc_cleanup);
701
702/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000703 * drm_crtc_index - find the index of a registered CRTC
704 * @crtc: CRTC to find index for
705 *
706 * Given a registered CRTC, return the index of that CRTC within a DRM
707 * device's list of CRTCs.
708 */
709unsigned int drm_crtc_index(struct drm_crtc *crtc)
710{
711 unsigned int index = 0;
712 struct drm_crtc *tmp;
713
714 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
715 if (tmp == crtc)
716 return index;
717
718 index++;
719 }
720
721 BUG();
722}
723EXPORT_SYMBOL(drm_crtc_index);
724
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100725/*
Dave Airlief453ba02008-11-07 14:05:41 -0800726 * drm_mode_remove - remove and free a mode
727 * @connector: connector list to modify
728 * @mode: mode to remove
729 *
Dave Airlief453ba02008-11-07 14:05:41 -0800730 * Remove @mode from @connector's mode list, then free it.
731 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100732static void drm_mode_remove(struct drm_connector *connector,
733 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800734{
735 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100736 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800737}
Dave Airlief453ba02008-11-07 14:05:41 -0800738
739/**
740 * drm_connector_init - Init a preallocated connector
741 * @dev: DRM device
742 * @connector: the connector to init
743 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100744 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800745 *
Dave Airlief453ba02008-11-07 14:05:41 -0800746 * Initialises a preallocated connector. Connectors should be
747 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200748 *
749 * RETURNS:
750 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800751 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200752int drm_connector_init(struct drm_device *dev,
753 struct drm_connector *connector,
754 const struct drm_connector_funcs *funcs,
755 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800756{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200757 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400758 struct ida *connector_ida =
759 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200760
Daniel Vetter84849902012-12-02 00:28:11 +0100761 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800762
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200763 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
764 if (ret)
765 goto out;
766
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300767 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800768 connector->dev = dev;
769 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800770 connector->connector_type = connector_type;
771 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400772 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
773 if (connector->connector_type_id < 0) {
774 ret = connector->connector_type_id;
775 drm_mode_object_put(dev, &connector->base);
776 goto out;
777 }
Dave Airlief453ba02008-11-07 14:05:41 -0800778 INIT_LIST_HEAD(&connector->probed_modes);
779 INIT_LIST_HEAD(&connector->modes);
780 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000781 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800782
783 list_add_tail(&connector->head, &dev->mode_config.connector_list);
784 dev->mode_config.num_connector++;
785
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200786 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500787 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200788 dev->mode_config.edid_property,
789 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800790
Rob Clark58495562012-10-11 20:50:56 -0500791 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800792 dev->mode_config.dpms_property, 0);
793
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200794 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100795 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200796
797 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800798}
799EXPORT_SYMBOL(drm_connector_init);
800
801/**
802 * drm_connector_cleanup - cleans up an initialised connector
803 * @connector: connector to cleanup
804 *
Dave Airlief453ba02008-11-07 14:05:41 -0800805 * Cleans up the connector but doesn't free the object.
806 */
807void drm_connector_cleanup(struct drm_connector *connector)
808{
809 struct drm_device *dev = connector->dev;
810 struct drm_display_mode *mode, *t;
811
812 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
813 drm_mode_remove(connector, mode);
814
815 list_for_each_entry_safe(mode, t, &connector->modes, head)
816 drm_mode_remove(connector, mode);
817
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400818 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
819 connector->connector_type_id);
820
Dave Airlief453ba02008-11-07 14:05:41 -0800821 drm_mode_object_put(dev, &connector->base);
822 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000823 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800824}
825EXPORT_SYMBOL(drm_connector_cleanup);
826
Dave Airliecbc7e222012-02-20 14:16:40 +0000827void drm_connector_unplug_all(struct drm_device *dev)
828{
829 struct drm_connector *connector;
830
831 /* taking the mode config mutex ends up in a clash with sysfs */
832 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
833 drm_sysfs_connector_remove(connector);
834
835}
836EXPORT_SYMBOL(drm_connector_unplug_all);
837
Sean Paul3b336ec2013-08-14 16:47:37 -0400838int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
839 const struct drm_bridge_funcs *funcs)
840{
841 int ret;
842
843 drm_modeset_lock_all(dev);
844
845 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
846 if (ret)
847 goto out;
848
849 bridge->dev = dev;
850 bridge->funcs = funcs;
851
852 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
853 dev->mode_config.num_bridge++;
854
855 out:
856 drm_modeset_unlock_all(dev);
857 return ret;
858}
859EXPORT_SYMBOL(drm_bridge_init);
860
861void drm_bridge_cleanup(struct drm_bridge *bridge)
862{
863 struct drm_device *dev = bridge->dev;
864
865 drm_modeset_lock_all(dev);
866 drm_mode_object_put(dev, &bridge->base);
867 list_del(&bridge->head);
868 dev->mode_config.num_bridge--;
869 drm_modeset_unlock_all(dev);
870}
871EXPORT_SYMBOL(drm_bridge_cleanup);
872
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200873int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000874 struct drm_encoder *encoder,
875 const struct drm_encoder_funcs *funcs,
876 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800877{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200878 int ret;
879
Daniel Vetter84849902012-12-02 00:28:11 +0100880 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800881
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200882 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
883 if (ret)
884 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800885
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200886 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800887 encoder->encoder_type = encoder_type;
888 encoder->funcs = funcs;
889
890 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
891 dev->mode_config.num_encoder++;
892
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200893 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100894 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200895
896 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800897}
898EXPORT_SYMBOL(drm_encoder_init);
899
900void drm_encoder_cleanup(struct drm_encoder *encoder)
901{
902 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100903 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800904 drm_mode_object_put(dev, &encoder->base);
905 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000906 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100907 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800908}
909EXPORT_SYMBOL(drm_encoder_cleanup);
910
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300911/**
912 * drm_plane_init - Initialise a new plane object
913 * @dev: DRM device
914 * @plane: plane object to init
915 * @possible_crtcs: bitmask of possible CRTCs
916 * @funcs: callbacks for the new plane
917 * @formats: array of supported formats (%DRM_FORMAT_*)
918 * @format_count: number of elements in @formats
919 * @priv: plane is private (hidden from userspace)?
920 *
921 * Inits a new object created as base part of a driver plane object.
922 *
923 * RETURNS:
924 * Zero on success, error code on failure.
925 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800926int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
927 unsigned long possible_crtcs,
928 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600929 const uint32_t *formats, uint32_t format_count,
930 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800931{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200932 int ret;
933
Daniel Vetter84849902012-12-02 00:28:11 +0100934 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800935
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200936 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
937 if (ret)
938 goto out;
939
Rob Clark4d939142012-05-17 02:23:27 -0600940 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800941 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800942 plane->funcs = funcs;
943 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
944 GFP_KERNEL);
945 if (!plane->format_types) {
946 DRM_DEBUG_KMS("out of memory when allocating plane\n");
947 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200948 ret = -ENOMEM;
949 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800950 }
951
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800952 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800953 plane->format_count = format_count;
954 plane->possible_crtcs = possible_crtcs;
955
Rob Clark0a7eb242011-12-13 20:19:36 -0600956 /* private planes are not exposed to userspace, but depending on
957 * display hardware, might be convenient to allow sharing programming
958 * for the scanout engine with the crtc implementation.
959 */
960 if (!priv) {
961 list_add_tail(&plane->head, &dev->mode_config.plane_list);
962 dev->mode_config.num_plane++;
963 } else {
964 INIT_LIST_HEAD(&plane->head);
965 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800966
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200967 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100968 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800969
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200970 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800971}
972EXPORT_SYMBOL(drm_plane_init);
973
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300974/**
975 * drm_plane_cleanup - Clean up the core plane usage
976 * @plane: plane to cleanup
977 *
978 * This function cleans up @plane and removes it from the DRM mode setting
979 * core. Note that the function does *not* free the plane structure itself,
980 * this is the responsibility of the caller.
981 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800982void drm_plane_cleanup(struct drm_plane *plane)
983{
984 struct drm_device *dev = plane->dev;
985
Daniel Vetter84849902012-12-02 00:28:11 +0100986 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800987 kfree(plane->format_types);
988 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600989 /* if not added to a list, it must be a private plane */
990 if (!list_empty(&plane->head)) {
991 list_del(&plane->head);
992 dev->mode_config.num_plane--;
993 }
Daniel Vetter84849902012-12-02 00:28:11 +0100994 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800995}
996EXPORT_SYMBOL(drm_plane_cleanup);
997
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300998/**
999 * drm_plane_force_disable - Forcibly disable a plane
1000 * @plane: plane to disable
1001 *
1002 * Forces the plane to be disabled.
1003 *
1004 * Used when the plane's current framebuffer is destroyed,
1005 * and when restoring fbdev mode.
1006 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001007void drm_plane_force_disable(struct drm_plane *plane)
1008{
1009 int ret;
1010
1011 if (!plane->fb)
1012 return;
1013
1014 ret = plane->funcs->disable_plane(plane);
1015 if (ret)
1016 DRM_ERROR("failed to disable plane with busy fb\n");
1017 /* disconnect the plane from the fb and crtc: */
1018 __drm_framebuffer_unreference(plane->fb);
1019 plane->fb = NULL;
1020 plane->crtc = NULL;
1021}
1022EXPORT_SYMBOL(drm_plane_force_disable);
1023
Dave Airlief453ba02008-11-07 14:05:41 -08001024static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1025{
1026 struct drm_property *edid;
1027 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001028
1029 /*
1030 * Standard properties (apply to all connectors)
1031 */
1032 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1033 DRM_MODE_PROP_IMMUTABLE,
1034 "EDID", 0);
1035 dev->mode_config.edid_property = edid;
1036
Sascha Hauer4a67d392012-02-06 10:58:17 +01001037 dpms = drm_property_create_enum(dev, 0,
1038 "DPMS", drm_dpms_enum_list,
1039 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001040 dev->mode_config.dpms_property = dpms;
1041
1042 return 0;
1043}
1044
1045/**
1046 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1047 * @dev: DRM device
1048 *
1049 * Called by a driver the first time a DVI-I connector is made.
1050 */
1051int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1052{
1053 struct drm_property *dvi_i_selector;
1054 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001055
1056 if (dev->mode_config.dvi_i_select_subconnector_property)
1057 return 0;
1058
1059 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001060 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001061 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001062 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001063 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001064 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1065
Sascha Hauer4a67d392012-02-06 10:58:17 +01001066 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001067 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001068 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001069 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001070 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1071
1072 return 0;
1073}
1074EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1075
1076/**
1077 * drm_create_tv_properties - create TV specific connector properties
1078 * @dev: DRM device
1079 * @num_modes: number of different TV formats (modes) supported
1080 * @modes: array of pointers to strings containing name of each format
1081 *
1082 * Called by a driver's TV initialization routine, this function creates
1083 * the TV specific connector properties for a given device. Caller is
1084 * responsible for allocating a list of format names and passing them to
1085 * this routine.
1086 */
1087int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1088 char *modes[])
1089{
1090 struct drm_property *tv_selector;
1091 struct drm_property *tv_subconnector;
1092 int i;
1093
1094 if (dev->mode_config.tv_select_subconnector_property)
1095 return 0;
1096
1097 /*
1098 * Basic connector properties
1099 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001100 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001101 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001102 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001103 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001104 dev->mode_config.tv_select_subconnector_property = tv_selector;
1105
1106 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001107 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1108 "subconnector",
1109 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001110 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001111 dev->mode_config.tv_subconnector_property = tv_subconnector;
1112
1113 /*
1114 * Other, TV specific properties: margins & TV modes.
1115 */
1116 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001117 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001118
1119 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001120 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001121
1122 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001123 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001124
1125 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001126 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001127
1128 dev->mode_config.tv_mode_property =
1129 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1130 "mode", num_modes);
1131 for (i = 0; i < num_modes; i++)
1132 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1133 i, modes[i]);
1134
Francisco Jerezb6b79022009-08-02 04:19:20 +02001135 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001136 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001137
1138 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001139 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001140
1141 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001142 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001143
Francisco Jereza75f0232009-08-12 02:30:10 +02001144 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001145 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001146
1147 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001148 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001149
1150 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001151 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001152
Dave Airlief453ba02008-11-07 14:05:41 -08001153 return 0;
1154}
1155EXPORT_SYMBOL(drm_mode_create_tv_properties);
1156
1157/**
1158 * drm_mode_create_scaling_mode_property - create scaling mode property
1159 * @dev: DRM device
1160 *
1161 * Called by a driver the first time it's needed, must be attached to desired
1162 * connectors.
1163 */
1164int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1165{
1166 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001167
1168 if (dev->mode_config.scaling_mode_property)
1169 return 0;
1170
1171 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001172 drm_property_create_enum(dev, 0, "scaling mode",
1173 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001174 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001175
1176 dev->mode_config.scaling_mode_property = scaling_mode;
1177
1178 return 0;
1179}
1180EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1181
1182/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001183 * drm_mode_create_dirty_property - create dirty property
1184 * @dev: DRM device
1185 *
1186 * Called by a driver the first time it's needed, must be attached to desired
1187 * connectors.
1188 */
1189int drm_mode_create_dirty_info_property(struct drm_device *dev)
1190{
1191 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001192
1193 if (dev->mode_config.dirty_info_property)
1194 return 0;
1195
1196 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001197 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001198 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001199 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001200 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001201 dev->mode_config.dirty_info_property = dirty_info;
1202
1203 return 0;
1204}
1205EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1206
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001207static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001208{
1209 uint32_t total_objects = 0;
1210
1211 total_objects += dev->mode_config.num_crtc;
1212 total_objects += dev->mode_config.num_connector;
1213 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001214 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001215
Dave Airlief453ba02008-11-07 14:05:41 -08001216 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1217 if (!group->id_list)
1218 return -ENOMEM;
1219
1220 group->num_crtcs = 0;
1221 group->num_connectors = 0;
1222 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001223 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001224 return 0;
1225}
1226
1227int drm_mode_group_init_legacy_group(struct drm_device *dev,
1228 struct drm_mode_group *group)
1229{
1230 struct drm_crtc *crtc;
1231 struct drm_encoder *encoder;
1232 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001233 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001234 int ret;
1235
1236 if ((ret = drm_mode_group_init(dev, group)))
1237 return ret;
1238
1239 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1240 group->id_list[group->num_crtcs++] = crtc->base.id;
1241
1242 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1243 group->id_list[group->num_crtcs + group->num_encoders++] =
1244 encoder->base.id;
1245
1246 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1247 group->id_list[group->num_crtcs + group->num_encoders +
1248 group->num_connectors++] = connector->base.id;
1249
Sean Paul3b336ec2013-08-14 16:47:37 -04001250 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1251 group->id_list[group->num_crtcs + group->num_encoders +
1252 group->num_connectors + group->num_bridges++] =
1253 bridge->base.id;
1254
Dave Airlief453ba02008-11-07 14:05:41 -08001255 return 0;
1256}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001257EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001258
1259/**
Dave Airlief453ba02008-11-07 14:05:41 -08001260 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1261 * @out: drm_mode_modeinfo struct to return to the user
1262 * @in: drm_display_mode to use
1263 *
Dave Airlief453ba02008-11-07 14:05:41 -08001264 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1265 * the user.
1266 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001267static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1268 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001269{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001270 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1271 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1272 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1273 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1274 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1275 "timing values too large for mode info\n");
1276
Dave Airlief453ba02008-11-07 14:05:41 -08001277 out->clock = in->clock;
1278 out->hdisplay = in->hdisplay;
1279 out->hsync_start = in->hsync_start;
1280 out->hsync_end = in->hsync_end;
1281 out->htotal = in->htotal;
1282 out->hskew = in->hskew;
1283 out->vdisplay = in->vdisplay;
1284 out->vsync_start = in->vsync_start;
1285 out->vsync_end = in->vsync_end;
1286 out->vtotal = in->vtotal;
1287 out->vscan = in->vscan;
1288 out->vrefresh = in->vrefresh;
1289 out->flags = in->flags;
1290 out->type = in->type;
1291 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1292 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1293}
1294
1295/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001296 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001297 * @out: drm_display_mode to return to the user
1298 * @in: drm_mode_modeinfo to use
1299 *
Dave Airlief453ba02008-11-07 14:05:41 -08001300 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1301 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001302 *
1303 * RETURNS:
1304 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001305 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001306static int drm_crtc_convert_umode(struct drm_display_mode *out,
1307 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001308{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001309 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1310 return -ERANGE;
1311
Damien Lespiau5848ad42013-09-27 12:11:50 +01001312 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1313 return -EINVAL;
1314
Dave Airlief453ba02008-11-07 14:05:41 -08001315 out->clock = in->clock;
1316 out->hdisplay = in->hdisplay;
1317 out->hsync_start = in->hsync_start;
1318 out->hsync_end = in->hsync_end;
1319 out->htotal = in->htotal;
1320 out->hskew = in->hskew;
1321 out->vdisplay = in->vdisplay;
1322 out->vsync_start = in->vsync_start;
1323 out->vsync_end = in->vsync_end;
1324 out->vtotal = in->vtotal;
1325 out->vscan = in->vscan;
1326 out->vrefresh = in->vrefresh;
1327 out->flags = in->flags;
1328 out->type = in->type;
1329 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1330 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001331
1332 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001333}
1334
1335/**
1336 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001337 * @dev: drm device for the ioctl
1338 * @data: data pointer for the ioctl
1339 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001340 *
Dave Airlief453ba02008-11-07 14:05:41 -08001341 * Construct a set of configuration description structures and return
1342 * them to the user, including CRTC, connector and framebuffer configuration.
1343 *
1344 * Called by the user via ioctl.
1345 *
1346 * RETURNS:
1347 * Zero on success, errno on failure.
1348 */
1349int drm_mode_getresources(struct drm_device *dev, void *data,
1350 struct drm_file *file_priv)
1351{
1352 struct drm_mode_card_res *card_res = data;
1353 struct list_head *lh;
1354 struct drm_framebuffer *fb;
1355 struct drm_connector *connector;
1356 struct drm_crtc *crtc;
1357 struct drm_encoder *encoder;
1358 int ret = 0;
1359 int connector_count = 0;
1360 int crtc_count = 0;
1361 int fb_count = 0;
1362 int encoder_count = 0;
1363 int copied = 0, i;
1364 uint32_t __user *fb_id;
1365 uint32_t __user *crtc_id;
1366 uint32_t __user *connector_id;
1367 uint32_t __user *encoder_id;
1368 struct drm_mode_group *mode_group;
1369
Dave Airliefb3b06c2011-02-08 13:55:21 +10001370 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1371 return -EINVAL;
1372
Dave Airlief453ba02008-11-07 14:05:41 -08001373
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001374 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001375 /*
1376 * For the non-control nodes we need to limit the list of resources
1377 * by IDs in the group list for this node
1378 */
1379 list_for_each(lh, &file_priv->fbs)
1380 fb_count++;
1381
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001382 /* handle this in 4 parts */
1383 /* FBs */
1384 if (card_res->count_fbs >= fb_count) {
1385 copied = 0;
1386 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1387 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1388 if (put_user(fb->base.id, fb_id + copied)) {
1389 mutex_unlock(&file_priv->fbs_lock);
1390 return -EFAULT;
1391 }
1392 copied++;
1393 }
1394 }
1395 card_res->count_fbs = fb_count;
1396 mutex_unlock(&file_priv->fbs_lock);
1397
1398 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001399 mode_group = &file_priv->master->minor->mode_group;
1400 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1401
1402 list_for_each(lh, &dev->mode_config.crtc_list)
1403 crtc_count++;
1404
1405 list_for_each(lh, &dev->mode_config.connector_list)
1406 connector_count++;
1407
1408 list_for_each(lh, &dev->mode_config.encoder_list)
1409 encoder_count++;
1410 } else {
1411
1412 crtc_count = mode_group->num_crtcs;
1413 connector_count = mode_group->num_connectors;
1414 encoder_count = mode_group->num_encoders;
1415 }
1416
1417 card_res->max_height = dev->mode_config.max_height;
1418 card_res->min_height = dev->mode_config.min_height;
1419 card_res->max_width = dev->mode_config.max_width;
1420 card_res->min_width = dev->mode_config.min_width;
1421
Dave Airlief453ba02008-11-07 14:05:41 -08001422 /* CRTCs */
1423 if (card_res->count_crtcs >= crtc_count) {
1424 copied = 0;
1425 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1426 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1427 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1428 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001429 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001430 if (put_user(crtc->base.id, crtc_id + copied)) {
1431 ret = -EFAULT;
1432 goto out;
1433 }
1434 copied++;
1435 }
1436 } else {
1437 for (i = 0; i < mode_group->num_crtcs; i++) {
1438 if (put_user(mode_group->id_list[i],
1439 crtc_id + copied)) {
1440 ret = -EFAULT;
1441 goto out;
1442 }
1443 copied++;
1444 }
1445 }
1446 }
1447 card_res->count_crtcs = crtc_count;
1448
1449 /* Encoders */
1450 if (card_res->count_encoders >= encoder_count) {
1451 copied = 0;
1452 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1453 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1454 list_for_each_entry(encoder,
1455 &dev->mode_config.encoder_list,
1456 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001457 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1458 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001459 if (put_user(encoder->base.id, encoder_id +
1460 copied)) {
1461 ret = -EFAULT;
1462 goto out;
1463 }
1464 copied++;
1465 }
1466 } else {
1467 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1468 if (put_user(mode_group->id_list[i],
1469 encoder_id + copied)) {
1470 ret = -EFAULT;
1471 goto out;
1472 }
1473 copied++;
1474 }
1475
1476 }
1477 }
1478 card_res->count_encoders = encoder_count;
1479
1480 /* Connectors */
1481 if (card_res->count_connectors >= connector_count) {
1482 copied = 0;
1483 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1484 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1485 list_for_each_entry(connector,
1486 &dev->mode_config.connector_list,
1487 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001488 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1489 connector->base.id,
1490 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001491 if (put_user(connector->base.id,
1492 connector_id + copied)) {
1493 ret = -EFAULT;
1494 goto out;
1495 }
1496 copied++;
1497 }
1498 } else {
1499 int start = mode_group->num_crtcs +
1500 mode_group->num_encoders;
1501 for (i = start; i < start + mode_group->num_connectors; i++) {
1502 if (put_user(mode_group->id_list[i],
1503 connector_id + copied)) {
1504 ret = -EFAULT;
1505 goto out;
1506 }
1507 copied++;
1508 }
1509 }
1510 }
1511 card_res->count_connectors = connector_count;
1512
Jerome Glisse94401062010-07-15 15:43:25 -04001513 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001514 card_res->count_connectors, card_res->count_encoders);
1515
1516out:
Daniel Vetter84849902012-12-02 00:28:11 +01001517 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001518 return ret;
1519}
1520
1521/**
1522 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001523 * @dev: drm device for the ioctl
1524 * @data: data pointer for the ioctl
1525 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001526 *
Dave Airlief453ba02008-11-07 14:05:41 -08001527 * Construct a CRTC configuration structure to return to the user.
1528 *
1529 * Called by the user via ioctl.
1530 *
1531 * RETURNS:
1532 * Zero on success, errno on failure.
1533 */
1534int drm_mode_getcrtc(struct drm_device *dev,
1535 void *data, struct drm_file *file_priv)
1536{
1537 struct drm_mode_crtc *crtc_resp = data;
1538 struct drm_crtc *crtc;
1539 struct drm_mode_object *obj;
1540 int ret = 0;
1541
Dave Airliefb3b06c2011-02-08 13:55:21 +10001542 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1543 return -EINVAL;
1544
Daniel Vetter84849902012-12-02 00:28:11 +01001545 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001546
1547 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1548 DRM_MODE_OBJECT_CRTC);
1549 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001550 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001551 goto out;
1552 }
1553 crtc = obj_to_crtc(obj);
1554
1555 crtc_resp->x = crtc->x;
1556 crtc_resp->y = crtc->y;
1557 crtc_resp->gamma_size = crtc->gamma_size;
1558 if (crtc->fb)
1559 crtc_resp->fb_id = crtc->fb->base.id;
1560 else
1561 crtc_resp->fb_id = 0;
1562
1563 if (crtc->enabled) {
1564
1565 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1566 crtc_resp->mode_valid = 1;
1567
1568 } else {
1569 crtc_resp->mode_valid = 0;
1570 }
1571
1572out:
Daniel Vetter84849902012-12-02 00:28:11 +01001573 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001574 return ret;
1575}
1576
Damien Lespiau61d8e322013-09-25 16:45:22 +01001577static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1578 const struct drm_file *file_priv)
1579{
1580 /*
1581 * If user-space hasn't configured the driver to expose the stereo 3D
1582 * modes, don't expose them.
1583 */
1584 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1585 return false;
1586
1587 return true;
1588}
1589
Dave Airlief453ba02008-11-07 14:05:41 -08001590/**
1591 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001592 * @dev: drm device for the ioctl
1593 * @data: data pointer for the ioctl
1594 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001595 *
Dave Airlief453ba02008-11-07 14:05:41 -08001596 * Construct a connector configuration structure to return to the user.
1597 *
1598 * Called by the user via ioctl.
1599 *
1600 * RETURNS:
1601 * Zero on success, errno on failure.
1602 */
1603int drm_mode_getconnector(struct drm_device *dev, void *data,
1604 struct drm_file *file_priv)
1605{
1606 struct drm_mode_get_connector *out_resp = data;
1607 struct drm_mode_object *obj;
1608 struct drm_connector *connector;
1609 struct drm_display_mode *mode;
1610 int mode_count = 0;
1611 int props_count = 0;
1612 int encoders_count = 0;
1613 int ret = 0;
1614 int copied = 0;
1615 int i;
1616 struct drm_mode_modeinfo u_mode;
1617 struct drm_mode_modeinfo __user *mode_ptr;
1618 uint32_t __user *prop_ptr;
1619 uint64_t __user *prop_values;
1620 uint32_t __user *encoder_ptr;
1621
Dave Airliefb3b06c2011-02-08 13:55:21 +10001622 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1623 return -EINVAL;
1624
Dave Airlief453ba02008-11-07 14:05:41 -08001625 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1626
Jerome Glisse94401062010-07-15 15:43:25 -04001627 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001628
Daniel Vetter7b240562012-12-12 00:35:33 +01001629 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001630
1631 obj = drm_mode_object_find(dev, out_resp->connector_id,
1632 DRM_MODE_OBJECT_CONNECTOR);
1633 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001634 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001635 goto out;
1636 }
1637 connector = obj_to_connector(obj);
1638
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001639 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001640
1641 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1642 if (connector->encoder_ids[i] != 0) {
1643 encoders_count++;
1644 }
1645 }
1646
1647 if (out_resp->count_modes == 0) {
1648 connector->funcs->fill_modes(connector,
1649 dev->mode_config.max_width,
1650 dev->mode_config.max_height);
1651 }
1652
1653 /* delayed so we get modes regardless of pre-fill_modes state */
1654 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001655 if (drm_mode_expose_to_userspace(mode, file_priv))
1656 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001657
1658 out_resp->connector_id = connector->base.id;
1659 out_resp->connector_type = connector->connector_type;
1660 out_resp->connector_type_id = connector->connector_type_id;
1661 out_resp->mm_width = connector->display_info.width_mm;
1662 out_resp->mm_height = connector->display_info.height_mm;
1663 out_resp->subpixel = connector->display_info.subpixel_order;
1664 out_resp->connection = connector->status;
1665 if (connector->encoder)
1666 out_resp->encoder_id = connector->encoder->base.id;
1667 else
1668 out_resp->encoder_id = 0;
1669
1670 /*
1671 * This ioctl is called twice, once to determine how much space is
1672 * needed, and the 2nd time to fill it.
1673 */
1674 if ((out_resp->count_modes >= mode_count) && mode_count) {
1675 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001676 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001677 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001678 if (!drm_mode_expose_to_userspace(mode, file_priv))
1679 continue;
1680
Dave Airlief453ba02008-11-07 14:05:41 -08001681 drm_crtc_convert_to_umode(&u_mode, mode);
1682 if (copy_to_user(mode_ptr + copied,
1683 &u_mode, sizeof(u_mode))) {
1684 ret = -EFAULT;
1685 goto out;
1686 }
1687 copied++;
1688 }
1689 }
1690 out_resp->count_modes = mode_count;
1691
1692 if ((out_resp->count_props >= props_count) && props_count) {
1693 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001694 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1695 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001696 for (i = 0; i < connector->properties.count; i++) {
1697 if (put_user(connector->properties.ids[i],
1698 prop_ptr + copied)) {
1699 ret = -EFAULT;
1700 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001701 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001702
1703 if (put_user(connector->properties.values[i],
1704 prop_values + copied)) {
1705 ret = -EFAULT;
1706 goto out;
1707 }
1708 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001709 }
1710 }
1711 out_resp->count_props = props_count;
1712
1713 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1714 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001715 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001716 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1717 if (connector->encoder_ids[i] != 0) {
1718 if (put_user(connector->encoder_ids[i],
1719 encoder_ptr + copied)) {
1720 ret = -EFAULT;
1721 goto out;
1722 }
1723 copied++;
1724 }
1725 }
1726 }
1727 out_resp->count_encoders = encoders_count;
1728
1729out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001730 mutex_unlock(&dev->mode_config.mutex);
1731
Dave Airlief453ba02008-11-07 14:05:41 -08001732 return ret;
1733}
1734
1735int drm_mode_getencoder(struct drm_device *dev, void *data,
1736 struct drm_file *file_priv)
1737{
1738 struct drm_mode_get_encoder *enc_resp = data;
1739 struct drm_mode_object *obj;
1740 struct drm_encoder *encoder;
1741 int ret = 0;
1742
Dave Airliefb3b06c2011-02-08 13:55:21 +10001743 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1744 return -EINVAL;
1745
Daniel Vetter84849902012-12-02 00:28:11 +01001746 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001747 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1748 DRM_MODE_OBJECT_ENCODER);
1749 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001750 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001751 goto out;
1752 }
1753 encoder = obj_to_encoder(obj);
1754
1755 if (encoder->crtc)
1756 enc_resp->crtc_id = encoder->crtc->base.id;
1757 else
1758 enc_resp->crtc_id = 0;
1759 enc_resp->encoder_type = encoder->encoder_type;
1760 enc_resp->encoder_id = encoder->base.id;
1761 enc_resp->possible_crtcs = encoder->possible_crtcs;
1762 enc_resp->possible_clones = encoder->possible_clones;
1763
1764out:
Daniel Vetter84849902012-12-02 00:28:11 +01001765 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001766 return ret;
1767}
1768
1769/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001770 * drm_mode_getplane_res - get plane info
1771 * @dev: DRM device
1772 * @data: ioctl data
1773 * @file_priv: DRM file info
1774 *
1775 * Return an plane count and set of IDs.
1776 */
1777int drm_mode_getplane_res(struct drm_device *dev, void *data,
1778 struct drm_file *file_priv)
1779{
1780 struct drm_mode_get_plane_res *plane_resp = data;
1781 struct drm_mode_config *config;
1782 struct drm_plane *plane;
1783 uint32_t __user *plane_ptr;
1784 int copied = 0, ret = 0;
1785
1786 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1787 return -EINVAL;
1788
Daniel Vetter84849902012-12-02 00:28:11 +01001789 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001790 config = &dev->mode_config;
1791
1792 /*
1793 * This ioctl is called twice, once to determine how much space is
1794 * needed, and the 2nd time to fill it.
1795 */
1796 if (config->num_plane &&
1797 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001798 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001799
1800 list_for_each_entry(plane, &config->plane_list, head) {
1801 if (put_user(plane->base.id, plane_ptr + copied)) {
1802 ret = -EFAULT;
1803 goto out;
1804 }
1805 copied++;
1806 }
1807 }
1808 plane_resp->count_planes = config->num_plane;
1809
1810out:
Daniel Vetter84849902012-12-02 00:28:11 +01001811 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001812 return ret;
1813}
1814
1815/**
1816 * drm_mode_getplane - get plane info
1817 * @dev: DRM device
1818 * @data: ioctl data
1819 * @file_priv: DRM file info
1820 *
1821 * Return plane info, including formats supported, gamma size, any
1822 * current fb, etc.
1823 */
1824int drm_mode_getplane(struct drm_device *dev, void *data,
1825 struct drm_file *file_priv)
1826{
1827 struct drm_mode_get_plane *plane_resp = data;
1828 struct drm_mode_object *obj;
1829 struct drm_plane *plane;
1830 uint32_t __user *format_ptr;
1831 int ret = 0;
1832
1833 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1834 return -EINVAL;
1835
Daniel Vetter84849902012-12-02 00:28:11 +01001836 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001837 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1838 DRM_MODE_OBJECT_PLANE);
1839 if (!obj) {
1840 ret = -ENOENT;
1841 goto out;
1842 }
1843 plane = obj_to_plane(obj);
1844
1845 if (plane->crtc)
1846 plane_resp->crtc_id = plane->crtc->base.id;
1847 else
1848 plane_resp->crtc_id = 0;
1849
1850 if (plane->fb)
1851 plane_resp->fb_id = plane->fb->base.id;
1852 else
1853 plane_resp->fb_id = 0;
1854
1855 plane_resp->plane_id = plane->base.id;
1856 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001857 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001858
1859 /*
1860 * This ioctl is called twice, once to determine how much space is
1861 * needed, and the 2nd time to fill it.
1862 */
1863 if (plane->format_count &&
1864 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001865 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001866 if (copy_to_user(format_ptr,
1867 plane->format_types,
1868 sizeof(uint32_t) * plane->format_count)) {
1869 ret = -EFAULT;
1870 goto out;
1871 }
1872 }
1873 plane_resp->count_format_types = plane->format_count;
1874
1875out:
Daniel Vetter84849902012-12-02 00:28:11 +01001876 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001877 return ret;
1878}
1879
1880/**
1881 * drm_mode_setplane - set up or tear down an plane
1882 * @dev: DRM device
1883 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001884 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001885 *
1886 * Set plane info, including placement, fb, scaling, and other factors.
1887 * Or pass a NULL fb to disable.
1888 */
1889int drm_mode_setplane(struct drm_device *dev, void *data,
1890 struct drm_file *file_priv)
1891{
1892 struct drm_mode_set_plane *plane_req = data;
1893 struct drm_mode_object *obj;
1894 struct drm_plane *plane;
1895 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001896 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001897 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001898 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001899 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001900
1901 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1902 return -EINVAL;
1903
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001904 /*
1905 * First, find the plane, crtc, and fb objects. If not available,
1906 * we don't bother to call the driver.
1907 */
1908 obj = drm_mode_object_find(dev, plane_req->plane_id,
1909 DRM_MODE_OBJECT_PLANE);
1910 if (!obj) {
1911 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1912 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001913 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001914 }
1915 plane = obj_to_plane(obj);
1916
1917 /* No fb means shut it down */
1918 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001919 drm_modeset_lock_all(dev);
1920 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001921 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001922 plane->crtc = NULL;
1923 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001924 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001925 goto out;
1926 }
1927
1928 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1929 DRM_MODE_OBJECT_CRTC);
1930 if (!obj) {
1931 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1932 plane_req->crtc_id);
1933 ret = -ENOENT;
1934 goto out;
1935 }
1936 crtc = obj_to_crtc(obj);
1937
Daniel Vetter786b99e2012-12-02 21:53:40 +01001938 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1939 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001940 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1941 plane_req->fb_id);
1942 ret = -ENOENT;
1943 goto out;
1944 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001945
Ville Syrjälä62443be2011-12-20 00:06:47 +02001946 /* Check whether this plane supports the fb pixel format. */
1947 for (i = 0; i < plane->format_count; i++)
1948 if (fb->pixel_format == plane->format_types[i])
1949 break;
1950 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03001951 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1952 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02001953 ret = -EINVAL;
1954 goto out;
1955 }
1956
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001957 fb_width = fb->width << 16;
1958 fb_height = fb->height << 16;
1959
1960 /* Make sure source coordinates are inside the fb. */
1961 if (plane_req->src_w > fb_width ||
1962 plane_req->src_x > fb_width - plane_req->src_w ||
1963 plane_req->src_h > fb_height ||
1964 plane_req->src_y > fb_height - plane_req->src_h) {
1965 DRM_DEBUG_KMS("Invalid source coordinates "
1966 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1967 plane_req->src_w >> 16,
1968 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1969 plane_req->src_h >> 16,
1970 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1971 plane_req->src_x >> 16,
1972 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1973 plane_req->src_y >> 16,
1974 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1975 ret = -ENOSPC;
1976 goto out;
1977 }
1978
Ville Syrjälä687a0402011-12-20 00:06:45 +02001979 /* Give drivers some help against integer overflows */
1980 if (plane_req->crtc_w > INT_MAX ||
1981 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1982 plane_req->crtc_h > INT_MAX ||
1983 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1984 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1985 plane_req->crtc_w, plane_req->crtc_h,
1986 plane_req->crtc_x, plane_req->crtc_y);
1987 ret = -ERANGE;
1988 goto out;
1989 }
1990
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001991 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001992 ret = plane->funcs->update_plane(plane, crtc, fb,
1993 plane_req->crtc_x, plane_req->crtc_y,
1994 plane_req->crtc_w, plane_req->crtc_h,
1995 plane_req->src_x, plane_req->src_y,
1996 plane_req->src_w, plane_req->src_h);
1997 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001998 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001999 plane->crtc = crtc;
2000 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002001 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002002 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002003 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002004
2005out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002006 if (fb)
2007 drm_framebuffer_unreference(fb);
2008 if (old_fb)
2009 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002010
2011 return ret;
2012}
2013
2014/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002015 * drm_mode_set_config_internal - helper to call ->set_config
2016 * @set: modeset config to set
2017 *
2018 * This is a little helper to wrap internal calls to the ->set_config driver
2019 * interface. The only thing it adds is correct refcounting dance.
2020 */
2021int drm_mode_set_config_internal(struct drm_mode_set *set)
2022{
2023 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002024 struct drm_framebuffer *fb;
2025 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002026 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002027
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002028 /*
2029 * NOTE: ->set_config can also disable other crtcs (if we steal all
2030 * connectors from it), hence we need to refcount the fbs across all
2031 * crtcs. Atomic modeset will have saner semantics ...
2032 */
2033 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2034 tmp->old_fb = tmp->fb;
2035
Daniel Vetterb0d12322012-12-11 01:07:12 +01002036 fb = set->fb;
2037
2038 ret = crtc->funcs->set_config(set);
2039 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02002040 /* crtc->fb must be updated by ->set_config, enforces this. */
2041 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002042 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002043
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002044 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2045 if (tmp->fb)
2046 drm_framebuffer_reference(tmp->fb);
2047 if (tmp->old_fb)
2048 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002049 }
2050
2051 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002052}
2053EXPORT_SYMBOL(drm_mode_set_config_internal);
2054
Damien Lespiauc11e9282013-09-25 16:45:30 +01002055/*
2056 * Checks that the framebuffer is big enough for the CRTC viewport
2057 * (x, y, hdisplay, vdisplay)
2058 */
2059static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2060 int x, int y,
2061 const struct drm_display_mode *mode,
2062 const struct drm_framebuffer *fb)
2063
2064{
2065 int hdisplay, vdisplay;
2066
2067 hdisplay = mode->hdisplay;
2068 vdisplay = mode->vdisplay;
2069
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002070 if (drm_mode_is_stereo(mode)) {
2071 struct drm_display_mode adjusted = *mode;
2072
2073 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2074 hdisplay = adjusted.crtc_hdisplay;
2075 vdisplay = adjusted.crtc_vdisplay;
2076 }
2077
Damien Lespiauc11e9282013-09-25 16:45:30 +01002078 if (crtc->invert_dimensions)
2079 swap(hdisplay, vdisplay);
2080
2081 if (hdisplay > fb->width ||
2082 vdisplay > fb->height ||
2083 x > fb->width - hdisplay ||
2084 y > fb->height - vdisplay) {
2085 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2086 fb->width, fb->height, hdisplay, vdisplay, x, y,
2087 crtc->invert_dimensions ? " (inverted)" : "");
2088 return -ENOSPC;
2089 }
2090
2091 return 0;
2092}
2093
Daniel Vetter2d13b672012-12-11 13:47:23 +01002094/**
Dave Airlief453ba02008-11-07 14:05:41 -08002095 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002096 * @dev: drm device for the ioctl
2097 * @data: data pointer for the ioctl
2098 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002099 *
Dave Airlief453ba02008-11-07 14:05:41 -08002100 * Build a new CRTC configuration based on user request.
2101 *
2102 * Called by the user via ioctl.
2103 *
2104 * RETURNS:
2105 * Zero on success, errno on failure.
2106 */
2107int drm_mode_setcrtc(struct drm_device *dev, void *data,
2108 struct drm_file *file_priv)
2109{
2110 struct drm_mode_config *config = &dev->mode_config;
2111 struct drm_mode_crtc *crtc_req = data;
2112 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002113 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002114 struct drm_connector **connector_set = NULL, *connector;
2115 struct drm_framebuffer *fb = NULL;
2116 struct drm_display_mode *mode = NULL;
2117 struct drm_mode_set set;
2118 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002119 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002120 int i;
2121
Dave Airliefb3b06c2011-02-08 13:55:21 +10002122 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2123 return -EINVAL;
2124
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002125 /* For some reason crtc x/y offsets are signed internally. */
2126 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2127 return -ERANGE;
2128
Daniel Vetter84849902012-12-02 00:28:11 +01002129 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002130 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2131 DRM_MODE_OBJECT_CRTC);
2132 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002133 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002134 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002135 goto out;
2136 }
2137 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002138 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002139
2140 if (crtc_req->mode_valid) {
2141 /* If we have a mode we need a framebuffer. */
2142 /* If we pass -1, set the mode with the currently bound fb */
2143 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002144 if (!crtc->fb) {
2145 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2146 ret = -EINVAL;
2147 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002148 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002149 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002150 /* Make refcounting symmetric with the lookup path. */
2151 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002152 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002153 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2154 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002155 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2156 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002157 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002158 goto out;
2159 }
Dave Airlief453ba02008-11-07 14:05:41 -08002160 }
2161
2162 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002163 if (!mode) {
2164 ret = -ENOMEM;
2165 goto out;
2166 }
2167
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002168 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2169 if (ret) {
2170 DRM_DEBUG_KMS("Invalid mode\n");
2171 goto out;
2172 }
2173
Dave Airlief453ba02008-11-07 14:05:41 -08002174 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002175
Damien Lespiauc11e9282013-09-25 16:45:30 +01002176 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2177 mode, fb);
2178 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002179 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002180
Dave Airlief453ba02008-11-07 14:05:41 -08002181 }
2182
2183 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002184 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002185 ret = -EINVAL;
2186 goto out;
2187 }
2188
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002189 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002190 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002191 crtc_req->count_connectors);
2192 ret = -EINVAL;
2193 goto out;
2194 }
2195
2196 if (crtc_req->count_connectors > 0) {
2197 u32 out_id;
2198
2199 /* Avoid unbounded kernel memory allocation */
2200 if (crtc_req->count_connectors > config->num_connector) {
2201 ret = -EINVAL;
2202 goto out;
2203 }
2204
2205 connector_set = kmalloc(crtc_req->count_connectors *
2206 sizeof(struct drm_connector *),
2207 GFP_KERNEL);
2208 if (!connector_set) {
2209 ret = -ENOMEM;
2210 goto out;
2211 }
2212
2213 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002214 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002215 if (get_user(out_id, &set_connectors_ptr[i])) {
2216 ret = -EFAULT;
2217 goto out;
2218 }
2219
2220 obj = drm_mode_object_find(dev, out_id,
2221 DRM_MODE_OBJECT_CONNECTOR);
2222 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002223 DRM_DEBUG_KMS("Connector id %d unknown\n",
2224 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002225 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002226 goto out;
2227 }
2228 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002229 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2230 connector->base.id,
2231 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002232
2233 connector_set[i] = connector;
2234 }
2235 }
2236
2237 set.crtc = crtc;
2238 set.x = crtc_req->x;
2239 set.y = crtc_req->y;
2240 set.mode = mode;
2241 set.connectors = connector_set;
2242 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002243 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002244 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002245
2246out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002247 if (fb)
2248 drm_framebuffer_unreference(fb);
2249
Dave Airlief453ba02008-11-07 14:05:41 -08002250 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002251 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002252 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002253 return ret;
2254}
2255
Dave Airlie4c813d42013-06-20 11:48:52 +10002256static int drm_mode_cursor_common(struct drm_device *dev,
2257 struct drm_mode_cursor2 *req,
2258 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002259{
Dave Airlief453ba02008-11-07 14:05:41 -08002260 struct drm_mode_object *obj;
2261 struct drm_crtc *crtc;
2262 int ret = 0;
2263
Dave Airliefb3b06c2011-02-08 13:55:21 +10002264 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2265 return -EINVAL;
2266
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002267 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002268 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002269
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002270 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002271 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002272 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002273 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002274 }
2275 crtc = obj_to_crtc(obj);
2276
Daniel Vetterdac35662012-12-02 15:24:10 +01002277 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002278 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002279 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002280 ret = -ENXIO;
2281 goto out;
2282 }
2283 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002284 if (crtc->funcs->cursor_set2)
2285 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2286 req->width, req->height, req->hot_x, req->hot_y);
2287 else
2288 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2289 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002290 }
2291
2292 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2293 if (crtc->funcs->cursor_move) {
2294 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2295 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002296 ret = -EFAULT;
2297 goto out;
2298 }
2299 }
2300out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002301 mutex_unlock(&crtc->mutex);
2302
Dave Airlief453ba02008-11-07 14:05:41 -08002303 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002304
2305}
2306int drm_mode_cursor_ioctl(struct drm_device *dev,
2307 void *data, struct drm_file *file_priv)
2308{
2309 struct drm_mode_cursor *req = data;
2310 struct drm_mode_cursor2 new_req;
2311
2312 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2313 new_req.hot_x = new_req.hot_y = 0;
2314
2315 return drm_mode_cursor_common(dev, &new_req, file_priv);
2316}
2317
2318int drm_mode_cursor2_ioctl(struct drm_device *dev,
2319 void *data, struct drm_file *file_priv)
2320{
2321 struct drm_mode_cursor2 *req = data;
2322 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002323}
2324
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002325/* Original addfb only supported RGB formats, so figure out which one */
2326uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2327{
2328 uint32_t fmt;
2329
2330 switch (bpp) {
2331 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002332 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002333 break;
2334 case 16:
2335 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002336 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002337 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002338 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002339 break;
2340 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002341 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002342 break;
2343 case 32:
2344 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002345 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002346 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002347 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002348 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002349 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002350 break;
2351 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002352 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2353 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002354 break;
2355 }
2356
2357 return fmt;
2358}
2359EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2360
Dave Airlief453ba02008-11-07 14:05:41 -08002361/**
2362 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002363 * @dev: drm device for the ioctl
2364 * @data: data pointer for the ioctl
2365 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002366 *
Dave Airlief453ba02008-11-07 14:05:41 -08002367 * Add a new FB to the specified CRTC, given a user request.
2368 *
2369 * Called by the user via ioctl.
2370 *
2371 * RETURNS:
2372 * Zero on success, errno on failure.
2373 */
2374int drm_mode_addfb(struct drm_device *dev,
2375 void *data, struct drm_file *file_priv)
2376{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002377 struct drm_mode_fb_cmd *or = data;
2378 struct drm_mode_fb_cmd2 r = {};
2379 struct drm_mode_config *config = &dev->mode_config;
2380 struct drm_framebuffer *fb;
2381 int ret = 0;
2382
2383 /* Use new struct with format internally */
2384 r.fb_id = or->fb_id;
2385 r.width = or->width;
2386 r.height = or->height;
2387 r.pitches[0] = or->pitch;
2388 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2389 r.handles[0] = or->handle;
2390
2391 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2392 return -EINVAL;
2393
Jesse Barnesacb4b992011-11-07 12:03:23 -08002394 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002395 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002396
2397 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002398 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002399
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002400 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2401 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002402 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002403 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002404 }
2405
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002406 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002407 or->fb_id = fb->base.id;
2408 list_add(&fb->filp_head, &file_priv->fbs);
2409 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002410 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002411
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002412 return ret;
2413}
2414
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002415static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002416{
2417 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2418
2419 switch (format) {
2420 case DRM_FORMAT_C8:
2421 case DRM_FORMAT_RGB332:
2422 case DRM_FORMAT_BGR233:
2423 case DRM_FORMAT_XRGB4444:
2424 case DRM_FORMAT_XBGR4444:
2425 case DRM_FORMAT_RGBX4444:
2426 case DRM_FORMAT_BGRX4444:
2427 case DRM_FORMAT_ARGB4444:
2428 case DRM_FORMAT_ABGR4444:
2429 case DRM_FORMAT_RGBA4444:
2430 case DRM_FORMAT_BGRA4444:
2431 case DRM_FORMAT_XRGB1555:
2432 case DRM_FORMAT_XBGR1555:
2433 case DRM_FORMAT_RGBX5551:
2434 case DRM_FORMAT_BGRX5551:
2435 case DRM_FORMAT_ARGB1555:
2436 case DRM_FORMAT_ABGR1555:
2437 case DRM_FORMAT_RGBA5551:
2438 case DRM_FORMAT_BGRA5551:
2439 case DRM_FORMAT_RGB565:
2440 case DRM_FORMAT_BGR565:
2441 case DRM_FORMAT_RGB888:
2442 case DRM_FORMAT_BGR888:
2443 case DRM_FORMAT_XRGB8888:
2444 case DRM_FORMAT_XBGR8888:
2445 case DRM_FORMAT_RGBX8888:
2446 case DRM_FORMAT_BGRX8888:
2447 case DRM_FORMAT_ARGB8888:
2448 case DRM_FORMAT_ABGR8888:
2449 case DRM_FORMAT_RGBA8888:
2450 case DRM_FORMAT_BGRA8888:
2451 case DRM_FORMAT_XRGB2101010:
2452 case DRM_FORMAT_XBGR2101010:
2453 case DRM_FORMAT_RGBX1010102:
2454 case DRM_FORMAT_BGRX1010102:
2455 case DRM_FORMAT_ARGB2101010:
2456 case DRM_FORMAT_ABGR2101010:
2457 case DRM_FORMAT_RGBA1010102:
2458 case DRM_FORMAT_BGRA1010102:
2459 case DRM_FORMAT_YUYV:
2460 case DRM_FORMAT_YVYU:
2461 case DRM_FORMAT_UYVY:
2462 case DRM_FORMAT_VYUY:
2463 case DRM_FORMAT_AYUV:
2464 case DRM_FORMAT_NV12:
2465 case DRM_FORMAT_NV21:
2466 case DRM_FORMAT_NV16:
2467 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002468 case DRM_FORMAT_NV24:
2469 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002470 case DRM_FORMAT_YUV410:
2471 case DRM_FORMAT_YVU410:
2472 case DRM_FORMAT_YUV411:
2473 case DRM_FORMAT_YVU411:
2474 case DRM_FORMAT_YUV420:
2475 case DRM_FORMAT_YVU420:
2476 case DRM_FORMAT_YUV422:
2477 case DRM_FORMAT_YVU422:
2478 case DRM_FORMAT_YUV444:
2479 case DRM_FORMAT_YVU444:
2480 return 0;
2481 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002482 DRM_DEBUG_KMS("invalid pixel format %s\n",
2483 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002484 return -EINVAL;
2485 }
2486}
2487
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002488static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002489{
2490 int ret, hsub, vsub, num_planes, i;
2491
2492 ret = format_check(r);
2493 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002494 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2495 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002496 return ret;
2497 }
2498
2499 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2500 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2501 num_planes = drm_format_num_planes(r->pixel_format);
2502
2503 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002504 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002505 return -EINVAL;
2506 }
2507
2508 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002509 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002510 return -EINVAL;
2511 }
2512
2513 for (i = 0; i < num_planes; i++) {
2514 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002515 unsigned int height = r->height / (i != 0 ? vsub : 1);
2516 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002517
2518 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002519 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002520 return -EINVAL;
2521 }
2522
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002523 if ((uint64_t) width * cpp > UINT_MAX)
2524 return -ERANGE;
2525
2526 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2527 return -ERANGE;
2528
2529 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002530 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002531 return -EINVAL;
2532 }
2533 }
2534
2535 return 0;
2536}
2537
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002538/**
2539 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002540 * @dev: drm device for the ioctl
2541 * @data: data pointer for the ioctl
2542 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002543 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002544 * Add a new FB to the specified CRTC, given a user request with format.
2545 *
2546 * Called by the user via ioctl.
2547 *
2548 * RETURNS:
2549 * Zero on success, errno on failure.
2550 */
2551int drm_mode_addfb2(struct drm_device *dev,
2552 void *data, struct drm_file *file_priv)
2553{
2554 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002555 struct drm_mode_config *config = &dev->mode_config;
2556 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002557 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002558
Dave Airliefb3b06c2011-02-08 13:55:21 +10002559 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2560 return -EINVAL;
2561
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002562 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2563 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2564 return -EINVAL;
2565 }
2566
Dave Airlief453ba02008-11-07 14:05:41 -08002567 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002568 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002569 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002570 return -EINVAL;
2571 }
2572 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002573 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002574 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002575 return -EINVAL;
2576 }
2577
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002578 ret = framebuffer_check(r);
2579 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002580 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002581
Dave Airlief453ba02008-11-07 14:05:41 -08002582 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002583 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002584 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002585 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002586 }
2587
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002588 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002589 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002590 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002591 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002592 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002593
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002594
Dave Airlief453ba02008-11-07 14:05:41 -08002595 return ret;
2596}
2597
2598/**
2599 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002600 * @dev: drm device for the ioctl
2601 * @data: data pointer for the ioctl
2602 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002603 *
Dave Airlief453ba02008-11-07 14:05:41 -08002604 * Remove the FB specified by the user.
2605 *
2606 * Called by the user via ioctl.
2607 *
2608 * RETURNS:
2609 * Zero on success, errno on failure.
2610 */
2611int drm_mode_rmfb(struct drm_device *dev,
2612 void *data, struct drm_file *file_priv)
2613{
Dave Airlief453ba02008-11-07 14:05:41 -08002614 struct drm_framebuffer *fb = NULL;
2615 struct drm_framebuffer *fbl = NULL;
2616 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002617 int found = 0;
2618
Dave Airliefb3b06c2011-02-08 13:55:21 +10002619 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2620 return -EINVAL;
2621
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002622 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002623 mutex_lock(&dev->mode_config.fb_lock);
2624 fb = __drm_framebuffer_lookup(dev, *id);
2625 if (!fb)
2626 goto fail_lookup;
2627
Dave Airlief453ba02008-11-07 14:05:41 -08002628 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2629 if (fb == fbl)
2630 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002631 if (!found)
2632 goto fail_lookup;
2633
2634 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2635 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002636
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002637 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002638 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002639 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002640
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002641 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002642
Daniel Vetter2b677e82012-12-10 21:16:05 +01002643 return 0;
2644
2645fail_lookup:
2646 mutex_unlock(&dev->mode_config.fb_lock);
2647 mutex_unlock(&file_priv->fbs_lock);
2648
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002649 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002650}
2651
2652/**
2653 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002654 * @dev: drm device for the ioctl
2655 * @data: data pointer for the ioctl
2656 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002657 *
Dave Airlief453ba02008-11-07 14:05:41 -08002658 * Lookup the FB given its ID and return info about it.
2659 *
2660 * Called by the user via ioctl.
2661 *
2662 * RETURNS:
2663 * Zero on success, errno on failure.
2664 */
2665int drm_mode_getfb(struct drm_device *dev,
2666 void *data, struct drm_file *file_priv)
2667{
2668 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002669 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002670 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002671
Dave Airliefb3b06c2011-02-08 13:55:21 +10002672 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2673 return -EINVAL;
2674
Daniel Vetter786b99e2012-12-02 21:53:40 +01002675 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002676 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002677 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002678
2679 r->height = fb->height;
2680 r->width = fb->width;
2681 r->depth = fb->depth;
2682 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002683 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002684 if (fb->funcs->create_handle) {
2685 if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
2686 ret = fb->funcs->create_handle(fb, file_priv,
2687 &r->handle);
2688 } else {
2689 /* GET_FB() is an unprivileged ioctl so we must not
2690 * return a buffer-handle to non-master processes! For
2691 * backwards-compatibility reasons, we cannot make
2692 * GET_FB() privileged, so just return an invalid handle
2693 * for non-masters. */
2694 r->handle = 0;
2695 ret = 0;
2696 }
2697 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002698 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02002699 }
Dave Airlief453ba02008-11-07 14:05:41 -08002700
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002701 drm_framebuffer_unreference(fb);
2702
Dave Airlief453ba02008-11-07 14:05:41 -08002703 return ret;
2704}
2705
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002706int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2707 void *data, struct drm_file *file_priv)
2708{
2709 struct drm_clip_rect __user *clips_ptr;
2710 struct drm_clip_rect *clips = NULL;
2711 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002712 struct drm_framebuffer *fb;
2713 unsigned flags;
2714 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002715 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002716
Dave Airliefb3b06c2011-02-08 13:55:21 +10002717 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2718 return -EINVAL;
2719
Daniel Vetter786b99e2012-12-02 21:53:40 +01002720 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002721 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002722 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002723
2724 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002725 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002726
2727 if (!num_clips != !clips_ptr) {
2728 ret = -EINVAL;
2729 goto out_err1;
2730 }
2731
2732 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2733
2734 /* If userspace annotates copy, clips must come in pairs */
2735 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2736 ret = -EINVAL;
2737 goto out_err1;
2738 }
2739
2740 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002741 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2742 ret = -EINVAL;
2743 goto out_err1;
2744 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002745 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2746 if (!clips) {
2747 ret = -ENOMEM;
2748 goto out_err1;
2749 }
2750
2751 ret = copy_from_user(clips, clips_ptr,
2752 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002753 if (ret) {
2754 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002755 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002756 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002757 }
2758
2759 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002760 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2761 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002762 } else {
2763 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002764 }
2765
2766out_err2:
2767 kfree(clips);
2768out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002769 drm_framebuffer_unreference(fb);
2770
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002771 return ret;
2772}
2773
2774
Dave Airlief453ba02008-11-07 14:05:41 -08002775/**
2776 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002777 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002778 *
Dave Airlief453ba02008-11-07 14:05:41 -08002779 * Destroy all the FBs associated with @filp.
2780 *
2781 * Called by the user via ioctl.
2782 *
2783 * RETURNS:
2784 * Zero on success, errno on failure.
2785 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002786void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002787{
Dave Airlief453ba02008-11-07 14:05:41 -08002788 struct drm_device *dev = priv->minor->dev;
2789 struct drm_framebuffer *fb, *tfb;
2790
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002791 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002792 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002793
2794 mutex_lock(&dev->mode_config.fb_lock);
2795 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2796 __drm_framebuffer_unregister(dev, fb);
2797 mutex_unlock(&dev->mode_config.fb_lock);
2798
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002799 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002800
2801 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002802 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002803 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002804 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002805}
2806
Dave Airlief453ba02008-11-07 14:05:41 -08002807struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2808 const char *name, int num_values)
2809{
2810 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002811 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002812
2813 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2814 if (!property)
2815 return NULL;
2816
2817 if (num_values) {
2818 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2819 if (!property->values)
2820 goto fail;
2821 }
2822
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002823 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2824 if (ret)
2825 goto fail;
2826
Dave Airlief453ba02008-11-07 14:05:41 -08002827 property->flags = flags;
2828 property->num_values = num_values;
2829 INIT_LIST_HEAD(&property->enum_blob_list);
2830
Vinson Lee471dd2e2011-11-10 11:55:40 -08002831 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002832 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002833 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2834 }
Dave Airlief453ba02008-11-07 14:05:41 -08002835
2836 list_add_tail(&property->head, &dev->mode_config.property_list);
2837 return property;
2838fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002839 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002840 kfree(property);
2841 return NULL;
2842}
2843EXPORT_SYMBOL(drm_property_create);
2844
Sascha Hauer4a67d392012-02-06 10:58:17 +01002845struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2846 const char *name,
2847 const struct drm_prop_enum_list *props,
2848 int num_values)
2849{
2850 struct drm_property *property;
2851 int i, ret;
2852
2853 flags |= DRM_MODE_PROP_ENUM;
2854
2855 property = drm_property_create(dev, flags, name, num_values);
2856 if (!property)
2857 return NULL;
2858
2859 for (i = 0; i < num_values; i++) {
2860 ret = drm_property_add_enum(property, i,
2861 props[i].type,
2862 props[i].name);
2863 if (ret) {
2864 drm_property_destroy(dev, property);
2865 return NULL;
2866 }
2867 }
2868
2869 return property;
2870}
2871EXPORT_SYMBOL(drm_property_create_enum);
2872
Rob Clark49e27542012-05-17 02:23:26 -06002873struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2874 int flags, const char *name,
2875 const struct drm_prop_enum_list *props,
2876 int num_values)
2877{
2878 struct drm_property *property;
2879 int i, ret;
2880
2881 flags |= DRM_MODE_PROP_BITMASK;
2882
2883 property = drm_property_create(dev, flags, name, num_values);
2884 if (!property)
2885 return NULL;
2886
2887 for (i = 0; i < num_values; i++) {
2888 ret = drm_property_add_enum(property, i,
2889 props[i].type,
2890 props[i].name);
2891 if (ret) {
2892 drm_property_destroy(dev, property);
2893 return NULL;
2894 }
2895 }
2896
2897 return property;
2898}
2899EXPORT_SYMBOL(drm_property_create_bitmask);
2900
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002901struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2902 const char *name,
2903 uint64_t min, uint64_t max)
2904{
2905 struct drm_property *property;
2906
2907 flags |= DRM_MODE_PROP_RANGE;
2908
2909 property = drm_property_create(dev, flags, name, 2);
2910 if (!property)
2911 return NULL;
2912
2913 property->values[0] = min;
2914 property->values[1] = max;
2915
2916 return property;
2917}
2918EXPORT_SYMBOL(drm_property_create_range);
2919
Dave Airlief453ba02008-11-07 14:05:41 -08002920int drm_property_add_enum(struct drm_property *property, int index,
2921 uint64_t value, const char *name)
2922{
2923 struct drm_property_enum *prop_enum;
2924
Rob Clark49e27542012-05-17 02:23:26 -06002925 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2926 return -EINVAL;
2927
2928 /*
2929 * Bitmask enum properties have the additional constraint of values
2930 * from 0 to 63
2931 */
2932 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002933 return -EINVAL;
2934
2935 if (!list_empty(&property->enum_blob_list)) {
2936 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2937 if (prop_enum->value == value) {
2938 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2939 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2940 return 0;
2941 }
2942 }
2943 }
2944
2945 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2946 if (!prop_enum)
2947 return -ENOMEM;
2948
2949 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2950 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2951 prop_enum->value = value;
2952
2953 property->values[index] = value;
2954 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2955 return 0;
2956}
2957EXPORT_SYMBOL(drm_property_add_enum);
2958
2959void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2960{
2961 struct drm_property_enum *prop_enum, *pt;
2962
2963 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2964 list_del(&prop_enum->head);
2965 kfree(prop_enum);
2966 }
2967
2968 if (property->num_values)
2969 kfree(property->values);
2970 drm_mode_object_put(dev, &property->base);
2971 list_del(&property->head);
2972 kfree(property);
2973}
2974EXPORT_SYMBOL(drm_property_destroy);
2975
Paulo Zanonic5431882012-05-15 18:09:02 -03002976void drm_object_attach_property(struct drm_mode_object *obj,
2977 struct drm_property *property,
2978 uint64_t init_val)
2979{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002980 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002981
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002982 if (count == DRM_OBJECT_MAX_PROPERTY) {
2983 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2984 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2985 "you see this message on the same object type.\n",
2986 obj->type);
2987 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002988 }
2989
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002990 obj->properties->ids[count] = property->base.id;
2991 obj->properties->values[count] = init_val;
2992 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002993}
2994EXPORT_SYMBOL(drm_object_attach_property);
2995
2996int drm_object_property_set_value(struct drm_mode_object *obj,
2997 struct drm_property *property, uint64_t val)
2998{
2999 int i;
3000
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003001 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003002 if (obj->properties->ids[i] == property->base.id) {
3003 obj->properties->values[i] = val;
3004 return 0;
3005 }
3006 }
3007
3008 return -EINVAL;
3009}
3010EXPORT_SYMBOL(drm_object_property_set_value);
3011
3012int drm_object_property_get_value(struct drm_mode_object *obj,
3013 struct drm_property *property, uint64_t *val)
3014{
3015 int i;
3016
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003017 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003018 if (obj->properties->ids[i] == property->base.id) {
3019 *val = obj->properties->values[i];
3020 return 0;
3021 }
3022 }
3023
3024 return -EINVAL;
3025}
3026EXPORT_SYMBOL(drm_object_property_get_value);
3027
Dave Airlief453ba02008-11-07 14:05:41 -08003028int drm_mode_getproperty_ioctl(struct drm_device *dev,
3029 void *data, struct drm_file *file_priv)
3030{
3031 struct drm_mode_object *obj;
3032 struct drm_mode_get_property *out_resp = data;
3033 struct drm_property *property;
3034 int enum_count = 0;
3035 int blob_count = 0;
3036 int value_count = 0;
3037 int ret = 0, i;
3038 int copied;
3039 struct drm_property_enum *prop_enum;
3040 struct drm_mode_property_enum __user *enum_ptr;
3041 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003042 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003043 uint64_t __user *values_ptr;
3044 uint32_t __user *blob_length_ptr;
3045
Dave Airliefb3b06c2011-02-08 13:55:21 +10003046 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3047 return -EINVAL;
3048
Daniel Vetter84849902012-12-02 00:28:11 +01003049 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003050 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3051 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003052 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003053 goto done;
3054 }
3055 property = obj_to_property(obj);
3056
Rob Clark49e27542012-05-17 02:23:26 -06003057 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003058 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3059 enum_count++;
3060 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3061 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3062 blob_count++;
3063 }
3064
3065 value_count = property->num_values;
3066
3067 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3068 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3069 out_resp->flags = property->flags;
3070
3071 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003072 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003073 for (i = 0; i < value_count; i++) {
3074 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3075 ret = -EFAULT;
3076 goto done;
3077 }
3078 }
3079 }
3080 out_resp->count_values = value_count;
3081
Rob Clark49e27542012-05-17 02:23:26 -06003082 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003083 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3084 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003085 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003086 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3087
3088 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3089 ret = -EFAULT;
3090 goto done;
3091 }
3092
3093 if (copy_to_user(&enum_ptr[copied].name,
3094 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3095 ret = -EFAULT;
3096 goto done;
3097 }
3098 copied++;
3099 }
3100 }
3101 out_resp->count_enum_blobs = enum_count;
3102 }
3103
3104 if (property->flags & DRM_MODE_PROP_BLOB) {
3105 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3106 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003107 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3108 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003109
3110 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3111 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3112 ret = -EFAULT;
3113 goto done;
3114 }
3115
3116 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3117 ret = -EFAULT;
3118 goto done;
3119 }
3120
3121 copied++;
3122 }
3123 }
3124 out_resp->count_enum_blobs = blob_count;
3125 }
3126done:
Daniel Vetter84849902012-12-02 00:28:11 +01003127 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003128 return ret;
3129}
3130
3131static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3132 void *data)
3133{
3134 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003135 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003136
3137 if (!length || !data)
3138 return NULL;
3139
3140 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3141 if (!blob)
3142 return NULL;
3143
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003144 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3145 if (ret) {
3146 kfree(blob);
3147 return NULL;
3148 }
3149
Dave Airlief453ba02008-11-07 14:05:41 -08003150 blob->length = length;
3151
3152 memcpy(blob->data, data, length);
3153
Dave Airlief453ba02008-11-07 14:05:41 -08003154 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3155 return blob;
3156}
3157
3158static void drm_property_destroy_blob(struct drm_device *dev,
3159 struct drm_property_blob *blob)
3160{
3161 drm_mode_object_put(dev, &blob->base);
3162 list_del(&blob->head);
3163 kfree(blob);
3164}
3165
3166int drm_mode_getblob_ioctl(struct drm_device *dev,
3167 void *data, struct drm_file *file_priv)
3168{
3169 struct drm_mode_object *obj;
3170 struct drm_mode_get_blob *out_resp = data;
3171 struct drm_property_blob *blob;
3172 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003173 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003174
Dave Airliefb3b06c2011-02-08 13:55:21 +10003175 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3176 return -EINVAL;
3177
Daniel Vetter84849902012-12-02 00:28:11 +01003178 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003179 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3180 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003181 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003182 goto done;
3183 }
3184 blob = obj_to_blob(obj);
3185
3186 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003187 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003188 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3189 ret = -EFAULT;
3190 goto done;
3191 }
3192 }
3193 out_resp->length = blob->length;
3194
3195done:
Daniel Vetter84849902012-12-02 00:28:11 +01003196 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003197 return ret;
3198}
3199
3200int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3201 struct edid *edid)
3202{
3203 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003204 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003205
3206 if (connector->edid_blob_ptr)
3207 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3208
3209 /* Delete edid, when there is none. */
3210 if (!edid) {
3211 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003212 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003213 return ret;
3214 }
3215
Adam Jackson7466f4c2010-03-29 21:43:23 +00003216 size = EDID_LENGTH * (1 + edid->extensions);
3217 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3218 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003219 if (!connector->edid_blob_ptr)
3220 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003221
Rob Clark58495562012-10-11 20:50:56 -05003222 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003223 dev->mode_config.edid_property,
3224 connector->edid_blob_ptr->base.id);
3225
3226 return ret;
3227}
3228EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3229
Paulo Zanoni26a34812012-05-15 18:08:59 -03003230static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003231 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003232{
3233 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3234 return false;
3235 if (property->flags & DRM_MODE_PROP_RANGE) {
3236 if (value < property->values[0] || value > property->values[1])
3237 return false;
3238 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003239 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3240 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003241 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003242 for (i = 0; i < property->num_values; i++)
3243 valid_mask |= (1ULL << property->values[i]);
3244 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003245 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3246 /* Only the driver knows */
3247 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003248 } else {
3249 int i;
3250 for (i = 0; i < property->num_values; i++)
3251 if (property->values[i] == value)
3252 return true;
3253 return false;
3254 }
3255}
3256
Dave Airlief453ba02008-11-07 14:05:41 -08003257int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3258 void *data, struct drm_file *file_priv)
3259{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003260 struct drm_mode_connector_set_property *conn_set_prop = data;
3261 struct drm_mode_obj_set_property obj_set_prop = {
3262 .value = conn_set_prop->value,
3263 .prop_id = conn_set_prop->prop_id,
3264 .obj_id = conn_set_prop->connector_id,
3265 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3266 };
Dave Airlief453ba02008-11-07 14:05:41 -08003267
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003268 /* It does all the locking and checking we need */
3269 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003270}
3271
Paulo Zanonic5431882012-05-15 18:09:02 -03003272static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3273 struct drm_property *property,
3274 uint64_t value)
3275{
3276 int ret = -EINVAL;
3277 struct drm_connector *connector = obj_to_connector(obj);
3278
3279 /* Do DPMS ourselves */
3280 if (property == connector->dev->mode_config.dpms_property) {
3281 if (connector->funcs->dpms)
3282 (*connector->funcs->dpms)(connector, (int)value);
3283 ret = 0;
3284 } else if (connector->funcs->set_property)
3285 ret = connector->funcs->set_property(connector, property, value);
3286
3287 /* store the property value if successful */
3288 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003289 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003290 return ret;
3291}
3292
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003293static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3294 struct drm_property *property,
3295 uint64_t value)
3296{
3297 int ret = -EINVAL;
3298 struct drm_crtc *crtc = obj_to_crtc(obj);
3299
3300 if (crtc->funcs->set_property)
3301 ret = crtc->funcs->set_property(crtc, property, value);
3302 if (!ret)
3303 drm_object_property_set_value(obj, property, value);
3304
3305 return ret;
3306}
3307
Rob Clark4d939142012-05-17 02:23:27 -06003308static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3309 struct drm_property *property,
3310 uint64_t value)
3311{
3312 int ret = -EINVAL;
3313 struct drm_plane *plane = obj_to_plane(obj);
3314
3315 if (plane->funcs->set_property)
3316 ret = plane->funcs->set_property(plane, property, value);
3317 if (!ret)
3318 drm_object_property_set_value(obj, property, value);
3319
3320 return ret;
3321}
3322
Paulo Zanonic5431882012-05-15 18:09:02 -03003323int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3324 struct drm_file *file_priv)
3325{
3326 struct drm_mode_obj_get_properties *arg = data;
3327 struct drm_mode_object *obj;
3328 int ret = 0;
3329 int i;
3330 int copied = 0;
3331 int props_count = 0;
3332 uint32_t __user *props_ptr;
3333 uint64_t __user *prop_values_ptr;
3334
3335 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3336 return -EINVAL;
3337
Daniel Vetter84849902012-12-02 00:28:11 +01003338 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003339
3340 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3341 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003342 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003343 goto out;
3344 }
3345 if (!obj->properties) {
3346 ret = -EINVAL;
3347 goto out;
3348 }
3349
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003350 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003351
3352 /* This ioctl is called twice, once to determine how much space is
3353 * needed, and the 2nd time to fill it. */
3354 if ((arg->count_props >= props_count) && props_count) {
3355 copied = 0;
3356 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3357 prop_values_ptr = (uint64_t __user *)(unsigned long)
3358 (arg->prop_values_ptr);
3359 for (i = 0; i < props_count; i++) {
3360 if (put_user(obj->properties->ids[i],
3361 props_ptr + copied)) {
3362 ret = -EFAULT;
3363 goto out;
3364 }
3365 if (put_user(obj->properties->values[i],
3366 prop_values_ptr + copied)) {
3367 ret = -EFAULT;
3368 goto out;
3369 }
3370 copied++;
3371 }
3372 }
3373 arg->count_props = props_count;
3374out:
Daniel Vetter84849902012-12-02 00:28:11 +01003375 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003376 return ret;
3377}
3378
3379int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3380 struct drm_file *file_priv)
3381{
3382 struct drm_mode_obj_set_property *arg = data;
3383 struct drm_mode_object *arg_obj;
3384 struct drm_mode_object *prop_obj;
3385 struct drm_property *property;
3386 int ret = -EINVAL;
3387 int i;
3388
3389 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3390 return -EINVAL;
3391
Daniel Vetter84849902012-12-02 00:28:11 +01003392 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003393
3394 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003395 if (!arg_obj) {
3396 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003397 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003398 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003399 if (!arg_obj->properties)
3400 goto out;
3401
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003402 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003403 if (arg_obj->properties->ids[i] == arg->prop_id)
3404 break;
3405
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003406 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003407 goto out;
3408
3409 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3410 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003411 if (!prop_obj) {
3412 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003413 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003414 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003415 property = obj_to_property(prop_obj);
3416
3417 if (!drm_property_change_is_valid(property, arg->value))
3418 goto out;
3419
3420 switch (arg_obj->type) {
3421 case DRM_MODE_OBJECT_CONNECTOR:
3422 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3423 arg->value);
3424 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003425 case DRM_MODE_OBJECT_CRTC:
3426 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3427 break;
Rob Clark4d939142012-05-17 02:23:27 -06003428 case DRM_MODE_OBJECT_PLANE:
3429 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3430 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003431 }
3432
3433out:
Daniel Vetter84849902012-12-02 00:28:11 +01003434 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003435 return ret;
3436}
3437
Dave Airlief453ba02008-11-07 14:05:41 -08003438int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3439 struct drm_encoder *encoder)
3440{
3441 int i;
3442
3443 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3444 if (connector->encoder_ids[i] == 0) {
3445 connector->encoder_ids[i] = encoder->base.id;
3446 return 0;
3447 }
3448 }
3449 return -ENOMEM;
3450}
3451EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3452
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003453int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003454 int gamma_size)
3455{
3456 crtc->gamma_size = gamma_size;
3457
3458 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3459 if (!crtc->gamma_store) {
3460 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003461 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003462 }
3463
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003464 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003465}
3466EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3467
3468int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3469 void *data, struct drm_file *file_priv)
3470{
3471 struct drm_mode_crtc_lut *crtc_lut = data;
3472 struct drm_mode_object *obj;
3473 struct drm_crtc *crtc;
3474 void *r_base, *g_base, *b_base;
3475 int size;
3476 int ret = 0;
3477
Dave Airliefb3b06c2011-02-08 13:55:21 +10003478 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3479 return -EINVAL;
3480
Daniel Vetter84849902012-12-02 00:28:11 +01003481 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003482 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3483 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003484 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003485 goto out;
3486 }
3487 crtc = obj_to_crtc(obj);
3488
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003489 if (crtc->funcs->gamma_set == NULL) {
3490 ret = -ENOSYS;
3491 goto out;
3492 }
3493
Dave Airlief453ba02008-11-07 14:05:41 -08003494 /* memcpy into gamma store */
3495 if (crtc_lut->gamma_size != crtc->gamma_size) {
3496 ret = -EINVAL;
3497 goto out;
3498 }
3499
3500 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3501 r_base = crtc->gamma_store;
3502 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3503 ret = -EFAULT;
3504 goto out;
3505 }
3506
3507 g_base = r_base + size;
3508 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3509 ret = -EFAULT;
3510 goto out;
3511 }
3512
3513 b_base = g_base + size;
3514 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3515 ret = -EFAULT;
3516 goto out;
3517 }
3518
James Simmons72034252010-08-03 01:33:19 +01003519 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003520
3521out:
Daniel Vetter84849902012-12-02 00:28:11 +01003522 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003523 return ret;
3524
3525}
3526
3527int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3528 void *data, struct drm_file *file_priv)
3529{
3530 struct drm_mode_crtc_lut *crtc_lut = data;
3531 struct drm_mode_object *obj;
3532 struct drm_crtc *crtc;
3533 void *r_base, *g_base, *b_base;
3534 int size;
3535 int ret = 0;
3536
Dave Airliefb3b06c2011-02-08 13:55:21 +10003537 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3538 return -EINVAL;
3539
Daniel Vetter84849902012-12-02 00:28:11 +01003540 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003541 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3542 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003543 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003544 goto out;
3545 }
3546 crtc = obj_to_crtc(obj);
3547
3548 /* memcpy into gamma store */
3549 if (crtc_lut->gamma_size != crtc->gamma_size) {
3550 ret = -EINVAL;
3551 goto out;
3552 }
3553
3554 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3555 r_base = crtc->gamma_store;
3556 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3557 ret = -EFAULT;
3558 goto out;
3559 }
3560
3561 g_base = r_base + size;
3562 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3563 ret = -EFAULT;
3564 goto out;
3565 }
3566
3567 b_base = g_base + size;
3568 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3569 ret = -EFAULT;
3570 goto out;
3571 }
3572out:
Daniel Vetter84849902012-12-02 00:28:11 +01003573 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003574 return ret;
3575}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003576
3577int drm_mode_page_flip_ioctl(struct drm_device *dev,
3578 void *data, struct drm_file *file_priv)
3579{
3580 struct drm_mode_crtc_page_flip *page_flip = data;
3581 struct drm_mode_object *obj;
3582 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003583 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003584 struct drm_pending_vblank_event *e = NULL;
3585 unsigned long flags;
3586 int ret = -EINVAL;
3587
3588 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3589 page_flip->reserved != 0)
3590 return -EINVAL;
3591
Keith Packard62f21042013-07-22 18:50:00 -07003592 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3593 return -EINVAL;
3594
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003595 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3596 if (!obj)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003597 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003598 crtc = obj_to_crtc(obj);
3599
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003600 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003601 if (crtc->fb == NULL) {
3602 /* The framebuffer is currently unbound, presumably
3603 * due to a hotplug event, that userspace has not
3604 * yet discovered.
3605 */
3606 ret = -EBUSY;
3607 goto out;
3608 }
3609
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003610 if (crtc->funcs->page_flip == NULL)
3611 goto out;
3612
Daniel Vetter786b99e2012-12-02 21:53:40 +01003613 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003614 if (!fb) {
3615 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003616 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003617 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003618
Damien Lespiauc11e9282013-09-25 16:45:30 +01003619 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3620 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003621 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003622
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003623 if (crtc->fb->pixel_format != fb->pixel_format) {
3624 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3625 ret = -EINVAL;
3626 goto out;
3627 }
3628
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003629 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3630 ret = -ENOMEM;
3631 spin_lock_irqsave(&dev->event_lock, flags);
3632 if (file_priv->event_space < sizeof e->event) {
3633 spin_unlock_irqrestore(&dev->event_lock, flags);
3634 goto out;
3635 }
3636 file_priv->event_space -= sizeof e->event;
3637 spin_unlock_irqrestore(&dev->event_lock, flags);
3638
3639 e = kzalloc(sizeof *e, GFP_KERNEL);
3640 if (e == NULL) {
3641 spin_lock_irqsave(&dev->event_lock, flags);
3642 file_priv->event_space += sizeof e->event;
3643 spin_unlock_irqrestore(&dev->event_lock, flags);
3644 goto out;
3645 }
3646
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003647 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003648 e->event.base.length = sizeof e->event;
3649 e->event.user_data = page_flip->user_data;
3650 e->base.event = &e->event.base;
3651 e->base.file_priv = file_priv;
3652 e->base.destroy =
3653 (void (*) (struct drm_pending_event *)) kfree;
3654 }
3655
Daniel Vetterb0d12322012-12-11 01:07:12 +01003656 old_fb = crtc->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07003657 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003658 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003659 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3660 spin_lock_irqsave(&dev->event_lock, flags);
3661 file_priv->event_space += sizeof e->event;
3662 spin_unlock_irqrestore(&dev->event_lock, flags);
3663 kfree(e);
3664 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003665 /* Keep the old fb, don't unref it. */
3666 old_fb = NULL;
3667 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003668 /*
3669 * Warn if the driver hasn't properly updated the crtc->fb
3670 * field to reflect that the new framebuffer is now used.
3671 * Failing to do so will screw with the reference counting
3672 * on framebuffers.
3673 */
3674 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003675 /* Unref only the old framebuffer. */
3676 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003677 }
3678
3679out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003680 if (fb)
3681 drm_framebuffer_unreference(fb);
3682 if (old_fb)
3683 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003684 mutex_unlock(&crtc->mutex);
3685
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003686 return ret;
3687}
Chris Wilsoneb033552011-01-24 15:11:08 +00003688
3689void drm_mode_config_reset(struct drm_device *dev)
3690{
3691 struct drm_crtc *crtc;
3692 struct drm_encoder *encoder;
3693 struct drm_connector *connector;
3694
3695 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3696 if (crtc->funcs->reset)
3697 crtc->funcs->reset(crtc);
3698
3699 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3700 if (encoder->funcs->reset)
3701 encoder->funcs->reset(encoder);
3702
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003703 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3704 connector->status = connector_status_unknown;
3705
Chris Wilsoneb033552011-01-24 15:11:08 +00003706 if (connector->funcs->reset)
3707 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003708 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003709}
3710EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003711
3712int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3713 void *data, struct drm_file *file_priv)
3714{
3715 struct drm_mode_create_dumb *args = data;
3716
3717 if (!dev->driver->dumb_create)
3718 return -ENOSYS;
3719 return dev->driver->dumb_create(file_priv, dev, args);
3720}
3721
3722int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3723 void *data, struct drm_file *file_priv)
3724{
3725 struct drm_mode_map_dumb *args = data;
3726
3727 /* call driver ioctl to get mmap offset */
3728 if (!dev->driver->dumb_map_offset)
3729 return -ENOSYS;
3730
3731 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3732}
3733
3734int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3735 void *data, struct drm_file *file_priv)
3736{
3737 struct drm_mode_destroy_dumb *args = data;
3738
3739 if (!dev->driver->dumb_destroy)
3740 return -ENOSYS;
3741
3742 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3743}
Dave Airlie248dbc22011-11-29 20:02:54 +00003744
3745/*
3746 * Just need to support RGB formats here for compat with code that doesn't
3747 * use pixel formats directly yet.
3748 */
3749void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3750 int *bpp)
3751{
3752 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003753 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003754 case DRM_FORMAT_RGB332:
3755 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003756 *depth = 8;
3757 *bpp = 8;
3758 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003759 case DRM_FORMAT_XRGB1555:
3760 case DRM_FORMAT_XBGR1555:
3761 case DRM_FORMAT_RGBX5551:
3762 case DRM_FORMAT_BGRX5551:
3763 case DRM_FORMAT_ARGB1555:
3764 case DRM_FORMAT_ABGR1555:
3765 case DRM_FORMAT_RGBA5551:
3766 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003767 *depth = 15;
3768 *bpp = 16;
3769 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003770 case DRM_FORMAT_RGB565:
3771 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003772 *depth = 16;
3773 *bpp = 16;
3774 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003775 case DRM_FORMAT_RGB888:
3776 case DRM_FORMAT_BGR888:
3777 *depth = 24;
3778 *bpp = 24;
3779 break;
3780 case DRM_FORMAT_XRGB8888:
3781 case DRM_FORMAT_XBGR8888:
3782 case DRM_FORMAT_RGBX8888:
3783 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003784 *depth = 24;
3785 *bpp = 32;
3786 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003787 case DRM_FORMAT_XRGB2101010:
3788 case DRM_FORMAT_XBGR2101010:
3789 case DRM_FORMAT_RGBX1010102:
3790 case DRM_FORMAT_BGRX1010102:
3791 case DRM_FORMAT_ARGB2101010:
3792 case DRM_FORMAT_ABGR2101010:
3793 case DRM_FORMAT_RGBA1010102:
3794 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003795 *depth = 30;
3796 *bpp = 32;
3797 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003798 case DRM_FORMAT_ARGB8888:
3799 case DRM_FORMAT_ABGR8888:
3800 case DRM_FORMAT_RGBA8888:
3801 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003802 *depth = 32;
3803 *bpp = 32;
3804 break;
3805 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003806 DRM_DEBUG_KMS("unsupported pixel format %s\n",
3807 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00003808 *depth = 0;
3809 *bpp = 0;
3810 break;
3811 }
3812}
3813EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003814
3815/**
3816 * drm_format_num_planes - get the number of planes for format
3817 * @format: pixel format (DRM_FORMAT_*)
3818 *
3819 * RETURNS:
3820 * The number of planes used by the specified pixel format.
3821 */
3822int drm_format_num_planes(uint32_t format)
3823{
3824 switch (format) {
3825 case DRM_FORMAT_YUV410:
3826 case DRM_FORMAT_YVU410:
3827 case DRM_FORMAT_YUV411:
3828 case DRM_FORMAT_YVU411:
3829 case DRM_FORMAT_YUV420:
3830 case DRM_FORMAT_YVU420:
3831 case DRM_FORMAT_YUV422:
3832 case DRM_FORMAT_YVU422:
3833 case DRM_FORMAT_YUV444:
3834 case DRM_FORMAT_YVU444:
3835 return 3;
3836 case DRM_FORMAT_NV12:
3837 case DRM_FORMAT_NV21:
3838 case DRM_FORMAT_NV16:
3839 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003840 case DRM_FORMAT_NV24:
3841 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003842 return 2;
3843 default:
3844 return 1;
3845 }
3846}
3847EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003848
3849/**
3850 * drm_format_plane_cpp - determine the bytes per pixel value
3851 * @format: pixel format (DRM_FORMAT_*)
3852 * @plane: plane index
3853 *
3854 * RETURNS:
3855 * The bytes per pixel value for the specified plane.
3856 */
3857int drm_format_plane_cpp(uint32_t format, int plane)
3858{
3859 unsigned int depth;
3860 int bpp;
3861
3862 if (plane >= drm_format_num_planes(format))
3863 return 0;
3864
3865 switch (format) {
3866 case DRM_FORMAT_YUYV:
3867 case DRM_FORMAT_YVYU:
3868 case DRM_FORMAT_UYVY:
3869 case DRM_FORMAT_VYUY:
3870 return 2;
3871 case DRM_FORMAT_NV12:
3872 case DRM_FORMAT_NV21:
3873 case DRM_FORMAT_NV16:
3874 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003875 case DRM_FORMAT_NV24:
3876 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003877 return plane ? 2 : 1;
3878 case DRM_FORMAT_YUV410:
3879 case DRM_FORMAT_YVU410:
3880 case DRM_FORMAT_YUV411:
3881 case DRM_FORMAT_YVU411:
3882 case DRM_FORMAT_YUV420:
3883 case DRM_FORMAT_YVU420:
3884 case DRM_FORMAT_YUV422:
3885 case DRM_FORMAT_YVU422:
3886 case DRM_FORMAT_YUV444:
3887 case DRM_FORMAT_YVU444:
3888 return 1;
3889 default:
3890 drm_fb_get_bpp_depth(format, &depth, &bpp);
3891 return bpp >> 3;
3892 }
3893}
3894EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003895
3896/**
3897 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3898 * @format: pixel format (DRM_FORMAT_*)
3899 *
3900 * RETURNS:
3901 * The horizontal chroma subsampling factor for the
3902 * specified pixel format.
3903 */
3904int drm_format_horz_chroma_subsampling(uint32_t format)
3905{
3906 switch (format) {
3907 case DRM_FORMAT_YUV411:
3908 case DRM_FORMAT_YVU411:
3909 case DRM_FORMAT_YUV410:
3910 case DRM_FORMAT_YVU410:
3911 return 4;
3912 case DRM_FORMAT_YUYV:
3913 case DRM_FORMAT_YVYU:
3914 case DRM_FORMAT_UYVY:
3915 case DRM_FORMAT_VYUY:
3916 case DRM_FORMAT_NV12:
3917 case DRM_FORMAT_NV21:
3918 case DRM_FORMAT_NV16:
3919 case DRM_FORMAT_NV61:
3920 case DRM_FORMAT_YUV422:
3921 case DRM_FORMAT_YVU422:
3922 case DRM_FORMAT_YUV420:
3923 case DRM_FORMAT_YVU420:
3924 return 2;
3925 default:
3926 return 1;
3927 }
3928}
3929EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3930
3931/**
3932 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3933 * @format: pixel format (DRM_FORMAT_*)
3934 *
3935 * RETURNS:
3936 * The vertical chroma subsampling factor for the
3937 * specified pixel format.
3938 */
3939int drm_format_vert_chroma_subsampling(uint32_t format)
3940{
3941 switch (format) {
3942 case DRM_FORMAT_YUV410:
3943 case DRM_FORMAT_YVU410:
3944 return 4;
3945 case DRM_FORMAT_YUV420:
3946 case DRM_FORMAT_YVU420:
3947 case DRM_FORMAT_NV12:
3948 case DRM_FORMAT_NV21:
3949 return 2;
3950 default:
3951 return 1;
3952 }
3953}
3954EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003955
3956/**
3957 * drm_mode_config_init - initialize DRM mode_configuration structure
3958 * @dev: DRM device
3959 *
3960 * Initialize @dev's mode_config structure, used for tracking the graphics
3961 * configuration of @dev.
3962 *
3963 * Since this initializes the modeset locks, no locking is possible. Which is no
3964 * problem, since this should happen single threaded at init time. It is the
3965 * driver's problem to ensure this guarantee.
3966 *
3967 */
3968void drm_mode_config_init(struct drm_device *dev)
3969{
3970 mutex_init(&dev->mode_config.mutex);
3971 mutex_init(&dev->mode_config.idr_mutex);
3972 mutex_init(&dev->mode_config.fb_lock);
3973 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3974 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3975 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04003976 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003977 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3978 INIT_LIST_HEAD(&dev->mode_config.property_list);
3979 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3980 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3981 idr_init(&dev->mode_config.crtc_idr);
3982
3983 drm_modeset_lock_all(dev);
3984 drm_mode_create_standard_connector_properties(dev);
3985 drm_modeset_unlock_all(dev);
3986
3987 /* Just to be sure */
3988 dev->mode_config.num_fb = 0;
3989 dev->mode_config.num_connector = 0;
3990 dev->mode_config.num_crtc = 0;
3991 dev->mode_config.num_encoder = 0;
3992}
3993EXPORT_SYMBOL(drm_mode_config_init);
3994
3995/**
3996 * drm_mode_config_cleanup - free up DRM mode_config info
3997 * @dev: DRM device
3998 *
3999 * Free up all the connectors and CRTCs associated with this DRM device, then
4000 * free up the framebuffers and associated buffer objects.
4001 *
4002 * Note that since this /should/ happen single-threaded at driver/device
4003 * teardown time, no locking is required. It's the driver's job to ensure that
4004 * this guarantee actually holds true.
4005 *
4006 * FIXME: cleanup any dangling user buffer objects too
4007 */
4008void drm_mode_config_cleanup(struct drm_device *dev)
4009{
4010 struct drm_connector *connector, *ot;
4011 struct drm_crtc *crtc, *ct;
4012 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004013 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004014 struct drm_framebuffer *fb, *fbt;
4015 struct drm_property *property, *pt;
4016 struct drm_property_blob *blob, *bt;
4017 struct drm_plane *plane, *plt;
4018
4019 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4020 head) {
4021 encoder->funcs->destroy(encoder);
4022 }
4023
Sean Paul3b336ec2013-08-14 16:47:37 -04004024 list_for_each_entry_safe(bridge, brt,
4025 &dev->mode_config.bridge_list, head) {
4026 bridge->funcs->destroy(bridge);
4027 }
4028
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004029 list_for_each_entry_safe(connector, ot,
4030 &dev->mode_config.connector_list, head) {
4031 connector->funcs->destroy(connector);
4032 }
4033
4034 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4035 head) {
4036 drm_property_destroy(dev, property);
4037 }
4038
4039 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4040 head) {
4041 drm_property_destroy_blob(dev, blob);
4042 }
4043
4044 /*
4045 * Single-threaded teardown context, so it's not required to grab the
4046 * fb_lock to protect against concurrent fb_list access. Contrary, it
4047 * would actually deadlock with the drm_framebuffer_cleanup function.
4048 *
4049 * Also, if there are any framebuffers left, that's a driver leak now,
4050 * so politely WARN about this.
4051 */
4052 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4053 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4054 drm_framebuffer_remove(fb);
4055 }
4056
4057 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4058 head) {
4059 plane->funcs->destroy(plane);
4060 }
4061
4062 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4063 crtc->funcs->destroy(crtc);
4064 }
4065
4066 idr_destroy(&dev->mode_config.crtc_idr);
4067}
4068EXPORT_SYMBOL(drm_mode_config_cleanup);