blob: 35ea15d5ffffed35a4f494afe6df9a1a4e89a8ae [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 Vetter84849902012-12-02 00:28:11 +010041/**
42 * drm_modeset_lock_all - take all modeset locks
43 * @dev: drm device
44 *
45 * This function takes all modeset locks, suitable where a more fine-grained
46 * scheme isn't (yet) implemented.
47 */
48void drm_modeset_lock_all(struct drm_device *dev)
49{
Daniel Vetter29494c12012-12-02 02:18:25 +010050 struct drm_crtc *crtc;
51
Daniel Vetter84849902012-12-02 00:28:11 +010052 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter29494c12012-12-02 02:18:25 +010053
54 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
55 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Daniel Vetter84849902012-12-02 00:28:11 +010056}
57EXPORT_SYMBOL(drm_modeset_lock_all);
58
59/**
60 * drm_modeset_unlock_all - drop all modeset locks
61 * @dev: device
62 */
63void drm_modeset_unlock_all(struct drm_device *dev)
64{
Daniel Vetter29494c12012-12-02 02:18:25 +010065 struct drm_crtc *crtc;
66
67 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
68 mutex_unlock(&crtc->mutex);
69
Daniel Vetter84849902012-12-02 00:28:11 +010070 mutex_unlock(&dev->mode_config.mutex);
71}
72EXPORT_SYMBOL(drm_modeset_unlock_all);
73
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010074/**
75 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
76 * @dev: device
77 */
78void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
79{
80 struct drm_crtc *crtc;
81
Daniel Vettera9b054e2013-05-02 09:43:05 +020082 /* Locking is currently fubar in the panic handler. */
83 if (oops_in_progress)
84 return;
85
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010086 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
87 WARN_ON(!mutex_is_locked(&crtc->mutex));
88
89 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
90}
91EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
92
Dave Airlief453ba02008-11-07 14:05:41 -080093/* Avoid boilerplate. I'm tired of typing. */
94#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000095 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080096 { \
97 int i; \
98 for (i = 0; i < ARRAY_SIZE(list); i++) { \
99 if (list[i].type == val) \
100 return list[i].name; \
101 } \
102 return "(unknown)"; \
103 }
104
105/*
106 * Global properties
107 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000108static const struct drm_prop_enum_list drm_dpms_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800109{ { DRM_MODE_DPMS_ON, "On" },
110 { DRM_MODE_DPMS_STANDBY, "Standby" },
111 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
112 { DRM_MODE_DPMS_OFF, "Off" }
113};
114
115DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
116
117/*
118 * Optional properties
119 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000120static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800121{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700122 { DRM_MODE_SCALE_NONE, "None" },
123 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
124 { DRM_MODE_SCALE_CENTER, "Center" },
125 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800126};
127
Dave Airlief453ba02008-11-07 14:05:41 -0800128/*
129 * Non-global properties, but "required" for certain connectors.
130 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000131static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800132{
133 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
134 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
135 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
136};
137
138DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
139
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000140static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800141{
142 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
143 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
144 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
145};
146
147DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
148 drm_dvi_i_subconnector_enum_list)
149
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000150static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800151{
152 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
153 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
154 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
155 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200156 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800157};
158
159DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
160
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000161static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800162{
163 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
164 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
165 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
166 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200167 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800168};
169
170DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
171 drm_tv_subconnector_enum_list)
172
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000173static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000174 { DRM_MODE_DIRTY_OFF, "Off" },
175 { DRM_MODE_DIRTY_ON, "On" },
176 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
177};
178
Dave Airlief453ba02008-11-07 14:05:41 -0800179struct drm_conn_prop_enum_list {
180 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000181 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400182 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800183};
184
185/*
186 * Connector and encoder types.
187 */
188static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400189{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
190 { DRM_MODE_CONNECTOR_VGA, "VGA" },
191 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
192 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
193 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
194 { DRM_MODE_CONNECTOR_Composite, "Composite" },
195 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
196 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
197 { DRM_MODE_CONNECTOR_Component, "Component" },
198 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
199 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
200 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
201 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
202 { DRM_MODE_CONNECTOR_TV, "TV" },
203 { DRM_MODE_CONNECTOR_eDP, "eDP" },
204 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300205 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800206};
207
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000208static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800209{ { DRM_MODE_ENCODER_NONE, "None" },
210 { DRM_MODE_ENCODER_DAC, "DAC" },
211 { DRM_MODE_ENCODER_TMDS, "TMDS" },
212 { DRM_MODE_ENCODER_LVDS, "LVDS" },
213 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200214 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300215 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800216};
217
Jesse Barnesac1bb362014-02-10 15:32:44 -0800218static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
219{
220 { SubPixelUnknown, "Unknown" },
221 { SubPixelHorizontalRGB, "Horizontal RGB" },
222 { SubPixelHorizontalBGR, "Horizontal BGR" },
223 { SubPixelVerticalRGB, "Vertical RGB" },
224 { SubPixelVerticalBGR, "Vertical BGR" },
225 { SubPixelNone, "None" },
226};
227
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400228void drm_connector_ida_init(void)
229{
230 int i;
231
232 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
233 ida_init(&drm_connector_enum_list[i].ida);
234}
235
236void drm_connector_ida_destroy(void)
237{
238 int i;
239
240 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
241 ida_destroy(&drm_connector_enum_list[i].ida);
242}
243
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000244const char *drm_get_encoder_name(const struct drm_encoder *encoder)
Dave Airlief453ba02008-11-07 14:05:41 -0800245{
246 static char buf[32];
247
248 snprintf(buf, 32, "%s-%d",
249 drm_encoder_enum_list[encoder->encoder_type].name,
250 encoder->base.id);
251 return buf;
252}
Dave Airlie13a81952009-09-07 15:45:33 +1000253EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800254
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000255const char *drm_get_connector_name(const struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -0800256{
257 static char buf[32];
258
259 snprintf(buf, 32, "%s-%d",
260 drm_connector_enum_list[connector->connector_type].name,
261 connector->connector_type_id);
262 return buf;
263}
264EXPORT_SYMBOL(drm_get_connector_name);
265
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000266const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800267{
268 if (status == connector_status_connected)
269 return "connected";
270 else if (status == connector_status_disconnected)
271 return "disconnected";
272 else
273 return "unknown";
274}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000275EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800276
Jesse Barnesac1bb362014-02-10 15:32:44 -0800277/**
278 * drm_get_subpixel_order_name - return a string for a given subpixel enum
279 * @order: enum of subpixel_order
280 *
281 * Note you could abuse this and return something out of bounds, but that
282 * would be a caller error. No unscrubbed user data should make it here.
283 */
284const char *drm_get_subpixel_order_name(enum subpixel_order order)
285{
286 return drm_subpixel_enum_list[order].name;
287}
288EXPORT_SYMBOL(drm_get_subpixel_order_name);
289
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300290static char printable_char(int c)
291{
292 return isascii(c) && isprint(c) ? c : '?';
293}
294
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000295const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300296{
297 static char buf[32];
298
299 snprintf(buf, sizeof(buf),
300 "%c%c%c%c %s-endian (0x%08x)",
301 printable_char(format & 0xff),
302 printable_char((format >> 8) & 0xff),
303 printable_char((format >> 16) & 0xff),
304 printable_char((format >> 24) & 0x7f),
305 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
306 format);
307
308 return buf;
309}
310EXPORT_SYMBOL(drm_get_format_name);
311
Dave Airlief453ba02008-11-07 14:05:41 -0800312/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100313 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800314 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100315 * @obj: object pointer, used to generate unique ID
316 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800317 *
Dave Airlief453ba02008-11-07 14:05:41 -0800318 * Create a unique identifier based on @ptr in @dev's identifier space. Used
319 * for tracking modes, CRTCs and connectors.
320 *
321 * RETURNS:
322 * New unique (relative to other objects in @dev) integer identifier for the
323 * object.
324 */
325static int drm_mode_object_get(struct drm_device *dev,
326 struct drm_mode_object *obj, uint32_t obj_type)
327{
Dave Airlief453ba02008-11-07 14:05:41 -0800328 int ret;
329
Jesse Barnesad2563c2009-01-19 17:21:45 +1000330 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800331 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
332 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100333 /*
334 * Set up the object linking under the protection of the idr
335 * lock so that other users can't see inconsistent state.
336 */
Tejun Heo2e928812013-02-27 17:04:08 -0800337 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100338 obj->type = obj_type;
339 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000340 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100341
Tejun Heo2e928812013-02-27 17:04:08 -0800342 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800343}
344
345/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100346 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800347 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100348 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800349 *
Dave Airlief453ba02008-11-07 14:05:41 -0800350 * Free @id from @dev's unique identifier pool.
351 */
352static void drm_mode_object_put(struct drm_device *dev,
353 struct drm_mode_object *object)
354{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000355 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800356 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000357 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800358}
359
Daniel Vetter786b99e2012-12-02 21:53:40 +0100360/**
361 * drm_mode_object_find - look up a drm object with static lifetime
362 * @dev: drm device
363 * @id: id of the mode object
364 * @type: type of the mode object
365 *
366 * Note that framebuffers cannot be looked up with this functions - since those
367 * are reference counted, they need special treatment.
368 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200369struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
370 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800371{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000372 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800373
Daniel Vetter786b99e2012-12-02 21:53:40 +0100374 /* Framebuffers are reference counted and need their own lookup
375 * function.*/
376 WARN_ON(type == DRM_MODE_OBJECT_FB);
377
Jesse Barnesad2563c2009-01-19 17:21:45 +1000378 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800379 obj = idr_find(&dev->mode_config.crtc_idr, id);
380 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000381 obj = NULL;
382 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800383
384 return obj;
385}
386EXPORT_SYMBOL(drm_mode_object_find);
387
388/**
Dave Airlief453ba02008-11-07 14:05:41 -0800389 * drm_framebuffer_init - initialize a framebuffer
390 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100391 * @fb: framebuffer to be initialized
392 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800393 *
Dave Airlief453ba02008-11-07 14:05:41 -0800394 * Allocates an ID for the framebuffer's parent mode object, sets its mode
395 * functions & device file and adds it to the master fd list.
396 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100397 * IMPORTANT:
398 * This functions publishes the fb and makes it available for concurrent access
399 * by other users. Which means by this point the fb _must_ be fully set up -
400 * since all the fb attributes are invariant over its lifetime, no further
401 * locking but only correct reference counting is required.
402 *
Dave Airlief453ba02008-11-07 14:05:41 -0800403 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200404 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800405 */
406int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
407 const struct drm_framebuffer_funcs *funcs)
408{
409 int ret;
410
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100411 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000412 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100413 INIT_LIST_HEAD(&fb->filp_head);
414 fb->dev = dev;
415 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000416
Dave Airlief453ba02008-11-07 14:05:41 -0800417 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200418 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100419 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800420
Daniel Vetter2b677e82012-12-10 21:16:05 +0100421 /* Grab the idr reference. */
422 drm_framebuffer_reference(fb);
423
Dave Airlief453ba02008-11-07 14:05:41 -0800424 dev->mode_config.num_fb++;
425 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100426out:
427 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800428
429 return 0;
430}
431EXPORT_SYMBOL(drm_framebuffer_init);
432
Rob Clarkf7eff602012-09-05 21:48:38 +0000433static void drm_framebuffer_free(struct kref *kref)
434{
435 struct drm_framebuffer *fb =
436 container_of(kref, struct drm_framebuffer, refcount);
437 fb->funcs->destroy(fb);
438}
439
Daniel Vetter2b677e82012-12-10 21:16:05 +0100440static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
441 uint32_t id)
442{
443 struct drm_mode_object *obj = NULL;
444 struct drm_framebuffer *fb;
445
446 mutex_lock(&dev->mode_config.idr_mutex);
447 obj = idr_find(&dev->mode_config.crtc_idr, id);
448 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
449 fb = NULL;
450 else
451 fb = obj_to_fb(obj);
452 mutex_unlock(&dev->mode_config.idr_mutex);
453
454 return fb;
455}
456
Rob Clarkf7eff602012-09-05 21:48:38 +0000457/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100458 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
459 * @dev: drm device
460 * @id: id of the fb object
461 *
462 * If successful, this grabs an additional reference to the framebuffer -
463 * callers need to make sure to eventually unreference the returned framebuffer
464 * again.
465 */
466struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
467 uint32_t id)
468{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100469 struct drm_framebuffer *fb;
470
471 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100472 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100473 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000474 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100475 mutex_unlock(&dev->mode_config.fb_lock);
476
477 return fb;
478}
479EXPORT_SYMBOL(drm_framebuffer_lookup);
480
481/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000482 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100483 * @fb: framebuffer to unref
484 *
485 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000486 */
487void drm_framebuffer_unreference(struct drm_framebuffer *fb)
488{
Rob Clarkf7eff602012-09-05 21:48:38 +0000489 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000490 kref_put(&fb->refcount, drm_framebuffer_free);
491}
492EXPORT_SYMBOL(drm_framebuffer_unreference);
493
494/**
495 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100496 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000497 */
498void drm_framebuffer_reference(struct drm_framebuffer *fb)
499{
500 DRM_DEBUG("FB ID: %d\n", fb->base.id);
501 kref_get(&fb->refcount);
502}
503EXPORT_SYMBOL(drm_framebuffer_reference);
504
Daniel Vetter2b677e82012-12-10 21:16:05 +0100505static void drm_framebuffer_free_bug(struct kref *kref)
506{
507 BUG();
508}
509
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100510static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
511{
512 DRM_DEBUG("FB ID: %d\n", fb->base.id);
513 kref_put(&fb->refcount, drm_framebuffer_free_bug);
514}
515
Daniel Vetter2b677e82012-12-10 21:16:05 +0100516/* dev->mode_config.fb_lock must be held! */
517static void __drm_framebuffer_unregister(struct drm_device *dev,
518 struct drm_framebuffer *fb)
519{
520 mutex_lock(&dev->mode_config.idr_mutex);
521 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
522 mutex_unlock(&dev->mode_config.idr_mutex);
523
524 fb->base.id = 0;
525
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100526 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100527}
528
Dave Airlief453ba02008-11-07 14:05:41 -0800529/**
Daniel Vetter36206362012-12-10 20:42:17 +0100530 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
531 * @fb: fb to unregister
532 *
533 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
534 * those used for fbdev. Note that the caller must hold a reference of it's own,
535 * i.e. the object may not be destroyed through this call (since it'll lead to a
536 * locking inversion).
537 */
538void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
539{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100540 struct drm_device *dev = fb->dev;
541
542 mutex_lock(&dev->mode_config.fb_lock);
543 /* Mark fb as reaped and drop idr ref. */
544 __drm_framebuffer_unregister(dev, fb);
545 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100546}
547EXPORT_SYMBOL(drm_framebuffer_unregister_private);
548
549/**
Dave Airlief453ba02008-11-07 14:05:41 -0800550 * drm_framebuffer_cleanup - remove a framebuffer object
551 * @fb: framebuffer to remove
552 *
Daniel Vetter36206362012-12-10 20:42:17 +0100553 * Cleanup references to a user-created framebuffer. This function is intended
554 * to be used from the drivers ->destroy callback.
555 *
556 * Note that this function does not remove the fb from active usuage - if it is
557 * still used anywhere, hilarity can ensue since userspace could call getfb on
558 * the id and get back -EINVAL. Obviously no concern at driver unload time.
559 *
560 * Also, the framebuffer will not be removed from the lookup idr - for
561 * user-created framebuffers this will happen in in the rmfb ioctl. For
562 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
563 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800564 */
565void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
566{
567 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100568
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100569 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000570 list_del(&fb->head);
571 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100572 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000573}
574EXPORT_SYMBOL(drm_framebuffer_cleanup);
575
576/**
577 * drm_framebuffer_remove - remove and unreference a framebuffer object
578 * @fb: framebuffer to remove
579 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000580 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100581 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100582 * passed-in framebuffer. Might take the modeset locks.
583 *
584 * Note that this function optimizes the cleanup away if the caller holds the
585 * last reference to the framebuffer. It is also guaranteed to not take the
586 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000587 */
588void drm_framebuffer_remove(struct drm_framebuffer *fb)
589{
590 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800591 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800592 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000593 struct drm_mode_set set;
594 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800595
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100596 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100597
Daniel Vetterb62584e2012-12-11 16:51:35 +0100598 /*
599 * drm ABI mandates that we remove any deleted framebuffers from active
600 * useage. But since most sane clients only remove framebuffers they no
601 * longer need, try to optimize this away.
602 *
603 * Since we're holding a reference ourselves, observing a refcount of 1
604 * means that we're the last holder and can skip it. Also, the refcount
605 * can never increase from 1 again, so we don't need any barriers or
606 * locks.
607 *
608 * Note that userspace could try to race with use and instate a new
609 * usage _after_ we've cleared all current ones. End result will be an
610 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
611 * in this manner.
612 */
613 if (atomic_read(&fb->refcount.refcount) > 1) {
614 drm_modeset_lock_all(dev);
615 /* remove from any CRTC */
616 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
617 if (crtc->fb == fb) {
618 /* should turn off the crtc */
619 memset(&set, 0, sizeof(struct drm_mode_set));
620 set.crtc = crtc;
621 set.fb = NULL;
622 ret = drm_mode_set_config_internal(&set);
623 if (ret)
624 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
625 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000626 }
Dave Airlief453ba02008-11-07 14:05:41 -0800627
Daniel Vetterb62584e2012-12-11 16:51:35 +0100628 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300629 if (plane->fb == fb)
630 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800631 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100632 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800633 }
634
Rob Clarkf7eff602012-09-05 21:48:38 +0000635 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800636}
Rob Clarkf7eff602012-09-05 21:48:38 +0000637EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800638
639/**
640 * drm_crtc_init - Initialise a new CRTC object
641 * @dev: DRM device
642 * @crtc: CRTC object to init
643 * @funcs: callbacks for the new CRTC
644 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000645 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200646 *
647 * RETURNS:
648 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800649 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200650int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800651 const struct drm_crtc_funcs *funcs)
652{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200653 int ret;
654
Dave Airlief453ba02008-11-07 14:05:41 -0800655 crtc->dev = dev;
656 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000657 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800658
Daniel Vetter84849902012-12-02 00:28:11 +0100659 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100660 mutex_init(&crtc->mutex);
661 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200662
663 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
664 if (ret)
665 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800666
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300667 crtc->base.properties = &crtc->properties;
668
Dave Airlief453ba02008-11-07 14:05:41 -0800669 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
670 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200671
672 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100673 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200674
675 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800676}
677EXPORT_SYMBOL(drm_crtc_init);
678
679/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000680 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800681 * @crtc: CRTC to cleanup
682 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000683 * This function cleans up @crtc and removes it from the DRM mode setting
684 * core. Note that the function does *not* free the crtc structure itself,
685 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800686 */
687void drm_crtc_cleanup(struct drm_crtc *crtc)
688{
689 struct drm_device *dev = crtc->dev;
690
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000691 kfree(crtc->gamma_store);
692 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800693
694 drm_mode_object_put(dev, &crtc->base);
695 list_del(&crtc->head);
696 dev->mode_config.num_crtc--;
697}
698EXPORT_SYMBOL(drm_crtc_cleanup);
699
700/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000701 * drm_crtc_index - find the index of a registered CRTC
702 * @crtc: CRTC to find index for
703 *
704 * Given a registered CRTC, return the index of that CRTC within a DRM
705 * device's list of CRTCs.
706 */
707unsigned int drm_crtc_index(struct drm_crtc *crtc)
708{
709 unsigned int index = 0;
710 struct drm_crtc *tmp;
711
712 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
713 if (tmp == crtc)
714 return index;
715
716 index++;
717 }
718
719 BUG();
720}
721EXPORT_SYMBOL(drm_crtc_index);
722
723/**
Dave Airlief453ba02008-11-07 14:05:41 -0800724 * drm_mode_probed_add - add a mode to a connector's probed mode list
725 * @connector: connector the new mode
726 * @mode: mode data
727 *
Dave Airlief453ba02008-11-07 14:05:41 -0800728 * Add @mode to @connector's mode list for later use.
729 */
730void drm_mode_probed_add(struct drm_connector *connector,
731 struct drm_display_mode *mode)
732{
Ville Syrjälä990256a2013-05-31 12:17:07 +0000733 list_add_tail(&mode->head, &connector->probed_modes);
Dave Airlief453ba02008-11-07 14:05:41 -0800734}
735EXPORT_SYMBOL(drm_mode_probed_add);
736
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100737/*
Dave Airlief453ba02008-11-07 14:05:41 -0800738 * drm_mode_remove - remove and free a mode
739 * @connector: connector list to modify
740 * @mode: mode to remove
741 *
Dave Airlief453ba02008-11-07 14:05:41 -0800742 * Remove @mode from @connector's mode list, then free it.
743 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100744static void drm_mode_remove(struct drm_connector *connector,
745 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800746{
747 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100748 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800749}
Dave Airlief453ba02008-11-07 14:05:41 -0800750
751/**
752 * drm_connector_init - Init a preallocated connector
753 * @dev: DRM device
754 * @connector: the connector to init
755 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100756 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800757 *
Dave Airlief453ba02008-11-07 14:05:41 -0800758 * Initialises a preallocated connector. Connectors should be
759 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200760 *
761 * RETURNS:
762 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800763 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200764int drm_connector_init(struct drm_device *dev,
765 struct drm_connector *connector,
766 const struct drm_connector_funcs *funcs,
767 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800768{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200769 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400770 struct ida *connector_ida =
771 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200772
Daniel Vetter84849902012-12-02 00:28:11 +0100773 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800774
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200775 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
776 if (ret)
777 goto out;
778
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300779 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800780 connector->dev = dev;
781 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800782 connector->connector_type = connector_type;
783 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400784 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
785 if (connector->connector_type_id < 0) {
786 ret = connector->connector_type_id;
787 drm_mode_object_put(dev, &connector->base);
788 goto out;
789 }
Dave Airlief453ba02008-11-07 14:05:41 -0800790 INIT_LIST_HEAD(&connector->probed_modes);
791 INIT_LIST_HEAD(&connector->modes);
792 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000793 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800794
795 list_add_tail(&connector->head, &dev->mode_config.connector_list);
796 dev->mode_config.num_connector++;
797
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200798 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500799 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200800 dev->mode_config.edid_property,
801 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800802
Rob Clark58495562012-10-11 20:50:56 -0500803 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800804 dev->mode_config.dpms_property, 0);
805
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200806 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100807 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200808
809 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800810}
811EXPORT_SYMBOL(drm_connector_init);
812
813/**
814 * drm_connector_cleanup - cleans up an initialised connector
815 * @connector: connector to cleanup
816 *
Dave Airlief453ba02008-11-07 14:05:41 -0800817 * Cleans up the connector but doesn't free the object.
818 */
819void drm_connector_cleanup(struct drm_connector *connector)
820{
821 struct drm_device *dev = connector->dev;
822 struct drm_display_mode *mode, *t;
823
824 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
825 drm_mode_remove(connector, mode);
826
827 list_for_each_entry_safe(mode, t, &connector->modes, head)
828 drm_mode_remove(connector, mode);
829
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400830 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
831 connector->connector_type_id);
832
Dave Airlief453ba02008-11-07 14:05:41 -0800833 drm_mode_object_put(dev, &connector->base);
834 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000835 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800836}
837EXPORT_SYMBOL(drm_connector_cleanup);
838
Dave Airliecbc7e222012-02-20 14:16:40 +0000839void drm_connector_unplug_all(struct drm_device *dev)
840{
841 struct drm_connector *connector;
842
843 /* taking the mode config mutex ends up in a clash with sysfs */
844 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
845 drm_sysfs_connector_remove(connector);
846
847}
848EXPORT_SYMBOL(drm_connector_unplug_all);
849
Sean Paul3b336ec2013-08-14 16:47:37 -0400850int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
851 const struct drm_bridge_funcs *funcs)
852{
853 int ret;
854
855 drm_modeset_lock_all(dev);
856
857 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
858 if (ret)
859 goto out;
860
861 bridge->dev = dev;
862 bridge->funcs = funcs;
863
864 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
865 dev->mode_config.num_bridge++;
866
867 out:
868 drm_modeset_unlock_all(dev);
869 return ret;
870}
871EXPORT_SYMBOL(drm_bridge_init);
872
873void drm_bridge_cleanup(struct drm_bridge *bridge)
874{
875 struct drm_device *dev = bridge->dev;
876
877 drm_modeset_lock_all(dev);
878 drm_mode_object_put(dev, &bridge->base);
879 list_del(&bridge->head);
880 dev->mode_config.num_bridge--;
881 drm_modeset_unlock_all(dev);
882}
883EXPORT_SYMBOL(drm_bridge_cleanup);
884
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200885int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000886 struct drm_encoder *encoder,
887 const struct drm_encoder_funcs *funcs,
888 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800889{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200890 int ret;
891
Daniel Vetter84849902012-12-02 00:28:11 +0100892 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800893
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200894 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
895 if (ret)
896 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800897
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200898 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800899 encoder->encoder_type = encoder_type;
900 encoder->funcs = funcs;
901
902 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
903 dev->mode_config.num_encoder++;
904
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200905 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100906 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200907
908 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800909}
910EXPORT_SYMBOL(drm_encoder_init);
911
912void drm_encoder_cleanup(struct drm_encoder *encoder)
913{
914 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100915 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800916 drm_mode_object_put(dev, &encoder->base);
917 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000918 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100919 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800920}
921EXPORT_SYMBOL(drm_encoder_cleanup);
922
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300923/**
924 * drm_plane_init - Initialise a new plane object
925 * @dev: DRM device
926 * @plane: plane object to init
927 * @possible_crtcs: bitmask of possible CRTCs
928 * @funcs: callbacks for the new plane
929 * @formats: array of supported formats (%DRM_FORMAT_*)
930 * @format_count: number of elements in @formats
931 * @priv: plane is private (hidden from userspace)?
932 *
933 * Inits a new object created as base part of a driver plane object.
934 *
935 * RETURNS:
936 * Zero on success, error code on failure.
937 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800938int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
939 unsigned long possible_crtcs,
940 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600941 const uint32_t *formats, uint32_t format_count,
942 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800943{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200944 int ret;
945
Daniel Vetter84849902012-12-02 00:28:11 +0100946 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800947
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200948 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
949 if (ret)
950 goto out;
951
Rob Clark4d939142012-05-17 02:23:27 -0600952 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800953 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800954 plane->funcs = funcs;
955 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
956 GFP_KERNEL);
957 if (!plane->format_types) {
958 DRM_DEBUG_KMS("out of memory when allocating plane\n");
959 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200960 ret = -ENOMEM;
961 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800962 }
963
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800964 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800965 plane->format_count = format_count;
966 plane->possible_crtcs = possible_crtcs;
967
Rob Clark0a7eb242011-12-13 20:19:36 -0600968 /* private planes are not exposed to userspace, but depending on
969 * display hardware, might be convenient to allow sharing programming
970 * for the scanout engine with the crtc implementation.
971 */
972 if (!priv) {
973 list_add_tail(&plane->head, &dev->mode_config.plane_list);
974 dev->mode_config.num_plane++;
975 } else {
976 INIT_LIST_HEAD(&plane->head);
977 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800978
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200979 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100980 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800981
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200982 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800983}
984EXPORT_SYMBOL(drm_plane_init);
985
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300986/**
987 * drm_plane_cleanup - Clean up the core plane usage
988 * @plane: plane to cleanup
989 *
990 * This function cleans up @plane and removes it from the DRM mode setting
991 * core. Note that the function does *not* free the plane structure itself,
992 * this is the responsibility of the caller.
993 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800994void drm_plane_cleanup(struct drm_plane *plane)
995{
996 struct drm_device *dev = plane->dev;
997
Daniel Vetter84849902012-12-02 00:28:11 +0100998 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800999 kfree(plane->format_types);
1000 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -06001001 /* if not added to a list, it must be a private plane */
1002 if (!list_empty(&plane->head)) {
1003 list_del(&plane->head);
1004 dev->mode_config.num_plane--;
1005 }
Daniel Vetter84849902012-12-02 00:28:11 +01001006 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001007}
1008EXPORT_SYMBOL(drm_plane_cleanup);
1009
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001010/**
1011 * drm_plane_force_disable - Forcibly disable a plane
1012 * @plane: plane to disable
1013 *
1014 * Forces the plane to be disabled.
1015 *
1016 * Used when the plane's current framebuffer is destroyed,
1017 * and when restoring fbdev mode.
1018 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001019void drm_plane_force_disable(struct drm_plane *plane)
1020{
1021 int ret;
1022
1023 if (!plane->fb)
1024 return;
1025
1026 ret = plane->funcs->disable_plane(plane);
1027 if (ret)
1028 DRM_ERROR("failed to disable plane with busy fb\n");
1029 /* disconnect the plane from the fb and crtc: */
1030 __drm_framebuffer_unreference(plane->fb);
1031 plane->fb = NULL;
1032 plane->crtc = NULL;
1033}
1034EXPORT_SYMBOL(drm_plane_force_disable);
1035
Dave Airlief453ba02008-11-07 14:05:41 -08001036/**
1037 * drm_mode_create - create a new display mode
1038 * @dev: DRM device
1039 *
Dave Airlief453ba02008-11-07 14:05:41 -08001040 * Create a new drm_display_mode, give it an ID, and return it.
1041 *
1042 * RETURNS:
1043 * Pointer to new mode on success, NULL on error.
1044 */
1045struct drm_display_mode *drm_mode_create(struct drm_device *dev)
1046{
1047 struct drm_display_mode *nmode;
1048
1049 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
1050 if (!nmode)
1051 return NULL;
1052
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001053 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
1054 kfree(nmode);
1055 return NULL;
1056 }
1057
Dave Airlief453ba02008-11-07 14:05:41 -08001058 return nmode;
1059}
1060EXPORT_SYMBOL(drm_mode_create);
1061
1062/**
1063 * drm_mode_destroy - remove a mode
1064 * @dev: DRM device
1065 * @mode: mode to remove
1066 *
Dave Airlief453ba02008-11-07 14:05:41 -08001067 * Free @mode's unique identifier, then free it.
1068 */
1069void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
1070{
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001071 if (!mode)
1072 return;
1073
Dave Airlief453ba02008-11-07 14:05:41 -08001074 drm_mode_object_put(dev, &mode->base);
1075
1076 kfree(mode);
1077}
1078EXPORT_SYMBOL(drm_mode_destroy);
1079
1080static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1081{
1082 struct drm_property *edid;
1083 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001084
1085 /*
1086 * Standard properties (apply to all connectors)
1087 */
1088 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1089 DRM_MODE_PROP_IMMUTABLE,
1090 "EDID", 0);
1091 dev->mode_config.edid_property = edid;
1092
Sascha Hauer4a67d392012-02-06 10:58:17 +01001093 dpms = drm_property_create_enum(dev, 0,
1094 "DPMS", drm_dpms_enum_list,
1095 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001096 dev->mode_config.dpms_property = dpms;
1097
1098 return 0;
1099}
1100
1101/**
1102 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1103 * @dev: DRM device
1104 *
1105 * Called by a driver the first time a DVI-I connector is made.
1106 */
1107int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1108{
1109 struct drm_property *dvi_i_selector;
1110 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001111
1112 if (dev->mode_config.dvi_i_select_subconnector_property)
1113 return 0;
1114
1115 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001116 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001117 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001118 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001119 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001120 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1121
Sascha Hauer4a67d392012-02-06 10:58:17 +01001122 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001123 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001124 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001125 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001126 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1127
1128 return 0;
1129}
1130EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1131
1132/**
1133 * drm_create_tv_properties - create TV specific connector properties
1134 * @dev: DRM device
1135 * @num_modes: number of different TV formats (modes) supported
1136 * @modes: array of pointers to strings containing name of each format
1137 *
1138 * Called by a driver's TV initialization routine, this function creates
1139 * the TV specific connector properties for a given device. Caller is
1140 * responsible for allocating a list of format names and passing them to
1141 * this routine.
1142 */
1143int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1144 char *modes[])
1145{
1146 struct drm_property *tv_selector;
1147 struct drm_property *tv_subconnector;
1148 int i;
1149
1150 if (dev->mode_config.tv_select_subconnector_property)
1151 return 0;
1152
1153 /*
1154 * Basic connector properties
1155 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001156 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001157 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001158 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001159 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001160 dev->mode_config.tv_select_subconnector_property = tv_selector;
1161
1162 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001163 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1164 "subconnector",
1165 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001166 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001167 dev->mode_config.tv_subconnector_property = tv_subconnector;
1168
1169 /*
1170 * Other, TV specific properties: margins & TV modes.
1171 */
1172 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001173 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001174
1175 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001176 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001177
1178 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001179 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001180
1181 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001182 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001183
1184 dev->mode_config.tv_mode_property =
1185 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1186 "mode", num_modes);
1187 for (i = 0; i < num_modes; i++)
1188 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1189 i, modes[i]);
1190
Francisco Jerezb6b79022009-08-02 04:19:20 +02001191 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001192 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001193
1194 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001195 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001196
1197 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001198 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001199
Francisco Jereza75f0232009-08-12 02:30:10 +02001200 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001201 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001202
1203 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001204 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001205
1206 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001207 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001208
Dave Airlief453ba02008-11-07 14:05:41 -08001209 return 0;
1210}
1211EXPORT_SYMBOL(drm_mode_create_tv_properties);
1212
1213/**
1214 * drm_mode_create_scaling_mode_property - create scaling mode property
1215 * @dev: DRM device
1216 *
1217 * Called by a driver the first time it's needed, must be attached to desired
1218 * connectors.
1219 */
1220int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1221{
1222 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001223
1224 if (dev->mode_config.scaling_mode_property)
1225 return 0;
1226
1227 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001228 drm_property_create_enum(dev, 0, "scaling mode",
1229 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001230 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001231
1232 dev->mode_config.scaling_mode_property = scaling_mode;
1233
1234 return 0;
1235}
1236EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1237
1238/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001239 * drm_mode_create_dirty_property - create dirty property
1240 * @dev: DRM device
1241 *
1242 * Called by a driver the first time it's needed, must be attached to desired
1243 * connectors.
1244 */
1245int drm_mode_create_dirty_info_property(struct drm_device *dev)
1246{
1247 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001248
1249 if (dev->mode_config.dirty_info_property)
1250 return 0;
1251
1252 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001253 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001254 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001255 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001256 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001257 dev->mode_config.dirty_info_property = dirty_info;
1258
1259 return 0;
1260}
1261EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1262
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001263static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001264{
1265 uint32_t total_objects = 0;
1266
1267 total_objects += dev->mode_config.num_crtc;
1268 total_objects += dev->mode_config.num_connector;
1269 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001270 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001271
Dave Airlief453ba02008-11-07 14:05:41 -08001272 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1273 if (!group->id_list)
1274 return -ENOMEM;
1275
1276 group->num_crtcs = 0;
1277 group->num_connectors = 0;
1278 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001279 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001280 return 0;
1281}
1282
1283int drm_mode_group_init_legacy_group(struct drm_device *dev,
1284 struct drm_mode_group *group)
1285{
1286 struct drm_crtc *crtc;
1287 struct drm_encoder *encoder;
1288 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001289 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001290 int ret;
1291
1292 if ((ret = drm_mode_group_init(dev, group)))
1293 return ret;
1294
1295 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1296 group->id_list[group->num_crtcs++] = crtc->base.id;
1297
1298 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1299 group->id_list[group->num_crtcs + group->num_encoders++] =
1300 encoder->base.id;
1301
1302 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1303 group->id_list[group->num_crtcs + group->num_encoders +
1304 group->num_connectors++] = connector->base.id;
1305
Sean Paul3b336ec2013-08-14 16:47:37 -04001306 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1307 group->id_list[group->num_crtcs + group->num_encoders +
1308 group->num_connectors + group->num_bridges++] =
1309 bridge->base.id;
1310
Dave Airlief453ba02008-11-07 14:05:41 -08001311 return 0;
1312}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001313EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001314
1315/**
Dave Airlief453ba02008-11-07 14:05:41 -08001316 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1317 * @out: drm_mode_modeinfo struct to return to the user
1318 * @in: drm_display_mode to use
1319 *
Dave Airlief453ba02008-11-07 14:05:41 -08001320 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1321 * the user.
1322 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001323static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1324 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001325{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001326 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1327 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1328 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1329 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1330 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1331 "timing values too large for mode info\n");
1332
Dave Airlief453ba02008-11-07 14:05:41 -08001333 out->clock = in->clock;
1334 out->hdisplay = in->hdisplay;
1335 out->hsync_start = in->hsync_start;
1336 out->hsync_end = in->hsync_end;
1337 out->htotal = in->htotal;
1338 out->hskew = in->hskew;
1339 out->vdisplay = in->vdisplay;
1340 out->vsync_start = in->vsync_start;
1341 out->vsync_end = in->vsync_end;
1342 out->vtotal = in->vtotal;
1343 out->vscan = in->vscan;
1344 out->vrefresh = in->vrefresh;
1345 out->flags = in->flags;
1346 out->type = in->type;
1347 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1348 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1349}
1350
1351/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001352 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001353 * @out: drm_display_mode to return to the user
1354 * @in: drm_mode_modeinfo to use
1355 *
Dave Airlief453ba02008-11-07 14:05:41 -08001356 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1357 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001358 *
1359 * RETURNS:
1360 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001361 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001362static int drm_crtc_convert_umode(struct drm_display_mode *out,
1363 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001364{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001365 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1366 return -ERANGE;
1367
Damien Lespiau5848ad42013-09-27 12:11:50 +01001368 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1369 return -EINVAL;
1370
Dave Airlief453ba02008-11-07 14:05:41 -08001371 out->clock = in->clock;
1372 out->hdisplay = in->hdisplay;
1373 out->hsync_start = in->hsync_start;
1374 out->hsync_end = in->hsync_end;
1375 out->htotal = in->htotal;
1376 out->hskew = in->hskew;
1377 out->vdisplay = in->vdisplay;
1378 out->vsync_start = in->vsync_start;
1379 out->vsync_end = in->vsync_end;
1380 out->vtotal = in->vtotal;
1381 out->vscan = in->vscan;
1382 out->vrefresh = in->vrefresh;
1383 out->flags = in->flags;
1384 out->type = in->type;
1385 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1386 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001387
1388 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001389}
1390
1391/**
1392 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001393 * @dev: drm device for the ioctl
1394 * @data: data pointer for the ioctl
1395 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001396 *
Dave Airlief453ba02008-11-07 14:05:41 -08001397 * Construct a set of configuration description structures and return
1398 * them to the user, including CRTC, connector and framebuffer configuration.
1399 *
1400 * Called by the user via ioctl.
1401 *
1402 * RETURNS:
1403 * Zero on success, errno on failure.
1404 */
1405int drm_mode_getresources(struct drm_device *dev, void *data,
1406 struct drm_file *file_priv)
1407{
1408 struct drm_mode_card_res *card_res = data;
1409 struct list_head *lh;
1410 struct drm_framebuffer *fb;
1411 struct drm_connector *connector;
1412 struct drm_crtc *crtc;
1413 struct drm_encoder *encoder;
1414 int ret = 0;
1415 int connector_count = 0;
1416 int crtc_count = 0;
1417 int fb_count = 0;
1418 int encoder_count = 0;
1419 int copied = 0, i;
1420 uint32_t __user *fb_id;
1421 uint32_t __user *crtc_id;
1422 uint32_t __user *connector_id;
1423 uint32_t __user *encoder_id;
1424 struct drm_mode_group *mode_group;
1425
Dave Airliefb3b06c2011-02-08 13:55:21 +10001426 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1427 return -EINVAL;
1428
Dave Airlief453ba02008-11-07 14:05:41 -08001429
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001430 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001431 /*
1432 * For the non-control nodes we need to limit the list of resources
1433 * by IDs in the group list for this node
1434 */
1435 list_for_each(lh, &file_priv->fbs)
1436 fb_count++;
1437
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001438 /* handle this in 4 parts */
1439 /* FBs */
1440 if (card_res->count_fbs >= fb_count) {
1441 copied = 0;
1442 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1443 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1444 if (put_user(fb->base.id, fb_id + copied)) {
1445 mutex_unlock(&file_priv->fbs_lock);
1446 return -EFAULT;
1447 }
1448 copied++;
1449 }
1450 }
1451 card_res->count_fbs = fb_count;
1452 mutex_unlock(&file_priv->fbs_lock);
1453
1454 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001455 mode_group = &file_priv->master->minor->mode_group;
1456 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1457
1458 list_for_each(lh, &dev->mode_config.crtc_list)
1459 crtc_count++;
1460
1461 list_for_each(lh, &dev->mode_config.connector_list)
1462 connector_count++;
1463
1464 list_for_each(lh, &dev->mode_config.encoder_list)
1465 encoder_count++;
1466 } else {
1467
1468 crtc_count = mode_group->num_crtcs;
1469 connector_count = mode_group->num_connectors;
1470 encoder_count = mode_group->num_encoders;
1471 }
1472
1473 card_res->max_height = dev->mode_config.max_height;
1474 card_res->min_height = dev->mode_config.min_height;
1475 card_res->max_width = dev->mode_config.max_width;
1476 card_res->min_width = dev->mode_config.min_width;
1477
Dave Airlief453ba02008-11-07 14:05:41 -08001478 /* CRTCs */
1479 if (card_res->count_crtcs >= crtc_count) {
1480 copied = 0;
1481 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1482 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1483 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1484 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001485 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001486 if (put_user(crtc->base.id, crtc_id + copied)) {
1487 ret = -EFAULT;
1488 goto out;
1489 }
1490 copied++;
1491 }
1492 } else {
1493 for (i = 0; i < mode_group->num_crtcs; i++) {
1494 if (put_user(mode_group->id_list[i],
1495 crtc_id + copied)) {
1496 ret = -EFAULT;
1497 goto out;
1498 }
1499 copied++;
1500 }
1501 }
1502 }
1503 card_res->count_crtcs = crtc_count;
1504
1505 /* Encoders */
1506 if (card_res->count_encoders >= encoder_count) {
1507 copied = 0;
1508 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1509 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1510 list_for_each_entry(encoder,
1511 &dev->mode_config.encoder_list,
1512 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001513 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1514 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001515 if (put_user(encoder->base.id, encoder_id +
1516 copied)) {
1517 ret = -EFAULT;
1518 goto out;
1519 }
1520 copied++;
1521 }
1522 } else {
1523 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1524 if (put_user(mode_group->id_list[i],
1525 encoder_id + copied)) {
1526 ret = -EFAULT;
1527 goto out;
1528 }
1529 copied++;
1530 }
1531
1532 }
1533 }
1534 card_res->count_encoders = encoder_count;
1535
1536 /* Connectors */
1537 if (card_res->count_connectors >= connector_count) {
1538 copied = 0;
1539 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1540 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1541 list_for_each_entry(connector,
1542 &dev->mode_config.connector_list,
1543 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001544 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1545 connector->base.id,
1546 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001547 if (put_user(connector->base.id,
1548 connector_id + copied)) {
1549 ret = -EFAULT;
1550 goto out;
1551 }
1552 copied++;
1553 }
1554 } else {
1555 int start = mode_group->num_crtcs +
1556 mode_group->num_encoders;
1557 for (i = start; i < start + mode_group->num_connectors; i++) {
1558 if (put_user(mode_group->id_list[i],
1559 connector_id + copied)) {
1560 ret = -EFAULT;
1561 goto out;
1562 }
1563 copied++;
1564 }
1565 }
1566 }
1567 card_res->count_connectors = connector_count;
1568
Jerome Glisse94401062010-07-15 15:43:25 -04001569 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001570 card_res->count_connectors, card_res->count_encoders);
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
1577/**
1578 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001579 * @dev: drm device for the ioctl
1580 * @data: data pointer for the ioctl
1581 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001582 *
Dave Airlief453ba02008-11-07 14:05:41 -08001583 * Construct a CRTC configuration structure to return to the user.
1584 *
1585 * Called by the user via ioctl.
1586 *
1587 * RETURNS:
1588 * Zero on success, errno on failure.
1589 */
1590int drm_mode_getcrtc(struct drm_device *dev,
1591 void *data, struct drm_file *file_priv)
1592{
1593 struct drm_mode_crtc *crtc_resp = data;
1594 struct drm_crtc *crtc;
1595 struct drm_mode_object *obj;
1596 int ret = 0;
1597
Dave Airliefb3b06c2011-02-08 13:55:21 +10001598 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1599 return -EINVAL;
1600
Daniel Vetter84849902012-12-02 00:28:11 +01001601 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001602
1603 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1604 DRM_MODE_OBJECT_CRTC);
1605 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001606 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001607 goto out;
1608 }
1609 crtc = obj_to_crtc(obj);
1610
1611 crtc_resp->x = crtc->x;
1612 crtc_resp->y = crtc->y;
1613 crtc_resp->gamma_size = crtc->gamma_size;
1614 if (crtc->fb)
1615 crtc_resp->fb_id = crtc->fb->base.id;
1616 else
1617 crtc_resp->fb_id = 0;
1618
1619 if (crtc->enabled) {
1620
1621 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1622 crtc_resp->mode_valid = 1;
1623
1624 } else {
1625 crtc_resp->mode_valid = 0;
1626 }
1627
1628out:
Daniel Vetter84849902012-12-02 00:28:11 +01001629 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001630 return ret;
1631}
1632
Damien Lespiau61d8e322013-09-25 16:45:22 +01001633static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1634 const struct drm_file *file_priv)
1635{
1636 /*
1637 * If user-space hasn't configured the driver to expose the stereo 3D
1638 * modes, don't expose them.
1639 */
1640 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1641 return false;
1642
1643 return true;
1644}
1645
Dave Airlief453ba02008-11-07 14:05:41 -08001646/**
1647 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001648 * @dev: drm device for the ioctl
1649 * @data: data pointer for the ioctl
1650 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001651 *
Dave Airlief453ba02008-11-07 14:05:41 -08001652 * Construct a connector configuration structure to return to the user.
1653 *
1654 * Called by the user via ioctl.
1655 *
1656 * RETURNS:
1657 * Zero on success, errno on failure.
1658 */
1659int drm_mode_getconnector(struct drm_device *dev, void *data,
1660 struct drm_file *file_priv)
1661{
1662 struct drm_mode_get_connector *out_resp = data;
1663 struct drm_mode_object *obj;
1664 struct drm_connector *connector;
1665 struct drm_display_mode *mode;
1666 int mode_count = 0;
1667 int props_count = 0;
1668 int encoders_count = 0;
1669 int ret = 0;
1670 int copied = 0;
1671 int i;
1672 struct drm_mode_modeinfo u_mode;
1673 struct drm_mode_modeinfo __user *mode_ptr;
1674 uint32_t __user *prop_ptr;
1675 uint64_t __user *prop_values;
1676 uint32_t __user *encoder_ptr;
1677
Dave Airliefb3b06c2011-02-08 13:55:21 +10001678 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1679 return -EINVAL;
1680
Dave Airlief453ba02008-11-07 14:05:41 -08001681 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1682
Jerome Glisse94401062010-07-15 15:43:25 -04001683 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001684
Daniel Vetter7b240562012-12-12 00:35:33 +01001685 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001686
1687 obj = drm_mode_object_find(dev, out_resp->connector_id,
1688 DRM_MODE_OBJECT_CONNECTOR);
1689 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001690 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001691 goto out;
1692 }
1693 connector = obj_to_connector(obj);
1694
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001695 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001696
1697 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1698 if (connector->encoder_ids[i] != 0) {
1699 encoders_count++;
1700 }
1701 }
1702
1703 if (out_resp->count_modes == 0) {
1704 connector->funcs->fill_modes(connector,
1705 dev->mode_config.max_width,
1706 dev->mode_config.max_height);
1707 }
1708
1709 /* delayed so we get modes regardless of pre-fill_modes state */
1710 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001711 if (drm_mode_expose_to_userspace(mode, file_priv))
1712 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001713
1714 out_resp->connector_id = connector->base.id;
1715 out_resp->connector_type = connector->connector_type;
1716 out_resp->connector_type_id = connector->connector_type_id;
1717 out_resp->mm_width = connector->display_info.width_mm;
1718 out_resp->mm_height = connector->display_info.height_mm;
1719 out_resp->subpixel = connector->display_info.subpixel_order;
1720 out_resp->connection = connector->status;
1721 if (connector->encoder)
1722 out_resp->encoder_id = connector->encoder->base.id;
1723 else
1724 out_resp->encoder_id = 0;
1725
1726 /*
1727 * This ioctl is called twice, once to determine how much space is
1728 * needed, and the 2nd time to fill it.
1729 */
1730 if ((out_resp->count_modes >= mode_count) && mode_count) {
1731 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001732 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001733 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001734 if (!drm_mode_expose_to_userspace(mode, file_priv))
1735 continue;
1736
Dave Airlief453ba02008-11-07 14:05:41 -08001737 drm_crtc_convert_to_umode(&u_mode, mode);
1738 if (copy_to_user(mode_ptr + copied,
1739 &u_mode, sizeof(u_mode))) {
1740 ret = -EFAULT;
1741 goto out;
1742 }
1743 copied++;
1744 }
1745 }
1746 out_resp->count_modes = mode_count;
1747
1748 if ((out_resp->count_props >= props_count) && props_count) {
1749 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001750 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1751 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001752 for (i = 0; i < connector->properties.count; i++) {
1753 if (put_user(connector->properties.ids[i],
1754 prop_ptr + copied)) {
1755 ret = -EFAULT;
1756 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001757 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001758
1759 if (put_user(connector->properties.values[i],
1760 prop_values + copied)) {
1761 ret = -EFAULT;
1762 goto out;
1763 }
1764 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001765 }
1766 }
1767 out_resp->count_props = props_count;
1768
1769 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1770 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001771 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001772 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1773 if (connector->encoder_ids[i] != 0) {
1774 if (put_user(connector->encoder_ids[i],
1775 encoder_ptr + copied)) {
1776 ret = -EFAULT;
1777 goto out;
1778 }
1779 copied++;
1780 }
1781 }
1782 }
1783 out_resp->count_encoders = encoders_count;
1784
1785out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001786 mutex_unlock(&dev->mode_config.mutex);
1787
Dave Airlief453ba02008-11-07 14:05:41 -08001788 return ret;
1789}
1790
1791int drm_mode_getencoder(struct drm_device *dev, void *data,
1792 struct drm_file *file_priv)
1793{
1794 struct drm_mode_get_encoder *enc_resp = data;
1795 struct drm_mode_object *obj;
1796 struct drm_encoder *encoder;
1797 int ret = 0;
1798
Dave Airliefb3b06c2011-02-08 13:55:21 +10001799 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1800 return -EINVAL;
1801
Daniel Vetter84849902012-12-02 00:28:11 +01001802 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001803 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1804 DRM_MODE_OBJECT_ENCODER);
1805 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001806 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001807 goto out;
1808 }
1809 encoder = obj_to_encoder(obj);
1810
1811 if (encoder->crtc)
1812 enc_resp->crtc_id = encoder->crtc->base.id;
1813 else
1814 enc_resp->crtc_id = 0;
1815 enc_resp->encoder_type = encoder->encoder_type;
1816 enc_resp->encoder_id = encoder->base.id;
1817 enc_resp->possible_crtcs = encoder->possible_crtcs;
1818 enc_resp->possible_clones = encoder->possible_clones;
1819
1820out:
Daniel Vetter84849902012-12-02 00:28:11 +01001821 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001822 return ret;
1823}
1824
1825/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001826 * drm_mode_getplane_res - get plane info
1827 * @dev: DRM device
1828 * @data: ioctl data
1829 * @file_priv: DRM file info
1830 *
1831 * Return an plane count and set of IDs.
1832 */
1833int drm_mode_getplane_res(struct drm_device *dev, void *data,
1834 struct drm_file *file_priv)
1835{
1836 struct drm_mode_get_plane_res *plane_resp = data;
1837 struct drm_mode_config *config;
1838 struct drm_plane *plane;
1839 uint32_t __user *plane_ptr;
1840 int copied = 0, ret = 0;
1841
1842 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1843 return -EINVAL;
1844
Daniel Vetter84849902012-12-02 00:28:11 +01001845 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001846 config = &dev->mode_config;
1847
1848 /*
1849 * This ioctl is called twice, once to determine how much space is
1850 * needed, and the 2nd time to fill it.
1851 */
1852 if (config->num_plane &&
1853 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001854 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001855
1856 list_for_each_entry(plane, &config->plane_list, head) {
1857 if (put_user(plane->base.id, plane_ptr + copied)) {
1858 ret = -EFAULT;
1859 goto out;
1860 }
1861 copied++;
1862 }
1863 }
1864 plane_resp->count_planes = config->num_plane;
1865
1866out:
Daniel Vetter84849902012-12-02 00:28:11 +01001867 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001868 return ret;
1869}
1870
1871/**
1872 * drm_mode_getplane - get plane info
1873 * @dev: DRM device
1874 * @data: ioctl data
1875 * @file_priv: DRM file info
1876 *
1877 * Return plane info, including formats supported, gamma size, any
1878 * current fb, etc.
1879 */
1880int drm_mode_getplane(struct drm_device *dev, void *data,
1881 struct drm_file *file_priv)
1882{
1883 struct drm_mode_get_plane *plane_resp = data;
1884 struct drm_mode_object *obj;
1885 struct drm_plane *plane;
1886 uint32_t __user *format_ptr;
1887 int ret = 0;
1888
1889 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1890 return -EINVAL;
1891
Daniel Vetter84849902012-12-02 00:28:11 +01001892 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001893 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1894 DRM_MODE_OBJECT_PLANE);
1895 if (!obj) {
1896 ret = -ENOENT;
1897 goto out;
1898 }
1899 plane = obj_to_plane(obj);
1900
1901 if (plane->crtc)
1902 plane_resp->crtc_id = plane->crtc->base.id;
1903 else
1904 plane_resp->crtc_id = 0;
1905
1906 if (plane->fb)
1907 plane_resp->fb_id = plane->fb->base.id;
1908 else
1909 plane_resp->fb_id = 0;
1910
1911 plane_resp->plane_id = plane->base.id;
1912 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001913 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001914
1915 /*
1916 * This ioctl is called twice, once to determine how much space is
1917 * needed, and the 2nd time to fill it.
1918 */
1919 if (plane->format_count &&
1920 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001921 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001922 if (copy_to_user(format_ptr,
1923 plane->format_types,
1924 sizeof(uint32_t) * plane->format_count)) {
1925 ret = -EFAULT;
1926 goto out;
1927 }
1928 }
1929 plane_resp->count_format_types = plane->format_count;
1930
1931out:
Daniel Vetter84849902012-12-02 00:28:11 +01001932 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001933 return ret;
1934}
1935
1936/**
1937 * drm_mode_setplane - set up or tear down an plane
1938 * @dev: DRM device
1939 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001940 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001941 *
1942 * Set plane info, including placement, fb, scaling, and other factors.
1943 * Or pass a NULL fb to disable.
1944 */
1945int drm_mode_setplane(struct drm_device *dev, void *data,
1946 struct drm_file *file_priv)
1947{
1948 struct drm_mode_set_plane *plane_req = data;
1949 struct drm_mode_object *obj;
1950 struct drm_plane *plane;
1951 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001952 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001953 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001954 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001955 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001956
1957 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1958 return -EINVAL;
1959
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001960 /*
1961 * First, find the plane, crtc, and fb objects. If not available,
1962 * we don't bother to call the driver.
1963 */
1964 obj = drm_mode_object_find(dev, plane_req->plane_id,
1965 DRM_MODE_OBJECT_PLANE);
1966 if (!obj) {
1967 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1968 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001969 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001970 }
1971 plane = obj_to_plane(obj);
1972
1973 /* No fb means shut it down */
1974 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001975 drm_modeset_lock_all(dev);
1976 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001977 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001978 plane->crtc = NULL;
1979 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001980 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001981 goto out;
1982 }
1983
1984 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1985 DRM_MODE_OBJECT_CRTC);
1986 if (!obj) {
1987 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1988 plane_req->crtc_id);
1989 ret = -ENOENT;
1990 goto out;
1991 }
1992 crtc = obj_to_crtc(obj);
1993
Daniel Vetter786b99e2012-12-02 21:53:40 +01001994 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1995 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001996 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1997 plane_req->fb_id);
1998 ret = -ENOENT;
1999 goto out;
2000 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002001
Ville Syrjälä62443be2011-12-20 00:06:47 +02002002 /* Check whether this plane supports the fb pixel format. */
2003 for (i = 0; i < plane->format_count; i++)
2004 if (fb->pixel_format == plane->format_types[i])
2005 break;
2006 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002007 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2008 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002009 ret = -EINVAL;
2010 goto out;
2011 }
2012
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002013 fb_width = fb->width << 16;
2014 fb_height = fb->height << 16;
2015
2016 /* Make sure source coordinates are inside the fb. */
2017 if (plane_req->src_w > fb_width ||
2018 plane_req->src_x > fb_width - plane_req->src_w ||
2019 plane_req->src_h > fb_height ||
2020 plane_req->src_y > fb_height - plane_req->src_h) {
2021 DRM_DEBUG_KMS("Invalid source coordinates "
2022 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2023 plane_req->src_w >> 16,
2024 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2025 plane_req->src_h >> 16,
2026 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2027 plane_req->src_x >> 16,
2028 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2029 plane_req->src_y >> 16,
2030 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2031 ret = -ENOSPC;
2032 goto out;
2033 }
2034
Ville Syrjälä687a0402011-12-20 00:06:45 +02002035 /* Give drivers some help against integer overflows */
2036 if (plane_req->crtc_w > INT_MAX ||
2037 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2038 plane_req->crtc_h > INT_MAX ||
2039 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2040 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2041 plane_req->crtc_w, plane_req->crtc_h,
2042 plane_req->crtc_x, plane_req->crtc_y);
2043 ret = -ERANGE;
2044 goto out;
2045 }
2046
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002047 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002048 ret = plane->funcs->update_plane(plane, crtc, fb,
2049 plane_req->crtc_x, plane_req->crtc_y,
2050 plane_req->crtc_w, plane_req->crtc_h,
2051 plane_req->src_x, plane_req->src_y,
2052 plane_req->src_w, plane_req->src_h);
2053 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002054 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002055 plane->crtc = crtc;
2056 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002057 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002058 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002059 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002060
2061out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002062 if (fb)
2063 drm_framebuffer_unreference(fb);
2064 if (old_fb)
2065 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002066
2067 return ret;
2068}
2069
2070/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002071 * drm_mode_set_config_internal - helper to call ->set_config
2072 * @set: modeset config to set
2073 *
2074 * This is a little helper to wrap internal calls to the ->set_config driver
2075 * interface. The only thing it adds is correct refcounting dance.
2076 */
2077int drm_mode_set_config_internal(struct drm_mode_set *set)
2078{
2079 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002080 struct drm_framebuffer *fb;
2081 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002082 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002083
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002084 /*
2085 * NOTE: ->set_config can also disable other crtcs (if we steal all
2086 * connectors from it), hence we need to refcount the fbs across all
2087 * crtcs. Atomic modeset will have saner semantics ...
2088 */
2089 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2090 tmp->old_fb = tmp->fb;
2091
Daniel Vetterb0d12322012-12-11 01:07:12 +01002092 fb = set->fb;
2093
2094 ret = crtc->funcs->set_config(set);
2095 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02002096 /* crtc->fb must be updated by ->set_config, enforces this. */
2097 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002098 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002099
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002100 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2101 if (tmp->fb)
2102 drm_framebuffer_reference(tmp->fb);
2103 if (tmp->old_fb)
2104 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002105 }
2106
2107 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002108}
2109EXPORT_SYMBOL(drm_mode_set_config_internal);
2110
Damien Lespiauc11e9282013-09-25 16:45:30 +01002111/*
2112 * Checks that the framebuffer is big enough for the CRTC viewport
2113 * (x, y, hdisplay, vdisplay)
2114 */
2115static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2116 int x, int y,
2117 const struct drm_display_mode *mode,
2118 const struct drm_framebuffer *fb)
2119
2120{
2121 int hdisplay, vdisplay;
2122
2123 hdisplay = mode->hdisplay;
2124 vdisplay = mode->vdisplay;
2125
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002126 if (drm_mode_is_stereo(mode)) {
2127 struct drm_display_mode adjusted = *mode;
2128
2129 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2130 hdisplay = adjusted.crtc_hdisplay;
2131 vdisplay = adjusted.crtc_vdisplay;
2132 }
2133
Damien Lespiauc11e9282013-09-25 16:45:30 +01002134 if (crtc->invert_dimensions)
2135 swap(hdisplay, vdisplay);
2136
2137 if (hdisplay > fb->width ||
2138 vdisplay > fb->height ||
2139 x > fb->width - hdisplay ||
2140 y > fb->height - vdisplay) {
2141 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2142 fb->width, fb->height, hdisplay, vdisplay, x, y,
2143 crtc->invert_dimensions ? " (inverted)" : "");
2144 return -ENOSPC;
2145 }
2146
2147 return 0;
2148}
2149
Daniel Vetter2d13b672012-12-11 13:47:23 +01002150/**
Dave Airlief453ba02008-11-07 14:05:41 -08002151 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002152 * @dev: drm device for the ioctl
2153 * @data: data pointer for the ioctl
2154 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002155 *
Dave Airlief453ba02008-11-07 14:05:41 -08002156 * Build a new CRTC configuration based on user request.
2157 *
2158 * Called by the user via ioctl.
2159 *
2160 * RETURNS:
2161 * Zero on success, errno on failure.
2162 */
2163int drm_mode_setcrtc(struct drm_device *dev, void *data,
2164 struct drm_file *file_priv)
2165{
2166 struct drm_mode_config *config = &dev->mode_config;
2167 struct drm_mode_crtc *crtc_req = data;
2168 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002169 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002170 struct drm_connector **connector_set = NULL, *connector;
2171 struct drm_framebuffer *fb = NULL;
2172 struct drm_display_mode *mode = NULL;
2173 struct drm_mode_set set;
2174 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002175 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002176 int i;
2177
Dave Airliefb3b06c2011-02-08 13:55:21 +10002178 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2179 return -EINVAL;
2180
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002181 /* For some reason crtc x/y offsets are signed internally. */
2182 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2183 return -ERANGE;
2184
Daniel Vetter84849902012-12-02 00:28:11 +01002185 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002186 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2187 DRM_MODE_OBJECT_CRTC);
2188 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002189 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002190 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002191 goto out;
2192 }
2193 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002194 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002195
2196 if (crtc_req->mode_valid) {
2197 /* If we have a mode we need a framebuffer. */
2198 /* If we pass -1, set the mode with the currently bound fb */
2199 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002200 if (!crtc->fb) {
2201 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2202 ret = -EINVAL;
2203 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002204 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002205 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002206 /* Make refcounting symmetric with the lookup path. */
2207 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002208 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002209 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2210 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002211 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2212 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002213 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002214 goto out;
2215 }
Dave Airlief453ba02008-11-07 14:05:41 -08002216 }
2217
2218 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002219 if (!mode) {
2220 ret = -ENOMEM;
2221 goto out;
2222 }
2223
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002224 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2225 if (ret) {
2226 DRM_DEBUG_KMS("Invalid mode\n");
2227 goto out;
2228 }
2229
Dave Airlief453ba02008-11-07 14:05:41 -08002230 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002231
Damien Lespiauc11e9282013-09-25 16:45:30 +01002232 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2233 mode, fb);
2234 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002235 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002236
Dave Airlief453ba02008-11-07 14:05:41 -08002237 }
2238
2239 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002240 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002241 ret = -EINVAL;
2242 goto out;
2243 }
2244
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002245 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002246 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002247 crtc_req->count_connectors);
2248 ret = -EINVAL;
2249 goto out;
2250 }
2251
2252 if (crtc_req->count_connectors > 0) {
2253 u32 out_id;
2254
2255 /* Avoid unbounded kernel memory allocation */
2256 if (crtc_req->count_connectors > config->num_connector) {
2257 ret = -EINVAL;
2258 goto out;
2259 }
2260
2261 connector_set = kmalloc(crtc_req->count_connectors *
2262 sizeof(struct drm_connector *),
2263 GFP_KERNEL);
2264 if (!connector_set) {
2265 ret = -ENOMEM;
2266 goto out;
2267 }
2268
2269 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002270 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002271 if (get_user(out_id, &set_connectors_ptr[i])) {
2272 ret = -EFAULT;
2273 goto out;
2274 }
2275
2276 obj = drm_mode_object_find(dev, out_id,
2277 DRM_MODE_OBJECT_CONNECTOR);
2278 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002279 DRM_DEBUG_KMS("Connector id %d unknown\n",
2280 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002281 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002282 goto out;
2283 }
2284 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002285 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2286 connector->base.id,
2287 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002288
2289 connector_set[i] = connector;
2290 }
2291 }
2292
2293 set.crtc = crtc;
2294 set.x = crtc_req->x;
2295 set.y = crtc_req->y;
2296 set.mode = mode;
2297 set.connectors = connector_set;
2298 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002299 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002300 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002301
2302out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002303 if (fb)
2304 drm_framebuffer_unreference(fb);
2305
Dave Airlief453ba02008-11-07 14:05:41 -08002306 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002307 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002308 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002309 return ret;
2310}
2311
Dave Airlie4c813d42013-06-20 11:48:52 +10002312static int drm_mode_cursor_common(struct drm_device *dev,
2313 struct drm_mode_cursor2 *req,
2314 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002315{
Dave Airlief453ba02008-11-07 14:05:41 -08002316 struct drm_mode_object *obj;
2317 struct drm_crtc *crtc;
2318 int ret = 0;
2319
Dave Airliefb3b06c2011-02-08 13:55:21 +10002320 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2321 return -EINVAL;
2322
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002323 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002324 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002325
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002326 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002327 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002328 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002329 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002330 }
2331 crtc = obj_to_crtc(obj);
2332
Daniel Vetterdac35662012-12-02 15:24:10 +01002333 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002334 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002335 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002336 ret = -ENXIO;
2337 goto out;
2338 }
2339 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002340 if (crtc->funcs->cursor_set2)
2341 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2342 req->width, req->height, req->hot_x, req->hot_y);
2343 else
2344 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2345 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002346 }
2347
2348 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2349 if (crtc->funcs->cursor_move) {
2350 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2351 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002352 ret = -EFAULT;
2353 goto out;
2354 }
2355 }
2356out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002357 mutex_unlock(&crtc->mutex);
2358
Dave Airlief453ba02008-11-07 14:05:41 -08002359 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002360
2361}
2362int drm_mode_cursor_ioctl(struct drm_device *dev,
2363 void *data, struct drm_file *file_priv)
2364{
2365 struct drm_mode_cursor *req = data;
2366 struct drm_mode_cursor2 new_req;
2367
2368 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2369 new_req.hot_x = new_req.hot_y = 0;
2370
2371 return drm_mode_cursor_common(dev, &new_req, file_priv);
2372}
2373
2374int drm_mode_cursor2_ioctl(struct drm_device *dev,
2375 void *data, struct drm_file *file_priv)
2376{
2377 struct drm_mode_cursor2 *req = data;
2378 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002379}
2380
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002381/* Original addfb only supported RGB formats, so figure out which one */
2382uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2383{
2384 uint32_t fmt;
2385
2386 switch (bpp) {
2387 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002388 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002389 break;
2390 case 16:
2391 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002392 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002393 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002394 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002395 break;
2396 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002397 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002398 break;
2399 case 32:
2400 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002401 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002402 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002403 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002404 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002405 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002406 break;
2407 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002408 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2409 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002410 break;
2411 }
2412
2413 return fmt;
2414}
2415EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2416
Dave Airlief453ba02008-11-07 14:05:41 -08002417/**
2418 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002419 * @dev: drm device for the ioctl
2420 * @data: data pointer for the ioctl
2421 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002422 *
Dave Airlief453ba02008-11-07 14:05:41 -08002423 * Add a new FB to the specified CRTC, given a user request.
2424 *
2425 * Called by the user via ioctl.
2426 *
2427 * RETURNS:
2428 * Zero on success, errno on failure.
2429 */
2430int drm_mode_addfb(struct drm_device *dev,
2431 void *data, struct drm_file *file_priv)
2432{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002433 struct drm_mode_fb_cmd *or = data;
2434 struct drm_mode_fb_cmd2 r = {};
2435 struct drm_mode_config *config = &dev->mode_config;
2436 struct drm_framebuffer *fb;
2437 int ret = 0;
2438
2439 /* Use new struct with format internally */
2440 r.fb_id = or->fb_id;
2441 r.width = or->width;
2442 r.height = or->height;
2443 r.pitches[0] = or->pitch;
2444 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2445 r.handles[0] = or->handle;
2446
2447 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2448 return -EINVAL;
2449
Jesse Barnesacb4b992011-11-07 12:03:23 -08002450 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002451 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002452
2453 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002454 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002455
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002456 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2457 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002458 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002459 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002460 }
2461
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002462 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002463 or->fb_id = fb->base.id;
2464 list_add(&fb->filp_head, &file_priv->fbs);
2465 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002466 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002467
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002468 return ret;
2469}
2470
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002471static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002472{
2473 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2474
2475 switch (format) {
2476 case DRM_FORMAT_C8:
2477 case DRM_FORMAT_RGB332:
2478 case DRM_FORMAT_BGR233:
2479 case DRM_FORMAT_XRGB4444:
2480 case DRM_FORMAT_XBGR4444:
2481 case DRM_FORMAT_RGBX4444:
2482 case DRM_FORMAT_BGRX4444:
2483 case DRM_FORMAT_ARGB4444:
2484 case DRM_FORMAT_ABGR4444:
2485 case DRM_FORMAT_RGBA4444:
2486 case DRM_FORMAT_BGRA4444:
2487 case DRM_FORMAT_XRGB1555:
2488 case DRM_FORMAT_XBGR1555:
2489 case DRM_FORMAT_RGBX5551:
2490 case DRM_FORMAT_BGRX5551:
2491 case DRM_FORMAT_ARGB1555:
2492 case DRM_FORMAT_ABGR1555:
2493 case DRM_FORMAT_RGBA5551:
2494 case DRM_FORMAT_BGRA5551:
2495 case DRM_FORMAT_RGB565:
2496 case DRM_FORMAT_BGR565:
2497 case DRM_FORMAT_RGB888:
2498 case DRM_FORMAT_BGR888:
2499 case DRM_FORMAT_XRGB8888:
2500 case DRM_FORMAT_XBGR8888:
2501 case DRM_FORMAT_RGBX8888:
2502 case DRM_FORMAT_BGRX8888:
2503 case DRM_FORMAT_ARGB8888:
2504 case DRM_FORMAT_ABGR8888:
2505 case DRM_FORMAT_RGBA8888:
2506 case DRM_FORMAT_BGRA8888:
2507 case DRM_FORMAT_XRGB2101010:
2508 case DRM_FORMAT_XBGR2101010:
2509 case DRM_FORMAT_RGBX1010102:
2510 case DRM_FORMAT_BGRX1010102:
2511 case DRM_FORMAT_ARGB2101010:
2512 case DRM_FORMAT_ABGR2101010:
2513 case DRM_FORMAT_RGBA1010102:
2514 case DRM_FORMAT_BGRA1010102:
2515 case DRM_FORMAT_YUYV:
2516 case DRM_FORMAT_YVYU:
2517 case DRM_FORMAT_UYVY:
2518 case DRM_FORMAT_VYUY:
2519 case DRM_FORMAT_AYUV:
2520 case DRM_FORMAT_NV12:
2521 case DRM_FORMAT_NV21:
2522 case DRM_FORMAT_NV16:
2523 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002524 case DRM_FORMAT_NV24:
2525 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002526 case DRM_FORMAT_YUV410:
2527 case DRM_FORMAT_YVU410:
2528 case DRM_FORMAT_YUV411:
2529 case DRM_FORMAT_YVU411:
2530 case DRM_FORMAT_YUV420:
2531 case DRM_FORMAT_YVU420:
2532 case DRM_FORMAT_YUV422:
2533 case DRM_FORMAT_YVU422:
2534 case DRM_FORMAT_YUV444:
2535 case DRM_FORMAT_YVU444:
2536 return 0;
2537 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002538 DRM_DEBUG_KMS("invalid pixel format %s\n",
2539 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002540 return -EINVAL;
2541 }
2542}
2543
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002544static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002545{
2546 int ret, hsub, vsub, num_planes, i;
2547
2548 ret = format_check(r);
2549 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002550 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2551 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002552 return ret;
2553 }
2554
2555 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2556 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2557 num_planes = drm_format_num_planes(r->pixel_format);
2558
2559 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002560 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002561 return -EINVAL;
2562 }
2563
2564 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002565 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002566 return -EINVAL;
2567 }
2568
2569 for (i = 0; i < num_planes; i++) {
2570 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002571 unsigned int height = r->height / (i != 0 ? vsub : 1);
2572 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002573
2574 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002575 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002576 return -EINVAL;
2577 }
2578
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002579 if ((uint64_t) width * cpp > UINT_MAX)
2580 return -ERANGE;
2581
2582 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2583 return -ERANGE;
2584
2585 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002586 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002587 return -EINVAL;
2588 }
2589 }
2590
2591 return 0;
2592}
2593
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002594/**
2595 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002596 * @dev: drm device for the ioctl
2597 * @data: data pointer for the ioctl
2598 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002599 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002600 * Add a new FB to the specified CRTC, given a user request with format.
2601 *
2602 * Called by the user via ioctl.
2603 *
2604 * RETURNS:
2605 * Zero on success, errno on failure.
2606 */
2607int drm_mode_addfb2(struct drm_device *dev,
2608 void *data, struct drm_file *file_priv)
2609{
2610 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002611 struct drm_mode_config *config = &dev->mode_config;
2612 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002613 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002614
Dave Airliefb3b06c2011-02-08 13:55:21 +10002615 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2616 return -EINVAL;
2617
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002618 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2619 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2620 return -EINVAL;
2621 }
2622
Dave Airlief453ba02008-11-07 14:05:41 -08002623 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002624 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002625 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002626 return -EINVAL;
2627 }
2628 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002629 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002630 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002631 return -EINVAL;
2632 }
2633
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002634 ret = framebuffer_check(r);
2635 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002636 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002637
Dave Airlief453ba02008-11-07 14:05:41 -08002638 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002639 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002640 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002641 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002642 }
2643
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002644 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002645 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002646 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002647 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002648 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002649
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002650
Dave Airlief453ba02008-11-07 14:05:41 -08002651 return ret;
2652}
2653
2654/**
2655 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002656 * @dev: drm device for the ioctl
2657 * @data: data pointer for the ioctl
2658 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002659 *
Dave Airlief453ba02008-11-07 14:05:41 -08002660 * Remove the FB specified by the user.
2661 *
2662 * Called by the user via ioctl.
2663 *
2664 * RETURNS:
2665 * Zero on success, errno on failure.
2666 */
2667int drm_mode_rmfb(struct drm_device *dev,
2668 void *data, struct drm_file *file_priv)
2669{
Dave Airlief453ba02008-11-07 14:05:41 -08002670 struct drm_framebuffer *fb = NULL;
2671 struct drm_framebuffer *fbl = NULL;
2672 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002673 int found = 0;
2674
Dave Airliefb3b06c2011-02-08 13:55:21 +10002675 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2676 return -EINVAL;
2677
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002678 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002679 mutex_lock(&dev->mode_config.fb_lock);
2680 fb = __drm_framebuffer_lookup(dev, *id);
2681 if (!fb)
2682 goto fail_lookup;
2683
Dave Airlief453ba02008-11-07 14:05:41 -08002684 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2685 if (fb == fbl)
2686 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002687 if (!found)
2688 goto fail_lookup;
2689
2690 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2691 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002692
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002693 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002694 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002695 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002696
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002697 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002698
Daniel Vetter2b677e82012-12-10 21:16:05 +01002699 return 0;
2700
2701fail_lookup:
2702 mutex_unlock(&dev->mode_config.fb_lock);
2703 mutex_unlock(&file_priv->fbs_lock);
2704
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002705 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002706}
2707
2708/**
2709 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002710 * @dev: drm device for the ioctl
2711 * @data: data pointer for the ioctl
2712 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002713 *
Dave Airlief453ba02008-11-07 14:05:41 -08002714 * Lookup the FB given its ID and return info about it.
2715 *
2716 * Called by the user via ioctl.
2717 *
2718 * RETURNS:
2719 * Zero on success, errno on failure.
2720 */
2721int drm_mode_getfb(struct drm_device *dev,
2722 void *data, struct drm_file *file_priv)
2723{
2724 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002725 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002726 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002727
Dave Airliefb3b06c2011-02-08 13:55:21 +10002728 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2729 return -EINVAL;
2730
Daniel Vetter786b99e2012-12-02 21:53:40 +01002731 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002732 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002733 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002734
2735 r->height = fb->height;
2736 r->width = fb->width;
2737 r->depth = fb->depth;
2738 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002739 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002740 if (fb->funcs->create_handle) {
2741 if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
2742 ret = fb->funcs->create_handle(fb, file_priv,
2743 &r->handle);
2744 } else {
2745 /* GET_FB() is an unprivileged ioctl so we must not
2746 * return a buffer-handle to non-master processes! For
2747 * backwards-compatibility reasons, we cannot make
2748 * GET_FB() privileged, so just return an invalid handle
2749 * for non-masters. */
2750 r->handle = 0;
2751 ret = 0;
2752 }
2753 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002754 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02002755 }
Dave Airlief453ba02008-11-07 14:05:41 -08002756
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002757 drm_framebuffer_unreference(fb);
2758
Dave Airlief453ba02008-11-07 14:05:41 -08002759 return ret;
2760}
2761
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002762int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2763 void *data, struct drm_file *file_priv)
2764{
2765 struct drm_clip_rect __user *clips_ptr;
2766 struct drm_clip_rect *clips = NULL;
2767 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002768 struct drm_framebuffer *fb;
2769 unsigned flags;
2770 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002771 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002772
Dave Airliefb3b06c2011-02-08 13:55:21 +10002773 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2774 return -EINVAL;
2775
Daniel Vetter786b99e2012-12-02 21:53:40 +01002776 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002777 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002778 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002779
2780 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002781 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002782
2783 if (!num_clips != !clips_ptr) {
2784 ret = -EINVAL;
2785 goto out_err1;
2786 }
2787
2788 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2789
2790 /* If userspace annotates copy, clips must come in pairs */
2791 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2792 ret = -EINVAL;
2793 goto out_err1;
2794 }
2795
2796 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002797 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2798 ret = -EINVAL;
2799 goto out_err1;
2800 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002801 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2802 if (!clips) {
2803 ret = -ENOMEM;
2804 goto out_err1;
2805 }
2806
2807 ret = copy_from_user(clips, clips_ptr,
2808 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002809 if (ret) {
2810 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002811 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002812 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002813 }
2814
2815 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002816 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2817 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002818 } else {
2819 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002820 }
2821
2822out_err2:
2823 kfree(clips);
2824out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002825 drm_framebuffer_unreference(fb);
2826
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002827 return ret;
2828}
2829
2830
Dave Airlief453ba02008-11-07 14:05:41 -08002831/**
2832 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002833 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002834 *
Dave Airlief453ba02008-11-07 14:05:41 -08002835 * Destroy all the FBs associated with @filp.
2836 *
2837 * Called by the user via ioctl.
2838 *
2839 * RETURNS:
2840 * Zero on success, errno on failure.
2841 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002842void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002843{
Dave Airlief453ba02008-11-07 14:05:41 -08002844 struct drm_device *dev = priv->minor->dev;
2845 struct drm_framebuffer *fb, *tfb;
2846
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002847 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002848 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002849
2850 mutex_lock(&dev->mode_config.fb_lock);
2851 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2852 __drm_framebuffer_unregister(dev, fb);
2853 mutex_unlock(&dev->mode_config.fb_lock);
2854
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002855 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002856
2857 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002858 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002859 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002860 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002861}
2862
Dave Airlief453ba02008-11-07 14:05:41 -08002863struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2864 const char *name, int num_values)
2865{
2866 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002867 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002868
2869 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2870 if (!property)
2871 return NULL;
2872
2873 if (num_values) {
2874 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2875 if (!property->values)
2876 goto fail;
2877 }
2878
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002879 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2880 if (ret)
2881 goto fail;
2882
Dave Airlief453ba02008-11-07 14:05:41 -08002883 property->flags = flags;
2884 property->num_values = num_values;
2885 INIT_LIST_HEAD(&property->enum_blob_list);
2886
Vinson Lee471dd2e2011-11-10 11:55:40 -08002887 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002888 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002889 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2890 }
Dave Airlief453ba02008-11-07 14:05:41 -08002891
2892 list_add_tail(&property->head, &dev->mode_config.property_list);
2893 return property;
2894fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002895 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002896 kfree(property);
2897 return NULL;
2898}
2899EXPORT_SYMBOL(drm_property_create);
2900
Sascha Hauer4a67d392012-02-06 10:58:17 +01002901struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2902 const char *name,
2903 const struct drm_prop_enum_list *props,
2904 int num_values)
2905{
2906 struct drm_property *property;
2907 int i, ret;
2908
2909 flags |= DRM_MODE_PROP_ENUM;
2910
2911 property = drm_property_create(dev, flags, name, num_values);
2912 if (!property)
2913 return NULL;
2914
2915 for (i = 0; i < num_values; i++) {
2916 ret = drm_property_add_enum(property, i,
2917 props[i].type,
2918 props[i].name);
2919 if (ret) {
2920 drm_property_destroy(dev, property);
2921 return NULL;
2922 }
2923 }
2924
2925 return property;
2926}
2927EXPORT_SYMBOL(drm_property_create_enum);
2928
Rob Clark49e27542012-05-17 02:23:26 -06002929struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2930 int flags, const char *name,
2931 const struct drm_prop_enum_list *props,
2932 int num_values)
2933{
2934 struct drm_property *property;
2935 int i, ret;
2936
2937 flags |= DRM_MODE_PROP_BITMASK;
2938
2939 property = drm_property_create(dev, flags, name, num_values);
2940 if (!property)
2941 return NULL;
2942
2943 for (i = 0; i < num_values; i++) {
2944 ret = drm_property_add_enum(property, i,
2945 props[i].type,
2946 props[i].name);
2947 if (ret) {
2948 drm_property_destroy(dev, property);
2949 return NULL;
2950 }
2951 }
2952
2953 return property;
2954}
2955EXPORT_SYMBOL(drm_property_create_bitmask);
2956
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002957struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2958 const char *name,
2959 uint64_t min, uint64_t max)
2960{
2961 struct drm_property *property;
2962
2963 flags |= DRM_MODE_PROP_RANGE;
2964
2965 property = drm_property_create(dev, flags, name, 2);
2966 if (!property)
2967 return NULL;
2968
2969 property->values[0] = min;
2970 property->values[1] = max;
2971
2972 return property;
2973}
2974EXPORT_SYMBOL(drm_property_create_range);
2975
Dave Airlief453ba02008-11-07 14:05:41 -08002976int drm_property_add_enum(struct drm_property *property, int index,
2977 uint64_t value, const char *name)
2978{
2979 struct drm_property_enum *prop_enum;
2980
Rob Clark49e27542012-05-17 02:23:26 -06002981 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2982 return -EINVAL;
2983
2984 /*
2985 * Bitmask enum properties have the additional constraint of values
2986 * from 0 to 63
2987 */
2988 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002989 return -EINVAL;
2990
2991 if (!list_empty(&property->enum_blob_list)) {
2992 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2993 if (prop_enum->value == value) {
2994 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2995 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2996 return 0;
2997 }
2998 }
2999 }
3000
3001 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3002 if (!prop_enum)
3003 return -ENOMEM;
3004
3005 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3006 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3007 prop_enum->value = value;
3008
3009 property->values[index] = value;
3010 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3011 return 0;
3012}
3013EXPORT_SYMBOL(drm_property_add_enum);
3014
3015void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3016{
3017 struct drm_property_enum *prop_enum, *pt;
3018
3019 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3020 list_del(&prop_enum->head);
3021 kfree(prop_enum);
3022 }
3023
3024 if (property->num_values)
3025 kfree(property->values);
3026 drm_mode_object_put(dev, &property->base);
3027 list_del(&property->head);
3028 kfree(property);
3029}
3030EXPORT_SYMBOL(drm_property_destroy);
3031
Paulo Zanonic5431882012-05-15 18:09:02 -03003032void drm_object_attach_property(struct drm_mode_object *obj,
3033 struct drm_property *property,
3034 uint64_t init_val)
3035{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003036 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003037
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003038 if (count == DRM_OBJECT_MAX_PROPERTY) {
3039 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3040 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3041 "you see this message on the same object type.\n",
3042 obj->type);
3043 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003044 }
3045
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003046 obj->properties->ids[count] = property->base.id;
3047 obj->properties->values[count] = init_val;
3048 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003049}
3050EXPORT_SYMBOL(drm_object_attach_property);
3051
3052int drm_object_property_set_value(struct drm_mode_object *obj,
3053 struct drm_property *property, uint64_t val)
3054{
3055 int i;
3056
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003057 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003058 if (obj->properties->ids[i] == property->base.id) {
3059 obj->properties->values[i] = val;
3060 return 0;
3061 }
3062 }
3063
3064 return -EINVAL;
3065}
3066EXPORT_SYMBOL(drm_object_property_set_value);
3067
3068int drm_object_property_get_value(struct drm_mode_object *obj,
3069 struct drm_property *property, uint64_t *val)
3070{
3071 int i;
3072
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003073 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003074 if (obj->properties->ids[i] == property->base.id) {
3075 *val = obj->properties->values[i];
3076 return 0;
3077 }
3078 }
3079
3080 return -EINVAL;
3081}
3082EXPORT_SYMBOL(drm_object_property_get_value);
3083
Dave Airlief453ba02008-11-07 14:05:41 -08003084int drm_mode_getproperty_ioctl(struct drm_device *dev,
3085 void *data, struct drm_file *file_priv)
3086{
3087 struct drm_mode_object *obj;
3088 struct drm_mode_get_property *out_resp = data;
3089 struct drm_property *property;
3090 int enum_count = 0;
3091 int blob_count = 0;
3092 int value_count = 0;
3093 int ret = 0, i;
3094 int copied;
3095 struct drm_property_enum *prop_enum;
3096 struct drm_mode_property_enum __user *enum_ptr;
3097 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003098 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003099 uint64_t __user *values_ptr;
3100 uint32_t __user *blob_length_ptr;
3101
Dave Airliefb3b06c2011-02-08 13:55:21 +10003102 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3103 return -EINVAL;
3104
Daniel Vetter84849902012-12-02 00:28:11 +01003105 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003106 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3107 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003108 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003109 goto done;
3110 }
3111 property = obj_to_property(obj);
3112
Rob Clark49e27542012-05-17 02:23:26 -06003113 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003114 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3115 enum_count++;
3116 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3117 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3118 blob_count++;
3119 }
3120
3121 value_count = property->num_values;
3122
3123 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3124 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3125 out_resp->flags = property->flags;
3126
3127 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003128 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003129 for (i = 0; i < value_count; i++) {
3130 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3131 ret = -EFAULT;
3132 goto done;
3133 }
3134 }
3135 }
3136 out_resp->count_values = value_count;
3137
Rob Clark49e27542012-05-17 02:23:26 -06003138 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003139 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3140 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003141 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003142 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3143
3144 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3145 ret = -EFAULT;
3146 goto done;
3147 }
3148
3149 if (copy_to_user(&enum_ptr[copied].name,
3150 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3151 ret = -EFAULT;
3152 goto done;
3153 }
3154 copied++;
3155 }
3156 }
3157 out_resp->count_enum_blobs = enum_count;
3158 }
3159
3160 if (property->flags & DRM_MODE_PROP_BLOB) {
3161 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3162 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003163 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3164 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003165
3166 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3167 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3168 ret = -EFAULT;
3169 goto done;
3170 }
3171
3172 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3173 ret = -EFAULT;
3174 goto done;
3175 }
3176
3177 copied++;
3178 }
3179 }
3180 out_resp->count_enum_blobs = blob_count;
3181 }
3182done:
Daniel Vetter84849902012-12-02 00:28:11 +01003183 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003184 return ret;
3185}
3186
3187static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3188 void *data)
3189{
3190 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003191 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003192
3193 if (!length || !data)
3194 return NULL;
3195
3196 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3197 if (!blob)
3198 return NULL;
3199
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003200 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3201 if (ret) {
3202 kfree(blob);
3203 return NULL;
3204 }
3205
Dave Airlief453ba02008-11-07 14:05:41 -08003206 blob->length = length;
3207
3208 memcpy(blob->data, data, length);
3209
Dave Airlief453ba02008-11-07 14:05:41 -08003210 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3211 return blob;
3212}
3213
3214static void drm_property_destroy_blob(struct drm_device *dev,
3215 struct drm_property_blob *blob)
3216{
3217 drm_mode_object_put(dev, &blob->base);
3218 list_del(&blob->head);
3219 kfree(blob);
3220}
3221
3222int drm_mode_getblob_ioctl(struct drm_device *dev,
3223 void *data, struct drm_file *file_priv)
3224{
3225 struct drm_mode_object *obj;
3226 struct drm_mode_get_blob *out_resp = data;
3227 struct drm_property_blob *blob;
3228 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003229 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003230
Dave Airliefb3b06c2011-02-08 13:55:21 +10003231 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3232 return -EINVAL;
3233
Daniel Vetter84849902012-12-02 00:28:11 +01003234 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003235 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3236 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003237 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003238 goto done;
3239 }
3240 blob = obj_to_blob(obj);
3241
3242 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003243 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003244 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3245 ret = -EFAULT;
3246 goto done;
3247 }
3248 }
3249 out_resp->length = blob->length;
3250
3251done:
Daniel Vetter84849902012-12-02 00:28:11 +01003252 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003253 return ret;
3254}
3255
3256int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3257 struct edid *edid)
3258{
3259 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003260 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003261
3262 if (connector->edid_blob_ptr)
3263 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3264
3265 /* Delete edid, when there is none. */
3266 if (!edid) {
3267 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003268 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003269 return ret;
3270 }
3271
Adam Jackson7466f4c2010-03-29 21:43:23 +00003272 size = EDID_LENGTH * (1 + edid->extensions);
3273 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3274 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003275 if (!connector->edid_blob_ptr)
3276 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003277
Rob Clark58495562012-10-11 20:50:56 -05003278 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003279 dev->mode_config.edid_property,
3280 connector->edid_blob_ptr->base.id);
3281
3282 return ret;
3283}
3284EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3285
Paulo Zanoni26a34812012-05-15 18:08:59 -03003286static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003287 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003288{
3289 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3290 return false;
3291 if (property->flags & DRM_MODE_PROP_RANGE) {
3292 if (value < property->values[0] || value > property->values[1])
3293 return false;
3294 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003295 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3296 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003297 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003298 for (i = 0; i < property->num_values; i++)
3299 valid_mask |= (1ULL << property->values[i]);
3300 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003301 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3302 /* Only the driver knows */
3303 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003304 } else {
3305 int i;
3306 for (i = 0; i < property->num_values; i++)
3307 if (property->values[i] == value)
3308 return true;
3309 return false;
3310 }
3311}
3312
Dave Airlief453ba02008-11-07 14:05:41 -08003313int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3314 void *data, struct drm_file *file_priv)
3315{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003316 struct drm_mode_connector_set_property *conn_set_prop = data;
3317 struct drm_mode_obj_set_property obj_set_prop = {
3318 .value = conn_set_prop->value,
3319 .prop_id = conn_set_prop->prop_id,
3320 .obj_id = conn_set_prop->connector_id,
3321 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3322 };
Dave Airlief453ba02008-11-07 14:05:41 -08003323
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003324 /* It does all the locking and checking we need */
3325 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003326}
3327
Paulo Zanonic5431882012-05-15 18:09:02 -03003328static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3329 struct drm_property *property,
3330 uint64_t value)
3331{
3332 int ret = -EINVAL;
3333 struct drm_connector *connector = obj_to_connector(obj);
3334
3335 /* Do DPMS ourselves */
3336 if (property == connector->dev->mode_config.dpms_property) {
3337 if (connector->funcs->dpms)
3338 (*connector->funcs->dpms)(connector, (int)value);
3339 ret = 0;
3340 } else if (connector->funcs->set_property)
3341 ret = connector->funcs->set_property(connector, property, value);
3342
3343 /* store the property value if successful */
3344 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003345 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003346 return ret;
3347}
3348
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003349static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3350 struct drm_property *property,
3351 uint64_t value)
3352{
3353 int ret = -EINVAL;
3354 struct drm_crtc *crtc = obj_to_crtc(obj);
3355
3356 if (crtc->funcs->set_property)
3357 ret = crtc->funcs->set_property(crtc, property, value);
3358 if (!ret)
3359 drm_object_property_set_value(obj, property, value);
3360
3361 return ret;
3362}
3363
Rob Clark4d939142012-05-17 02:23:27 -06003364static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3365 struct drm_property *property,
3366 uint64_t value)
3367{
3368 int ret = -EINVAL;
3369 struct drm_plane *plane = obj_to_plane(obj);
3370
3371 if (plane->funcs->set_property)
3372 ret = plane->funcs->set_property(plane, property, value);
3373 if (!ret)
3374 drm_object_property_set_value(obj, property, value);
3375
3376 return ret;
3377}
3378
Paulo Zanonic5431882012-05-15 18:09:02 -03003379int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3380 struct drm_file *file_priv)
3381{
3382 struct drm_mode_obj_get_properties *arg = data;
3383 struct drm_mode_object *obj;
3384 int ret = 0;
3385 int i;
3386 int copied = 0;
3387 int props_count = 0;
3388 uint32_t __user *props_ptr;
3389 uint64_t __user *prop_values_ptr;
3390
3391 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3392 return -EINVAL;
3393
Daniel Vetter84849902012-12-02 00:28:11 +01003394 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003395
3396 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3397 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003398 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003399 goto out;
3400 }
3401 if (!obj->properties) {
3402 ret = -EINVAL;
3403 goto out;
3404 }
3405
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003406 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003407
3408 /* This ioctl is called twice, once to determine how much space is
3409 * needed, and the 2nd time to fill it. */
3410 if ((arg->count_props >= props_count) && props_count) {
3411 copied = 0;
3412 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3413 prop_values_ptr = (uint64_t __user *)(unsigned long)
3414 (arg->prop_values_ptr);
3415 for (i = 0; i < props_count; i++) {
3416 if (put_user(obj->properties->ids[i],
3417 props_ptr + copied)) {
3418 ret = -EFAULT;
3419 goto out;
3420 }
3421 if (put_user(obj->properties->values[i],
3422 prop_values_ptr + copied)) {
3423 ret = -EFAULT;
3424 goto out;
3425 }
3426 copied++;
3427 }
3428 }
3429 arg->count_props = props_count;
3430out:
Daniel Vetter84849902012-12-02 00:28:11 +01003431 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003432 return ret;
3433}
3434
3435int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3436 struct drm_file *file_priv)
3437{
3438 struct drm_mode_obj_set_property *arg = data;
3439 struct drm_mode_object *arg_obj;
3440 struct drm_mode_object *prop_obj;
3441 struct drm_property *property;
3442 int ret = -EINVAL;
3443 int i;
3444
3445 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3446 return -EINVAL;
3447
Daniel Vetter84849902012-12-02 00:28:11 +01003448 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003449
3450 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003451 if (!arg_obj) {
3452 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003453 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003454 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003455 if (!arg_obj->properties)
3456 goto out;
3457
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003458 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003459 if (arg_obj->properties->ids[i] == arg->prop_id)
3460 break;
3461
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003462 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003463 goto out;
3464
3465 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3466 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003467 if (!prop_obj) {
3468 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003469 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003470 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003471 property = obj_to_property(prop_obj);
3472
3473 if (!drm_property_change_is_valid(property, arg->value))
3474 goto out;
3475
3476 switch (arg_obj->type) {
3477 case DRM_MODE_OBJECT_CONNECTOR:
3478 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3479 arg->value);
3480 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003481 case DRM_MODE_OBJECT_CRTC:
3482 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3483 break;
Rob Clark4d939142012-05-17 02:23:27 -06003484 case DRM_MODE_OBJECT_PLANE:
3485 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3486 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003487 }
3488
3489out:
Daniel Vetter84849902012-12-02 00:28:11 +01003490 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003491 return ret;
3492}
3493
Dave Airlief453ba02008-11-07 14:05:41 -08003494int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3495 struct drm_encoder *encoder)
3496{
3497 int i;
3498
3499 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3500 if (connector->encoder_ids[i] == 0) {
3501 connector->encoder_ids[i] = encoder->base.id;
3502 return 0;
3503 }
3504 }
3505 return -ENOMEM;
3506}
3507EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3508
3509void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3510 struct drm_encoder *encoder)
3511{
3512 int i;
3513 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3514 if (connector->encoder_ids[i] == encoder->base.id) {
3515 connector->encoder_ids[i] = 0;
3516 if (connector->encoder == encoder)
3517 connector->encoder = NULL;
3518 break;
3519 }
3520 }
3521}
3522EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3523
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003524int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003525 int gamma_size)
3526{
3527 crtc->gamma_size = gamma_size;
3528
3529 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3530 if (!crtc->gamma_store) {
3531 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003532 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003533 }
3534
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003535 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003536}
3537EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3538
3539int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3540 void *data, struct drm_file *file_priv)
3541{
3542 struct drm_mode_crtc_lut *crtc_lut = data;
3543 struct drm_mode_object *obj;
3544 struct drm_crtc *crtc;
3545 void *r_base, *g_base, *b_base;
3546 int size;
3547 int ret = 0;
3548
Dave Airliefb3b06c2011-02-08 13:55:21 +10003549 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3550 return -EINVAL;
3551
Daniel Vetter84849902012-12-02 00:28:11 +01003552 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003553 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3554 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003555 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003556 goto out;
3557 }
3558 crtc = obj_to_crtc(obj);
3559
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003560 if (crtc->funcs->gamma_set == NULL) {
3561 ret = -ENOSYS;
3562 goto out;
3563 }
3564
Dave Airlief453ba02008-11-07 14:05:41 -08003565 /* memcpy into gamma store */
3566 if (crtc_lut->gamma_size != crtc->gamma_size) {
3567 ret = -EINVAL;
3568 goto out;
3569 }
3570
3571 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3572 r_base = crtc->gamma_store;
3573 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3574 ret = -EFAULT;
3575 goto out;
3576 }
3577
3578 g_base = r_base + size;
3579 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3580 ret = -EFAULT;
3581 goto out;
3582 }
3583
3584 b_base = g_base + size;
3585 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3586 ret = -EFAULT;
3587 goto out;
3588 }
3589
James Simmons72034252010-08-03 01:33:19 +01003590 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003591
3592out:
Daniel Vetter84849902012-12-02 00:28:11 +01003593 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003594 return ret;
3595
3596}
3597
3598int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3599 void *data, struct drm_file *file_priv)
3600{
3601 struct drm_mode_crtc_lut *crtc_lut = data;
3602 struct drm_mode_object *obj;
3603 struct drm_crtc *crtc;
3604 void *r_base, *g_base, *b_base;
3605 int size;
3606 int ret = 0;
3607
Dave Airliefb3b06c2011-02-08 13:55:21 +10003608 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3609 return -EINVAL;
3610
Daniel Vetter84849902012-12-02 00:28:11 +01003611 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003612 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3613 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003614 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003615 goto out;
3616 }
3617 crtc = obj_to_crtc(obj);
3618
3619 /* memcpy into gamma store */
3620 if (crtc_lut->gamma_size != crtc->gamma_size) {
3621 ret = -EINVAL;
3622 goto out;
3623 }
3624
3625 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3626 r_base = crtc->gamma_store;
3627 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3628 ret = -EFAULT;
3629 goto out;
3630 }
3631
3632 g_base = r_base + size;
3633 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3634 ret = -EFAULT;
3635 goto out;
3636 }
3637
3638 b_base = g_base + size;
3639 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3640 ret = -EFAULT;
3641 goto out;
3642 }
3643out:
Daniel Vetter84849902012-12-02 00:28:11 +01003644 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003645 return ret;
3646}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003647
3648int drm_mode_page_flip_ioctl(struct drm_device *dev,
3649 void *data, struct drm_file *file_priv)
3650{
3651 struct drm_mode_crtc_page_flip *page_flip = data;
3652 struct drm_mode_object *obj;
3653 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003654 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003655 struct drm_pending_vblank_event *e = NULL;
3656 unsigned long flags;
3657 int ret = -EINVAL;
3658
3659 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3660 page_flip->reserved != 0)
3661 return -EINVAL;
3662
Keith Packard62f21042013-07-22 18:50:00 -07003663 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3664 return -EINVAL;
3665
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003666 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3667 if (!obj)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003668 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003669 crtc = obj_to_crtc(obj);
3670
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003671 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003672 if (crtc->fb == NULL) {
3673 /* The framebuffer is currently unbound, presumably
3674 * due to a hotplug event, that userspace has not
3675 * yet discovered.
3676 */
3677 ret = -EBUSY;
3678 goto out;
3679 }
3680
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003681 if (crtc->funcs->page_flip == NULL)
3682 goto out;
3683
Daniel Vetter786b99e2012-12-02 21:53:40 +01003684 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003685 if (!fb) {
3686 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003687 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003688 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003689
Damien Lespiauc11e9282013-09-25 16:45:30 +01003690 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3691 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003692 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003693
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003694 if (crtc->fb->pixel_format != fb->pixel_format) {
3695 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3696 ret = -EINVAL;
3697 goto out;
3698 }
3699
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003700 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3701 ret = -ENOMEM;
3702 spin_lock_irqsave(&dev->event_lock, flags);
3703 if (file_priv->event_space < sizeof e->event) {
3704 spin_unlock_irqrestore(&dev->event_lock, flags);
3705 goto out;
3706 }
3707 file_priv->event_space -= sizeof e->event;
3708 spin_unlock_irqrestore(&dev->event_lock, flags);
3709
3710 e = kzalloc(sizeof *e, GFP_KERNEL);
3711 if (e == NULL) {
3712 spin_lock_irqsave(&dev->event_lock, flags);
3713 file_priv->event_space += sizeof e->event;
3714 spin_unlock_irqrestore(&dev->event_lock, flags);
3715 goto out;
3716 }
3717
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003718 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003719 e->event.base.length = sizeof e->event;
3720 e->event.user_data = page_flip->user_data;
3721 e->base.event = &e->event.base;
3722 e->base.file_priv = file_priv;
3723 e->base.destroy =
3724 (void (*) (struct drm_pending_event *)) kfree;
3725 }
3726
Daniel Vetterb0d12322012-12-11 01:07:12 +01003727 old_fb = crtc->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07003728 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003729 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003730 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3731 spin_lock_irqsave(&dev->event_lock, flags);
3732 file_priv->event_space += sizeof e->event;
3733 spin_unlock_irqrestore(&dev->event_lock, flags);
3734 kfree(e);
3735 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003736 /* Keep the old fb, don't unref it. */
3737 old_fb = NULL;
3738 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003739 /*
3740 * Warn if the driver hasn't properly updated the crtc->fb
3741 * field to reflect that the new framebuffer is now used.
3742 * Failing to do so will screw with the reference counting
3743 * on framebuffers.
3744 */
3745 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003746 /* Unref only the old framebuffer. */
3747 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003748 }
3749
3750out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003751 if (fb)
3752 drm_framebuffer_unreference(fb);
3753 if (old_fb)
3754 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003755 mutex_unlock(&crtc->mutex);
3756
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003757 return ret;
3758}
Chris Wilsoneb033552011-01-24 15:11:08 +00003759
3760void drm_mode_config_reset(struct drm_device *dev)
3761{
3762 struct drm_crtc *crtc;
3763 struct drm_encoder *encoder;
3764 struct drm_connector *connector;
3765
3766 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3767 if (crtc->funcs->reset)
3768 crtc->funcs->reset(crtc);
3769
3770 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3771 if (encoder->funcs->reset)
3772 encoder->funcs->reset(encoder);
3773
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003774 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3775 connector->status = connector_status_unknown;
3776
Chris Wilsoneb033552011-01-24 15:11:08 +00003777 if (connector->funcs->reset)
3778 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003779 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003780}
3781EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003782
3783int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3784 void *data, struct drm_file *file_priv)
3785{
3786 struct drm_mode_create_dumb *args = data;
3787
3788 if (!dev->driver->dumb_create)
3789 return -ENOSYS;
3790 return dev->driver->dumb_create(file_priv, dev, args);
3791}
3792
3793int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3794 void *data, struct drm_file *file_priv)
3795{
3796 struct drm_mode_map_dumb *args = data;
3797
3798 /* call driver ioctl to get mmap offset */
3799 if (!dev->driver->dumb_map_offset)
3800 return -ENOSYS;
3801
3802 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3803}
3804
3805int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3806 void *data, struct drm_file *file_priv)
3807{
3808 struct drm_mode_destroy_dumb *args = data;
3809
3810 if (!dev->driver->dumb_destroy)
3811 return -ENOSYS;
3812
3813 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3814}
Dave Airlie248dbc22011-11-29 20:02:54 +00003815
3816/*
3817 * Just need to support RGB formats here for compat with code that doesn't
3818 * use pixel formats directly yet.
3819 */
3820void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3821 int *bpp)
3822{
3823 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003824 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003825 case DRM_FORMAT_RGB332:
3826 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003827 *depth = 8;
3828 *bpp = 8;
3829 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003830 case DRM_FORMAT_XRGB1555:
3831 case DRM_FORMAT_XBGR1555:
3832 case DRM_FORMAT_RGBX5551:
3833 case DRM_FORMAT_BGRX5551:
3834 case DRM_FORMAT_ARGB1555:
3835 case DRM_FORMAT_ABGR1555:
3836 case DRM_FORMAT_RGBA5551:
3837 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003838 *depth = 15;
3839 *bpp = 16;
3840 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003841 case DRM_FORMAT_RGB565:
3842 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003843 *depth = 16;
3844 *bpp = 16;
3845 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003846 case DRM_FORMAT_RGB888:
3847 case DRM_FORMAT_BGR888:
3848 *depth = 24;
3849 *bpp = 24;
3850 break;
3851 case DRM_FORMAT_XRGB8888:
3852 case DRM_FORMAT_XBGR8888:
3853 case DRM_FORMAT_RGBX8888:
3854 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003855 *depth = 24;
3856 *bpp = 32;
3857 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003858 case DRM_FORMAT_XRGB2101010:
3859 case DRM_FORMAT_XBGR2101010:
3860 case DRM_FORMAT_RGBX1010102:
3861 case DRM_FORMAT_BGRX1010102:
3862 case DRM_FORMAT_ARGB2101010:
3863 case DRM_FORMAT_ABGR2101010:
3864 case DRM_FORMAT_RGBA1010102:
3865 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003866 *depth = 30;
3867 *bpp = 32;
3868 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003869 case DRM_FORMAT_ARGB8888:
3870 case DRM_FORMAT_ABGR8888:
3871 case DRM_FORMAT_RGBA8888:
3872 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003873 *depth = 32;
3874 *bpp = 32;
3875 break;
3876 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003877 DRM_DEBUG_KMS("unsupported pixel format %s\n",
3878 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00003879 *depth = 0;
3880 *bpp = 0;
3881 break;
3882 }
3883}
3884EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003885
3886/**
3887 * drm_format_num_planes - get the number of planes for format
3888 * @format: pixel format (DRM_FORMAT_*)
3889 *
3890 * RETURNS:
3891 * The number of planes used by the specified pixel format.
3892 */
3893int drm_format_num_planes(uint32_t format)
3894{
3895 switch (format) {
3896 case DRM_FORMAT_YUV410:
3897 case DRM_FORMAT_YVU410:
3898 case DRM_FORMAT_YUV411:
3899 case DRM_FORMAT_YVU411:
3900 case DRM_FORMAT_YUV420:
3901 case DRM_FORMAT_YVU420:
3902 case DRM_FORMAT_YUV422:
3903 case DRM_FORMAT_YVU422:
3904 case DRM_FORMAT_YUV444:
3905 case DRM_FORMAT_YVU444:
3906 return 3;
3907 case DRM_FORMAT_NV12:
3908 case DRM_FORMAT_NV21:
3909 case DRM_FORMAT_NV16:
3910 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003911 case DRM_FORMAT_NV24:
3912 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003913 return 2;
3914 default:
3915 return 1;
3916 }
3917}
3918EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003919
3920/**
3921 * drm_format_plane_cpp - determine the bytes per pixel value
3922 * @format: pixel format (DRM_FORMAT_*)
3923 * @plane: plane index
3924 *
3925 * RETURNS:
3926 * The bytes per pixel value for the specified plane.
3927 */
3928int drm_format_plane_cpp(uint32_t format, int plane)
3929{
3930 unsigned int depth;
3931 int bpp;
3932
3933 if (plane >= drm_format_num_planes(format))
3934 return 0;
3935
3936 switch (format) {
3937 case DRM_FORMAT_YUYV:
3938 case DRM_FORMAT_YVYU:
3939 case DRM_FORMAT_UYVY:
3940 case DRM_FORMAT_VYUY:
3941 return 2;
3942 case DRM_FORMAT_NV12:
3943 case DRM_FORMAT_NV21:
3944 case DRM_FORMAT_NV16:
3945 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003946 case DRM_FORMAT_NV24:
3947 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003948 return plane ? 2 : 1;
3949 case DRM_FORMAT_YUV410:
3950 case DRM_FORMAT_YVU410:
3951 case DRM_FORMAT_YUV411:
3952 case DRM_FORMAT_YVU411:
3953 case DRM_FORMAT_YUV420:
3954 case DRM_FORMAT_YVU420:
3955 case DRM_FORMAT_YUV422:
3956 case DRM_FORMAT_YVU422:
3957 case DRM_FORMAT_YUV444:
3958 case DRM_FORMAT_YVU444:
3959 return 1;
3960 default:
3961 drm_fb_get_bpp_depth(format, &depth, &bpp);
3962 return bpp >> 3;
3963 }
3964}
3965EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003966
3967/**
3968 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3969 * @format: pixel format (DRM_FORMAT_*)
3970 *
3971 * RETURNS:
3972 * The horizontal chroma subsampling factor for the
3973 * specified pixel format.
3974 */
3975int drm_format_horz_chroma_subsampling(uint32_t format)
3976{
3977 switch (format) {
3978 case DRM_FORMAT_YUV411:
3979 case DRM_FORMAT_YVU411:
3980 case DRM_FORMAT_YUV410:
3981 case DRM_FORMAT_YVU410:
3982 return 4;
3983 case DRM_FORMAT_YUYV:
3984 case DRM_FORMAT_YVYU:
3985 case DRM_FORMAT_UYVY:
3986 case DRM_FORMAT_VYUY:
3987 case DRM_FORMAT_NV12:
3988 case DRM_FORMAT_NV21:
3989 case DRM_FORMAT_NV16:
3990 case DRM_FORMAT_NV61:
3991 case DRM_FORMAT_YUV422:
3992 case DRM_FORMAT_YVU422:
3993 case DRM_FORMAT_YUV420:
3994 case DRM_FORMAT_YVU420:
3995 return 2;
3996 default:
3997 return 1;
3998 }
3999}
4000EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4001
4002/**
4003 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4004 * @format: pixel format (DRM_FORMAT_*)
4005 *
4006 * RETURNS:
4007 * The vertical chroma subsampling factor for the
4008 * specified pixel format.
4009 */
4010int drm_format_vert_chroma_subsampling(uint32_t format)
4011{
4012 switch (format) {
4013 case DRM_FORMAT_YUV410:
4014 case DRM_FORMAT_YVU410:
4015 return 4;
4016 case DRM_FORMAT_YUV420:
4017 case DRM_FORMAT_YVU420:
4018 case DRM_FORMAT_NV12:
4019 case DRM_FORMAT_NV21:
4020 return 2;
4021 default:
4022 return 1;
4023 }
4024}
4025EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004026
4027/**
4028 * drm_mode_config_init - initialize DRM mode_configuration structure
4029 * @dev: DRM device
4030 *
4031 * Initialize @dev's mode_config structure, used for tracking the graphics
4032 * configuration of @dev.
4033 *
4034 * Since this initializes the modeset locks, no locking is possible. Which is no
4035 * problem, since this should happen single threaded at init time. It is the
4036 * driver's problem to ensure this guarantee.
4037 *
4038 */
4039void drm_mode_config_init(struct drm_device *dev)
4040{
4041 mutex_init(&dev->mode_config.mutex);
4042 mutex_init(&dev->mode_config.idr_mutex);
4043 mutex_init(&dev->mode_config.fb_lock);
4044 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4045 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4046 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004047 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004048 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4049 INIT_LIST_HEAD(&dev->mode_config.property_list);
4050 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4051 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4052 idr_init(&dev->mode_config.crtc_idr);
4053
4054 drm_modeset_lock_all(dev);
4055 drm_mode_create_standard_connector_properties(dev);
4056 drm_modeset_unlock_all(dev);
4057
4058 /* Just to be sure */
4059 dev->mode_config.num_fb = 0;
4060 dev->mode_config.num_connector = 0;
4061 dev->mode_config.num_crtc = 0;
4062 dev->mode_config.num_encoder = 0;
4063}
4064EXPORT_SYMBOL(drm_mode_config_init);
4065
4066/**
4067 * drm_mode_config_cleanup - free up DRM mode_config info
4068 * @dev: DRM device
4069 *
4070 * Free up all the connectors and CRTCs associated with this DRM device, then
4071 * free up the framebuffers and associated buffer objects.
4072 *
4073 * Note that since this /should/ happen single-threaded at driver/device
4074 * teardown time, no locking is required. It's the driver's job to ensure that
4075 * this guarantee actually holds true.
4076 *
4077 * FIXME: cleanup any dangling user buffer objects too
4078 */
4079void drm_mode_config_cleanup(struct drm_device *dev)
4080{
4081 struct drm_connector *connector, *ot;
4082 struct drm_crtc *crtc, *ct;
4083 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004084 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004085 struct drm_framebuffer *fb, *fbt;
4086 struct drm_property *property, *pt;
4087 struct drm_property_blob *blob, *bt;
4088 struct drm_plane *plane, *plt;
4089
4090 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4091 head) {
4092 encoder->funcs->destroy(encoder);
4093 }
4094
Sean Paul3b336ec2013-08-14 16:47:37 -04004095 list_for_each_entry_safe(bridge, brt,
4096 &dev->mode_config.bridge_list, head) {
4097 bridge->funcs->destroy(bridge);
4098 }
4099
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004100 list_for_each_entry_safe(connector, ot,
4101 &dev->mode_config.connector_list, head) {
4102 connector->funcs->destroy(connector);
4103 }
4104
4105 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4106 head) {
4107 drm_property_destroy(dev, property);
4108 }
4109
4110 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4111 head) {
4112 drm_property_destroy_blob(dev, blob);
4113 }
4114
4115 /*
4116 * Single-threaded teardown context, so it's not required to grab the
4117 * fb_lock to protect against concurrent fb_list access. Contrary, it
4118 * would actually deadlock with the drm_framebuffer_cleanup function.
4119 *
4120 * Also, if there are any framebuffers left, that's a driver leak now,
4121 * so politely WARN about this.
4122 */
4123 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4124 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4125 drm_framebuffer_remove(fb);
4126 }
4127
4128 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4129 head) {
4130 plane->funcs->destroy(plane);
4131 }
4132
4133 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4134 crtc->funcs->destroy(crtc);
4135 }
4136
4137 idr_destroy(&dev->mode_config.crtc_idr);
4138}
4139EXPORT_SYMBOL(drm_mode_config_cleanup);