blob: 3b7d32da16046ffcdc238ba41b1b03a3ec152435 [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/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000678 * drm_crtc_index - find the index of a registered CRTC
679 * @crtc: CRTC to find index for
680 *
681 * Given a registered CRTC, return the index of that CRTC within a DRM
682 * device's list of CRTCs.
683 */
684unsigned int drm_crtc_index(struct drm_crtc *crtc)
685{
686 unsigned int index = 0;
687 struct drm_crtc *tmp;
688
689 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
690 if (tmp == crtc)
691 return index;
692
693 index++;
694 }
695
696 BUG();
697}
698EXPORT_SYMBOL(drm_crtc_index);
699
700/**
Dave Airlief453ba02008-11-07 14:05:41 -0800701 * drm_mode_probed_add - add a mode to a connector's probed mode list
702 * @connector: connector the new mode
703 * @mode: mode data
704 *
Dave Airlief453ba02008-11-07 14:05:41 -0800705 * Add @mode to @connector's mode list for later use.
706 */
707void drm_mode_probed_add(struct drm_connector *connector,
708 struct drm_display_mode *mode)
709{
Ville Syrjälä990256a2013-05-31 12:17:07 +0000710 list_add_tail(&mode->head, &connector->probed_modes);
Dave Airlief453ba02008-11-07 14:05:41 -0800711}
712EXPORT_SYMBOL(drm_mode_probed_add);
713
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100714/*
Dave Airlief453ba02008-11-07 14:05:41 -0800715 * drm_mode_remove - remove and free a mode
716 * @connector: connector list to modify
717 * @mode: mode to remove
718 *
Dave Airlief453ba02008-11-07 14:05:41 -0800719 * Remove @mode from @connector's mode list, then free it.
720 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100721static void drm_mode_remove(struct drm_connector *connector,
722 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800723{
724 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100725 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800726}
Dave Airlief453ba02008-11-07 14:05:41 -0800727
728/**
729 * drm_connector_init - Init a preallocated connector
730 * @dev: DRM device
731 * @connector: the connector to init
732 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100733 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800734 *
Dave Airlief453ba02008-11-07 14:05:41 -0800735 * Initialises a preallocated connector. Connectors should be
736 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200737 *
738 * RETURNS:
739 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800740 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200741int drm_connector_init(struct drm_device *dev,
742 struct drm_connector *connector,
743 const struct drm_connector_funcs *funcs,
744 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800745{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200746 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400747 struct ida *connector_ida =
748 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200749
Daniel Vetter84849902012-12-02 00:28:11 +0100750 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800751
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200752 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
753 if (ret)
754 goto out;
755
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300756 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800757 connector->dev = dev;
758 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800759 connector->connector_type = connector_type;
760 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400761 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
762 if (connector->connector_type_id < 0) {
763 ret = connector->connector_type_id;
764 drm_mode_object_put(dev, &connector->base);
765 goto out;
766 }
Dave Airlief453ba02008-11-07 14:05:41 -0800767 INIT_LIST_HEAD(&connector->probed_modes);
768 INIT_LIST_HEAD(&connector->modes);
769 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000770 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800771
772 list_add_tail(&connector->head, &dev->mode_config.connector_list);
773 dev->mode_config.num_connector++;
774
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200775 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500776 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200777 dev->mode_config.edid_property,
778 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800779
Rob Clark58495562012-10-11 20:50:56 -0500780 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800781 dev->mode_config.dpms_property, 0);
782
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200783 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100784 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200785
786 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800787}
788EXPORT_SYMBOL(drm_connector_init);
789
790/**
791 * drm_connector_cleanup - cleans up an initialised connector
792 * @connector: connector to cleanup
793 *
Dave Airlief453ba02008-11-07 14:05:41 -0800794 * Cleans up the connector but doesn't free the object.
795 */
796void drm_connector_cleanup(struct drm_connector *connector)
797{
798 struct drm_device *dev = connector->dev;
799 struct drm_display_mode *mode, *t;
800
801 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
802 drm_mode_remove(connector, mode);
803
804 list_for_each_entry_safe(mode, t, &connector->modes, head)
805 drm_mode_remove(connector, mode);
806
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400807 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
808 connector->connector_type_id);
809
Dave Airlief453ba02008-11-07 14:05:41 -0800810 drm_mode_object_put(dev, &connector->base);
811 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000812 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800813}
814EXPORT_SYMBOL(drm_connector_cleanup);
815
Dave Airliecbc7e222012-02-20 14:16:40 +0000816void drm_connector_unplug_all(struct drm_device *dev)
817{
818 struct drm_connector *connector;
819
820 /* taking the mode config mutex ends up in a clash with sysfs */
821 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
822 drm_sysfs_connector_remove(connector);
823
824}
825EXPORT_SYMBOL(drm_connector_unplug_all);
826
Sean Paul3b336ec2013-08-14 16:47:37 -0400827int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
828 const struct drm_bridge_funcs *funcs)
829{
830 int ret;
831
832 drm_modeset_lock_all(dev);
833
834 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
835 if (ret)
836 goto out;
837
838 bridge->dev = dev;
839 bridge->funcs = funcs;
840
841 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
842 dev->mode_config.num_bridge++;
843
844 out:
845 drm_modeset_unlock_all(dev);
846 return ret;
847}
848EXPORT_SYMBOL(drm_bridge_init);
849
850void drm_bridge_cleanup(struct drm_bridge *bridge)
851{
852 struct drm_device *dev = bridge->dev;
853
854 drm_modeset_lock_all(dev);
855 drm_mode_object_put(dev, &bridge->base);
856 list_del(&bridge->head);
857 dev->mode_config.num_bridge--;
858 drm_modeset_unlock_all(dev);
859}
860EXPORT_SYMBOL(drm_bridge_cleanup);
861
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200862int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000863 struct drm_encoder *encoder,
864 const struct drm_encoder_funcs *funcs,
865 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800866{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200867 int ret;
868
Daniel Vetter84849902012-12-02 00:28:11 +0100869 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800870
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200871 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
872 if (ret)
873 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800874
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200875 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800876 encoder->encoder_type = encoder_type;
877 encoder->funcs = funcs;
878
879 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
880 dev->mode_config.num_encoder++;
881
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200882 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100883 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200884
885 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800886}
887EXPORT_SYMBOL(drm_encoder_init);
888
889void drm_encoder_cleanup(struct drm_encoder *encoder)
890{
891 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100892 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800893 drm_mode_object_put(dev, &encoder->base);
894 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000895 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100896 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800897}
898EXPORT_SYMBOL(drm_encoder_cleanup);
899
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300900/**
901 * drm_plane_init - Initialise a new plane object
902 * @dev: DRM device
903 * @plane: plane object to init
904 * @possible_crtcs: bitmask of possible CRTCs
905 * @funcs: callbacks for the new plane
906 * @formats: array of supported formats (%DRM_FORMAT_*)
907 * @format_count: number of elements in @formats
908 * @priv: plane is private (hidden from userspace)?
909 *
910 * Inits a new object created as base part of a driver plane object.
911 *
912 * RETURNS:
913 * Zero on success, error code on failure.
914 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800915int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
916 unsigned long possible_crtcs,
917 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600918 const uint32_t *formats, uint32_t format_count,
919 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800920{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200921 int ret;
922
Daniel Vetter84849902012-12-02 00:28:11 +0100923 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800924
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200925 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
926 if (ret)
927 goto out;
928
Rob Clark4d939142012-05-17 02:23:27 -0600929 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800930 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800931 plane->funcs = funcs;
932 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
933 GFP_KERNEL);
934 if (!plane->format_types) {
935 DRM_DEBUG_KMS("out of memory when allocating plane\n");
936 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200937 ret = -ENOMEM;
938 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800939 }
940
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800941 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800942 plane->format_count = format_count;
943 plane->possible_crtcs = possible_crtcs;
944
Rob Clark0a7eb242011-12-13 20:19:36 -0600945 /* private planes are not exposed to userspace, but depending on
946 * display hardware, might be convenient to allow sharing programming
947 * for the scanout engine with the crtc implementation.
948 */
949 if (!priv) {
950 list_add_tail(&plane->head, &dev->mode_config.plane_list);
951 dev->mode_config.num_plane++;
952 } else {
953 INIT_LIST_HEAD(&plane->head);
954 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800955
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200956 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100957 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800958
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200959 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800960}
961EXPORT_SYMBOL(drm_plane_init);
962
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300963/**
964 * drm_plane_cleanup - Clean up the core plane usage
965 * @plane: plane to cleanup
966 *
967 * This function cleans up @plane and removes it from the DRM mode setting
968 * core. Note that the function does *not* free the plane structure itself,
969 * this is the responsibility of the caller.
970 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800971void drm_plane_cleanup(struct drm_plane *plane)
972{
973 struct drm_device *dev = plane->dev;
974
Daniel Vetter84849902012-12-02 00:28:11 +0100975 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800976 kfree(plane->format_types);
977 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600978 /* if not added to a list, it must be a private plane */
979 if (!list_empty(&plane->head)) {
980 list_del(&plane->head);
981 dev->mode_config.num_plane--;
982 }
Daniel Vetter84849902012-12-02 00:28:11 +0100983 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800984}
985EXPORT_SYMBOL(drm_plane_cleanup);
986
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300987/**
988 * drm_plane_force_disable - Forcibly disable a plane
989 * @plane: plane to disable
990 *
991 * Forces the plane to be disabled.
992 *
993 * Used when the plane's current framebuffer is destroyed,
994 * and when restoring fbdev mode.
995 */
Ville Syrjälä9125e612013-06-03 16:10:40 +0300996void drm_plane_force_disable(struct drm_plane *plane)
997{
998 int ret;
999
1000 if (!plane->fb)
1001 return;
1002
1003 ret = plane->funcs->disable_plane(plane);
1004 if (ret)
1005 DRM_ERROR("failed to disable plane with busy fb\n");
1006 /* disconnect the plane from the fb and crtc: */
1007 __drm_framebuffer_unreference(plane->fb);
1008 plane->fb = NULL;
1009 plane->crtc = NULL;
1010}
1011EXPORT_SYMBOL(drm_plane_force_disable);
1012
Dave Airlief453ba02008-11-07 14:05:41 -08001013/**
1014 * drm_mode_create - create a new display mode
1015 * @dev: DRM device
1016 *
Dave Airlief453ba02008-11-07 14:05:41 -08001017 * Create a new drm_display_mode, give it an ID, and return it.
1018 *
1019 * RETURNS:
1020 * Pointer to new mode on success, NULL on error.
1021 */
1022struct drm_display_mode *drm_mode_create(struct drm_device *dev)
1023{
1024 struct drm_display_mode *nmode;
1025
1026 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
1027 if (!nmode)
1028 return NULL;
1029
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001030 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
1031 kfree(nmode);
1032 return NULL;
1033 }
1034
Dave Airlief453ba02008-11-07 14:05:41 -08001035 return nmode;
1036}
1037EXPORT_SYMBOL(drm_mode_create);
1038
1039/**
1040 * drm_mode_destroy - remove a mode
1041 * @dev: DRM device
1042 * @mode: mode to remove
1043 *
Dave Airlief453ba02008-11-07 14:05:41 -08001044 * Free @mode's unique identifier, then free it.
1045 */
1046void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
1047{
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001048 if (!mode)
1049 return;
1050
Dave Airlief453ba02008-11-07 14:05:41 -08001051 drm_mode_object_put(dev, &mode->base);
1052
1053 kfree(mode);
1054}
1055EXPORT_SYMBOL(drm_mode_destroy);
1056
1057static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1058{
1059 struct drm_property *edid;
1060 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001061
1062 /*
1063 * Standard properties (apply to all connectors)
1064 */
1065 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1066 DRM_MODE_PROP_IMMUTABLE,
1067 "EDID", 0);
1068 dev->mode_config.edid_property = edid;
1069
Sascha Hauer4a67d392012-02-06 10:58:17 +01001070 dpms = drm_property_create_enum(dev, 0,
1071 "DPMS", drm_dpms_enum_list,
1072 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001073 dev->mode_config.dpms_property = dpms;
1074
1075 return 0;
1076}
1077
1078/**
1079 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1080 * @dev: DRM device
1081 *
1082 * Called by a driver the first time a DVI-I connector is made.
1083 */
1084int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1085{
1086 struct drm_property *dvi_i_selector;
1087 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001088
1089 if (dev->mode_config.dvi_i_select_subconnector_property)
1090 return 0;
1091
1092 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001093 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001094 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001095 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001096 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001097 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1098
Sascha Hauer4a67d392012-02-06 10:58:17 +01001099 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001100 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001101 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001102 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001103 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1104
1105 return 0;
1106}
1107EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1108
1109/**
1110 * drm_create_tv_properties - create TV specific connector properties
1111 * @dev: DRM device
1112 * @num_modes: number of different TV formats (modes) supported
1113 * @modes: array of pointers to strings containing name of each format
1114 *
1115 * Called by a driver's TV initialization routine, this function creates
1116 * the TV specific connector properties for a given device. Caller is
1117 * responsible for allocating a list of format names and passing them to
1118 * this routine.
1119 */
1120int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1121 char *modes[])
1122{
1123 struct drm_property *tv_selector;
1124 struct drm_property *tv_subconnector;
1125 int i;
1126
1127 if (dev->mode_config.tv_select_subconnector_property)
1128 return 0;
1129
1130 /*
1131 * Basic connector properties
1132 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001133 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001134 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001135 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001136 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001137 dev->mode_config.tv_select_subconnector_property = tv_selector;
1138
1139 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001140 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1141 "subconnector",
1142 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001143 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001144 dev->mode_config.tv_subconnector_property = tv_subconnector;
1145
1146 /*
1147 * Other, TV specific properties: margins & TV modes.
1148 */
1149 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001150 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001151
1152 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001153 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001154
1155 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001156 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001157
1158 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001159 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001160
1161 dev->mode_config.tv_mode_property =
1162 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1163 "mode", num_modes);
1164 for (i = 0; i < num_modes; i++)
1165 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1166 i, modes[i]);
1167
Francisco Jerezb6b79022009-08-02 04:19:20 +02001168 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001169 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001170
1171 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001172 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001173
1174 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001175 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001176
Francisco Jereza75f0232009-08-12 02:30:10 +02001177 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001178 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001179
1180 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001181 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001182
1183 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001184 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001185
Dave Airlief453ba02008-11-07 14:05:41 -08001186 return 0;
1187}
1188EXPORT_SYMBOL(drm_mode_create_tv_properties);
1189
1190/**
1191 * drm_mode_create_scaling_mode_property - create scaling mode property
1192 * @dev: DRM device
1193 *
1194 * Called by a driver the first time it's needed, must be attached to desired
1195 * connectors.
1196 */
1197int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1198{
1199 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001200
1201 if (dev->mode_config.scaling_mode_property)
1202 return 0;
1203
1204 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001205 drm_property_create_enum(dev, 0, "scaling mode",
1206 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001207 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001208
1209 dev->mode_config.scaling_mode_property = scaling_mode;
1210
1211 return 0;
1212}
1213EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1214
1215/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001216 * drm_mode_create_dirty_property - create dirty property
1217 * @dev: DRM device
1218 *
1219 * Called by a driver the first time it's needed, must be attached to desired
1220 * connectors.
1221 */
1222int drm_mode_create_dirty_info_property(struct drm_device *dev)
1223{
1224 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001225
1226 if (dev->mode_config.dirty_info_property)
1227 return 0;
1228
1229 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001230 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001231 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001232 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001233 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001234 dev->mode_config.dirty_info_property = dirty_info;
1235
1236 return 0;
1237}
1238EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1239
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001240static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001241{
1242 uint32_t total_objects = 0;
1243
1244 total_objects += dev->mode_config.num_crtc;
1245 total_objects += dev->mode_config.num_connector;
1246 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001247 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001248
Dave Airlief453ba02008-11-07 14:05:41 -08001249 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1250 if (!group->id_list)
1251 return -ENOMEM;
1252
1253 group->num_crtcs = 0;
1254 group->num_connectors = 0;
1255 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001256 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001257 return 0;
1258}
1259
1260int drm_mode_group_init_legacy_group(struct drm_device *dev,
1261 struct drm_mode_group *group)
1262{
1263 struct drm_crtc *crtc;
1264 struct drm_encoder *encoder;
1265 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001266 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001267 int ret;
1268
1269 if ((ret = drm_mode_group_init(dev, group)))
1270 return ret;
1271
1272 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1273 group->id_list[group->num_crtcs++] = crtc->base.id;
1274
1275 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1276 group->id_list[group->num_crtcs + group->num_encoders++] =
1277 encoder->base.id;
1278
1279 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1280 group->id_list[group->num_crtcs + group->num_encoders +
1281 group->num_connectors++] = connector->base.id;
1282
Sean Paul3b336ec2013-08-14 16:47:37 -04001283 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1284 group->id_list[group->num_crtcs + group->num_encoders +
1285 group->num_connectors + group->num_bridges++] =
1286 bridge->base.id;
1287
Dave Airlief453ba02008-11-07 14:05:41 -08001288 return 0;
1289}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001290EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001291
1292/**
Dave Airlief453ba02008-11-07 14:05:41 -08001293 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1294 * @out: drm_mode_modeinfo struct to return to the user
1295 * @in: drm_display_mode to use
1296 *
Dave Airlief453ba02008-11-07 14:05:41 -08001297 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1298 * the user.
1299 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001300static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1301 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001302{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001303 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1304 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1305 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1306 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1307 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1308 "timing values too large for mode info\n");
1309
Dave Airlief453ba02008-11-07 14:05:41 -08001310 out->clock = in->clock;
1311 out->hdisplay = in->hdisplay;
1312 out->hsync_start = in->hsync_start;
1313 out->hsync_end = in->hsync_end;
1314 out->htotal = in->htotal;
1315 out->hskew = in->hskew;
1316 out->vdisplay = in->vdisplay;
1317 out->vsync_start = in->vsync_start;
1318 out->vsync_end = in->vsync_end;
1319 out->vtotal = in->vtotal;
1320 out->vscan = in->vscan;
1321 out->vrefresh = in->vrefresh;
1322 out->flags = in->flags;
1323 out->type = in->type;
1324 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1325 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1326}
1327
1328/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001329 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001330 * @out: drm_display_mode to return to the user
1331 * @in: drm_mode_modeinfo to use
1332 *
Dave Airlief453ba02008-11-07 14:05:41 -08001333 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1334 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001335 *
1336 * RETURNS:
1337 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001338 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001339static int drm_crtc_convert_umode(struct drm_display_mode *out,
1340 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001341{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001342 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1343 return -ERANGE;
1344
Damien Lespiau5848ad402013-09-27 12:11:50 +01001345 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1346 return -EINVAL;
1347
Dave Airlief453ba02008-11-07 14:05:41 -08001348 out->clock = in->clock;
1349 out->hdisplay = in->hdisplay;
1350 out->hsync_start = in->hsync_start;
1351 out->hsync_end = in->hsync_end;
1352 out->htotal = in->htotal;
1353 out->hskew = in->hskew;
1354 out->vdisplay = in->vdisplay;
1355 out->vsync_start = in->vsync_start;
1356 out->vsync_end = in->vsync_end;
1357 out->vtotal = in->vtotal;
1358 out->vscan = in->vscan;
1359 out->vrefresh = in->vrefresh;
1360 out->flags = in->flags;
1361 out->type = in->type;
1362 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1363 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001364
1365 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001366}
1367
1368/**
1369 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001370 * @dev: drm device for the ioctl
1371 * @data: data pointer for the ioctl
1372 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001373 *
Dave Airlief453ba02008-11-07 14:05:41 -08001374 * Construct a set of configuration description structures and return
1375 * them to the user, including CRTC, connector and framebuffer configuration.
1376 *
1377 * Called by the user via ioctl.
1378 *
1379 * RETURNS:
1380 * Zero on success, errno on failure.
1381 */
1382int drm_mode_getresources(struct drm_device *dev, void *data,
1383 struct drm_file *file_priv)
1384{
1385 struct drm_mode_card_res *card_res = data;
1386 struct list_head *lh;
1387 struct drm_framebuffer *fb;
1388 struct drm_connector *connector;
1389 struct drm_crtc *crtc;
1390 struct drm_encoder *encoder;
1391 int ret = 0;
1392 int connector_count = 0;
1393 int crtc_count = 0;
1394 int fb_count = 0;
1395 int encoder_count = 0;
1396 int copied = 0, i;
1397 uint32_t __user *fb_id;
1398 uint32_t __user *crtc_id;
1399 uint32_t __user *connector_id;
1400 uint32_t __user *encoder_id;
1401 struct drm_mode_group *mode_group;
1402
Dave Airliefb3b06c2011-02-08 13:55:21 +10001403 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1404 return -EINVAL;
1405
Dave Airlief453ba02008-11-07 14:05:41 -08001406
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001407 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001408 /*
1409 * For the non-control nodes we need to limit the list of resources
1410 * by IDs in the group list for this node
1411 */
1412 list_for_each(lh, &file_priv->fbs)
1413 fb_count++;
1414
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001415 /* handle this in 4 parts */
1416 /* FBs */
1417 if (card_res->count_fbs >= fb_count) {
1418 copied = 0;
1419 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1420 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1421 if (put_user(fb->base.id, fb_id + copied)) {
1422 mutex_unlock(&file_priv->fbs_lock);
1423 return -EFAULT;
1424 }
1425 copied++;
1426 }
1427 }
1428 card_res->count_fbs = fb_count;
1429 mutex_unlock(&file_priv->fbs_lock);
1430
1431 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001432 mode_group = &file_priv->master->minor->mode_group;
1433 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1434
1435 list_for_each(lh, &dev->mode_config.crtc_list)
1436 crtc_count++;
1437
1438 list_for_each(lh, &dev->mode_config.connector_list)
1439 connector_count++;
1440
1441 list_for_each(lh, &dev->mode_config.encoder_list)
1442 encoder_count++;
1443 } else {
1444
1445 crtc_count = mode_group->num_crtcs;
1446 connector_count = mode_group->num_connectors;
1447 encoder_count = mode_group->num_encoders;
1448 }
1449
1450 card_res->max_height = dev->mode_config.max_height;
1451 card_res->min_height = dev->mode_config.min_height;
1452 card_res->max_width = dev->mode_config.max_width;
1453 card_res->min_width = dev->mode_config.min_width;
1454
Dave Airlief453ba02008-11-07 14:05:41 -08001455 /* CRTCs */
1456 if (card_res->count_crtcs >= crtc_count) {
1457 copied = 0;
1458 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1459 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1460 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1461 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001462 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001463 if (put_user(crtc->base.id, crtc_id + copied)) {
1464 ret = -EFAULT;
1465 goto out;
1466 }
1467 copied++;
1468 }
1469 } else {
1470 for (i = 0; i < mode_group->num_crtcs; i++) {
1471 if (put_user(mode_group->id_list[i],
1472 crtc_id + copied)) {
1473 ret = -EFAULT;
1474 goto out;
1475 }
1476 copied++;
1477 }
1478 }
1479 }
1480 card_res->count_crtcs = crtc_count;
1481
1482 /* Encoders */
1483 if (card_res->count_encoders >= encoder_count) {
1484 copied = 0;
1485 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1486 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1487 list_for_each_entry(encoder,
1488 &dev->mode_config.encoder_list,
1489 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001490 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1491 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001492 if (put_user(encoder->base.id, encoder_id +
1493 copied)) {
1494 ret = -EFAULT;
1495 goto out;
1496 }
1497 copied++;
1498 }
1499 } else {
1500 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1501 if (put_user(mode_group->id_list[i],
1502 encoder_id + copied)) {
1503 ret = -EFAULT;
1504 goto out;
1505 }
1506 copied++;
1507 }
1508
1509 }
1510 }
1511 card_res->count_encoders = encoder_count;
1512
1513 /* Connectors */
1514 if (card_res->count_connectors >= connector_count) {
1515 copied = 0;
1516 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1517 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1518 list_for_each_entry(connector,
1519 &dev->mode_config.connector_list,
1520 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001521 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1522 connector->base.id,
1523 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001524 if (put_user(connector->base.id,
1525 connector_id + copied)) {
1526 ret = -EFAULT;
1527 goto out;
1528 }
1529 copied++;
1530 }
1531 } else {
1532 int start = mode_group->num_crtcs +
1533 mode_group->num_encoders;
1534 for (i = start; i < start + mode_group->num_connectors; i++) {
1535 if (put_user(mode_group->id_list[i],
1536 connector_id + copied)) {
1537 ret = -EFAULT;
1538 goto out;
1539 }
1540 copied++;
1541 }
1542 }
1543 }
1544 card_res->count_connectors = connector_count;
1545
Jerome Glisse94401062010-07-15 15:43:25 -04001546 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001547 card_res->count_connectors, card_res->count_encoders);
1548
1549out:
Daniel Vetter84849902012-12-02 00:28:11 +01001550 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001551 return ret;
1552}
1553
1554/**
1555 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001556 * @dev: drm device for the ioctl
1557 * @data: data pointer for the ioctl
1558 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001559 *
Dave Airlief453ba02008-11-07 14:05:41 -08001560 * Construct a CRTC configuration structure to return to the user.
1561 *
1562 * Called by the user via ioctl.
1563 *
1564 * RETURNS:
1565 * Zero on success, errno on failure.
1566 */
1567int drm_mode_getcrtc(struct drm_device *dev,
1568 void *data, struct drm_file *file_priv)
1569{
1570 struct drm_mode_crtc *crtc_resp = data;
1571 struct drm_crtc *crtc;
1572 struct drm_mode_object *obj;
1573 int ret = 0;
1574
Dave Airliefb3b06c2011-02-08 13:55:21 +10001575 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1576 return -EINVAL;
1577
Daniel Vetter84849902012-12-02 00:28:11 +01001578 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001579
1580 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1581 DRM_MODE_OBJECT_CRTC);
1582 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001583 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001584 goto out;
1585 }
1586 crtc = obj_to_crtc(obj);
1587
1588 crtc_resp->x = crtc->x;
1589 crtc_resp->y = crtc->y;
1590 crtc_resp->gamma_size = crtc->gamma_size;
1591 if (crtc->fb)
1592 crtc_resp->fb_id = crtc->fb->base.id;
1593 else
1594 crtc_resp->fb_id = 0;
1595
1596 if (crtc->enabled) {
1597
1598 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1599 crtc_resp->mode_valid = 1;
1600
1601 } else {
1602 crtc_resp->mode_valid = 0;
1603 }
1604
1605out:
Daniel Vetter84849902012-12-02 00:28:11 +01001606 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001607 return ret;
1608}
1609
Damien Lespiau61d8e322013-09-25 16:45:22 +01001610static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1611 const struct drm_file *file_priv)
1612{
1613 /*
1614 * If user-space hasn't configured the driver to expose the stereo 3D
1615 * modes, don't expose them.
1616 */
1617 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1618 return false;
1619
1620 return true;
1621}
1622
Dave Airlief453ba02008-11-07 14:05:41 -08001623/**
1624 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001625 * @dev: drm device for the ioctl
1626 * @data: data pointer for the ioctl
1627 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001628 *
Dave Airlief453ba02008-11-07 14:05:41 -08001629 * Construct a connector configuration structure to return to the user.
1630 *
1631 * Called by the user via ioctl.
1632 *
1633 * RETURNS:
1634 * Zero on success, errno on failure.
1635 */
1636int drm_mode_getconnector(struct drm_device *dev, void *data,
1637 struct drm_file *file_priv)
1638{
1639 struct drm_mode_get_connector *out_resp = data;
1640 struct drm_mode_object *obj;
1641 struct drm_connector *connector;
1642 struct drm_display_mode *mode;
1643 int mode_count = 0;
1644 int props_count = 0;
1645 int encoders_count = 0;
1646 int ret = 0;
1647 int copied = 0;
1648 int i;
1649 struct drm_mode_modeinfo u_mode;
1650 struct drm_mode_modeinfo __user *mode_ptr;
1651 uint32_t __user *prop_ptr;
1652 uint64_t __user *prop_values;
1653 uint32_t __user *encoder_ptr;
1654
Dave Airliefb3b06c2011-02-08 13:55:21 +10001655 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1656 return -EINVAL;
1657
Dave Airlief453ba02008-11-07 14:05:41 -08001658 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1659
Jerome Glisse94401062010-07-15 15:43:25 -04001660 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001661
Daniel Vetter7b240562012-12-12 00:35:33 +01001662 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001663
1664 obj = drm_mode_object_find(dev, out_resp->connector_id,
1665 DRM_MODE_OBJECT_CONNECTOR);
1666 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001667 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001668 goto out;
1669 }
1670 connector = obj_to_connector(obj);
1671
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001672 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001673
1674 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1675 if (connector->encoder_ids[i] != 0) {
1676 encoders_count++;
1677 }
1678 }
1679
1680 if (out_resp->count_modes == 0) {
1681 connector->funcs->fill_modes(connector,
1682 dev->mode_config.max_width,
1683 dev->mode_config.max_height);
1684 }
1685
1686 /* delayed so we get modes regardless of pre-fill_modes state */
1687 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001688 if (drm_mode_expose_to_userspace(mode, file_priv))
1689 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001690
1691 out_resp->connector_id = connector->base.id;
1692 out_resp->connector_type = connector->connector_type;
1693 out_resp->connector_type_id = connector->connector_type_id;
1694 out_resp->mm_width = connector->display_info.width_mm;
1695 out_resp->mm_height = connector->display_info.height_mm;
1696 out_resp->subpixel = connector->display_info.subpixel_order;
1697 out_resp->connection = connector->status;
1698 if (connector->encoder)
1699 out_resp->encoder_id = connector->encoder->base.id;
1700 else
1701 out_resp->encoder_id = 0;
1702
1703 /*
1704 * This ioctl is called twice, once to determine how much space is
1705 * needed, and the 2nd time to fill it.
1706 */
1707 if ((out_resp->count_modes >= mode_count) && mode_count) {
1708 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001709 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001710 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001711 if (!drm_mode_expose_to_userspace(mode, file_priv))
1712 continue;
1713
Dave Airlief453ba02008-11-07 14:05:41 -08001714 drm_crtc_convert_to_umode(&u_mode, mode);
1715 if (copy_to_user(mode_ptr + copied,
1716 &u_mode, sizeof(u_mode))) {
1717 ret = -EFAULT;
1718 goto out;
1719 }
1720 copied++;
1721 }
1722 }
1723 out_resp->count_modes = mode_count;
1724
1725 if ((out_resp->count_props >= props_count) && props_count) {
1726 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001727 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1728 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001729 for (i = 0; i < connector->properties.count; i++) {
1730 if (put_user(connector->properties.ids[i],
1731 prop_ptr + copied)) {
1732 ret = -EFAULT;
1733 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001734 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001735
1736 if (put_user(connector->properties.values[i],
1737 prop_values + copied)) {
1738 ret = -EFAULT;
1739 goto out;
1740 }
1741 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001742 }
1743 }
1744 out_resp->count_props = props_count;
1745
1746 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1747 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001748 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001749 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1750 if (connector->encoder_ids[i] != 0) {
1751 if (put_user(connector->encoder_ids[i],
1752 encoder_ptr + copied)) {
1753 ret = -EFAULT;
1754 goto out;
1755 }
1756 copied++;
1757 }
1758 }
1759 }
1760 out_resp->count_encoders = encoders_count;
1761
1762out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001763 mutex_unlock(&dev->mode_config.mutex);
1764
Dave Airlief453ba02008-11-07 14:05:41 -08001765 return ret;
1766}
1767
1768int drm_mode_getencoder(struct drm_device *dev, void *data,
1769 struct drm_file *file_priv)
1770{
1771 struct drm_mode_get_encoder *enc_resp = data;
1772 struct drm_mode_object *obj;
1773 struct drm_encoder *encoder;
1774 int ret = 0;
1775
Dave Airliefb3b06c2011-02-08 13:55:21 +10001776 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1777 return -EINVAL;
1778
Daniel Vetter84849902012-12-02 00:28:11 +01001779 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001780 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1781 DRM_MODE_OBJECT_ENCODER);
1782 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001783 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001784 goto out;
1785 }
1786 encoder = obj_to_encoder(obj);
1787
1788 if (encoder->crtc)
1789 enc_resp->crtc_id = encoder->crtc->base.id;
1790 else
1791 enc_resp->crtc_id = 0;
1792 enc_resp->encoder_type = encoder->encoder_type;
1793 enc_resp->encoder_id = encoder->base.id;
1794 enc_resp->possible_crtcs = encoder->possible_crtcs;
1795 enc_resp->possible_clones = encoder->possible_clones;
1796
1797out:
Daniel Vetter84849902012-12-02 00:28:11 +01001798 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001799 return ret;
1800}
1801
1802/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001803 * drm_mode_getplane_res - get plane info
1804 * @dev: DRM device
1805 * @data: ioctl data
1806 * @file_priv: DRM file info
1807 *
1808 * Return an plane count and set of IDs.
1809 */
1810int drm_mode_getplane_res(struct drm_device *dev, void *data,
1811 struct drm_file *file_priv)
1812{
1813 struct drm_mode_get_plane_res *plane_resp = data;
1814 struct drm_mode_config *config;
1815 struct drm_plane *plane;
1816 uint32_t __user *plane_ptr;
1817 int copied = 0, ret = 0;
1818
1819 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1820 return -EINVAL;
1821
Daniel Vetter84849902012-12-02 00:28:11 +01001822 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001823 config = &dev->mode_config;
1824
1825 /*
1826 * This ioctl is called twice, once to determine how much space is
1827 * needed, and the 2nd time to fill it.
1828 */
1829 if (config->num_plane &&
1830 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001831 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001832
1833 list_for_each_entry(plane, &config->plane_list, head) {
1834 if (put_user(plane->base.id, plane_ptr + copied)) {
1835 ret = -EFAULT;
1836 goto out;
1837 }
1838 copied++;
1839 }
1840 }
1841 plane_resp->count_planes = config->num_plane;
1842
1843out:
Daniel Vetter84849902012-12-02 00:28:11 +01001844 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001845 return ret;
1846}
1847
1848/**
1849 * drm_mode_getplane - get plane info
1850 * @dev: DRM device
1851 * @data: ioctl data
1852 * @file_priv: DRM file info
1853 *
1854 * Return plane info, including formats supported, gamma size, any
1855 * current fb, etc.
1856 */
1857int drm_mode_getplane(struct drm_device *dev, void *data,
1858 struct drm_file *file_priv)
1859{
1860 struct drm_mode_get_plane *plane_resp = data;
1861 struct drm_mode_object *obj;
1862 struct drm_plane *plane;
1863 uint32_t __user *format_ptr;
1864 int ret = 0;
1865
1866 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1867 return -EINVAL;
1868
Daniel Vetter84849902012-12-02 00:28:11 +01001869 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001870 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1871 DRM_MODE_OBJECT_PLANE);
1872 if (!obj) {
1873 ret = -ENOENT;
1874 goto out;
1875 }
1876 plane = obj_to_plane(obj);
1877
1878 if (plane->crtc)
1879 plane_resp->crtc_id = plane->crtc->base.id;
1880 else
1881 plane_resp->crtc_id = 0;
1882
1883 if (plane->fb)
1884 plane_resp->fb_id = plane->fb->base.id;
1885 else
1886 plane_resp->fb_id = 0;
1887
1888 plane_resp->plane_id = plane->base.id;
1889 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001890 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001891
1892 /*
1893 * This ioctl is called twice, once to determine how much space is
1894 * needed, and the 2nd time to fill it.
1895 */
1896 if (plane->format_count &&
1897 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001898 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001899 if (copy_to_user(format_ptr,
1900 plane->format_types,
1901 sizeof(uint32_t) * plane->format_count)) {
1902 ret = -EFAULT;
1903 goto out;
1904 }
1905 }
1906 plane_resp->count_format_types = plane->format_count;
1907
1908out:
Daniel Vetter84849902012-12-02 00:28:11 +01001909 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001910 return ret;
1911}
1912
1913/**
1914 * drm_mode_setplane - set up or tear down an plane
1915 * @dev: DRM device
1916 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001917 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001918 *
1919 * Set plane info, including placement, fb, scaling, and other factors.
1920 * Or pass a NULL fb to disable.
1921 */
1922int drm_mode_setplane(struct drm_device *dev, void *data,
1923 struct drm_file *file_priv)
1924{
1925 struct drm_mode_set_plane *plane_req = data;
1926 struct drm_mode_object *obj;
1927 struct drm_plane *plane;
1928 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001929 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001930 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001931 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001932 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001933
1934 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1935 return -EINVAL;
1936
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001937 /*
1938 * First, find the plane, crtc, and fb objects. If not available,
1939 * we don't bother to call the driver.
1940 */
1941 obj = drm_mode_object_find(dev, plane_req->plane_id,
1942 DRM_MODE_OBJECT_PLANE);
1943 if (!obj) {
1944 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1945 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001946 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001947 }
1948 plane = obj_to_plane(obj);
1949
1950 /* No fb means shut it down */
1951 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001952 drm_modeset_lock_all(dev);
1953 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001954 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001955 plane->crtc = NULL;
1956 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001957 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001958 goto out;
1959 }
1960
1961 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1962 DRM_MODE_OBJECT_CRTC);
1963 if (!obj) {
1964 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1965 plane_req->crtc_id);
1966 ret = -ENOENT;
1967 goto out;
1968 }
1969 crtc = obj_to_crtc(obj);
1970
Daniel Vetter786b99e2012-12-02 21:53:40 +01001971 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1972 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001973 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1974 plane_req->fb_id);
1975 ret = -ENOENT;
1976 goto out;
1977 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001978
Ville Syrjälä62443be2011-12-20 00:06:47 +02001979 /* Check whether this plane supports the fb pixel format. */
1980 for (i = 0; i < plane->format_count; i++)
1981 if (fb->pixel_format == plane->format_types[i])
1982 break;
1983 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03001984 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1985 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02001986 ret = -EINVAL;
1987 goto out;
1988 }
1989
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001990 fb_width = fb->width << 16;
1991 fb_height = fb->height << 16;
1992
1993 /* Make sure source coordinates are inside the fb. */
1994 if (plane_req->src_w > fb_width ||
1995 plane_req->src_x > fb_width - plane_req->src_w ||
1996 plane_req->src_h > fb_height ||
1997 plane_req->src_y > fb_height - plane_req->src_h) {
1998 DRM_DEBUG_KMS("Invalid source coordinates "
1999 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2000 plane_req->src_w >> 16,
2001 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2002 plane_req->src_h >> 16,
2003 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2004 plane_req->src_x >> 16,
2005 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2006 plane_req->src_y >> 16,
2007 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2008 ret = -ENOSPC;
2009 goto out;
2010 }
2011
Ville Syrjälä687a0402011-12-20 00:06:45 +02002012 /* Give drivers some help against integer overflows */
2013 if (plane_req->crtc_w > INT_MAX ||
2014 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2015 plane_req->crtc_h > INT_MAX ||
2016 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2017 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2018 plane_req->crtc_w, plane_req->crtc_h,
2019 plane_req->crtc_x, plane_req->crtc_y);
2020 ret = -ERANGE;
2021 goto out;
2022 }
2023
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002024 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002025 ret = plane->funcs->update_plane(plane, crtc, fb,
2026 plane_req->crtc_x, plane_req->crtc_y,
2027 plane_req->crtc_w, plane_req->crtc_h,
2028 plane_req->src_x, plane_req->src_y,
2029 plane_req->src_w, plane_req->src_h);
2030 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002031 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002032 plane->crtc = crtc;
2033 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002034 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002035 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002036 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002037
2038out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002039 if (fb)
2040 drm_framebuffer_unreference(fb);
2041 if (old_fb)
2042 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002043
2044 return ret;
2045}
2046
2047/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002048 * drm_mode_set_config_internal - helper to call ->set_config
2049 * @set: modeset config to set
2050 *
2051 * This is a little helper to wrap internal calls to the ->set_config driver
2052 * interface. The only thing it adds is correct refcounting dance.
2053 */
2054int drm_mode_set_config_internal(struct drm_mode_set *set)
2055{
2056 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002057 struct drm_framebuffer *fb;
2058 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002059 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002060
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002061 /*
2062 * NOTE: ->set_config can also disable other crtcs (if we steal all
2063 * connectors from it), hence we need to refcount the fbs across all
2064 * crtcs. Atomic modeset will have saner semantics ...
2065 */
2066 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2067 tmp->old_fb = tmp->fb;
2068
Daniel Vetterb0d12322012-12-11 01:07:12 +01002069 fb = set->fb;
2070
2071 ret = crtc->funcs->set_config(set);
2072 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02002073 /* crtc->fb must be updated by ->set_config, enforces this. */
2074 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002075 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002076
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002077 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2078 if (tmp->fb)
2079 drm_framebuffer_reference(tmp->fb);
2080 if (tmp->old_fb)
2081 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002082 }
2083
2084 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002085}
2086EXPORT_SYMBOL(drm_mode_set_config_internal);
2087
Damien Lespiauc11e9282013-09-25 16:45:30 +01002088/*
2089 * Checks that the framebuffer is big enough for the CRTC viewport
2090 * (x, y, hdisplay, vdisplay)
2091 */
2092static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2093 int x, int y,
2094 const struct drm_display_mode *mode,
2095 const struct drm_framebuffer *fb)
2096
2097{
2098 int hdisplay, vdisplay;
2099
2100 hdisplay = mode->hdisplay;
2101 vdisplay = mode->vdisplay;
2102
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002103 if (drm_mode_is_stereo(mode)) {
2104 struct drm_display_mode adjusted = *mode;
2105
2106 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2107 hdisplay = adjusted.crtc_hdisplay;
2108 vdisplay = adjusted.crtc_vdisplay;
2109 }
2110
Damien Lespiauc11e9282013-09-25 16:45:30 +01002111 if (crtc->invert_dimensions)
2112 swap(hdisplay, vdisplay);
2113
2114 if (hdisplay > fb->width ||
2115 vdisplay > fb->height ||
2116 x > fb->width - hdisplay ||
2117 y > fb->height - vdisplay) {
2118 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2119 fb->width, fb->height, hdisplay, vdisplay, x, y,
2120 crtc->invert_dimensions ? " (inverted)" : "");
2121 return -ENOSPC;
2122 }
2123
2124 return 0;
2125}
2126
Daniel Vetter2d13b672012-12-11 13:47:23 +01002127/**
Dave Airlief453ba02008-11-07 14:05:41 -08002128 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002129 * @dev: drm device for the ioctl
2130 * @data: data pointer for the ioctl
2131 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002132 *
Dave Airlief453ba02008-11-07 14:05:41 -08002133 * Build a new CRTC configuration based on user request.
2134 *
2135 * Called by the user via ioctl.
2136 *
2137 * RETURNS:
2138 * Zero on success, errno on failure.
2139 */
2140int drm_mode_setcrtc(struct drm_device *dev, void *data,
2141 struct drm_file *file_priv)
2142{
2143 struct drm_mode_config *config = &dev->mode_config;
2144 struct drm_mode_crtc *crtc_req = data;
2145 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002146 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002147 struct drm_connector **connector_set = NULL, *connector;
2148 struct drm_framebuffer *fb = NULL;
2149 struct drm_display_mode *mode = NULL;
2150 struct drm_mode_set set;
2151 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002152 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002153 int i;
2154
Dave Airliefb3b06c2011-02-08 13:55:21 +10002155 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2156 return -EINVAL;
2157
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002158 /* For some reason crtc x/y offsets are signed internally. */
2159 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2160 return -ERANGE;
2161
Daniel Vetter84849902012-12-02 00:28:11 +01002162 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002163 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2164 DRM_MODE_OBJECT_CRTC);
2165 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002166 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002167 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002168 goto out;
2169 }
2170 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002171 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002172
2173 if (crtc_req->mode_valid) {
2174 /* If we have a mode we need a framebuffer. */
2175 /* If we pass -1, set the mode with the currently bound fb */
2176 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002177 if (!crtc->fb) {
2178 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2179 ret = -EINVAL;
2180 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002181 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002182 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002183 /* Make refcounting symmetric with the lookup path. */
2184 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002185 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002186 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2187 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002188 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2189 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002190 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002191 goto out;
2192 }
Dave Airlief453ba02008-11-07 14:05:41 -08002193 }
2194
2195 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002196 if (!mode) {
2197 ret = -ENOMEM;
2198 goto out;
2199 }
2200
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002201 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2202 if (ret) {
2203 DRM_DEBUG_KMS("Invalid mode\n");
2204 goto out;
2205 }
2206
Dave Airlief453ba02008-11-07 14:05:41 -08002207 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002208
Damien Lespiauc11e9282013-09-25 16:45:30 +01002209 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2210 mode, fb);
2211 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002212 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002213
Dave Airlief453ba02008-11-07 14:05:41 -08002214 }
2215
2216 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002217 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002218 ret = -EINVAL;
2219 goto out;
2220 }
2221
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002222 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002223 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002224 crtc_req->count_connectors);
2225 ret = -EINVAL;
2226 goto out;
2227 }
2228
2229 if (crtc_req->count_connectors > 0) {
2230 u32 out_id;
2231
2232 /* Avoid unbounded kernel memory allocation */
2233 if (crtc_req->count_connectors > config->num_connector) {
2234 ret = -EINVAL;
2235 goto out;
2236 }
2237
2238 connector_set = kmalloc(crtc_req->count_connectors *
2239 sizeof(struct drm_connector *),
2240 GFP_KERNEL);
2241 if (!connector_set) {
2242 ret = -ENOMEM;
2243 goto out;
2244 }
2245
2246 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002247 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002248 if (get_user(out_id, &set_connectors_ptr[i])) {
2249 ret = -EFAULT;
2250 goto out;
2251 }
2252
2253 obj = drm_mode_object_find(dev, out_id,
2254 DRM_MODE_OBJECT_CONNECTOR);
2255 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002256 DRM_DEBUG_KMS("Connector id %d unknown\n",
2257 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002258 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002259 goto out;
2260 }
2261 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002262 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2263 connector->base.id,
2264 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002265
2266 connector_set[i] = connector;
2267 }
2268 }
2269
2270 set.crtc = crtc;
2271 set.x = crtc_req->x;
2272 set.y = crtc_req->y;
2273 set.mode = mode;
2274 set.connectors = connector_set;
2275 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002276 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002277 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002278
2279out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002280 if (fb)
2281 drm_framebuffer_unreference(fb);
2282
Dave Airlief453ba02008-11-07 14:05:41 -08002283 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002284 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002285 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002286 return ret;
2287}
2288
Dave Airlie4c813d42013-06-20 11:48:52 +10002289static int drm_mode_cursor_common(struct drm_device *dev,
2290 struct drm_mode_cursor2 *req,
2291 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002292{
Dave Airlief453ba02008-11-07 14:05:41 -08002293 struct drm_mode_object *obj;
2294 struct drm_crtc *crtc;
2295 int ret = 0;
2296
Dave Airliefb3b06c2011-02-08 13:55:21 +10002297 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2298 return -EINVAL;
2299
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002300 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002301 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002302
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002303 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002304 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002305 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002306 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002307 }
2308 crtc = obj_to_crtc(obj);
2309
Daniel Vetterdac35662012-12-02 15:24:10 +01002310 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002311 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002312 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002313 ret = -ENXIO;
2314 goto out;
2315 }
2316 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002317 if (crtc->funcs->cursor_set2)
2318 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2319 req->width, req->height, req->hot_x, req->hot_y);
2320 else
2321 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2322 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002323 }
2324
2325 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2326 if (crtc->funcs->cursor_move) {
2327 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2328 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002329 ret = -EFAULT;
2330 goto out;
2331 }
2332 }
2333out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002334 mutex_unlock(&crtc->mutex);
2335
Dave Airlief453ba02008-11-07 14:05:41 -08002336 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002337
2338}
2339int drm_mode_cursor_ioctl(struct drm_device *dev,
2340 void *data, struct drm_file *file_priv)
2341{
2342 struct drm_mode_cursor *req = data;
2343 struct drm_mode_cursor2 new_req;
2344
2345 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2346 new_req.hot_x = new_req.hot_y = 0;
2347
2348 return drm_mode_cursor_common(dev, &new_req, file_priv);
2349}
2350
2351int drm_mode_cursor2_ioctl(struct drm_device *dev,
2352 void *data, struct drm_file *file_priv)
2353{
2354 struct drm_mode_cursor2 *req = data;
2355 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002356}
2357
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002358/* Original addfb only supported RGB formats, so figure out which one */
2359uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2360{
2361 uint32_t fmt;
2362
2363 switch (bpp) {
2364 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002365 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002366 break;
2367 case 16:
2368 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002369 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002370 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002371 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002372 break;
2373 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002374 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002375 break;
2376 case 32:
2377 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002378 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002379 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002380 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002381 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002382 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002383 break;
2384 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002385 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2386 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002387 break;
2388 }
2389
2390 return fmt;
2391}
2392EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2393
Dave Airlief453ba02008-11-07 14:05:41 -08002394/**
2395 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002396 * @dev: drm device for the ioctl
2397 * @data: data pointer for the ioctl
2398 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002399 *
Dave Airlief453ba02008-11-07 14:05:41 -08002400 * Add a new FB to the specified CRTC, given a user request.
2401 *
2402 * Called by the user via ioctl.
2403 *
2404 * RETURNS:
2405 * Zero on success, errno on failure.
2406 */
2407int drm_mode_addfb(struct drm_device *dev,
2408 void *data, struct drm_file *file_priv)
2409{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002410 struct drm_mode_fb_cmd *or = data;
2411 struct drm_mode_fb_cmd2 r = {};
2412 struct drm_mode_config *config = &dev->mode_config;
2413 struct drm_framebuffer *fb;
2414 int ret = 0;
2415
2416 /* Use new struct with format internally */
2417 r.fb_id = or->fb_id;
2418 r.width = or->width;
2419 r.height = or->height;
2420 r.pitches[0] = or->pitch;
2421 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2422 r.handles[0] = or->handle;
2423
2424 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2425 return -EINVAL;
2426
Jesse Barnesacb4b992011-11-07 12:03:23 -08002427 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002428 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002429
2430 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002431 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002432
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002433 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2434 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002435 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002436 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002437 }
2438
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002439 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002440 or->fb_id = fb->base.id;
2441 list_add(&fb->filp_head, &file_priv->fbs);
2442 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002443 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002444
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002445 return ret;
2446}
2447
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002448static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002449{
2450 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2451
2452 switch (format) {
2453 case DRM_FORMAT_C8:
2454 case DRM_FORMAT_RGB332:
2455 case DRM_FORMAT_BGR233:
2456 case DRM_FORMAT_XRGB4444:
2457 case DRM_FORMAT_XBGR4444:
2458 case DRM_FORMAT_RGBX4444:
2459 case DRM_FORMAT_BGRX4444:
2460 case DRM_FORMAT_ARGB4444:
2461 case DRM_FORMAT_ABGR4444:
2462 case DRM_FORMAT_RGBA4444:
2463 case DRM_FORMAT_BGRA4444:
2464 case DRM_FORMAT_XRGB1555:
2465 case DRM_FORMAT_XBGR1555:
2466 case DRM_FORMAT_RGBX5551:
2467 case DRM_FORMAT_BGRX5551:
2468 case DRM_FORMAT_ARGB1555:
2469 case DRM_FORMAT_ABGR1555:
2470 case DRM_FORMAT_RGBA5551:
2471 case DRM_FORMAT_BGRA5551:
2472 case DRM_FORMAT_RGB565:
2473 case DRM_FORMAT_BGR565:
2474 case DRM_FORMAT_RGB888:
2475 case DRM_FORMAT_BGR888:
2476 case DRM_FORMAT_XRGB8888:
2477 case DRM_FORMAT_XBGR8888:
2478 case DRM_FORMAT_RGBX8888:
2479 case DRM_FORMAT_BGRX8888:
2480 case DRM_FORMAT_ARGB8888:
2481 case DRM_FORMAT_ABGR8888:
2482 case DRM_FORMAT_RGBA8888:
2483 case DRM_FORMAT_BGRA8888:
2484 case DRM_FORMAT_XRGB2101010:
2485 case DRM_FORMAT_XBGR2101010:
2486 case DRM_FORMAT_RGBX1010102:
2487 case DRM_FORMAT_BGRX1010102:
2488 case DRM_FORMAT_ARGB2101010:
2489 case DRM_FORMAT_ABGR2101010:
2490 case DRM_FORMAT_RGBA1010102:
2491 case DRM_FORMAT_BGRA1010102:
2492 case DRM_FORMAT_YUYV:
2493 case DRM_FORMAT_YVYU:
2494 case DRM_FORMAT_UYVY:
2495 case DRM_FORMAT_VYUY:
2496 case DRM_FORMAT_AYUV:
2497 case DRM_FORMAT_NV12:
2498 case DRM_FORMAT_NV21:
2499 case DRM_FORMAT_NV16:
2500 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002501 case DRM_FORMAT_NV24:
2502 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002503 case DRM_FORMAT_YUV410:
2504 case DRM_FORMAT_YVU410:
2505 case DRM_FORMAT_YUV411:
2506 case DRM_FORMAT_YVU411:
2507 case DRM_FORMAT_YUV420:
2508 case DRM_FORMAT_YVU420:
2509 case DRM_FORMAT_YUV422:
2510 case DRM_FORMAT_YVU422:
2511 case DRM_FORMAT_YUV444:
2512 case DRM_FORMAT_YVU444:
2513 return 0;
2514 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002515 DRM_DEBUG_KMS("invalid pixel format %s\n",
2516 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002517 return -EINVAL;
2518 }
2519}
2520
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002521static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002522{
2523 int ret, hsub, vsub, num_planes, i;
2524
2525 ret = format_check(r);
2526 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002527 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2528 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002529 return ret;
2530 }
2531
2532 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2533 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2534 num_planes = drm_format_num_planes(r->pixel_format);
2535
2536 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002537 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002538 return -EINVAL;
2539 }
2540
2541 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002542 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002543 return -EINVAL;
2544 }
2545
2546 for (i = 0; i < num_planes; i++) {
2547 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002548 unsigned int height = r->height / (i != 0 ? vsub : 1);
2549 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002550
2551 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002552 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002553 return -EINVAL;
2554 }
2555
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002556 if ((uint64_t) width * cpp > UINT_MAX)
2557 return -ERANGE;
2558
2559 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2560 return -ERANGE;
2561
2562 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002563 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002564 return -EINVAL;
2565 }
2566 }
2567
2568 return 0;
2569}
2570
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002571/**
2572 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002573 * @dev: drm device for the ioctl
2574 * @data: data pointer for the ioctl
2575 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002576 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002577 * Add a new FB to the specified CRTC, given a user request with format.
2578 *
2579 * Called by the user via ioctl.
2580 *
2581 * RETURNS:
2582 * Zero on success, errno on failure.
2583 */
2584int drm_mode_addfb2(struct drm_device *dev,
2585 void *data, struct drm_file *file_priv)
2586{
2587 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002588 struct drm_mode_config *config = &dev->mode_config;
2589 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002590 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002591
Dave Airliefb3b06c2011-02-08 13:55:21 +10002592 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2593 return -EINVAL;
2594
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002595 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2596 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2597 return -EINVAL;
2598 }
2599
Dave Airlief453ba02008-11-07 14:05:41 -08002600 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002601 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002602 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002603 return -EINVAL;
2604 }
2605 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002606 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002607 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002608 return -EINVAL;
2609 }
2610
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002611 ret = framebuffer_check(r);
2612 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002613 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002614
Dave Airlief453ba02008-11-07 14:05:41 -08002615 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002616 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002617 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002618 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002619 }
2620
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002621 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002622 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002623 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002624 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002625 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002626
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002627
Dave Airlief453ba02008-11-07 14:05:41 -08002628 return ret;
2629}
2630
2631/**
2632 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002633 * @dev: drm device for the ioctl
2634 * @data: data pointer for the ioctl
2635 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002636 *
Dave Airlief453ba02008-11-07 14:05:41 -08002637 * Remove the FB specified by the user.
2638 *
2639 * Called by the user via ioctl.
2640 *
2641 * RETURNS:
2642 * Zero on success, errno on failure.
2643 */
2644int drm_mode_rmfb(struct drm_device *dev,
2645 void *data, struct drm_file *file_priv)
2646{
Dave Airlief453ba02008-11-07 14:05:41 -08002647 struct drm_framebuffer *fb = NULL;
2648 struct drm_framebuffer *fbl = NULL;
2649 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002650 int found = 0;
2651
Dave Airliefb3b06c2011-02-08 13:55:21 +10002652 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2653 return -EINVAL;
2654
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002655 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002656 mutex_lock(&dev->mode_config.fb_lock);
2657 fb = __drm_framebuffer_lookup(dev, *id);
2658 if (!fb)
2659 goto fail_lookup;
2660
Dave Airlief453ba02008-11-07 14:05:41 -08002661 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2662 if (fb == fbl)
2663 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002664 if (!found)
2665 goto fail_lookup;
2666
2667 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2668 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002669
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002670 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002671 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002672 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002673
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002674 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002675
Daniel Vetter2b677e82012-12-10 21:16:05 +01002676 return 0;
2677
2678fail_lookup:
2679 mutex_unlock(&dev->mode_config.fb_lock);
2680 mutex_unlock(&file_priv->fbs_lock);
2681
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002682 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002683}
2684
2685/**
2686 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002687 * @dev: drm device for the ioctl
2688 * @data: data pointer for the ioctl
2689 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002690 *
Dave Airlief453ba02008-11-07 14:05:41 -08002691 * Lookup the FB given its ID and return info about it.
2692 *
2693 * Called by the user via ioctl.
2694 *
2695 * RETURNS:
2696 * Zero on success, errno on failure.
2697 */
2698int drm_mode_getfb(struct drm_device *dev,
2699 void *data, struct drm_file *file_priv)
2700{
2701 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002702 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002703 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002704
Dave Airliefb3b06c2011-02-08 13:55:21 +10002705 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2706 return -EINVAL;
2707
Daniel Vetter786b99e2012-12-02 21:53:40 +01002708 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002709 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002710 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002711
2712 r->height = fb->height;
2713 r->width = fb->width;
2714 r->depth = fb->depth;
2715 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002716 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002717 if (fb->funcs->create_handle) {
2718 if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
2719 ret = fb->funcs->create_handle(fb, file_priv,
2720 &r->handle);
2721 } else {
2722 /* GET_FB() is an unprivileged ioctl so we must not
2723 * return a buffer-handle to non-master processes! For
2724 * backwards-compatibility reasons, we cannot make
2725 * GET_FB() privileged, so just return an invalid handle
2726 * for non-masters. */
2727 r->handle = 0;
2728 ret = 0;
2729 }
2730 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002731 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02002732 }
Dave Airlief453ba02008-11-07 14:05:41 -08002733
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002734 drm_framebuffer_unreference(fb);
2735
Dave Airlief453ba02008-11-07 14:05:41 -08002736 return ret;
2737}
2738
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002739int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2740 void *data, struct drm_file *file_priv)
2741{
2742 struct drm_clip_rect __user *clips_ptr;
2743 struct drm_clip_rect *clips = NULL;
2744 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002745 struct drm_framebuffer *fb;
2746 unsigned flags;
2747 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002748 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002749
Dave Airliefb3b06c2011-02-08 13:55:21 +10002750 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2751 return -EINVAL;
2752
Daniel Vetter786b99e2012-12-02 21:53:40 +01002753 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002754 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002755 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002756
2757 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002758 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002759
2760 if (!num_clips != !clips_ptr) {
2761 ret = -EINVAL;
2762 goto out_err1;
2763 }
2764
2765 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2766
2767 /* If userspace annotates copy, clips must come in pairs */
2768 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2769 ret = -EINVAL;
2770 goto out_err1;
2771 }
2772
2773 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002774 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2775 ret = -EINVAL;
2776 goto out_err1;
2777 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002778 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2779 if (!clips) {
2780 ret = -ENOMEM;
2781 goto out_err1;
2782 }
2783
2784 ret = copy_from_user(clips, clips_ptr,
2785 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002786 if (ret) {
2787 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002788 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002789 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002790 }
2791
2792 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002793 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2794 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002795 } else {
2796 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002797 }
2798
2799out_err2:
2800 kfree(clips);
2801out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002802 drm_framebuffer_unreference(fb);
2803
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002804 return ret;
2805}
2806
2807
Dave Airlief453ba02008-11-07 14:05:41 -08002808/**
2809 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002810 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002811 *
Dave Airlief453ba02008-11-07 14:05:41 -08002812 * Destroy all the FBs associated with @filp.
2813 *
2814 * Called by the user via ioctl.
2815 *
2816 * RETURNS:
2817 * Zero on success, errno on failure.
2818 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002819void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002820{
Dave Airlief453ba02008-11-07 14:05:41 -08002821 struct drm_device *dev = priv->minor->dev;
2822 struct drm_framebuffer *fb, *tfb;
2823
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002824 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002825 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002826
2827 mutex_lock(&dev->mode_config.fb_lock);
2828 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2829 __drm_framebuffer_unregister(dev, fb);
2830 mutex_unlock(&dev->mode_config.fb_lock);
2831
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002832 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002833
2834 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002835 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002836 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002837 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002838}
2839
Dave Airlief453ba02008-11-07 14:05:41 -08002840struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2841 const char *name, int num_values)
2842{
2843 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002844 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002845
2846 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2847 if (!property)
2848 return NULL;
2849
2850 if (num_values) {
2851 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2852 if (!property->values)
2853 goto fail;
2854 }
2855
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002856 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2857 if (ret)
2858 goto fail;
2859
Dave Airlief453ba02008-11-07 14:05:41 -08002860 property->flags = flags;
2861 property->num_values = num_values;
2862 INIT_LIST_HEAD(&property->enum_blob_list);
2863
Vinson Lee471dd2e2011-11-10 11:55:40 -08002864 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002865 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002866 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2867 }
Dave Airlief453ba02008-11-07 14:05:41 -08002868
2869 list_add_tail(&property->head, &dev->mode_config.property_list);
2870 return property;
2871fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002872 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002873 kfree(property);
2874 return NULL;
2875}
2876EXPORT_SYMBOL(drm_property_create);
2877
Sascha Hauer4a67d392012-02-06 10:58:17 +01002878struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2879 const char *name,
2880 const struct drm_prop_enum_list *props,
2881 int num_values)
2882{
2883 struct drm_property *property;
2884 int i, ret;
2885
2886 flags |= DRM_MODE_PROP_ENUM;
2887
2888 property = drm_property_create(dev, flags, name, num_values);
2889 if (!property)
2890 return NULL;
2891
2892 for (i = 0; i < num_values; i++) {
2893 ret = drm_property_add_enum(property, i,
2894 props[i].type,
2895 props[i].name);
2896 if (ret) {
2897 drm_property_destroy(dev, property);
2898 return NULL;
2899 }
2900 }
2901
2902 return property;
2903}
2904EXPORT_SYMBOL(drm_property_create_enum);
2905
Rob Clark49e27542012-05-17 02:23:26 -06002906struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2907 int flags, const char *name,
2908 const struct drm_prop_enum_list *props,
2909 int num_values)
2910{
2911 struct drm_property *property;
2912 int i, ret;
2913
2914 flags |= DRM_MODE_PROP_BITMASK;
2915
2916 property = drm_property_create(dev, flags, name, num_values);
2917 if (!property)
2918 return NULL;
2919
2920 for (i = 0; i < num_values; i++) {
2921 ret = drm_property_add_enum(property, i,
2922 props[i].type,
2923 props[i].name);
2924 if (ret) {
2925 drm_property_destroy(dev, property);
2926 return NULL;
2927 }
2928 }
2929
2930 return property;
2931}
2932EXPORT_SYMBOL(drm_property_create_bitmask);
2933
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002934struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2935 const char *name,
2936 uint64_t min, uint64_t max)
2937{
2938 struct drm_property *property;
2939
2940 flags |= DRM_MODE_PROP_RANGE;
2941
2942 property = drm_property_create(dev, flags, name, 2);
2943 if (!property)
2944 return NULL;
2945
2946 property->values[0] = min;
2947 property->values[1] = max;
2948
2949 return property;
2950}
2951EXPORT_SYMBOL(drm_property_create_range);
2952
Dave Airlief453ba02008-11-07 14:05:41 -08002953int drm_property_add_enum(struct drm_property *property, int index,
2954 uint64_t value, const char *name)
2955{
2956 struct drm_property_enum *prop_enum;
2957
Rob Clark49e27542012-05-17 02:23:26 -06002958 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2959 return -EINVAL;
2960
2961 /*
2962 * Bitmask enum properties have the additional constraint of values
2963 * from 0 to 63
2964 */
2965 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002966 return -EINVAL;
2967
2968 if (!list_empty(&property->enum_blob_list)) {
2969 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2970 if (prop_enum->value == value) {
2971 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2972 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2973 return 0;
2974 }
2975 }
2976 }
2977
2978 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2979 if (!prop_enum)
2980 return -ENOMEM;
2981
2982 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2983 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2984 prop_enum->value = value;
2985
2986 property->values[index] = value;
2987 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2988 return 0;
2989}
2990EXPORT_SYMBOL(drm_property_add_enum);
2991
2992void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2993{
2994 struct drm_property_enum *prop_enum, *pt;
2995
2996 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2997 list_del(&prop_enum->head);
2998 kfree(prop_enum);
2999 }
3000
3001 if (property->num_values)
3002 kfree(property->values);
3003 drm_mode_object_put(dev, &property->base);
3004 list_del(&property->head);
3005 kfree(property);
3006}
3007EXPORT_SYMBOL(drm_property_destroy);
3008
Paulo Zanonic5431882012-05-15 18:09:02 -03003009void drm_object_attach_property(struct drm_mode_object *obj,
3010 struct drm_property *property,
3011 uint64_t init_val)
3012{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003013 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003014
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003015 if (count == DRM_OBJECT_MAX_PROPERTY) {
3016 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3017 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3018 "you see this message on the same object type.\n",
3019 obj->type);
3020 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003021 }
3022
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003023 obj->properties->ids[count] = property->base.id;
3024 obj->properties->values[count] = init_val;
3025 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003026}
3027EXPORT_SYMBOL(drm_object_attach_property);
3028
3029int drm_object_property_set_value(struct drm_mode_object *obj,
3030 struct drm_property *property, uint64_t val)
3031{
3032 int i;
3033
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003034 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003035 if (obj->properties->ids[i] == property->base.id) {
3036 obj->properties->values[i] = val;
3037 return 0;
3038 }
3039 }
3040
3041 return -EINVAL;
3042}
3043EXPORT_SYMBOL(drm_object_property_set_value);
3044
3045int drm_object_property_get_value(struct drm_mode_object *obj,
3046 struct drm_property *property, uint64_t *val)
3047{
3048 int i;
3049
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003050 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003051 if (obj->properties->ids[i] == property->base.id) {
3052 *val = obj->properties->values[i];
3053 return 0;
3054 }
3055 }
3056
3057 return -EINVAL;
3058}
3059EXPORT_SYMBOL(drm_object_property_get_value);
3060
Dave Airlief453ba02008-11-07 14:05:41 -08003061int drm_mode_getproperty_ioctl(struct drm_device *dev,
3062 void *data, struct drm_file *file_priv)
3063{
3064 struct drm_mode_object *obj;
3065 struct drm_mode_get_property *out_resp = data;
3066 struct drm_property *property;
3067 int enum_count = 0;
3068 int blob_count = 0;
3069 int value_count = 0;
3070 int ret = 0, i;
3071 int copied;
3072 struct drm_property_enum *prop_enum;
3073 struct drm_mode_property_enum __user *enum_ptr;
3074 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003075 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003076 uint64_t __user *values_ptr;
3077 uint32_t __user *blob_length_ptr;
3078
Dave Airliefb3b06c2011-02-08 13:55:21 +10003079 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3080 return -EINVAL;
3081
Daniel Vetter84849902012-12-02 00:28:11 +01003082 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003083 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3084 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003085 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003086 goto done;
3087 }
3088 property = obj_to_property(obj);
3089
Rob Clark49e27542012-05-17 02:23:26 -06003090 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003091 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3092 enum_count++;
3093 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3094 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3095 blob_count++;
3096 }
3097
3098 value_count = property->num_values;
3099
3100 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3101 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3102 out_resp->flags = property->flags;
3103
3104 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003105 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003106 for (i = 0; i < value_count; i++) {
3107 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3108 ret = -EFAULT;
3109 goto done;
3110 }
3111 }
3112 }
3113 out_resp->count_values = value_count;
3114
Rob Clark49e27542012-05-17 02:23:26 -06003115 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003116 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3117 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003118 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003119 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3120
3121 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3122 ret = -EFAULT;
3123 goto done;
3124 }
3125
3126 if (copy_to_user(&enum_ptr[copied].name,
3127 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3128 ret = -EFAULT;
3129 goto done;
3130 }
3131 copied++;
3132 }
3133 }
3134 out_resp->count_enum_blobs = enum_count;
3135 }
3136
3137 if (property->flags & DRM_MODE_PROP_BLOB) {
3138 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3139 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003140 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3141 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003142
3143 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3144 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3145 ret = -EFAULT;
3146 goto done;
3147 }
3148
3149 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3150 ret = -EFAULT;
3151 goto done;
3152 }
3153
3154 copied++;
3155 }
3156 }
3157 out_resp->count_enum_blobs = blob_count;
3158 }
3159done:
Daniel Vetter84849902012-12-02 00:28:11 +01003160 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003161 return ret;
3162}
3163
3164static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3165 void *data)
3166{
3167 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003168 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003169
3170 if (!length || !data)
3171 return NULL;
3172
3173 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3174 if (!blob)
3175 return NULL;
3176
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003177 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3178 if (ret) {
3179 kfree(blob);
3180 return NULL;
3181 }
3182
Dave Airlief453ba02008-11-07 14:05:41 -08003183 blob->length = length;
3184
3185 memcpy(blob->data, data, length);
3186
Dave Airlief453ba02008-11-07 14:05:41 -08003187 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3188 return blob;
3189}
3190
3191static void drm_property_destroy_blob(struct drm_device *dev,
3192 struct drm_property_blob *blob)
3193{
3194 drm_mode_object_put(dev, &blob->base);
3195 list_del(&blob->head);
3196 kfree(blob);
3197}
3198
3199int drm_mode_getblob_ioctl(struct drm_device *dev,
3200 void *data, struct drm_file *file_priv)
3201{
3202 struct drm_mode_object *obj;
3203 struct drm_mode_get_blob *out_resp = data;
3204 struct drm_property_blob *blob;
3205 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003206 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003207
Dave Airliefb3b06c2011-02-08 13:55:21 +10003208 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3209 return -EINVAL;
3210
Daniel Vetter84849902012-12-02 00:28:11 +01003211 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003212 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3213 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003214 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003215 goto done;
3216 }
3217 blob = obj_to_blob(obj);
3218
3219 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003220 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003221 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3222 ret = -EFAULT;
3223 goto done;
3224 }
3225 }
3226 out_resp->length = blob->length;
3227
3228done:
Daniel Vetter84849902012-12-02 00:28:11 +01003229 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003230 return ret;
3231}
3232
3233int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3234 struct edid *edid)
3235{
3236 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003237 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003238
3239 if (connector->edid_blob_ptr)
3240 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3241
3242 /* Delete edid, when there is none. */
3243 if (!edid) {
3244 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003245 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003246 return ret;
3247 }
3248
Adam Jackson7466f4c2010-03-29 21:43:23 +00003249 size = EDID_LENGTH * (1 + edid->extensions);
3250 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3251 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003252 if (!connector->edid_blob_ptr)
3253 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003254
Rob Clark58495562012-10-11 20:50:56 -05003255 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003256 dev->mode_config.edid_property,
3257 connector->edid_blob_ptr->base.id);
3258
3259 return ret;
3260}
3261EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3262
Paulo Zanoni26a34812012-05-15 18:08:59 -03003263static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003264 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003265{
3266 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3267 return false;
3268 if (property->flags & DRM_MODE_PROP_RANGE) {
3269 if (value < property->values[0] || value > property->values[1])
3270 return false;
3271 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003272 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3273 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003274 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003275 for (i = 0; i < property->num_values; i++)
3276 valid_mask |= (1ULL << property->values[i]);
3277 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003278 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3279 /* Only the driver knows */
3280 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003281 } else {
3282 int i;
3283 for (i = 0; i < property->num_values; i++)
3284 if (property->values[i] == value)
3285 return true;
3286 return false;
3287 }
3288}
3289
Dave Airlief453ba02008-11-07 14:05:41 -08003290int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3291 void *data, struct drm_file *file_priv)
3292{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003293 struct drm_mode_connector_set_property *conn_set_prop = data;
3294 struct drm_mode_obj_set_property obj_set_prop = {
3295 .value = conn_set_prop->value,
3296 .prop_id = conn_set_prop->prop_id,
3297 .obj_id = conn_set_prop->connector_id,
3298 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3299 };
Dave Airlief453ba02008-11-07 14:05:41 -08003300
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003301 /* It does all the locking and checking we need */
3302 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003303}
3304
Paulo Zanonic5431882012-05-15 18:09:02 -03003305static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3306 struct drm_property *property,
3307 uint64_t value)
3308{
3309 int ret = -EINVAL;
3310 struct drm_connector *connector = obj_to_connector(obj);
3311
3312 /* Do DPMS ourselves */
3313 if (property == connector->dev->mode_config.dpms_property) {
3314 if (connector->funcs->dpms)
3315 (*connector->funcs->dpms)(connector, (int)value);
3316 ret = 0;
3317 } else if (connector->funcs->set_property)
3318 ret = connector->funcs->set_property(connector, property, value);
3319
3320 /* store the property value if successful */
3321 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003322 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003323 return ret;
3324}
3325
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003326static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3327 struct drm_property *property,
3328 uint64_t value)
3329{
3330 int ret = -EINVAL;
3331 struct drm_crtc *crtc = obj_to_crtc(obj);
3332
3333 if (crtc->funcs->set_property)
3334 ret = crtc->funcs->set_property(crtc, property, value);
3335 if (!ret)
3336 drm_object_property_set_value(obj, property, value);
3337
3338 return ret;
3339}
3340
Rob Clark4d939142012-05-17 02:23:27 -06003341static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3342 struct drm_property *property,
3343 uint64_t value)
3344{
3345 int ret = -EINVAL;
3346 struct drm_plane *plane = obj_to_plane(obj);
3347
3348 if (plane->funcs->set_property)
3349 ret = plane->funcs->set_property(plane, property, value);
3350 if (!ret)
3351 drm_object_property_set_value(obj, property, value);
3352
3353 return ret;
3354}
3355
Paulo Zanonic5431882012-05-15 18:09:02 -03003356int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3357 struct drm_file *file_priv)
3358{
3359 struct drm_mode_obj_get_properties *arg = data;
3360 struct drm_mode_object *obj;
3361 int ret = 0;
3362 int i;
3363 int copied = 0;
3364 int props_count = 0;
3365 uint32_t __user *props_ptr;
3366 uint64_t __user *prop_values_ptr;
3367
3368 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3369 return -EINVAL;
3370
Daniel Vetter84849902012-12-02 00:28:11 +01003371 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003372
3373 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3374 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003375 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003376 goto out;
3377 }
3378 if (!obj->properties) {
3379 ret = -EINVAL;
3380 goto out;
3381 }
3382
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003383 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003384
3385 /* This ioctl is called twice, once to determine how much space is
3386 * needed, and the 2nd time to fill it. */
3387 if ((arg->count_props >= props_count) && props_count) {
3388 copied = 0;
3389 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3390 prop_values_ptr = (uint64_t __user *)(unsigned long)
3391 (arg->prop_values_ptr);
3392 for (i = 0; i < props_count; i++) {
3393 if (put_user(obj->properties->ids[i],
3394 props_ptr + copied)) {
3395 ret = -EFAULT;
3396 goto out;
3397 }
3398 if (put_user(obj->properties->values[i],
3399 prop_values_ptr + copied)) {
3400 ret = -EFAULT;
3401 goto out;
3402 }
3403 copied++;
3404 }
3405 }
3406 arg->count_props = props_count;
3407out:
Daniel Vetter84849902012-12-02 00:28:11 +01003408 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003409 return ret;
3410}
3411
3412int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3413 struct drm_file *file_priv)
3414{
3415 struct drm_mode_obj_set_property *arg = data;
3416 struct drm_mode_object *arg_obj;
3417 struct drm_mode_object *prop_obj;
3418 struct drm_property *property;
3419 int ret = -EINVAL;
3420 int i;
3421
3422 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3423 return -EINVAL;
3424
Daniel Vetter84849902012-12-02 00:28:11 +01003425 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003426
3427 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003428 if (!arg_obj) {
3429 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003430 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003431 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003432 if (!arg_obj->properties)
3433 goto out;
3434
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003435 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003436 if (arg_obj->properties->ids[i] == arg->prop_id)
3437 break;
3438
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003439 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003440 goto out;
3441
3442 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3443 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003444 if (!prop_obj) {
3445 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003446 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003447 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003448 property = obj_to_property(prop_obj);
3449
3450 if (!drm_property_change_is_valid(property, arg->value))
3451 goto out;
3452
3453 switch (arg_obj->type) {
3454 case DRM_MODE_OBJECT_CONNECTOR:
3455 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3456 arg->value);
3457 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003458 case DRM_MODE_OBJECT_CRTC:
3459 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3460 break;
Rob Clark4d939142012-05-17 02:23:27 -06003461 case DRM_MODE_OBJECT_PLANE:
3462 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3463 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003464 }
3465
3466out:
Daniel Vetter84849902012-12-02 00:28:11 +01003467 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003468 return ret;
3469}
3470
Dave Airlief453ba02008-11-07 14:05:41 -08003471int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3472 struct drm_encoder *encoder)
3473{
3474 int i;
3475
3476 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3477 if (connector->encoder_ids[i] == 0) {
3478 connector->encoder_ids[i] = encoder->base.id;
3479 return 0;
3480 }
3481 }
3482 return -ENOMEM;
3483}
3484EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3485
3486void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3487 struct drm_encoder *encoder)
3488{
3489 int i;
3490 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3491 if (connector->encoder_ids[i] == encoder->base.id) {
3492 connector->encoder_ids[i] = 0;
3493 if (connector->encoder == encoder)
3494 connector->encoder = NULL;
3495 break;
3496 }
3497 }
3498}
3499EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3500
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003501int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003502 int gamma_size)
3503{
3504 crtc->gamma_size = gamma_size;
3505
3506 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3507 if (!crtc->gamma_store) {
3508 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003509 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003510 }
3511
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003512 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003513}
3514EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3515
3516int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3517 void *data, struct drm_file *file_priv)
3518{
3519 struct drm_mode_crtc_lut *crtc_lut = data;
3520 struct drm_mode_object *obj;
3521 struct drm_crtc *crtc;
3522 void *r_base, *g_base, *b_base;
3523 int size;
3524 int ret = 0;
3525
Dave Airliefb3b06c2011-02-08 13:55:21 +10003526 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3527 return -EINVAL;
3528
Daniel Vetter84849902012-12-02 00:28:11 +01003529 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003530 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3531 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003532 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003533 goto out;
3534 }
3535 crtc = obj_to_crtc(obj);
3536
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003537 if (crtc->funcs->gamma_set == NULL) {
3538 ret = -ENOSYS;
3539 goto out;
3540 }
3541
Dave Airlief453ba02008-11-07 14:05:41 -08003542 /* memcpy into gamma store */
3543 if (crtc_lut->gamma_size != crtc->gamma_size) {
3544 ret = -EINVAL;
3545 goto out;
3546 }
3547
3548 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3549 r_base = crtc->gamma_store;
3550 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3551 ret = -EFAULT;
3552 goto out;
3553 }
3554
3555 g_base = r_base + size;
3556 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3557 ret = -EFAULT;
3558 goto out;
3559 }
3560
3561 b_base = g_base + size;
3562 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3563 ret = -EFAULT;
3564 goto out;
3565 }
3566
James Simmons72034252010-08-03 01:33:19 +01003567 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003568
3569out:
Daniel Vetter84849902012-12-02 00:28:11 +01003570 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003571 return ret;
3572
3573}
3574
3575int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3576 void *data, struct drm_file *file_priv)
3577{
3578 struct drm_mode_crtc_lut *crtc_lut = data;
3579 struct drm_mode_object *obj;
3580 struct drm_crtc *crtc;
3581 void *r_base, *g_base, *b_base;
3582 int size;
3583 int ret = 0;
3584
Dave Airliefb3b06c2011-02-08 13:55:21 +10003585 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3586 return -EINVAL;
3587
Daniel Vetter84849902012-12-02 00:28:11 +01003588 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003589 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3590 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003591 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003592 goto out;
3593 }
3594 crtc = obj_to_crtc(obj);
3595
3596 /* memcpy into gamma store */
3597 if (crtc_lut->gamma_size != crtc->gamma_size) {
3598 ret = -EINVAL;
3599 goto out;
3600 }
3601
3602 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3603 r_base = crtc->gamma_store;
3604 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3605 ret = -EFAULT;
3606 goto out;
3607 }
3608
3609 g_base = r_base + size;
3610 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3611 ret = -EFAULT;
3612 goto out;
3613 }
3614
3615 b_base = g_base + size;
3616 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3617 ret = -EFAULT;
3618 goto out;
3619 }
3620out:
Daniel Vetter84849902012-12-02 00:28:11 +01003621 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003622 return ret;
3623}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003624
3625int drm_mode_page_flip_ioctl(struct drm_device *dev,
3626 void *data, struct drm_file *file_priv)
3627{
3628 struct drm_mode_crtc_page_flip *page_flip = data;
3629 struct drm_mode_object *obj;
3630 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003631 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003632 struct drm_pending_vblank_event *e = NULL;
3633 unsigned long flags;
3634 int ret = -EINVAL;
3635
3636 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3637 page_flip->reserved != 0)
3638 return -EINVAL;
3639
Keith Packard62f21042013-07-22 18:50:00 -07003640 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3641 return -EINVAL;
3642
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003643 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3644 if (!obj)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003645 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003646 crtc = obj_to_crtc(obj);
3647
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003648 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003649 if (crtc->fb == NULL) {
3650 /* The framebuffer is currently unbound, presumably
3651 * due to a hotplug event, that userspace has not
3652 * yet discovered.
3653 */
3654 ret = -EBUSY;
3655 goto out;
3656 }
3657
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003658 if (crtc->funcs->page_flip == NULL)
3659 goto out;
3660
Daniel Vetter786b99e2012-12-02 21:53:40 +01003661 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003662 if (!fb) {
3663 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003664 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003665 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003666
Damien Lespiauc11e9282013-09-25 16:45:30 +01003667 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3668 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003669 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003670
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003671 if (crtc->fb->pixel_format != fb->pixel_format) {
3672 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3673 ret = -EINVAL;
3674 goto out;
3675 }
3676
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003677 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3678 ret = -ENOMEM;
3679 spin_lock_irqsave(&dev->event_lock, flags);
3680 if (file_priv->event_space < sizeof e->event) {
3681 spin_unlock_irqrestore(&dev->event_lock, flags);
3682 goto out;
3683 }
3684 file_priv->event_space -= sizeof e->event;
3685 spin_unlock_irqrestore(&dev->event_lock, flags);
3686
3687 e = kzalloc(sizeof *e, GFP_KERNEL);
3688 if (e == NULL) {
3689 spin_lock_irqsave(&dev->event_lock, flags);
3690 file_priv->event_space += sizeof e->event;
3691 spin_unlock_irqrestore(&dev->event_lock, flags);
3692 goto out;
3693 }
3694
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003695 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003696 e->event.base.length = sizeof e->event;
3697 e->event.user_data = page_flip->user_data;
3698 e->base.event = &e->event.base;
3699 e->base.file_priv = file_priv;
3700 e->base.destroy =
3701 (void (*) (struct drm_pending_event *)) kfree;
3702 }
3703
Daniel Vetterb0d12322012-12-11 01:07:12 +01003704 old_fb = crtc->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07003705 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003706 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003707 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3708 spin_lock_irqsave(&dev->event_lock, flags);
3709 file_priv->event_space += sizeof e->event;
3710 spin_unlock_irqrestore(&dev->event_lock, flags);
3711 kfree(e);
3712 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003713 /* Keep the old fb, don't unref it. */
3714 old_fb = NULL;
3715 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003716 /*
3717 * Warn if the driver hasn't properly updated the crtc->fb
3718 * field to reflect that the new framebuffer is now used.
3719 * Failing to do so will screw with the reference counting
3720 * on framebuffers.
3721 */
3722 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003723 /* Unref only the old framebuffer. */
3724 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003725 }
3726
3727out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003728 if (fb)
3729 drm_framebuffer_unreference(fb);
3730 if (old_fb)
3731 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003732 mutex_unlock(&crtc->mutex);
3733
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003734 return ret;
3735}
Chris Wilsoneb033552011-01-24 15:11:08 +00003736
3737void drm_mode_config_reset(struct drm_device *dev)
3738{
3739 struct drm_crtc *crtc;
3740 struct drm_encoder *encoder;
3741 struct drm_connector *connector;
3742
3743 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3744 if (crtc->funcs->reset)
3745 crtc->funcs->reset(crtc);
3746
3747 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3748 if (encoder->funcs->reset)
3749 encoder->funcs->reset(encoder);
3750
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003751 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3752 connector->status = connector_status_unknown;
3753
Chris Wilsoneb033552011-01-24 15:11:08 +00003754 if (connector->funcs->reset)
3755 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003756 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003757}
3758EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003759
3760int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3761 void *data, struct drm_file *file_priv)
3762{
3763 struct drm_mode_create_dumb *args = data;
3764
3765 if (!dev->driver->dumb_create)
3766 return -ENOSYS;
3767 return dev->driver->dumb_create(file_priv, dev, args);
3768}
3769
3770int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3771 void *data, struct drm_file *file_priv)
3772{
3773 struct drm_mode_map_dumb *args = data;
3774
3775 /* call driver ioctl to get mmap offset */
3776 if (!dev->driver->dumb_map_offset)
3777 return -ENOSYS;
3778
3779 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3780}
3781
3782int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3783 void *data, struct drm_file *file_priv)
3784{
3785 struct drm_mode_destroy_dumb *args = data;
3786
3787 if (!dev->driver->dumb_destroy)
3788 return -ENOSYS;
3789
3790 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3791}
Dave Airlie248dbc22011-11-29 20:02:54 +00003792
3793/*
3794 * Just need to support RGB formats here for compat with code that doesn't
3795 * use pixel formats directly yet.
3796 */
3797void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3798 int *bpp)
3799{
3800 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003801 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003802 case DRM_FORMAT_RGB332:
3803 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003804 *depth = 8;
3805 *bpp = 8;
3806 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003807 case DRM_FORMAT_XRGB1555:
3808 case DRM_FORMAT_XBGR1555:
3809 case DRM_FORMAT_RGBX5551:
3810 case DRM_FORMAT_BGRX5551:
3811 case DRM_FORMAT_ARGB1555:
3812 case DRM_FORMAT_ABGR1555:
3813 case DRM_FORMAT_RGBA5551:
3814 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003815 *depth = 15;
3816 *bpp = 16;
3817 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003818 case DRM_FORMAT_RGB565:
3819 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003820 *depth = 16;
3821 *bpp = 16;
3822 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003823 case DRM_FORMAT_RGB888:
3824 case DRM_FORMAT_BGR888:
3825 *depth = 24;
3826 *bpp = 24;
3827 break;
3828 case DRM_FORMAT_XRGB8888:
3829 case DRM_FORMAT_XBGR8888:
3830 case DRM_FORMAT_RGBX8888:
3831 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003832 *depth = 24;
3833 *bpp = 32;
3834 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003835 case DRM_FORMAT_XRGB2101010:
3836 case DRM_FORMAT_XBGR2101010:
3837 case DRM_FORMAT_RGBX1010102:
3838 case DRM_FORMAT_BGRX1010102:
3839 case DRM_FORMAT_ARGB2101010:
3840 case DRM_FORMAT_ABGR2101010:
3841 case DRM_FORMAT_RGBA1010102:
3842 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003843 *depth = 30;
3844 *bpp = 32;
3845 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003846 case DRM_FORMAT_ARGB8888:
3847 case DRM_FORMAT_ABGR8888:
3848 case DRM_FORMAT_RGBA8888:
3849 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003850 *depth = 32;
3851 *bpp = 32;
3852 break;
3853 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003854 DRM_DEBUG_KMS("unsupported pixel format %s\n",
3855 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00003856 *depth = 0;
3857 *bpp = 0;
3858 break;
3859 }
3860}
3861EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003862
3863/**
3864 * drm_format_num_planes - get the number of planes for format
3865 * @format: pixel format (DRM_FORMAT_*)
3866 *
3867 * RETURNS:
3868 * The number of planes used by the specified pixel format.
3869 */
3870int drm_format_num_planes(uint32_t format)
3871{
3872 switch (format) {
3873 case DRM_FORMAT_YUV410:
3874 case DRM_FORMAT_YVU410:
3875 case DRM_FORMAT_YUV411:
3876 case DRM_FORMAT_YVU411:
3877 case DRM_FORMAT_YUV420:
3878 case DRM_FORMAT_YVU420:
3879 case DRM_FORMAT_YUV422:
3880 case DRM_FORMAT_YVU422:
3881 case DRM_FORMAT_YUV444:
3882 case DRM_FORMAT_YVU444:
3883 return 3;
3884 case DRM_FORMAT_NV12:
3885 case DRM_FORMAT_NV21:
3886 case DRM_FORMAT_NV16:
3887 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003888 case DRM_FORMAT_NV24:
3889 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003890 return 2;
3891 default:
3892 return 1;
3893 }
3894}
3895EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003896
3897/**
3898 * drm_format_plane_cpp - determine the bytes per pixel value
3899 * @format: pixel format (DRM_FORMAT_*)
3900 * @plane: plane index
3901 *
3902 * RETURNS:
3903 * The bytes per pixel value for the specified plane.
3904 */
3905int drm_format_plane_cpp(uint32_t format, int plane)
3906{
3907 unsigned int depth;
3908 int bpp;
3909
3910 if (plane >= drm_format_num_planes(format))
3911 return 0;
3912
3913 switch (format) {
3914 case DRM_FORMAT_YUYV:
3915 case DRM_FORMAT_YVYU:
3916 case DRM_FORMAT_UYVY:
3917 case DRM_FORMAT_VYUY:
3918 return 2;
3919 case DRM_FORMAT_NV12:
3920 case DRM_FORMAT_NV21:
3921 case DRM_FORMAT_NV16:
3922 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003923 case DRM_FORMAT_NV24:
3924 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003925 return plane ? 2 : 1;
3926 case DRM_FORMAT_YUV410:
3927 case DRM_FORMAT_YVU410:
3928 case DRM_FORMAT_YUV411:
3929 case DRM_FORMAT_YVU411:
3930 case DRM_FORMAT_YUV420:
3931 case DRM_FORMAT_YVU420:
3932 case DRM_FORMAT_YUV422:
3933 case DRM_FORMAT_YVU422:
3934 case DRM_FORMAT_YUV444:
3935 case DRM_FORMAT_YVU444:
3936 return 1;
3937 default:
3938 drm_fb_get_bpp_depth(format, &depth, &bpp);
3939 return bpp >> 3;
3940 }
3941}
3942EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003943
3944/**
3945 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3946 * @format: pixel format (DRM_FORMAT_*)
3947 *
3948 * RETURNS:
3949 * The horizontal chroma subsampling factor for the
3950 * specified pixel format.
3951 */
3952int drm_format_horz_chroma_subsampling(uint32_t format)
3953{
3954 switch (format) {
3955 case DRM_FORMAT_YUV411:
3956 case DRM_FORMAT_YVU411:
3957 case DRM_FORMAT_YUV410:
3958 case DRM_FORMAT_YVU410:
3959 return 4;
3960 case DRM_FORMAT_YUYV:
3961 case DRM_FORMAT_YVYU:
3962 case DRM_FORMAT_UYVY:
3963 case DRM_FORMAT_VYUY:
3964 case DRM_FORMAT_NV12:
3965 case DRM_FORMAT_NV21:
3966 case DRM_FORMAT_NV16:
3967 case DRM_FORMAT_NV61:
3968 case DRM_FORMAT_YUV422:
3969 case DRM_FORMAT_YVU422:
3970 case DRM_FORMAT_YUV420:
3971 case DRM_FORMAT_YVU420:
3972 return 2;
3973 default:
3974 return 1;
3975 }
3976}
3977EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3978
3979/**
3980 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3981 * @format: pixel format (DRM_FORMAT_*)
3982 *
3983 * RETURNS:
3984 * The vertical chroma subsampling factor for the
3985 * specified pixel format.
3986 */
3987int drm_format_vert_chroma_subsampling(uint32_t format)
3988{
3989 switch (format) {
3990 case DRM_FORMAT_YUV410:
3991 case DRM_FORMAT_YVU410:
3992 return 4;
3993 case DRM_FORMAT_YUV420:
3994 case DRM_FORMAT_YVU420:
3995 case DRM_FORMAT_NV12:
3996 case DRM_FORMAT_NV21:
3997 return 2;
3998 default:
3999 return 1;
4000 }
4001}
4002EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004003
4004/**
4005 * drm_mode_config_init - initialize DRM mode_configuration structure
4006 * @dev: DRM device
4007 *
4008 * Initialize @dev's mode_config structure, used for tracking the graphics
4009 * configuration of @dev.
4010 *
4011 * Since this initializes the modeset locks, no locking is possible. Which is no
4012 * problem, since this should happen single threaded at init time. It is the
4013 * driver's problem to ensure this guarantee.
4014 *
4015 */
4016void drm_mode_config_init(struct drm_device *dev)
4017{
4018 mutex_init(&dev->mode_config.mutex);
4019 mutex_init(&dev->mode_config.idr_mutex);
4020 mutex_init(&dev->mode_config.fb_lock);
4021 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4022 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4023 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004024 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004025 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4026 INIT_LIST_HEAD(&dev->mode_config.property_list);
4027 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4028 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4029 idr_init(&dev->mode_config.crtc_idr);
4030
4031 drm_modeset_lock_all(dev);
4032 drm_mode_create_standard_connector_properties(dev);
4033 drm_modeset_unlock_all(dev);
4034
4035 /* Just to be sure */
4036 dev->mode_config.num_fb = 0;
4037 dev->mode_config.num_connector = 0;
4038 dev->mode_config.num_crtc = 0;
4039 dev->mode_config.num_encoder = 0;
4040}
4041EXPORT_SYMBOL(drm_mode_config_init);
4042
4043/**
4044 * drm_mode_config_cleanup - free up DRM mode_config info
4045 * @dev: DRM device
4046 *
4047 * Free up all the connectors and CRTCs associated with this DRM device, then
4048 * free up the framebuffers and associated buffer objects.
4049 *
4050 * Note that since this /should/ happen single-threaded at driver/device
4051 * teardown time, no locking is required. It's the driver's job to ensure that
4052 * this guarantee actually holds true.
4053 *
4054 * FIXME: cleanup any dangling user buffer objects too
4055 */
4056void drm_mode_config_cleanup(struct drm_device *dev)
4057{
4058 struct drm_connector *connector, *ot;
4059 struct drm_crtc *crtc, *ct;
4060 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004061 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004062 struct drm_framebuffer *fb, *fbt;
4063 struct drm_property *property, *pt;
4064 struct drm_property_blob *blob, *bt;
4065 struct drm_plane *plane, *plt;
4066
4067 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4068 head) {
4069 encoder->funcs->destroy(encoder);
4070 }
4071
Sean Paul3b336ec2013-08-14 16:47:37 -04004072 list_for_each_entry_safe(bridge, brt,
4073 &dev->mode_config.bridge_list, head) {
4074 bridge->funcs->destroy(bridge);
4075 }
4076
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004077 list_for_each_entry_safe(connector, ot,
4078 &dev->mode_config.connector_list, head) {
4079 connector->funcs->destroy(connector);
4080 }
4081
4082 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4083 head) {
4084 drm_property_destroy(dev, property);
4085 }
4086
4087 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4088 head) {
4089 drm_property_destroy_blob(dev, blob);
4090 }
4091
4092 /*
4093 * Single-threaded teardown context, so it's not required to grab the
4094 * fb_lock to protect against concurrent fb_list access. Contrary, it
4095 * would actually deadlock with the drm_framebuffer_cleanup function.
4096 *
4097 * Also, if there are any framebuffers left, that's a driver leak now,
4098 * so politely WARN about this.
4099 */
4100 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4101 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4102 drm_framebuffer_remove(fb);
4103 }
4104
4105 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4106 head) {
4107 plane->funcs->destroy(plane);
4108 }
4109
4110 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4111 crtc->funcs->destroy(crtc);
4112 }
4113
4114 idr_destroy(&dev->mode_config.crtc_idr);
4115}
4116EXPORT_SYMBOL(drm_mode_config_cleanup);