blob: 54b4169fc48effaa797fdfd79f10dfde1a5b82b1 [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" },
Dave Airlief453ba02008-11-07 14:05:41 -0800205};
206
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000207static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800208{ { DRM_MODE_ENCODER_NONE, "None" },
209 { DRM_MODE_ENCODER_DAC, "DAC" },
210 { DRM_MODE_ENCODER_TMDS, "TMDS" },
211 { DRM_MODE_ENCODER_LVDS, "LVDS" },
212 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200213 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800214};
215
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400216void drm_connector_ida_init(void)
217{
218 int i;
219
220 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
221 ida_init(&drm_connector_enum_list[i].ida);
222}
223
224void drm_connector_ida_destroy(void)
225{
226 int i;
227
228 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
229 ida_destroy(&drm_connector_enum_list[i].ida);
230}
231
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000232const char *drm_get_encoder_name(const struct drm_encoder *encoder)
Dave Airlief453ba02008-11-07 14:05:41 -0800233{
234 static char buf[32];
235
236 snprintf(buf, 32, "%s-%d",
237 drm_encoder_enum_list[encoder->encoder_type].name,
238 encoder->base.id);
239 return buf;
240}
Dave Airlie13a81952009-09-07 15:45:33 +1000241EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800242
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000243const char *drm_get_connector_name(const struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -0800244{
245 static char buf[32];
246
247 snprintf(buf, 32, "%s-%d",
248 drm_connector_enum_list[connector->connector_type].name,
249 connector->connector_type_id);
250 return buf;
251}
252EXPORT_SYMBOL(drm_get_connector_name);
253
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000254const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800255{
256 if (status == connector_status_connected)
257 return "connected";
258 else if (status == connector_status_disconnected)
259 return "disconnected";
260 else
261 return "unknown";
262}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000263EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800264
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300265static char printable_char(int c)
266{
267 return isascii(c) && isprint(c) ? c : '?';
268}
269
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000270const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300271{
272 static char buf[32];
273
274 snprintf(buf, sizeof(buf),
275 "%c%c%c%c %s-endian (0x%08x)",
276 printable_char(format & 0xff),
277 printable_char((format >> 8) & 0xff),
278 printable_char((format >> 16) & 0xff),
279 printable_char((format >> 24) & 0x7f),
280 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
281 format);
282
283 return buf;
284}
285EXPORT_SYMBOL(drm_get_format_name);
286
Dave Airlief453ba02008-11-07 14:05:41 -0800287/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100288 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800289 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100290 * @obj: object pointer, used to generate unique ID
291 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800292 *
Dave Airlief453ba02008-11-07 14:05:41 -0800293 * Create a unique identifier based on @ptr in @dev's identifier space. Used
294 * for tracking modes, CRTCs and connectors.
295 *
296 * RETURNS:
297 * New unique (relative to other objects in @dev) integer identifier for the
298 * object.
299 */
300static int drm_mode_object_get(struct drm_device *dev,
301 struct drm_mode_object *obj, uint32_t obj_type)
302{
Dave Airlief453ba02008-11-07 14:05:41 -0800303 int ret;
304
Jesse Barnesad2563c2009-01-19 17:21:45 +1000305 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800306 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
307 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100308 /*
309 * Set up the object linking under the protection of the idr
310 * lock so that other users can't see inconsistent state.
311 */
Tejun Heo2e928812013-02-27 17:04:08 -0800312 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100313 obj->type = obj_type;
314 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000315 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100316
Tejun Heo2e928812013-02-27 17:04:08 -0800317 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800318}
319
320/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100321 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800322 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100323 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800324 *
Dave Airlief453ba02008-11-07 14:05:41 -0800325 * Free @id from @dev's unique identifier pool.
326 */
327static void drm_mode_object_put(struct drm_device *dev,
328 struct drm_mode_object *object)
329{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000330 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800331 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000332 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800333}
334
Daniel Vetter786b99e2012-12-02 21:53:40 +0100335/**
336 * drm_mode_object_find - look up a drm object with static lifetime
337 * @dev: drm device
338 * @id: id of the mode object
339 * @type: type of the mode object
340 *
341 * Note that framebuffers cannot be looked up with this functions - since those
342 * are reference counted, they need special treatment.
343 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200344struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
345 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800346{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000347 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800348
Daniel Vetter786b99e2012-12-02 21:53:40 +0100349 /* Framebuffers are reference counted and need their own lookup
350 * function.*/
351 WARN_ON(type == DRM_MODE_OBJECT_FB);
352
Jesse Barnesad2563c2009-01-19 17:21:45 +1000353 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800354 obj = idr_find(&dev->mode_config.crtc_idr, id);
355 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000356 obj = NULL;
357 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800358
359 return obj;
360}
361EXPORT_SYMBOL(drm_mode_object_find);
362
363/**
Dave Airlief453ba02008-11-07 14:05:41 -0800364 * drm_framebuffer_init - initialize a framebuffer
365 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100366 * @fb: framebuffer to be initialized
367 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800368 *
Dave Airlief453ba02008-11-07 14:05:41 -0800369 * Allocates an ID for the framebuffer's parent mode object, sets its mode
370 * functions & device file and adds it to the master fd list.
371 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100372 * IMPORTANT:
373 * This functions publishes the fb and makes it available for concurrent access
374 * by other users. Which means by this point the fb _must_ be fully set up -
375 * since all the fb attributes are invariant over its lifetime, no further
376 * locking but only correct reference counting is required.
377 *
Dave Airlief453ba02008-11-07 14:05:41 -0800378 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200379 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800380 */
381int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
382 const struct drm_framebuffer_funcs *funcs)
383{
384 int ret;
385
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100386 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000387 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100388 INIT_LIST_HEAD(&fb->filp_head);
389 fb->dev = dev;
390 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000391
Dave Airlief453ba02008-11-07 14:05:41 -0800392 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200393 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100394 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800395
Daniel Vetter2b677e82012-12-10 21:16:05 +0100396 /* Grab the idr reference. */
397 drm_framebuffer_reference(fb);
398
Dave Airlief453ba02008-11-07 14:05:41 -0800399 dev->mode_config.num_fb++;
400 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100401out:
402 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800403
404 return 0;
405}
406EXPORT_SYMBOL(drm_framebuffer_init);
407
Rob Clarkf7eff602012-09-05 21:48:38 +0000408static void drm_framebuffer_free(struct kref *kref)
409{
410 struct drm_framebuffer *fb =
411 container_of(kref, struct drm_framebuffer, refcount);
412 fb->funcs->destroy(fb);
413}
414
Daniel Vetter2b677e82012-12-10 21:16:05 +0100415static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
416 uint32_t id)
417{
418 struct drm_mode_object *obj = NULL;
419 struct drm_framebuffer *fb;
420
421 mutex_lock(&dev->mode_config.idr_mutex);
422 obj = idr_find(&dev->mode_config.crtc_idr, id);
423 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
424 fb = NULL;
425 else
426 fb = obj_to_fb(obj);
427 mutex_unlock(&dev->mode_config.idr_mutex);
428
429 return fb;
430}
431
Rob Clarkf7eff602012-09-05 21:48:38 +0000432/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100433 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
434 * @dev: drm device
435 * @id: id of the fb object
436 *
437 * If successful, this grabs an additional reference to the framebuffer -
438 * callers need to make sure to eventually unreference the returned framebuffer
439 * again.
440 */
441struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
442 uint32_t id)
443{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100444 struct drm_framebuffer *fb;
445
446 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100447 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100448 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000449 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100450 mutex_unlock(&dev->mode_config.fb_lock);
451
452 return fb;
453}
454EXPORT_SYMBOL(drm_framebuffer_lookup);
455
456/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000457 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100458 * @fb: framebuffer to unref
459 *
460 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000461 */
462void drm_framebuffer_unreference(struct drm_framebuffer *fb)
463{
Rob Clarkf7eff602012-09-05 21:48:38 +0000464 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000465 kref_put(&fb->refcount, drm_framebuffer_free);
466}
467EXPORT_SYMBOL(drm_framebuffer_unreference);
468
469/**
470 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100471 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000472 */
473void drm_framebuffer_reference(struct drm_framebuffer *fb)
474{
475 DRM_DEBUG("FB ID: %d\n", fb->base.id);
476 kref_get(&fb->refcount);
477}
478EXPORT_SYMBOL(drm_framebuffer_reference);
479
Daniel Vetter2b677e82012-12-10 21:16:05 +0100480static void drm_framebuffer_free_bug(struct kref *kref)
481{
482 BUG();
483}
484
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100485static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
486{
487 DRM_DEBUG("FB ID: %d\n", fb->base.id);
488 kref_put(&fb->refcount, drm_framebuffer_free_bug);
489}
490
Daniel Vetter2b677e82012-12-10 21:16:05 +0100491/* dev->mode_config.fb_lock must be held! */
492static void __drm_framebuffer_unregister(struct drm_device *dev,
493 struct drm_framebuffer *fb)
494{
495 mutex_lock(&dev->mode_config.idr_mutex);
496 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
497 mutex_unlock(&dev->mode_config.idr_mutex);
498
499 fb->base.id = 0;
500
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100501 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100502}
503
Dave Airlief453ba02008-11-07 14:05:41 -0800504/**
Daniel Vetter36206362012-12-10 20:42:17 +0100505 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
506 * @fb: fb to unregister
507 *
508 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
509 * those used for fbdev. Note that the caller must hold a reference of it's own,
510 * i.e. the object may not be destroyed through this call (since it'll lead to a
511 * locking inversion).
512 */
513void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
514{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100515 struct drm_device *dev = fb->dev;
516
517 mutex_lock(&dev->mode_config.fb_lock);
518 /* Mark fb as reaped and drop idr ref. */
519 __drm_framebuffer_unregister(dev, fb);
520 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100521}
522EXPORT_SYMBOL(drm_framebuffer_unregister_private);
523
524/**
Dave Airlief453ba02008-11-07 14:05:41 -0800525 * drm_framebuffer_cleanup - remove a framebuffer object
526 * @fb: framebuffer to remove
527 *
Daniel Vetter36206362012-12-10 20:42:17 +0100528 * Cleanup references to a user-created framebuffer. This function is intended
529 * to be used from the drivers ->destroy callback.
530 *
531 * Note that this function does not remove the fb from active usuage - if it is
532 * still used anywhere, hilarity can ensue since userspace could call getfb on
533 * the id and get back -EINVAL. Obviously no concern at driver unload time.
534 *
535 * Also, the framebuffer will not be removed from the lookup idr - for
536 * user-created framebuffers this will happen in in the rmfb ioctl. For
537 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
538 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800539 */
540void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
541{
542 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100543
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100544 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000545 list_del(&fb->head);
546 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100547 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000548}
549EXPORT_SYMBOL(drm_framebuffer_cleanup);
550
551/**
552 * drm_framebuffer_remove - remove and unreference a framebuffer object
553 * @fb: framebuffer to remove
554 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000555 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100556 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100557 * passed-in framebuffer. Might take the modeset locks.
558 *
559 * Note that this function optimizes the cleanup away if the caller holds the
560 * last reference to the framebuffer. It is also guaranteed to not take the
561 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000562 */
563void drm_framebuffer_remove(struct drm_framebuffer *fb)
564{
565 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800566 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800567 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000568 struct drm_mode_set set;
569 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800570
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100571 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100572
Daniel Vetterb62584e2012-12-11 16:51:35 +0100573 /*
574 * drm ABI mandates that we remove any deleted framebuffers from active
575 * useage. But since most sane clients only remove framebuffers they no
576 * longer need, try to optimize this away.
577 *
578 * Since we're holding a reference ourselves, observing a refcount of 1
579 * means that we're the last holder and can skip it. Also, the refcount
580 * can never increase from 1 again, so we don't need any barriers or
581 * locks.
582 *
583 * Note that userspace could try to race with use and instate a new
584 * usage _after_ we've cleared all current ones. End result will be an
585 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
586 * in this manner.
587 */
588 if (atomic_read(&fb->refcount.refcount) > 1) {
589 drm_modeset_lock_all(dev);
590 /* remove from any CRTC */
591 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
592 if (crtc->fb == fb) {
593 /* should turn off the crtc */
594 memset(&set, 0, sizeof(struct drm_mode_set));
595 set.crtc = crtc;
596 set.fb = NULL;
597 ret = drm_mode_set_config_internal(&set);
598 if (ret)
599 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
600 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000601 }
Dave Airlief453ba02008-11-07 14:05:41 -0800602
Daniel Vetterb62584e2012-12-11 16:51:35 +0100603 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300604 if (plane->fb == fb)
605 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800606 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100607 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800608 }
609
Rob Clarkf7eff602012-09-05 21:48:38 +0000610 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800611}
Rob Clarkf7eff602012-09-05 21:48:38 +0000612EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800613
614/**
615 * drm_crtc_init - Initialise a new CRTC object
616 * @dev: DRM device
617 * @crtc: CRTC object to init
618 * @funcs: callbacks for the new CRTC
619 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000620 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200621 *
622 * RETURNS:
623 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800624 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200625int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800626 const struct drm_crtc_funcs *funcs)
627{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200628 int ret;
629
Dave Airlief453ba02008-11-07 14:05:41 -0800630 crtc->dev = dev;
631 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000632 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800633
Daniel Vetter84849902012-12-02 00:28:11 +0100634 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100635 mutex_init(&crtc->mutex);
636 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200637
638 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
639 if (ret)
640 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800641
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300642 crtc->base.properties = &crtc->properties;
643
Dave Airlief453ba02008-11-07 14:05:41 -0800644 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
645 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200646
647 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100648 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200649
650 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800651}
652EXPORT_SYMBOL(drm_crtc_init);
653
654/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000655 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800656 * @crtc: CRTC to cleanup
657 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000658 * This function cleans up @crtc and removes it from the DRM mode setting
659 * core. Note that the function does *not* free the crtc structure itself,
660 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800661 */
662void drm_crtc_cleanup(struct drm_crtc *crtc)
663{
664 struct drm_device *dev = crtc->dev;
665
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000666 kfree(crtc->gamma_store);
667 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800668
669 drm_mode_object_put(dev, &crtc->base);
670 list_del(&crtc->head);
671 dev->mode_config.num_crtc--;
672}
673EXPORT_SYMBOL(drm_crtc_cleanup);
674
675/**
676 * drm_mode_probed_add - add a mode to a connector's probed mode list
677 * @connector: connector the new mode
678 * @mode: mode data
679 *
Dave Airlief453ba02008-11-07 14:05:41 -0800680 * Add @mode to @connector's mode list for later use.
681 */
682void drm_mode_probed_add(struct drm_connector *connector,
683 struct drm_display_mode *mode)
684{
Ville Syrjälä990256a2013-05-31 12:17:07 +0000685 list_add_tail(&mode->head, &connector->probed_modes);
Dave Airlief453ba02008-11-07 14:05:41 -0800686}
687EXPORT_SYMBOL(drm_mode_probed_add);
688
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100689/*
Dave Airlief453ba02008-11-07 14:05:41 -0800690 * drm_mode_remove - remove and free a mode
691 * @connector: connector list to modify
692 * @mode: mode to remove
693 *
Dave Airlief453ba02008-11-07 14:05:41 -0800694 * Remove @mode from @connector's mode list, then free it.
695 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100696static void drm_mode_remove(struct drm_connector *connector,
697 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800698{
699 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100700 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800701}
Dave Airlief453ba02008-11-07 14:05:41 -0800702
703/**
704 * drm_connector_init - Init a preallocated connector
705 * @dev: DRM device
706 * @connector: the connector to init
707 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100708 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800709 *
Dave Airlief453ba02008-11-07 14:05:41 -0800710 * Initialises a preallocated connector. Connectors should be
711 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200712 *
713 * RETURNS:
714 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800715 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200716int drm_connector_init(struct drm_device *dev,
717 struct drm_connector *connector,
718 const struct drm_connector_funcs *funcs,
719 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800720{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200721 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400722 struct ida *connector_ida =
723 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200724
Daniel Vetter84849902012-12-02 00:28:11 +0100725 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800726
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200727 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
728 if (ret)
729 goto out;
730
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300731 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800732 connector->dev = dev;
733 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800734 connector->connector_type = connector_type;
735 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400736 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
737 if (connector->connector_type_id < 0) {
738 ret = connector->connector_type_id;
739 drm_mode_object_put(dev, &connector->base);
740 goto out;
741 }
Dave Airlief453ba02008-11-07 14:05:41 -0800742 INIT_LIST_HEAD(&connector->probed_modes);
743 INIT_LIST_HEAD(&connector->modes);
744 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000745 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800746
747 list_add_tail(&connector->head, &dev->mode_config.connector_list);
748 dev->mode_config.num_connector++;
749
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200750 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500751 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200752 dev->mode_config.edid_property,
753 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800754
Rob Clark58495562012-10-11 20:50:56 -0500755 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800756 dev->mode_config.dpms_property, 0);
757
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200758 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100759 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200760
761 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800762}
763EXPORT_SYMBOL(drm_connector_init);
764
765/**
766 * drm_connector_cleanup - cleans up an initialised connector
767 * @connector: connector to cleanup
768 *
Dave Airlief453ba02008-11-07 14:05:41 -0800769 * Cleans up the connector but doesn't free the object.
770 */
771void drm_connector_cleanup(struct drm_connector *connector)
772{
773 struct drm_device *dev = connector->dev;
774 struct drm_display_mode *mode, *t;
775
776 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
777 drm_mode_remove(connector, mode);
778
779 list_for_each_entry_safe(mode, t, &connector->modes, head)
780 drm_mode_remove(connector, mode);
781
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400782 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
783 connector->connector_type_id);
784
Dave Airlief453ba02008-11-07 14:05:41 -0800785 drm_mode_object_put(dev, &connector->base);
786 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000787 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800788}
789EXPORT_SYMBOL(drm_connector_cleanup);
790
Dave Airliecbc7e222012-02-20 14:16:40 +0000791void drm_connector_unplug_all(struct drm_device *dev)
792{
793 struct drm_connector *connector;
794
795 /* taking the mode config mutex ends up in a clash with sysfs */
796 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
797 drm_sysfs_connector_remove(connector);
798
799}
800EXPORT_SYMBOL(drm_connector_unplug_all);
801
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200802int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000803 struct drm_encoder *encoder,
804 const struct drm_encoder_funcs *funcs,
805 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800806{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200807 int ret;
808
Daniel Vetter84849902012-12-02 00:28:11 +0100809 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800810
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200811 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
812 if (ret)
813 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800814
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200815 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800816 encoder->encoder_type = encoder_type;
817 encoder->funcs = funcs;
818
819 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
820 dev->mode_config.num_encoder++;
821
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200822 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100823 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200824
825 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800826}
827EXPORT_SYMBOL(drm_encoder_init);
828
829void drm_encoder_cleanup(struct drm_encoder *encoder)
830{
831 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100832 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800833 drm_mode_object_put(dev, &encoder->base);
834 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000835 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100836 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800837}
838EXPORT_SYMBOL(drm_encoder_cleanup);
839
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300840/**
841 * drm_plane_init - Initialise a new plane object
842 * @dev: DRM device
843 * @plane: plane object to init
844 * @possible_crtcs: bitmask of possible CRTCs
845 * @funcs: callbacks for the new plane
846 * @formats: array of supported formats (%DRM_FORMAT_*)
847 * @format_count: number of elements in @formats
848 * @priv: plane is private (hidden from userspace)?
849 *
850 * Inits a new object created as base part of a driver plane object.
851 *
852 * RETURNS:
853 * Zero on success, error code on failure.
854 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800855int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
856 unsigned long possible_crtcs,
857 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600858 const uint32_t *formats, uint32_t format_count,
859 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800860{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200861 int ret;
862
Daniel Vetter84849902012-12-02 00:28:11 +0100863 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800864
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200865 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
866 if (ret)
867 goto out;
868
Rob Clark4d939142012-05-17 02:23:27 -0600869 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800870 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800871 plane->funcs = funcs;
872 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
873 GFP_KERNEL);
874 if (!plane->format_types) {
875 DRM_DEBUG_KMS("out of memory when allocating plane\n");
876 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200877 ret = -ENOMEM;
878 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800879 }
880
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800881 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800882 plane->format_count = format_count;
883 plane->possible_crtcs = possible_crtcs;
884
Rob Clark0a7eb242011-12-13 20:19:36 -0600885 /* private planes are not exposed to userspace, but depending on
886 * display hardware, might be convenient to allow sharing programming
887 * for the scanout engine with the crtc implementation.
888 */
889 if (!priv) {
890 list_add_tail(&plane->head, &dev->mode_config.plane_list);
891 dev->mode_config.num_plane++;
892 } else {
893 INIT_LIST_HEAD(&plane->head);
894 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800895
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200896 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100897 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800898
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200899 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800900}
901EXPORT_SYMBOL(drm_plane_init);
902
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300903/**
904 * drm_plane_cleanup - Clean up the core plane usage
905 * @plane: plane to cleanup
906 *
907 * This function cleans up @plane and removes it from the DRM mode setting
908 * core. Note that the function does *not* free the plane structure itself,
909 * this is the responsibility of the caller.
910 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800911void drm_plane_cleanup(struct drm_plane *plane)
912{
913 struct drm_device *dev = plane->dev;
914
Daniel Vetter84849902012-12-02 00:28:11 +0100915 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800916 kfree(plane->format_types);
917 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600918 /* if not added to a list, it must be a private plane */
919 if (!list_empty(&plane->head)) {
920 list_del(&plane->head);
921 dev->mode_config.num_plane--;
922 }
Daniel Vetter84849902012-12-02 00:28:11 +0100923 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800924}
925EXPORT_SYMBOL(drm_plane_cleanup);
926
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300927/**
928 * drm_plane_force_disable - Forcibly disable a plane
929 * @plane: plane to disable
930 *
931 * Forces the plane to be disabled.
932 *
933 * Used when the plane's current framebuffer is destroyed,
934 * and when restoring fbdev mode.
935 */
Ville Syrjälä9125e612013-06-03 16:10:40 +0300936void drm_plane_force_disable(struct drm_plane *plane)
937{
938 int ret;
939
940 if (!plane->fb)
941 return;
942
943 ret = plane->funcs->disable_plane(plane);
944 if (ret)
945 DRM_ERROR("failed to disable plane with busy fb\n");
946 /* disconnect the plane from the fb and crtc: */
947 __drm_framebuffer_unreference(plane->fb);
948 plane->fb = NULL;
949 plane->crtc = NULL;
950}
951EXPORT_SYMBOL(drm_plane_force_disable);
952
Dave Airlief453ba02008-11-07 14:05:41 -0800953/**
954 * drm_mode_create - create a new display mode
955 * @dev: DRM device
956 *
Dave Airlief453ba02008-11-07 14:05:41 -0800957 * Create a new drm_display_mode, give it an ID, and return it.
958 *
959 * RETURNS:
960 * Pointer to new mode on success, NULL on error.
961 */
962struct drm_display_mode *drm_mode_create(struct drm_device *dev)
963{
964 struct drm_display_mode *nmode;
965
966 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
967 if (!nmode)
968 return NULL;
969
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200970 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
971 kfree(nmode);
972 return NULL;
973 }
974
Dave Airlief453ba02008-11-07 14:05:41 -0800975 return nmode;
976}
977EXPORT_SYMBOL(drm_mode_create);
978
979/**
980 * drm_mode_destroy - remove a mode
981 * @dev: DRM device
982 * @mode: mode to remove
983 *
Dave Airlief453ba02008-11-07 14:05:41 -0800984 * Free @mode's unique identifier, then free it.
985 */
986void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
987{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200988 if (!mode)
989 return;
990
Dave Airlief453ba02008-11-07 14:05:41 -0800991 drm_mode_object_put(dev, &mode->base);
992
993 kfree(mode);
994}
995EXPORT_SYMBOL(drm_mode_destroy);
996
997static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
998{
999 struct drm_property *edid;
1000 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001001
1002 /*
1003 * Standard properties (apply to all connectors)
1004 */
1005 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1006 DRM_MODE_PROP_IMMUTABLE,
1007 "EDID", 0);
1008 dev->mode_config.edid_property = edid;
1009
Sascha Hauer4a67d392012-02-06 10:58:17 +01001010 dpms = drm_property_create_enum(dev, 0,
1011 "DPMS", drm_dpms_enum_list,
1012 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001013 dev->mode_config.dpms_property = dpms;
1014
1015 return 0;
1016}
1017
1018/**
1019 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1020 * @dev: DRM device
1021 *
1022 * Called by a driver the first time a DVI-I connector is made.
1023 */
1024int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1025{
1026 struct drm_property *dvi_i_selector;
1027 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001028
1029 if (dev->mode_config.dvi_i_select_subconnector_property)
1030 return 0;
1031
1032 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001033 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001034 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001035 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001036 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001037 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1038
Sascha Hauer4a67d392012-02-06 10:58:17 +01001039 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001040 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001041 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001042 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001043 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1044
1045 return 0;
1046}
1047EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1048
1049/**
1050 * drm_create_tv_properties - create TV specific connector properties
1051 * @dev: DRM device
1052 * @num_modes: number of different TV formats (modes) supported
1053 * @modes: array of pointers to strings containing name of each format
1054 *
1055 * Called by a driver's TV initialization routine, this function creates
1056 * the TV specific connector properties for a given device. Caller is
1057 * responsible for allocating a list of format names and passing them to
1058 * this routine.
1059 */
1060int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1061 char *modes[])
1062{
1063 struct drm_property *tv_selector;
1064 struct drm_property *tv_subconnector;
1065 int i;
1066
1067 if (dev->mode_config.tv_select_subconnector_property)
1068 return 0;
1069
1070 /*
1071 * Basic connector properties
1072 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001073 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001074 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001075 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001076 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001077 dev->mode_config.tv_select_subconnector_property = tv_selector;
1078
1079 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001080 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1081 "subconnector",
1082 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001083 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001084 dev->mode_config.tv_subconnector_property = tv_subconnector;
1085
1086 /*
1087 * Other, TV specific properties: margins & TV modes.
1088 */
1089 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001090 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001091
1092 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001093 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001094
1095 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001096 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001097
1098 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001099 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001100
1101 dev->mode_config.tv_mode_property =
1102 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1103 "mode", num_modes);
1104 for (i = 0; i < num_modes; i++)
1105 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1106 i, modes[i]);
1107
Francisco Jerezb6b79022009-08-02 04:19:20 +02001108 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001109 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001110
1111 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001112 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001113
1114 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001115 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001116
Francisco Jereza75f0232009-08-12 02:30:10 +02001117 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001118 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001119
1120 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001121 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001122
1123 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001124 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001125
Dave Airlief453ba02008-11-07 14:05:41 -08001126 return 0;
1127}
1128EXPORT_SYMBOL(drm_mode_create_tv_properties);
1129
1130/**
1131 * drm_mode_create_scaling_mode_property - create scaling mode property
1132 * @dev: DRM device
1133 *
1134 * Called by a driver the first time it's needed, must be attached to desired
1135 * connectors.
1136 */
1137int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1138{
1139 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001140
1141 if (dev->mode_config.scaling_mode_property)
1142 return 0;
1143
1144 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001145 drm_property_create_enum(dev, 0, "scaling mode",
1146 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001147 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001148
1149 dev->mode_config.scaling_mode_property = scaling_mode;
1150
1151 return 0;
1152}
1153EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1154
1155/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001156 * drm_mode_create_dirty_property - create dirty property
1157 * @dev: DRM device
1158 *
1159 * Called by a driver the first time it's needed, must be attached to desired
1160 * connectors.
1161 */
1162int drm_mode_create_dirty_info_property(struct drm_device *dev)
1163{
1164 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001165
1166 if (dev->mode_config.dirty_info_property)
1167 return 0;
1168
1169 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001170 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001171 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001172 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001173 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001174 dev->mode_config.dirty_info_property = dirty_info;
1175
1176 return 0;
1177}
1178EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1179
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001180static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001181{
1182 uint32_t total_objects = 0;
1183
1184 total_objects += dev->mode_config.num_crtc;
1185 total_objects += dev->mode_config.num_connector;
1186 total_objects += dev->mode_config.num_encoder;
1187
Dave Airlief453ba02008-11-07 14:05:41 -08001188 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1189 if (!group->id_list)
1190 return -ENOMEM;
1191
1192 group->num_crtcs = 0;
1193 group->num_connectors = 0;
1194 group->num_encoders = 0;
1195 return 0;
1196}
1197
1198int drm_mode_group_init_legacy_group(struct drm_device *dev,
1199 struct drm_mode_group *group)
1200{
1201 struct drm_crtc *crtc;
1202 struct drm_encoder *encoder;
1203 struct drm_connector *connector;
1204 int ret;
1205
1206 if ((ret = drm_mode_group_init(dev, group)))
1207 return ret;
1208
1209 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1210 group->id_list[group->num_crtcs++] = crtc->base.id;
1211
1212 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1213 group->id_list[group->num_crtcs + group->num_encoders++] =
1214 encoder->base.id;
1215
1216 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1217 group->id_list[group->num_crtcs + group->num_encoders +
1218 group->num_connectors++] = connector->base.id;
1219
1220 return 0;
1221}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001222EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001223
1224/**
Dave Airlief453ba02008-11-07 14:05:41 -08001225 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1226 * @out: drm_mode_modeinfo struct to return to the user
1227 * @in: drm_display_mode to use
1228 *
Dave Airlief453ba02008-11-07 14:05:41 -08001229 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1230 * the user.
1231 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001232static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1233 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001234{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001235 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1236 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1237 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1238 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1239 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1240 "timing values too large for mode info\n");
1241
Dave Airlief453ba02008-11-07 14:05:41 -08001242 out->clock = in->clock;
1243 out->hdisplay = in->hdisplay;
1244 out->hsync_start = in->hsync_start;
1245 out->hsync_end = in->hsync_end;
1246 out->htotal = in->htotal;
1247 out->hskew = in->hskew;
1248 out->vdisplay = in->vdisplay;
1249 out->vsync_start = in->vsync_start;
1250 out->vsync_end = in->vsync_end;
1251 out->vtotal = in->vtotal;
1252 out->vscan = in->vscan;
1253 out->vrefresh = in->vrefresh;
1254 out->flags = in->flags;
1255 out->type = in->type;
1256 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1257 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1258}
1259
1260/**
1261 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1262 * @out: drm_display_mode to return to the user
1263 * @in: drm_mode_modeinfo to use
1264 *
Dave Airlief453ba02008-11-07 14:05:41 -08001265 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1266 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001267 *
1268 * RETURNS:
1269 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001270 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001271static int drm_crtc_convert_umode(struct drm_display_mode *out,
1272 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001273{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001274 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1275 return -ERANGE;
1276
Dave Airlief453ba02008-11-07 14:05:41 -08001277 out->clock = in->clock;
1278 out->hdisplay = in->hdisplay;
1279 out->hsync_start = in->hsync_start;
1280 out->hsync_end = in->hsync_end;
1281 out->htotal = in->htotal;
1282 out->hskew = in->hskew;
1283 out->vdisplay = in->vdisplay;
1284 out->vsync_start = in->vsync_start;
1285 out->vsync_end = in->vsync_end;
1286 out->vtotal = in->vtotal;
1287 out->vscan = in->vscan;
1288 out->vrefresh = in->vrefresh;
1289 out->flags = in->flags;
1290 out->type = in->type;
1291 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1292 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001293
1294 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001295}
1296
1297/**
1298 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001299 * @dev: drm device for the ioctl
1300 * @data: data pointer for the ioctl
1301 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001302 *
Dave Airlief453ba02008-11-07 14:05:41 -08001303 * Construct a set of configuration description structures and return
1304 * them to the user, including CRTC, connector and framebuffer configuration.
1305 *
1306 * Called by the user via ioctl.
1307 *
1308 * RETURNS:
1309 * Zero on success, errno on failure.
1310 */
1311int drm_mode_getresources(struct drm_device *dev, void *data,
1312 struct drm_file *file_priv)
1313{
1314 struct drm_mode_card_res *card_res = data;
1315 struct list_head *lh;
1316 struct drm_framebuffer *fb;
1317 struct drm_connector *connector;
1318 struct drm_crtc *crtc;
1319 struct drm_encoder *encoder;
1320 int ret = 0;
1321 int connector_count = 0;
1322 int crtc_count = 0;
1323 int fb_count = 0;
1324 int encoder_count = 0;
1325 int copied = 0, i;
1326 uint32_t __user *fb_id;
1327 uint32_t __user *crtc_id;
1328 uint32_t __user *connector_id;
1329 uint32_t __user *encoder_id;
1330 struct drm_mode_group *mode_group;
1331
Dave Airliefb3b06c2011-02-08 13:55:21 +10001332 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1333 return -EINVAL;
1334
Dave Airlief453ba02008-11-07 14:05:41 -08001335
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001336 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001337 /*
1338 * For the non-control nodes we need to limit the list of resources
1339 * by IDs in the group list for this node
1340 */
1341 list_for_each(lh, &file_priv->fbs)
1342 fb_count++;
1343
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001344 /* handle this in 4 parts */
1345 /* FBs */
1346 if (card_res->count_fbs >= fb_count) {
1347 copied = 0;
1348 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1349 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1350 if (put_user(fb->base.id, fb_id + copied)) {
1351 mutex_unlock(&file_priv->fbs_lock);
1352 return -EFAULT;
1353 }
1354 copied++;
1355 }
1356 }
1357 card_res->count_fbs = fb_count;
1358 mutex_unlock(&file_priv->fbs_lock);
1359
1360 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001361 mode_group = &file_priv->master->minor->mode_group;
1362 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1363
1364 list_for_each(lh, &dev->mode_config.crtc_list)
1365 crtc_count++;
1366
1367 list_for_each(lh, &dev->mode_config.connector_list)
1368 connector_count++;
1369
1370 list_for_each(lh, &dev->mode_config.encoder_list)
1371 encoder_count++;
1372 } else {
1373
1374 crtc_count = mode_group->num_crtcs;
1375 connector_count = mode_group->num_connectors;
1376 encoder_count = mode_group->num_encoders;
1377 }
1378
1379 card_res->max_height = dev->mode_config.max_height;
1380 card_res->min_height = dev->mode_config.min_height;
1381 card_res->max_width = dev->mode_config.max_width;
1382 card_res->min_width = dev->mode_config.min_width;
1383
Dave Airlief453ba02008-11-07 14:05:41 -08001384 /* CRTCs */
1385 if (card_res->count_crtcs >= crtc_count) {
1386 copied = 0;
1387 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1388 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1389 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1390 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001391 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001392 if (put_user(crtc->base.id, crtc_id + copied)) {
1393 ret = -EFAULT;
1394 goto out;
1395 }
1396 copied++;
1397 }
1398 } else {
1399 for (i = 0; i < mode_group->num_crtcs; i++) {
1400 if (put_user(mode_group->id_list[i],
1401 crtc_id + copied)) {
1402 ret = -EFAULT;
1403 goto out;
1404 }
1405 copied++;
1406 }
1407 }
1408 }
1409 card_res->count_crtcs = crtc_count;
1410
1411 /* Encoders */
1412 if (card_res->count_encoders >= encoder_count) {
1413 copied = 0;
1414 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1415 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1416 list_for_each_entry(encoder,
1417 &dev->mode_config.encoder_list,
1418 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001419 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1420 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001421 if (put_user(encoder->base.id, encoder_id +
1422 copied)) {
1423 ret = -EFAULT;
1424 goto out;
1425 }
1426 copied++;
1427 }
1428 } else {
1429 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1430 if (put_user(mode_group->id_list[i],
1431 encoder_id + copied)) {
1432 ret = -EFAULT;
1433 goto out;
1434 }
1435 copied++;
1436 }
1437
1438 }
1439 }
1440 card_res->count_encoders = encoder_count;
1441
1442 /* Connectors */
1443 if (card_res->count_connectors >= connector_count) {
1444 copied = 0;
1445 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1446 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1447 list_for_each_entry(connector,
1448 &dev->mode_config.connector_list,
1449 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001450 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1451 connector->base.id,
1452 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001453 if (put_user(connector->base.id,
1454 connector_id + copied)) {
1455 ret = -EFAULT;
1456 goto out;
1457 }
1458 copied++;
1459 }
1460 } else {
1461 int start = mode_group->num_crtcs +
1462 mode_group->num_encoders;
1463 for (i = start; i < start + mode_group->num_connectors; i++) {
1464 if (put_user(mode_group->id_list[i],
1465 connector_id + copied)) {
1466 ret = -EFAULT;
1467 goto out;
1468 }
1469 copied++;
1470 }
1471 }
1472 }
1473 card_res->count_connectors = connector_count;
1474
Jerome Glisse94401062010-07-15 15:43:25 -04001475 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001476 card_res->count_connectors, card_res->count_encoders);
1477
1478out:
Daniel Vetter84849902012-12-02 00:28:11 +01001479 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001480 return ret;
1481}
1482
1483/**
1484 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001485 * @dev: drm device for the ioctl
1486 * @data: data pointer for the ioctl
1487 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001488 *
Dave Airlief453ba02008-11-07 14:05:41 -08001489 * Construct a CRTC configuration structure to return to the user.
1490 *
1491 * Called by the user via ioctl.
1492 *
1493 * RETURNS:
1494 * Zero on success, errno on failure.
1495 */
1496int drm_mode_getcrtc(struct drm_device *dev,
1497 void *data, struct drm_file *file_priv)
1498{
1499 struct drm_mode_crtc *crtc_resp = data;
1500 struct drm_crtc *crtc;
1501 struct drm_mode_object *obj;
1502 int ret = 0;
1503
Dave Airliefb3b06c2011-02-08 13:55:21 +10001504 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1505 return -EINVAL;
1506
Daniel Vetter84849902012-12-02 00:28:11 +01001507 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001508
1509 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1510 DRM_MODE_OBJECT_CRTC);
1511 if (!obj) {
1512 ret = -EINVAL;
1513 goto out;
1514 }
1515 crtc = obj_to_crtc(obj);
1516
1517 crtc_resp->x = crtc->x;
1518 crtc_resp->y = crtc->y;
1519 crtc_resp->gamma_size = crtc->gamma_size;
1520 if (crtc->fb)
1521 crtc_resp->fb_id = crtc->fb->base.id;
1522 else
1523 crtc_resp->fb_id = 0;
1524
1525 if (crtc->enabled) {
1526
1527 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1528 crtc_resp->mode_valid = 1;
1529
1530 } else {
1531 crtc_resp->mode_valid = 0;
1532 }
1533
1534out:
Daniel Vetter84849902012-12-02 00:28:11 +01001535 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001536 return ret;
1537}
1538
1539/**
1540 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001541 * @dev: drm device for the ioctl
1542 * @data: data pointer for the ioctl
1543 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001544 *
Dave Airlief453ba02008-11-07 14:05:41 -08001545 * Construct a connector configuration structure to return to the user.
1546 *
1547 * Called by the user via ioctl.
1548 *
1549 * RETURNS:
1550 * Zero on success, errno on failure.
1551 */
1552int drm_mode_getconnector(struct drm_device *dev, void *data,
1553 struct drm_file *file_priv)
1554{
1555 struct drm_mode_get_connector *out_resp = data;
1556 struct drm_mode_object *obj;
1557 struct drm_connector *connector;
1558 struct drm_display_mode *mode;
1559 int mode_count = 0;
1560 int props_count = 0;
1561 int encoders_count = 0;
1562 int ret = 0;
1563 int copied = 0;
1564 int i;
1565 struct drm_mode_modeinfo u_mode;
1566 struct drm_mode_modeinfo __user *mode_ptr;
1567 uint32_t __user *prop_ptr;
1568 uint64_t __user *prop_values;
1569 uint32_t __user *encoder_ptr;
1570
Dave Airliefb3b06c2011-02-08 13:55:21 +10001571 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1572 return -EINVAL;
1573
Dave Airlief453ba02008-11-07 14:05:41 -08001574 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1575
Jerome Glisse94401062010-07-15 15:43:25 -04001576 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001577
Daniel Vetter7b240562012-12-12 00:35:33 +01001578 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001579
1580 obj = drm_mode_object_find(dev, out_resp->connector_id,
1581 DRM_MODE_OBJECT_CONNECTOR);
1582 if (!obj) {
1583 ret = -EINVAL;
1584 goto out;
1585 }
1586 connector = obj_to_connector(obj);
1587
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001588 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001589
1590 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1591 if (connector->encoder_ids[i] != 0) {
1592 encoders_count++;
1593 }
1594 }
1595
1596 if (out_resp->count_modes == 0) {
1597 connector->funcs->fill_modes(connector,
1598 dev->mode_config.max_width,
1599 dev->mode_config.max_height);
1600 }
1601
1602 /* delayed so we get modes regardless of pre-fill_modes state */
1603 list_for_each_entry(mode, &connector->modes, head)
1604 mode_count++;
1605
1606 out_resp->connector_id = connector->base.id;
1607 out_resp->connector_type = connector->connector_type;
1608 out_resp->connector_type_id = connector->connector_type_id;
1609 out_resp->mm_width = connector->display_info.width_mm;
1610 out_resp->mm_height = connector->display_info.height_mm;
1611 out_resp->subpixel = connector->display_info.subpixel_order;
1612 out_resp->connection = connector->status;
1613 if (connector->encoder)
1614 out_resp->encoder_id = connector->encoder->base.id;
1615 else
1616 out_resp->encoder_id = 0;
1617
1618 /*
1619 * This ioctl is called twice, once to determine how much space is
1620 * needed, and the 2nd time to fill it.
1621 */
1622 if ((out_resp->count_modes >= mode_count) && mode_count) {
1623 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001624 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001625 list_for_each_entry(mode, &connector->modes, head) {
1626 drm_crtc_convert_to_umode(&u_mode, mode);
1627 if (copy_to_user(mode_ptr + copied,
1628 &u_mode, sizeof(u_mode))) {
1629 ret = -EFAULT;
1630 goto out;
1631 }
1632 copied++;
1633 }
1634 }
1635 out_resp->count_modes = mode_count;
1636
1637 if ((out_resp->count_props >= props_count) && props_count) {
1638 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001639 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1640 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001641 for (i = 0; i < connector->properties.count; i++) {
1642 if (put_user(connector->properties.ids[i],
1643 prop_ptr + copied)) {
1644 ret = -EFAULT;
1645 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001646 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001647
1648 if (put_user(connector->properties.values[i],
1649 prop_values + copied)) {
1650 ret = -EFAULT;
1651 goto out;
1652 }
1653 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001654 }
1655 }
1656 out_resp->count_props = props_count;
1657
1658 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1659 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001660 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001661 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1662 if (connector->encoder_ids[i] != 0) {
1663 if (put_user(connector->encoder_ids[i],
1664 encoder_ptr + copied)) {
1665 ret = -EFAULT;
1666 goto out;
1667 }
1668 copied++;
1669 }
1670 }
1671 }
1672 out_resp->count_encoders = encoders_count;
1673
1674out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001675 mutex_unlock(&dev->mode_config.mutex);
1676
Dave Airlief453ba02008-11-07 14:05:41 -08001677 return ret;
1678}
1679
1680int drm_mode_getencoder(struct drm_device *dev, void *data,
1681 struct drm_file *file_priv)
1682{
1683 struct drm_mode_get_encoder *enc_resp = data;
1684 struct drm_mode_object *obj;
1685 struct drm_encoder *encoder;
1686 int ret = 0;
1687
Dave Airliefb3b06c2011-02-08 13:55:21 +10001688 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1689 return -EINVAL;
1690
Daniel Vetter84849902012-12-02 00:28:11 +01001691 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001692 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1693 DRM_MODE_OBJECT_ENCODER);
1694 if (!obj) {
1695 ret = -EINVAL;
1696 goto out;
1697 }
1698 encoder = obj_to_encoder(obj);
1699
1700 if (encoder->crtc)
1701 enc_resp->crtc_id = encoder->crtc->base.id;
1702 else
1703 enc_resp->crtc_id = 0;
1704 enc_resp->encoder_type = encoder->encoder_type;
1705 enc_resp->encoder_id = encoder->base.id;
1706 enc_resp->possible_crtcs = encoder->possible_crtcs;
1707 enc_resp->possible_clones = encoder->possible_clones;
1708
1709out:
Daniel Vetter84849902012-12-02 00:28:11 +01001710 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001711 return ret;
1712}
1713
1714/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001715 * drm_mode_getplane_res - get plane info
1716 * @dev: DRM device
1717 * @data: ioctl data
1718 * @file_priv: DRM file info
1719 *
1720 * Return an plane count and set of IDs.
1721 */
1722int drm_mode_getplane_res(struct drm_device *dev, void *data,
1723 struct drm_file *file_priv)
1724{
1725 struct drm_mode_get_plane_res *plane_resp = data;
1726 struct drm_mode_config *config;
1727 struct drm_plane *plane;
1728 uint32_t __user *plane_ptr;
1729 int copied = 0, ret = 0;
1730
1731 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1732 return -EINVAL;
1733
Daniel Vetter84849902012-12-02 00:28:11 +01001734 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001735 config = &dev->mode_config;
1736
1737 /*
1738 * This ioctl is called twice, once to determine how much space is
1739 * needed, and the 2nd time to fill it.
1740 */
1741 if (config->num_plane &&
1742 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001743 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001744
1745 list_for_each_entry(plane, &config->plane_list, head) {
1746 if (put_user(plane->base.id, plane_ptr + copied)) {
1747 ret = -EFAULT;
1748 goto out;
1749 }
1750 copied++;
1751 }
1752 }
1753 plane_resp->count_planes = config->num_plane;
1754
1755out:
Daniel Vetter84849902012-12-02 00:28:11 +01001756 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001757 return ret;
1758}
1759
1760/**
1761 * drm_mode_getplane - get plane info
1762 * @dev: DRM device
1763 * @data: ioctl data
1764 * @file_priv: DRM file info
1765 *
1766 * Return plane info, including formats supported, gamma size, any
1767 * current fb, etc.
1768 */
1769int drm_mode_getplane(struct drm_device *dev, void *data,
1770 struct drm_file *file_priv)
1771{
1772 struct drm_mode_get_plane *plane_resp = data;
1773 struct drm_mode_object *obj;
1774 struct drm_plane *plane;
1775 uint32_t __user *format_ptr;
1776 int ret = 0;
1777
1778 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1779 return -EINVAL;
1780
Daniel Vetter84849902012-12-02 00:28:11 +01001781 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001782 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1783 DRM_MODE_OBJECT_PLANE);
1784 if (!obj) {
1785 ret = -ENOENT;
1786 goto out;
1787 }
1788 plane = obj_to_plane(obj);
1789
1790 if (plane->crtc)
1791 plane_resp->crtc_id = plane->crtc->base.id;
1792 else
1793 plane_resp->crtc_id = 0;
1794
1795 if (plane->fb)
1796 plane_resp->fb_id = plane->fb->base.id;
1797 else
1798 plane_resp->fb_id = 0;
1799
1800 plane_resp->plane_id = plane->base.id;
1801 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001802 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001803
1804 /*
1805 * This ioctl is called twice, once to determine how much space is
1806 * needed, and the 2nd time to fill it.
1807 */
1808 if (plane->format_count &&
1809 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001810 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001811 if (copy_to_user(format_ptr,
1812 plane->format_types,
1813 sizeof(uint32_t) * plane->format_count)) {
1814 ret = -EFAULT;
1815 goto out;
1816 }
1817 }
1818 plane_resp->count_format_types = plane->format_count;
1819
1820out:
Daniel Vetter84849902012-12-02 00:28:11 +01001821 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001822 return ret;
1823}
1824
1825/**
1826 * drm_mode_setplane - set up or tear down an plane
1827 * @dev: DRM device
1828 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001829 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001830 *
1831 * Set plane info, including placement, fb, scaling, and other factors.
1832 * Or pass a NULL fb to disable.
1833 */
1834int drm_mode_setplane(struct drm_device *dev, void *data,
1835 struct drm_file *file_priv)
1836{
1837 struct drm_mode_set_plane *plane_req = data;
1838 struct drm_mode_object *obj;
1839 struct drm_plane *plane;
1840 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001841 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001842 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001843 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001844 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001845
1846 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1847 return -EINVAL;
1848
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001849 /*
1850 * First, find the plane, crtc, and fb objects. If not available,
1851 * we don't bother to call the driver.
1852 */
1853 obj = drm_mode_object_find(dev, plane_req->plane_id,
1854 DRM_MODE_OBJECT_PLANE);
1855 if (!obj) {
1856 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1857 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001858 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001859 }
1860 plane = obj_to_plane(obj);
1861
1862 /* No fb means shut it down */
1863 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001864 drm_modeset_lock_all(dev);
1865 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001866 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001867 plane->crtc = NULL;
1868 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001869 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001870 goto out;
1871 }
1872
1873 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1874 DRM_MODE_OBJECT_CRTC);
1875 if (!obj) {
1876 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1877 plane_req->crtc_id);
1878 ret = -ENOENT;
1879 goto out;
1880 }
1881 crtc = obj_to_crtc(obj);
1882
Daniel Vetter786b99e2012-12-02 21:53:40 +01001883 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1884 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001885 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1886 plane_req->fb_id);
1887 ret = -ENOENT;
1888 goto out;
1889 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001890
Ville Syrjälä62443be2011-12-20 00:06:47 +02001891 /* Check whether this plane supports the fb pixel format. */
1892 for (i = 0; i < plane->format_count; i++)
1893 if (fb->pixel_format == plane->format_types[i])
1894 break;
1895 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03001896 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1897 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02001898 ret = -EINVAL;
1899 goto out;
1900 }
1901
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001902 fb_width = fb->width << 16;
1903 fb_height = fb->height << 16;
1904
1905 /* Make sure source coordinates are inside the fb. */
1906 if (plane_req->src_w > fb_width ||
1907 plane_req->src_x > fb_width - plane_req->src_w ||
1908 plane_req->src_h > fb_height ||
1909 plane_req->src_y > fb_height - plane_req->src_h) {
1910 DRM_DEBUG_KMS("Invalid source coordinates "
1911 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1912 plane_req->src_w >> 16,
1913 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1914 plane_req->src_h >> 16,
1915 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1916 plane_req->src_x >> 16,
1917 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1918 plane_req->src_y >> 16,
1919 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1920 ret = -ENOSPC;
1921 goto out;
1922 }
1923
Ville Syrjälä687a0402011-12-20 00:06:45 +02001924 /* Give drivers some help against integer overflows */
1925 if (plane_req->crtc_w > INT_MAX ||
1926 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1927 plane_req->crtc_h > INT_MAX ||
1928 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1929 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1930 plane_req->crtc_w, plane_req->crtc_h,
1931 plane_req->crtc_x, plane_req->crtc_y);
1932 ret = -ERANGE;
1933 goto out;
1934 }
1935
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001936 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001937 ret = plane->funcs->update_plane(plane, crtc, fb,
1938 plane_req->crtc_x, plane_req->crtc_y,
1939 plane_req->crtc_w, plane_req->crtc_h,
1940 plane_req->src_x, plane_req->src_y,
1941 plane_req->src_w, plane_req->src_h);
1942 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001943 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001944 plane->crtc = crtc;
1945 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001946 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001947 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001948 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001949
1950out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001951 if (fb)
1952 drm_framebuffer_unreference(fb);
1953 if (old_fb)
1954 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001955
1956 return ret;
1957}
1958
1959/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001960 * drm_mode_set_config_internal - helper to call ->set_config
1961 * @set: modeset config to set
1962 *
1963 * This is a little helper to wrap internal calls to the ->set_config driver
1964 * interface. The only thing it adds is correct refcounting dance.
1965 */
1966int drm_mode_set_config_internal(struct drm_mode_set *set)
1967{
1968 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001969 struct drm_framebuffer *fb;
1970 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001971 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001972
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001973 /*
1974 * NOTE: ->set_config can also disable other crtcs (if we steal all
1975 * connectors from it), hence we need to refcount the fbs across all
1976 * crtcs. Atomic modeset will have saner semantics ...
1977 */
1978 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
1979 tmp->old_fb = tmp->fb;
1980
Daniel Vetterb0d12322012-12-11 01:07:12 +01001981 fb = set->fb;
1982
1983 ret = crtc->funcs->set_config(set);
1984 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02001985 /* crtc->fb must be updated by ->set_config, enforces this. */
1986 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001987 }
Daniel Vettercc85e122013-06-15 00:13:15 +02001988
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001989 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
1990 if (tmp->fb)
1991 drm_framebuffer_reference(tmp->fb);
1992 if (tmp->old_fb)
1993 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01001994 }
1995
1996 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001997}
1998EXPORT_SYMBOL(drm_mode_set_config_internal);
1999
2000/**
Dave Airlief453ba02008-11-07 14:05:41 -08002001 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002002 * @dev: drm device for the ioctl
2003 * @data: data pointer for the ioctl
2004 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002005 *
Dave Airlief453ba02008-11-07 14:05:41 -08002006 * Build a new CRTC configuration based on user request.
2007 *
2008 * Called by the user via ioctl.
2009 *
2010 * RETURNS:
2011 * Zero on success, errno on failure.
2012 */
2013int drm_mode_setcrtc(struct drm_device *dev, void *data,
2014 struct drm_file *file_priv)
2015{
2016 struct drm_mode_config *config = &dev->mode_config;
2017 struct drm_mode_crtc *crtc_req = data;
2018 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002019 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002020 struct drm_connector **connector_set = NULL, *connector;
2021 struct drm_framebuffer *fb = NULL;
2022 struct drm_display_mode *mode = NULL;
2023 struct drm_mode_set set;
2024 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002025 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002026 int i;
2027
Dave Airliefb3b06c2011-02-08 13:55:21 +10002028 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2029 return -EINVAL;
2030
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002031 /* For some reason crtc x/y offsets are signed internally. */
2032 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2033 return -ERANGE;
2034
Daniel Vetter84849902012-12-02 00:28:11 +01002035 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002036 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2037 DRM_MODE_OBJECT_CRTC);
2038 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002039 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002040 ret = -EINVAL;
2041 goto out;
2042 }
2043 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002044 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002045
2046 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002047 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002048 /* If we have a mode we need a framebuffer. */
2049 /* If we pass -1, set the mode with the currently bound fb */
2050 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002051 if (!crtc->fb) {
2052 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2053 ret = -EINVAL;
2054 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002055 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002056 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002057 /* Make refcounting symmetric with the lookup path. */
2058 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002059 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002060 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2061 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002062 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2063 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002064 ret = -EINVAL;
2065 goto out;
2066 }
Dave Airlief453ba02008-11-07 14:05:41 -08002067 }
2068
2069 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002070 if (!mode) {
2071 ret = -ENOMEM;
2072 goto out;
2073 }
2074
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002075 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2076 if (ret) {
2077 DRM_DEBUG_KMS("Invalid mode\n");
2078 goto out;
2079 }
2080
Dave Airlief453ba02008-11-07 14:05:41 -08002081 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002082
Rob Clark7c80e122012-09-04 16:35:56 +00002083 hdisplay = mode->hdisplay;
2084 vdisplay = mode->vdisplay;
2085
2086 if (crtc->invert_dimensions)
2087 swap(hdisplay, vdisplay);
2088
2089 if (hdisplay > fb->width ||
2090 vdisplay > fb->height ||
2091 crtc_req->x > fb->width - hdisplay ||
2092 crtc_req->y > fb->height - vdisplay) {
2093 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2094 fb->width, fb->height,
2095 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2096 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002097 ret = -ENOSPC;
2098 goto out;
2099 }
Dave Airlief453ba02008-11-07 14:05:41 -08002100 }
2101
2102 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002103 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002104 ret = -EINVAL;
2105 goto out;
2106 }
2107
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002108 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002109 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002110 crtc_req->count_connectors);
2111 ret = -EINVAL;
2112 goto out;
2113 }
2114
2115 if (crtc_req->count_connectors > 0) {
2116 u32 out_id;
2117
2118 /* Avoid unbounded kernel memory allocation */
2119 if (crtc_req->count_connectors > config->num_connector) {
2120 ret = -EINVAL;
2121 goto out;
2122 }
2123
2124 connector_set = kmalloc(crtc_req->count_connectors *
2125 sizeof(struct drm_connector *),
2126 GFP_KERNEL);
2127 if (!connector_set) {
2128 ret = -ENOMEM;
2129 goto out;
2130 }
2131
2132 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002133 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002134 if (get_user(out_id, &set_connectors_ptr[i])) {
2135 ret = -EFAULT;
2136 goto out;
2137 }
2138
2139 obj = drm_mode_object_find(dev, out_id,
2140 DRM_MODE_OBJECT_CONNECTOR);
2141 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002142 DRM_DEBUG_KMS("Connector id %d unknown\n",
2143 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002144 ret = -EINVAL;
2145 goto out;
2146 }
2147 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002148 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2149 connector->base.id,
2150 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002151
2152 connector_set[i] = connector;
2153 }
2154 }
2155
2156 set.crtc = crtc;
2157 set.x = crtc_req->x;
2158 set.y = crtc_req->y;
2159 set.mode = mode;
2160 set.connectors = connector_set;
2161 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002162 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002163 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002164
2165out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002166 if (fb)
2167 drm_framebuffer_unreference(fb);
2168
Dave Airlief453ba02008-11-07 14:05:41 -08002169 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002170 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002171 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002172 return ret;
2173}
2174
Dave Airlie4c813d42013-06-20 11:48:52 +10002175static int drm_mode_cursor_common(struct drm_device *dev,
2176 struct drm_mode_cursor2 *req,
2177 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002178{
Dave Airlief453ba02008-11-07 14:05:41 -08002179 struct drm_mode_object *obj;
2180 struct drm_crtc *crtc;
2181 int ret = 0;
2182
Dave Airliefb3b06c2011-02-08 13:55:21 +10002183 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2184 return -EINVAL;
2185
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002186 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002187 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002188
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002189 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002190 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002191 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002192 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002193 }
2194 crtc = obj_to_crtc(obj);
2195
Daniel Vetterdac35662012-12-02 15:24:10 +01002196 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002197 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002198 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002199 ret = -ENXIO;
2200 goto out;
2201 }
2202 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002203 if (crtc->funcs->cursor_set2)
2204 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2205 req->width, req->height, req->hot_x, req->hot_y);
2206 else
2207 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2208 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002209 }
2210
2211 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2212 if (crtc->funcs->cursor_move) {
2213 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2214 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002215 ret = -EFAULT;
2216 goto out;
2217 }
2218 }
2219out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002220 mutex_unlock(&crtc->mutex);
2221
Dave Airlief453ba02008-11-07 14:05:41 -08002222 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002223
2224}
2225int drm_mode_cursor_ioctl(struct drm_device *dev,
2226 void *data, struct drm_file *file_priv)
2227{
2228 struct drm_mode_cursor *req = data;
2229 struct drm_mode_cursor2 new_req;
2230
2231 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2232 new_req.hot_x = new_req.hot_y = 0;
2233
2234 return drm_mode_cursor_common(dev, &new_req, file_priv);
2235}
2236
2237int drm_mode_cursor2_ioctl(struct drm_device *dev,
2238 void *data, struct drm_file *file_priv)
2239{
2240 struct drm_mode_cursor2 *req = data;
2241 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002242}
2243
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002244/* Original addfb only supported RGB formats, so figure out which one */
2245uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2246{
2247 uint32_t fmt;
2248
2249 switch (bpp) {
2250 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002251 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002252 break;
2253 case 16:
2254 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002255 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002256 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002257 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002258 break;
2259 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002260 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002261 break;
2262 case 32:
2263 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002264 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002265 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002266 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002267 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002268 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002269 break;
2270 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002271 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2272 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002273 break;
2274 }
2275
2276 return fmt;
2277}
2278EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2279
Dave Airlief453ba02008-11-07 14:05:41 -08002280/**
2281 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002282 * @dev: drm device for the ioctl
2283 * @data: data pointer for the ioctl
2284 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002285 *
Dave Airlief453ba02008-11-07 14:05:41 -08002286 * Add a new FB to the specified CRTC, given a user request.
2287 *
2288 * Called by the user via ioctl.
2289 *
2290 * RETURNS:
2291 * Zero on success, errno on failure.
2292 */
2293int drm_mode_addfb(struct drm_device *dev,
2294 void *data, struct drm_file *file_priv)
2295{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002296 struct drm_mode_fb_cmd *or = data;
2297 struct drm_mode_fb_cmd2 r = {};
2298 struct drm_mode_config *config = &dev->mode_config;
2299 struct drm_framebuffer *fb;
2300 int ret = 0;
2301
2302 /* Use new struct with format internally */
2303 r.fb_id = or->fb_id;
2304 r.width = or->width;
2305 r.height = or->height;
2306 r.pitches[0] = or->pitch;
2307 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2308 r.handles[0] = or->handle;
2309
2310 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2311 return -EINVAL;
2312
Jesse Barnesacb4b992011-11-07 12:03:23 -08002313 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002314 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002315
2316 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002317 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002318
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002319 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2320 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002321 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002322 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002323 }
2324
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002325 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002326 or->fb_id = fb->base.id;
2327 list_add(&fb->filp_head, &file_priv->fbs);
2328 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002329 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002330
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002331 return ret;
2332}
2333
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002334static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002335{
2336 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2337
2338 switch (format) {
2339 case DRM_FORMAT_C8:
2340 case DRM_FORMAT_RGB332:
2341 case DRM_FORMAT_BGR233:
2342 case DRM_FORMAT_XRGB4444:
2343 case DRM_FORMAT_XBGR4444:
2344 case DRM_FORMAT_RGBX4444:
2345 case DRM_FORMAT_BGRX4444:
2346 case DRM_FORMAT_ARGB4444:
2347 case DRM_FORMAT_ABGR4444:
2348 case DRM_FORMAT_RGBA4444:
2349 case DRM_FORMAT_BGRA4444:
2350 case DRM_FORMAT_XRGB1555:
2351 case DRM_FORMAT_XBGR1555:
2352 case DRM_FORMAT_RGBX5551:
2353 case DRM_FORMAT_BGRX5551:
2354 case DRM_FORMAT_ARGB1555:
2355 case DRM_FORMAT_ABGR1555:
2356 case DRM_FORMAT_RGBA5551:
2357 case DRM_FORMAT_BGRA5551:
2358 case DRM_FORMAT_RGB565:
2359 case DRM_FORMAT_BGR565:
2360 case DRM_FORMAT_RGB888:
2361 case DRM_FORMAT_BGR888:
2362 case DRM_FORMAT_XRGB8888:
2363 case DRM_FORMAT_XBGR8888:
2364 case DRM_FORMAT_RGBX8888:
2365 case DRM_FORMAT_BGRX8888:
2366 case DRM_FORMAT_ARGB8888:
2367 case DRM_FORMAT_ABGR8888:
2368 case DRM_FORMAT_RGBA8888:
2369 case DRM_FORMAT_BGRA8888:
2370 case DRM_FORMAT_XRGB2101010:
2371 case DRM_FORMAT_XBGR2101010:
2372 case DRM_FORMAT_RGBX1010102:
2373 case DRM_FORMAT_BGRX1010102:
2374 case DRM_FORMAT_ARGB2101010:
2375 case DRM_FORMAT_ABGR2101010:
2376 case DRM_FORMAT_RGBA1010102:
2377 case DRM_FORMAT_BGRA1010102:
2378 case DRM_FORMAT_YUYV:
2379 case DRM_FORMAT_YVYU:
2380 case DRM_FORMAT_UYVY:
2381 case DRM_FORMAT_VYUY:
2382 case DRM_FORMAT_AYUV:
2383 case DRM_FORMAT_NV12:
2384 case DRM_FORMAT_NV21:
2385 case DRM_FORMAT_NV16:
2386 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002387 case DRM_FORMAT_NV24:
2388 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002389 case DRM_FORMAT_YUV410:
2390 case DRM_FORMAT_YVU410:
2391 case DRM_FORMAT_YUV411:
2392 case DRM_FORMAT_YVU411:
2393 case DRM_FORMAT_YUV420:
2394 case DRM_FORMAT_YVU420:
2395 case DRM_FORMAT_YUV422:
2396 case DRM_FORMAT_YVU422:
2397 case DRM_FORMAT_YUV444:
2398 case DRM_FORMAT_YVU444:
2399 return 0;
2400 default:
2401 return -EINVAL;
2402 }
2403}
2404
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002405static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002406{
2407 int ret, hsub, vsub, num_planes, i;
2408
2409 ret = format_check(r);
2410 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002411 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2412 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002413 return ret;
2414 }
2415
2416 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2417 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2418 num_planes = drm_format_num_planes(r->pixel_format);
2419
2420 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002421 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002422 return -EINVAL;
2423 }
2424
2425 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002426 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002427 return -EINVAL;
2428 }
2429
2430 for (i = 0; i < num_planes; i++) {
2431 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002432 unsigned int height = r->height / (i != 0 ? vsub : 1);
2433 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002434
2435 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002436 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002437 return -EINVAL;
2438 }
2439
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002440 if ((uint64_t) width * cpp > UINT_MAX)
2441 return -ERANGE;
2442
2443 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2444 return -ERANGE;
2445
2446 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002447 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002448 return -EINVAL;
2449 }
2450 }
2451
2452 return 0;
2453}
2454
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002455/**
2456 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002457 * @dev: drm device for the ioctl
2458 * @data: data pointer for the ioctl
2459 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002460 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002461 * Add a new FB to the specified CRTC, given a user request with format.
2462 *
2463 * Called by the user via ioctl.
2464 *
2465 * RETURNS:
2466 * Zero on success, errno on failure.
2467 */
2468int drm_mode_addfb2(struct drm_device *dev,
2469 void *data, struct drm_file *file_priv)
2470{
2471 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002472 struct drm_mode_config *config = &dev->mode_config;
2473 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002474 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002475
Dave Airliefb3b06c2011-02-08 13:55:21 +10002476 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2477 return -EINVAL;
2478
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002479 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2480 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2481 return -EINVAL;
2482 }
2483
Dave Airlief453ba02008-11-07 14:05:41 -08002484 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002485 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002486 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002487 return -EINVAL;
2488 }
2489 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002490 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002491 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002492 return -EINVAL;
2493 }
2494
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002495 ret = framebuffer_check(r);
2496 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002497 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002498
Dave Airlief453ba02008-11-07 14:05:41 -08002499 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002500 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002501 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002502 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002503 }
2504
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002505 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002506 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002507 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002508 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002509 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002510
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002511
Dave Airlief453ba02008-11-07 14:05:41 -08002512 return ret;
2513}
2514
2515/**
2516 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002517 * @dev: drm device for the ioctl
2518 * @data: data pointer for the ioctl
2519 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002520 *
Dave Airlief453ba02008-11-07 14:05:41 -08002521 * Remove the FB specified by the user.
2522 *
2523 * Called by the user via ioctl.
2524 *
2525 * RETURNS:
2526 * Zero on success, errno on failure.
2527 */
2528int drm_mode_rmfb(struct drm_device *dev,
2529 void *data, struct drm_file *file_priv)
2530{
Dave Airlief453ba02008-11-07 14:05:41 -08002531 struct drm_framebuffer *fb = NULL;
2532 struct drm_framebuffer *fbl = NULL;
2533 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002534 int found = 0;
2535
Dave Airliefb3b06c2011-02-08 13:55:21 +10002536 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2537 return -EINVAL;
2538
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002539 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002540 mutex_lock(&dev->mode_config.fb_lock);
2541 fb = __drm_framebuffer_lookup(dev, *id);
2542 if (!fb)
2543 goto fail_lookup;
2544
Dave Airlief453ba02008-11-07 14:05:41 -08002545 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2546 if (fb == fbl)
2547 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002548 if (!found)
2549 goto fail_lookup;
2550
2551 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2552 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002553
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002554 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002555 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002556 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002557
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002558 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002559
Daniel Vetter2b677e82012-12-10 21:16:05 +01002560 return 0;
2561
2562fail_lookup:
2563 mutex_unlock(&dev->mode_config.fb_lock);
2564 mutex_unlock(&file_priv->fbs_lock);
2565
2566 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002567}
2568
2569/**
2570 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002571 * @dev: drm device for the ioctl
2572 * @data: data pointer for the ioctl
2573 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002574 *
Dave Airlief453ba02008-11-07 14:05:41 -08002575 * Lookup the FB given its ID and return info about it.
2576 *
2577 * Called by the user via ioctl.
2578 *
2579 * RETURNS:
2580 * Zero on success, errno on failure.
2581 */
2582int drm_mode_getfb(struct drm_device *dev,
2583 void *data, struct drm_file *file_priv)
2584{
2585 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002586 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002587 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002588
Dave Airliefb3b06c2011-02-08 13:55:21 +10002589 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2590 return -EINVAL;
2591
Daniel Vetter786b99e2012-12-02 21:53:40 +01002592 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002593 if (!fb)
2594 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002595
2596 r->height = fb->height;
2597 r->width = fb->width;
2598 r->depth = fb->depth;
2599 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002600 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002601 if (fb->funcs->create_handle)
2602 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2603 else
2604 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002605
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002606 drm_framebuffer_unreference(fb);
2607
Dave Airlief453ba02008-11-07 14:05:41 -08002608 return ret;
2609}
2610
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002611int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2612 void *data, struct drm_file *file_priv)
2613{
2614 struct drm_clip_rect __user *clips_ptr;
2615 struct drm_clip_rect *clips = NULL;
2616 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002617 struct drm_framebuffer *fb;
2618 unsigned flags;
2619 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002620 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002621
Dave Airliefb3b06c2011-02-08 13:55:21 +10002622 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2623 return -EINVAL;
2624
Daniel Vetter786b99e2012-12-02 21:53:40 +01002625 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002626 if (!fb)
2627 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002628
2629 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002630 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002631
2632 if (!num_clips != !clips_ptr) {
2633 ret = -EINVAL;
2634 goto out_err1;
2635 }
2636
2637 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2638
2639 /* If userspace annotates copy, clips must come in pairs */
2640 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2641 ret = -EINVAL;
2642 goto out_err1;
2643 }
2644
2645 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002646 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2647 ret = -EINVAL;
2648 goto out_err1;
2649 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002650 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2651 if (!clips) {
2652 ret = -ENOMEM;
2653 goto out_err1;
2654 }
2655
2656 ret = copy_from_user(clips, clips_ptr,
2657 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002658 if (ret) {
2659 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002660 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002661 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002662 }
2663
2664 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002665 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002666 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2667 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002668 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002669 } else {
2670 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002671 }
2672
2673out_err2:
2674 kfree(clips);
2675out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002676 drm_framebuffer_unreference(fb);
2677
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002678 return ret;
2679}
2680
2681
Dave Airlief453ba02008-11-07 14:05:41 -08002682/**
2683 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002684 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002685 *
Dave Airlief453ba02008-11-07 14:05:41 -08002686 * Destroy all the FBs associated with @filp.
2687 *
2688 * Called by the user via ioctl.
2689 *
2690 * RETURNS:
2691 * Zero on success, errno on failure.
2692 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002693void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002694{
Dave Airlief453ba02008-11-07 14:05:41 -08002695 struct drm_device *dev = priv->minor->dev;
2696 struct drm_framebuffer *fb, *tfb;
2697
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002698 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002699 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002700
2701 mutex_lock(&dev->mode_config.fb_lock);
2702 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2703 __drm_framebuffer_unregister(dev, fb);
2704 mutex_unlock(&dev->mode_config.fb_lock);
2705
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002706 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002707
2708 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002709 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002710 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002711 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002712}
2713
Dave Airlief453ba02008-11-07 14:05:41 -08002714struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2715 const char *name, int num_values)
2716{
2717 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002718 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002719
2720 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2721 if (!property)
2722 return NULL;
2723
2724 if (num_values) {
2725 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2726 if (!property->values)
2727 goto fail;
2728 }
2729
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002730 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2731 if (ret)
2732 goto fail;
2733
Dave Airlief453ba02008-11-07 14:05:41 -08002734 property->flags = flags;
2735 property->num_values = num_values;
2736 INIT_LIST_HEAD(&property->enum_blob_list);
2737
Vinson Lee471dd2e2011-11-10 11:55:40 -08002738 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002739 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002740 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2741 }
Dave Airlief453ba02008-11-07 14:05:41 -08002742
2743 list_add_tail(&property->head, &dev->mode_config.property_list);
2744 return property;
2745fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002746 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002747 kfree(property);
2748 return NULL;
2749}
2750EXPORT_SYMBOL(drm_property_create);
2751
Sascha Hauer4a67d392012-02-06 10:58:17 +01002752struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2753 const char *name,
2754 const struct drm_prop_enum_list *props,
2755 int num_values)
2756{
2757 struct drm_property *property;
2758 int i, ret;
2759
2760 flags |= DRM_MODE_PROP_ENUM;
2761
2762 property = drm_property_create(dev, flags, name, num_values);
2763 if (!property)
2764 return NULL;
2765
2766 for (i = 0; i < num_values; i++) {
2767 ret = drm_property_add_enum(property, i,
2768 props[i].type,
2769 props[i].name);
2770 if (ret) {
2771 drm_property_destroy(dev, property);
2772 return NULL;
2773 }
2774 }
2775
2776 return property;
2777}
2778EXPORT_SYMBOL(drm_property_create_enum);
2779
Rob Clark49e27542012-05-17 02:23:26 -06002780struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2781 int flags, const char *name,
2782 const struct drm_prop_enum_list *props,
2783 int num_values)
2784{
2785 struct drm_property *property;
2786 int i, ret;
2787
2788 flags |= DRM_MODE_PROP_BITMASK;
2789
2790 property = drm_property_create(dev, flags, name, num_values);
2791 if (!property)
2792 return NULL;
2793
2794 for (i = 0; i < num_values; i++) {
2795 ret = drm_property_add_enum(property, i,
2796 props[i].type,
2797 props[i].name);
2798 if (ret) {
2799 drm_property_destroy(dev, property);
2800 return NULL;
2801 }
2802 }
2803
2804 return property;
2805}
2806EXPORT_SYMBOL(drm_property_create_bitmask);
2807
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002808struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2809 const char *name,
2810 uint64_t min, uint64_t max)
2811{
2812 struct drm_property *property;
2813
2814 flags |= DRM_MODE_PROP_RANGE;
2815
2816 property = drm_property_create(dev, flags, name, 2);
2817 if (!property)
2818 return NULL;
2819
2820 property->values[0] = min;
2821 property->values[1] = max;
2822
2823 return property;
2824}
2825EXPORT_SYMBOL(drm_property_create_range);
2826
Dave Airlief453ba02008-11-07 14:05:41 -08002827int drm_property_add_enum(struct drm_property *property, int index,
2828 uint64_t value, const char *name)
2829{
2830 struct drm_property_enum *prop_enum;
2831
Rob Clark49e27542012-05-17 02:23:26 -06002832 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2833 return -EINVAL;
2834
2835 /*
2836 * Bitmask enum properties have the additional constraint of values
2837 * from 0 to 63
2838 */
2839 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002840 return -EINVAL;
2841
2842 if (!list_empty(&property->enum_blob_list)) {
2843 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2844 if (prop_enum->value == value) {
2845 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2846 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2847 return 0;
2848 }
2849 }
2850 }
2851
2852 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2853 if (!prop_enum)
2854 return -ENOMEM;
2855
2856 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2857 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2858 prop_enum->value = value;
2859
2860 property->values[index] = value;
2861 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2862 return 0;
2863}
2864EXPORT_SYMBOL(drm_property_add_enum);
2865
2866void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2867{
2868 struct drm_property_enum *prop_enum, *pt;
2869
2870 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2871 list_del(&prop_enum->head);
2872 kfree(prop_enum);
2873 }
2874
2875 if (property->num_values)
2876 kfree(property->values);
2877 drm_mode_object_put(dev, &property->base);
2878 list_del(&property->head);
2879 kfree(property);
2880}
2881EXPORT_SYMBOL(drm_property_destroy);
2882
Paulo Zanonic5431882012-05-15 18:09:02 -03002883void drm_object_attach_property(struct drm_mode_object *obj,
2884 struct drm_property *property,
2885 uint64_t init_val)
2886{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002887 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002888
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002889 if (count == DRM_OBJECT_MAX_PROPERTY) {
2890 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2891 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2892 "you see this message on the same object type.\n",
2893 obj->type);
2894 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002895 }
2896
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002897 obj->properties->ids[count] = property->base.id;
2898 obj->properties->values[count] = init_val;
2899 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002900}
2901EXPORT_SYMBOL(drm_object_attach_property);
2902
2903int drm_object_property_set_value(struct drm_mode_object *obj,
2904 struct drm_property *property, uint64_t val)
2905{
2906 int i;
2907
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002908 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002909 if (obj->properties->ids[i] == property->base.id) {
2910 obj->properties->values[i] = val;
2911 return 0;
2912 }
2913 }
2914
2915 return -EINVAL;
2916}
2917EXPORT_SYMBOL(drm_object_property_set_value);
2918
2919int drm_object_property_get_value(struct drm_mode_object *obj,
2920 struct drm_property *property, uint64_t *val)
2921{
2922 int i;
2923
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002924 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002925 if (obj->properties->ids[i] == property->base.id) {
2926 *val = obj->properties->values[i];
2927 return 0;
2928 }
2929 }
2930
2931 return -EINVAL;
2932}
2933EXPORT_SYMBOL(drm_object_property_get_value);
2934
Dave Airlief453ba02008-11-07 14:05:41 -08002935int drm_mode_getproperty_ioctl(struct drm_device *dev,
2936 void *data, struct drm_file *file_priv)
2937{
2938 struct drm_mode_object *obj;
2939 struct drm_mode_get_property *out_resp = data;
2940 struct drm_property *property;
2941 int enum_count = 0;
2942 int blob_count = 0;
2943 int value_count = 0;
2944 int ret = 0, i;
2945 int copied;
2946 struct drm_property_enum *prop_enum;
2947 struct drm_mode_property_enum __user *enum_ptr;
2948 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002949 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002950 uint64_t __user *values_ptr;
2951 uint32_t __user *blob_length_ptr;
2952
Dave Airliefb3b06c2011-02-08 13:55:21 +10002953 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2954 return -EINVAL;
2955
Daniel Vetter84849902012-12-02 00:28:11 +01002956 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002957 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2958 if (!obj) {
2959 ret = -EINVAL;
2960 goto done;
2961 }
2962 property = obj_to_property(obj);
2963
Rob Clark49e27542012-05-17 02:23:26 -06002964 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002965 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2966 enum_count++;
2967 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2968 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2969 blob_count++;
2970 }
2971
2972 value_count = property->num_values;
2973
2974 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2975 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2976 out_resp->flags = property->flags;
2977
2978 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002979 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002980 for (i = 0; i < value_count; i++) {
2981 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2982 ret = -EFAULT;
2983 goto done;
2984 }
2985 }
2986 }
2987 out_resp->count_values = value_count;
2988
Rob Clark49e27542012-05-17 02:23:26 -06002989 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002990 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2991 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002992 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002993 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2994
2995 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2996 ret = -EFAULT;
2997 goto done;
2998 }
2999
3000 if (copy_to_user(&enum_ptr[copied].name,
3001 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3002 ret = -EFAULT;
3003 goto done;
3004 }
3005 copied++;
3006 }
3007 }
3008 out_resp->count_enum_blobs = enum_count;
3009 }
3010
3011 if (property->flags & DRM_MODE_PROP_BLOB) {
3012 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3013 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003014 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3015 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003016
3017 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3018 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3019 ret = -EFAULT;
3020 goto done;
3021 }
3022
3023 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3024 ret = -EFAULT;
3025 goto done;
3026 }
3027
3028 copied++;
3029 }
3030 }
3031 out_resp->count_enum_blobs = blob_count;
3032 }
3033done:
Daniel Vetter84849902012-12-02 00:28:11 +01003034 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003035 return ret;
3036}
3037
3038static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3039 void *data)
3040{
3041 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003042 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003043
3044 if (!length || !data)
3045 return NULL;
3046
3047 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3048 if (!blob)
3049 return NULL;
3050
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003051 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3052 if (ret) {
3053 kfree(blob);
3054 return NULL;
3055 }
3056
Dave Airlief453ba02008-11-07 14:05:41 -08003057 blob->length = length;
3058
3059 memcpy(blob->data, data, length);
3060
Dave Airlief453ba02008-11-07 14:05:41 -08003061 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3062 return blob;
3063}
3064
3065static void drm_property_destroy_blob(struct drm_device *dev,
3066 struct drm_property_blob *blob)
3067{
3068 drm_mode_object_put(dev, &blob->base);
3069 list_del(&blob->head);
3070 kfree(blob);
3071}
3072
3073int drm_mode_getblob_ioctl(struct drm_device *dev,
3074 void *data, struct drm_file *file_priv)
3075{
3076 struct drm_mode_object *obj;
3077 struct drm_mode_get_blob *out_resp = data;
3078 struct drm_property_blob *blob;
3079 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003080 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003081
Dave Airliefb3b06c2011-02-08 13:55:21 +10003082 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3083 return -EINVAL;
3084
Daniel Vetter84849902012-12-02 00:28:11 +01003085 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003086 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3087 if (!obj) {
3088 ret = -EINVAL;
3089 goto done;
3090 }
3091 blob = obj_to_blob(obj);
3092
3093 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003094 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003095 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3096 ret = -EFAULT;
3097 goto done;
3098 }
3099 }
3100 out_resp->length = blob->length;
3101
3102done:
Daniel Vetter84849902012-12-02 00:28:11 +01003103 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003104 return ret;
3105}
3106
3107int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3108 struct edid *edid)
3109{
3110 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003111 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003112
3113 if (connector->edid_blob_ptr)
3114 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3115
3116 /* Delete edid, when there is none. */
3117 if (!edid) {
3118 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003119 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003120 return ret;
3121 }
3122
Adam Jackson7466f4c2010-03-29 21:43:23 +00003123 size = EDID_LENGTH * (1 + edid->extensions);
3124 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3125 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003126 if (!connector->edid_blob_ptr)
3127 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003128
Rob Clark58495562012-10-11 20:50:56 -05003129 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003130 dev->mode_config.edid_property,
3131 connector->edid_blob_ptr->base.id);
3132
3133 return ret;
3134}
3135EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3136
Paulo Zanoni26a34812012-05-15 18:08:59 -03003137static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003138 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003139{
3140 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3141 return false;
3142 if (property->flags & DRM_MODE_PROP_RANGE) {
3143 if (value < property->values[0] || value > property->values[1])
3144 return false;
3145 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003146 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3147 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003148 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003149 for (i = 0; i < property->num_values; i++)
3150 valid_mask |= (1ULL << property->values[i]);
3151 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003152 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3153 /* Only the driver knows */
3154 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003155 } else {
3156 int i;
3157 for (i = 0; i < property->num_values; i++)
3158 if (property->values[i] == value)
3159 return true;
3160 return false;
3161 }
3162}
3163
Dave Airlief453ba02008-11-07 14:05:41 -08003164int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3165 void *data, struct drm_file *file_priv)
3166{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003167 struct drm_mode_connector_set_property *conn_set_prop = data;
3168 struct drm_mode_obj_set_property obj_set_prop = {
3169 .value = conn_set_prop->value,
3170 .prop_id = conn_set_prop->prop_id,
3171 .obj_id = conn_set_prop->connector_id,
3172 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3173 };
Dave Airlief453ba02008-11-07 14:05:41 -08003174
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003175 /* It does all the locking and checking we need */
3176 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003177}
3178
Paulo Zanonic5431882012-05-15 18:09:02 -03003179static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3180 struct drm_property *property,
3181 uint64_t value)
3182{
3183 int ret = -EINVAL;
3184 struct drm_connector *connector = obj_to_connector(obj);
3185
3186 /* Do DPMS ourselves */
3187 if (property == connector->dev->mode_config.dpms_property) {
3188 if (connector->funcs->dpms)
3189 (*connector->funcs->dpms)(connector, (int)value);
3190 ret = 0;
3191 } else if (connector->funcs->set_property)
3192 ret = connector->funcs->set_property(connector, property, value);
3193
3194 /* store the property value if successful */
3195 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003196 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003197 return ret;
3198}
3199
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003200static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3201 struct drm_property *property,
3202 uint64_t value)
3203{
3204 int ret = -EINVAL;
3205 struct drm_crtc *crtc = obj_to_crtc(obj);
3206
3207 if (crtc->funcs->set_property)
3208 ret = crtc->funcs->set_property(crtc, property, value);
3209 if (!ret)
3210 drm_object_property_set_value(obj, property, value);
3211
3212 return ret;
3213}
3214
Rob Clark4d939142012-05-17 02:23:27 -06003215static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3216 struct drm_property *property,
3217 uint64_t value)
3218{
3219 int ret = -EINVAL;
3220 struct drm_plane *plane = obj_to_plane(obj);
3221
3222 if (plane->funcs->set_property)
3223 ret = plane->funcs->set_property(plane, property, value);
3224 if (!ret)
3225 drm_object_property_set_value(obj, property, value);
3226
3227 return ret;
3228}
3229
Paulo Zanonic5431882012-05-15 18:09:02 -03003230int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3231 struct drm_file *file_priv)
3232{
3233 struct drm_mode_obj_get_properties *arg = data;
3234 struct drm_mode_object *obj;
3235 int ret = 0;
3236 int i;
3237 int copied = 0;
3238 int props_count = 0;
3239 uint32_t __user *props_ptr;
3240 uint64_t __user *prop_values_ptr;
3241
3242 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3243 return -EINVAL;
3244
Daniel Vetter84849902012-12-02 00:28:11 +01003245 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003246
3247 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3248 if (!obj) {
3249 ret = -EINVAL;
3250 goto out;
3251 }
3252 if (!obj->properties) {
3253 ret = -EINVAL;
3254 goto out;
3255 }
3256
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003257 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003258
3259 /* This ioctl is called twice, once to determine how much space is
3260 * needed, and the 2nd time to fill it. */
3261 if ((arg->count_props >= props_count) && props_count) {
3262 copied = 0;
3263 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3264 prop_values_ptr = (uint64_t __user *)(unsigned long)
3265 (arg->prop_values_ptr);
3266 for (i = 0; i < props_count; i++) {
3267 if (put_user(obj->properties->ids[i],
3268 props_ptr + copied)) {
3269 ret = -EFAULT;
3270 goto out;
3271 }
3272 if (put_user(obj->properties->values[i],
3273 prop_values_ptr + copied)) {
3274 ret = -EFAULT;
3275 goto out;
3276 }
3277 copied++;
3278 }
3279 }
3280 arg->count_props = props_count;
3281out:
Daniel Vetter84849902012-12-02 00:28:11 +01003282 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003283 return ret;
3284}
3285
3286int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3287 struct drm_file *file_priv)
3288{
3289 struct drm_mode_obj_set_property *arg = data;
3290 struct drm_mode_object *arg_obj;
3291 struct drm_mode_object *prop_obj;
3292 struct drm_property *property;
3293 int ret = -EINVAL;
3294 int i;
3295
3296 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3297 return -EINVAL;
3298
Daniel Vetter84849902012-12-02 00:28:11 +01003299 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003300
3301 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3302 if (!arg_obj)
3303 goto out;
3304 if (!arg_obj->properties)
3305 goto out;
3306
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003307 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003308 if (arg_obj->properties->ids[i] == arg->prop_id)
3309 break;
3310
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003311 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003312 goto out;
3313
3314 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3315 DRM_MODE_OBJECT_PROPERTY);
3316 if (!prop_obj)
3317 goto out;
3318 property = obj_to_property(prop_obj);
3319
3320 if (!drm_property_change_is_valid(property, arg->value))
3321 goto out;
3322
3323 switch (arg_obj->type) {
3324 case DRM_MODE_OBJECT_CONNECTOR:
3325 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3326 arg->value);
3327 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003328 case DRM_MODE_OBJECT_CRTC:
3329 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3330 break;
Rob Clark4d939142012-05-17 02:23:27 -06003331 case DRM_MODE_OBJECT_PLANE:
3332 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3333 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003334 }
3335
3336out:
Daniel Vetter84849902012-12-02 00:28:11 +01003337 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003338 return ret;
3339}
3340
Dave Airlief453ba02008-11-07 14:05:41 -08003341int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3342 struct drm_encoder *encoder)
3343{
3344 int i;
3345
3346 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3347 if (connector->encoder_ids[i] == 0) {
3348 connector->encoder_ids[i] = encoder->base.id;
3349 return 0;
3350 }
3351 }
3352 return -ENOMEM;
3353}
3354EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3355
3356void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3357 struct drm_encoder *encoder)
3358{
3359 int i;
3360 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3361 if (connector->encoder_ids[i] == encoder->base.id) {
3362 connector->encoder_ids[i] = 0;
3363 if (connector->encoder == encoder)
3364 connector->encoder = NULL;
3365 break;
3366 }
3367 }
3368}
3369EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3370
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003371int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003372 int gamma_size)
3373{
3374 crtc->gamma_size = gamma_size;
3375
3376 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3377 if (!crtc->gamma_store) {
3378 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003379 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003380 }
3381
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003382 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003383}
3384EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3385
3386int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3387 void *data, struct drm_file *file_priv)
3388{
3389 struct drm_mode_crtc_lut *crtc_lut = data;
3390 struct drm_mode_object *obj;
3391 struct drm_crtc *crtc;
3392 void *r_base, *g_base, *b_base;
3393 int size;
3394 int ret = 0;
3395
Dave Airliefb3b06c2011-02-08 13:55:21 +10003396 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3397 return -EINVAL;
3398
Daniel Vetter84849902012-12-02 00:28:11 +01003399 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003400 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3401 if (!obj) {
3402 ret = -EINVAL;
3403 goto out;
3404 }
3405 crtc = obj_to_crtc(obj);
3406
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003407 if (crtc->funcs->gamma_set == NULL) {
3408 ret = -ENOSYS;
3409 goto out;
3410 }
3411
Dave Airlief453ba02008-11-07 14:05:41 -08003412 /* memcpy into gamma store */
3413 if (crtc_lut->gamma_size != crtc->gamma_size) {
3414 ret = -EINVAL;
3415 goto out;
3416 }
3417
3418 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3419 r_base = crtc->gamma_store;
3420 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3421 ret = -EFAULT;
3422 goto out;
3423 }
3424
3425 g_base = r_base + size;
3426 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3427 ret = -EFAULT;
3428 goto out;
3429 }
3430
3431 b_base = g_base + size;
3432 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3433 ret = -EFAULT;
3434 goto out;
3435 }
3436
James Simmons72034252010-08-03 01:33:19 +01003437 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003438
3439out:
Daniel Vetter84849902012-12-02 00:28:11 +01003440 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003441 return ret;
3442
3443}
3444
3445int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3446 void *data, struct drm_file *file_priv)
3447{
3448 struct drm_mode_crtc_lut *crtc_lut = data;
3449 struct drm_mode_object *obj;
3450 struct drm_crtc *crtc;
3451 void *r_base, *g_base, *b_base;
3452 int size;
3453 int ret = 0;
3454
Dave Airliefb3b06c2011-02-08 13:55:21 +10003455 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3456 return -EINVAL;
3457
Daniel Vetter84849902012-12-02 00:28:11 +01003458 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003459 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3460 if (!obj) {
3461 ret = -EINVAL;
3462 goto out;
3463 }
3464 crtc = obj_to_crtc(obj);
3465
3466 /* memcpy into gamma store */
3467 if (crtc_lut->gamma_size != crtc->gamma_size) {
3468 ret = -EINVAL;
3469 goto out;
3470 }
3471
3472 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3473 r_base = crtc->gamma_store;
3474 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3475 ret = -EFAULT;
3476 goto out;
3477 }
3478
3479 g_base = r_base + size;
3480 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3481 ret = -EFAULT;
3482 goto out;
3483 }
3484
3485 b_base = g_base + size;
3486 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3487 ret = -EFAULT;
3488 goto out;
3489 }
3490out:
Daniel Vetter84849902012-12-02 00:28:11 +01003491 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003492 return ret;
3493}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003494
3495int drm_mode_page_flip_ioctl(struct drm_device *dev,
3496 void *data, struct drm_file *file_priv)
3497{
3498 struct drm_mode_crtc_page_flip *page_flip = data;
3499 struct drm_mode_object *obj;
3500 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003501 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003502 struct drm_pending_vblank_event *e = NULL;
3503 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003504 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003505 int ret = -EINVAL;
3506
3507 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3508 page_flip->reserved != 0)
3509 return -EINVAL;
3510
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003511 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3512 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003513 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003514 crtc = obj_to_crtc(obj);
3515
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003516 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003517 if (crtc->fb == NULL) {
3518 /* The framebuffer is currently unbound, presumably
3519 * due to a hotplug event, that userspace has not
3520 * yet discovered.
3521 */
3522 ret = -EBUSY;
3523 goto out;
3524 }
3525
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003526 if (crtc->funcs->page_flip == NULL)
3527 goto out;
3528
Daniel Vetter786b99e2012-12-02 21:53:40 +01003529 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3530 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003531 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003532
Rob Clark7c80e122012-09-04 16:35:56 +00003533 hdisplay = crtc->mode.hdisplay;
3534 vdisplay = crtc->mode.vdisplay;
3535
3536 if (crtc->invert_dimensions)
3537 swap(hdisplay, vdisplay);
3538
3539 if (hdisplay > fb->width ||
3540 vdisplay > fb->height ||
3541 crtc->x > fb->width - hdisplay ||
3542 crtc->y > fb->height - vdisplay) {
3543 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3544 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3545 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003546 ret = -ENOSPC;
3547 goto out;
3548 }
3549
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003550 if (crtc->fb->pixel_format != fb->pixel_format) {
3551 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3552 ret = -EINVAL;
3553 goto out;
3554 }
3555
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003556 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3557 ret = -ENOMEM;
3558 spin_lock_irqsave(&dev->event_lock, flags);
3559 if (file_priv->event_space < sizeof e->event) {
3560 spin_unlock_irqrestore(&dev->event_lock, flags);
3561 goto out;
3562 }
3563 file_priv->event_space -= sizeof e->event;
3564 spin_unlock_irqrestore(&dev->event_lock, flags);
3565
3566 e = kzalloc(sizeof *e, GFP_KERNEL);
3567 if (e == NULL) {
3568 spin_lock_irqsave(&dev->event_lock, flags);
3569 file_priv->event_space += sizeof e->event;
3570 spin_unlock_irqrestore(&dev->event_lock, flags);
3571 goto out;
3572 }
3573
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003574 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003575 e->event.base.length = sizeof e->event;
3576 e->event.user_data = page_flip->user_data;
3577 e->base.event = &e->event.base;
3578 e->base.file_priv = file_priv;
3579 e->base.destroy =
3580 (void (*) (struct drm_pending_event *)) kfree;
3581 }
3582
Daniel Vetterb0d12322012-12-11 01:07:12 +01003583 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003584 ret = crtc->funcs->page_flip(crtc, fb, e);
3585 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003586 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3587 spin_lock_irqsave(&dev->event_lock, flags);
3588 file_priv->event_space += sizeof e->event;
3589 spin_unlock_irqrestore(&dev->event_lock, flags);
3590 kfree(e);
3591 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003592 /* Keep the old fb, don't unref it. */
3593 old_fb = NULL;
3594 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003595 /*
3596 * Warn if the driver hasn't properly updated the crtc->fb
3597 * field to reflect that the new framebuffer is now used.
3598 * Failing to do so will screw with the reference counting
3599 * on framebuffers.
3600 */
3601 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003602 /* Unref only the old framebuffer. */
3603 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003604 }
3605
3606out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003607 if (fb)
3608 drm_framebuffer_unreference(fb);
3609 if (old_fb)
3610 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003611 mutex_unlock(&crtc->mutex);
3612
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003613 return ret;
3614}
Chris Wilsoneb033552011-01-24 15:11:08 +00003615
3616void drm_mode_config_reset(struct drm_device *dev)
3617{
3618 struct drm_crtc *crtc;
3619 struct drm_encoder *encoder;
3620 struct drm_connector *connector;
3621
3622 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3623 if (crtc->funcs->reset)
3624 crtc->funcs->reset(crtc);
3625
3626 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3627 if (encoder->funcs->reset)
3628 encoder->funcs->reset(encoder);
3629
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003630 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3631 connector->status = connector_status_unknown;
3632
Chris Wilsoneb033552011-01-24 15:11:08 +00003633 if (connector->funcs->reset)
3634 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003635 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003636}
3637EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003638
3639int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3640 void *data, struct drm_file *file_priv)
3641{
3642 struct drm_mode_create_dumb *args = data;
3643
3644 if (!dev->driver->dumb_create)
3645 return -ENOSYS;
3646 return dev->driver->dumb_create(file_priv, dev, args);
3647}
3648
3649int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3650 void *data, struct drm_file *file_priv)
3651{
3652 struct drm_mode_map_dumb *args = data;
3653
3654 /* call driver ioctl to get mmap offset */
3655 if (!dev->driver->dumb_map_offset)
3656 return -ENOSYS;
3657
3658 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3659}
3660
3661int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3662 void *data, struct drm_file *file_priv)
3663{
3664 struct drm_mode_destroy_dumb *args = data;
3665
3666 if (!dev->driver->dumb_destroy)
3667 return -ENOSYS;
3668
3669 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3670}
Dave Airlie248dbc22011-11-29 20:02:54 +00003671
3672/*
3673 * Just need to support RGB formats here for compat with code that doesn't
3674 * use pixel formats directly yet.
3675 */
3676void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3677 int *bpp)
3678{
3679 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003680 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003681 case DRM_FORMAT_RGB332:
3682 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003683 *depth = 8;
3684 *bpp = 8;
3685 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003686 case DRM_FORMAT_XRGB1555:
3687 case DRM_FORMAT_XBGR1555:
3688 case DRM_FORMAT_RGBX5551:
3689 case DRM_FORMAT_BGRX5551:
3690 case DRM_FORMAT_ARGB1555:
3691 case DRM_FORMAT_ABGR1555:
3692 case DRM_FORMAT_RGBA5551:
3693 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003694 *depth = 15;
3695 *bpp = 16;
3696 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003697 case DRM_FORMAT_RGB565:
3698 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003699 *depth = 16;
3700 *bpp = 16;
3701 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003702 case DRM_FORMAT_RGB888:
3703 case DRM_FORMAT_BGR888:
3704 *depth = 24;
3705 *bpp = 24;
3706 break;
3707 case DRM_FORMAT_XRGB8888:
3708 case DRM_FORMAT_XBGR8888:
3709 case DRM_FORMAT_RGBX8888:
3710 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003711 *depth = 24;
3712 *bpp = 32;
3713 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003714 case DRM_FORMAT_XRGB2101010:
3715 case DRM_FORMAT_XBGR2101010:
3716 case DRM_FORMAT_RGBX1010102:
3717 case DRM_FORMAT_BGRX1010102:
3718 case DRM_FORMAT_ARGB2101010:
3719 case DRM_FORMAT_ABGR2101010:
3720 case DRM_FORMAT_RGBA1010102:
3721 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003722 *depth = 30;
3723 *bpp = 32;
3724 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003725 case DRM_FORMAT_ARGB8888:
3726 case DRM_FORMAT_ABGR8888:
3727 case DRM_FORMAT_RGBA8888:
3728 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003729 *depth = 32;
3730 *bpp = 32;
3731 break;
3732 default:
3733 DRM_DEBUG_KMS("unsupported pixel format\n");
3734 *depth = 0;
3735 *bpp = 0;
3736 break;
3737 }
3738}
3739EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003740
3741/**
3742 * drm_format_num_planes - get the number of planes for format
3743 * @format: pixel format (DRM_FORMAT_*)
3744 *
3745 * RETURNS:
3746 * The number of planes used by the specified pixel format.
3747 */
3748int drm_format_num_planes(uint32_t format)
3749{
3750 switch (format) {
3751 case DRM_FORMAT_YUV410:
3752 case DRM_FORMAT_YVU410:
3753 case DRM_FORMAT_YUV411:
3754 case DRM_FORMAT_YVU411:
3755 case DRM_FORMAT_YUV420:
3756 case DRM_FORMAT_YVU420:
3757 case DRM_FORMAT_YUV422:
3758 case DRM_FORMAT_YVU422:
3759 case DRM_FORMAT_YUV444:
3760 case DRM_FORMAT_YVU444:
3761 return 3;
3762 case DRM_FORMAT_NV12:
3763 case DRM_FORMAT_NV21:
3764 case DRM_FORMAT_NV16:
3765 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003766 case DRM_FORMAT_NV24:
3767 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003768 return 2;
3769 default:
3770 return 1;
3771 }
3772}
3773EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003774
3775/**
3776 * drm_format_plane_cpp - determine the bytes per pixel value
3777 * @format: pixel format (DRM_FORMAT_*)
3778 * @plane: plane index
3779 *
3780 * RETURNS:
3781 * The bytes per pixel value for the specified plane.
3782 */
3783int drm_format_plane_cpp(uint32_t format, int plane)
3784{
3785 unsigned int depth;
3786 int bpp;
3787
3788 if (plane >= drm_format_num_planes(format))
3789 return 0;
3790
3791 switch (format) {
3792 case DRM_FORMAT_YUYV:
3793 case DRM_FORMAT_YVYU:
3794 case DRM_FORMAT_UYVY:
3795 case DRM_FORMAT_VYUY:
3796 return 2;
3797 case DRM_FORMAT_NV12:
3798 case DRM_FORMAT_NV21:
3799 case DRM_FORMAT_NV16:
3800 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003801 case DRM_FORMAT_NV24:
3802 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003803 return plane ? 2 : 1;
3804 case DRM_FORMAT_YUV410:
3805 case DRM_FORMAT_YVU410:
3806 case DRM_FORMAT_YUV411:
3807 case DRM_FORMAT_YVU411:
3808 case DRM_FORMAT_YUV420:
3809 case DRM_FORMAT_YVU420:
3810 case DRM_FORMAT_YUV422:
3811 case DRM_FORMAT_YVU422:
3812 case DRM_FORMAT_YUV444:
3813 case DRM_FORMAT_YVU444:
3814 return 1;
3815 default:
3816 drm_fb_get_bpp_depth(format, &depth, &bpp);
3817 return bpp >> 3;
3818 }
3819}
3820EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003821
3822/**
3823 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3824 * @format: pixel format (DRM_FORMAT_*)
3825 *
3826 * RETURNS:
3827 * The horizontal chroma subsampling factor for the
3828 * specified pixel format.
3829 */
3830int drm_format_horz_chroma_subsampling(uint32_t format)
3831{
3832 switch (format) {
3833 case DRM_FORMAT_YUV411:
3834 case DRM_FORMAT_YVU411:
3835 case DRM_FORMAT_YUV410:
3836 case DRM_FORMAT_YVU410:
3837 return 4;
3838 case DRM_FORMAT_YUYV:
3839 case DRM_FORMAT_YVYU:
3840 case DRM_FORMAT_UYVY:
3841 case DRM_FORMAT_VYUY:
3842 case DRM_FORMAT_NV12:
3843 case DRM_FORMAT_NV21:
3844 case DRM_FORMAT_NV16:
3845 case DRM_FORMAT_NV61:
3846 case DRM_FORMAT_YUV422:
3847 case DRM_FORMAT_YVU422:
3848 case DRM_FORMAT_YUV420:
3849 case DRM_FORMAT_YVU420:
3850 return 2;
3851 default:
3852 return 1;
3853 }
3854}
3855EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3856
3857/**
3858 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3859 * @format: pixel format (DRM_FORMAT_*)
3860 *
3861 * RETURNS:
3862 * The vertical chroma subsampling factor for the
3863 * specified pixel format.
3864 */
3865int drm_format_vert_chroma_subsampling(uint32_t format)
3866{
3867 switch (format) {
3868 case DRM_FORMAT_YUV410:
3869 case DRM_FORMAT_YVU410:
3870 return 4;
3871 case DRM_FORMAT_YUV420:
3872 case DRM_FORMAT_YVU420:
3873 case DRM_FORMAT_NV12:
3874 case DRM_FORMAT_NV21:
3875 return 2;
3876 default:
3877 return 1;
3878 }
3879}
3880EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003881
3882/**
3883 * drm_mode_config_init - initialize DRM mode_configuration structure
3884 * @dev: DRM device
3885 *
3886 * Initialize @dev's mode_config structure, used for tracking the graphics
3887 * configuration of @dev.
3888 *
3889 * Since this initializes the modeset locks, no locking is possible. Which is no
3890 * problem, since this should happen single threaded at init time. It is the
3891 * driver's problem to ensure this guarantee.
3892 *
3893 */
3894void drm_mode_config_init(struct drm_device *dev)
3895{
3896 mutex_init(&dev->mode_config.mutex);
3897 mutex_init(&dev->mode_config.idr_mutex);
3898 mutex_init(&dev->mode_config.fb_lock);
3899 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3900 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3901 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3902 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3903 INIT_LIST_HEAD(&dev->mode_config.property_list);
3904 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3905 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3906 idr_init(&dev->mode_config.crtc_idr);
3907
3908 drm_modeset_lock_all(dev);
3909 drm_mode_create_standard_connector_properties(dev);
3910 drm_modeset_unlock_all(dev);
3911
3912 /* Just to be sure */
3913 dev->mode_config.num_fb = 0;
3914 dev->mode_config.num_connector = 0;
3915 dev->mode_config.num_crtc = 0;
3916 dev->mode_config.num_encoder = 0;
3917}
3918EXPORT_SYMBOL(drm_mode_config_init);
3919
3920/**
3921 * drm_mode_config_cleanup - free up DRM mode_config info
3922 * @dev: DRM device
3923 *
3924 * Free up all the connectors and CRTCs associated with this DRM device, then
3925 * free up the framebuffers and associated buffer objects.
3926 *
3927 * Note that since this /should/ happen single-threaded at driver/device
3928 * teardown time, no locking is required. It's the driver's job to ensure that
3929 * this guarantee actually holds true.
3930 *
3931 * FIXME: cleanup any dangling user buffer objects too
3932 */
3933void drm_mode_config_cleanup(struct drm_device *dev)
3934{
3935 struct drm_connector *connector, *ot;
3936 struct drm_crtc *crtc, *ct;
3937 struct drm_encoder *encoder, *enct;
3938 struct drm_framebuffer *fb, *fbt;
3939 struct drm_property *property, *pt;
3940 struct drm_property_blob *blob, *bt;
3941 struct drm_plane *plane, *plt;
3942
3943 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3944 head) {
3945 encoder->funcs->destroy(encoder);
3946 }
3947
3948 list_for_each_entry_safe(connector, ot,
3949 &dev->mode_config.connector_list, head) {
3950 connector->funcs->destroy(connector);
3951 }
3952
3953 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3954 head) {
3955 drm_property_destroy(dev, property);
3956 }
3957
3958 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3959 head) {
3960 drm_property_destroy_blob(dev, blob);
3961 }
3962
3963 /*
3964 * Single-threaded teardown context, so it's not required to grab the
3965 * fb_lock to protect against concurrent fb_list access. Contrary, it
3966 * would actually deadlock with the drm_framebuffer_cleanup function.
3967 *
3968 * Also, if there are any framebuffers left, that's a driver leak now,
3969 * so politely WARN about this.
3970 */
3971 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3972 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3973 drm_framebuffer_remove(fb);
3974 }
3975
3976 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3977 head) {
3978 plane->funcs->destroy(plane);
3979 }
3980
3981 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3982 crtc->funcs->destroy(crtc);
3983 }
3984
3985 idr_destroy(&dev->mode_config.crtc_idr);
3986}
3987EXPORT_SYMBOL(drm_mode_config_cleanup);