blob: baee5752ca6bde3349fc8c40e1725711e48e657c [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 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000619 * Inits a new object created as base part of a 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/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000654 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800655 * @crtc: CRTC to cleanup
656 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000657 * This function cleans up @crtc and removes it from the DRM mode setting
658 * core. Note that the function does *not* free the crtc structure itself,
659 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800660 */
661void drm_crtc_cleanup(struct drm_crtc *crtc)
662{
663 struct drm_device *dev = crtc->dev;
664
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000665 kfree(crtc->gamma_store);
666 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800667
668 drm_mode_object_put(dev, &crtc->base);
669 list_del(&crtc->head);
670 dev->mode_config.num_crtc--;
671}
672EXPORT_SYMBOL(drm_crtc_cleanup);
673
674/**
675 * drm_mode_probed_add - add a mode to a connector's probed mode list
676 * @connector: connector the new mode
677 * @mode: mode data
678 *
Dave Airlief453ba02008-11-07 14:05:41 -0800679 * Add @mode to @connector's mode list for later use.
680 */
681void drm_mode_probed_add(struct drm_connector *connector,
682 struct drm_display_mode *mode)
683{
Ville Syrjälä990256a2013-05-31 12:17:07 +0000684 list_add_tail(&mode->head, &connector->probed_modes);
Dave Airlief453ba02008-11-07 14:05:41 -0800685}
686EXPORT_SYMBOL(drm_mode_probed_add);
687
688/**
689 * drm_mode_remove - remove and free a mode
690 * @connector: connector list to modify
691 * @mode: mode to remove
692 *
Dave Airlief453ba02008-11-07 14:05:41 -0800693 * Remove @mode from @connector's mode list, then free it.
694 */
695void drm_mode_remove(struct drm_connector *connector,
696 struct drm_display_mode *mode)
697{
698 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100699 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800700}
701EXPORT_SYMBOL(drm_mode_remove);
702
703/**
704 * drm_connector_init - Init a preallocated connector
705 * @dev: DRM device
706 * @connector: the connector to init
707 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100708 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800709 *
Dave Airlief453ba02008-11-07 14:05:41 -0800710 * Initialises a preallocated connector. Connectors should be
711 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200712 *
713 * RETURNS:
714 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800715 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200716int drm_connector_init(struct drm_device *dev,
717 struct drm_connector *connector,
718 const struct drm_connector_funcs *funcs,
719 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800720{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200721 int ret;
722
Daniel Vetter84849902012-12-02 00:28:11 +0100723 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800724
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200725 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
726 if (ret)
727 goto out;
728
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300729 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800730 connector->dev = dev;
731 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800732 connector->connector_type = connector_type;
733 connector->connector_type_id =
734 ++drm_connector_enum_list[connector_type].count; /* TODO */
Dave Airlief453ba02008-11-07 14:05:41 -0800735 INIT_LIST_HEAD(&connector->probed_modes);
736 INIT_LIST_HEAD(&connector->modes);
737 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000738 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800739
740 list_add_tail(&connector->head, &dev->mode_config.connector_list);
741 dev->mode_config.num_connector++;
742
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200743 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500744 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200745 dev->mode_config.edid_property,
746 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800747
Rob Clark58495562012-10-11 20:50:56 -0500748 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800749 dev->mode_config.dpms_property, 0);
750
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200751 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100752 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200753
754 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800755}
756EXPORT_SYMBOL(drm_connector_init);
757
758/**
759 * drm_connector_cleanup - cleans up an initialised connector
760 * @connector: connector to cleanup
761 *
Dave Airlief453ba02008-11-07 14:05:41 -0800762 * Cleans up the connector but doesn't free the object.
763 */
764void drm_connector_cleanup(struct drm_connector *connector)
765{
766 struct drm_device *dev = connector->dev;
767 struct drm_display_mode *mode, *t;
768
769 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
770 drm_mode_remove(connector, mode);
771
772 list_for_each_entry_safe(mode, t, &connector->modes, head)
773 drm_mode_remove(connector, mode);
774
Dave Airlief453ba02008-11-07 14:05:41 -0800775 drm_mode_object_put(dev, &connector->base);
776 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000777 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800778}
779EXPORT_SYMBOL(drm_connector_cleanup);
780
Dave Airliecbc7e222012-02-20 14:16:40 +0000781void drm_connector_unplug_all(struct drm_device *dev)
782{
783 struct drm_connector *connector;
784
785 /* taking the mode config mutex ends up in a clash with sysfs */
786 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
787 drm_sysfs_connector_remove(connector);
788
789}
790EXPORT_SYMBOL(drm_connector_unplug_all);
791
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200792int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000793 struct drm_encoder *encoder,
794 const struct drm_encoder_funcs *funcs,
795 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800796{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200797 int ret;
798
Daniel Vetter84849902012-12-02 00:28:11 +0100799 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800800
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200801 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
802 if (ret)
803 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800804
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200805 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800806 encoder->encoder_type = encoder_type;
807 encoder->funcs = funcs;
808
809 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
810 dev->mode_config.num_encoder++;
811
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200812 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100813 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200814
815 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800816}
817EXPORT_SYMBOL(drm_encoder_init);
818
819void drm_encoder_cleanup(struct drm_encoder *encoder)
820{
821 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100822 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800823 drm_mode_object_put(dev, &encoder->base);
824 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000825 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100826 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800827}
828EXPORT_SYMBOL(drm_encoder_cleanup);
829
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800830int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
831 unsigned long possible_crtcs,
832 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600833 const uint32_t *formats, uint32_t format_count,
834 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800835{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200836 int ret;
837
Daniel Vetter84849902012-12-02 00:28:11 +0100838 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800839
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200840 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
841 if (ret)
842 goto out;
843
Rob Clark4d939142012-05-17 02:23:27 -0600844 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800845 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800846 plane->funcs = funcs;
847 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
848 GFP_KERNEL);
849 if (!plane->format_types) {
850 DRM_DEBUG_KMS("out of memory when allocating plane\n");
851 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200852 ret = -ENOMEM;
853 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800854 }
855
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800856 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800857 plane->format_count = format_count;
858 plane->possible_crtcs = possible_crtcs;
859
Rob Clark0a7eb242011-12-13 20:19:36 -0600860 /* private planes are not exposed to userspace, but depending on
861 * display hardware, might be convenient to allow sharing programming
862 * for the scanout engine with the crtc implementation.
863 */
864 if (!priv) {
865 list_add_tail(&plane->head, &dev->mode_config.plane_list);
866 dev->mode_config.num_plane++;
867 } else {
868 INIT_LIST_HEAD(&plane->head);
869 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800870
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200871 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100872 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800873
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200874 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800875}
876EXPORT_SYMBOL(drm_plane_init);
877
878void drm_plane_cleanup(struct drm_plane *plane)
879{
880 struct drm_device *dev = plane->dev;
881
Daniel Vetter84849902012-12-02 00:28:11 +0100882 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800883 kfree(plane->format_types);
884 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600885 /* if not added to a list, it must be a private plane */
886 if (!list_empty(&plane->head)) {
887 list_del(&plane->head);
888 dev->mode_config.num_plane--;
889 }
Daniel Vetter84849902012-12-02 00:28:11 +0100890 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800891}
892EXPORT_SYMBOL(drm_plane_cleanup);
893
Dave Airlief453ba02008-11-07 14:05:41 -0800894/**
895 * drm_mode_create - create a new display mode
896 * @dev: DRM device
897 *
Dave Airlief453ba02008-11-07 14:05:41 -0800898 * Create a new drm_display_mode, give it an ID, and return it.
899 *
900 * RETURNS:
901 * Pointer to new mode on success, NULL on error.
902 */
903struct drm_display_mode *drm_mode_create(struct drm_device *dev)
904{
905 struct drm_display_mode *nmode;
906
907 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
908 if (!nmode)
909 return NULL;
910
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200911 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
912 kfree(nmode);
913 return NULL;
914 }
915
Dave Airlief453ba02008-11-07 14:05:41 -0800916 return nmode;
917}
918EXPORT_SYMBOL(drm_mode_create);
919
920/**
921 * drm_mode_destroy - remove a mode
922 * @dev: DRM device
923 * @mode: mode to remove
924 *
Dave Airlief453ba02008-11-07 14:05:41 -0800925 * Free @mode's unique identifier, then free it.
926 */
927void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
928{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200929 if (!mode)
930 return;
931
Dave Airlief453ba02008-11-07 14:05:41 -0800932 drm_mode_object_put(dev, &mode->base);
933
934 kfree(mode);
935}
936EXPORT_SYMBOL(drm_mode_destroy);
937
938static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
939{
940 struct drm_property *edid;
941 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800942
943 /*
944 * Standard properties (apply to all connectors)
945 */
946 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
947 DRM_MODE_PROP_IMMUTABLE,
948 "EDID", 0);
949 dev->mode_config.edid_property = edid;
950
Sascha Hauer4a67d392012-02-06 10:58:17 +0100951 dpms = drm_property_create_enum(dev, 0,
952 "DPMS", drm_dpms_enum_list,
953 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800954 dev->mode_config.dpms_property = dpms;
955
956 return 0;
957}
958
959/**
960 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
961 * @dev: DRM device
962 *
963 * Called by a driver the first time a DVI-I connector is made.
964 */
965int drm_mode_create_dvi_i_properties(struct drm_device *dev)
966{
967 struct drm_property *dvi_i_selector;
968 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800969
970 if (dev->mode_config.dvi_i_select_subconnector_property)
971 return 0;
972
973 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100974 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800975 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100976 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800977 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800978 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
979
Sascha Hauer4a67d392012-02-06 10:58:17 +0100980 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800981 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100982 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800983 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800984 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
985
986 return 0;
987}
988EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
989
990/**
991 * drm_create_tv_properties - create TV specific connector properties
992 * @dev: DRM device
993 * @num_modes: number of different TV formats (modes) supported
994 * @modes: array of pointers to strings containing name of each format
995 *
996 * Called by a driver's TV initialization routine, this function creates
997 * the TV specific connector properties for a given device. Caller is
998 * responsible for allocating a list of format names and passing them to
999 * this routine.
1000 */
1001int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1002 char *modes[])
1003{
1004 struct drm_property *tv_selector;
1005 struct drm_property *tv_subconnector;
1006 int i;
1007
1008 if (dev->mode_config.tv_select_subconnector_property)
1009 return 0;
1010
1011 /*
1012 * Basic connector properties
1013 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001014 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001015 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001016 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001017 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001018 dev->mode_config.tv_select_subconnector_property = tv_selector;
1019
1020 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001021 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1022 "subconnector",
1023 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001024 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001025 dev->mode_config.tv_subconnector_property = tv_subconnector;
1026
1027 /*
1028 * Other, TV specific properties: margins & TV modes.
1029 */
1030 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001031 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001032
1033 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001034 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001035
1036 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001037 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001038
1039 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001040 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001041
1042 dev->mode_config.tv_mode_property =
1043 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1044 "mode", num_modes);
1045 for (i = 0; i < num_modes; i++)
1046 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1047 i, modes[i]);
1048
Francisco Jerezb6b79022009-08-02 04:19:20 +02001049 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001050 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001051
1052 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001053 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001054
1055 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001056 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001057
Francisco Jereza75f0232009-08-12 02:30:10 +02001058 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001059 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001060
1061 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001062 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001063
1064 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001065 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001066
Dave Airlief453ba02008-11-07 14:05:41 -08001067 return 0;
1068}
1069EXPORT_SYMBOL(drm_mode_create_tv_properties);
1070
1071/**
1072 * drm_mode_create_scaling_mode_property - create scaling mode property
1073 * @dev: DRM device
1074 *
1075 * Called by a driver the first time it's needed, must be attached to desired
1076 * connectors.
1077 */
1078int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1079{
1080 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001081
1082 if (dev->mode_config.scaling_mode_property)
1083 return 0;
1084
1085 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001086 drm_property_create_enum(dev, 0, "scaling mode",
1087 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001088 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001089
1090 dev->mode_config.scaling_mode_property = scaling_mode;
1091
1092 return 0;
1093}
1094EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1095
1096/**
1097 * drm_mode_create_dithering_property - create dithering property
1098 * @dev: DRM device
1099 *
1100 * Called by a driver the first time it's needed, must be attached to desired
1101 * connectors.
1102 */
1103int drm_mode_create_dithering_property(struct drm_device *dev)
1104{
1105 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001106
1107 if (dev->mode_config.dithering_mode_property)
1108 return 0;
1109
1110 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001111 drm_property_create_enum(dev, 0, "dithering",
1112 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001113 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001114 dev->mode_config.dithering_mode_property = dithering_mode;
1115
1116 return 0;
1117}
1118EXPORT_SYMBOL(drm_mode_create_dithering_property);
1119
1120/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001121 * drm_mode_create_dirty_property - create dirty property
1122 * @dev: DRM device
1123 *
1124 * Called by a driver the first time it's needed, must be attached to desired
1125 * connectors.
1126 */
1127int drm_mode_create_dirty_info_property(struct drm_device *dev)
1128{
1129 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001130
1131 if (dev->mode_config.dirty_info_property)
1132 return 0;
1133
1134 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001135 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001136 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001137 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001138 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001139 dev->mode_config.dirty_info_property = dirty_info;
1140
1141 return 0;
1142}
1143EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1144
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001145static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001146{
1147 uint32_t total_objects = 0;
1148
1149 total_objects += dev->mode_config.num_crtc;
1150 total_objects += dev->mode_config.num_connector;
1151 total_objects += dev->mode_config.num_encoder;
1152
Dave Airlief453ba02008-11-07 14:05:41 -08001153 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1154 if (!group->id_list)
1155 return -ENOMEM;
1156
1157 group->num_crtcs = 0;
1158 group->num_connectors = 0;
1159 group->num_encoders = 0;
1160 return 0;
1161}
1162
1163int drm_mode_group_init_legacy_group(struct drm_device *dev,
1164 struct drm_mode_group *group)
1165{
1166 struct drm_crtc *crtc;
1167 struct drm_encoder *encoder;
1168 struct drm_connector *connector;
1169 int ret;
1170
1171 if ((ret = drm_mode_group_init(dev, group)))
1172 return ret;
1173
1174 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1175 group->id_list[group->num_crtcs++] = crtc->base.id;
1176
1177 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1178 group->id_list[group->num_crtcs + group->num_encoders++] =
1179 encoder->base.id;
1180
1181 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1182 group->id_list[group->num_crtcs + group->num_encoders +
1183 group->num_connectors++] = connector->base.id;
1184
1185 return 0;
1186}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001187EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001188
1189/**
Dave Airlief453ba02008-11-07 14:05:41 -08001190 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1191 * @out: drm_mode_modeinfo struct to return to the user
1192 * @in: drm_display_mode to use
1193 *
Dave Airlief453ba02008-11-07 14:05:41 -08001194 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1195 * the user.
1196 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001197static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1198 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001199{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001200 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1201 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1202 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1203 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1204 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1205 "timing values too large for mode info\n");
1206
Dave Airlief453ba02008-11-07 14:05:41 -08001207 out->clock = in->clock;
1208 out->hdisplay = in->hdisplay;
1209 out->hsync_start = in->hsync_start;
1210 out->hsync_end = in->hsync_end;
1211 out->htotal = in->htotal;
1212 out->hskew = in->hskew;
1213 out->vdisplay = in->vdisplay;
1214 out->vsync_start = in->vsync_start;
1215 out->vsync_end = in->vsync_end;
1216 out->vtotal = in->vtotal;
1217 out->vscan = in->vscan;
1218 out->vrefresh = in->vrefresh;
1219 out->flags = in->flags;
1220 out->type = in->type;
1221 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1222 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1223}
1224
1225/**
1226 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1227 * @out: drm_display_mode to return to the user
1228 * @in: drm_mode_modeinfo to use
1229 *
Dave Airlief453ba02008-11-07 14:05:41 -08001230 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1231 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001232 *
1233 * RETURNS:
1234 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001235 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001236static int drm_crtc_convert_umode(struct drm_display_mode *out,
1237 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001238{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001239 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1240 return -ERANGE;
1241
Dave Airlief453ba02008-11-07 14:05:41 -08001242 out->clock = in->clock;
1243 out->hdisplay = in->hdisplay;
1244 out->hsync_start = in->hsync_start;
1245 out->hsync_end = in->hsync_end;
1246 out->htotal = in->htotal;
1247 out->hskew = in->hskew;
1248 out->vdisplay = in->vdisplay;
1249 out->vsync_start = in->vsync_start;
1250 out->vsync_end = in->vsync_end;
1251 out->vtotal = in->vtotal;
1252 out->vscan = in->vscan;
1253 out->vrefresh = in->vrefresh;
1254 out->flags = in->flags;
1255 out->type = in->type;
1256 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1257 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001258
1259 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001260}
1261
1262/**
1263 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001264 * @dev: drm device for the ioctl
1265 * @data: data pointer for the ioctl
1266 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001267 *
Dave Airlief453ba02008-11-07 14:05:41 -08001268 * Construct a set of configuration description structures and return
1269 * them to the user, including CRTC, connector and framebuffer configuration.
1270 *
1271 * Called by the user via ioctl.
1272 *
1273 * RETURNS:
1274 * Zero on success, errno on failure.
1275 */
1276int drm_mode_getresources(struct drm_device *dev, void *data,
1277 struct drm_file *file_priv)
1278{
1279 struct drm_mode_card_res *card_res = data;
1280 struct list_head *lh;
1281 struct drm_framebuffer *fb;
1282 struct drm_connector *connector;
1283 struct drm_crtc *crtc;
1284 struct drm_encoder *encoder;
1285 int ret = 0;
1286 int connector_count = 0;
1287 int crtc_count = 0;
1288 int fb_count = 0;
1289 int encoder_count = 0;
1290 int copied = 0, i;
1291 uint32_t __user *fb_id;
1292 uint32_t __user *crtc_id;
1293 uint32_t __user *connector_id;
1294 uint32_t __user *encoder_id;
1295 struct drm_mode_group *mode_group;
1296
Dave Airliefb3b06c2011-02-08 13:55:21 +10001297 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1298 return -EINVAL;
1299
Dave Airlief453ba02008-11-07 14:05:41 -08001300
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001301 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001302 /*
1303 * For the non-control nodes we need to limit the list of resources
1304 * by IDs in the group list for this node
1305 */
1306 list_for_each(lh, &file_priv->fbs)
1307 fb_count++;
1308
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001309 /* handle this in 4 parts */
1310 /* FBs */
1311 if (card_res->count_fbs >= fb_count) {
1312 copied = 0;
1313 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1314 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1315 if (put_user(fb->base.id, fb_id + copied)) {
1316 mutex_unlock(&file_priv->fbs_lock);
1317 return -EFAULT;
1318 }
1319 copied++;
1320 }
1321 }
1322 card_res->count_fbs = fb_count;
1323 mutex_unlock(&file_priv->fbs_lock);
1324
1325 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001326 mode_group = &file_priv->master->minor->mode_group;
1327 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1328
1329 list_for_each(lh, &dev->mode_config.crtc_list)
1330 crtc_count++;
1331
1332 list_for_each(lh, &dev->mode_config.connector_list)
1333 connector_count++;
1334
1335 list_for_each(lh, &dev->mode_config.encoder_list)
1336 encoder_count++;
1337 } else {
1338
1339 crtc_count = mode_group->num_crtcs;
1340 connector_count = mode_group->num_connectors;
1341 encoder_count = mode_group->num_encoders;
1342 }
1343
1344 card_res->max_height = dev->mode_config.max_height;
1345 card_res->min_height = dev->mode_config.min_height;
1346 card_res->max_width = dev->mode_config.max_width;
1347 card_res->min_width = dev->mode_config.min_width;
1348
Dave Airlief453ba02008-11-07 14:05:41 -08001349 /* CRTCs */
1350 if (card_res->count_crtcs >= crtc_count) {
1351 copied = 0;
1352 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1353 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1354 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1355 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001356 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001357 if (put_user(crtc->base.id, crtc_id + copied)) {
1358 ret = -EFAULT;
1359 goto out;
1360 }
1361 copied++;
1362 }
1363 } else {
1364 for (i = 0; i < mode_group->num_crtcs; i++) {
1365 if (put_user(mode_group->id_list[i],
1366 crtc_id + copied)) {
1367 ret = -EFAULT;
1368 goto out;
1369 }
1370 copied++;
1371 }
1372 }
1373 }
1374 card_res->count_crtcs = crtc_count;
1375
1376 /* Encoders */
1377 if (card_res->count_encoders >= encoder_count) {
1378 copied = 0;
1379 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1380 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1381 list_for_each_entry(encoder,
1382 &dev->mode_config.encoder_list,
1383 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001384 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1385 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001386 if (put_user(encoder->base.id, encoder_id +
1387 copied)) {
1388 ret = -EFAULT;
1389 goto out;
1390 }
1391 copied++;
1392 }
1393 } else {
1394 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1395 if (put_user(mode_group->id_list[i],
1396 encoder_id + copied)) {
1397 ret = -EFAULT;
1398 goto out;
1399 }
1400 copied++;
1401 }
1402
1403 }
1404 }
1405 card_res->count_encoders = encoder_count;
1406
1407 /* Connectors */
1408 if (card_res->count_connectors >= connector_count) {
1409 copied = 0;
1410 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1411 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1412 list_for_each_entry(connector,
1413 &dev->mode_config.connector_list,
1414 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001415 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1416 connector->base.id,
1417 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001418 if (put_user(connector->base.id,
1419 connector_id + copied)) {
1420 ret = -EFAULT;
1421 goto out;
1422 }
1423 copied++;
1424 }
1425 } else {
1426 int start = mode_group->num_crtcs +
1427 mode_group->num_encoders;
1428 for (i = start; i < start + mode_group->num_connectors; i++) {
1429 if (put_user(mode_group->id_list[i],
1430 connector_id + copied)) {
1431 ret = -EFAULT;
1432 goto out;
1433 }
1434 copied++;
1435 }
1436 }
1437 }
1438 card_res->count_connectors = connector_count;
1439
Jerome Glisse94401062010-07-15 15:43:25 -04001440 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001441 card_res->count_connectors, card_res->count_encoders);
1442
1443out:
Daniel Vetter84849902012-12-02 00:28:11 +01001444 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001445 return ret;
1446}
1447
1448/**
1449 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001450 * @dev: drm device for the ioctl
1451 * @data: data pointer for the ioctl
1452 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001453 *
Dave Airlief453ba02008-11-07 14:05:41 -08001454 * Construct a CRTC configuration structure to return to the user.
1455 *
1456 * Called by the user via ioctl.
1457 *
1458 * RETURNS:
1459 * Zero on success, errno on failure.
1460 */
1461int drm_mode_getcrtc(struct drm_device *dev,
1462 void *data, struct drm_file *file_priv)
1463{
1464 struct drm_mode_crtc *crtc_resp = data;
1465 struct drm_crtc *crtc;
1466 struct drm_mode_object *obj;
1467 int ret = 0;
1468
Dave Airliefb3b06c2011-02-08 13:55:21 +10001469 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1470 return -EINVAL;
1471
Daniel Vetter84849902012-12-02 00:28:11 +01001472 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001473
1474 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1475 DRM_MODE_OBJECT_CRTC);
1476 if (!obj) {
1477 ret = -EINVAL;
1478 goto out;
1479 }
1480 crtc = obj_to_crtc(obj);
1481
1482 crtc_resp->x = crtc->x;
1483 crtc_resp->y = crtc->y;
1484 crtc_resp->gamma_size = crtc->gamma_size;
1485 if (crtc->fb)
1486 crtc_resp->fb_id = crtc->fb->base.id;
1487 else
1488 crtc_resp->fb_id = 0;
1489
1490 if (crtc->enabled) {
1491
1492 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1493 crtc_resp->mode_valid = 1;
1494
1495 } else {
1496 crtc_resp->mode_valid = 0;
1497 }
1498
1499out:
Daniel Vetter84849902012-12-02 00:28:11 +01001500 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001501 return ret;
1502}
1503
1504/**
1505 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001506 * @dev: drm device for the ioctl
1507 * @data: data pointer for the ioctl
1508 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001509 *
Dave Airlief453ba02008-11-07 14:05:41 -08001510 * Construct a connector configuration structure to return to the user.
1511 *
1512 * Called by the user via ioctl.
1513 *
1514 * RETURNS:
1515 * Zero on success, errno on failure.
1516 */
1517int drm_mode_getconnector(struct drm_device *dev, void *data,
1518 struct drm_file *file_priv)
1519{
1520 struct drm_mode_get_connector *out_resp = data;
1521 struct drm_mode_object *obj;
1522 struct drm_connector *connector;
1523 struct drm_display_mode *mode;
1524 int mode_count = 0;
1525 int props_count = 0;
1526 int encoders_count = 0;
1527 int ret = 0;
1528 int copied = 0;
1529 int i;
1530 struct drm_mode_modeinfo u_mode;
1531 struct drm_mode_modeinfo __user *mode_ptr;
1532 uint32_t __user *prop_ptr;
1533 uint64_t __user *prop_values;
1534 uint32_t __user *encoder_ptr;
1535
Dave Airliefb3b06c2011-02-08 13:55:21 +10001536 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1537 return -EINVAL;
1538
Dave Airlief453ba02008-11-07 14:05:41 -08001539 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1540
Jerome Glisse94401062010-07-15 15:43:25 -04001541 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001542
Daniel Vetter7b240562012-12-12 00:35:33 +01001543 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001544
1545 obj = drm_mode_object_find(dev, out_resp->connector_id,
1546 DRM_MODE_OBJECT_CONNECTOR);
1547 if (!obj) {
1548 ret = -EINVAL;
1549 goto out;
1550 }
1551 connector = obj_to_connector(obj);
1552
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001553 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001554
1555 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1556 if (connector->encoder_ids[i] != 0) {
1557 encoders_count++;
1558 }
1559 }
1560
1561 if (out_resp->count_modes == 0) {
1562 connector->funcs->fill_modes(connector,
1563 dev->mode_config.max_width,
1564 dev->mode_config.max_height);
1565 }
1566
1567 /* delayed so we get modes regardless of pre-fill_modes state */
1568 list_for_each_entry(mode, &connector->modes, head)
1569 mode_count++;
1570
1571 out_resp->connector_id = connector->base.id;
1572 out_resp->connector_type = connector->connector_type;
1573 out_resp->connector_type_id = connector->connector_type_id;
1574 out_resp->mm_width = connector->display_info.width_mm;
1575 out_resp->mm_height = connector->display_info.height_mm;
1576 out_resp->subpixel = connector->display_info.subpixel_order;
1577 out_resp->connection = connector->status;
1578 if (connector->encoder)
1579 out_resp->encoder_id = connector->encoder->base.id;
1580 else
1581 out_resp->encoder_id = 0;
1582
1583 /*
1584 * This ioctl is called twice, once to determine how much space is
1585 * needed, and the 2nd time to fill it.
1586 */
1587 if ((out_resp->count_modes >= mode_count) && mode_count) {
1588 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001589 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001590 list_for_each_entry(mode, &connector->modes, head) {
1591 drm_crtc_convert_to_umode(&u_mode, mode);
1592 if (copy_to_user(mode_ptr + copied,
1593 &u_mode, sizeof(u_mode))) {
1594 ret = -EFAULT;
1595 goto out;
1596 }
1597 copied++;
1598 }
1599 }
1600 out_resp->count_modes = mode_count;
1601
1602 if ((out_resp->count_props >= props_count) && props_count) {
1603 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001604 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1605 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001606 for (i = 0; i < connector->properties.count; i++) {
1607 if (put_user(connector->properties.ids[i],
1608 prop_ptr + copied)) {
1609 ret = -EFAULT;
1610 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001611 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001612
1613 if (put_user(connector->properties.values[i],
1614 prop_values + copied)) {
1615 ret = -EFAULT;
1616 goto out;
1617 }
1618 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001619 }
1620 }
1621 out_resp->count_props = props_count;
1622
1623 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1624 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001625 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001626 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1627 if (connector->encoder_ids[i] != 0) {
1628 if (put_user(connector->encoder_ids[i],
1629 encoder_ptr + copied)) {
1630 ret = -EFAULT;
1631 goto out;
1632 }
1633 copied++;
1634 }
1635 }
1636 }
1637 out_resp->count_encoders = encoders_count;
1638
1639out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001640 mutex_unlock(&dev->mode_config.mutex);
1641
Dave Airlief453ba02008-11-07 14:05:41 -08001642 return ret;
1643}
1644
1645int drm_mode_getencoder(struct drm_device *dev, void *data,
1646 struct drm_file *file_priv)
1647{
1648 struct drm_mode_get_encoder *enc_resp = data;
1649 struct drm_mode_object *obj;
1650 struct drm_encoder *encoder;
1651 int ret = 0;
1652
Dave Airliefb3b06c2011-02-08 13:55:21 +10001653 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1654 return -EINVAL;
1655
Daniel Vetter84849902012-12-02 00:28:11 +01001656 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001657 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1658 DRM_MODE_OBJECT_ENCODER);
1659 if (!obj) {
1660 ret = -EINVAL;
1661 goto out;
1662 }
1663 encoder = obj_to_encoder(obj);
1664
1665 if (encoder->crtc)
1666 enc_resp->crtc_id = encoder->crtc->base.id;
1667 else
1668 enc_resp->crtc_id = 0;
1669 enc_resp->encoder_type = encoder->encoder_type;
1670 enc_resp->encoder_id = encoder->base.id;
1671 enc_resp->possible_crtcs = encoder->possible_crtcs;
1672 enc_resp->possible_clones = encoder->possible_clones;
1673
1674out:
Daniel Vetter84849902012-12-02 00:28:11 +01001675 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001676 return ret;
1677}
1678
1679/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001680 * drm_mode_getplane_res - get plane info
1681 * @dev: DRM device
1682 * @data: ioctl data
1683 * @file_priv: DRM file info
1684 *
1685 * Return an plane count and set of IDs.
1686 */
1687int drm_mode_getplane_res(struct drm_device *dev, void *data,
1688 struct drm_file *file_priv)
1689{
1690 struct drm_mode_get_plane_res *plane_resp = data;
1691 struct drm_mode_config *config;
1692 struct drm_plane *plane;
1693 uint32_t __user *plane_ptr;
1694 int copied = 0, ret = 0;
1695
1696 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1697 return -EINVAL;
1698
Daniel Vetter84849902012-12-02 00:28:11 +01001699 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001700 config = &dev->mode_config;
1701
1702 /*
1703 * This ioctl is called twice, once to determine how much space is
1704 * needed, and the 2nd time to fill it.
1705 */
1706 if (config->num_plane &&
1707 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001708 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001709
1710 list_for_each_entry(plane, &config->plane_list, head) {
1711 if (put_user(plane->base.id, plane_ptr + copied)) {
1712 ret = -EFAULT;
1713 goto out;
1714 }
1715 copied++;
1716 }
1717 }
1718 plane_resp->count_planes = config->num_plane;
1719
1720out:
Daniel Vetter84849902012-12-02 00:28:11 +01001721 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001722 return ret;
1723}
1724
1725/**
1726 * drm_mode_getplane - get plane info
1727 * @dev: DRM device
1728 * @data: ioctl data
1729 * @file_priv: DRM file info
1730 *
1731 * Return plane info, including formats supported, gamma size, any
1732 * current fb, etc.
1733 */
1734int drm_mode_getplane(struct drm_device *dev, void *data,
1735 struct drm_file *file_priv)
1736{
1737 struct drm_mode_get_plane *plane_resp = data;
1738 struct drm_mode_object *obj;
1739 struct drm_plane *plane;
1740 uint32_t __user *format_ptr;
1741 int ret = 0;
1742
1743 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1744 return -EINVAL;
1745
Daniel Vetter84849902012-12-02 00:28:11 +01001746 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001747 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1748 DRM_MODE_OBJECT_PLANE);
1749 if (!obj) {
1750 ret = -ENOENT;
1751 goto out;
1752 }
1753 plane = obj_to_plane(obj);
1754
1755 if (plane->crtc)
1756 plane_resp->crtc_id = plane->crtc->base.id;
1757 else
1758 plane_resp->crtc_id = 0;
1759
1760 if (plane->fb)
1761 plane_resp->fb_id = plane->fb->base.id;
1762 else
1763 plane_resp->fb_id = 0;
1764
1765 plane_resp->plane_id = plane->base.id;
1766 plane_resp->possible_crtcs = plane->possible_crtcs;
1767 plane_resp->gamma_size = plane->gamma_size;
1768
1769 /*
1770 * This ioctl is called twice, once to determine how much space is
1771 * needed, and the 2nd time to fill it.
1772 */
1773 if (plane->format_count &&
1774 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001775 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001776 if (copy_to_user(format_ptr,
1777 plane->format_types,
1778 sizeof(uint32_t) * plane->format_count)) {
1779 ret = -EFAULT;
1780 goto out;
1781 }
1782 }
1783 plane_resp->count_format_types = plane->format_count;
1784
1785out:
Daniel Vetter84849902012-12-02 00:28:11 +01001786 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001787 return ret;
1788}
1789
1790/**
1791 * drm_mode_setplane - set up or tear down an plane
1792 * @dev: DRM device
1793 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001794 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001795 *
1796 * Set plane info, including placement, fb, scaling, and other factors.
1797 * Or pass a NULL fb to disable.
1798 */
1799int drm_mode_setplane(struct drm_device *dev, void *data,
1800 struct drm_file *file_priv)
1801{
1802 struct drm_mode_set_plane *plane_req = data;
1803 struct drm_mode_object *obj;
1804 struct drm_plane *plane;
1805 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001806 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001807 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001808 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001809 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001810
1811 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1812 return -EINVAL;
1813
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001814 /*
1815 * First, find the plane, crtc, and fb objects. If not available,
1816 * we don't bother to call the driver.
1817 */
1818 obj = drm_mode_object_find(dev, plane_req->plane_id,
1819 DRM_MODE_OBJECT_PLANE);
1820 if (!obj) {
1821 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1822 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001823 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001824 }
1825 plane = obj_to_plane(obj);
1826
1827 /* No fb means shut it down */
1828 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001829 drm_modeset_lock_all(dev);
1830 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001831 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001832 plane->crtc = NULL;
1833 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001834 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001835 goto out;
1836 }
1837
1838 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1839 DRM_MODE_OBJECT_CRTC);
1840 if (!obj) {
1841 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1842 plane_req->crtc_id);
1843 ret = -ENOENT;
1844 goto out;
1845 }
1846 crtc = obj_to_crtc(obj);
1847
Daniel Vetter786b99e2012-12-02 21:53:40 +01001848 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1849 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001850 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1851 plane_req->fb_id);
1852 ret = -ENOENT;
1853 goto out;
1854 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001855
Ville Syrjälä62443be2011-12-20 00:06:47 +02001856 /* Check whether this plane supports the fb pixel format. */
1857 for (i = 0; i < plane->format_count; i++)
1858 if (fb->pixel_format == plane->format_types[i])
1859 break;
1860 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03001861 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1862 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02001863 ret = -EINVAL;
1864 goto out;
1865 }
1866
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001867 fb_width = fb->width << 16;
1868 fb_height = fb->height << 16;
1869
1870 /* Make sure source coordinates are inside the fb. */
1871 if (plane_req->src_w > fb_width ||
1872 plane_req->src_x > fb_width - plane_req->src_w ||
1873 plane_req->src_h > fb_height ||
1874 plane_req->src_y > fb_height - plane_req->src_h) {
1875 DRM_DEBUG_KMS("Invalid source coordinates "
1876 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1877 plane_req->src_w >> 16,
1878 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1879 plane_req->src_h >> 16,
1880 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1881 plane_req->src_x >> 16,
1882 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1883 plane_req->src_y >> 16,
1884 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1885 ret = -ENOSPC;
1886 goto out;
1887 }
1888
Ville Syrjälä687a0402011-12-20 00:06:45 +02001889 /* Give drivers some help against integer overflows */
1890 if (plane_req->crtc_w > INT_MAX ||
1891 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1892 plane_req->crtc_h > INT_MAX ||
1893 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1894 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1895 plane_req->crtc_w, plane_req->crtc_h,
1896 plane_req->crtc_x, plane_req->crtc_y);
1897 ret = -ERANGE;
1898 goto out;
1899 }
1900
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001901 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001902 ret = plane->funcs->update_plane(plane, crtc, fb,
1903 plane_req->crtc_x, plane_req->crtc_y,
1904 plane_req->crtc_w, plane_req->crtc_h,
1905 plane_req->src_x, plane_req->src_y,
1906 plane_req->src_w, plane_req->src_h);
1907 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001908 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001909 plane->crtc = crtc;
1910 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001911 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001912 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001913 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001914
1915out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001916 if (fb)
1917 drm_framebuffer_unreference(fb);
1918 if (old_fb)
1919 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001920
1921 return ret;
1922}
1923
1924/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001925 * drm_mode_set_config_internal - helper to call ->set_config
1926 * @set: modeset config to set
1927 *
1928 * This is a little helper to wrap internal calls to the ->set_config driver
1929 * interface. The only thing it adds is correct refcounting dance.
1930 */
1931int drm_mode_set_config_internal(struct drm_mode_set *set)
1932{
1933 struct drm_crtc *crtc = set->crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001934 struct drm_framebuffer *fb, *old_fb;
1935 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001936
Daniel Vetterb0d12322012-12-11 01:07:12 +01001937 old_fb = crtc->fb;
1938 fb = set->fb;
1939
1940 ret = crtc->funcs->set_config(set);
1941 if (ret == 0) {
1942 if (old_fb)
1943 drm_framebuffer_unreference(old_fb);
1944 if (fb)
1945 drm_framebuffer_reference(fb);
1946 }
1947
1948 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001949}
1950EXPORT_SYMBOL(drm_mode_set_config_internal);
1951
1952/**
Dave Airlief453ba02008-11-07 14:05:41 -08001953 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001954 * @dev: drm device for the ioctl
1955 * @data: data pointer for the ioctl
1956 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001957 *
Dave Airlief453ba02008-11-07 14:05:41 -08001958 * Build a new CRTC configuration based on user request.
1959 *
1960 * Called by the user via ioctl.
1961 *
1962 * RETURNS:
1963 * Zero on success, errno on failure.
1964 */
1965int drm_mode_setcrtc(struct drm_device *dev, void *data,
1966 struct drm_file *file_priv)
1967{
1968 struct drm_mode_config *config = &dev->mode_config;
1969 struct drm_mode_crtc *crtc_req = data;
1970 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001971 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001972 struct drm_connector **connector_set = NULL, *connector;
1973 struct drm_framebuffer *fb = NULL;
1974 struct drm_display_mode *mode = NULL;
1975 struct drm_mode_set set;
1976 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001977 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001978 int i;
1979
Dave Airliefb3b06c2011-02-08 13:55:21 +10001980 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1981 return -EINVAL;
1982
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001983 /* For some reason crtc x/y offsets are signed internally. */
1984 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1985 return -ERANGE;
1986
Daniel Vetter84849902012-12-02 00:28:11 +01001987 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001988 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1989 DRM_MODE_OBJECT_CRTC);
1990 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001991 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001992 ret = -EINVAL;
1993 goto out;
1994 }
1995 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001996 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001997
1998 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001999 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002000 /* If we have a mode we need a framebuffer. */
2001 /* If we pass -1, set the mode with the currently bound fb */
2002 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002003 if (!crtc->fb) {
2004 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2005 ret = -EINVAL;
2006 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002007 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002008 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002009 /* Make refcounting symmetric with the lookup path. */
2010 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002011 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002012 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2013 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002014 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2015 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002016 ret = -EINVAL;
2017 goto out;
2018 }
Dave Airlief453ba02008-11-07 14:05:41 -08002019 }
2020
2021 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002022 if (!mode) {
2023 ret = -ENOMEM;
2024 goto out;
2025 }
2026
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002027 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2028 if (ret) {
2029 DRM_DEBUG_KMS("Invalid mode\n");
2030 goto out;
2031 }
2032
Dave Airlief453ba02008-11-07 14:05:41 -08002033 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002034
Rob Clark7c80e122012-09-04 16:35:56 +00002035 hdisplay = mode->hdisplay;
2036 vdisplay = mode->vdisplay;
2037
2038 if (crtc->invert_dimensions)
2039 swap(hdisplay, vdisplay);
2040
2041 if (hdisplay > fb->width ||
2042 vdisplay > fb->height ||
2043 crtc_req->x > fb->width - hdisplay ||
2044 crtc_req->y > fb->height - vdisplay) {
2045 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2046 fb->width, fb->height,
2047 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2048 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002049 ret = -ENOSPC;
2050 goto out;
2051 }
Dave Airlief453ba02008-11-07 14:05:41 -08002052 }
2053
2054 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002055 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002056 ret = -EINVAL;
2057 goto out;
2058 }
2059
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002060 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002061 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002062 crtc_req->count_connectors);
2063 ret = -EINVAL;
2064 goto out;
2065 }
2066
2067 if (crtc_req->count_connectors > 0) {
2068 u32 out_id;
2069
2070 /* Avoid unbounded kernel memory allocation */
2071 if (crtc_req->count_connectors > config->num_connector) {
2072 ret = -EINVAL;
2073 goto out;
2074 }
2075
2076 connector_set = kmalloc(crtc_req->count_connectors *
2077 sizeof(struct drm_connector *),
2078 GFP_KERNEL);
2079 if (!connector_set) {
2080 ret = -ENOMEM;
2081 goto out;
2082 }
2083
2084 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002085 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002086 if (get_user(out_id, &set_connectors_ptr[i])) {
2087 ret = -EFAULT;
2088 goto out;
2089 }
2090
2091 obj = drm_mode_object_find(dev, out_id,
2092 DRM_MODE_OBJECT_CONNECTOR);
2093 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002094 DRM_DEBUG_KMS("Connector id %d unknown\n",
2095 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002096 ret = -EINVAL;
2097 goto out;
2098 }
2099 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002100 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2101 connector->base.id,
2102 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002103
2104 connector_set[i] = connector;
2105 }
2106 }
2107
2108 set.crtc = crtc;
2109 set.x = crtc_req->x;
2110 set.y = crtc_req->y;
2111 set.mode = mode;
2112 set.connectors = connector_set;
2113 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002114 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002115 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002116
2117out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002118 if (fb)
2119 drm_framebuffer_unreference(fb);
2120
Dave Airlief453ba02008-11-07 14:05:41 -08002121 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002122 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002123 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002124 return ret;
2125}
2126
2127int drm_mode_cursor_ioctl(struct drm_device *dev,
2128 void *data, struct drm_file *file_priv)
2129{
2130 struct drm_mode_cursor *req = data;
2131 struct drm_mode_object *obj;
2132 struct drm_crtc *crtc;
2133 int ret = 0;
2134
Dave Airliefb3b06c2011-02-08 13:55:21 +10002135 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2136 return -EINVAL;
2137
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002138 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002139 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002140
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002141 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002142 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002143 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002144 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002145 }
2146 crtc = obj_to_crtc(obj);
2147
Daniel Vetterdac35662012-12-02 15:24:10 +01002148 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002149 if (req->flags & DRM_MODE_CURSOR_BO) {
2150 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002151 ret = -ENXIO;
2152 goto out;
2153 }
2154 /* Turns off the cursor if handle is 0 */
2155 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2156 req->width, req->height);
2157 }
2158
2159 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2160 if (crtc->funcs->cursor_move) {
2161 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2162 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002163 ret = -EFAULT;
2164 goto out;
2165 }
2166 }
2167out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002168 mutex_unlock(&crtc->mutex);
2169
Dave Airlief453ba02008-11-07 14:05:41 -08002170 return ret;
2171}
2172
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002173/* Original addfb only supported RGB formats, so figure out which one */
2174uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2175{
2176 uint32_t fmt;
2177
2178 switch (bpp) {
2179 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002180 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002181 break;
2182 case 16:
2183 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002184 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002185 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002186 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002187 break;
2188 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002189 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002190 break;
2191 case 32:
2192 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002193 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002194 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002195 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002196 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002197 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002198 break;
2199 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002200 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2201 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002202 break;
2203 }
2204
2205 return fmt;
2206}
2207EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2208
Dave Airlief453ba02008-11-07 14:05:41 -08002209/**
2210 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002211 * @dev: drm device for the ioctl
2212 * @data: data pointer for the ioctl
2213 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002214 *
Dave Airlief453ba02008-11-07 14:05:41 -08002215 * Add a new FB to the specified CRTC, given a user request.
2216 *
2217 * Called by the user via ioctl.
2218 *
2219 * RETURNS:
2220 * Zero on success, errno on failure.
2221 */
2222int drm_mode_addfb(struct drm_device *dev,
2223 void *data, struct drm_file *file_priv)
2224{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002225 struct drm_mode_fb_cmd *or = data;
2226 struct drm_mode_fb_cmd2 r = {};
2227 struct drm_mode_config *config = &dev->mode_config;
2228 struct drm_framebuffer *fb;
2229 int ret = 0;
2230
2231 /* Use new struct with format internally */
2232 r.fb_id = or->fb_id;
2233 r.width = or->width;
2234 r.height = or->height;
2235 r.pitches[0] = or->pitch;
2236 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2237 r.handles[0] = or->handle;
2238
2239 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2240 return -EINVAL;
2241
Jesse Barnesacb4b992011-11-07 12:03:23 -08002242 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002243 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002244
2245 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002246 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002247
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002248 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2249 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002250 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002251 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002252 }
2253
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002254 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002255 or->fb_id = fb->base.id;
2256 list_add(&fb->filp_head, &file_priv->fbs);
2257 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002258 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002259
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002260 return ret;
2261}
2262
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002263static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002264{
2265 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2266
2267 switch (format) {
2268 case DRM_FORMAT_C8:
2269 case DRM_FORMAT_RGB332:
2270 case DRM_FORMAT_BGR233:
2271 case DRM_FORMAT_XRGB4444:
2272 case DRM_FORMAT_XBGR4444:
2273 case DRM_FORMAT_RGBX4444:
2274 case DRM_FORMAT_BGRX4444:
2275 case DRM_FORMAT_ARGB4444:
2276 case DRM_FORMAT_ABGR4444:
2277 case DRM_FORMAT_RGBA4444:
2278 case DRM_FORMAT_BGRA4444:
2279 case DRM_FORMAT_XRGB1555:
2280 case DRM_FORMAT_XBGR1555:
2281 case DRM_FORMAT_RGBX5551:
2282 case DRM_FORMAT_BGRX5551:
2283 case DRM_FORMAT_ARGB1555:
2284 case DRM_FORMAT_ABGR1555:
2285 case DRM_FORMAT_RGBA5551:
2286 case DRM_FORMAT_BGRA5551:
2287 case DRM_FORMAT_RGB565:
2288 case DRM_FORMAT_BGR565:
2289 case DRM_FORMAT_RGB888:
2290 case DRM_FORMAT_BGR888:
2291 case DRM_FORMAT_XRGB8888:
2292 case DRM_FORMAT_XBGR8888:
2293 case DRM_FORMAT_RGBX8888:
2294 case DRM_FORMAT_BGRX8888:
2295 case DRM_FORMAT_ARGB8888:
2296 case DRM_FORMAT_ABGR8888:
2297 case DRM_FORMAT_RGBA8888:
2298 case DRM_FORMAT_BGRA8888:
2299 case DRM_FORMAT_XRGB2101010:
2300 case DRM_FORMAT_XBGR2101010:
2301 case DRM_FORMAT_RGBX1010102:
2302 case DRM_FORMAT_BGRX1010102:
2303 case DRM_FORMAT_ARGB2101010:
2304 case DRM_FORMAT_ABGR2101010:
2305 case DRM_FORMAT_RGBA1010102:
2306 case DRM_FORMAT_BGRA1010102:
2307 case DRM_FORMAT_YUYV:
2308 case DRM_FORMAT_YVYU:
2309 case DRM_FORMAT_UYVY:
2310 case DRM_FORMAT_VYUY:
2311 case DRM_FORMAT_AYUV:
2312 case DRM_FORMAT_NV12:
2313 case DRM_FORMAT_NV21:
2314 case DRM_FORMAT_NV16:
2315 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002316 case DRM_FORMAT_NV24:
2317 case DRM_FORMAT_NV42:
Ville Syrjälä935b59772011-12-20 00:06:48 +02002318 case DRM_FORMAT_YUV410:
2319 case DRM_FORMAT_YVU410:
2320 case DRM_FORMAT_YUV411:
2321 case DRM_FORMAT_YVU411:
2322 case DRM_FORMAT_YUV420:
2323 case DRM_FORMAT_YVU420:
2324 case DRM_FORMAT_YUV422:
2325 case DRM_FORMAT_YVU422:
2326 case DRM_FORMAT_YUV444:
2327 case DRM_FORMAT_YVU444:
2328 return 0;
2329 default:
2330 return -EINVAL;
2331 }
2332}
2333
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002334static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002335{
2336 int ret, hsub, vsub, num_planes, i;
2337
2338 ret = format_check(r);
2339 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002340 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2341 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002342 return ret;
2343 }
2344
2345 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2346 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2347 num_planes = drm_format_num_planes(r->pixel_format);
2348
2349 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002350 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002351 return -EINVAL;
2352 }
2353
2354 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002355 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002356 return -EINVAL;
2357 }
2358
2359 for (i = 0; i < num_planes; i++) {
2360 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002361 unsigned int height = r->height / (i != 0 ? vsub : 1);
2362 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002363
2364 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002365 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002366 return -EINVAL;
2367 }
2368
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002369 if ((uint64_t) width * cpp > UINT_MAX)
2370 return -ERANGE;
2371
2372 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2373 return -ERANGE;
2374
2375 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002376 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002377 return -EINVAL;
2378 }
2379 }
2380
2381 return 0;
2382}
2383
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002384/**
2385 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002386 * @dev: drm device for the ioctl
2387 * @data: data pointer for the ioctl
2388 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002389 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002390 * Add a new FB to the specified CRTC, given a user request with format.
2391 *
2392 * Called by the user via ioctl.
2393 *
2394 * RETURNS:
2395 * Zero on success, errno on failure.
2396 */
2397int drm_mode_addfb2(struct drm_device *dev,
2398 void *data, struct drm_file *file_priv)
2399{
2400 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002401 struct drm_mode_config *config = &dev->mode_config;
2402 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002403 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002404
Dave Airliefb3b06c2011-02-08 13:55:21 +10002405 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2406 return -EINVAL;
2407
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002408 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2409 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2410 return -EINVAL;
2411 }
2412
Dave Airlief453ba02008-11-07 14:05:41 -08002413 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002414 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002415 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002416 return -EINVAL;
2417 }
2418 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002419 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002420 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002421 return -EINVAL;
2422 }
2423
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002424 ret = framebuffer_check(r);
2425 if (ret)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002426 return ret;
Ville Syrjälä935b59772011-12-20 00:06:48 +02002427
Dave Airlief453ba02008-11-07 14:05:41 -08002428 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002429 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002430 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002431 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002432 }
2433
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002434 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002435 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002436 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002437 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002438 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002439
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002440
Dave Airlief453ba02008-11-07 14:05:41 -08002441 return ret;
2442}
2443
2444/**
2445 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002446 * @dev: drm device for the ioctl
2447 * @data: data pointer for the ioctl
2448 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002449 *
Dave Airlief453ba02008-11-07 14:05:41 -08002450 * Remove the FB specified by the user.
2451 *
2452 * Called by the user via ioctl.
2453 *
2454 * RETURNS:
2455 * Zero on success, errno on failure.
2456 */
2457int drm_mode_rmfb(struct drm_device *dev,
2458 void *data, struct drm_file *file_priv)
2459{
Dave Airlief453ba02008-11-07 14:05:41 -08002460 struct drm_framebuffer *fb = NULL;
2461 struct drm_framebuffer *fbl = NULL;
2462 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002463 int found = 0;
2464
Dave Airliefb3b06c2011-02-08 13:55:21 +10002465 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2466 return -EINVAL;
2467
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002468 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002469 mutex_lock(&dev->mode_config.fb_lock);
2470 fb = __drm_framebuffer_lookup(dev, *id);
2471 if (!fb)
2472 goto fail_lookup;
2473
Dave Airlief453ba02008-11-07 14:05:41 -08002474 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2475 if (fb == fbl)
2476 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002477 if (!found)
2478 goto fail_lookup;
2479
2480 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2481 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002482
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002483 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002484 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002485 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002486
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002487 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002488
Daniel Vetter2b677e82012-12-10 21:16:05 +01002489 return 0;
2490
2491fail_lookup:
2492 mutex_unlock(&dev->mode_config.fb_lock);
2493 mutex_unlock(&file_priv->fbs_lock);
2494
2495 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002496}
2497
2498/**
2499 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002500 * @dev: drm device for the ioctl
2501 * @data: data pointer for the ioctl
2502 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002503 *
Dave Airlief453ba02008-11-07 14:05:41 -08002504 * Lookup the FB given its ID and return info about it.
2505 *
2506 * Called by the user via ioctl.
2507 *
2508 * RETURNS:
2509 * Zero on success, errno on failure.
2510 */
2511int drm_mode_getfb(struct drm_device *dev,
2512 void *data, struct drm_file *file_priv)
2513{
2514 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002515 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002516 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002517
Dave Airliefb3b06c2011-02-08 13:55:21 +10002518 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2519 return -EINVAL;
2520
Daniel Vetter786b99e2012-12-02 21:53:40 +01002521 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002522 if (!fb)
2523 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002524
2525 r->height = fb->height;
2526 r->width = fb->width;
2527 r->depth = fb->depth;
2528 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002529 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002530 if (fb->funcs->create_handle)
2531 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2532 else
2533 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002534
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002535 drm_framebuffer_unreference(fb);
2536
Dave Airlief453ba02008-11-07 14:05:41 -08002537 return ret;
2538}
2539
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002540int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2541 void *data, struct drm_file *file_priv)
2542{
2543 struct drm_clip_rect __user *clips_ptr;
2544 struct drm_clip_rect *clips = NULL;
2545 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002546 struct drm_framebuffer *fb;
2547 unsigned flags;
2548 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002549 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002550
Dave Airliefb3b06c2011-02-08 13:55:21 +10002551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2552 return -EINVAL;
2553
Daniel Vetter786b99e2012-12-02 21:53:40 +01002554 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002555 if (!fb)
2556 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002557
2558 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002559 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002560
2561 if (!num_clips != !clips_ptr) {
2562 ret = -EINVAL;
2563 goto out_err1;
2564 }
2565
2566 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2567
2568 /* If userspace annotates copy, clips must come in pairs */
2569 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2570 ret = -EINVAL;
2571 goto out_err1;
2572 }
2573
2574 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002575 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2576 ret = -EINVAL;
2577 goto out_err1;
2578 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002579 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2580 if (!clips) {
2581 ret = -ENOMEM;
2582 goto out_err1;
2583 }
2584
2585 ret = copy_from_user(clips, clips_ptr,
2586 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002587 if (ret) {
2588 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002589 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002590 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002591 }
2592
2593 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002594 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002595 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2596 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002597 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002598 } else {
2599 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002600 }
2601
2602out_err2:
2603 kfree(clips);
2604out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002605 drm_framebuffer_unreference(fb);
2606
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002607 return ret;
2608}
2609
2610
Dave Airlief453ba02008-11-07 14:05:41 -08002611/**
2612 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002613 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002614 *
Dave Airlief453ba02008-11-07 14:05:41 -08002615 * Destroy all the FBs associated with @filp.
2616 *
2617 * Called by the user via ioctl.
2618 *
2619 * RETURNS:
2620 * Zero on success, errno on failure.
2621 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002622void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002623{
Dave Airlief453ba02008-11-07 14:05:41 -08002624 struct drm_device *dev = priv->minor->dev;
2625 struct drm_framebuffer *fb, *tfb;
2626
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002627 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002628 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002629
2630 mutex_lock(&dev->mode_config.fb_lock);
2631 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2632 __drm_framebuffer_unregister(dev, fb);
2633 mutex_unlock(&dev->mode_config.fb_lock);
2634
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002635 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002636
2637 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002638 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002639 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002640 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002641}
2642
Dave Airlief453ba02008-11-07 14:05:41 -08002643struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2644 const char *name, int num_values)
2645{
2646 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002647 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002648
2649 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2650 if (!property)
2651 return NULL;
2652
2653 if (num_values) {
2654 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2655 if (!property->values)
2656 goto fail;
2657 }
2658
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002659 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2660 if (ret)
2661 goto fail;
2662
Dave Airlief453ba02008-11-07 14:05:41 -08002663 property->flags = flags;
2664 property->num_values = num_values;
2665 INIT_LIST_HEAD(&property->enum_blob_list);
2666
Vinson Lee471dd2e2011-11-10 11:55:40 -08002667 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002668 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002669 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2670 }
Dave Airlief453ba02008-11-07 14:05:41 -08002671
2672 list_add_tail(&property->head, &dev->mode_config.property_list);
2673 return property;
2674fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002675 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002676 kfree(property);
2677 return NULL;
2678}
2679EXPORT_SYMBOL(drm_property_create);
2680
Sascha Hauer4a67d392012-02-06 10:58:17 +01002681struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2682 const char *name,
2683 const struct drm_prop_enum_list *props,
2684 int num_values)
2685{
2686 struct drm_property *property;
2687 int i, ret;
2688
2689 flags |= DRM_MODE_PROP_ENUM;
2690
2691 property = drm_property_create(dev, flags, name, num_values);
2692 if (!property)
2693 return NULL;
2694
2695 for (i = 0; i < num_values; i++) {
2696 ret = drm_property_add_enum(property, i,
2697 props[i].type,
2698 props[i].name);
2699 if (ret) {
2700 drm_property_destroy(dev, property);
2701 return NULL;
2702 }
2703 }
2704
2705 return property;
2706}
2707EXPORT_SYMBOL(drm_property_create_enum);
2708
Rob Clark49e27542012-05-17 02:23:26 -06002709struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2710 int flags, const char *name,
2711 const struct drm_prop_enum_list *props,
2712 int num_values)
2713{
2714 struct drm_property *property;
2715 int i, ret;
2716
2717 flags |= DRM_MODE_PROP_BITMASK;
2718
2719 property = drm_property_create(dev, flags, name, num_values);
2720 if (!property)
2721 return NULL;
2722
2723 for (i = 0; i < num_values; i++) {
2724 ret = drm_property_add_enum(property, i,
2725 props[i].type,
2726 props[i].name);
2727 if (ret) {
2728 drm_property_destroy(dev, property);
2729 return NULL;
2730 }
2731 }
2732
2733 return property;
2734}
2735EXPORT_SYMBOL(drm_property_create_bitmask);
2736
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002737struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2738 const char *name,
2739 uint64_t min, uint64_t max)
2740{
2741 struct drm_property *property;
2742
2743 flags |= DRM_MODE_PROP_RANGE;
2744
2745 property = drm_property_create(dev, flags, name, 2);
2746 if (!property)
2747 return NULL;
2748
2749 property->values[0] = min;
2750 property->values[1] = max;
2751
2752 return property;
2753}
2754EXPORT_SYMBOL(drm_property_create_range);
2755
Dave Airlief453ba02008-11-07 14:05:41 -08002756int drm_property_add_enum(struct drm_property *property, int index,
2757 uint64_t value, const char *name)
2758{
2759 struct drm_property_enum *prop_enum;
2760
Rob Clark49e27542012-05-17 02:23:26 -06002761 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2762 return -EINVAL;
2763
2764 /*
2765 * Bitmask enum properties have the additional constraint of values
2766 * from 0 to 63
2767 */
2768 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002769 return -EINVAL;
2770
2771 if (!list_empty(&property->enum_blob_list)) {
2772 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2773 if (prop_enum->value == value) {
2774 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2775 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2776 return 0;
2777 }
2778 }
2779 }
2780
2781 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2782 if (!prop_enum)
2783 return -ENOMEM;
2784
2785 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2786 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2787 prop_enum->value = value;
2788
2789 property->values[index] = value;
2790 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2791 return 0;
2792}
2793EXPORT_SYMBOL(drm_property_add_enum);
2794
2795void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2796{
2797 struct drm_property_enum *prop_enum, *pt;
2798
2799 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2800 list_del(&prop_enum->head);
2801 kfree(prop_enum);
2802 }
2803
2804 if (property->num_values)
2805 kfree(property->values);
2806 drm_mode_object_put(dev, &property->base);
2807 list_del(&property->head);
2808 kfree(property);
2809}
2810EXPORT_SYMBOL(drm_property_destroy);
2811
Paulo Zanonic5431882012-05-15 18:09:02 -03002812void drm_object_attach_property(struct drm_mode_object *obj,
2813 struct drm_property *property,
2814 uint64_t init_val)
2815{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002816 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002817
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002818 if (count == DRM_OBJECT_MAX_PROPERTY) {
2819 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2820 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2821 "you see this message on the same object type.\n",
2822 obj->type);
2823 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002824 }
2825
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002826 obj->properties->ids[count] = property->base.id;
2827 obj->properties->values[count] = init_val;
2828 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002829}
2830EXPORT_SYMBOL(drm_object_attach_property);
2831
2832int drm_object_property_set_value(struct drm_mode_object *obj,
2833 struct drm_property *property, uint64_t val)
2834{
2835 int i;
2836
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002837 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002838 if (obj->properties->ids[i] == property->base.id) {
2839 obj->properties->values[i] = val;
2840 return 0;
2841 }
2842 }
2843
2844 return -EINVAL;
2845}
2846EXPORT_SYMBOL(drm_object_property_set_value);
2847
2848int drm_object_property_get_value(struct drm_mode_object *obj,
2849 struct drm_property *property, uint64_t *val)
2850{
2851 int i;
2852
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002853 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002854 if (obj->properties->ids[i] == property->base.id) {
2855 *val = obj->properties->values[i];
2856 return 0;
2857 }
2858 }
2859
2860 return -EINVAL;
2861}
2862EXPORT_SYMBOL(drm_object_property_get_value);
2863
Dave Airlief453ba02008-11-07 14:05:41 -08002864int drm_mode_getproperty_ioctl(struct drm_device *dev,
2865 void *data, struct drm_file *file_priv)
2866{
2867 struct drm_mode_object *obj;
2868 struct drm_mode_get_property *out_resp = data;
2869 struct drm_property *property;
2870 int enum_count = 0;
2871 int blob_count = 0;
2872 int value_count = 0;
2873 int ret = 0, i;
2874 int copied;
2875 struct drm_property_enum *prop_enum;
2876 struct drm_mode_property_enum __user *enum_ptr;
2877 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002878 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002879 uint64_t __user *values_ptr;
2880 uint32_t __user *blob_length_ptr;
2881
Dave Airliefb3b06c2011-02-08 13:55:21 +10002882 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2883 return -EINVAL;
2884
Daniel Vetter84849902012-12-02 00:28:11 +01002885 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002886 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2887 if (!obj) {
2888 ret = -EINVAL;
2889 goto done;
2890 }
2891 property = obj_to_property(obj);
2892
Rob Clark49e27542012-05-17 02:23:26 -06002893 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002894 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2895 enum_count++;
2896 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2897 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2898 blob_count++;
2899 }
2900
2901 value_count = property->num_values;
2902
2903 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2904 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2905 out_resp->flags = property->flags;
2906
2907 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002908 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002909 for (i = 0; i < value_count; i++) {
2910 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2911 ret = -EFAULT;
2912 goto done;
2913 }
2914 }
2915 }
2916 out_resp->count_values = value_count;
2917
Rob Clark49e27542012-05-17 02:23:26 -06002918 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002919 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2920 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002921 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002922 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2923
2924 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2925 ret = -EFAULT;
2926 goto done;
2927 }
2928
2929 if (copy_to_user(&enum_ptr[copied].name,
2930 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2931 ret = -EFAULT;
2932 goto done;
2933 }
2934 copied++;
2935 }
2936 }
2937 out_resp->count_enum_blobs = enum_count;
2938 }
2939
2940 if (property->flags & DRM_MODE_PROP_BLOB) {
2941 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2942 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002943 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2944 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002945
2946 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2947 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2948 ret = -EFAULT;
2949 goto done;
2950 }
2951
2952 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2953 ret = -EFAULT;
2954 goto done;
2955 }
2956
2957 copied++;
2958 }
2959 }
2960 out_resp->count_enum_blobs = blob_count;
2961 }
2962done:
Daniel Vetter84849902012-12-02 00:28:11 +01002963 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002964 return ret;
2965}
2966
2967static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2968 void *data)
2969{
2970 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002971 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002972
2973 if (!length || !data)
2974 return NULL;
2975
2976 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2977 if (!blob)
2978 return NULL;
2979
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002980 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2981 if (ret) {
2982 kfree(blob);
2983 return NULL;
2984 }
2985
Dave Airlief453ba02008-11-07 14:05:41 -08002986 blob->length = length;
2987
2988 memcpy(blob->data, data, length);
2989
Dave Airlief453ba02008-11-07 14:05:41 -08002990 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2991 return blob;
2992}
2993
2994static void drm_property_destroy_blob(struct drm_device *dev,
2995 struct drm_property_blob *blob)
2996{
2997 drm_mode_object_put(dev, &blob->base);
2998 list_del(&blob->head);
2999 kfree(blob);
3000}
3001
3002int drm_mode_getblob_ioctl(struct drm_device *dev,
3003 void *data, struct drm_file *file_priv)
3004{
3005 struct drm_mode_object *obj;
3006 struct drm_mode_get_blob *out_resp = data;
3007 struct drm_property_blob *blob;
3008 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003009 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003010
Dave Airliefb3b06c2011-02-08 13:55:21 +10003011 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3012 return -EINVAL;
3013
Daniel Vetter84849902012-12-02 00:28:11 +01003014 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003015 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3016 if (!obj) {
3017 ret = -EINVAL;
3018 goto done;
3019 }
3020 blob = obj_to_blob(obj);
3021
3022 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003023 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003024 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3025 ret = -EFAULT;
3026 goto done;
3027 }
3028 }
3029 out_resp->length = blob->length;
3030
3031done:
Daniel Vetter84849902012-12-02 00:28:11 +01003032 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003033 return ret;
3034}
3035
3036int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3037 struct edid *edid)
3038{
3039 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003040 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003041
3042 if (connector->edid_blob_ptr)
3043 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3044
3045 /* Delete edid, when there is none. */
3046 if (!edid) {
3047 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003048 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003049 return ret;
3050 }
3051
Adam Jackson7466f4c2010-03-29 21:43:23 +00003052 size = EDID_LENGTH * (1 + edid->extensions);
3053 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3054 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003055 if (!connector->edid_blob_ptr)
3056 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003057
Rob Clark58495562012-10-11 20:50:56 -05003058 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003059 dev->mode_config.edid_property,
3060 connector->edid_blob_ptr->base.id);
3061
3062 return ret;
3063}
3064EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3065
Paulo Zanoni26a34812012-05-15 18:08:59 -03003066static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003067 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003068{
3069 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3070 return false;
3071 if (property->flags & DRM_MODE_PROP_RANGE) {
3072 if (value < property->values[0] || value > property->values[1])
3073 return false;
3074 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003075 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3076 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003077 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003078 for (i = 0; i < property->num_values; i++)
3079 valid_mask |= (1ULL << property->values[i]);
3080 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003081 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3082 /* Only the driver knows */
3083 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003084 } else {
3085 int i;
3086 for (i = 0; i < property->num_values; i++)
3087 if (property->values[i] == value)
3088 return true;
3089 return false;
3090 }
3091}
3092
Dave Airlief453ba02008-11-07 14:05:41 -08003093int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3094 void *data, struct drm_file *file_priv)
3095{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003096 struct drm_mode_connector_set_property *conn_set_prop = data;
3097 struct drm_mode_obj_set_property obj_set_prop = {
3098 .value = conn_set_prop->value,
3099 .prop_id = conn_set_prop->prop_id,
3100 .obj_id = conn_set_prop->connector_id,
3101 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3102 };
Dave Airlief453ba02008-11-07 14:05:41 -08003103
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003104 /* It does all the locking and checking we need */
3105 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003106}
3107
Paulo Zanonic5431882012-05-15 18:09:02 -03003108static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3109 struct drm_property *property,
3110 uint64_t value)
3111{
3112 int ret = -EINVAL;
3113 struct drm_connector *connector = obj_to_connector(obj);
3114
3115 /* Do DPMS ourselves */
3116 if (property == connector->dev->mode_config.dpms_property) {
3117 if (connector->funcs->dpms)
3118 (*connector->funcs->dpms)(connector, (int)value);
3119 ret = 0;
3120 } else if (connector->funcs->set_property)
3121 ret = connector->funcs->set_property(connector, property, value);
3122
3123 /* store the property value if successful */
3124 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003125 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003126 return ret;
3127}
3128
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003129static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3130 struct drm_property *property,
3131 uint64_t value)
3132{
3133 int ret = -EINVAL;
3134 struct drm_crtc *crtc = obj_to_crtc(obj);
3135
3136 if (crtc->funcs->set_property)
3137 ret = crtc->funcs->set_property(crtc, property, value);
3138 if (!ret)
3139 drm_object_property_set_value(obj, property, value);
3140
3141 return ret;
3142}
3143
Rob Clark4d939142012-05-17 02:23:27 -06003144static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3145 struct drm_property *property,
3146 uint64_t value)
3147{
3148 int ret = -EINVAL;
3149 struct drm_plane *plane = obj_to_plane(obj);
3150
3151 if (plane->funcs->set_property)
3152 ret = plane->funcs->set_property(plane, property, value);
3153 if (!ret)
3154 drm_object_property_set_value(obj, property, value);
3155
3156 return ret;
3157}
3158
Paulo Zanonic5431882012-05-15 18:09:02 -03003159int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3160 struct drm_file *file_priv)
3161{
3162 struct drm_mode_obj_get_properties *arg = data;
3163 struct drm_mode_object *obj;
3164 int ret = 0;
3165 int i;
3166 int copied = 0;
3167 int props_count = 0;
3168 uint32_t __user *props_ptr;
3169 uint64_t __user *prop_values_ptr;
3170
3171 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3172 return -EINVAL;
3173
Daniel Vetter84849902012-12-02 00:28:11 +01003174 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003175
3176 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3177 if (!obj) {
3178 ret = -EINVAL;
3179 goto out;
3180 }
3181 if (!obj->properties) {
3182 ret = -EINVAL;
3183 goto out;
3184 }
3185
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003186 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003187
3188 /* This ioctl is called twice, once to determine how much space is
3189 * needed, and the 2nd time to fill it. */
3190 if ((arg->count_props >= props_count) && props_count) {
3191 copied = 0;
3192 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3193 prop_values_ptr = (uint64_t __user *)(unsigned long)
3194 (arg->prop_values_ptr);
3195 for (i = 0; i < props_count; i++) {
3196 if (put_user(obj->properties->ids[i],
3197 props_ptr + copied)) {
3198 ret = -EFAULT;
3199 goto out;
3200 }
3201 if (put_user(obj->properties->values[i],
3202 prop_values_ptr + copied)) {
3203 ret = -EFAULT;
3204 goto out;
3205 }
3206 copied++;
3207 }
3208 }
3209 arg->count_props = props_count;
3210out:
Daniel Vetter84849902012-12-02 00:28:11 +01003211 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003212 return ret;
3213}
3214
3215int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3216 struct drm_file *file_priv)
3217{
3218 struct drm_mode_obj_set_property *arg = data;
3219 struct drm_mode_object *arg_obj;
3220 struct drm_mode_object *prop_obj;
3221 struct drm_property *property;
3222 int ret = -EINVAL;
3223 int i;
3224
3225 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3226 return -EINVAL;
3227
Daniel Vetter84849902012-12-02 00:28:11 +01003228 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003229
3230 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3231 if (!arg_obj)
3232 goto out;
3233 if (!arg_obj->properties)
3234 goto out;
3235
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003236 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003237 if (arg_obj->properties->ids[i] == arg->prop_id)
3238 break;
3239
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003240 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003241 goto out;
3242
3243 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3244 DRM_MODE_OBJECT_PROPERTY);
3245 if (!prop_obj)
3246 goto out;
3247 property = obj_to_property(prop_obj);
3248
3249 if (!drm_property_change_is_valid(property, arg->value))
3250 goto out;
3251
3252 switch (arg_obj->type) {
3253 case DRM_MODE_OBJECT_CONNECTOR:
3254 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3255 arg->value);
3256 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003257 case DRM_MODE_OBJECT_CRTC:
3258 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3259 break;
Rob Clark4d939142012-05-17 02:23:27 -06003260 case DRM_MODE_OBJECT_PLANE:
3261 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3262 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003263 }
3264
3265out:
Daniel Vetter84849902012-12-02 00:28:11 +01003266 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003267 return ret;
3268}
3269
Dave Airlief453ba02008-11-07 14:05:41 -08003270int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3271 struct drm_encoder *encoder)
3272{
3273 int i;
3274
3275 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3276 if (connector->encoder_ids[i] == 0) {
3277 connector->encoder_ids[i] = encoder->base.id;
3278 return 0;
3279 }
3280 }
3281 return -ENOMEM;
3282}
3283EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3284
3285void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3286 struct drm_encoder *encoder)
3287{
3288 int i;
3289 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3290 if (connector->encoder_ids[i] == encoder->base.id) {
3291 connector->encoder_ids[i] = 0;
3292 if (connector->encoder == encoder)
3293 connector->encoder = NULL;
3294 break;
3295 }
3296 }
3297}
3298EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3299
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003300int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003301 int gamma_size)
3302{
3303 crtc->gamma_size = gamma_size;
3304
3305 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3306 if (!crtc->gamma_store) {
3307 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003308 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003309 }
3310
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003311 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003312}
3313EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3314
3315int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3316 void *data, struct drm_file *file_priv)
3317{
3318 struct drm_mode_crtc_lut *crtc_lut = data;
3319 struct drm_mode_object *obj;
3320 struct drm_crtc *crtc;
3321 void *r_base, *g_base, *b_base;
3322 int size;
3323 int ret = 0;
3324
Dave Airliefb3b06c2011-02-08 13:55:21 +10003325 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3326 return -EINVAL;
3327
Daniel Vetter84849902012-12-02 00:28:11 +01003328 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003329 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3330 if (!obj) {
3331 ret = -EINVAL;
3332 goto out;
3333 }
3334 crtc = obj_to_crtc(obj);
3335
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003336 if (crtc->funcs->gamma_set == NULL) {
3337 ret = -ENOSYS;
3338 goto out;
3339 }
3340
Dave Airlief453ba02008-11-07 14:05:41 -08003341 /* memcpy into gamma store */
3342 if (crtc_lut->gamma_size != crtc->gamma_size) {
3343 ret = -EINVAL;
3344 goto out;
3345 }
3346
3347 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3348 r_base = crtc->gamma_store;
3349 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3350 ret = -EFAULT;
3351 goto out;
3352 }
3353
3354 g_base = r_base + size;
3355 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3356 ret = -EFAULT;
3357 goto out;
3358 }
3359
3360 b_base = g_base + size;
3361 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3362 ret = -EFAULT;
3363 goto out;
3364 }
3365
James Simmons72034252010-08-03 01:33:19 +01003366 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003367
3368out:
Daniel Vetter84849902012-12-02 00:28:11 +01003369 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003370 return ret;
3371
3372}
3373
3374int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3375 void *data, struct drm_file *file_priv)
3376{
3377 struct drm_mode_crtc_lut *crtc_lut = data;
3378 struct drm_mode_object *obj;
3379 struct drm_crtc *crtc;
3380 void *r_base, *g_base, *b_base;
3381 int size;
3382 int ret = 0;
3383
Dave Airliefb3b06c2011-02-08 13:55:21 +10003384 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3385 return -EINVAL;
3386
Daniel Vetter84849902012-12-02 00:28:11 +01003387 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003388 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3389 if (!obj) {
3390 ret = -EINVAL;
3391 goto out;
3392 }
3393 crtc = obj_to_crtc(obj);
3394
3395 /* memcpy into gamma store */
3396 if (crtc_lut->gamma_size != crtc->gamma_size) {
3397 ret = -EINVAL;
3398 goto out;
3399 }
3400
3401 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3402 r_base = crtc->gamma_store;
3403 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3404 ret = -EFAULT;
3405 goto out;
3406 }
3407
3408 g_base = r_base + size;
3409 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3410 ret = -EFAULT;
3411 goto out;
3412 }
3413
3414 b_base = g_base + size;
3415 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3416 ret = -EFAULT;
3417 goto out;
3418 }
3419out:
Daniel Vetter84849902012-12-02 00:28:11 +01003420 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003421 return ret;
3422}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003423
3424int drm_mode_page_flip_ioctl(struct drm_device *dev,
3425 void *data, struct drm_file *file_priv)
3426{
3427 struct drm_mode_crtc_page_flip *page_flip = data;
3428 struct drm_mode_object *obj;
3429 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003430 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003431 struct drm_pending_vblank_event *e = NULL;
3432 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003433 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003434 int ret = -EINVAL;
3435
3436 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3437 page_flip->reserved != 0)
3438 return -EINVAL;
3439
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003440 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3441 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003442 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003443 crtc = obj_to_crtc(obj);
3444
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003445 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003446 if (crtc->fb == NULL) {
3447 /* The framebuffer is currently unbound, presumably
3448 * due to a hotplug event, that userspace has not
3449 * yet discovered.
3450 */
3451 ret = -EBUSY;
3452 goto out;
3453 }
3454
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003455 if (crtc->funcs->page_flip == NULL)
3456 goto out;
3457
Daniel Vetter786b99e2012-12-02 21:53:40 +01003458 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3459 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003460 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003461
Rob Clark7c80e122012-09-04 16:35:56 +00003462 hdisplay = crtc->mode.hdisplay;
3463 vdisplay = crtc->mode.vdisplay;
3464
3465 if (crtc->invert_dimensions)
3466 swap(hdisplay, vdisplay);
3467
3468 if (hdisplay > fb->width ||
3469 vdisplay > fb->height ||
3470 crtc->x > fb->width - hdisplay ||
3471 crtc->y > fb->height - vdisplay) {
3472 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3473 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3474 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003475 ret = -ENOSPC;
3476 goto out;
3477 }
3478
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003479 if (crtc->fb->pixel_format != fb->pixel_format) {
3480 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3481 ret = -EINVAL;
3482 goto out;
3483 }
3484
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003485 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3486 ret = -ENOMEM;
3487 spin_lock_irqsave(&dev->event_lock, flags);
3488 if (file_priv->event_space < sizeof e->event) {
3489 spin_unlock_irqrestore(&dev->event_lock, flags);
3490 goto out;
3491 }
3492 file_priv->event_space -= sizeof e->event;
3493 spin_unlock_irqrestore(&dev->event_lock, flags);
3494
3495 e = kzalloc(sizeof *e, GFP_KERNEL);
3496 if (e == NULL) {
3497 spin_lock_irqsave(&dev->event_lock, flags);
3498 file_priv->event_space += sizeof e->event;
3499 spin_unlock_irqrestore(&dev->event_lock, flags);
3500 goto out;
3501 }
3502
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003503 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003504 e->event.base.length = sizeof e->event;
3505 e->event.user_data = page_flip->user_data;
3506 e->base.event = &e->event.base;
3507 e->base.file_priv = file_priv;
3508 e->base.destroy =
3509 (void (*) (struct drm_pending_event *)) kfree;
3510 }
3511
Daniel Vetterb0d12322012-12-11 01:07:12 +01003512 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003513 ret = crtc->funcs->page_flip(crtc, fb, e);
3514 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003515 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3516 spin_lock_irqsave(&dev->event_lock, flags);
3517 file_priv->event_space += sizeof e->event;
3518 spin_unlock_irqrestore(&dev->event_lock, flags);
3519 kfree(e);
3520 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003521 /* Keep the old fb, don't unref it. */
3522 old_fb = NULL;
3523 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003524 /*
3525 * Warn if the driver hasn't properly updated the crtc->fb
3526 * field to reflect that the new framebuffer is now used.
3527 * Failing to do so will screw with the reference counting
3528 * on framebuffers.
3529 */
3530 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003531 /* Unref only the old framebuffer. */
3532 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003533 }
3534
3535out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003536 if (fb)
3537 drm_framebuffer_unreference(fb);
3538 if (old_fb)
3539 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003540 mutex_unlock(&crtc->mutex);
3541
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003542 return ret;
3543}
Chris Wilsoneb033552011-01-24 15:11:08 +00003544
3545void drm_mode_config_reset(struct drm_device *dev)
3546{
3547 struct drm_crtc *crtc;
3548 struct drm_encoder *encoder;
3549 struct drm_connector *connector;
3550
3551 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3552 if (crtc->funcs->reset)
3553 crtc->funcs->reset(crtc);
3554
3555 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3556 if (encoder->funcs->reset)
3557 encoder->funcs->reset(encoder);
3558
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003559 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3560 connector->status = connector_status_unknown;
3561
Chris Wilsoneb033552011-01-24 15:11:08 +00003562 if (connector->funcs->reset)
3563 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003564 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003565}
3566EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003567
3568int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3569 void *data, struct drm_file *file_priv)
3570{
3571 struct drm_mode_create_dumb *args = data;
3572
3573 if (!dev->driver->dumb_create)
3574 return -ENOSYS;
3575 return dev->driver->dumb_create(file_priv, dev, args);
3576}
3577
3578int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3579 void *data, struct drm_file *file_priv)
3580{
3581 struct drm_mode_map_dumb *args = data;
3582
3583 /* call driver ioctl to get mmap offset */
3584 if (!dev->driver->dumb_map_offset)
3585 return -ENOSYS;
3586
3587 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3588}
3589
3590int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3591 void *data, struct drm_file *file_priv)
3592{
3593 struct drm_mode_destroy_dumb *args = data;
3594
3595 if (!dev->driver->dumb_destroy)
3596 return -ENOSYS;
3597
3598 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3599}
Dave Airlie248dbc22011-11-29 20:02:54 +00003600
3601/*
3602 * Just need to support RGB formats here for compat with code that doesn't
3603 * use pixel formats directly yet.
3604 */
3605void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3606 int *bpp)
3607{
3608 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003609 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003610 case DRM_FORMAT_RGB332:
3611 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003612 *depth = 8;
3613 *bpp = 8;
3614 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003615 case DRM_FORMAT_XRGB1555:
3616 case DRM_FORMAT_XBGR1555:
3617 case DRM_FORMAT_RGBX5551:
3618 case DRM_FORMAT_BGRX5551:
3619 case DRM_FORMAT_ARGB1555:
3620 case DRM_FORMAT_ABGR1555:
3621 case DRM_FORMAT_RGBA5551:
3622 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003623 *depth = 15;
3624 *bpp = 16;
3625 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003626 case DRM_FORMAT_RGB565:
3627 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003628 *depth = 16;
3629 *bpp = 16;
3630 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003631 case DRM_FORMAT_RGB888:
3632 case DRM_FORMAT_BGR888:
3633 *depth = 24;
3634 *bpp = 24;
3635 break;
3636 case DRM_FORMAT_XRGB8888:
3637 case DRM_FORMAT_XBGR8888:
3638 case DRM_FORMAT_RGBX8888:
3639 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003640 *depth = 24;
3641 *bpp = 32;
3642 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003643 case DRM_FORMAT_XRGB2101010:
3644 case DRM_FORMAT_XBGR2101010:
3645 case DRM_FORMAT_RGBX1010102:
3646 case DRM_FORMAT_BGRX1010102:
3647 case DRM_FORMAT_ARGB2101010:
3648 case DRM_FORMAT_ABGR2101010:
3649 case DRM_FORMAT_RGBA1010102:
3650 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003651 *depth = 30;
3652 *bpp = 32;
3653 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003654 case DRM_FORMAT_ARGB8888:
3655 case DRM_FORMAT_ABGR8888:
3656 case DRM_FORMAT_RGBA8888:
3657 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003658 *depth = 32;
3659 *bpp = 32;
3660 break;
3661 default:
3662 DRM_DEBUG_KMS("unsupported pixel format\n");
3663 *depth = 0;
3664 *bpp = 0;
3665 break;
3666 }
3667}
3668EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003669
3670/**
3671 * drm_format_num_planes - get the number of planes for format
3672 * @format: pixel format (DRM_FORMAT_*)
3673 *
3674 * RETURNS:
3675 * The number of planes used by the specified pixel format.
3676 */
3677int drm_format_num_planes(uint32_t format)
3678{
3679 switch (format) {
3680 case DRM_FORMAT_YUV410:
3681 case DRM_FORMAT_YVU410:
3682 case DRM_FORMAT_YUV411:
3683 case DRM_FORMAT_YVU411:
3684 case DRM_FORMAT_YUV420:
3685 case DRM_FORMAT_YVU420:
3686 case DRM_FORMAT_YUV422:
3687 case DRM_FORMAT_YVU422:
3688 case DRM_FORMAT_YUV444:
3689 case DRM_FORMAT_YVU444:
3690 return 3;
3691 case DRM_FORMAT_NV12:
3692 case DRM_FORMAT_NV21:
3693 case DRM_FORMAT_NV16:
3694 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003695 case DRM_FORMAT_NV24:
3696 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003697 return 2;
3698 default:
3699 return 1;
3700 }
3701}
3702EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003703
3704/**
3705 * drm_format_plane_cpp - determine the bytes per pixel value
3706 * @format: pixel format (DRM_FORMAT_*)
3707 * @plane: plane index
3708 *
3709 * RETURNS:
3710 * The bytes per pixel value for the specified plane.
3711 */
3712int drm_format_plane_cpp(uint32_t format, int plane)
3713{
3714 unsigned int depth;
3715 int bpp;
3716
3717 if (plane >= drm_format_num_planes(format))
3718 return 0;
3719
3720 switch (format) {
3721 case DRM_FORMAT_YUYV:
3722 case DRM_FORMAT_YVYU:
3723 case DRM_FORMAT_UYVY:
3724 case DRM_FORMAT_VYUY:
3725 return 2;
3726 case DRM_FORMAT_NV12:
3727 case DRM_FORMAT_NV21:
3728 case DRM_FORMAT_NV16:
3729 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003730 case DRM_FORMAT_NV24:
3731 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003732 return plane ? 2 : 1;
3733 case DRM_FORMAT_YUV410:
3734 case DRM_FORMAT_YVU410:
3735 case DRM_FORMAT_YUV411:
3736 case DRM_FORMAT_YVU411:
3737 case DRM_FORMAT_YUV420:
3738 case DRM_FORMAT_YVU420:
3739 case DRM_FORMAT_YUV422:
3740 case DRM_FORMAT_YVU422:
3741 case DRM_FORMAT_YUV444:
3742 case DRM_FORMAT_YVU444:
3743 return 1;
3744 default:
3745 drm_fb_get_bpp_depth(format, &depth, &bpp);
3746 return bpp >> 3;
3747 }
3748}
3749EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003750
3751/**
3752 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3753 * @format: pixel format (DRM_FORMAT_*)
3754 *
3755 * RETURNS:
3756 * The horizontal chroma subsampling factor for the
3757 * specified pixel format.
3758 */
3759int drm_format_horz_chroma_subsampling(uint32_t format)
3760{
3761 switch (format) {
3762 case DRM_FORMAT_YUV411:
3763 case DRM_FORMAT_YVU411:
3764 case DRM_FORMAT_YUV410:
3765 case DRM_FORMAT_YVU410:
3766 return 4;
3767 case DRM_FORMAT_YUYV:
3768 case DRM_FORMAT_YVYU:
3769 case DRM_FORMAT_UYVY:
3770 case DRM_FORMAT_VYUY:
3771 case DRM_FORMAT_NV12:
3772 case DRM_FORMAT_NV21:
3773 case DRM_FORMAT_NV16:
3774 case DRM_FORMAT_NV61:
3775 case DRM_FORMAT_YUV422:
3776 case DRM_FORMAT_YVU422:
3777 case DRM_FORMAT_YUV420:
3778 case DRM_FORMAT_YVU420:
3779 return 2;
3780 default:
3781 return 1;
3782 }
3783}
3784EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3785
3786/**
3787 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3788 * @format: pixel format (DRM_FORMAT_*)
3789 *
3790 * RETURNS:
3791 * The vertical chroma subsampling factor for the
3792 * specified pixel format.
3793 */
3794int drm_format_vert_chroma_subsampling(uint32_t format)
3795{
3796 switch (format) {
3797 case DRM_FORMAT_YUV410:
3798 case DRM_FORMAT_YVU410:
3799 return 4;
3800 case DRM_FORMAT_YUV420:
3801 case DRM_FORMAT_YVU420:
3802 case DRM_FORMAT_NV12:
3803 case DRM_FORMAT_NV21:
3804 return 2;
3805 default:
3806 return 1;
3807 }
3808}
3809EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003810
3811/**
3812 * drm_mode_config_init - initialize DRM mode_configuration structure
3813 * @dev: DRM device
3814 *
3815 * Initialize @dev's mode_config structure, used for tracking the graphics
3816 * configuration of @dev.
3817 *
3818 * Since this initializes the modeset locks, no locking is possible. Which is no
3819 * problem, since this should happen single threaded at init time. It is the
3820 * driver's problem to ensure this guarantee.
3821 *
3822 */
3823void drm_mode_config_init(struct drm_device *dev)
3824{
3825 mutex_init(&dev->mode_config.mutex);
3826 mutex_init(&dev->mode_config.idr_mutex);
3827 mutex_init(&dev->mode_config.fb_lock);
3828 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3829 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3830 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3831 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3832 INIT_LIST_HEAD(&dev->mode_config.property_list);
3833 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3834 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3835 idr_init(&dev->mode_config.crtc_idr);
3836
3837 drm_modeset_lock_all(dev);
3838 drm_mode_create_standard_connector_properties(dev);
3839 drm_modeset_unlock_all(dev);
3840
3841 /* Just to be sure */
3842 dev->mode_config.num_fb = 0;
3843 dev->mode_config.num_connector = 0;
3844 dev->mode_config.num_crtc = 0;
3845 dev->mode_config.num_encoder = 0;
3846}
3847EXPORT_SYMBOL(drm_mode_config_init);
3848
3849/**
3850 * drm_mode_config_cleanup - free up DRM mode_config info
3851 * @dev: DRM device
3852 *
3853 * Free up all the connectors and CRTCs associated with this DRM device, then
3854 * free up the framebuffers and associated buffer objects.
3855 *
3856 * Note that since this /should/ happen single-threaded at driver/device
3857 * teardown time, no locking is required. It's the driver's job to ensure that
3858 * this guarantee actually holds true.
3859 *
3860 * FIXME: cleanup any dangling user buffer objects too
3861 */
3862void drm_mode_config_cleanup(struct drm_device *dev)
3863{
3864 struct drm_connector *connector, *ot;
3865 struct drm_crtc *crtc, *ct;
3866 struct drm_encoder *encoder, *enct;
3867 struct drm_framebuffer *fb, *fbt;
3868 struct drm_property *property, *pt;
3869 struct drm_property_blob *blob, *bt;
3870 struct drm_plane *plane, *plt;
3871
3872 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3873 head) {
3874 encoder->funcs->destroy(encoder);
3875 }
3876
3877 list_for_each_entry_safe(connector, ot,
3878 &dev->mode_config.connector_list, head) {
3879 connector->funcs->destroy(connector);
3880 }
3881
3882 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3883 head) {
3884 drm_property_destroy(dev, property);
3885 }
3886
3887 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3888 head) {
3889 drm_property_destroy_blob(dev, blob);
3890 }
3891
3892 /*
3893 * Single-threaded teardown context, so it's not required to grab the
3894 * fb_lock to protect against concurrent fb_list access. Contrary, it
3895 * would actually deadlock with the drm_framebuffer_cleanup function.
3896 *
3897 * Also, if there are any framebuffers left, that's a driver leak now,
3898 * so politely WARN about this.
3899 */
3900 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3901 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3902 drm_framebuffer_remove(fb);
3903 }
3904
3905 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3906 head) {
3907 plane->funcs->destroy(plane);
3908 }
3909
3910 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3911 crtc->funcs->destroy(crtc);
3912 }
3913
3914 idr_destroy(&dev->mode_config.crtc_idr);
3915}
3916EXPORT_SYMBOL(drm_mode_config_cleanup);