blob: db058643b01d12e51bc11fcb9e8f653e17a3c40f [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
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400218void drm_connector_ida_init(void)
219{
220 int i;
221
222 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
223 ida_init(&drm_connector_enum_list[i].ida);
224}
225
226void drm_connector_ida_destroy(void)
227{
228 int i;
229
230 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
231 ida_destroy(&drm_connector_enum_list[i].ida);
232}
233
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000234const char *drm_get_encoder_name(const struct drm_encoder *encoder)
Dave Airlief453ba02008-11-07 14:05:41 -0800235{
236 static char buf[32];
237
238 snprintf(buf, 32, "%s-%d",
239 drm_encoder_enum_list[encoder->encoder_type].name,
240 encoder->base.id);
241 return buf;
242}
Dave Airlie13a81952009-09-07 15:45:33 +1000243EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800244
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000245const char *drm_get_connector_name(const struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -0800246{
247 static char buf[32];
248
249 snprintf(buf, 32, "%s-%d",
250 drm_connector_enum_list[connector->connector_type].name,
251 connector->connector_type_id);
252 return buf;
253}
254EXPORT_SYMBOL(drm_get_connector_name);
255
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000256const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800257{
258 if (status == connector_status_connected)
259 return "connected";
260 else if (status == connector_status_disconnected)
261 return "disconnected";
262 else
263 return "unknown";
264}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000265EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800266
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300267static char printable_char(int c)
268{
269 return isascii(c) && isprint(c) ? c : '?';
270}
271
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000272const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300273{
274 static char buf[32];
275
276 snprintf(buf, sizeof(buf),
277 "%c%c%c%c %s-endian (0x%08x)",
278 printable_char(format & 0xff),
279 printable_char((format >> 8) & 0xff),
280 printable_char((format >> 16) & 0xff),
281 printable_char((format >> 24) & 0x7f),
282 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
283 format);
284
285 return buf;
286}
287EXPORT_SYMBOL(drm_get_format_name);
288
Dave Airlief453ba02008-11-07 14:05:41 -0800289/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100290 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800291 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100292 * @obj: object pointer, used to generate unique ID
293 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800294 *
Dave Airlief453ba02008-11-07 14:05:41 -0800295 * Create a unique identifier based on @ptr in @dev's identifier space. Used
296 * for tracking modes, CRTCs and connectors.
297 *
298 * RETURNS:
299 * New unique (relative to other objects in @dev) integer identifier for the
300 * object.
301 */
302static int drm_mode_object_get(struct drm_device *dev,
303 struct drm_mode_object *obj, uint32_t obj_type)
304{
Dave Airlief453ba02008-11-07 14:05:41 -0800305 int ret;
306
Jesse Barnesad2563c2009-01-19 17:21:45 +1000307 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800308 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
309 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100310 /*
311 * Set up the object linking under the protection of the idr
312 * lock so that other users can't see inconsistent state.
313 */
Tejun Heo2e928812013-02-27 17:04:08 -0800314 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100315 obj->type = obj_type;
316 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000317 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100318
Tejun Heo2e928812013-02-27 17:04:08 -0800319 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800320}
321
322/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100323 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800324 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100325 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800326 *
Dave Airlief453ba02008-11-07 14:05:41 -0800327 * Free @id from @dev's unique identifier pool.
328 */
329static void drm_mode_object_put(struct drm_device *dev,
330 struct drm_mode_object *object)
331{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000332 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800333 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000334 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800335}
336
Daniel Vetter786b99e2012-12-02 21:53:40 +0100337/**
338 * drm_mode_object_find - look up a drm object with static lifetime
339 * @dev: drm device
340 * @id: id of the mode object
341 * @type: type of the mode object
342 *
343 * Note that framebuffers cannot be looked up with this functions - since those
344 * are reference counted, they need special treatment.
345 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200346struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
347 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800348{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000349 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800350
Daniel Vetter786b99e2012-12-02 21:53:40 +0100351 /* Framebuffers are reference counted and need their own lookup
352 * function.*/
353 WARN_ON(type == DRM_MODE_OBJECT_FB);
354
Jesse Barnesad2563c2009-01-19 17:21:45 +1000355 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800356 obj = idr_find(&dev->mode_config.crtc_idr, id);
357 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000358 obj = NULL;
359 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800360
361 return obj;
362}
363EXPORT_SYMBOL(drm_mode_object_find);
364
365/**
Dave Airlief453ba02008-11-07 14:05:41 -0800366 * drm_framebuffer_init - initialize a framebuffer
367 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100368 * @fb: framebuffer to be initialized
369 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800370 *
Dave Airlief453ba02008-11-07 14:05:41 -0800371 * Allocates an ID for the framebuffer's parent mode object, sets its mode
372 * functions & device file and adds it to the master fd list.
373 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100374 * IMPORTANT:
375 * This functions publishes the fb and makes it available for concurrent access
376 * by other users. Which means by this point the fb _must_ be fully set up -
377 * since all the fb attributes are invariant over its lifetime, no further
378 * locking but only correct reference counting is required.
379 *
Dave Airlief453ba02008-11-07 14:05:41 -0800380 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200381 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800382 */
383int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
384 const struct drm_framebuffer_funcs *funcs)
385{
386 int ret;
387
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100388 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000389 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100390 INIT_LIST_HEAD(&fb->filp_head);
391 fb->dev = dev;
392 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000393
Dave Airlief453ba02008-11-07 14:05:41 -0800394 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200395 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100396 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800397
Daniel Vetter2b677e82012-12-10 21:16:05 +0100398 /* Grab the idr reference. */
399 drm_framebuffer_reference(fb);
400
Dave Airlief453ba02008-11-07 14:05:41 -0800401 dev->mode_config.num_fb++;
402 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100403out:
404 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800405
406 return 0;
407}
408EXPORT_SYMBOL(drm_framebuffer_init);
409
Rob Clarkf7eff602012-09-05 21:48:38 +0000410static void drm_framebuffer_free(struct kref *kref)
411{
412 struct drm_framebuffer *fb =
413 container_of(kref, struct drm_framebuffer, refcount);
414 fb->funcs->destroy(fb);
415}
416
Daniel Vetter2b677e82012-12-10 21:16:05 +0100417static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
418 uint32_t id)
419{
420 struct drm_mode_object *obj = NULL;
421 struct drm_framebuffer *fb;
422
423 mutex_lock(&dev->mode_config.idr_mutex);
424 obj = idr_find(&dev->mode_config.crtc_idr, id);
425 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
426 fb = NULL;
427 else
428 fb = obj_to_fb(obj);
429 mutex_unlock(&dev->mode_config.idr_mutex);
430
431 return fb;
432}
433
Rob Clarkf7eff602012-09-05 21:48:38 +0000434/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100435 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
436 * @dev: drm device
437 * @id: id of the fb object
438 *
439 * If successful, this grabs an additional reference to the framebuffer -
440 * callers need to make sure to eventually unreference the returned framebuffer
441 * again.
442 */
443struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
444 uint32_t id)
445{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100446 struct drm_framebuffer *fb;
447
448 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100449 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100450 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000451 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100452 mutex_unlock(&dev->mode_config.fb_lock);
453
454 return fb;
455}
456EXPORT_SYMBOL(drm_framebuffer_lookup);
457
458/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000459 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100460 * @fb: framebuffer to unref
461 *
462 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000463 */
464void drm_framebuffer_unreference(struct drm_framebuffer *fb)
465{
Rob Clarkf7eff602012-09-05 21:48:38 +0000466 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000467 kref_put(&fb->refcount, drm_framebuffer_free);
468}
469EXPORT_SYMBOL(drm_framebuffer_unreference);
470
471/**
472 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100473 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000474 */
475void drm_framebuffer_reference(struct drm_framebuffer *fb)
476{
477 DRM_DEBUG("FB ID: %d\n", fb->base.id);
478 kref_get(&fb->refcount);
479}
480EXPORT_SYMBOL(drm_framebuffer_reference);
481
Daniel Vetter2b677e82012-12-10 21:16:05 +0100482static void drm_framebuffer_free_bug(struct kref *kref)
483{
484 BUG();
485}
486
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100487static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
488{
489 DRM_DEBUG("FB ID: %d\n", fb->base.id);
490 kref_put(&fb->refcount, drm_framebuffer_free_bug);
491}
492
Daniel Vetter2b677e82012-12-10 21:16:05 +0100493/* dev->mode_config.fb_lock must be held! */
494static void __drm_framebuffer_unregister(struct drm_device *dev,
495 struct drm_framebuffer *fb)
496{
497 mutex_lock(&dev->mode_config.idr_mutex);
498 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
499 mutex_unlock(&dev->mode_config.idr_mutex);
500
501 fb->base.id = 0;
502
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100503 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100504}
505
Dave Airlief453ba02008-11-07 14:05:41 -0800506/**
Daniel Vetter36206362012-12-10 20:42:17 +0100507 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
508 * @fb: fb to unregister
509 *
510 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
511 * those used for fbdev. Note that the caller must hold a reference of it's own,
512 * i.e. the object may not be destroyed through this call (since it'll lead to a
513 * locking inversion).
514 */
515void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
516{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100517 struct drm_device *dev = fb->dev;
518
519 mutex_lock(&dev->mode_config.fb_lock);
520 /* Mark fb as reaped and drop idr ref. */
521 __drm_framebuffer_unregister(dev, fb);
522 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100523}
524EXPORT_SYMBOL(drm_framebuffer_unregister_private);
525
526/**
Dave Airlief453ba02008-11-07 14:05:41 -0800527 * drm_framebuffer_cleanup - remove a framebuffer object
528 * @fb: framebuffer to remove
529 *
Daniel Vetter36206362012-12-10 20:42:17 +0100530 * Cleanup references to a user-created framebuffer. This function is intended
531 * to be used from the drivers ->destroy callback.
532 *
533 * Note that this function does not remove the fb from active usuage - if it is
534 * still used anywhere, hilarity can ensue since userspace could call getfb on
535 * the id and get back -EINVAL. Obviously no concern at driver unload time.
536 *
537 * Also, the framebuffer will not be removed from the lookup idr - for
538 * user-created framebuffers this will happen in in the rmfb ioctl. For
539 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
540 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800541 */
542void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
543{
544 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100545
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100546 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000547 list_del(&fb->head);
548 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100549 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000550}
551EXPORT_SYMBOL(drm_framebuffer_cleanup);
552
553/**
554 * drm_framebuffer_remove - remove and unreference a framebuffer object
555 * @fb: framebuffer to remove
556 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000557 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100558 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100559 * passed-in framebuffer. Might take the modeset locks.
560 *
561 * Note that this function optimizes the cleanup away if the caller holds the
562 * last reference to the framebuffer. It is also guaranteed to not take the
563 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000564 */
565void drm_framebuffer_remove(struct drm_framebuffer *fb)
566{
567 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800568 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800569 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000570 struct drm_mode_set set;
571 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800572
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100573 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100574
Daniel Vetterb62584e2012-12-11 16:51:35 +0100575 /*
576 * drm ABI mandates that we remove any deleted framebuffers from active
577 * useage. But since most sane clients only remove framebuffers they no
578 * longer need, try to optimize this away.
579 *
580 * Since we're holding a reference ourselves, observing a refcount of 1
581 * means that we're the last holder and can skip it. Also, the refcount
582 * can never increase from 1 again, so we don't need any barriers or
583 * locks.
584 *
585 * Note that userspace could try to race with use and instate a new
586 * usage _after_ we've cleared all current ones. End result will be an
587 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
588 * in this manner.
589 */
590 if (atomic_read(&fb->refcount.refcount) > 1) {
591 drm_modeset_lock_all(dev);
592 /* remove from any CRTC */
593 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
594 if (crtc->fb == fb) {
595 /* should turn off the crtc */
596 memset(&set, 0, sizeof(struct drm_mode_set));
597 set.crtc = crtc;
598 set.fb = NULL;
599 ret = drm_mode_set_config_internal(&set);
600 if (ret)
601 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
602 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000603 }
Dave Airlief453ba02008-11-07 14:05:41 -0800604
Daniel Vetterb62584e2012-12-11 16:51:35 +0100605 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300606 if (plane->fb == fb)
607 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800608 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100609 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800610 }
611
Rob Clarkf7eff602012-09-05 21:48:38 +0000612 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800613}
Rob Clarkf7eff602012-09-05 21:48:38 +0000614EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800615
616/**
617 * drm_crtc_init - Initialise a new CRTC object
618 * @dev: DRM device
619 * @crtc: CRTC object to init
620 * @funcs: callbacks for the new CRTC
621 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000622 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200623 *
624 * RETURNS:
625 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800626 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200627int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800628 const struct drm_crtc_funcs *funcs)
629{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200630 int ret;
631
Dave Airlief453ba02008-11-07 14:05:41 -0800632 crtc->dev = dev;
633 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000634 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800635
Daniel Vetter84849902012-12-02 00:28:11 +0100636 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100637 mutex_init(&crtc->mutex);
638 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200639
640 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
641 if (ret)
642 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800643
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300644 crtc->base.properties = &crtc->properties;
645
Dave Airlief453ba02008-11-07 14:05:41 -0800646 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
647 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200648
649 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100650 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200651
652 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800653}
654EXPORT_SYMBOL(drm_crtc_init);
655
656/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000657 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800658 * @crtc: CRTC to cleanup
659 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000660 * This function cleans up @crtc and removes it from the DRM mode setting
661 * core. Note that the function does *not* free the crtc structure itself,
662 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800663 */
664void drm_crtc_cleanup(struct drm_crtc *crtc)
665{
666 struct drm_device *dev = crtc->dev;
667
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000668 kfree(crtc->gamma_store);
669 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800670
671 drm_mode_object_put(dev, &crtc->base);
672 list_del(&crtc->head);
673 dev->mode_config.num_crtc--;
674}
675EXPORT_SYMBOL(drm_crtc_cleanup);
676
677/**
678 * drm_mode_probed_add - add a mode to a connector's probed mode list
679 * @connector: connector the new mode
680 * @mode: mode data
681 *
Dave Airlief453ba02008-11-07 14:05:41 -0800682 * Add @mode to @connector's mode list for later use.
683 */
684void drm_mode_probed_add(struct drm_connector *connector,
685 struct drm_display_mode *mode)
686{
Ville Syrjälä990256a2013-05-31 12:17:07 +0000687 list_add_tail(&mode->head, &connector->probed_modes);
Dave Airlief453ba02008-11-07 14:05:41 -0800688}
689EXPORT_SYMBOL(drm_mode_probed_add);
690
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100691/*
Dave Airlief453ba02008-11-07 14:05:41 -0800692 * drm_mode_remove - remove and free a mode
693 * @connector: connector list to modify
694 * @mode: mode to remove
695 *
Dave Airlief453ba02008-11-07 14:05:41 -0800696 * Remove @mode from @connector's mode list, then free it.
697 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100698static void drm_mode_remove(struct drm_connector *connector,
699 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800700{
701 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100702 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800703}
Dave Airlief453ba02008-11-07 14:05:41 -0800704
705/**
706 * drm_connector_init - Init a preallocated connector
707 * @dev: DRM device
708 * @connector: the connector to init
709 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100710 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800711 *
Dave Airlief453ba02008-11-07 14:05:41 -0800712 * Initialises a preallocated connector. Connectors should be
713 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200714 *
715 * RETURNS:
716 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800717 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200718int drm_connector_init(struct drm_device *dev,
719 struct drm_connector *connector,
720 const struct drm_connector_funcs *funcs,
721 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800722{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200723 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400724 struct ida *connector_ida =
725 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200726
Daniel Vetter84849902012-12-02 00:28:11 +0100727 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800728
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200729 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
730 if (ret)
731 goto out;
732
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300733 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800734 connector->dev = dev;
735 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800736 connector->connector_type = connector_type;
737 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400738 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
739 if (connector->connector_type_id < 0) {
740 ret = connector->connector_type_id;
741 drm_mode_object_put(dev, &connector->base);
742 goto out;
743 }
Dave Airlief453ba02008-11-07 14:05:41 -0800744 INIT_LIST_HEAD(&connector->probed_modes);
745 INIT_LIST_HEAD(&connector->modes);
746 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000747 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800748
749 list_add_tail(&connector->head, &dev->mode_config.connector_list);
750 dev->mode_config.num_connector++;
751
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200752 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500753 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200754 dev->mode_config.edid_property,
755 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800756
Rob Clark58495562012-10-11 20:50:56 -0500757 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800758 dev->mode_config.dpms_property, 0);
759
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200760 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100761 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200762
763 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800764}
765EXPORT_SYMBOL(drm_connector_init);
766
767/**
768 * drm_connector_cleanup - cleans up an initialised connector
769 * @connector: connector to cleanup
770 *
Dave Airlief453ba02008-11-07 14:05:41 -0800771 * Cleans up the connector but doesn't free the object.
772 */
773void drm_connector_cleanup(struct drm_connector *connector)
774{
775 struct drm_device *dev = connector->dev;
776 struct drm_display_mode *mode, *t;
777
778 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
779 drm_mode_remove(connector, mode);
780
781 list_for_each_entry_safe(mode, t, &connector->modes, head)
782 drm_mode_remove(connector, mode);
783
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400784 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
785 connector->connector_type_id);
786
Dave Airlief453ba02008-11-07 14:05:41 -0800787 drm_mode_object_put(dev, &connector->base);
788 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000789 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800790}
791EXPORT_SYMBOL(drm_connector_cleanup);
792
Dave Airliecbc7e222012-02-20 14:16:40 +0000793void drm_connector_unplug_all(struct drm_device *dev)
794{
795 struct drm_connector *connector;
796
797 /* taking the mode config mutex ends up in a clash with sysfs */
798 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
799 drm_sysfs_connector_remove(connector);
800
801}
802EXPORT_SYMBOL(drm_connector_unplug_all);
803
Sean Paul3b336ec2013-08-14 16:47:37 -0400804int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
805 const struct drm_bridge_funcs *funcs)
806{
807 int ret;
808
809 drm_modeset_lock_all(dev);
810
811 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
812 if (ret)
813 goto out;
814
815 bridge->dev = dev;
816 bridge->funcs = funcs;
817
818 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
819 dev->mode_config.num_bridge++;
820
821 out:
822 drm_modeset_unlock_all(dev);
823 return ret;
824}
825EXPORT_SYMBOL(drm_bridge_init);
826
827void drm_bridge_cleanup(struct drm_bridge *bridge)
828{
829 struct drm_device *dev = bridge->dev;
830
831 drm_modeset_lock_all(dev);
832 drm_mode_object_put(dev, &bridge->base);
833 list_del(&bridge->head);
834 dev->mode_config.num_bridge--;
835 drm_modeset_unlock_all(dev);
836}
837EXPORT_SYMBOL(drm_bridge_cleanup);
838
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200839int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000840 struct drm_encoder *encoder,
841 const struct drm_encoder_funcs *funcs,
842 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800843{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200844 int ret;
845
Daniel Vetter84849902012-12-02 00:28:11 +0100846 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800847
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200848 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
849 if (ret)
850 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800851
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200852 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800853 encoder->encoder_type = encoder_type;
854 encoder->funcs = funcs;
855
856 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
857 dev->mode_config.num_encoder++;
858
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200859 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100860 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200861
862 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800863}
864EXPORT_SYMBOL(drm_encoder_init);
865
866void drm_encoder_cleanup(struct drm_encoder *encoder)
867{
868 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100869 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800870 drm_mode_object_put(dev, &encoder->base);
871 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000872 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100873 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800874}
875EXPORT_SYMBOL(drm_encoder_cleanup);
876
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300877/**
878 * drm_plane_init - Initialise a new plane object
879 * @dev: DRM device
880 * @plane: plane object to init
881 * @possible_crtcs: bitmask of possible CRTCs
882 * @funcs: callbacks for the new plane
883 * @formats: array of supported formats (%DRM_FORMAT_*)
884 * @format_count: number of elements in @formats
885 * @priv: plane is private (hidden from userspace)?
886 *
887 * Inits a new object created as base part of a driver plane object.
888 *
889 * RETURNS:
890 * Zero on success, error code on failure.
891 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800892int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
893 unsigned long possible_crtcs,
894 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600895 const uint32_t *formats, uint32_t format_count,
896 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800897{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200898 int ret;
899
Daniel Vetter84849902012-12-02 00:28:11 +0100900 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800901
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200902 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
903 if (ret)
904 goto out;
905
Rob Clark4d939142012-05-17 02:23:27 -0600906 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800907 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800908 plane->funcs = funcs;
909 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
910 GFP_KERNEL);
911 if (!plane->format_types) {
912 DRM_DEBUG_KMS("out of memory when allocating plane\n");
913 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200914 ret = -ENOMEM;
915 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800916 }
917
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800918 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800919 plane->format_count = format_count;
920 plane->possible_crtcs = possible_crtcs;
921
Rob Clark0a7eb242011-12-13 20:19:36 -0600922 /* private planes are not exposed to userspace, but depending on
923 * display hardware, might be convenient to allow sharing programming
924 * for the scanout engine with the crtc implementation.
925 */
926 if (!priv) {
927 list_add_tail(&plane->head, &dev->mode_config.plane_list);
928 dev->mode_config.num_plane++;
929 } else {
930 INIT_LIST_HEAD(&plane->head);
931 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800932
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200933 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100934 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800935
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200936 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800937}
938EXPORT_SYMBOL(drm_plane_init);
939
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300940/**
941 * drm_plane_cleanup - Clean up the core plane usage
942 * @plane: plane to cleanup
943 *
944 * This function cleans up @plane and removes it from the DRM mode setting
945 * core. Note that the function does *not* free the plane structure itself,
946 * this is the responsibility of the caller.
947 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800948void drm_plane_cleanup(struct drm_plane *plane)
949{
950 struct drm_device *dev = plane->dev;
951
Daniel Vetter84849902012-12-02 00:28:11 +0100952 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800953 kfree(plane->format_types);
954 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600955 /* if not added to a list, it must be a private plane */
956 if (!list_empty(&plane->head)) {
957 list_del(&plane->head);
958 dev->mode_config.num_plane--;
959 }
Daniel Vetter84849902012-12-02 00:28:11 +0100960 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800961}
962EXPORT_SYMBOL(drm_plane_cleanup);
963
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300964/**
965 * drm_plane_force_disable - Forcibly disable a plane
966 * @plane: plane to disable
967 *
968 * Forces the plane to be disabled.
969 *
970 * Used when the plane's current framebuffer is destroyed,
971 * and when restoring fbdev mode.
972 */
Ville Syrjälä9125e612013-06-03 16:10:40 +0300973void drm_plane_force_disable(struct drm_plane *plane)
974{
975 int ret;
976
977 if (!plane->fb)
978 return;
979
980 ret = plane->funcs->disable_plane(plane);
981 if (ret)
982 DRM_ERROR("failed to disable plane with busy fb\n");
983 /* disconnect the plane from the fb and crtc: */
984 __drm_framebuffer_unreference(plane->fb);
985 plane->fb = NULL;
986 plane->crtc = NULL;
987}
988EXPORT_SYMBOL(drm_plane_force_disable);
989
Dave Airlief453ba02008-11-07 14:05:41 -0800990/**
991 * drm_mode_create - create a new display mode
992 * @dev: DRM device
993 *
Dave Airlief453ba02008-11-07 14:05:41 -0800994 * Create a new drm_display_mode, give it an ID, and return it.
995 *
996 * RETURNS:
997 * Pointer to new mode on success, NULL on error.
998 */
999struct drm_display_mode *drm_mode_create(struct drm_device *dev)
1000{
1001 struct drm_display_mode *nmode;
1002
1003 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
1004 if (!nmode)
1005 return NULL;
1006
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001007 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
1008 kfree(nmode);
1009 return NULL;
1010 }
1011
Dave Airlief453ba02008-11-07 14:05:41 -08001012 return nmode;
1013}
1014EXPORT_SYMBOL(drm_mode_create);
1015
1016/**
1017 * drm_mode_destroy - remove a mode
1018 * @dev: DRM device
1019 * @mode: mode to remove
1020 *
Dave Airlief453ba02008-11-07 14:05:41 -08001021 * Free @mode's unique identifier, then free it.
1022 */
1023void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
1024{
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001025 if (!mode)
1026 return;
1027
Dave Airlief453ba02008-11-07 14:05:41 -08001028 drm_mode_object_put(dev, &mode->base);
1029
1030 kfree(mode);
1031}
1032EXPORT_SYMBOL(drm_mode_destroy);
1033
1034static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1035{
1036 struct drm_property *edid;
1037 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001038
1039 /*
1040 * Standard properties (apply to all connectors)
1041 */
1042 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1043 DRM_MODE_PROP_IMMUTABLE,
1044 "EDID", 0);
1045 dev->mode_config.edid_property = edid;
1046
Sascha Hauer4a67d392012-02-06 10:58:17 +01001047 dpms = drm_property_create_enum(dev, 0,
1048 "DPMS", drm_dpms_enum_list,
1049 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001050 dev->mode_config.dpms_property = dpms;
1051
1052 return 0;
1053}
1054
1055/**
1056 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1057 * @dev: DRM device
1058 *
1059 * Called by a driver the first time a DVI-I connector is made.
1060 */
1061int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1062{
1063 struct drm_property *dvi_i_selector;
1064 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001065
1066 if (dev->mode_config.dvi_i_select_subconnector_property)
1067 return 0;
1068
1069 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001070 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001071 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001072 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001073 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001074 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1075
Sascha Hauer4a67d392012-02-06 10:58:17 +01001076 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001077 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001078 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001079 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001080 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1081
1082 return 0;
1083}
1084EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1085
1086/**
1087 * drm_create_tv_properties - create TV specific connector properties
1088 * @dev: DRM device
1089 * @num_modes: number of different TV formats (modes) supported
1090 * @modes: array of pointers to strings containing name of each format
1091 *
1092 * Called by a driver's TV initialization routine, this function creates
1093 * the TV specific connector properties for a given device. Caller is
1094 * responsible for allocating a list of format names and passing them to
1095 * this routine.
1096 */
1097int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1098 char *modes[])
1099{
1100 struct drm_property *tv_selector;
1101 struct drm_property *tv_subconnector;
1102 int i;
1103
1104 if (dev->mode_config.tv_select_subconnector_property)
1105 return 0;
1106
1107 /*
1108 * Basic connector properties
1109 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001110 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001111 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001112 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001113 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001114 dev->mode_config.tv_select_subconnector_property = tv_selector;
1115
1116 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001117 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1118 "subconnector",
1119 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001120 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001121 dev->mode_config.tv_subconnector_property = tv_subconnector;
1122
1123 /*
1124 * Other, TV specific properties: margins & TV modes.
1125 */
1126 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001127 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001128
1129 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001130 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001131
1132 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001133 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001134
1135 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001136 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001137
1138 dev->mode_config.tv_mode_property =
1139 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1140 "mode", num_modes);
1141 for (i = 0; i < num_modes; i++)
1142 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1143 i, modes[i]);
1144
Francisco Jerezb6b79022009-08-02 04:19:20 +02001145 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001146 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001147
1148 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001149 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001150
1151 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001152 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001153
Francisco Jereza75f0232009-08-12 02:30:10 +02001154 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001155 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001156
1157 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001158 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001159
1160 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001161 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001162
Dave Airlief453ba02008-11-07 14:05:41 -08001163 return 0;
1164}
1165EXPORT_SYMBOL(drm_mode_create_tv_properties);
1166
1167/**
1168 * drm_mode_create_scaling_mode_property - create scaling mode property
1169 * @dev: DRM device
1170 *
1171 * Called by a driver the first time it's needed, must be attached to desired
1172 * connectors.
1173 */
1174int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1175{
1176 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001177
1178 if (dev->mode_config.scaling_mode_property)
1179 return 0;
1180
1181 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001182 drm_property_create_enum(dev, 0, "scaling mode",
1183 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001184 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001185
1186 dev->mode_config.scaling_mode_property = scaling_mode;
1187
1188 return 0;
1189}
1190EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1191
1192/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001193 * drm_mode_create_dirty_property - create dirty property
1194 * @dev: DRM device
1195 *
1196 * Called by a driver the first time it's needed, must be attached to desired
1197 * connectors.
1198 */
1199int drm_mode_create_dirty_info_property(struct drm_device *dev)
1200{
1201 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001202
1203 if (dev->mode_config.dirty_info_property)
1204 return 0;
1205
1206 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001207 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001208 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001209 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001210 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001211 dev->mode_config.dirty_info_property = dirty_info;
1212
1213 return 0;
1214}
1215EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1216
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001217static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001218{
1219 uint32_t total_objects = 0;
1220
1221 total_objects += dev->mode_config.num_crtc;
1222 total_objects += dev->mode_config.num_connector;
1223 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001224 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001225
Dave Airlief453ba02008-11-07 14:05:41 -08001226 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1227 if (!group->id_list)
1228 return -ENOMEM;
1229
1230 group->num_crtcs = 0;
1231 group->num_connectors = 0;
1232 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001233 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001234 return 0;
1235}
1236
1237int drm_mode_group_init_legacy_group(struct drm_device *dev,
1238 struct drm_mode_group *group)
1239{
1240 struct drm_crtc *crtc;
1241 struct drm_encoder *encoder;
1242 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001243 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001244 int ret;
1245
1246 if ((ret = drm_mode_group_init(dev, group)))
1247 return ret;
1248
1249 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1250 group->id_list[group->num_crtcs++] = crtc->base.id;
1251
1252 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1253 group->id_list[group->num_crtcs + group->num_encoders++] =
1254 encoder->base.id;
1255
1256 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1257 group->id_list[group->num_crtcs + group->num_encoders +
1258 group->num_connectors++] = connector->base.id;
1259
Sean Paul3b336ec2013-08-14 16:47:37 -04001260 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1261 group->id_list[group->num_crtcs + group->num_encoders +
1262 group->num_connectors + group->num_bridges++] =
1263 bridge->base.id;
1264
Dave Airlief453ba02008-11-07 14:05:41 -08001265 return 0;
1266}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001267EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001268
1269/**
Dave Airlief453ba02008-11-07 14:05:41 -08001270 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1271 * @out: drm_mode_modeinfo struct to return to the user
1272 * @in: drm_display_mode to use
1273 *
Dave Airlief453ba02008-11-07 14:05:41 -08001274 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1275 * the user.
1276 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001277static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1278 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001279{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001280 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1281 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1282 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1283 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1284 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1285 "timing values too large for mode info\n");
1286
Dave Airlief453ba02008-11-07 14:05:41 -08001287 out->clock = in->clock;
1288 out->hdisplay = in->hdisplay;
1289 out->hsync_start = in->hsync_start;
1290 out->hsync_end = in->hsync_end;
1291 out->htotal = in->htotal;
1292 out->hskew = in->hskew;
1293 out->vdisplay = in->vdisplay;
1294 out->vsync_start = in->vsync_start;
1295 out->vsync_end = in->vsync_end;
1296 out->vtotal = in->vtotal;
1297 out->vscan = in->vscan;
1298 out->vrefresh = in->vrefresh;
1299 out->flags = in->flags;
1300 out->type = in->type;
1301 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1302 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1303}
1304
1305/**
1306 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1307 * @out: drm_display_mode to return to the user
1308 * @in: drm_mode_modeinfo to use
1309 *
Dave Airlief453ba02008-11-07 14:05:41 -08001310 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1311 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001312 *
1313 * RETURNS:
1314 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001315 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001316static int drm_crtc_convert_umode(struct drm_display_mode *out,
1317 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001318{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001319 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1320 return -ERANGE;
1321
Damien Lespiaua3ff6d52013-09-25 16:45:25 +01001322 /* At most, 1 set bit describing the 3D layout of the mode */
1323 if (hweight32(in->flags & DRM_MODE_FLAG_3D_MASK) > 1)
1324 return -EINVAL;
1325
Dave Airlief453ba02008-11-07 14:05:41 -08001326 out->clock = in->clock;
1327 out->hdisplay = in->hdisplay;
1328 out->hsync_start = in->hsync_start;
1329 out->hsync_end = in->hsync_end;
1330 out->htotal = in->htotal;
1331 out->hskew = in->hskew;
1332 out->vdisplay = in->vdisplay;
1333 out->vsync_start = in->vsync_start;
1334 out->vsync_end = in->vsync_end;
1335 out->vtotal = in->vtotal;
1336 out->vscan = in->vscan;
1337 out->vrefresh = in->vrefresh;
1338 out->flags = in->flags;
1339 out->type = in->type;
1340 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1341 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001342
1343 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001344}
1345
1346/**
1347 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001348 * @dev: drm device for the ioctl
1349 * @data: data pointer for the ioctl
1350 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001351 *
Dave Airlief453ba02008-11-07 14:05:41 -08001352 * Construct a set of configuration description structures and return
1353 * them to the user, including CRTC, connector and framebuffer configuration.
1354 *
1355 * Called by the user via ioctl.
1356 *
1357 * RETURNS:
1358 * Zero on success, errno on failure.
1359 */
1360int drm_mode_getresources(struct drm_device *dev, void *data,
1361 struct drm_file *file_priv)
1362{
1363 struct drm_mode_card_res *card_res = data;
1364 struct list_head *lh;
1365 struct drm_framebuffer *fb;
1366 struct drm_connector *connector;
1367 struct drm_crtc *crtc;
1368 struct drm_encoder *encoder;
1369 int ret = 0;
1370 int connector_count = 0;
1371 int crtc_count = 0;
1372 int fb_count = 0;
1373 int encoder_count = 0;
1374 int copied = 0, i;
1375 uint32_t __user *fb_id;
1376 uint32_t __user *crtc_id;
1377 uint32_t __user *connector_id;
1378 uint32_t __user *encoder_id;
1379 struct drm_mode_group *mode_group;
1380
Dave Airliefb3b06c2011-02-08 13:55:21 +10001381 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1382 return -EINVAL;
1383
Dave Airlief453ba02008-11-07 14:05:41 -08001384
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001385 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001386 /*
1387 * For the non-control nodes we need to limit the list of resources
1388 * by IDs in the group list for this node
1389 */
1390 list_for_each(lh, &file_priv->fbs)
1391 fb_count++;
1392
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001393 /* handle this in 4 parts */
1394 /* FBs */
1395 if (card_res->count_fbs >= fb_count) {
1396 copied = 0;
1397 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1398 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1399 if (put_user(fb->base.id, fb_id + copied)) {
1400 mutex_unlock(&file_priv->fbs_lock);
1401 return -EFAULT;
1402 }
1403 copied++;
1404 }
1405 }
1406 card_res->count_fbs = fb_count;
1407 mutex_unlock(&file_priv->fbs_lock);
1408
1409 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001410 mode_group = &file_priv->master->minor->mode_group;
1411 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1412
1413 list_for_each(lh, &dev->mode_config.crtc_list)
1414 crtc_count++;
1415
1416 list_for_each(lh, &dev->mode_config.connector_list)
1417 connector_count++;
1418
1419 list_for_each(lh, &dev->mode_config.encoder_list)
1420 encoder_count++;
1421 } else {
1422
1423 crtc_count = mode_group->num_crtcs;
1424 connector_count = mode_group->num_connectors;
1425 encoder_count = mode_group->num_encoders;
1426 }
1427
1428 card_res->max_height = dev->mode_config.max_height;
1429 card_res->min_height = dev->mode_config.min_height;
1430 card_res->max_width = dev->mode_config.max_width;
1431 card_res->min_width = dev->mode_config.min_width;
1432
Dave Airlief453ba02008-11-07 14:05:41 -08001433 /* CRTCs */
1434 if (card_res->count_crtcs >= crtc_count) {
1435 copied = 0;
1436 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1437 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1438 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1439 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001440 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001441 if (put_user(crtc->base.id, crtc_id + copied)) {
1442 ret = -EFAULT;
1443 goto out;
1444 }
1445 copied++;
1446 }
1447 } else {
1448 for (i = 0; i < mode_group->num_crtcs; i++) {
1449 if (put_user(mode_group->id_list[i],
1450 crtc_id + copied)) {
1451 ret = -EFAULT;
1452 goto out;
1453 }
1454 copied++;
1455 }
1456 }
1457 }
1458 card_res->count_crtcs = crtc_count;
1459
1460 /* Encoders */
1461 if (card_res->count_encoders >= encoder_count) {
1462 copied = 0;
1463 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1464 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1465 list_for_each_entry(encoder,
1466 &dev->mode_config.encoder_list,
1467 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001468 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1469 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001470 if (put_user(encoder->base.id, encoder_id +
1471 copied)) {
1472 ret = -EFAULT;
1473 goto out;
1474 }
1475 copied++;
1476 }
1477 } else {
1478 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1479 if (put_user(mode_group->id_list[i],
1480 encoder_id + copied)) {
1481 ret = -EFAULT;
1482 goto out;
1483 }
1484 copied++;
1485 }
1486
1487 }
1488 }
1489 card_res->count_encoders = encoder_count;
1490
1491 /* Connectors */
1492 if (card_res->count_connectors >= connector_count) {
1493 copied = 0;
1494 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1495 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1496 list_for_each_entry(connector,
1497 &dev->mode_config.connector_list,
1498 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001499 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1500 connector->base.id,
1501 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001502 if (put_user(connector->base.id,
1503 connector_id + copied)) {
1504 ret = -EFAULT;
1505 goto out;
1506 }
1507 copied++;
1508 }
1509 } else {
1510 int start = mode_group->num_crtcs +
1511 mode_group->num_encoders;
1512 for (i = start; i < start + mode_group->num_connectors; i++) {
1513 if (put_user(mode_group->id_list[i],
1514 connector_id + copied)) {
1515 ret = -EFAULT;
1516 goto out;
1517 }
1518 copied++;
1519 }
1520 }
1521 }
1522 card_res->count_connectors = connector_count;
1523
Jerome Glisse94401062010-07-15 15:43:25 -04001524 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001525 card_res->count_connectors, card_res->count_encoders);
1526
1527out:
Daniel Vetter84849902012-12-02 00:28:11 +01001528 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001529 return ret;
1530}
1531
1532/**
1533 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001534 * @dev: drm device for the ioctl
1535 * @data: data pointer for the ioctl
1536 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001537 *
Dave Airlief453ba02008-11-07 14:05:41 -08001538 * Construct a CRTC configuration structure to return to the user.
1539 *
1540 * Called by the user via ioctl.
1541 *
1542 * RETURNS:
1543 * Zero on success, errno on failure.
1544 */
1545int drm_mode_getcrtc(struct drm_device *dev,
1546 void *data, struct drm_file *file_priv)
1547{
1548 struct drm_mode_crtc *crtc_resp = data;
1549 struct drm_crtc *crtc;
1550 struct drm_mode_object *obj;
1551 int ret = 0;
1552
Dave Airliefb3b06c2011-02-08 13:55:21 +10001553 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1554 return -EINVAL;
1555
Daniel Vetter84849902012-12-02 00:28:11 +01001556 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001557
1558 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1559 DRM_MODE_OBJECT_CRTC);
1560 if (!obj) {
1561 ret = -EINVAL;
1562 goto out;
1563 }
1564 crtc = obj_to_crtc(obj);
1565
1566 crtc_resp->x = crtc->x;
1567 crtc_resp->y = crtc->y;
1568 crtc_resp->gamma_size = crtc->gamma_size;
1569 if (crtc->fb)
1570 crtc_resp->fb_id = crtc->fb->base.id;
1571 else
1572 crtc_resp->fb_id = 0;
1573
1574 if (crtc->enabled) {
1575
1576 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1577 crtc_resp->mode_valid = 1;
1578
1579 } else {
1580 crtc_resp->mode_valid = 0;
1581 }
1582
1583out:
Daniel Vetter84849902012-12-02 00:28:11 +01001584 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001585 return ret;
1586}
1587
Damien Lespiau61d8e322013-09-25 16:45:22 +01001588static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1589 const struct drm_file *file_priv)
1590{
1591 /*
1592 * If user-space hasn't configured the driver to expose the stereo 3D
1593 * modes, don't expose them.
1594 */
1595 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1596 return false;
1597
1598 return true;
1599}
1600
Dave Airlief453ba02008-11-07 14:05:41 -08001601/**
1602 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001603 * @dev: drm device for the ioctl
1604 * @data: data pointer for the ioctl
1605 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001606 *
Dave Airlief453ba02008-11-07 14:05:41 -08001607 * Construct a connector configuration structure to return to the user.
1608 *
1609 * Called by the user via ioctl.
1610 *
1611 * RETURNS:
1612 * Zero on success, errno on failure.
1613 */
1614int drm_mode_getconnector(struct drm_device *dev, void *data,
1615 struct drm_file *file_priv)
1616{
1617 struct drm_mode_get_connector *out_resp = data;
1618 struct drm_mode_object *obj;
1619 struct drm_connector *connector;
1620 struct drm_display_mode *mode;
1621 int mode_count = 0;
1622 int props_count = 0;
1623 int encoders_count = 0;
1624 int ret = 0;
1625 int copied = 0;
1626 int i;
1627 struct drm_mode_modeinfo u_mode;
1628 struct drm_mode_modeinfo __user *mode_ptr;
1629 uint32_t __user *prop_ptr;
1630 uint64_t __user *prop_values;
1631 uint32_t __user *encoder_ptr;
1632
Dave Airliefb3b06c2011-02-08 13:55:21 +10001633 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1634 return -EINVAL;
1635
Dave Airlief453ba02008-11-07 14:05:41 -08001636 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1637
Jerome Glisse94401062010-07-15 15:43:25 -04001638 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001639
Daniel Vetter7b240562012-12-12 00:35:33 +01001640 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001641
1642 obj = drm_mode_object_find(dev, out_resp->connector_id,
1643 DRM_MODE_OBJECT_CONNECTOR);
1644 if (!obj) {
1645 ret = -EINVAL;
1646 goto out;
1647 }
1648 connector = obj_to_connector(obj);
1649
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001650 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001651
1652 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1653 if (connector->encoder_ids[i] != 0) {
1654 encoders_count++;
1655 }
1656 }
1657
1658 if (out_resp->count_modes == 0) {
1659 connector->funcs->fill_modes(connector,
1660 dev->mode_config.max_width,
1661 dev->mode_config.max_height);
1662 }
1663
1664 /* delayed so we get modes regardless of pre-fill_modes state */
1665 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001666 if (drm_mode_expose_to_userspace(mode, file_priv))
1667 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001668
1669 out_resp->connector_id = connector->base.id;
1670 out_resp->connector_type = connector->connector_type;
1671 out_resp->connector_type_id = connector->connector_type_id;
1672 out_resp->mm_width = connector->display_info.width_mm;
1673 out_resp->mm_height = connector->display_info.height_mm;
1674 out_resp->subpixel = connector->display_info.subpixel_order;
1675 out_resp->connection = connector->status;
1676 if (connector->encoder)
1677 out_resp->encoder_id = connector->encoder->base.id;
1678 else
1679 out_resp->encoder_id = 0;
1680
1681 /*
1682 * This ioctl is called twice, once to determine how much space is
1683 * needed, and the 2nd time to fill it.
1684 */
1685 if ((out_resp->count_modes >= mode_count) && mode_count) {
1686 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001687 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001688 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001689 if (!drm_mode_expose_to_userspace(mode, file_priv))
1690 continue;
1691
Dave Airlief453ba02008-11-07 14:05:41 -08001692 drm_crtc_convert_to_umode(&u_mode, mode);
1693 if (copy_to_user(mode_ptr + copied,
1694 &u_mode, sizeof(u_mode))) {
1695 ret = -EFAULT;
1696 goto out;
1697 }
1698 copied++;
1699 }
1700 }
1701 out_resp->count_modes = mode_count;
1702
1703 if ((out_resp->count_props >= props_count) && props_count) {
1704 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001705 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1706 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001707 for (i = 0; i < connector->properties.count; i++) {
1708 if (put_user(connector->properties.ids[i],
1709 prop_ptr + copied)) {
1710 ret = -EFAULT;
1711 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001712 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001713
1714 if (put_user(connector->properties.values[i],
1715 prop_values + copied)) {
1716 ret = -EFAULT;
1717 goto out;
1718 }
1719 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001720 }
1721 }
1722 out_resp->count_props = props_count;
1723
1724 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1725 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001726 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001727 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1728 if (connector->encoder_ids[i] != 0) {
1729 if (put_user(connector->encoder_ids[i],
1730 encoder_ptr + copied)) {
1731 ret = -EFAULT;
1732 goto out;
1733 }
1734 copied++;
1735 }
1736 }
1737 }
1738 out_resp->count_encoders = encoders_count;
1739
1740out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001741 mutex_unlock(&dev->mode_config.mutex);
1742
Dave Airlief453ba02008-11-07 14:05:41 -08001743 return ret;
1744}
1745
1746int drm_mode_getencoder(struct drm_device *dev, void *data,
1747 struct drm_file *file_priv)
1748{
1749 struct drm_mode_get_encoder *enc_resp = data;
1750 struct drm_mode_object *obj;
1751 struct drm_encoder *encoder;
1752 int ret = 0;
1753
Dave Airliefb3b06c2011-02-08 13:55:21 +10001754 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1755 return -EINVAL;
1756
Daniel Vetter84849902012-12-02 00:28:11 +01001757 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001758 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1759 DRM_MODE_OBJECT_ENCODER);
1760 if (!obj) {
1761 ret = -EINVAL;
1762 goto out;
1763 }
1764 encoder = obj_to_encoder(obj);
1765
1766 if (encoder->crtc)
1767 enc_resp->crtc_id = encoder->crtc->base.id;
1768 else
1769 enc_resp->crtc_id = 0;
1770 enc_resp->encoder_type = encoder->encoder_type;
1771 enc_resp->encoder_id = encoder->base.id;
1772 enc_resp->possible_crtcs = encoder->possible_crtcs;
1773 enc_resp->possible_clones = encoder->possible_clones;
1774
1775out:
Daniel Vetter84849902012-12-02 00:28:11 +01001776 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001777 return ret;
1778}
1779
1780/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001781 * drm_mode_getplane_res - get plane info
1782 * @dev: DRM device
1783 * @data: ioctl data
1784 * @file_priv: DRM file info
1785 *
1786 * Return an plane count and set of IDs.
1787 */
1788int drm_mode_getplane_res(struct drm_device *dev, void *data,
1789 struct drm_file *file_priv)
1790{
1791 struct drm_mode_get_plane_res *plane_resp = data;
1792 struct drm_mode_config *config;
1793 struct drm_plane *plane;
1794 uint32_t __user *plane_ptr;
1795 int copied = 0, ret = 0;
1796
1797 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1798 return -EINVAL;
1799
Daniel Vetter84849902012-12-02 00:28:11 +01001800 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001801 config = &dev->mode_config;
1802
1803 /*
1804 * This ioctl is called twice, once to determine how much space is
1805 * needed, and the 2nd time to fill it.
1806 */
1807 if (config->num_plane &&
1808 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001809 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001810
1811 list_for_each_entry(plane, &config->plane_list, head) {
1812 if (put_user(plane->base.id, plane_ptr + copied)) {
1813 ret = -EFAULT;
1814 goto out;
1815 }
1816 copied++;
1817 }
1818 }
1819 plane_resp->count_planes = config->num_plane;
1820
1821out:
Daniel Vetter84849902012-12-02 00:28:11 +01001822 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001823 return ret;
1824}
1825
1826/**
1827 * drm_mode_getplane - get plane info
1828 * @dev: DRM device
1829 * @data: ioctl data
1830 * @file_priv: DRM file info
1831 *
1832 * Return plane info, including formats supported, gamma size, any
1833 * current fb, etc.
1834 */
1835int drm_mode_getplane(struct drm_device *dev, void *data,
1836 struct drm_file *file_priv)
1837{
1838 struct drm_mode_get_plane *plane_resp = data;
1839 struct drm_mode_object *obj;
1840 struct drm_plane *plane;
1841 uint32_t __user *format_ptr;
1842 int ret = 0;
1843
1844 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1845 return -EINVAL;
1846
Daniel Vetter84849902012-12-02 00:28:11 +01001847 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001848 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1849 DRM_MODE_OBJECT_PLANE);
1850 if (!obj) {
1851 ret = -ENOENT;
1852 goto out;
1853 }
1854 plane = obj_to_plane(obj);
1855
1856 if (plane->crtc)
1857 plane_resp->crtc_id = plane->crtc->base.id;
1858 else
1859 plane_resp->crtc_id = 0;
1860
1861 if (plane->fb)
1862 plane_resp->fb_id = plane->fb->base.id;
1863 else
1864 plane_resp->fb_id = 0;
1865
1866 plane_resp->plane_id = plane->base.id;
1867 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001868 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001869
1870 /*
1871 * This ioctl is called twice, once to determine how much space is
1872 * needed, and the 2nd time to fill it.
1873 */
1874 if (plane->format_count &&
1875 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001876 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001877 if (copy_to_user(format_ptr,
1878 plane->format_types,
1879 sizeof(uint32_t) * plane->format_count)) {
1880 ret = -EFAULT;
1881 goto out;
1882 }
1883 }
1884 plane_resp->count_format_types = plane->format_count;
1885
1886out:
Daniel Vetter84849902012-12-02 00:28:11 +01001887 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001888 return ret;
1889}
1890
1891/**
1892 * drm_mode_setplane - set up or tear down an plane
1893 * @dev: DRM device
1894 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001895 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001896 *
1897 * Set plane info, including placement, fb, scaling, and other factors.
1898 * Or pass a NULL fb to disable.
1899 */
1900int drm_mode_setplane(struct drm_device *dev, void *data,
1901 struct drm_file *file_priv)
1902{
1903 struct drm_mode_set_plane *plane_req = data;
1904 struct drm_mode_object *obj;
1905 struct drm_plane *plane;
1906 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001907 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001908 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001909 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001910 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001911
1912 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1913 return -EINVAL;
1914
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001915 /*
1916 * First, find the plane, crtc, and fb objects. If not available,
1917 * we don't bother to call the driver.
1918 */
1919 obj = drm_mode_object_find(dev, plane_req->plane_id,
1920 DRM_MODE_OBJECT_PLANE);
1921 if (!obj) {
1922 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1923 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001924 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001925 }
1926 plane = obj_to_plane(obj);
1927
1928 /* No fb means shut it down */
1929 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001930 drm_modeset_lock_all(dev);
1931 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001932 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001933 plane->crtc = NULL;
1934 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001935 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001936 goto out;
1937 }
1938
1939 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1940 DRM_MODE_OBJECT_CRTC);
1941 if (!obj) {
1942 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1943 plane_req->crtc_id);
1944 ret = -ENOENT;
1945 goto out;
1946 }
1947 crtc = obj_to_crtc(obj);
1948
Daniel Vetter786b99e2012-12-02 21:53:40 +01001949 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1950 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001951 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1952 plane_req->fb_id);
1953 ret = -ENOENT;
1954 goto out;
1955 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001956
Ville Syrjälä62443be2011-12-20 00:06:47 +02001957 /* Check whether this plane supports the fb pixel format. */
1958 for (i = 0; i < plane->format_count; i++)
1959 if (fb->pixel_format == plane->format_types[i])
1960 break;
1961 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03001962 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1963 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02001964 ret = -EINVAL;
1965 goto out;
1966 }
1967
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001968 fb_width = fb->width << 16;
1969 fb_height = fb->height << 16;
1970
1971 /* Make sure source coordinates are inside the fb. */
1972 if (plane_req->src_w > fb_width ||
1973 plane_req->src_x > fb_width - plane_req->src_w ||
1974 plane_req->src_h > fb_height ||
1975 plane_req->src_y > fb_height - plane_req->src_h) {
1976 DRM_DEBUG_KMS("Invalid source coordinates "
1977 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1978 plane_req->src_w >> 16,
1979 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1980 plane_req->src_h >> 16,
1981 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1982 plane_req->src_x >> 16,
1983 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1984 plane_req->src_y >> 16,
1985 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1986 ret = -ENOSPC;
1987 goto out;
1988 }
1989
Ville Syrjälä687a0402011-12-20 00:06:45 +02001990 /* Give drivers some help against integer overflows */
1991 if (plane_req->crtc_w > INT_MAX ||
1992 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1993 plane_req->crtc_h > INT_MAX ||
1994 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1995 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1996 plane_req->crtc_w, plane_req->crtc_h,
1997 plane_req->crtc_x, plane_req->crtc_y);
1998 ret = -ERANGE;
1999 goto out;
2000 }
2001
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002002 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002003 ret = plane->funcs->update_plane(plane, crtc, fb,
2004 plane_req->crtc_x, plane_req->crtc_y,
2005 plane_req->crtc_w, plane_req->crtc_h,
2006 plane_req->src_x, plane_req->src_y,
2007 plane_req->src_w, plane_req->src_h);
2008 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002009 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002010 plane->crtc = crtc;
2011 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002012 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002013 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002014 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002015
2016out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002017 if (fb)
2018 drm_framebuffer_unreference(fb);
2019 if (old_fb)
2020 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002021
2022 return ret;
2023}
2024
2025/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002026 * drm_mode_set_config_internal - helper to call ->set_config
2027 * @set: modeset config to set
2028 *
2029 * This is a little helper to wrap internal calls to the ->set_config driver
2030 * interface. The only thing it adds is correct refcounting dance.
2031 */
2032int drm_mode_set_config_internal(struct drm_mode_set *set)
2033{
2034 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002035 struct drm_framebuffer *fb;
2036 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002037 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002038
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002039 /*
2040 * NOTE: ->set_config can also disable other crtcs (if we steal all
2041 * connectors from it), hence we need to refcount the fbs across all
2042 * crtcs. Atomic modeset will have saner semantics ...
2043 */
2044 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2045 tmp->old_fb = tmp->fb;
2046
Daniel Vetterb0d12322012-12-11 01:07:12 +01002047 fb = set->fb;
2048
2049 ret = crtc->funcs->set_config(set);
2050 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02002051 /* crtc->fb must be updated by ->set_config, enforces this. */
2052 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002053 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002054
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002055 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2056 if (tmp->fb)
2057 drm_framebuffer_reference(tmp->fb);
2058 if (tmp->old_fb)
2059 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002060 }
2061
2062 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002063}
2064EXPORT_SYMBOL(drm_mode_set_config_internal);
2065
Damien Lespiauc11e9282013-09-25 16:45:30 +01002066/*
2067 * Checks that the framebuffer is big enough for the CRTC viewport
2068 * (x, y, hdisplay, vdisplay)
2069 */
2070static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2071 int x, int y,
2072 const struct drm_display_mode *mode,
2073 const struct drm_framebuffer *fb)
2074
2075{
2076 int hdisplay, vdisplay;
2077
2078 hdisplay = mode->hdisplay;
2079 vdisplay = mode->vdisplay;
2080
2081 if (crtc->invert_dimensions)
2082 swap(hdisplay, vdisplay);
2083
2084 if (hdisplay > fb->width ||
2085 vdisplay > fb->height ||
2086 x > fb->width - hdisplay ||
2087 y > fb->height - vdisplay) {
2088 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2089 fb->width, fb->height, hdisplay, vdisplay, x, y,
2090 crtc->invert_dimensions ? " (inverted)" : "");
2091 return -ENOSPC;
2092 }
2093
2094 return 0;
2095}
2096
Daniel Vetter2d13b672012-12-11 13:47:23 +01002097/**
Dave Airlief453ba02008-11-07 14:05:41 -08002098 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002099 * @dev: drm device for the ioctl
2100 * @data: data pointer for the ioctl
2101 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002102 *
Dave Airlief453ba02008-11-07 14:05:41 -08002103 * Build a new CRTC configuration based on user request.
2104 *
2105 * Called by the user via ioctl.
2106 *
2107 * RETURNS:
2108 * Zero on success, errno on failure.
2109 */
2110int drm_mode_setcrtc(struct drm_device *dev, void *data,
2111 struct drm_file *file_priv)
2112{
2113 struct drm_mode_config *config = &dev->mode_config;
2114 struct drm_mode_crtc *crtc_req = data;
2115 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002116 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002117 struct drm_connector **connector_set = NULL, *connector;
2118 struct drm_framebuffer *fb = NULL;
2119 struct drm_display_mode *mode = NULL;
2120 struct drm_mode_set set;
2121 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002122 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002123 int i;
2124
Dave Airliefb3b06c2011-02-08 13:55:21 +10002125 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2126 return -EINVAL;
2127
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002128 /* For some reason crtc x/y offsets are signed internally. */
2129 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2130 return -ERANGE;
2131
Daniel Vetter84849902012-12-02 00:28:11 +01002132 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002133 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2134 DRM_MODE_OBJECT_CRTC);
2135 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002136 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002137 ret = -EINVAL;
2138 goto out;
2139 }
2140 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002141 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002142
2143 if (crtc_req->mode_valid) {
2144 /* If we have a mode we need a framebuffer. */
2145 /* If we pass -1, set the mode with the currently bound fb */
2146 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002147 if (!crtc->fb) {
2148 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2149 ret = -EINVAL;
2150 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002151 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002152 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002153 /* Make refcounting symmetric with the lookup path. */
2154 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002155 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002156 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2157 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002158 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2159 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002160 ret = -EINVAL;
2161 goto out;
2162 }
Dave Airlief453ba02008-11-07 14:05:41 -08002163 }
2164
2165 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002166 if (!mode) {
2167 ret = -ENOMEM;
2168 goto out;
2169 }
2170
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002171 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2172 if (ret) {
2173 DRM_DEBUG_KMS("Invalid mode\n");
2174 goto out;
2175 }
2176
Dave Airlief453ba02008-11-07 14:05:41 -08002177 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002178
Damien Lespiauc11e9282013-09-25 16:45:30 +01002179 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2180 mode, fb);
2181 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002182 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002183
Dave Airlief453ba02008-11-07 14:05:41 -08002184 }
2185
2186 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002187 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002188 ret = -EINVAL;
2189 goto out;
2190 }
2191
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002192 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002193 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002194 crtc_req->count_connectors);
2195 ret = -EINVAL;
2196 goto out;
2197 }
2198
2199 if (crtc_req->count_connectors > 0) {
2200 u32 out_id;
2201
2202 /* Avoid unbounded kernel memory allocation */
2203 if (crtc_req->count_connectors > config->num_connector) {
2204 ret = -EINVAL;
2205 goto out;
2206 }
2207
2208 connector_set = kmalloc(crtc_req->count_connectors *
2209 sizeof(struct drm_connector *),
2210 GFP_KERNEL);
2211 if (!connector_set) {
2212 ret = -ENOMEM;
2213 goto out;
2214 }
2215
2216 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002217 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002218 if (get_user(out_id, &set_connectors_ptr[i])) {
2219 ret = -EFAULT;
2220 goto out;
2221 }
2222
2223 obj = drm_mode_object_find(dev, out_id,
2224 DRM_MODE_OBJECT_CONNECTOR);
2225 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002226 DRM_DEBUG_KMS("Connector id %d unknown\n",
2227 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002228 ret = -EINVAL;
2229 goto out;
2230 }
2231 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002232 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2233 connector->base.id,
2234 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002235
2236 connector_set[i] = connector;
2237 }
2238 }
2239
2240 set.crtc = crtc;
2241 set.x = crtc_req->x;
2242 set.y = crtc_req->y;
2243 set.mode = mode;
2244 set.connectors = connector_set;
2245 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002246 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002247 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002248
2249out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002250 if (fb)
2251 drm_framebuffer_unreference(fb);
2252
Dave Airlief453ba02008-11-07 14:05:41 -08002253 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002254 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002255 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002256 return ret;
2257}
2258
Dave Airlie4c813d42013-06-20 11:48:52 +10002259static int drm_mode_cursor_common(struct drm_device *dev,
2260 struct drm_mode_cursor2 *req,
2261 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002262{
Dave Airlief453ba02008-11-07 14:05:41 -08002263 struct drm_mode_object *obj;
2264 struct drm_crtc *crtc;
2265 int ret = 0;
2266
Dave Airliefb3b06c2011-02-08 13:55:21 +10002267 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2268 return -EINVAL;
2269
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002270 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002271 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002272
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002273 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002274 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002275 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002276 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002277 }
2278 crtc = obj_to_crtc(obj);
2279
Daniel Vetterdac35662012-12-02 15:24:10 +01002280 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002281 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002282 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002283 ret = -ENXIO;
2284 goto out;
2285 }
2286 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002287 if (crtc->funcs->cursor_set2)
2288 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2289 req->width, req->height, req->hot_x, req->hot_y);
2290 else
2291 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2292 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002293 }
2294
2295 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2296 if (crtc->funcs->cursor_move) {
2297 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2298 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002299 ret = -EFAULT;
2300 goto out;
2301 }
2302 }
2303out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002304 mutex_unlock(&crtc->mutex);
2305
Dave Airlief453ba02008-11-07 14:05:41 -08002306 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002307
2308}
2309int drm_mode_cursor_ioctl(struct drm_device *dev,
2310 void *data, struct drm_file *file_priv)
2311{
2312 struct drm_mode_cursor *req = data;
2313 struct drm_mode_cursor2 new_req;
2314
2315 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2316 new_req.hot_x = new_req.hot_y = 0;
2317
2318 return drm_mode_cursor_common(dev, &new_req, file_priv);
2319}
2320
2321int drm_mode_cursor2_ioctl(struct drm_device *dev,
2322 void *data, struct drm_file *file_priv)
2323{
2324 struct drm_mode_cursor2 *req = data;
2325 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002326}
2327
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002328/* Original addfb only supported RGB formats, so figure out which one */
2329uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2330{
2331 uint32_t fmt;
2332
2333 switch (bpp) {
2334 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002335 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002336 break;
2337 case 16:
2338 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002339 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002340 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002341 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002342 break;
2343 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002344 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002345 break;
2346 case 32:
2347 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002348 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002349 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002350 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002351 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002352 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002353 break;
2354 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002355 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2356 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002357 break;
2358 }
2359
2360 return fmt;
2361}
2362EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2363
Dave Airlief453ba02008-11-07 14:05:41 -08002364/**
2365 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002366 * @dev: drm device for the ioctl
2367 * @data: data pointer for the ioctl
2368 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002369 *
Dave Airlief453ba02008-11-07 14:05:41 -08002370 * Add a new FB to the specified CRTC, given a user request.
2371 *
2372 * Called by the user via ioctl.
2373 *
2374 * RETURNS:
2375 * Zero on success, errno on failure.
2376 */
2377int drm_mode_addfb(struct drm_device *dev,
2378 void *data, struct drm_file *file_priv)
2379{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002380 struct drm_mode_fb_cmd *or = data;
2381 struct drm_mode_fb_cmd2 r = {};
2382 struct drm_mode_config *config = &dev->mode_config;
2383 struct drm_framebuffer *fb;
2384 int ret = 0;
2385
2386 /* Use new struct with format internally */
2387 r.fb_id = or->fb_id;
2388 r.width = or->width;
2389 r.height = or->height;
2390 r.pitches[0] = or->pitch;
2391 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2392 r.handles[0] = or->handle;
2393
2394 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2395 return -EINVAL;
2396
Jesse Barnesacb4b992011-11-07 12:03:23 -08002397 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002398 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002399
2400 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002401 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002402
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002403 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2404 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002405 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002406 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002407 }
2408
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002409 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002410 or->fb_id = fb->base.id;
2411 list_add(&fb->filp_head, &file_priv->fbs);
2412 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002413 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002414
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002415 return ret;
2416}
2417
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002418static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002419{
2420 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2421
2422 switch (format) {
2423 case DRM_FORMAT_C8:
2424 case DRM_FORMAT_RGB332:
2425 case DRM_FORMAT_BGR233:
2426 case DRM_FORMAT_XRGB4444:
2427 case DRM_FORMAT_XBGR4444:
2428 case DRM_FORMAT_RGBX4444:
2429 case DRM_FORMAT_BGRX4444:
2430 case DRM_FORMAT_ARGB4444:
2431 case DRM_FORMAT_ABGR4444:
2432 case DRM_FORMAT_RGBA4444:
2433 case DRM_FORMAT_BGRA4444:
2434 case DRM_FORMAT_XRGB1555:
2435 case DRM_FORMAT_XBGR1555:
2436 case DRM_FORMAT_RGBX5551:
2437 case DRM_FORMAT_BGRX5551:
2438 case DRM_FORMAT_ARGB1555:
2439 case DRM_FORMAT_ABGR1555:
2440 case DRM_FORMAT_RGBA5551:
2441 case DRM_FORMAT_BGRA5551:
2442 case DRM_FORMAT_RGB565:
2443 case DRM_FORMAT_BGR565:
2444 case DRM_FORMAT_RGB888:
2445 case DRM_FORMAT_BGR888:
2446 case DRM_FORMAT_XRGB8888:
2447 case DRM_FORMAT_XBGR8888:
2448 case DRM_FORMAT_RGBX8888:
2449 case DRM_FORMAT_BGRX8888:
2450 case DRM_FORMAT_ARGB8888:
2451 case DRM_FORMAT_ABGR8888:
2452 case DRM_FORMAT_RGBA8888:
2453 case DRM_FORMAT_BGRA8888:
2454 case DRM_FORMAT_XRGB2101010:
2455 case DRM_FORMAT_XBGR2101010:
2456 case DRM_FORMAT_RGBX1010102:
2457 case DRM_FORMAT_BGRX1010102:
2458 case DRM_FORMAT_ARGB2101010:
2459 case DRM_FORMAT_ABGR2101010:
2460 case DRM_FORMAT_RGBA1010102:
2461 case DRM_FORMAT_BGRA1010102:
2462 case DRM_FORMAT_YUYV:
2463 case DRM_FORMAT_YVYU:
2464 case DRM_FORMAT_UYVY:
2465 case DRM_FORMAT_VYUY:
2466 case DRM_FORMAT_AYUV:
2467 case DRM_FORMAT_NV12:
2468 case DRM_FORMAT_NV21:
2469 case DRM_FORMAT_NV16:
2470 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002471 case DRM_FORMAT_NV24:
2472 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002473 case DRM_FORMAT_YUV410:
2474 case DRM_FORMAT_YVU410:
2475 case DRM_FORMAT_YUV411:
2476 case DRM_FORMAT_YVU411:
2477 case DRM_FORMAT_YUV420:
2478 case DRM_FORMAT_YVU420:
2479 case DRM_FORMAT_YUV422:
2480 case DRM_FORMAT_YVU422:
2481 case DRM_FORMAT_YUV444:
2482 case DRM_FORMAT_YVU444:
2483 return 0;
2484 default:
2485 return -EINVAL;
2486 }
2487}
2488
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002489static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002490{
2491 int ret, hsub, vsub, num_planes, i;
2492
2493 ret = format_check(r);
2494 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002495 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2496 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002497 return ret;
2498 }
2499
2500 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2501 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2502 num_planes = drm_format_num_planes(r->pixel_format);
2503
2504 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002505 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002506 return -EINVAL;
2507 }
2508
2509 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002510 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002511 return -EINVAL;
2512 }
2513
2514 for (i = 0; i < num_planes; i++) {
2515 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002516 unsigned int height = r->height / (i != 0 ? vsub : 1);
2517 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002518
2519 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002520 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002521 return -EINVAL;
2522 }
2523
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002524 if ((uint64_t) width * cpp > UINT_MAX)
2525 return -ERANGE;
2526
2527 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2528 return -ERANGE;
2529
2530 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002531 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002532 return -EINVAL;
2533 }
2534 }
2535
2536 return 0;
2537}
2538
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002539/**
2540 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002541 * @dev: drm device for the ioctl
2542 * @data: data pointer for the ioctl
2543 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002544 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002545 * Add a new FB to the specified CRTC, given a user request with format.
2546 *
2547 * Called by the user via ioctl.
2548 *
2549 * RETURNS:
2550 * Zero on success, errno on failure.
2551 */
2552int drm_mode_addfb2(struct drm_device *dev,
2553 void *data, struct drm_file *file_priv)
2554{
2555 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002556 struct drm_mode_config *config = &dev->mode_config;
2557 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002558 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002559
Dave Airliefb3b06c2011-02-08 13:55:21 +10002560 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2561 return -EINVAL;
2562
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002563 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2564 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2565 return -EINVAL;
2566 }
2567
Dave Airlief453ba02008-11-07 14:05:41 -08002568 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002569 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002570 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002571 return -EINVAL;
2572 }
2573 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002574 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002575 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002576 return -EINVAL;
2577 }
2578
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002579 ret = framebuffer_check(r);
2580 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002581 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002582
Dave Airlief453ba02008-11-07 14:05:41 -08002583 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002584 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002585 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002586 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002587 }
2588
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002589 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002590 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002591 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002592 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002593 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002594
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002595
Dave Airlief453ba02008-11-07 14:05:41 -08002596 return ret;
2597}
2598
2599/**
2600 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002601 * @dev: drm device for the ioctl
2602 * @data: data pointer for the ioctl
2603 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002604 *
Dave Airlief453ba02008-11-07 14:05:41 -08002605 * Remove the FB specified by the user.
2606 *
2607 * Called by the user via ioctl.
2608 *
2609 * RETURNS:
2610 * Zero on success, errno on failure.
2611 */
2612int drm_mode_rmfb(struct drm_device *dev,
2613 void *data, struct drm_file *file_priv)
2614{
Dave Airlief453ba02008-11-07 14:05:41 -08002615 struct drm_framebuffer *fb = NULL;
2616 struct drm_framebuffer *fbl = NULL;
2617 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002618 int found = 0;
2619
Dave Airliefb3b06c2011-02-08 13:55:21 +10002620 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2621 return -EINVAL;
2622
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002623 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002624 mutex_lock(&dev->mode_config.fb_lock);
2625 fb = __drm_framebuffer_lookup(dev, *id);
2626 if (!fb)
2627 goto fail_lookup;
2628
Dave Airlief453ba02008-11-07 14:05:41 -08002629 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2630 if (fb == fbl)
2631 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002632 if (!found)
2633 goto fail_lookup;
2634
2635 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2636 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002637
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002638 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002639 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002640 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002641
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002642 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002643
Daniel Vetter2b677e82012-12-10 21:16:05 +01002644 return 0;
2645
2646fail_lookup:
2647 mutex_unlock(&dev->mode_config.fb_lock);
2648 mutex_unlock(&file_priv->fbs_lock);
2649
2650 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002651}
2652
2653/**
2654 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002655 * @dev: drm device for the ioctl
2656 * @data: data pointer for the ioctl
2657 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002658 *
Dave Airlief453ba02008-11-07 14:05:41 -08002659 * Lookup the FB given its ID and return info about it.
2660 *
2661 * Called by the user via ioctl.
2662 *
2663 * RETURNS:
2664 * Zero on success, errno on failure.
2665 */
2666int drm_mode_getfb(struct drm_device *dev,
2667 void *data, struct drm_file *file_priv)
2668{
2669 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002670 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002671 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002672
Dave Airliefb3b06c2011-02-08 13:55:21 +10002673 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2674 return -EINVAL;
2675
Daniel Vetter786b99e2012-12-02 21:53:40 +01002676 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002677 if (!fb)
2678 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002679
2680 r->height = fb->height;
2681 r->width = fb->width;
2682 r->depth = fb->depth;
2683 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002684 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002685 if (fb->funcs->create_handle) {
2686 if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
2687 ret = fb->funcs->create_handle(fb, file_priv,
2688 &r->handle);
2689 } else {
2690 /* GET_FB() is an unprivileged ioctl so we must not
2691 * return a buffer-handle to non-master processes! For
2692 * backwards-compatibility reasons, we cannot make
2693 * GET_FB() privileged, so just return an invalid handle
2694 * for non-masters. */
2695 r->handle = 0;
2696 ret = 0;
2697 }
2698 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002699 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02002700 }
Dave Airlief453ba02008-11-07 14:05:41 -08002701
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002702 drm_framebuffer_unreference(fb);
2703
Dave Airlief453ba02008-11-07 14:05:41 -08002704 return ret;
2705}
2706
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002707int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2708 void *data, struct drm_file *file_priv)
2709{
2710 struct drm_clip_rect __user *clips_ptr;
2711 struct drm_clip_rect *clips = NULL;
2712 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002713 struct drm_framebuffer *fb;
2714 unsigned flags;
2715 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002716 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002717
Dave Airliefb3b06c2011-02-08 13:55:21 +10002718 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2719 return -EINVAL;
2720
Daniel Vetter786b99e2012-12-02 21:53:40 +01002721 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002722 if (!fb)
2723 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002724
2725 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002726 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002727
2728 if (!num_clips != !clips_ptr) {
2729 ret = -EINVAL;
2730 goto out_err1;
2731 }
2732
2733 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2734
2735 /* If userspace annotates copy, clips must come in pairs */
2736 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2737 ret = -EINVAL;
2738 goto out_err1;
2739 }
2740
2741 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002742 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2743 ret = -EINVAL;
2744 goto out_err1;
2745 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002746 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2747 if (!clips) {
2748 ret = -ENOMEM;
2749 goto out_err1;
2750 }
2751
2752 ret = copy_from_user(clips, clips_ptr,
2753 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002754 if (ret) {
2755 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002756 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002757 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002758 }
2759
2760 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002761 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002762 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2763 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002764 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002765 } else {
2766 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002767 }
2768
2769out_err2:
2770 kfree(clips);
2771out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002772 drm_framebuffer_unreference(fb);
2773
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002774 return ret;
2775}
2776
2777
Dave Airlief453ba02008-11-07 14:05:41 -08002778/**
2779 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002780 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002781 *
Dave Airlief453ba02008-11-07 14:05:41 -08002782 * Destroy all the FBs associated with @filp.
2783 *
2784 * Called by the user via ioctl.
2785 *
2786 * RETURNS:
2787 * Zero on success, errno on failure.
2788 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002789void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002790{
Dave Airlief453ba02008-11-07 14:05:41 -08002791 struct drm_device *dev = priv->minor->dev;
2792 struct drm_framebuffer *fb, *tfb;
2793
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002794 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002795 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002796
2797 mutex_lock(&dev->mode_config.fb_lock);
2798 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2799 __drm_framebuffer_unregister(dev, fb);
2800 mutex_unlock(&dev->mode_config.fb_lock);
2801
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002802 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002803
2804 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002805 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002806 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002807 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002808}
2809
Dave Airlief453ba02008-11-07 14:05:41 -08002810struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2811 const char *name, int num_values)
2812{
2813 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002814 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002815
2816 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2817 if (!property)
2818 return NULL;
2819
2820 if (num_values) {
2821 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2822 if (!property->values)
2823 goto fail;
2824 }
2825
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002826 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2827 if (ret)
2828 goto fail;
2829
Dave Airlief453ba02008-11-07 14:05:41 -08002830 property->flags = flags;
2831 property->num_values = num_values;
2832 INIT_LIST_HEAD(&property->enum_blob_list);
2833
Vinson Lee471dd2e2011-11-10 11:55:40 -08002834 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002835 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002836 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2837 }
Dave Airlief453ba02008-11-07 14:05:41 -08002838
2839 list_add_tail(&property->head, &dev->mode_config.property_list);
2840 return property;
2841fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002842 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002843 kfree(property);
2844 return NULL;
2845}
2846EXPORT_SYMBOL(drm_property_create);
2847
Sascha Hauer4a67d392012-02-06 10:58:17 +01002848struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2849 const char *name,
2850 const struct drm_prop_enum_list *props,
2851 int num_values)
2852{
2853 struct drm_property *property;
2854 int i, ret;
2855
2856 flags |= DRM_MODE_PROP_ENUM;
2857
2858 property = drm_property_create(dev, flags, name, num_values);
2859 if (!property)
2860 return NULL;
2861
2862 for (i = 0; i < num_values; i++) {
2863 ret = drm_property_add_enum(property, i,
2864 props[i].type,
2865 props[i].name);
2866 if (ret) {
2867 drm_property_destroy(dev, property);
2868 return NULL;
2869 }
2870 }
2871
2872 return property;
2873}
2874EXPORT_SYMBOL(drm_property_create_enum);
2875
Rob Clark49e27542012-05-17 02:23:26 -06002876struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2877 int flags, const char *name,
2878 const struct drm_prop_enum_list *props,
2879 int num_values)
2880{
2881 struct drm_property *property;
2882 int i, ret;
2883
2884 flags |= DRM_MODE_PROP_BITMASK;
2885
2886 property = drm_property_create(dev, flags, name, num_values);
2887 if (!property)
2888 return NULL;
2889
2890 for (i = 0; i < num_values; i++) {
2891 ret = drm_property_add_enum(property, i,
2892 props[i].type,
2893 props[i].name);
2894 if (ret) {
2895 drm_property_destroy(dev, property);
2896 return NULL;
2897 }
2898 }
2899
2900 return property;
2901}
2902EXPORT_SYMBOL(drm_property_create_bitmask);
2903
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002904struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2905 const char *name,
2906 uint64_t min, uint64_t max)
2907{
2908 struct drm_property *property;
2909
2910 flags |= DRM_MODE_PROP_RANGE;
2911
2912 property = drm_property_create(dev, flags, name, 2);
2913 if (!property)
2914 return NULL;
2915
2916 property->values[0] = min;
2917 property->values[1] = max;
2918
2919 return property;
2920}
2921EXPORT_SYMBOL(drm_property_create_range);
2922
Dave Airlief453ba02008-11-07 14:05:41 -08002923int drm_property_add_enum(struct drm_property *property, int index,
2924 uint64_t value, const char *name)
2925{
2926 struct drm_property_enum *prop_enum;
2927
Rob Clark49e27542012-05-17 02:23:26 -06002928 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2929 return -EINVAL;
2930
2931 /*
2932 * Bitmask enum properties have the additional constraint of values
2933 * from 0 to 63
2934 */
2935 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002936 return -EINVAL;
2937
2938 if (!list_empty(&property->enum_blob_list)) {
2939 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2940 if (prop_enum->value == value) {
2941 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2942 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2943 return 0;
2944 }
2945 }
2946 }
2947
2948 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2949 if (!prop_enum)
2950 return -ENOMEM;
2951
2952 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2953 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2954 prop_enum->value = value;
2955
2956 property->values[index] = value;
2957 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2958 return 0;
2959}
2960EXPORT_SYMBOL(drm_property_add_enum);
2961
2962void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2963{
2964 struct drm_property_enum *prop_enum, *pt;
2965
2966 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2967 list_del(&prop_enum->head);
2968 kfree(prop_enum);
2969 }
2970
2971 if (property->num_values)
2972 kfree(property->values);
2973 drm_mode_object_put(dev, &property->base);
2974 list_del(&property->head);
2975 kfree(property);
2976}
2977EXPORT_SYMBOL(drm_property_destroy);
2978
Paulo Zanonic5431882012-05-15 18:09:02 -03002979void drm_object_attach_property(struct drm_mode_object *obj,
2980 struct drm_property *property,
2981 uint64_t init_val)
2982{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002983 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002984
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002985 if (count == DRM_OBJECT_MAX_PROPERTY) {
2986 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2987 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2988 "you see this message on the same object type.\n",
2989 obj->type);
2990 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002991 }
2992
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002993 obj->properties->ids[count] = property->base.id;
2994 obj->properties->values[count] = init_val;
2995 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002996}
2997EXPORT_SYMBOL(drm_object_attach_property);
2998
2999int drm_object_property_set_value(struct drm_mode_object *obj,
3000 struct drm_property *property, uint64_t val)
3001{
3002 int i;
3003
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003004 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003005 if (obj->properties->ids[i] == property->base.id) {
3006 obj->properties->values[i] = val;
3007 return 0;
3008 }
3009 }
3010
3011 return -EINVAL;
3012}
3013EXPORT_SYMBOL(drm_object_property_set_value);
3014
3015int drm_object_property_get_value(struct drm_mode_object *obj,
3016 struct drm_property *property, uint64_t *val)
3017{
3018 int i;
3019
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003020 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003021 if (obj->properties->ids[i] == property->base.id) {
3022 *val = obj->properties->values[i];
3023 return 0;
3024 }
3025 }
3026
3027 return -EINVAL;
3028}
3029EXPORT_SYMBOL(drm_object_property_get_value);
3030
Dave Airlief453ba02008-11-07 14:05:41 -08003031int drm_mode_getproperty_ioctl(struct drm_device *dev,
3032 void *data, struct drm_file *file_priv)
3033{
3034 struct drm_mode_object *obj;
3035 struct drm_mode_get_property *out_resp = data;
3036 struct drm_property *property;
3037 int enum_count = 0;
3038 int blob_count = 0;
3039 int value_count = 0;
3040 int ret = 0, i;
3041 int copied;
3042 struct drm_property_enum *prop_enum;
3043 struct drm_mode_property_enum __user *enum_ptr;
3044 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003045 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003046 uint64_t __user *values_ptr;
3047 uint32_t __user *blob_length_ptr;
3048
Dave Airliefb3b06c2011-02-08 13:55:21 +10003049 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3050 return -EINVAL;
3051
Daniel Vetter84849902012-12-02 00:28:11 +01003052 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003053 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3054 if (!obj) {
3055 ret = -EINVAL;
3056 goto done;
3057 }
3058 property = obj_to_property(obj);
3059
Rob Clark49e27542012-05-17 02:23:26 -06003060 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003061 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3062 enum_count++;
3063 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3064 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3065 blob_count++;
3066 }
3067
3068 value_count = property->num_values;
3069
3070 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3071 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3072 out_resp->flags = property->flags;
3073
3074 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003075 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003076 for (i = 0; i < value_count; i++) {
3077 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3078 ret = -EFAULT;
3079 goto done;
3080 }
3081 }
3082 }
3083 out_resp->count_values = value_count;
3084
Rob Clark49e27542012-05-17 02:23:26 -06003085 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003086 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3087 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003088 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003089 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3090
3091 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3092 ret = -EFAULT;
3093 goto done;
3094 }
3095
3096 if (copy_to_user(&enum_ptr[copied].name,
3097 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3098 ret = -EFAULT;
3099 goto done;
3100 }
3101 copied++;
3102 }
3103 }
3104 out_resp->count_enum_blobs = enum_count;
3105 }
3106
3107 if (property->flags & DRM_MODE_PROP_BLOB) {
3108 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3109 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003110 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3111 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003112
3113 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3114 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3115 ret = -EFAULT;
3116 goto done;
3117 }
3118
3119 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3120 ret = -EFAULT;
3121 goto done;
3122 }
3123
3124 copied++;
3125 }
3126 }
3127 out_resp->count_enum_blobs = blob_count;
3128 }
3129done:
Daniel Vetter84849902012-12-02 00:28:11 +01003130 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003131 return ret;
3132}
3133
3134static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3135 void *data)
3136{
3137 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003138 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003139
3140 if (!length || !data)
3141 return NULL;
3142
3143 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3144 if (!blob)
3145 return NULL;
3146
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003147 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3148 if (ret) {
3149 kfree(blob);
3150 return NULL;
3151 }
3152
Dave Airlief453ba02008-11-07 14:05:41 -08003153 blob->length = length;
3154
3155 memcpy(blob->data, data, length);
3156
Dave Airlief453ba02008-11-07 14:05:41 -08003157 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3158 return blob;
3159}
3160
3161static void drm_property_destroy_blob(struct drm_device *dev,
3162 struct drm_property_blob *blob)
3163{
3164 drm_mode_object_put(dev, &blob->base);
3165 list_del(&blob->head);
3166 kfree(blob);
3167}
3168
3169int drm_mode_getblob_ioctl(struct drm_device *dev,
3170 void *data, struct drm_file *file_priv)
3171{
3172 struct drm_mode_object *obj;
3173 struct drm_mode_get_blob *out_resp = data;
3174 struct drm_property_blob *blob;
3175 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003176 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003177
Dave Airliefb3b06c2011-02-08 13:55:21 +10003178 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3179 return -EINVAL;
3180
Daniel Vetter84849902012-12-02 00:28:11 +01003181 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003182 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3183 if (!obj) {
3184 ret = -EINVAL;
3185 goto done;
3186 }
3187 blob = obj_to_blob(obj);
3188
3189 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003190 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003191 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3192 ret = -EFAULT;
3193 goto done;
3194 }
3195 }
3196 out_resp->length = blob->length;
3197
3198done:
Daniel Vetter84849902012-12-02 00:28:11 +01003199 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003200 return ret;
3201}
3202
3203int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3204 struct edid *edid)
3205{
3206 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003207 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003208
3209 if (connector->edid_blob_ptr)
3210 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3211
3212 /* Delete edid, when there is none. */
3213 if (!edid) {
3214 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003215 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003216 return ret;
3217 }
3218
Adam Jackson7466f4c2010-03-29 21:43:23 +00003219 size = EDID_LENGTH * (1 + edid->extensions);
3220 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3221 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003222 if (!connector->edid_blob_ptr)
3223 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003224
Rob Clark58495562012-10-11 20:50:56 -05003225 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003226 dev->mode_config.edid_property,
3227 connector->edid_blob_ptr->base.id);
3228
3229 return ret;
3230}
3231EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3232
Paulo Zanoni26a34812012-05-15 18:08:59 -03003233static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003234 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003235{
3236 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3237 return false;
3238 if (property->flags & DRM_MODE_PROP_RANGE) {
3239 if (value < property->values[0] || value > property->values[1])
3240 return false;
3241 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003242 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3243 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003244 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003245 for (i = 0; i < property->num_values; i++)
3246 valid_mask |= (1ULL << property->values[i]);
3247 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003248 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3249 /* Only the driver knows */
3250 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003251 } else {
3252 int i;
3253 for (i = 0; i < property->num_values; i++)
3254 if (property->values[i] == value)
3255 return true;
3256 return false;
3257 }
3258}
3259
Dave Airlief453ba02008-11-07 14:05:41 -08003260int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3261 void *data, struct drm_file *file_priv)
3262{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003263 struct drm_mode_connector_set_property *conn_set_prop = data;
3264 struct drm_mode_obj_set_property obj_set_prop = {
3265 .value = conn_set_prop->value,
3266 .prop_id = conn_set_prop->prop_id,
3267 .obj_id = conn_set_prop->connector_id,
3268 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3269 };
Dave Airlief453ba02008-11-07 14:05:41 -08003270
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003271 /* It does all the locking and checking we need */
3272 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003273}
3274
Paulo Zanonic5431882012-05-15 18:09:02 -03003275static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3276 struct drm_property *property,
3277 uint64_t value)
3278{
3279 int ret = -EINVAL;
3280 struct drm_connector *connector = obj_to_connector(obj);
3281
3282 /* Do DPMS ourselves */
3283 if (property == connector->dev->mode_config.dpms_property) {
3284 if (connector->funcs->dpms)
3285 (*connector->funcs->dpms)(connector, (int)value);
3286 ret = 0;
3287 } else if (connector->funcs->set_property)
3288 ret = connector->funcs->set_property(connector, property, value);
3289
3290 /* store the property value if successful */
3291 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003292 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003293 return ret;
3294}
3295
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003296static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3297 struct drm_property *property,
3298 uint64_t value)
3299{
3300 int ret = -EINVAL;
3301 struct drm_crtc *crtc = obj_to_crtc(obj);
3302
3303 if (crtc->funcs->set_property)
3304 ret = crtc->funcs->set_property(crtc, property, value);
3305 if (!ret)
3306 drm_object_property_set_value(obj, property, value);
3307
3308 return ret;
3309}
3310
Rob Clark4d939142012-05-17 02:23:27 -06003311static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3312 struct drm_property *property,
3313 uint64_t value)
3314{
3315 int ret = -EINVAL;
3316 struct drm_plane *plane = obj_to_plane(obj);
3317
3318 if (plane->funcs->set_property)
3319 ret = plane->funcs->set_property(plane, property, value);
3320 if (!ret)
3321 drm_object_property_set_value(obj, property, value);
3322
3323 return ret;
3324}
3325
Paulo Zanonic5431882012-05-15 18:09:02 -03003326int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3327 struct drm_file *file_priv)
3328{
3329 struct drm_mode_obj_get_properties *arg = data;
3330 struct drm_mode_object *obj;
3331 int ret = 0;
3332 int i;
3333 int copied = 0;
3334 int props_count = 0;
3335 uint32_t __user *props_ptr;
3336 uint64_t __user *prop_values_ptr;
3337
3338 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3339 return -EINVAL;
3340
Daniel Vetter84849902012-12-02 00:28:11 +01003341 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003342
3343 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3344 if (!obj) {
3345 ret = -EINVAL;
3346 goto out;
3347 }
3348 if (!obj->properties) {
3349 ret = -EINVAL;
3350 goto out;
3351 }
3352
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003353 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003354
3355 /* This ioctl is called twice, once to determine how much space is
3356 * needed, and the 2nd time to fill it. */
3357 if ((arg->count_props >= props_count) && props_count) {
3358 copied = 0;
3359 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3360 prop_values_ptr = (uint64_t __user *)(unsigned long)
3361 (arg->prop_values_ptr);
3362 for (i = 0; i < props_count; i++) {
3363 if (put_user(obj->properties->ids[i],
3364 props_ptr + copied)) {
3365 ret = -EFAULT;
3366 goto out;
3367 }
3368 if (put_user(obj->properties->values[i],
3369 prop_values_ptr + copied)) {
3370 ret = -EFAULT;
3371 goto out;
3372 }
3373 copied++;
3374 }
3375 }
3376 arg->count_props = props_count;
3377out:
Daniel Vetter84849902012-12-02 00:28:11 +01003378 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003379 return ret;
3380}
3381
3382int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3383 struct drm_file *file_priv)
3384{
3385 struct drm_mode_obj_set_property *arg = data;
3386 struct drm_mode_object *arg_obj;
3387 struct drm_mode_object *prop_obj;
3388 struct drm_property *property;
3389 int ret = -EINVAL;
3390 int i;
3391
3392 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3393 return -EINVAL;
3394
Daniel Vetter84849902012-12-02 00:28:11 +01003395 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003396
3397 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3398 if (!arg_obj)
3399 goto out;
3400 if (!arg_obj->properties)
3401 goto out;
3402
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003403 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003404 if (arg_obj->properties->ids[i] == arg->prop_id)
3405 break;
3406
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003407 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003408 goto out;
3409
3410 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3411 DRM_MODE_OBJECT_PROPERTY);
3412 if (!prop_obj)
3413 goto out;
3414 property = obj_to_property(prop_obj);
3415
3416 if (!drm_property_change_is_valid(property, arg->value))
3417 goto out;
3418
3419 switch (arg_obj->type) {
3420 case DRM_MODE_OBJECT_CONNECTOR:
3421 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3422 arg->value);
3423 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003424 case DRM_MODE_OBJECT_CRTC:
3425 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3426 break;
Rob Clark4d939142012-05-17 02:23:27 -06003427 case DRM_MODE_OBJECT_PLANE:
3428 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3429 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003430 }
3431
3432out:
Daniel Vetter84849902012-12-02 00:28:11 +01003433 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003434 return ret;
3435}
3436
Dave Airlief453ba02008-11-07 14:05:41 -08003437int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3438 struct drm_encoder *encoder)
3439{
3440 int i;
3441
3442 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3443 if (connector->encoder_ids[i] == 0) {
3444 connector->encoder_ids[i] = encoder->base.id;
3445 return 0;
3446 }
3447 }
3448 return -ENOMEM;
3449}
3450EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3451
3452void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3453 struct drm_encoder *encoder)
3454{
3455 int i;
3456 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3457 if (connector->encoder_ids[i] == encoder->base.id) {
3458 connector->encoder_ids[i] = 0;
3459 if (connector->encoder == encoder)
3460 connector->encoder = NULL;
3461 break;
3462 }
3463 }
3464}
3465EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3466
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003467int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003468 int gamma_size)
3469{
3470 crtc->gamma_size = gamma_size;
3471
3472 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3473 if (!crtc->gamma_store) {
3474 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003475 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003476 }
3477
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003478 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003479}
3480EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3481
3482int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3483 void *data, struct drm_file *file_priv)
3484{
3485 struct drm_mode_crtc_lut *crtc_lut = data;
3486 struct drm_mode_object *obj;
3487 struct drm_crtc *crtc;
3488 void *r_base, *g_base, *b_base;
3489 int size;
3490 int ret = 0;
3491
Dave Airliefb3b06c2011-02-08 13:55:21 +10003492 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3493 return -EINVAL;
3494
Daniel Vetter84849902012-12-02 00:28:11 +01003495 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003496 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3497 if (!obj) {
3498 ret = -EINVAL;
3499 goto out;
3500 }
3501 crtc = obj_to_crtc(obj);
3502
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003503 if (crtc->funcs->gamma_set == NULL) {
3504 ret = -ENOSYS;
3505 goto out;
3506 }
3507
Dave Airlief453ba02008-11-07 14:05:41 -08003508 /* memcpy into gamma store */
3509 if (crtc_lut->gamma_size != crtc->gamma_size) {
3510 ret = -EINVAL;
3511 goto out;
3512 }
3513
3514 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3515 r_base = crtc->gamma_store;
3516 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3517 ret = -EFAULT;
3518 goto out;
3519 }
3520
3521 g_base = r_base + size;
3522 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3523 ret = -EFAULT;
3524 goto out;
3525 }
3526
3527 b_base = g_base + size;
3528 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3529 ret = -EFAULT;
3530 goto out;
3531 }
3532
James Simmons72034252010-08-03 01:33:19 +01003533 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003534
3535out:
Daniel Vetter84849902012-12-02 00:28:11 +01003536 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003537 return ret;
3538
3539}
3540
3541int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3542 void *data, struct drm_file *file_priv)
3543{
3544 struct drm_mode_crtc_lut *crtc_lut = data;
3545 struct drm_mode_object *obj;
3546 struct drm_crtc *crtc;
3547 void *r_base, *g_base, *b_base;
3548 int size;
3549 int ret = 0;
3550
Dave Airliefb3b06c2011-02-08 13:55:21 +10003551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3552 return -EINVAL;
3553
Daniel Vetter84849902012-12-02 00:28:11 +01003554 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003555 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3556 if (!obj) {
3557 ret = -EINVAL;
3558 goto out;
3559 }
3560 crtc = obj_to_crtc(obj);
3561
3562 /* memcpy into gamma store */
3563 if (crtc_lut->gamma_size != crtc->gamma_size) {
3564 ret = -EINVAL;
3565 goto out;
3566 }
3567
3568 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3569 r_base = crtc->gamma_store;
3570 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3571 ret = -EFAULT;
3572 goto out;
3573 }
3574
3575 g_base = r_base + size;
3576 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3577 ret = -EFAULT;
3578 goto out;
3579 }
3580
3581 b_base = g_base + size;
3582 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3583 ret = -EFAULT;
3584 goto out;
3585 }
3586out:
Daniel Vetter84849902012-12-02 00:28:11 +01003587 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003588 return ret;
3589}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003590
3591int drm_mode_page_flip_ioctl(struct drm_device *dev,
3592 void *data, struct drm_file *file_priv)
3593{
3594 struct drm_mode_crtc_page_flip *page_flip = data;
3595 struct drm_mode_object *obj;
3596 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003597 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003598 struct drm_pending_vblank_event *e = NULL;
3599 unsigned long flags;
3600 int ret = -EINVAL;
3601
3602 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3603 page_flip->reserved != 0)
3604 return -EINVAL;
3605
Keith Packard62f21042013-07-22 18:50:00 -07003606 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3607 return -EINVAL;
3608
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003609 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3610 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003611 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003612 crtc = obj_to_crtc(obj);
3613
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003614 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003615 if (crtc->fb == NULL) {
3616 /* The framebuffer is currently unbound, presumably
3617 * due to a hotplug event, that userspace has not
3618 * yet discovered.
3619 */
3620 ret = -EBUSY;
3621 goto out;
3622 }
3623
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003624 if (crtc->funcs->page_flip == NULL)
3625 goto out;
3626
Daniel Vetter786b99e2012-12-02 21:53:40 +01003627 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3628 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003629 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003630
Damien Lespiauc11e9282013-09-25 16:45:30 +01003631 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3632 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003633 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003634
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003635 if (crtc->fb->pixel_format != fb->pixel_format) {
3636 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3637 ret = -EINVAL;
3638 goto out;
3639 }
3640
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003641 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3642 ret = -ENOMEM;
3643 spin_lock_irqsave(&dev->event_lock, flags);
3644 if (file_priv->event_space < sizeof e->event) {
3645 spin_unlock_irqrestore(&dev->event_lock, flags);
3646 goto out;
3647 }
3648 file_priv->event_space -= sizeof e->event;
3649 spin_unlock_irqrestore(&dev->event_lock, flags);
3650
3651 e = kzalloc(sizeof *e, GFP_KERNEL);
3652 if (e == NULL) {
3653 spin_lock_irqsave(&dev->event_lock, flags);
3654 file_priv->event_space += sizeof e->event;
3655 spin_unlock_irqrestore(&dev->event_lock, flags);
3656 goto out;
3657 }
3658
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003659 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003660 e->event.base.length = sizeof e->event;
3661 e->event.user_data = page_flip->user_data;
3662 e->base.event = &e->event.base;
3663 e->base.file_priv = file_priv;
3664 e->base.destroy =
3665 (void (*) (struct drm_pending_event *)) kfree;
3666 }
3667
Daniel Vetterb0d12322012-12-11 01:07:12 +01003668 old_fb = crtc->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07003669 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003670 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003671 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3672 spin_lock_irqsave(&dev->event_lock, flags);
3673 file_priv->event_space += sizeof e->event;
3674 spin_unlock_irqrestore(&dev->event_lock, flags);
3675 kfree(e);
3676 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003677 /* Keep the old fb, don't unref it. */
3678 old_fb = NULL;
3679 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003680 /*
3681 * Warn if the driver hasn't properly updated the crtc->fb
3682 * field to reflect that the new framebuffer is now used.
3683 * Failing to do so will screw with the reference counting
3684 * on framebuffers.
3685 */
3686 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003687 /* Unref only the old framebuffer. */
3688 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003689 }
3690
3691out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003692 if (fb)
3693 drm_framebuffer_unreference(fb);
3694 if (old_fb)
3695 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003696 mutex_unlock(&crtc->mutex);
3697
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003698 return ret;
3699}
Chris Wilsoneb033552011-01-24 15:11:08 +00003700
3701void drm_mode_config_reset(struct drm_device *dev)
3702{
3703 struct drm_crtc *crtc;
3704 struct drm_encoder *encoder;
3705 struct drm_connector *connector;
3706
3707 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3708 if (crtc->funcs->reset)
3709 crtc->funcs->reset(crtc);
3710
3711 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3712 if (encoder->funcs->reset)
3713 encoder->funcs->reset(encoder);
3714
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003715 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3716 connector->status = connector_status_unknown;
3717
Chris Wilsoneb033552011-01-24 15:11:08 +00003718 if (connector->funcs->reset)
3719 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003720 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003721}
3722EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003723
3724int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3725 void *data, struct drm_file *file_priv)
3726{
3727 struct drm_mode_create_dumb *args = data;
3728
3729 if (!dev->driver->dumb_create)
3730 return -ENOSYS;
3731 return dev->driver->dumb_create(file_priv, dev, args);
3732}
3733
3734int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3735 void *data, struct drm_file *file_priv)
3736{
3737 struct drm_mode_map_dumb *args = data;
3738
3739 /* call driver ioctl to get mmap offset */
3740 if (!dev->driver->dumb_map_offset)
3741 return -ENOSYS;
3742
3743 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3744}
3745
3746int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3747 void *data, struct drm_file *file_priv)
3748{
3749 struct drm_mode_destroy_dumb *args = data;
3750
3751 if (!dev->driver->dumb_destroy)
3752 return -ENOSYS;
3753
3754 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3755}
Dave Airlie248dbc22011-11-29 20:02:54 +00003756
3757/*
3758 * Just need to support RGB formats here for compat with code that doesn't
3759 * use pixel formats directly yet.
3760 */
3761void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3762 int *bpp)
3763{
3764 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003765 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003766 case DRM_FORMAT_RGB332:
3767 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003768 *depth = 8;
3769 *bpp = 8;
3770 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003771 case DRM_FORMAT_XRGB1555:
3772 case DRM_FORMAT_XBGR1555:
3773 case DRM_FORMAT_RGBX5551:
3774 case DRM_FORMAT_BGRX5551:
3775 case DRM_FORMAT_ARGB1555:
3776 case DRM_FORMAT_ABGR1555:
3777 case DRM_FORMAT_RGBA5551:
3778 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003779 *depth = 15;
3780 *bpp = 16;
3781 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003782 case DRM_FORMAT_RGB565:
3783 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003784 *depth = 16;
3785 *bpp = 16;
3786 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003787 case DRM_FORMAT_RGB888:
3788 case DRM_FORMAT_BGR888:
3789 *depth = 24;
3790 *bpp = 24;
3791 break;
3792 case DRM_FORMAT_XRGB8888:
3793 case DRM_FORMAT_XBGR8888:
3794 case DRM_FORMAT_RGBX8888:
3795 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003796 *depth = 24;
3797 *bpp = 32;
3798 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003799 case DRM_FORMAT_XRGB2101010:
3800 case DRM_FORMAT_XBGR2101010:
3801 case DRM_FORMAT_RGBX1010102:
3802 case DRM_FORMAT_BGRX1010102:
3803 case DRM_FORMAT_ARGB2101010:
3804 case DRM_FORMAT_ABGR2101010:
3805 case DRM_FORMAT_RGBA1010102:
3806 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003807 *depth = 30;
3808 *bpp = 32;
3809 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003810 case DRM_FORMAT_ARGB8888:
3811 case DRM_FORMAT_ABGR8888:
3812 case DRM_FORMAT_RGBA8888:
3813 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003814 *depth = 32;
3815 *bpp = 32;
3816 break;
3817 default:
3818 DRM_DEBUG_KMS("unsupported pixel format\n");
3819 *depth = 0;
3820 *bpp = 0;
3821 break;
3822 }
3823}
3824EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003825
3826/**
3827 * drm_format_num_planes - get the number of planes for format
3828 * @format: pixel format (DRM_FORMAT_*)
3829 *
3830 * RETURNS:
3831 * The number of planes used by the specified pixel format.
3832 */
3833int drm_format_num_planes(uint32_t format)
3834{
3835 switch (format) {
3836 case DRM_FORMAT_YUV410:
3837 case DRM_FORMAT_YVU410:
3838 case DRM_FORMAT_YUV411:
3839 case DRM_FORMAT_YVU411:
3840 case DRM_FORMAT_YUV420:
3841 case DRM_FORMAT_YVU420:
3842 case DRM_FORMAT_YUV422:
3843 case DRM_FORMAT_YVU422:
3844 case DRM_FORMAT_YUV444:
3845 case DRM_FORMAT_YVU444:
3846 return 3;
3847 case DRM_FORMAT_NV12:
3848 case DRM_FORMAT_NV21:
3849 case DRM_FORMAT_NV16:
3850 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003851 case DRM_FORMAT_NV24:
3852 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003853 return 2;
3854 default:
3855 return 1;
3856 }
3857}
3858EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003859
3860/**
3861 * drm_format_plane_cpp - determine the bytes per pixel value
3862 * @format: pixel format (DRM_FORMAT_*)
3863 * @plane: plane index
3864 *
3865 * RETURNS:
3866 * The bytes per pixel value for the specified plane.
3867 */
3868int drm_format_plane_cpp(uint32_t format, int plane)
3869{
3870 unsigned int depth;
3871 int bpp;
3872
3873 if (plane >= drm_format_num_planes(format))
3874 return 0;
3875
3876 switch (format) {
3877 case DRM_FORMAT_YUYV:
3878 case DRM_FORMAT_YVYU:
3879 case DRM_FORMAT_UYVY:
3880 case DRM_FORMAT_VYUY:
3881 return 2;
3882 case DRM_FORMAT_NV12:
3883 case DRM_FORMAT_NV21:
3884 case DRM_FORMAT_NV16:
3885 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003886 case DRM_FORMAT_NV24:
3887 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003888 return plane ? 2 : 1;
3889 case DRM_FORMAT_YUV410:
3890 case DRM_FORMAT_YVU410:
3891 case DRM_FORMAT_YUV411:
3892 case DRM_FORMAT_YVU411:
3893 case DRM_FORMAT_YUV420:
3894 case DRM_FORMAT_YVU420:
3895 case DRM_FORMAT_YUV422:
3896 case DRM_FORMAT_YVU422:
3897 case DRM_FORMAT_YUV444:
3898 case DRM_FORMAT_YVU444:
3899 return 1;
3900 default:
3901 drm_fb_get_bpp_depth(format, &depth, &bpp);
3902 return bpp >> 3;
3903 }
3904}
3905EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003906
3907/**
3908 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3909 * @format: pixel format (DRM_FORMAT_*)
3910 *
3911 * RETURNS:
3912 * The horizontal chroma subsampling factor for the
3913 * specified pixel format.
3914 */
3915int drm_format_horz_chroma_subsampling(uint32_t format)
3916{
3917 switch (format) {
3918 case DRM_FORMAT_YUV411:
3919 case DRM_FORMAT_YVU411:
3920 case DRM_FORMAT_YUV410:
3921 case DRM_FORMAT_YVU410:
3922 return 4;
3923 case DRM_FORMAT_YUYV:
3924 case DRM_FORMAT_YVYU:
3925 case DRM_FORMAT_UYVY:
3926 case DRM_FORMAT_VYUY:
3927 case DRM_FORMAT_NV12:
3928 case DRM_FORMAT_NV21:
3929 case DRM_FORMAT_NV16:
3930 case DRM_FORMAT_NV61:
3931 case DRM_FORMAT_YUV422:
3932 case DRM_FORMAT_YVU422:
3933 case DRM_FORMAT_YUV420:
3934 case DRM_FORMAT_YVU420:
3935 return 2;
3936 default:
3937 return 1;
3938 }
3939}
3940EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3941
3942/**
3943 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3944 * @format: pixel format (DRM_FORMAT_*)
3945 *
3946 * RETURNS:
3947 * The vertical chroma subsampling factor for the
3948 * specified pixel format.
3949 */
3950int drm_format_vert_chroma_subsampling(uint32_t format)
3951{
3952 switch (format) {
3953 case DRM_FORMAT_YUV410:
3954 case DRM_FORMAT_YVU410:
3955 return 4;
3956 case DRM_FORMAT_YUV420:
3957 case DRM_FORMAT_YVU420:
3958 case DRM_FORMAT_NV12:
3959 case DRM_FORMAT_NV21:
3960 return 2;
3961 default:
3962 return 1;
3963 }
3964}
3965EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003966
3967/**
3968 * drm_mode_config_init - initialize DRM mode_configuration structure
3969 * @dev: DRM device
3970 *
3971 * Initialize @dev's mode_config structure, used for tracking the graphics
3972 * configuration of @dev.
3973 *
3974 * Since this initializes the modeset locks, no locking is possible. Which is no
3975 * problem, since this should happen single threaded at init time. It is the
3976 * driver's problem to ensure this guarantee.
3977 *
3978 */
3979void drm_mode_config_init(struct drm_device *dev)
3980{
3981 mutex_init(&dev->mode_config.mutex);
3982 mutex_init(&dev->mode_config.idr_mutex);
3983 mutex_init(&dev->mode_config.fb_lock);
3984 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3985 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3986 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04003987 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003988 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3989 INIT_LIST_HEAD(&dev->mode_config.property_list);
3990 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3991 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3992 idr_init(&dev->mode_config.crtc_idr);
3993
3994 drm_modeset_lock_all(dev);
3995 drm_mode_create_standard_connector_properties(dev);
3996 drm_modeset_unlock_all(dev);
3997
3998 /* Just to be sure */
3999 dev->mode_config.num_fb = 0;
4000 dev->mode_config.num_connector = 0;
4001 dev->mode_config.num_crtc = 0;
4002 dev->mode_config.num_encoder = 0;
4003}
4004EXPORT_SYMBOL(drm_mode_config_init);
4005
4006/**
4007 * drm_mode_config_cleanup - free up DRM mode_config info
4008 * @dev: DRM device
4009 *
4010 * Free up all the connectors and CRTCs associated with this DRM device, then
4011 * free up the framebuffers and associated buffer objects.
4012 *
4013 * Note that since this /should/ happen single-threaded at driver/device
4014 * teardown time, no locking is required. It's the driver's job to ensure that
4015 * this guarantee actually holds true.
4016 *
4017 * FIXME: cleanup any dangling user buffer objects too
4018 */
4019void drm_mode_config_cleanup(struct drm_device *dev)
4020{
4021 struct drm_connector *connector, *ot;
4022 struct drm_crtc *crtc, *ct;
4023 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004024 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004025 struct drm_framebuffer *fb, *fbt;
4026 struct drm_property *property, *pt;
4027 struct drm_property_blob *blob, *bt;
4028 struct drm_plane *plane, *plt;
4029
4030 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4031 head) {
4032 encoder->funcs->destroy(encoder);
4033 }
4034
Sean Paul3b336ec2013-08-14 16:47:37 -04004035 list_for_each_entry_safe(bridge, brt,
4036 &dev->mode_config.bridge_list, head) {
4037 bridge->funcs->destroy(bridge);
4038 }
4039
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004040 list_for_each_entry_safe(connector, ot,
4041 &dev->mode_config.connector_list, head) {
4042 connector->funcs->destroy(connector);
4043 }
4044
4045 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4046 head) {
4047 drm_property_destroy(dev, property);
4048 }
4049
4050 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4051 head) {
4052 drm_property_destroy_blob(dev, blob);
4053 }
4054
4055 /*
4056 * Single-threaded teardown context, so it's not required to grab the
4057 * fb_lock to protect against concurrent fb_list access. Contrary, it
4058 * would actually deadlock with the drm_framebuffer_cleanup function.
4059 *
4060 * Also, if there are any framebuffers left, that's a driver leak now,
4061 * so politely WARN about this.
4062 */
4063 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4064 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4065 drm_framebuffer_remove(fb);
4066 }
4067
4068 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4069 head) {
4070 plane->funcs->destroy(plane);
4071 }
4072
4073 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4074 crtc->funcs->destroy(crtc);
4075 }
4076
4077 idr_destroy(&dev->mode_config.crtc_idr);
4078}
4079EXPORT_SYMBOL(drm_mode_config_cleanup);