blob: 44c3421899bb5ab337dd281a7a66873242521377 [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
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000128static const struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800129{
130 { DRM_MODE_DITHERING_OFF, "Off" },
131 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +1000132 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -0800133};
134
135/*
136 * Non-global properties, but "required" for certain connectors.
137 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000138static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800139{
140 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
141 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
142 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
143};
144
145DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
146
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000147static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800148{
149 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
150 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
151 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
152};
153
154DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
155 drm_dvi_i_subconnector_enum_list)
156
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000157static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800158{
159 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
160 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
161 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
162 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200163 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800164};
165
166DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
167
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000168static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800169{
170 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
171 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
172 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
173 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200174 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800175};
176
177DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
178 drm_tv_subconnector_enum_list)
179
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000180static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000181 { DRM_MODE_DIRTY_OFF, "Off" },
182 { DRM_MODE_DIRTY_ON, "On" },
183 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
184};
185
Dave Airlief453ba02008-11-07 14:05:41 -0800186struct drm_conn_prop_enum_list {
187 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000188 const char *name;
Dave Airlief453ba02008-11-07 14:05:41 -0800189 int count;
190};
191
192/*
193 * Connector and encoder types.
194 */
195static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
196{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
197 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
198 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
199 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
200 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
201 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
202 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
203 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
204 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500205 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
206 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
207 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
208 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200209 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500210 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200211 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800212};
213
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000214static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800215{ { DRM_MODE_ENCODER_NONE, "None" },
216 { DRM_MODE_ENCODER_DAC, "DAC" },
217 { DRM_MODE_ENCODER_TMDS, "TMDS" },
218 { DRM_MODE_ENCODER_LVDS, "LVDS" },
219 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200220 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800221};
222
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000223const char *drm_get_encoder_name(const struct drm_encoder *encoder)
Dave Airlief453ba02008-11-07 14:05:41 -0800224{
225 static char buf[32];
226
227 snprintf(buf, 32, "%s-%d",
228 drm_encoder_enum_list[encoder->encoder_type].name,
229 encoder->base.id);
230 return buf;
231}
Dave Airlie13a81952009-09-07 15:45:33 +1000232EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800233
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000234const char *drm_get_connector_name(const struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -0800235{
236 static char buf[32];
237
238 snprintf(buf, 32, "%s-%d",
239 drm_connector_enum_list[connector->connector_type].name,
240 connector->connector_type_id);
241 return buf;
242}
243EXPORT_SYMBOL(drm_get_connector_name);
244
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000245const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800246{
247 if (status == connector_status_connected)
248 return "connected";
249 else if (status == connector_status_disconnected)
250 return "disconnected";
251 else
252 return "unknown";
253}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000254EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800255
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300256static char printable_char(int c)
257{
258 return isascii(c) && isprint(c) ? c : '?';
259}
260
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000261const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300262{
263 static char buf[32];
264
265 snprintf(buf, sizeof(buf),
266 "%c%c%c%c %s-endian (0x%08x)",
267 printable_char(format & 0xff),
268 printable_char((format >> 8) & 0xff),
269 printable_char((format >> 16) & 0xff),
270 printable_char((format >> 24) & 0x7f),
271 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
272 format);
273
274 return buf;
275}
276EXPORT_SYMBOL(drm_get_format_name);
277
Dave Airlief453ba02008-11-07 14:05:41 -0800278/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100279 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800280 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100281 * @obj: object pointer, used to generate unique ID
282 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800283 *
Dave Airlief453ba02008-11-07 14:05:41 -0800284 * Create a unique identifier based on @ptr in @dev's identifier space. Used
285 * for tracking modes, CRTCs and connectors.
286 *
287 * RETURNS:
288 * New unique (relative to other objects in @dev) integer identifier for the
289 * object.
290 */
291static int drm_mode_object_get(struct drm_device *dev,
292 struct drm_mode_object *obj, uint32_t obj_type)
293{
Dave Airlief453ba02008-11-07 14:05:41 -0800294 int ret;
295
Jesse Barnesad2563c2009-01-19 17:21:45 +1000296 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800297 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
298 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100299 /*
300 * Set up the object linking under the protection of the idr
301 * lock so that other users can't see inconsistent state.
302 */
Tejun Heo2e928812013-02-27 17:04:08 -0800303 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100304 obj->type = obj_type;
305 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000306 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100307
Tejun Heo2e928812013-02-27 17:04:08 -0800308 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800309}
310
311/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100312 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800313 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100314 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800315 *
Dave Airlief453ba02008-11-07 14:05:41 -0800316 * Free @id from @dev's unique identifier pool.
317 */
318static void drm_mode_object_put(struct drm_device *dev,
319 struct drm_mode_object *object)
320{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000321 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800322 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000323 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800324}
325
Daniel Vetter786b99e2012-12-02 21:53:40 +0100326/**
327 * drm_mode_object_find - look up a drm object with static lifetime
328 * @dev: drm device
329 * @id: id of the mode object
330 * @type: type of the mode object
331 *
332 * Note that framebuffers cannot be looked up with this functions - since those
333 * are reference counted, they need special treatment.
334 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200335struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
336 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800337{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000338 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800339
Daniel Vetter786b99e2012-12-02 21:53:40 +0100340 /* Framebuffers are reference counted and need their own lookup
341 * function.*/
342 WARN_ON(type == DRM_MODE_OBJECT_FB);
343
Jesse Barnesad2563c2009-01-19 17:21:45 +1000344 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800345 obj = idr_find(&dev->mode_config.crtc_idr, id);
346 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000347 obj = NULL;
348 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800349
350 return obj;
351}
352EXPORT_SYMBOL(drm_mode_object_find);
353
354/**
Dave Airlief453ba02008-11-07 14:05:41 -0800355 * drm_framebuffer_init - initialize a framebuffer
356 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100357 * @fb: framebuffer to be initialized
358 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800359 *
Dave Airlief453ba02008-11-07 14:05:41 -0800360 * Allocates an ID for the framebuffer's parent mode object, sets its mode
361 * functions & device file and adds it to the master fd list.
362 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100363 * IMPORTANT:
364 * This functions publishes the fb and makes it available for concurrent access
365 * by other users. Which means by this point the fb _must_ be fully set up -
366 * since all the fb attributes are invariant over its lifetime, no further
367 * locking but only correct reference counting is required.
368 *
Dave Airlief453ba02008-11-07 14:05:41 -0800369 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200370 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800371 */
372int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
373 const struct drm_framebuffer_funcs *funcs)
374{
375 int ret;
376
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100377 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000378 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100379 INIT_LIST_HEAD(&fb->filp_head);
380 fb->dev = dev;
381 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000382
Dave Airlief453ba02008-11-07 14:05:41 -0800383 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200384 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100385 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800386
Daniel Vetter2b677e82012-12-10 21:16:05 +0100387 /* Grab the idr reference. */
388 drm_framebuffer_reference(fb);
389
Dave Airlief453ba02008-11-07 14:05:41 -0800390 dev->mode_config.num_fb++;
391 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100392out:
393 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800394
395 return 0;
396}
397EXPORT_SYMBOL(drm_framebuffer_init);
398
Rob Clarkf7eff602012-09-05 21:48:38 +0000399static void drm_framebuffer_free(struct kref *kref)
400{
401 struct drm_framebuffer *fb =
402 container_of(kref, struct drm_framebuffer, refcount);
403 fb->funcs->destroy(fb);
404}
405
Daniel Vetter2b677e82012-12-10 21:16:05 +0100406static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
407 uint32_t id)
408{
409 struct drm_mode_object *obj = NULL;
410 struct drm_framebuffer *fb;
411
412 mutex_lock(&dev->mode_config.idr_mutex);
413 obj = idr_find(&dev->mode_config.crtc_idr, id);
414 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
415 fb = NULL;
416 else
417 fb = obj_to_fb(obj);
418 mutex_unlock(&dev->mode_config.idr_mutex);
419
420 return fb;
421}
422
Rob Clarkf7eff602012-09-05 21:48:38 +0000423/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100424 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
425 * @dev: drm device
426 * @id: id of the fb object
427 *
428 * If successful, this grabs an additional reference to the framebuffer -
429 * callers need to make sure to eventually unreference the returned framebuffer
430 * again.
431 */
432struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
433 uint32_t id)
434{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100435 struct drm_framebuffer *fb;
436
437 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100438 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100439 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000440 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100441 mutex_unlock(&dev->mode_config.fb_lock);
442
443 return fb;
444}
445EXPORT_SYMBOL(drm_framebuffer_lookup);
446
447/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000448 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100449 * @fb: framebuffer to unref
450 *
451 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000452 */
453void drm_framebuffer_unreference(struct drm_framebuffer *fb)
454{
Rob Clarkf7eff602012-09-05 21:48:38 +0000455 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000456 kref_put(&fb->refcount, drm_framebuffer_free);
457}
458EXPORT_SYMBOL(drm_framebuffer_unreference);
459
460/**
461 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100462 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000463 */
464void drm_framebuffer_reference(struct drm_framebuffer *fb)
465{
466 DRM_DEBUG("FB ID: %d\n", fb->base.id);
467 kref_get(&fb->refcount);
468}
469EXPORT_SYMBOL(drm_framebuffer_reference);
470
Daniel Vetter2b677e82012-12-10 21:16:05 +0100471static void drm_framebuffer_free_bug(struct kref *kref)
472{
473 BUG();
474}
475
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100476static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
477{
478 DRM_DEBUG("FB ID: %d\n", fb->base.id);
479 kref_put(&fb->refcount, drm_framebuffer_free_bug);
480}
481
Daniel Vetter2b677e82012-12-10 21:16:05 +0100482/* dev->mode_config.fb_lock must be held! */
483static void __drm_framebuffer_unregister(struct drm_device *dev,
484 struct drm_framebuffer *fb)
485{
486 mutex_lock(&dev->mode_config.idr_mutex);
487 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
488 mutex_unlock(&dev->mode_config.idr_mutex);
489
490 fb->base.id = 0;
491
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100492 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100493}
494
Dave Airlief453ba02008-11-07 14:05:41 -0800495/**
Daniel Vetter36206362012-12-10 20:42:17 +0100496 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
497 * @fb: fb to unregister
498 *
499 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
500 * those used for fbdev. Note that the caller must hold a reference of it's own,
501 * i.e. the object may not be destroyed through this call (since it'll lead to a
502 * locking inversion).
503 */
504void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
505{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100506 struct drm_device *dev = fb->dev;
507
508 mutex_lock(&dev->mode_config.fb_lock);
509 /* Mark fb as reaped and drop idr ref. */
510 __drm_framebuffer_unregister(dev, fb);
511 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100512}
513EXPORT_SYMBOL(drm_framebuffer_unregister_private);
514
515/**
Dave Airlief453ba02008-11-07 14:05:41 -0800516 * drm_framebuffer_cleanup - remove a framebuffer object
517 * @fb: framebuffer to remove
518 *
Daniel Vetter36206362012-12-10 20:42:17 +0100519 * Cleanup references to a user-created framebuffer. This function is intended
520 * to be used from the drivers ->destroy callback.
521 *
522 * Note that this function does not remove the fb from active usuage - if it is
523 * still used anywhere, hilarity can ensue since userspace could call getfb on
524 * the id and get back -EINVAL. Obviously no concern at driver unload time.
525 *
526 * Also, the framebuffer will not be removed from the lookup idr - for
527 * user-created framebuffers this will happen in in the rmfb ioctl. For
528 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
529 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800530 */
531void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
532{
533 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100534
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100535 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000536 list_del(&fb->head);
537 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100538 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000539}
540EXPORT_SYMBOL(drm_framebuffer_cleanup);
541
542/**
543 * drm_framebuffer_remove - remove and unreference a framebuffer object
544 * @fb: framebuffer to remove
545 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000546 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100547 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100548 * passed-in framebuffer. Might take the modeset locks.
549 *
550 * Note that this function optimizes the cleanup away if the caller holds the
551 * last reference to the framebuffer. It is also guaranteed to not take the
552 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000553 */
554void drm_framebuffer_remove(struct drm_framebuffer *fb)
555{
556 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800557 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800558 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000559 struct drm_mode_set set;
560 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800561
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100562 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100563
Daniel Vetterb62584e2012-12-11 16:51:35 +0100564 /*
565 * drm ABI mandates that we remove any deleted framebuffers from active
566 * useage. But since most sane clients only remove framebuffers they no
567 * longer need, try to optimize this away.
568 *
569 * Since we're holding a reference ourselves, observing a refcount of 1
570 * means that we're the last holder and can skip it. Also, the refcount
571 * can never increase from 1 again, so we don't need any barriers or
572 * locks.
573 *
574 * Note that userspace could try to race with use and instate a new
575 * usage _after_ we've cleared all current ones. End result will be an
576 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
577 * in this manner.
578 */
579 if (atomic_read(&fb->refcount.refcount) > 1) {
580 drm_modeset_lock_all(dev);
581 /* remove from any CRTC */
582 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
583 if (crtc->fb == fb) {
584 /* should turn off the crtc */
585 memset(&set, 0, sizeof(struct drm_mode_set));
586 set.crtc = crtc;
587 set.fb = NULL;
588 ret = drm_mode_set_config_internal(&set);
589 if (ret)
590 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
591 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000592 }
Dave Airlief453ba02008-11-07 14:05:41 -0800593
Daniel Vetterb62584e2012-12-11 16:51:35 +0100594 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
595 if (plane->fb == fb) {
596 /* should turn off the crtc */
597 ret = plane->funcs->disable_plane(plane);
598 if (ret)
599 DRM_ERROR("failed to disable plane with busy fb\n");
600 /* disconnect the plane from the fb and crtc: */
601 __drm_framebuffer_unreference(plane->fb);
602 plane->fb = NULL;
603 plane->crtc = NULL;
604 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800605 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100606 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800607 }
608
Rob Clarkf7eff602012-09-05 21:48:38 +0000609 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800610}
Rob Clarkf7eff602012-09-05 21:48:38 +0000611EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800612
613/**
614 * drm_crtc_init - Initialise a new CRTC object
615 * @dev: DRM device
616 * @crtc: CRTC object to init
617 * @funcs: callbacks for the new CRTC
618 *
Dave Airlief453ba02008-11-07 14:05:41 -0800619 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200620 *
621 * RETURNS:
622 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800623 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200624int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800625 const struct drm_crtc_funcs *funcs)
626{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200627 int ret;
628
Dave Airlief453ba02008-11-07 14:05:41 -0800629 crtc->dev = dev;
630 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000631 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800632
Daniel Vetter84849902012-12-02 00:28:11 +0100633 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100634 mutex_init(&crtc->mutex);
635 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200636
637 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
638 if (ret)
639 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800640
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300641 crtc->base.properties = &crtc->properties;
642
Dave Airlief453ba02008-11-07 14:05:41 -0800643 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
644 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200645
646 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100647 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200648
649 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800650}
651EXPORT_SYMBOL(drm_crtc_init);
652
653/**
654 * drm_crtc_cleanup - Cleans up the core crtc usage.
655 * @crtc: CRTC to cleanup
656 *
Dave Airlief453ba02008-11-07 14:05:41 -0800657 * Cleanup @crtc. Removes from drm modesetting space
658 * does NOT free object, caller does that.
659 */
660void drm_crtc_cleanup(struct drm_crtc *crtc)
661{
662 struct drm_device *dev = crtc->dev;
663
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000664 kfree(crtc->gamma_store);
665 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800666
667 drm_mode_object_put(dev, &crtc->base);
668 list_del(&crtc->head);
669 dev->mode_config.num_crtc--;
670}
671EXPORT_SYMBOL(drm_crtc_cleanup);
672
673/**
674 * drm_mode_probed_add - add a mode to a connector's probed mode list
675 * @connector: connector the new mode
676 * @mode: mode data
677 *
Dave Airlief453ba02008-11-07 14:05:41 -0800678 * Add @mode to @connector's mode list for later use.
679 */
680void drm_mode_probed_add(struct drm_connector *connector,
681 struct drm_display_mode *mode)
682{
683 list_add(&mode->head, &connector->probed_modes);
684}
685EXPORT_SYMBOL(drm_mode_probed_add);
686
687/**
688 * drm_mode_remove - remove and free a mode
689 * @connector: connector list to modify
690 * @mode: mode to remove
691 *
Dave Airlief453ba02008-11-07 14:05:41 -0800692 * Remove @mode from @connector's mode list, then free it.
693 */
694void drm_mode_remove(struct drm_connector *connector,
695 struct drm_display_mode *mode)
696{
697 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100698 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800699}
700EXPORT_SYMBOL(drm_mode_remove);
701
702/**
703 * drm_connector_init - Init a preallocated connector
704 * @dev: DRM device
705 * @connector: the connector to init
706 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100707 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800708 *
Dave Airlief453ba02008-11-07 14:05:41 -0800709 * Initialises a preallocated connector. Connectors should be
710 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200711 *
712 * RETURNS:
713 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800714 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200715int drm_connector_init(struct drm_device *dev,
716 struct drm_connector *connector,
717 const struct drm_connector_funcs *funcs,
718 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800719{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200720 int ret;
721
Daniel Vetter84849902012-12-02 00:28:11 +0100722 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800723
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200724 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
725 if (ret)
726 goto out;
727
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300728 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800729 connector->dev = dev;
730 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800731 connector->connector_type = connector_type;
732 connector->connector_type_id =
733 ++drm_connector_enum_list[connector_type].count; /* TODO */
Dave Airlief453ba02008-11-07 14:05:41 -0800734 INIT_LIST_HEAD(&connector->probed_modes);
735 INIT_LIST_HEAD(&connector->modes);
736 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000737 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800738
739 list_add_tail(&connector->head, &dev->mode_config.connector_list);
740 dev->mode_config.num_connector++;
741
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200742 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500743 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200744 dev->mode_config.edid_property,
745 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800746
Rob Clark58495562012-10-11 20:50:56 -0500747 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800748 dev->mode_config.dpms_property, 0);
749
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200750 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100751 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200752
753 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800754}
755EXPORT_SYMBOL(drm_connector_init);
756
757/**
758 * drm_connector_cleanup - cleans up an initialised connector
759 * @connector: connector to cleanup
760 *
Dave Airlief453ba02008-11-07 14:05:41 -0800761 * Cleans up the connector but doesn't free the object.
762 */
763void drm_connector_cleanup(struct drm_connector *connector)
764{
765 struct drm_device *dev = connector->dev;
766 struct drm_display_mode *mode, *t;
767
768 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
769 drm_mode_remove(connector, mode);
770
771 list_for_each_entry_safe(mode, t, &connector->modes, head)
772 drm_mode_remove(connector, mode);
773
Dave Airlief453ba02008-11-07 14:05:41 -0800774 drm_mode_object_put(dev, &connector->base);
775 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000776 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800777}
778EXPORT_SYMBOL(drm_connector_cleanup);
779
Dave Airliecbc7e222012-02-20 14:16:40 +0000780void drm_connector_unplug_all(struct drm_device *dev)
781{
782 struct drm_connector *connector;
783
784 /* taking the mode config mutex ends up in a clash with sysfs */
785 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
786 drm_sysfs_connector_remove(connector);
787
788}
789EXPORT_SYMBOL(drm_connector_unplug_all);
790
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200791int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000792 struct drm_encoder *encoder,
793 const struct drm_encoder_funcs *funcs,
794 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800795{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200796 int ret;
797
Daniel Vetter84849902012-12-02 00:28:11 +0100798 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800799
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200800 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
801 if (ret)
802 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800803
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200804 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800805 encoder->encoder_type = encoder_type;
806 encoder->funcs = funcs;
807
808 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
809 dev->mode_config.num_encoder++;
810
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200811 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100812 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200813
814 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800815}
816EXPORT_SYMBOL(drm_encoder_init);
817
818void drm_encoder_cleanup(struct drm_encoder *encoder)
819{
820 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100821 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800822 drm_mode_object_put(dev, &encoder->base);
823 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000824 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100825 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800826}
827EXPORT_SYMBOL(drm_encoder_cleanup);
828
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800829int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
830 unsigned long possible_crtcs,
831 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600832 const uint32_t *formats, uint32_t format_count,
833 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800834{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200835 int ret;
836
Daniel Vetter84849902012-12-02 00:28:11 +0100837 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800838
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200839 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
840 if (ret)
841 goto out;
842
Rob Clark4d939142012-05-17 02:23:27 -0600843 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800844 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800845 plane->funcs = funcs;
846 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
847 GFP_KERNEL);
848 if (!plane->format_types) {
849 DRM_DEBUG_KMS("out of memory when allocating plane\n");
850 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200851 ret = -ENOMEM;
852 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800853 }
854
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800855 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800856 plane->format_count = format_count;
857 plane->possible_crtcs = possible_crtcs;
858
Rob Clark0a7eb242011-12-13 20:19:36 -0600859 /* private planes are not exposed to userspace, but depending on
860 * display hardware, might be convenient to allow sharing programming
861 * for the scanout engine with the crtc implementation.
862 */
863 if (!priv) {
864 list_add_tail(&plane->head, &dev->mode_config.plane_list);
865 dev->mode_config.num_plane++;
866 } else {
867 INIT_LIST_HEAD(&plane->head);
868 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800869
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200870 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100871 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800872
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200873 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800874}
875EXPORT_SYMBOL(drm_plane_init);
876
877void drm_plane_cleanup(struct drm_plane *plane)
878{
879 struct drm_device *dev = plane->dev;
880
Daniel Vetter84849902012-12-02 00:28:11 +0100881 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800882 kfree(plane->format_types);
883 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600884 /* if not added to a list, it must be a private plane */
885 if (!list_empty(&plane->head)) {
886 list_del(&plane->head);
887 dev->mode_config.num_plane--;
888 }
Daniel Vetter84849902012-12-02 00:28:11 +0100889 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800890}
891EXPORT_SYMBOL(drm_plane_cleanup);
892
Dave Airlief453ba02008-11-07 14:05:41 -0800893/**
894 * drm_mode_create - create a new display mode
895 * @dev: DRM device
896 *
Dave Airlief453ba02008-11-07 14:05:41 -0800897 * Create a new drm_display_mode, give it an ID, and return it.
898 *
899 * RETURNS:
900 * Pointer to new mode on success, NULL on error.
901 */
902struct drm_display_mode *drm_mode_create(struct drm_device *dev)
903{
904 struct drm_display_mode *nmode;
905
906 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
907 if (!nmode)
908 return NULL;
909
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200910 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
911 kfree(nmode);
912 return NULL;
913 }
914
Dave Airlief453ba02008-11-07 14:05:41 -0800915 return nmode;
916}
917EXPORT_SYMBOL(drm_mode_create);
918
919/**
920 * drm_mode_destroy - remove a mode
921 * @dev: DRM device
922 * @mode: mode to remove
923 *
Dave Airlief453ba02008-11-07 14:05:41 -0800924 * Free @mode's unique identifier, then free it.
925 */
926void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
927{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200928 if (!mode)
929 return;
930
Dave Airlief453ba02008-11-07 14:05:41 -0800931 drm_mode_object_put(dev, &mode->base);
932
933 kfree(mode);
934}
935EXPORT_SYMBOL(drm_mode_destroy);
936
937static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
938{
939 struct drm_property *edid;
940 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800941
942 /*
943 * Standard properties (apply to all connectors)
944 */
945 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
946 DRM_MODE_PROP_IMMUTABLE,
947 "EDID", 0);
948 dev->mode_config.edid_property = edid;
949
Sascha Hauer4a67d392012-02-06 10:58:17 +0100950 dpms = drm_property_create_enum(dev, 0,
951 "DPMS", drm_dpms_enum_list,
952 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800953 dev->mode_config.dpms_property = dpms;
954
955 return 0;
956}
957
958/**
959 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
960 * @dev: DRM device
961 *
962 * Called by a driver the first time a DVI-I connector is made.
963 */
964int drm_mode_create_dvi_i_properties(struct drm_device *dev)
965{
966 struct drm_property *dvi_i_selector;
967 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800968
969 if (dev->mode_config.dvi_i_select_subconnector_property)
970 return 0;
971
972 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100973 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800974 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100975 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800976 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800977 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
978
Sascha Hauer4a67d392012-02-06 10:58:17 +0100979 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800980 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100981 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800982 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800983 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
984
985 return 0;
986}
987EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
988
989/**
990 * drm_create_tv_properties - create TV specific connector properties
991 * @dev: DRM device
992 * @num_modes: number of different TV formats (modes) supported
993 * @modes: array of pointers to strings containing name of each format
994 *
995 * Called by a driver's TV initialization routine, this function creates
996 * the TV specific connector properties for a given device. Caller is
997 * responsible for allocating a list of format names and passing them to
998 * this routine.
999 */
1000int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1001 char *modes[])
1002{
1003 struct drm_property *tv_selector;
1004 struct drm_property *tv_subconnector;
1005 int i;
1006
1007 if (dev->mode_config.tv_select_subconnector_property)
1008 return 0;
1009
1010 /*
1011 * Basic connector properties
1012 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001013 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001014 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001015 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001016 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001017 dev->mode_config.tv_select_subconnector_property = tv_selector;
1018
1019 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001020 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1021 "subconnector",
1022 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001023 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001024 dev->mode_config.tv_subconnector_property = tv_subconnector;
1025
1026 /*
1027 * Other, TV specific properties: margins & TV modes.
1028 */
1029 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001030 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001031
1032 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001033 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001034
1035 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001036 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001037
1038 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001039 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001040
1041 dev->mode_config.tv_mode_property =
1042 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1043 "mode", num_modes);
1044 for (i = 0; i < num_modes; i++)
1045 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1046 i, modes[i]);
1047
Francisco Jerezb6b79022009-08-02 04:19:20 +02001048 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001049 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001050
1051 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001052 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001053
1054 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001055 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001056
Francisco Jereza75f0232009-08-12 02:30:10 +02001057 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001058 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001059
1060 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001061 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001062
1063 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001064 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001065
Dave Airlief453ba02008-11-07 14:05:41 -08001066 return 0;
1067}
1068EXPORT_SYMBOL(drm_mode_create_tv_properties);
1069
1070/**
1071 * drm_mode_create_scaling_mode_property - create scaling mode property
1072 * @dev: DRM device
1073 *
1074 * Called by a driver the first time it's needed, must be attached to desired
1075 * connectors.
1076 */
1077int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1078{
1079 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001080
1081 if (dev->mode_config.scaling_mode_property)
1082 return 0;
1083
1084 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001085 drm_property_create_enum(dev, 0, "scaling mode",
1086 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001087 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001088
1089 dev->mode_config.scaling_mode_property = scaling_mode;
1090
1091 return 0;
1092}
1093EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1094
1095/**
1096 * drm_mode_create_dithering_property - create dithering property
1097 * @dev: DRM device
1098 *
1099 * Called by a driver the first time it's needed, must be attached to desired
1100 * connectors.
1101 */
1102int drm_mode_create_dithering_property(struct drm_device *dev)
1103{
1104 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001105
1106 if (dev->mode_config.dithering_mode_property)
1107 return 0;
1108
1109 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001110 drm_property_create_enum(dev, 0, "dithering",
1111 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001112 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001113 dev->mode_config.dithering_mode_property = dithering_mode;
1114
1115 return 0;
1116}
1117EXPORT_SYMBOL(drm_mode_create_dithering_property);
1118
1119/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001120 * drm_mode_create_dirty_property - create dirty property
1121 * @dev: DRM device
1122 *
1123 * Called by a driver the first time it's needed, must be attached to desired
1124 * connectors.
1125 */
1126int drm_mode_create_dirty_info_property(struct drm_device *dev)
1127{
1128 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001129
1130 if (dev->mode_config.dirty_info_property)
1131 return 0;
1132
1133 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001134 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001135 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001136 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001137 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001138 dev->mode_config.dirty_info_property = dirty_info;
1139
1140 return 0;
1141}
1142EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1143
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001144static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001145{
1146 uint32_t total_objects = 0;
1147
1148 total_objects += dev->mode_config.num_crtc;
1149 total_objects += dev->mode_config.num_connector;
1150 total_objects += dev->mode_config.num_encoder;
1151
Dave Airlief453ba02008-11-07 14:05:41 -08001152 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1153 if (!group->id_list)
1154 return -ENOMEM;
1155
1156 group->num_crtcs = 0;
1157 group->num_connectors = 0;
1158 group->num_encoders = 0;
1159 return 0;
1160}
1161
1162int drm_mode_group_init_legacy_group(struct drm_device *dev,
1163 struct drm_mode_group *group)
1164{
1165 struct drm_crtc *crtc;
1166 struct drm_encoder *encoder;
1167 struct drm_connector *connector;
1168 int ret;
1169
1170 if ((ret = drm_mode_group_init(dev, group)))
1171 return ret;
1172
1173 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1174 group->id_list[group->num_crtcs++] = crtc->base.id;
1175
1176 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1177 group->id_list[group->num_crtcs + group->num_encoders++] =
1178 encoder->base.id;
1179
1180 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1181 group->id_list[group->num_crtcs + group->num_encoders +
1182 group->num_connectors++] = connector->base.id;
1183
1184 return 0;
1185}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001186EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001187
1188/**
Dave Airlief453ba02008-11-07 14:05:41 -08001189 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1190 * @out: drm_mode_modeinfo struct to return to the user
1191 * @in: drm_display_mode to use
1192 *
Dave Airlief453ba02008-11-07 14:05:41 -08001193 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1194 * the user.
1195 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001196static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1197 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001198{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001199 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1200 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1201 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1202 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1203 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1204 "timing values too large for mode info\n");
1205
Dave Airlief453ba02008-11-07 14:05:41 -08001206 out->clock = in->clock;
1207 out->hdisplay = in->hdisplay;
1208 out->hsync_start = in->hsync_start;
1209 out->hsync_end = in->hsync_end;
1210 out->htotal = in->htotal;
1211 out->hskew = in->hskew;
1212 out->vdisplay = in->vdisplay;
1213 out->vsync_start = in->vsync_start;
1214 out->vsync_end = in->vsync_end;
1215 out->vtotal = in->vtotal;
1216 out->vscan = in->vscan;
1217 out->vrefresh = in->vrefresh;
1218 out->flags = in->flags;
1219 out->type = in->type;
1220 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1221 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1222}
1223
1224/**
1225 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1226 * @out: drm_display_mode to return to the user
1227 * @in: drm_mode_modeinfo to use
1228 *
Dave Airlief453ba02008-11-07 14:05:41 -08001229 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1230 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001231 *
1232 * RETURNS:
1233 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001234 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001235static int drm_crtc_convert_umode(struct drm_display_mode *out,
1236 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001237{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001238 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1239 return -ERANGE;
1240
Dave Airlief453ba02008-11-07 14:05:41 -08001241 out->clock = in->clock;
1242 out->hdisplay = in->hdisplay;
1243 out->hsync_start = in->hsync_start;
1244 out->hsync_end = in->hsync_end;
1245 out->htotal = in->htotal;
1246 out->hskew = in->hskew;
1247 out->vdisplay = in->vdisplay;
1248 out->vsync_start = in->vsync_start;
1249 out->vsync_end = in->vsync_end;
1250 out->vtotal = in->vtotal;
1251 out->vscan = in->vscan;
1252 out->vrefresh = in->vrefresh;
1253 out->flags = in->flags;
1254 out->type = in->type;
1255 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1256 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001257
1258 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001259}
1260
1261/**
1262 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001263 * @dev: drm device for the ioctl
1264 * @data: data pointer for the ioctl
1265 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001266 *
Dave Airlief453ba02008-11-07 14:05:41 -08001267 * Construct a set of configuration description structures and return
1268 * them to the user, including CRTC, connector and framebuffer configuration.
1269 *
1270 * Called by the user via ioctl.
1271 *
1272 * RETURNS:
1273 * Zero on success, errno on failure.
1274 */
1275int drm_mode_getresources(struct drm_device *dev, void *data,
1276 struct drm_file *file_priv)
1277{
1278 struct drm_mode_card_res *card_res = data;
1279 struct list_head *lh;
1280 struct drm_framebuffer *fb;
1281 struct drm_connector *connector;
1282 struct drm_crtc *crtc;
1283 struct drm_encoder *encoder;
1284 int ret = 0;
1285 int connector_count = 0;
1286 int crtc_count = 0;
1287 int fb_count = 0;
1288 int encoder_count = 0;
1289 int copied = 0, i;
1290 uint32_t __user *fb_id;
1291 uint32_t __user *crtc_id;
1292 uint32_t __user *connector_id;
1293 uint32_t __user *encoder_id;
1294 struct drm_mode_group *mode_group;
1295
Dave Airliefb3b06c2011-02-08 13:55:21 +10001296 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1297 return -EINVAL;
1298
Dave Airlief453ba02008-11-07 14:05:41 -08001299
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001300 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001301 /*
1302 * For the non-control nodes we need to limit the list of resources
1303 * by IDs in the group list for this node
1304 */
1305 list_for_each(lh, &file_priv->fbs)
1306 fb_count++;
1307
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001308 /* handle this in 4 parts */
1309 /* FBs */
1310 if (card_res->count_fbs >= fb_count) {
1311 copied = 0;
1312 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1313 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1314 if (put_user(fb->base.id, fb_id + copied)) {
1315 mutex_unlock(&file_priv->fbs_lock);
1316 return -EFAULT;
1317 }
1318 copied++;
1319 }
1320 }
1321 card_res->count_fbs = fb_count;
1322 mutex_unlock(&file_priv->fbs_lock);
1323
1324 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001325 mode_group = &file_priv->master->minor->mode_group;
1326 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1327
1328 list_for_each(lh, &dev->mode_config.crtc_list)
1329 crtc_count++;
1330
1331 list_for_each(lh, &dev->mode_config.connector_list)
1332 connector_count++;
1333
1334 list_for_each(lh, &dev->mode_config.encoder_list)
1335 encoder_count++;
1336 } else {
1337
1338 crtc_count = mode_group->num_crtcs;
1339 connector_count = mode_group->num_connectors;
1340 encoder_count = mode_group->num_encoders;
1341 }
1342
1343 card_res->max_height = dev->mode_config.max_height;
1344 card_res->min_height = dev->mode_config.min_height;
1345 card_res->max_width = dev->mode_config.max_width;
1346 card_res->min_width = dev->mode_config.min_width;
1347
Dave Airlief453ba02008-11-07 14:05:41 -08001348 /* CRTCs */
1349 if (card_res->count_crtcs >= crtc_count) {
1350 copied = 0;
1351 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1352 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1353 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1354 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001355 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001356 if (put_user(crtc->base.id, crtc_id + copied)) {
1357 ret = -EFAULT;
1358 goto out;
1359 }
1360 copied++;
1361 }
1362 } else {
1363 for (i = 0; i < mode_group->num_crtcs; i++) {
1364 if (put_user(mode_group->id_list[i],
1365 crtc_id + copied)) {
1366 ret = -EFAULT;
1367 goto out;
1368 }
1369 copied++;
1370 }
1371 }
1372 }
1373 card_res->count_crtcs = crtc_count;
1374
1375 /* Encoders */
1376 if (card_res->count_encoders >= encoder_count) {
1377 copied = 0;
1378 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1379 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1380 list_for_each_entry(encoder,
1381 &dev->mode_config.encoder_list,
1382 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001383 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1384 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001385 if (put_user(encoder->base.id, encoder_id +
1386 copied)) {
1387 ret = -EFAULT;
1388 goto out;
1389 }
1390 copied++;
1391 }
1392 } else {
1393 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1394 if (put_user(mode_group->id_list[i],
1395 encoder_id + copied)) {
1396 ret = -EFAULT;
1397 goto out;
1398 }
1399 copied++;
1400 }
1401
1402 }
1403 }
1404 card_res->count_encoders = encoder_count;
1405
1406 /* Connectors */
1407 if (card_res->count_connectors >= connector_count) {
1408 copied = 0;
1409 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1410 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1411 list_for_each_entry(connector,
1412 &dev->mode_config.connector_list,
1413 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001414 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1415 connector->base.id,
1416 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001417 if (put_user(connector->base.id,
1418 connector_id + copied)) {
1419 ret = -EFAULT;
1420 goto out;
1421 }
1422 copied++;
1423 }
1424 } else {
1425 int start = mode_group->num_crtcs +
1426 mode_group->num_encoders;
1427 for (i = start; i < start + mode_group->num_connectors; i++) {
1428 if (put_user(mode_group->id_list[i],
1429 connector_id + copied)) {
1430 ret = -EFAULT;
1431 goto out;
1432 }
1433 copied++;
1434 }
1435 }
1436 }
1437 card_res->count_connectors = connector_count;
1438
Jerome Glisse94401062010-07-15 15:43:25 -04001439 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001440 card_res->count_connectors, card_res->count_encoders);
1441
1442out:
Daniel Vetter84849902012-12-02 00:28:11 +01001443 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001444 return ret;
1445}
1446
1447/**
1448 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001449 * @dev: drm device for the ioctl
1450 * @data: data pointer for the ioctl
1451 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001452 *
Dave Airlief453ba02008-11-07 14:05:41 -08001453 * Construct a CRTC configuration structure to return to the user.
1454 *
1455 * Called by the user via ioctl.
1456 *
1457 * RETURNS:
1458 * Zero on success, errno on failure.
1459 */
1460int drm_mode_getcrtc(struct drm_device *dev,
1461 void *data, struct drm_file *file_priv)
1462{
1463 struct drm_mode_crtc *crtc_resp = data;
1464 struct drm_crtc *crtc;
1465 struct drm_mode_object *obj;
1466 int ret = 0;
1467
Dave Airliefb3b06c2011-02-08 13:55:21 +10001468 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1469 return -EINVAL;
1470
Daniel Vetter84849902012-12-02 00:28:11 +01001471 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001472
1473 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1474 DRM_MODE_OBJECT_CRTC);
1475 if (!obj) {
1476 ret = -EINVAL;
1477 goto out;
1478 }
1479 crtc = obj_to_crtc(obj);
1480
1481 crtc_resp->x = crtc->x;
1482 crtc_resp->y = crtc->y;
1483 crtc_resp->gamma_size = crtc->gamma_size;
1484 if (crtc->fb)
1485 crtc_resp->fb_id = crtc->fb->base.id;
1486 else
1487 crtc_resp->fb_id = 0;
1488
1489 if (crtc->enabled) {
1490
1491 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1492 crtc_resp->mode_valid = 1;
1493
1494 } else {
1495 crtc_resp->mode_valid = 0;
1496 }
1497
1498out:
Daniel Vetter84849902012-12-02 00:28:11 +01001499 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001500 return ret;
1501}
1502
1503/**
1504 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001505 * @dev: drm device for the ioctl
1506 * @data: data pointer for the ioctl
1507 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001508 *
Dave Airlief453ba02008-11-07 14:05:41 -08001509 * Construct a connector configuration structure to return to the user.
1510 *
1511 * Called by the user via ioctl.
1512 *
1513 * RETURNS:
1514 * Zero on success, errno on failure.
1515 */
1516int drm_mode_getconnector(struct drm_device *dev, void *data,
1517 struct drm_file *file_priv)
1518{
1519 struct drm_mode_get_connector *out_resp = data;
1520 struct drm_mode_object *obj;
1521 struct drm_connector *connector;
1522 struct drm_display_mode *mode;
1523 int mode_count = 0;
1524 int props_count = 0;
1525 int encoders_count = 0;
1526 int ret = 0;
1527 int copied = 0;
1528 int i;
1529 struct drm_mode_modeinfo u_mode;
1530 struct drm_mode_modeinfo __user *mode_ptr;
1531 uint32_t __user *prop_ptr;
1532 uint64_t __user *prop_values;
1533 uint32_t __user *encoder_ptr;
1534
Dave Airliefb3b06c2011-02-08 13:55:21 +10001535 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1536 return -EINVAL;
1537
Dave Airlief453ba02008-11-07 14:05:41 -08001538 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1539
Jerome Glisse94401062010-07-15 15:43:25 -04001540 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001541
Daniel Vetter7b240562012-12-12 00:35:33 +01001542 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001543
1544 obj = drm_mode_object_find(dev, out_resp->connector_id,
1545 DRM_MODE_OBJECT_CONNECTOR);
1546 if (!obj) {
1547 ret = -EINVAL;
1548 goto out;
1549 }
1550 connector = obj_to_connector(obj);
1551
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001552 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001553
1554 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1555 if (connector->encoder_ids[i] != 0) {
1556 encoders_count++;
1557 }
1558 }
1559
1560 if (out_resp->count_modes == 0) {
1561 connector->funcs->fill_modes(connector,
1562 dev->mode_config.max_width,
1563 dev->mode_config.max_height);
1564 }
1565
1566 /* delayed so we get modes regardless of pre-fill_modes state */
1567 list_for_each_entry(mode, &connector->modes, head)
1568 mode_count++;
1569
1570 out_resp->connector_id = connector->base.id;
1571 out_resp->connector_type = connector->connector_type;
1572 out_resp->connector_type_id = connector->connector_type_id;
1573 out_resp->mm_width = connector->display_info.width_mm;
1574 out_resp->mm_height = connector->display_info.height_mm;
1575 out_resp->subpixel = connector->display_info.subpixel_order;
1576 out_resp->connection = connector->status;
1577 if (connector->encoder)
1578 out_resp->encoder_id = connector->encoder->base.id;
1579 else
1580 out_resp->encoder_id = 0;
1581
1582 /*
1583 * This ioctl is called twice, once to determine how much space is
1584 * needed, and the 2nd time to fill it.
1585 */
1586 if ((out_resp->count_modes >= mode_count) && mode_count) {
1587 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001588 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001589 list_for_each_entry(mode, &connector->modes, head) {
1590 drm_crtc_convert_to_umode(&u_mode, mode);
1591 if (copy_to_user(mode_ptr + copied,
1592 &u_mode, sizeof(u_mode))) {
1593 ret = -EFAULT;
1594 goto out;
1595 }
1596 copied++;
1597 }
1598 }
1599 out_resp->count_modes = mode_count;
1600
1601 if ((out_resp->count_props >= props_count) && props_count) {
1602 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001603 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1604 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001605 for (i = 0; i < connector->properties.count; i++) {
1606 if (put_user(connector->properties.ids[i],
1607 prop_ptr + copied)) {
1608 ret = -EFAULT;
1609 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001610 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001611
1612 if (put_user(connector->properties.values[i],
1613 prop_values + copied)) {
1614 ret = -EFAULT;
1615 goto out;
1616 }
1617 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001618 }
1619 }
1620 out_resp->count_props = props_count;
1621
1622 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1623 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001624 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001625 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1626 if (connector->encoder_ids[i] != 0) {
1627 if (put_user(connector->encoder_ids[i],
1628 encoder_ptr + copied)) {
1629 ret = -EFAULT;
1630 goto out;
1631 }
1632 copied++;
1633 }
1634 }
1635 }
1636 out_resp->count_encoders = encoders_count;
1637
1638out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001639 mutex_unlock(&dev->mode_config.mutex);
1640
Dave Airlief453ba02008-11-07 14:05:41 -08001641 return ret;
1642}
1643
1644int drm_mode_getencoder(struct drm_device *dev, void *data,
1645 struct drm_file *file_priv)
1646{
1647 struct drm_mode_get_encoder *enc_resp = data;
1648 struct drm_mode_object *obj;
1649 struct drm_encoder *encoder;
1650 int ret = 0;
1651
Dave Airliefb3b06c2011-02-08 13:55:21 +10001652 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1653 return -EINVAL;
1654
Daniel Vetter84849902012-12-02 00:28:11 +01001655 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001656 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1657 DRM_MODE_OBJECT_ENCODER);
1658 if (!obj) {
1659 ret = -EINVAL;
1660 goto out;
1661 }
1662 encoder = obj_to_encoder(obj);
1663
1664 if (encoder->crtc)
1665 enc_resp->crtc_id = encoder->crtc->base.id;
1666 else
1667 enc_resp->crtc_id = 0;
1668 enc_resp->encoder_type = encoder->encoder_type;
1669 enc_resp->encoder_id = encoder->base.id;
1670 enc_resp->possible_crtcs = encoder->possible_crtcs;
1671 enc_resp->possible_clones = encoder->possible_clones;
1672
1673out:
Daniel Vetter84849902012-12-02 00:28:11 +01001674 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001675 return ret;
1676}
1677
1678/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001679 * drm_mode_getplane_res - get plane info
1680 * @dev: DRM device
1681 * @data: ioctl data
1682 * @file_priv: DRM file info
1683 *
1684 * Return an plane count and set of IDs.
1685 */
1686int drm_mode_getplane_res(struct drm_device *dev, void *data,
1687 struct drm_file *file_priv)
1688{
1689 struct drm_mode_get_plane_res *plane_resp = data;
1690 struct drm_mode_config *config;
1691 struct drm_plane *plane;
1692 uint32_t __user *plane_ptr;
1693 int copied = 0, ret = 0;
1694
1695 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1696 return -EINVAL;
1697
Daniel Vetter84849902012-12-02 00:28:11 +01001698 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001699 config = &dev->mode_config;
1700
1701 /*
1702 * This ioctl is called twice, once to determine how much space is
1703 * needed, and the 2nd time to fill it.
1704 */
1705 if (config->num_plane &&
1706 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001707 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001708
1709 list_for_each_entry(plane, &config->plane_list, head) {
1710 if (put_user(plane->base.id, plane_ptr + copied)) {
1711 ret = -EFAULT;
1712 goto out;
1713 }
1714 copied++;
1715 }
1716 }
1717 plane_resp->count_planes = config->num_plane;
1718
1719out:
Daniel Vetter84849902012-12-02 00:28:11 +01001720 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001721 return ret;
1722}
1723
1724/**
1725 * drm_mode_getplane - get plane info
1726 * @dev: DRM device
1727 * @data: ioctl data
1728 * @file_priv: DRM file info
1729 *
1730 * Return plane info, including formats supported, gamma size, any
1731 * current fb, etc.
1732 */
1733int drm_mode_getplane(struct drm_device *dev, void *data,
1734 struct drm_file *file_priv)
1735{
1736 struct drm_mode_get_plane *plane_resp = data;
1737 struct drm_mode_object *obj;
1738 struct drm_plane *plane;
1739 uint32_t __user *format_ptr;
1740 int ret = 0;
1741
1742 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1743 return -EINVAL;
1744
Daniel Vetter84849902012-12-02 00:28:11 +01001745 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001746 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1747 DRM_MODE_OBJECT_PLANE);
1748 if (!obj) {
1749 ret = -ENOENT;
1750 goto out;
1751 }
1752 plane = obj_to_plane(obj);
1753
1754 if (plane->crtc)
1755 plane_resp->crtc_id = plane->crtc->base.id;
1756 else
1757 plane_resp->crtc_id = 0;
1758
1759 if (plane->fb)
1760 plane_resp->fb_id = plane->fb->base.id;
1761 else
1762 plane_resp->fb_id = 0;
1763
1764 plane_resp->plane_id = plane->base.id;
1765 plane_resp->possible_crtcs = plane->possible_crtcs;
1766 plane_resp->gamma_size = plane->gamma_size;
1767
1768 /*
1769 * This ioctl is called twice, once to determine how much space is
1770 * needed, and the 2nd time to fill it.
1771 */
1772 if (plane->format_count &&
1773 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001774 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001775 if (copy_to_user(format_ptr,
1776 plane->format_types,
1777 sizeof(uint32_t) * plane->format_count)) {
1778 ret = -EFAULT;
1779 goto out;
1780 }
1781 }
1782 plane_resp->count_format_types = plane->format_count;
1783
1784out:
Daniel Vetter84849902012-12-02 00:28:11 +01001785 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001786 return ret;
1787}
1788
1789/**
1790 * drm_mode_setplane - set up or tear down an plane
1791 * @dev: DRM device
1792 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001793 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001794 *
1795 * Set plane info, including placement, fb, scaling, and other factors.
1796 * Or pass a NULL fb to disable.
1797 */
1798int drm_mode_setplane(struct drm_device *dev, void *data,
1799 struct drm_file *file_priv)
1800{
1801 struct drm_mode_set_plane *plane_req = data;
1802 struct drm_mode_object *obj;
1803 struct drm_plane *plane;
1804 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001805 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001806 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001807 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001808 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001809
1810 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1811 return -EINVAL;
1812
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001813 /*
1814 * First, find the plane, crtc, and fb objects. If not available,
1815 * we don't bother to call the driver.
1816 */
1817 obj = drm_mode_object_find(dev, plane_req->plane_id,
1818 DRM_MODE_OBJECT_PLANE);
1819 if (!obj) {
1820 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1821 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001822 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001823 }
1824 plane = obj_to_plane(obj);
1825
1826 /* No fb means shut it down */
1827 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001828 drm_modeset_lock_all(dev);
1829 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001830 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001831 plane->crtc = NULL;
1832 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001833 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001834 goto out;
1835 }
1836
1837 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1838 DRM_MODE_OBJECT_CRTC);
1839 if (!obj) {
1840 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1841 plane_req->crtc_id);
1842 ret = -ENOENT;
1843 goto out;
1844 }
1845 crtc = obj_to_crtc(obj);
1846
Daniel Vetter786b99e2012-12-02 21:53:40 +01001847 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1848 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001849 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1850 plane_req->fb_id);
1851 ret = -ENOENT;
1852 goto out;
1853 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001854
Ville Syrjälä62443be2011-12-20 00:06:47 +02001855 /* Check whether this plane supports the fb pixel format. */
1856 for (i = 0; i < plane->format_count; i++)
1857 if (fb->pixel_format == plane->format_types[i])
1858 break;
1859 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03001860 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1861 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02001862 ret = -EINVAL;
1863 goto out;
1864 }
1865
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001866 fb_width = fb->width << 16;
1867 fb_height = fb->height << 16;
1868
1869 /* Make sure source coordinates are inside the fb. */
1870 if (plane_req->src_w > fb_width ||
1871 plane_req->src_x > fb_width - plane_req->src_w ||
1872 plane_req->src_h > fb_height ||
1873 plane_req->src_y > fb_height - plane_req->src_h) {
1874 DRM_DEBUG_KMS("Invalid source coordinates "
1875 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1876 plane_req->src_w >> 16,
1877 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1878 plane_req->src_h >> 16,
1879 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1880 plane_req->src_x >> 16,
1881 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1882 plane_req->src_y >> 16,
1883 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1884 ret = -ENOSPC;
1885 goto out;
1886 }
1887
Ville Syrjälä687a0402011-12-20 00:06:45 +02001888 /* Give drivers some help against integer overflows */
1889 if (plane_req->crtc_w > INT_MAX ||
1890 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1891 plane_req->crtc_h > INT_MAX ||
1892 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1893 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1894 plane_req->crtc_w, plane_req->crtc_h,
1895 plane_req->crtc_x, plane_req->crtc_y);
1896 ret = -ERANGE;
1897 goto out;
1898 }
1899
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001900 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001901 ret = plane->funcs->update_plane(plane, crtc, fb,
1902 plane_req->crtc_x, plane_req->crtc_y,
1903 plane_req->crtc_w, plane_req->crtc_h,
1904 plane_req->src_x, plane_req->src_y,
1905 plane_req->src_w, plane_req->src_h);
1906 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001907 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001908 plane->crtc = crtc;
1909 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001910 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001911 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001912 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001913
1914out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001915 if (fb)
1916 drm_framebuffer_unreference(fb);
1917 if (old_fb)
1918 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001919
1920 return ret;
1921}
1922
1923/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001924 * drm_mode_set_config_internal - helper to call ->set_config
1925 * @set: modeset config to set
1926 *
1927 * This is a little helper to wrap internal calls to the ->set_config driver
1928 * interface. The only thing it adds is correct refcounting dance.
1929 */
1930int drm_mode_set_config_internal(struct drm_mode_set *set)
1931{
1932 struct drm_crtc *crtc = set->crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001933 struct drm_framebuffer *fb, *old_fb;
1934 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001935
Daniel Vetterb0d12322012-12-11 01:07:12 +01001936 old_fb = crtc->fb;
1937 fb = set->fb;
1938
1939 ret = crtc->funcs->set_config(set);
1940 if (ret == 0) {
1941 if (old_fb)
1942 drm_framebuffer_unreference(old_fb);
1943 if (fb)
1944 drm_framebuffer_reference(fb);
1945 }
1946
1947 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001948}
1949EXPORT_SYMBOL(drm_mode_set_config_internal);
1950
1951/**
Dave Airlief453ba02008-11-07 14:05:41 -08001952 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001953 * @dev: drm device for the ioctl
1954 * @data: data pointer for the ioctl
1955 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001956 *
Dave Airlief453ba02008-11-07 14:05:41 -08001957 * Build a new CRTC configuration based on user request.
1958 *
1959 * Called by the user via ioctl.
1960 *
1961 * RETURNS:
1962 * Zero on success, errno on failure.
1963 */
1964int drm_mode_setcrtc(struct drm_device *dev, void *data,
1965 struct drm_file *file_priv)
1966{
1967 struct drm_mode_config *config = &dev->mode_config;
1968 struct drm_mode_crtc *crtc_req = data;
1969 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001970 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001971 struct drm_connector **connector_set = NULL, *connector;
1972 struct drm_framebuffer *fb = NULL;
1973 struct drm_display_mode *mode = NULL;
1974 struct drm_mode_set set;
1975 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001976 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001977 int i;
1978
Dave Airliefb3b06c2011-02-08 13:55:21 +10001979 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1980 return -EINVAL;
1981
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001982 /* For some reason crtc x/y offsets are signed internally. */
1983 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1984 return -ERANGE;
1985
Daniel Vetter84849902012-12-02 00:28:11 +01001986 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001987 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1988 DRM_MODE_OBJECT_CRTC);
1989 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001990 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001991 ret = -EINVAL;
1992 goto out;
1993 }
1994 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001995 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001996
1997 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001998 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001999 /* If we have a mode we need a framebuffer. */
2000 /* If we pass -1, set the mode with the currently bound fb */
2001 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002002 if (!crtc->fb) {
2003 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2004 ret = -EINVAL;
2005 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002006 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002007 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002008 /* Make refcounting symmetric with the lookup path. */
2009 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002010 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002011 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2012 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002013 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2014 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002015 ret = -EINVAL;
2016 goto out;
2017 }
Dave Airlief453ba02008-11-07 14:05:41 -08002018 }
2019
2020 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002021 if (!mode) {
2022 ret = -ENOMEM;
2023 goto out;
2024 }
2025
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002026 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2027 if (ret) {
2028 DRM_DEBUG_KMS("Invalid mode\n");
2029 goto out;
2030 }
2031
Dave Airlief453ba02008-11-07 14:05:41 -08002032 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002033
Rob Clark7c80e122012-09-04 16:35:56 +00002034 hdisplay = mode->hdisplay;
2035 vdisplay = mode->vdisplay;
2036
2037 if (crtc->invert_dimensions)
2038 swap(hdisplay, vdisplay);
2039
2040 if (hdisplay > fb->width ||
2041 vdisplay > fb->height ||
2042 crtc_req->x > fb->width - hdisplay ||
2043 crtc_req->y > fb->height - vdisplay) {
2044 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2045 fb->width, fb->height,
2046 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2047 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002048 ret = -ENOSPC;
2049 goto out;
2050 }
Dave Airlief453ba02008-11-07 14:05:41 -08002051 }
2052
2053 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002054 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002055 ret = -EINVAL;
2056 goto out;
2057 }
2058
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002059 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002060 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002061 crtc_req->count_connectors);
2062 ret = -EINVAL;
2063 goto out;
2064 }
2065
2066 if (crtc_req->count_connectors > 0) {
2067 u32 out_id;
2068
2069 /* Avoid unbounded kernel memory allocation */
2070 if (crtc_req->count_connectors > config->num_connector) {
2071 ret = -EINVAL;
2072 goto out;
2073 }
2074
2075 connector_set = kmalloc(crtc_req->count_connectors *
2076 sizeof(struct drm_connector *),
2077 GFP_KERNEL);
2078 if (!connector_set) {
2079 ret = -ENOMEM;
2080 goto out;
2081 }
2082
2083 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002084 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002085 if (get_user(out_id, &set_connectors_ptr[i])) {
2086 ret = -EFAULT;
2087 goto out;
2088 }
2089
2090 obj = drm_mode_object_find(dev, out_id,
2091 DRM_MODE_OBJECT_CONNECTOR);
2092 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002093 DRM_DEBUG_KMS("Connector id %d unknown\n",
2094 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002095 ret = -EINVAL;
2096 goto out;
2097 }
2098 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002099 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2100 connector->base.id,
2101 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002102
2103 connector_set[i] = connector;
2104 }
2105 }
2106
2107 set.crtc = crtc;
2108 set.x = crtc_req->x;
2109 set.y = crtc_req->y;
2110 set.mode = mode;
2111 set.connectors = connector_set;
2112 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002113 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002114 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002115
2116out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002117 if (fb)
2118 drm_framebuffer_unreference(fb);
2119
Dave Airlief453ba02008-11-07 14:05:41 -08002120 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002121 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002122 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002123 return ret;
2124}
2125
2126int drm_mode_cursor_ioctl(struct drm_device *dev,
2127 void *data, struct drm_file *file_priv)
2128{
2129 struct drm_mode_cursor *req = data;
2130 struct drm_mode_object *obj;
2131 struct drm_crtc *crtc;
2132 int ret = 0;
2133
Dave Airliefb3b06c2011-02-08 13:55:21 +10002134 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2135 return -EINVAL;
2136
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002137 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002138 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002139
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002140 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002141 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002142 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002143 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002144 }
2145 crtc = obj_to_crtc(obj);
2146
Daniel Vetterdac35662012-12-02 15:24:10 +01002147 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002148 if (req->flags & DRM_MODE_CURSOR_BO) {
2149 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002150 ret = -ENXIO;
2151 goto out;
2152 }
2153 /* Turns off the cursor if handle is 0 */
2154 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2155 req->width, req->height);
2156 }
2157
2158 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2159 if (crtc->funcs->cursor_move) {
2160 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2161 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002162 ret = -EFAULT;
2163 goto out;
2164 }
2165 }
2166out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002167 mutex_unlock(&crtc->mutex);
2168
Dave Airlief453ba02008-11-07 14:05:41 -08002169 return ret;
2170}
2171
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002172/* Original addfb only supported RGB formats, so figure out which one */
2173uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2174{
2175 uint32_t fmt;
2176
2177 switch (bpp) {
2178 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002179 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002180 break;
2181 case 16:
2182 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002183 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002184 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002185 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002186 break;
2187 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002188 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002189 break;
2190 case 32:
2191 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002192 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002193 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002194 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002195 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002196 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002197 break;
2198 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002199 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2200 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002201 break;
2202 }
2203
2204 return fmt;
2205}
2206EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2207
Dave Airlief453ba02008-11-07 14:05:41 -08002208/**
2209 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002210 * @dev: drm device for the ioctl
2211 * @data: data pointer for the ioctl
2212 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002213 *
Dave Airlief453ba02008-11-07 14:05:41 -08002214 * Add a new FB to the specified CRTC, given a user request.
2215 *
2216 * Called by the user via ioctl.
2217 *
2218 * RETURNS:
2219 * Zero on success, errno on failure.
2220 */
2221int drm_mode_addfb(struct drm_device *dev,
2222 void *data, struct drm_file *file_priv)
2223{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002224 struct drm_mode_fb_cmd *or = data;
2225 struct drm_mode_fb_cmd2 r = {};
2226 struct drm_mode_config *config = &dev->mode_config;
2227 struct drm_framebuffer *fb;
2228 int ret = 0;
2229
2230 /* Use new struct with format internally */
2231 r.fb_id = or->fb_id;
2232 r.width = or->width;
2233 r.height = or->height;
2234 r.pitches[0] = or->pitch;
2235 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2236 r.handles[0] = or->handle;
2237
2238 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2239 return -EINVAL;
2240
Jesse Barnesacb4b992011-11-07 12:03:23 -08002241 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002242 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002243
2244 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002245 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002246
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002247 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2248 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002249 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002250 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002251 }
2252
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002253 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002254 or->fb_id = fb->base.id;
2255 list_add(&fb->filp_head, &file_priv->fbs);
2256 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002257 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002258
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002259 return ret;
2260}
2261
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002262static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002263{
2264 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2265
2266 switch (format) {
2267 case DRM_FORMAT_C8:
2268 case DRM_FORMAT_RGB332:
2269 case DRM_FORMAT_BGR233:
2270 case DRM_FORMAT_XRGB4444:
2271 case DRM_FORMAT_XBGR4444:
2272 case DRM_FORMAT_RGBX4444:
2273 case DRM_FORMAT_BGRX4444:
2274 case DRM_FORMAT_ARGB4444:
2275 case DRM_FORMAT_ABGR4444:
2276 case DRM_FORMAT_RGBA4444:
2277 case DRM_FORMAT_BGRA4444:
2278 case DRM_FORMAT_XRGB1555:
2279 case DRM_FORMAT_XBGR1555:
2280 case DRM_FORMAT_RGBX5551:
2281 case DRM_FORMAT_BGRX5551:
2282 case DRM_FORMAT_ARGB1555:
2283 case DRM_FORMAT_ABGR1555:
2284 case DRM_FORMAT_RGBA5551:
2285 case DRM_FORMAT_BGRA5551:
2286 case DRM_FORMAT_RGB565:
2287 case DRM_FORMAT_BGR565:
2288 case DRM_FORMAT_RGB888:
2289 case DRM_FORMAT_BGR888:
2290 case DRM_FORMAT_XRGB8888:
2291 case DRM_FORMAT_XBGR8888:
2292 case DRM_FORMAT_RGBX8888:
2293 case DRM_FORMAT_BGRX8888:
2294 case DRM_FORMAT_ARGB8888:
2295 case DRM_FORMAT_ABGR8888:
2296 case DRM_FORMAT_RGBA8888:
2297 case DRM_FORMAT_BGRA8888:
2298 case DRM_FORMAT_XRGB2101010:
2299 case DRM_FORMAT_XBGR2101010:
2300 case DRM_FORMAT_RGBX1010102:
2301 case DRM_FORMAT_BGRX1010102:
2302 case DRM_FORMAT_ARGB2101010:
2303 case DRM_FORMAT_ABGR2101010:
2304 case DRM_FORMAT_RGBA1010102:
2305 case DRM_FORMAT_BGRA1010102:
2306 case DRM_FORMAT_YUYV:
2307 case DRM_FORMAT_YVYU:
2308 case DRM_FORMAT_UYVY:
2309 case DRM_FORMAT_VYUY:
2310 case DRM_FORMAT_AYUV:
2311 case DRM_FORMAT_NV12:
2312 case DRM_FORMAT_NV21:
2313 case DRM_FORMAT_NV16:
2314 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002315 case DRM_FORMAT_NV24:
2316 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002317 case DRM_FORMAT_YUV410:
2318 case DRM_FORMAT_YVU410:
2319 case DRM_FORMAT_YUV411:
2320 case DRM_FORMAT_YVU411:
2321 case DRM_FORMAT_YUV420:
2322 case DRM_FORMAT_YVU420:
2323 case DRM_FORMAT_YUV422:
2324 case DRM_FORMAT_YVU422:
2325 case DRM_FORMAT_YUV444:
2326 case DRM_FORMAT_YVU444:
2327 return 0;
2328 default:
2329 return -EINVAL;
2330 }
2331}
2332
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002333static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002334{
2335 int ret, hsub, vsub, num_planes, i;
2336
2337 ret = format_check(r);
2338 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002339 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2340 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002341 return ret;
2342 }
2343
2344 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2345 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2346 num_planes = drm_format_num_planes(r->pixel_format);
2347
2348 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002349 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002350 return -EINVAL;
2351 }
2352
2353 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002354 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002355 return -EINVAL;
2356 }
2357
2358 for (i = 0; i < num_planes; i++) {
2359 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002360 unsigned int height = r->height / (i != 0 ? vsub : 1);
2361 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002362
2363 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002364 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002365 return -EINVAL;
2366 }
2367
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002368 if ((uint64_t) width * cpp > UINT_MAX)
2369 return -ERANGE;
2370
2371 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2372 return -ERANGE;
2373
2374 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002375 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002376 return -EINVAL;
2377 }
2378 }
2379
2380 return 0;
2381}
2382
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002383/**
2384 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002385 * @dev: drm device for the ioctl
2386 * @data: data pointer for the ioctl
2387 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002388 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002389 * Add a new FB to the specified CRTC, given a user request with format.
2390 *
2391 * Called by the user via ioctl.
2392 *
2393 * RETURNS:
2394 * Zero on success, errno on failure.
2395 */
2396int drm_mode_addfb2(struct drm_device *dev,
2397 void *data, struct drm_file *file_priv)
2398{
2399 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002400 struct drm_mode_config *config = &dev->mode_config;
2401 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002402 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002403
Dave Airliefb3b06c2011-02-08 13:55:21 +10002404 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2405 return -EINVAL;
2406
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002407 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2408 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2409 return -EINVAL;
2410 }
2411
Dave Airlief453ba02008-11-07 14:05:41 -08002412 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002413 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002414 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002415 return -EINVAL;
2416 }
2417 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002418 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002419 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002420 return -EINVAL;
2421 }
2422
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002423 ret = framebuffer_check(r);
2424 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002425 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002426
Dave Airlief453ba02008-11-07 14:05:41 -08002427 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002428 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002429 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002430 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002431 }
2432
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002433 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002434 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002435 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002436 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002437 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002438
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002439
Dave Airlief453ba02008-11-07 14:05:41 -08002440 return ret;
2441}
2442
2443/**
2444 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002445 * @dev: drm device for the ioctl
2446 * @data: data pointer for the ioctl
2447 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002448 *
Dave Airlief453ba02008-11-07 14:05:41 -08002449 * Remove the FB specified by the user.
2450 *
2451 * Called by the user via ioctl.
2452 *
2453 * RETURNS:
2454 * Zero on success, errno on failure.
2455 */
2456int drm_mode_rmfb(struct drm_device *dev,
2457 void *data, struct drm_file *file_priv)
2458{
Dave Airlief453ba02008-11-07 14:05:41 -08002459 struct drm_framebuffer *fb = NULL;
2460 struct drm_framebuffer *fbl = NULL;
2461 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002462 int found = 0;
2463
Dave Airliefb3b06c2011-02-08 13:55:21 +10002464 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2465 return -EINVAL;
2466
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002467 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002468 mutex_lock(&dev->mode_config.fb_lock);
2469 fb = __drm_framebuffer_lookup(dev, *id);
2470 if (!fb)
2471 goto fail_lookup;
2472
Dave Airlief453ba02008-11-07 14:05:41 -08002473 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2474 if (fb == fbl)
2475 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002476 if (!found)
2477 goto fail_lookup;
2478
2479 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2480 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002481
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002482 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002483 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002484 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002485
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002486 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002487
Daniel Vetter2b677e82012-12-10 21:16:05 +01002488 return 0;
2489
2490fail_lookup:
2491 mutex_unlock(&dev->mode_config.fb_lock);
2492 mutex_unlock(&file_priv->fbs_lock);
2493
2494 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002495}
2496
2497/**
2498 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002499 * @dev: drm device for the ioctl
2500 * @data: data pointer for the ioctl
2501 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002502 *
Dave Airlief453ba02008-11-07 14:05:41 -08002503 * Lookup the FB given its ID and return info about it.
2504 *
2505 * Called by the user via ioctl.
2506 *
2507 * RETURNS:
2508 * Zero on success, errno on failure.
2509 */
2510int drm_mode_getfb(struct drm_device *dev,
2511 void *data, struct drm_file *file_priv)
2512{
2513 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002514 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002515 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002516
Dave Airliefb3b06c2011-02-08 13:55:21 +10002517 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2518 return -EINVAL;
2519
Daniel Vetter786b99e2012-12-02 21:53:40 +01002520 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002521 if (!fb)
2522 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002523
2524 r->height = fb->height;
2525 r->width = fb->width;
2526 r->depth = fb->depth;
2527 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002528 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002529 if (fb->funcs->create_handle)
2530 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2531 else
2532 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002533
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002534 drm_framebuffer_unreference(fb);
2535
Dave Airlief453ba02008-11-07 14:05:41 -08002536 return ret;
2537}
2538
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002539int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2540 void *data, struct drm_file *file_priv)
2541{
2542 struct drm_clip_rect __user *clips_ptr;
2543 struct drm_clip_rect *clips = NULL;
2544 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002545 struct drm_framebuffer *fb;
2546 unsigned flags;
2547 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002548 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002549
Dave Airliefb3b06c2011-02-08 13:55:21 +10002550 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2551 return -EINVAL;
2552
Daniel Vetter786b99e2012-12-02 21:53:40 +01002553 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002554 if (!fb)
2555 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002556
2557 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002558 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002559
2560 if (!num_clips != !clips_ptr) {
2561 ret = -EINVAL;
2562 goto out_err1;
2563 }
2564
2565 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2566
2567 /* If userspace annotates copy, clips must come in pairs */
2568 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2569 ret = -EINVAL;
2570 goto out_err1;
2571 }
2572
2573 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002574 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2575 ret = -EINVAL;
2576 goto out_err1;
2577 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002578 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2579 if (!clips) {
2580 ret = -ENOMEM;
2581 goto out_err1;
2582 }
2583
2584 ret = copy_from_user(clips, clips_ptr,
2585 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002586 if (ret) {
2587 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002588 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002589 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002590 }
2591
2592 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002593 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002594 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2595 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002596 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002597 } else {
2598 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002599 }
2600
2601out_err2:
2602 kfree(clips);
2603out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002604 drm_framebuffer_unreference(fb);
2605
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002606 return ret;
2607}
2608
2609
Dave Airlief453ba02008-11-07 14:05:41 -08002610/**
2611 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002612 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002613 *
Dave Airlief453ba02008-11-07 14:05:41 -08002614 * Destroy all the FBs associated with @filp.
2615 *
2616 * Called by the user via ioctl.
2617 *
2618 * RETURNS:
2619 * Zero on success, errno on failure.
2620 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002621void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002622{
Dave Airlief453ba02008-11-07 14:05:41 -08002623 struct drm_device *dev = priv->minor->dev;
2624 struct drm_framebuffer *fb, *tfb;
2625
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002626 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002627 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002628
2629 mutex_lock(&dev->mode_config.fb_lock);
2630 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2631 __drm_framebuffer_unregister(dev, fb);
2632 mutex_unlock(&dev->mode_config.fb_lock);
2633
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002634 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002635
2636 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002637 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002638 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002639 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002640}
2641
Dave Airlief453ba02008-11-07 14:05:41 -08002642struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2643 const char *name, int num_values)
2644{
2645 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002646 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002647
2648 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2649 if (!property)
2650 return NULL;
2651
2652 if (num_values) {
2653 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2654 if (!property->values)
2655 goto fail;
2656 }
2657
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002658 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2659 if (ret)
2660 goto fail;
2661
Dave Airlief453ba02008-11-07 14:05:41 -08002662 property->flags = flags;
2663 property->num_values = num_values;
2664 INIT_LIST_HEAD(&property->enum_blob_list);
2665
Vinson Lee471dd2e2011-11-10 11:55:40 -08002666 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002667 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002668 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2669 }
Dave Airlief453ba02008-11-07 14:05:41 -08002670
2671 list_add_tail(&property->head, &dev->mode_config.property_list);
2672 return property;
2673fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002674 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002675 kfree(property);
2676 return NULL;
2677}
2678EXPORT_SYMBOL(drm_property_create);
2679
Sascha Hauer4a67d392012-02-06 10:58:17 +01002680struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2681 const char *name,
2682 const struct drm_prop_enum_list *props,
2683 int num_values)
2684{
2685 struct drm_property *property;
2686 int i, ret;
2687
2688 flags |= DRM_MODE_PROP_ENUM;
2689
2690 property = drm_property_create(dev, flags, name, num_values);
2691 if (!property)
2692 return NULL;
2693
2694 for (i = 0; i < num_values; i++) {
2695 ret = drm_property_add_enum(property, i,
2696 props[i].type,
2697 props[i].name);
2698 if (ret) {
2699 drm_property_destroy(dev, property);
2700 return NULL;
2701 }
2702 }
2703
2704 return property;
2705}
2706EXPORT_SYMBOL(drm_property_create_enum);
2707
Rob Clark49e27542012-05-17 02:23:26 -06002708struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2709 int flags, const char *name,
2710 const struct drm_prop_enum_list *props,
2711 int num_values)
2712{
2713 struct drm_property *property;
2714 int i, ret;
2715
2716 flags |= DRM_MODE_PROP_BITMASK;
2717
2718 property = drm_property_create(dev, flags, name, num_values);
2719 if (!property)
2720 return NULL;
2721
2722 for (i = 0; i < num_values; i++) {
2723 ret = drm_property_add_enum(property, i,
2724 props[i].type,
2725 props[i].name);
2726 if (ret) {
2727 drm_property_destroy(dev, property);
2728 return NULL;
2729 }
2730 }
2731
2732 return property;
2733}
2734EXPORT_SYMBOL(drm_property_create_bitmask);
2735
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002736struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2737 const char *name,
2738 uint64_t min, uint64_t max)
2739{
2740 struct drm_property *property;
2741
2742 flags |= DRM_MODE_PROP_RANGE;
2743
2744 property = drm_property_create(dev, flags, name, 2);
2745 if (!property)
2746 return NULL;
2747
2748 property->values[0] = min;
2749 property->values[1] = max;
2750
2751 return property;
2752}
2753EXPORT_SYMBOL(drm_property_create_range);
2754
Dave Airlief453ba02008-11-07 14:05:41 -08002755int drm_property_add_enum(struct drm_property *property, int index,
2756 uint64_t value, const char *name)
2757{
2758 struct drm_property_enum *prop_enum;
2759
Rob Clark49e27542012-05-17 02:23:26 -06002760 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2761 return -EINVAL;
2762
2763 /*
2764 * Bitmask enum properties have the additional constraint of values
2765 * from 0 to 63
2766 */
2767 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002768 return -EINVAL;
2769
2770 if (!list_empty(&property->enum_blob_list)) {
2771 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2772 if (prop_enum->value == value) {
2773 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2774 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2775 return 0;
2776 }
2777 }
2778 }
2779
2780 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2781 if (!prop_enum)
2782 return -ENOMEM;
2783
2784 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2785 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2786 prop_enum->value = value;
2787
2788 property->values[index] = value;
2789 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2790 return 0;
2791}
2792EXPORT_SYMBOL(drm_property_add_enum);
2793
2794void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2795{
2796 struct drm_property_enum *prop_enum, *pt;
2797
2798 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2799 list_del(&prop_enum->head);
2800 kfree(prop_enum);
2801 }
2802
2803 if (property->num_values)
2804 kfree(property->values);
2805 drm_mode_object_put(dev, &property->base);
2806 list_del(&property->head);
2807 kfree(property);
2808}
2809EXPORT_SYMBOL(drm_property_destroy);
2810
Paulo Zanonic5431882012-05-15 18:09:02 -03002811void drm_object_attach_property(struct drm_mode_object *obj,
2812 struct drm_property *property,
2813 uint64_t init_val)
2814{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002815 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002816
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002817 if (count == DRM_OBJECT_MAX_PROPERTY) {
2818 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2819 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2820 "you see this message on the same object type.\n",
2821 obj->type);
2822 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002823 }
2824
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002825 obj->properties->ids[count] = property->base.id;
2826 obj->properties->values[count] = init_val;
2827 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002828}
2829EXPORT_SYMBOL(drm_object_attach_property);
2830
2831int drm_object_property_set_value(struct drm_mode_object *obj,
2832 struct drm_property *property, uint64_t val)
2833{
2834 int i;
2835
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002836 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002837 if (obj->properties->ids[i] == property->base.id) {
2838 obj->properties->values[i] = val;
2839 return 0;
2840 }
2841 }
2842
2843 return -EINVAL;
2844}
2845EXPORT_SYMBOL(drm_object_property_set_value);
2846
2847int drm_object_property_get_value(struct drm_mode_object *obj,
2848 struct drm_property *property, uint64_t *val)
2849{
2850 int i;
2851
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002852 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002853 if (obj->properties->ids[i] == property->base.id) {
2854 *val = obj->properties->values[i];
2855 return 0;
2856 }
2857 }
2858
2859 return -EINVAL;
2860}
2861EXPORT_SYMBOL(drm_object_property_get_value);
2862
Dave Airlief453ba02008-11-07 14:05:41 -08002863int drm_mode_getproperty_ioctl(struct drm_device *dev,
2864 void *data, struct drm_file *file_priv)
2865{
2866 struct drm_mode_object *obj;
2867 struct drm_mode_get_property *out_resp = data;
2868 struct drm_property *property;
2869 int enum_count = 0;
2870 int blob_count = 0;
2871 int value_count = 0;
2872 int ret = 0, i;
2873 int copied;
2874 struct drm_property_enum *prop_enum;
2875 struct drm_mode_property_enum __user *enum_ptr;
2876 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002877 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002878 uint64_t __user *values_ptr;
2879 uint32_t __user *blob_length_ptr;
2880
Dave Airliefb3b06c2011-02-08 13:55:21 +10002881 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2882 return -EINVAL;
2883
Daniel Vetter84849902012-12-02 00:28:11 +01002884 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002885 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2886 if (!obj) {
2887 ret = -EINVAL;
2888 goto done;
2889 }
2890 property = obj_to_property(obj);
2891
Rob Clark49e27542012-05-17 02:23:26 -06002892 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002893 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2894 enum_count++;
2895 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2896 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2897 blob_count++;
2898 }
2899
2900 value_count = property->num_values;
2901
2902 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2903 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2904 out_resp->flags = property->flags;
2905
2906 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002907 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002908 for (i = 0; i < value_count; i++) {
2909 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2910 ret = -EFAULT;
2911 goto done;
2912 }
2913 }
2914 }
2915 out_resp->count_values = value_count;
2916
Rob Clark49e27542012-05-17 02:23:26 -06002917 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002918 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2919 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002920 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002921 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2922
2923 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2924 ret = -EFAULT;
2925 goto done;
2926 }
2927
2928 if (copy_to_user(&enum_ptr[copied].name,
2929 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2930 ret = -EFAULT;
2931 goto done;
2932 }
2933 copied++;
2934 }
2935 }
2936 out_resp->count_enum_blobs = enum_count;
2937 }
2938
2939 if (property->flags & DRM_MODE_PROP_BLOB) {
2940 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2941 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002942 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2943 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002944
2945 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2946 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2947 ret = -EFAULT;
2948 goto done;
2949 }
2950
2951 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2952 ret = -EFAULT;
2953 goto done;
2954 }
2955
2956 copied++;
2957 }
2958 }
2959 out_resp->count_enum_blobs = blob_count;
2960 }
2961done:
Daniel Vetter84849902012-12-02 00:28:11 +01002962 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002963 return ret;
2964}
2965
2966static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2967 void *data)
2968{
2969 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002970 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002971
2972 if (!length || !data)
2973 return NULL;
2974
2975 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2976 if (!blob)
2977 return NULL;
2978
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002979 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2980 if (ret) {
2981 kfree(blob);
2982 return NULL;
2983 }
2984
Dave Airlief453ba02008-11-07 14:05:41 -08002985 blob->length = length;
2986
2987 memcpy(blob->data, data, length);
2988
Dave Airlief453ba02008-11-07 14:05:41 -08002989 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2990 return blob;
2991}
2992
2993static void drm_property_destroy_blob(struct drm_device *dev,
2994 struct drm_property_blob *blob)
2995{
2996 drm_mode_object_put(dev, &blob->base);
2997 list_del(&blob->head);
2998 kfree(blob);
2999}
3000
3001int drm_mode_getblob_ioctl(struct drm_device *dev,
3002 void *data, struct drm_file *file_priv)
3003{
3004 struct drm_mode_object *obj;
3005 struct drm_mode_get_blob *out_resp = data;
3006 struct drm_property_blob *blob;
3007 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003008 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003009
Dave Airliefb3b06c2011-02-08 13:55:21 +10003010 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3011 return -EINVAL;
3012
Daniel Vetter84849902012-12-02 00:28:11 +01003013 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003014 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3015 if (!obj) {
3016 ret = -EINVAL;
3017 goto done;
3018 }
3019 blob = obj_to_blob(obj);
3020
3021 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003022 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003023 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3024 ret = -EFAULT;
3025 goto done;
3026 }
3027 }
3028 out_resp->length = blob->length;
3029
3030done:
Daniel Vetter84849902012-12-02 00:28:11 +01003031 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003032 return ret;
3033}
3034
3035int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3036 struct edid *edid)
3037{
3038 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003039 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003040
3041 if (connector->edid_blob_ptr)
3042 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3043
3044 /* Delete edid, when there is none. */
3045 if (!edid) {
3046 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003047 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003048 return ret;
3049 }
3050
Adam Jackson7466f4c2010-03-29 21:43:23 +00003051 size = EDID_LENGTH * (1 + edid->extensions);
3052 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3053 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003054 if (!connector->edid_blob_ptr)
3055 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003056
Rob Clark58495562012-10-11 20:50:56 -05003057 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003058 dev->mode_config.edid_property,
3059 connector->edid_blob_ptr->base.id);
3060
3061 return ret;
3062}
3063EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3064
Paulo Zanoni26a34812012-05-15 18:08:59 -03003065static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003066 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003067{
3068 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3069 return false;
3070 if (property->flags & DRM_MODE_PROP_RANGE) {
3071 if (value < property->values[0] || value > property->values[1])
3072 return false;
3073 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003074 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3075 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003076 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003077 for (i = 0; i < property->num_values; i++)
3078 valid_mask |= (1ULL << property->values[i]);
3079 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003080 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3081 /* Only the driver knows */
3082 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003083 } else {
3084 int i;
3085 for (i = 0; i < property->num_values; i++)
3086 if (property->values[i] == value)
3087 return true;
3088 return false;
3089 }
3090}
3091
Dave Airlief453ba02008-11-07 14:05:41 -08003092int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3093 void *data, struct drm_file *file_priv)
3094{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003095 struct drm_mode_connector_set_property *conn_set_prop = data;
3096 struct drm_mode_obj_set_property obj_set_prop = {
3097 .value = conn_set_prop->value,
3098 .prop_id = conn_set_prop->prop_id,
3099 .obj_id = conn_set_prop->connector_id,
3100 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3101 };
Dave Airlief453ba02008-11-07 14:05:41 -08003102
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003103 /* It does all the locking and checking we need */
3104 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003105}
3106
Paulo Zanonic5431882012-05-15 18:09:02 -03003107static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3108 struct drm_property *property,
3109 uint64_t value)
3110{
3111 int ret = -EINVAL;
3112 struct drm_connector *connector = obj_to_connector(obj);
3113
3114 /* Do DPMS ourselves */
3115 if (property == connector->dev->mode_config.dpms_property) {
3116 if (connector->funcs->dpms)
3117 (*connector->funcs->dpms)(connector, (int)value);
3118 ret = 0;
3119 } else if (connector->funcs->set_property)
3120 ret = connector->funcs->set_property(connector, property, value);
3121
3122 /* store the property value if successful */
3123 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003124 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003125 return ret;
3126}
3127
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003128static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3129 struct drm_property *property,
3130 uint64_t value)
3131{
3132 int ret = -EINVAL;
3133 struct drm_crtc *crtc = obj_to_crtc(obj);
3134
3135 if (crtc->funcs->set_property)
3136 ret = crtc->funcs->set_property(crtc, property, value);
3137 if (!ret)
3138 drm_object_property_set_value(obj, property, value);
3139
3140 return ret;
3141}
3142
Rob Clark4d939142012-05-17 02:23:27 -06003143static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3144 struct drm_property *property,
3145 uint64_t value)
3146{
3147 int ret = -EINVAL;
3148 struct drm_plane *plane = obj_to_plane(obj);
3149
3150 if (plane->funcs->set_property)
3151 ret = plane->funcs->set_property(plane, property, value);
3152 if (!ret)
3153 drm_object_property_set_value(obj, property, value);
3154
3155 return ret;
3156}
3157
Paulo Zanonic5431882012-05-15 18:09:02 -03003158int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3159 struct drm_file *file_priv)
3160{
3161 struct drm_mode_obj_get_properties *arg = data;
3162 struct drm_mode_object *obj;
3163 int ret = 0;
3164 int i;
3165 int copied = 0;
3166 int props_count = 0;
3167 uint32_t __user *props_ptr;
3168 uint64_t __user *prop_values_ptr;
3169
3170 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3171 return -EINVAL;
3172
Daniel Vetter84849902012-12-02 00:28:11 +01003173 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003174
3175 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3176 if (!obj) {
3177 ret = -EINVAL;
3178 goto out;
3179 }
3180 if (!obj->properties) {
3181 ret = -EINVAL;
3182 goto out;
3183 }
3184
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003185 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003186
3187 /* This ioctl is called twice, once to determine how much space is
3188 * needed, and the 2nd time to fill it. */
3189 if ((arg->count_props >= props_count) && props_count) {
3190 copied = 0;
3191 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3192 prop_values_ptr = (uint64_t __user *)(unsigned long)
3193 (arg->prop_values_ptr);
3194 for (i = 0; i < props_count; i++) {
3195 if (put_user(obj->properties->ids[i],
3196 props_ptr + copied)) {
3197 ret = -EFAULT;
3198 goto out;
3199 }
3200 if (put_user(obj->properties->values[i],
3201 prop_values_ptr + copied)) {
3202 ret = -EFAULT;
3203 goto out;
3204 }
3205 copied++;
3206 }
3207 }
3208 arg->count_props = props_count;
3209out:
Daniel Vetter84849902012-12-02 00:28:11 +01003210 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003211 return ret;
3212}
3213
3214int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3215 struct drm_file *file_priv)
3216{
3217 struct drm_mode_obj_set_property *arg = data;
3218 struct drm_mode_object *arg_obj;
3219 struct drm_mode_object *prop_obj;
3220 struct drm_property *property;
3221 int ret = -EINVAL;
3222 int i;
3223
3224 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3225 return -EINVAL;
3226
Daniel Vetter84849902012-12-02 00:28:11 +01003227 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003228
3229 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3230 if (!arg_obj)
3231 goto out;
3232 if (!arg_obj->properties)
3233 goto out;
3234
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003235 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003236 if (arg_obj->properties->ids[i] == arg->prop_id)
3237 break;
3238
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003239 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003240 goto out;
3241
3242 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3243 DRM_MODE_OBJECT_PROPERTY);
3244 if (!prop_obj)
3245 goto out;
3246 property = obj_to_property(prop_obj);
3247
3248 if (!drm_property_change_is_valid(property, arg->value))
3249 goto out;
3250
3251 switch (arg_obj->type) {
3252 case DRM_MODE_OBJECT_CONNECTOR:
3253 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3254 arg->value);
3255 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003256 case DRM_MODE_OBJECT_CRTC:
3257 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3258 break;
Rob Clark4d939142012-05-17 02:23:27 -06003259 case DRM_MODE_OBJECT_PLANE:
3260 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3261 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003262 }
3263
3264out:
Daniel Vetter84849902012-12-02 00:28:11 +01003265 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003266 return ret;
3267}
3268
Dave Airlief453ba02008-11-07 14:05:41 -08003269int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3270 struct drm_encoder *encoder)
3271{
3272 int i;
3273
3274 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3275 if (connector->encoder_ids[i] == 0) {
3276 connector->encoder_ids[i] = encoder->base.id;
3277 return 0;
3278 }
3279 }
3280 return -ENOMEM;
3281}
3282EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3283
3284void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3285 struct drm_encoder *encoder)
3286{
3287 int i;
3288 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3289 if (connector->encoder_ids[i] == encoder->base.id) {
3290 connector->encoder_ids[i] = 0;
3291 if (connector->encoder == encoder)
3292 connector->encoder = NULL;
3293 break;
3294 }
3295 }
3296}
3297EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3298
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003299int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003300 int gamma_size)
3301{
3302 crtc->gamma_size = gamma_size;
3303
3304 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3305 if (!crtc->gamma_store) {
3306 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003307 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003308 }
3309
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003310 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003311}
3312EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3313
3314int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3315 void *data, struct drm_file *file_priv)
3316{
3317 struct drm_mode_crtc_lut *crtc_lut = data;
3318 struct drm_mode_object *obj;
3319 struct drm_crtc *crtc;
3320 void *r_base, *g_base, *b_base;
3321 int size;
3322 int ret = 0;
3323
Dave Airliefb3b06c2011-02-08 13:55:21 +10003324 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3325 return -EINVAL;
3326
Daniel Vetter84849902012-12-02 00:28:11 +01003327 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003328 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3329 if (!obj) {
3330 ret = -EINVAL;
3331 goto out;
3332 }
3333 crtc = obj_to_crtc(obj);
3334
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003335 if (crtc->funcs->gamma_set == NULL) {
3336 ret = -ENOSYS;
3337 goto out;
3338 }
3339
Dave Airlief453ba02008-11-07 14:05:41 -08003340 /* memcpy into gamma store */
3341 if (crtc_lut->gamma_size != crtc->gamma_size) {
3342 ret = -EINVAL;
3343 goto out;
3344 }
3345
3346 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3347 r_base = crtc->gamma_store;
3348 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3349 ret = -EFAULT;
3350 goto out;
3351 }
3352
3353 g_base = r_base + size;
3354 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3355 ret = -EFAULT;
3356 goto out;
3357 }
3358
3359 b_base = g_base + size;
3360 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3361 ret = -EFAULT;
3362 goto out;
3363 }
3364
James Simmons72034252010-08-03 01:33:19 +01003365 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003366
3367out:
Daniel Vetter84849902012-12-02 00:28:11 +01003368 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003369 return ret;
3370
3371}
3372
3373int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3374 void *data, struct drm_file *file_priv)
3375{
3376 struct drm_mode_crtc_lut *crtc_lut = data;
3377 struct drm_mode_object *obj;
3378 struct drm_crtc *crtc;
3379 void *r_base, *g_base, *b_base;
3380 int size;
3381 int ret = 0;
3382
Dave Airliefb3b06c2011-02-08 13:55:21 +10003383 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3384 return -EINVAL;
3385
Daniel Vetter84849902012-12-02 00:28:11 +01003386 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003387 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3388 if (!obj) {
3389 ret = -EINVAL;
3390 goto out;
3391 }
3392 crtc = obj_to_crtc(obj);
3393
3394 /* memcpy into gamma store */
3395 if (crtc_lut->gamma_size != crtc->gamma_size) {
3396 ret = -EINVAL;
3397 goto out;
3398 }
3399
3400 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3401 r_base = crtc->gamma_store;
3402 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3403 ret = -EFAULT;
3404 goto out;
3405 }
3406
3407 g_base = r_base + size;
3408 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3409 ret = -EFAULT;
3410 goto out;
3411 }
3412
3413 b_base = g_base + size;
3414 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3415 ret = -EFAULT;
3416 goto out;
3417 }
3418out:
Daniel Vetter84849902012-12-02 00:28:11 +01003419 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003420 return ret;
3421}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003422
3423int drm_mode_page_flip_ioctl(struct drm_device *dev,
3424 void *data, struct drm_file *file_priv)
3425{
3426 struct drm_mode_crtc_page_flip *page_flip = data;
3427 struct drm_mode_object *obj;
3428 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003429 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003430 struct drm_pending_vblank_event *e = NULL;
3431 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003432 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003433 int ret = -EINVAL;
3434
3435 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3436 page_flip->reserved != 0)
3437 return -EINVAL;
3438
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003439 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3440 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003441 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003442 crtc = obj_to_crtc(obj);
3443
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003444 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003445 if (crtc->fb == NULL) {
3446 /* The framebuffer is currently unbound, presumably
3447 * due to a hotplug event, that userspace has not
3448 * yet discovered.
3449 */
3450 ret = -EBUSY;
3451 goto out;
3452 }
3453
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003454 if (crtc->funcs->page_flip == NULL)
3455 goto out;
3456
Daniel Vetter786b99e2012-12-02 21:53:40 +01003457 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3458 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003459 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003460
Rob Clark7c80e122012-09-04 16:35:56 +00003461 hdisplay = crtc->mode.hdisplay;
3462 vdisplay = crtc->mode.vdisplay;
3463
3464 if (crtc->invert_dimensions)
3465 swap(hdisplay, vdisplay);
3466
3467 if (hdisplay > fb->width ||
3468 vdisplay > fb->height ||
3469 crtc->x > fb->width - hdisplay ||
3470 crtc->y > fb->height - vdisplay) {
3471 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3472 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3473 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003474 ret = -ENOSPC;
3475 goto out;
3476 }
3477
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003478 if (crtc->fb->pixel_format != fb->pixel_format) {
3479 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3480 ret = -EINVAL;
3481 goto out;
3482 }
3483
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003484 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3485 ret = -ENOMEM;
3486 spin_lock_irqsave(&dev->event_lock, flags);
3487 if (file_priv->event_space < sizeof e->event) {
3488 spin_unlock_irqrestore(&dev->event_lock, flags);
3489 goto out;
3490 }
3491 file_priv->event_space -= sizeof e->event;
3492 spin_unlock_irqrestore(&dev->event_lock, flags);
3493
3494 e = kzalloc(sizeof *e, GFP_KERNEL);
3495 if (e == NULL) {
3496 spin_lock_irqsave(&dev->event_lock, flags);
3497 file_priv->event_space += sizeof e->event;
3498 spin_unlock_irqrestore(&dev->event_lock, flags);
3499 goto out;
3500 }
3501
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003502 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003503 e->event.base.length = sizeof e->event;
3504 e->event.user_data = page_flip->user_data;
3505 e->base.event = &e->event.base;
3506 e->base.file_priv = file_priv;
3507 e->base.destroy =
3508 (void (*) (struct drm_pending_event *)) kfree;
3509 }
3510
Daniel Vetterb0d12322012-12-11 01:07:12 +01003511 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003512 ret = crtc->funcs->page_flip(crtc, fb, e);
3513 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003514 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3515 spin_lock_irqsave(&dev->event_lock, flags);
3516 file_priv->event_space += sizeof e->event;
3517 spin_unlock_irqrestore(&dev->event_lock, flags);
3518 kfree(e);
3519 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003520 /* Keep the old fb, don't unref it. */
3521 old_fb = NULL;
3522 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003523 /*
3524 * Warn if the driver hasn't properly updated the crtc->fb
3525 * field to reflect that the new framebuffer is now used.
3526 * Failing to do so will screw with the reference counting
3527 * on framebuffers.
3528 */
3529 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003530 /* Unref only the old framebuffer. */
3531 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003532 }
3533
3534out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003535 if (fb)
3536 drm_framebuffer_unreference(fb);
3537 if (old_fb)
3538 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003539 mutex_unlock(&crtc->mutex);
3540
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003541 return ret;
3542}
Chris Wilsoneb033552011-01-24 15:11:08 +00003543
3544void drm_mode_config_reset(struct drm_device *dev)
3545{
3546 struct drm_crtc *crtc;
3547 struct drm_encoder *encoder;
3548 struct drm_connector *connector;
3549
3550 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3551 if (crtc->funcs->reset)
3552 crtc->funcs->reset(crtc);
3553
3554 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3555 if (encoder->funcs->reset)
3556 encoder->funcs->reset(encoder);
3557
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003558 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3559 connector->status = connector_status_unknown;
3560
Chris Wilsoneb033552011-01-24 15:11:08 +00003561 if (connector->funcs->reset)
3562 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003563 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003564}
3565EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003566
3567int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3568 void *data, struct drm_file *file_priv)
3569{
3570 struct drm_mode_create_dumb *args = data;
3571
3572 if (!dev->driver->dumb_create)
3573 return -ENOSYS;
3574 return dev->driver->dumb_create(file_priv, dev, args);
3575}
3576
3577int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3578 void *data, struct drm_file *file_priv)
3579{
3580 struct drm_mode_map_dumb *args = data;
3581
3582 /* call driver ioctl to get mmap offset */
3583 if (!dev->driver->dumb_map_offset)
3584 return -ENOSYS;
3585
3586 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3587}
3588
3589int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3590 void *data, struct drm_file *file_priv)
3591{
3592 struct drm_mode_destroy_dumb *args = data;
3593
3594 if (!dev->driver->dumb_destroy)
3595 return -ENOSYS;
3596
3597 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3598}
Dave Airlie248dbc22011-11-29 20:02:54 +00003599
3600/*
3601 * Just need to support RGB formats here for compat with code that doesn't
3602 * use pixel formats directly yet.
3603 */
3604void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3605 int *bpp)
3606{
3607 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003608 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003609 case DRM_FORMAT_RGB332:
3610 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003611 *depth = 8;
3612 *bpp = 8;
3613 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003614 case DRM_FORMAT_XRGB1555:
3615 case DRM_FORMAT_XBGR1555:
3616 case DRM_FORMAT_RGBX5551:
3617 case DRM_FORMAT_BGRX5551:
3618 case DRM_FORMAT_ARGB1555:
3619 case DRM_FORMAT_ABGR1555:
3620 case DRM_FORMAT_RGBA5551:
3621 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003622 *depth = 15;
3623 *bpp = 16;
3624 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003625 case DRM_FORMAT_RGB565:
3626 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003627 *depth = 16;
3628 *bpp = 16;
3629 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003630 case DRM_FORMAT_RGB888:
3631 case DRM_FORMAT_BGR888:
3632 *depth = 24;
3633 *bpp = 24;
3634 break;
3635 case DRM_FORMAT_XRGB8888:
3636 case DRM_FORMAT_XBGR8888:
3637 case DRM_FORMAT_RGBX8888:
3638 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003639 *depth = 24;
3640 *bpp = 32;
3641 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003642 case DRM_FORMAT_XRGB2101010:
3643 case DRM_FORMAT_XBGR2101010:
3644 case DRM_FORMAT_RGBX1010102:
3645 case DRM_FORMAT_BGRX1010102:
3646 case DRM_FORMAT_ARGB2101010:
3647 case DRM_FORMAT_ABGR2101010:
3648 case DRM_FORMAT_RGBA1010102:
3649 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003650 *depth = 30;
3651 *bpp = 32;
3652 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003653 case DRM_FORMAT_ARGB8888:
3654 case DRM_FORMAT_ABGR8888:
3655 case DRM_FORMAT_RGBA8888:
3656 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003657 *depth = 32;
3658 *bpp = 32;
3659 break;
3660 default:
3661 DRM_DEBUG_KMS("unsupported pixel format\n");
3662 *depth = 0;
3663 *bpp = 0;
3664 break;
3665 }
3666}
3667EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003668
3669/**
3670 * drm_format_num_planes - get the number of planes for format
3671 * @format: pixel format (DRM_FORMAT_*)
3672 *
3673 * RETURNS:
3674 * The number of planes used by the specified pixel format.
3675 */
3676int drm_format_num_planes(uint32_t format)
3677{
3678 switch (format) {
3679 case DRM_FORMAT_YUV410:
3680 case DRM_FORMAT_YVU410:
3681 case DRM_FORMAT_YUV411:
3682 case DRM_FORMAT_YVU411:
3683 case DRM_FORMAT_YUV420:
3684 case DRM_FORMAT_YVU420:
3685 case DRM_FORMAT_YUV422:
3686 case DRM_FORMAT_YVU422:
3687 case DRM_FORMAT_YUV444:
3688 case DRM_FORMAT_YVU444:
3689 return 3;
3690 case DRM_FORMAT_NV12:
3691 case DRM_FORMAT_NV21:
3692 case DRM_FORMAT_NV16:
3693 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003694 case DRM_FORMAT_NV24:
3695 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003696 return 2;
3697 default:
3698 return 1;
3699 }
3700}
3701EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003702
3703/**
3704 * drm_format_plane_cpp - determine the bytes per pixel value
3705 * @format: pixel format (DRM_FORMAT_*)
3706 * @plane: plane index
3707 *
3708 * RETURNS:
3709 * The bytes per pixel value for the specified plane.
3710 */
3711int drm_format_plane_cpp(uint32_t format, int plane)
3712{
3713 unsigned int depth;
3714 int bpp;
3715
3716 if (plane >= drm_format_num_planes(format))
3717 return 0;
3718
3719 switch (format) {
3720 case DRM_FORMAT_YUYV:
3721 case DRM_FORMAT_YVYU:
3722 case DRM_FORMAT_UYVY:
3723 case DRM_FORMAT_VYUY:
3724 return 2;
3725 case DRM_FORMAT_NV12:
3726 case DRM_FORMAT_NV21:
3727 case DRM_FORMAT_NV16:
3728 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003729 case DRM_FORMAT_NV24:
3730 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003731 return plane ? 2 : 1;
3732 case DRM_FORMAT_YUV410:
3733 case DRM_FORMAT_YVU410:
3734 case DRM_FORMAT_YUV411:
3735 case DRM_FORMAT_YVU411:
3736 case DRM_FORMAT_YUV420:
3737 case DRM_FORMAT_YVU420:
3738 case DRM_FORMAT_YUV422:
3739 case DRM_FORMAT_YVU422:
3740 case DRM_FORMAT_YUV444:
3741 case DRM_FORMAT_YVU444:
3742 return 1;
3743 default:
3744 drm_fb_get_bpp_depth(format, &depth, &bpp);
3745 return bpp >> 3;
3746 }
3747}
3748EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003749
3750/**
3751 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3752 * @format: pixel format (DRM_FORMAT_*)
3753 *
3754 * RETURNS:
3755 * The horizontal chroma subsampling factor for the
3756 * specified pixel format.
3757 */
3758int drm_format_horz_chroma_subsampling(uint32_t format)
3759{
3760 switch (format) {
3761 case DRM_FORMAT_YUV411:
3762 case DRM_FORMAT_YVU411:
3763 case DRM_FORMAT_YUV410:
3764 case DRM_FORMAT_YVU410:
3765 return 4;
3766 case DRM_FORMAT_YUYV:
3767 case DRM_FORMAT_YVYU:
3768 case DRM_FORMAT_UYVY:
3769 case DRM_FORMAT_VYUY:
3770 case DRM_FORMAT_NV12:
3771 case DRM_FORMAT_NV21:
3772 case DRM_FORMAT_NV16:
3773 case DRM_FORMAT_NV61:
3774 case DRM_FORMAT_YUV422:
3775 case DRM_FORMAT_YVU422:
3776 case DRM_FORMAT_YUV420:
3777 case DRM_FORMAT_YVU420:
3778 return 2;
3779 default:
3780 return 1;
3781 }
3782}
3783EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3784
3785/**
3786 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3787 * @format: pixel format (DRM_FORMAT_*)
3788 *
3789 * RETURNS:
3790 * The vertical chroma subsampling factor for the
3791 * specified pixel format.
3792 */
3793int drm_format_vert_chroma_subsampling(uint32_t format)
3794{
3795 switch (format) {
3796 case DRM_FORMAT_YUV410:
3797 case DRM_FORMAT_YVU410:
3798 return 4;
3799 case DRM_FORMAT_YUV420:
3800 case DRM_FORMAT_YVU420:
3801 case DRM_FORMAT_NV12:
3802 case DRM_FORMAT_NV21:
3803 return 2;
3804 default:
3805 return 1;
3806 }
3807}
3808EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003809
3810/**
3811 * drm_mode_config_init - initialize DRM mode_configuration structure
3812 * @dev: DRM device
3813 *
3814 * Initialize @dev's mode_config structure, used for tracking the graphics
3815 * configuration of @dev.
3816 *
3817 * Since this initializes the modeset locks, no locking is possible. Which is no
3818 * problem, since this should happen single threaded at init time. It is the
3819 * driver's problem to ensure this guarantee.
3820 *
3821 */
3822void drm_mode_config_init(struct drm_device *dev)
3823{
3824 mutex_init(&dev->mode_config.mutex);
3825 mutex_init(&dev->mode_config.idr_mutex);
3826 mutex_init(&dev->mode_config.fb_lock);
3827 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3828 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3829 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3830 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3831 INIT_LIST_HEAD(&dev->mode_config.property_list);
3832 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3833 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3834 idr_init(&dev->mode_config.crtc_idr);
3835
3836 drm_modeset_lock_all(dev);
3837 drm_mode_create_standard_connector_properties(dev);
3838 drm_modeset_unlock_all(dev);
3839
3840 /* Just to be sure */
3841 dev->mode_config.num_fb = 0;
3842 dev->mode_config.num_connector = 0;
3843 dev->mode_config.num_crtc = 0;
3844 dev->mode_config.num_encoder = 0;
3845}
3846EXPORT_SYMBOL(drm_mode_config_init);
3847
3848/**
3849 * drm_mode_config_cleanup - free up DRM mode_config info
3850 * @dev: DRM device
3851 *
3852 * Free up all the connectors and CRTCs associated with this DRM device, then
3853 * free up the framebuffers and associated buffer objects.
3854 *
3855 * Note that since this /should/ happen single-threaded at driver/device
3856 * teardown time, no locking is required. It's the driver's job to ensure that
3857 * this guarantee actually holds true.
3858 *
3859 * FIXME: cleanup any dangling user buffer objects too
3860 */
3861void drm_mode_config_cleanup(struct drm_device *dev)
3862{
3863 struct drm_connector *connector, *ot;
3864 struct drm_crtc *crtc, *ct;
3865 struct drm_encoder *encoder, *enct;
3866 struct drm_framebuffer *fb, *fbt;
3867 struct drm_property *property, *pt;
3868 struct drm_property_blob *blob, *bt;
3869 struct drm_plane *plane, *plt;
3870
3871 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3872 head) {
3873 encoder->funcs->destroy(encoder);
3874 }
3875
3876 list_for_each_entry_safe(connector, ot,
3877 &dev->mode_config.connector_list, head) {
3878 connector->funcs->destroy(connector);
3879 }
3880
3881 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3882 head) {
3883 drm_property_destroy(dev, property);
3884 }
3885
3886 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3887 head) {
3888 drm_property_destroy_blob(dev, blob);
3889 }
3890
3891 /*
3892 * Single-threaded teardown context, so it's not required to grab the
3893 * fb_lock to protect against concurrent fb_list access. Contrary, it
3894 * would actually deadlock with the drm_framebuffer_cleanup function.
3895 *
3896 * Also, if there are any framebuffers left, that's a driver leak now,
3897 * so politely WARN about this.
3898 */
3899 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3900 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3901 drm_framebuffer_remove(fb);
3902 }
3903
3904 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3905 head) {
3906 plane->funcs->destroy(plane);
3907 }
3908
3909 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3910 crtc->funcs->destroy(crtc);
3911 }
3912
3913 idr_destroy(&dev->mode_config.crtc_idr);
3914}
3915EXPORT_SYMBOL(drm_mode_config_cleanup);