blob: 02838e66af76e8578806d956939492fe2b47bf5e [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) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300595 if (plane->fb == fb)
596 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800597 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100598 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800599 }
600
Rob Clarkf7eff602012-09-05 21:48:38 +0000601 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800602}
Rob Clarkf7eff602012-09-05 21:48:38 +0000603EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800604
605/**
606 * drm_crtc_init - Initialise a new CRTC object
607 * @dev: DRM device
608 * @crtc: CRTC object to init
609 * @funcs: callbacks for the new CRTC
610 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000611 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200612 *
613 * RETURNS:
614 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800615 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200616int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800617 const struct drm_crtc_funcs *funcs)
618{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200619 int ret;
620
Dave Airlief453ba02008-11-07 14:05:41 -0800621 crtc->dev = dev;
622 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000623 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800624
Daniel Vetter84849902012-12-02 00:28:11 +0100625 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100626 mutex_init(&crtc->mutex);
627 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200628
629 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
630 if (ret)
631 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800632
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300633 crtc->base.properties = &crtc->properties;
634
Dave Airlief453ba02008-11-07 14:05:41 -0800635 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
636 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200637
638 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100639 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200640
641 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800642}
643EXPORT_SYMBOL(drm_crtc_init);
644
645/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000646 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800647 * @crtc: CRTC to cleanup
648 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000649 * This function cleans up @crtc and removes it from the DRM mode setting
650 * core. Note that the function does *not* free the crtc structure itself,
651 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800652 */
653void drm_crtc_cleanup(struct drm_crtc *crtc)
654{
655 struct drm_device *dev = crtc->dev;
656
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000657 kfree(crtc->gamma_store);
658 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800659
660 drm_mode_object_put(dev, &crtc->base);
661 list_del(&crtc->head);
662 dev->mode_config.num_crtc--;
663}
664EXPORT_SYMBOL(drm_crtc_cleanup);
665
666/**
667 * drm_mode_probed_add - add a mode to a connector's probed mode list
668 * @connector: connector the new mode
669 * @mode: mode data
670 *
Dave Airlief453ba02008-11-07 14:05:41 -0800671 * Add @mode to @connector's mode list for later use.
672 */
673void drm_mode_probed_add(struct drm_connector *connector,
674 struct drm_display_mode *mode)
675{
Ville Syrjälä990256a2013-05-31 12:17:07 +0000676 list_add_tail(&mode->head, &connector->probed_modes);
Dave Airlief453ba02008-11-07 14:05:41 -0800677}
678EXPORT_SYMBOL(drm_mode_probed_add);
679
680/**
681 * drm_mode_remove - remove and free a mode
682 * @connector: connector list to modify
683 * @mode: mode to remove
684 *
Dave Airlief453ba02008-11-07 14:05:41 -0800685 * Remove @mode from @connector's mode list, then free it.
686 */
687void drm_mode_remove(struct drm_connector *connector,
688 struct drm_display_mode *mode)
689{
690 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100691 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800692}
693EXPORT_SYMBOL(drm_mode_remove);
694
695/**
696 * drm_connector_init - Init a preallocated connector
697 * @dev: DRM device
698 * @connector: the connector to init
699 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100700 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800701 *
Dave Airlief453ba02008-11-07 14:05:41 -0800702 * Initialises a preallocated connector. Connectors should be
703 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200704 *
705 * RETURNS:
706 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800707 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200708int drm_connector_init(struct drm_device *dev,
709 struct drm_connector *connector,
710 const struct drm_connector_funcs *funcs,
711 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800712{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200713 int ret;
714
Daniel Vetter84849902012-12-02 00:28:11 +0100715 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800716
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200717 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
718 if (ret)
719 goto out;
720
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300721 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800722 connector->dev = dev;
723 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800724 connector->connector_type = connector_type;
725 connector->connector_type_id =
726 ++drm_connector_enum_list[connector_type].count; /* TODO */
Dave Airlief453ba02008-11-07 14:05:41 -0800727 INIT_LIST_HEAD(&connector->probed_modes);
728 INIT_LIST_HEAD(&connector->modes);
729 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000730 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800731
732 list_add_tail(&connector->head, &dev->mode_config.connector_list);
733 dev->mode_config.num_connector++;
734
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200735 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500736 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200737 dev->mode_config.edid_property,
738 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800739
Rob Clark58495562012-10-11 20:50:56 -0500740 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800741 dev->mode_config.dpms_property, 0);
742
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200743 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100744 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200745
746 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800747}
748EXPORT_SYMBOL(drm_connector_init);
749
750/**
751 * drm_connector_cleanup - cleans up an initialised connector
752 * @connector: connector to cleanup
753 *
Dave Airlief453ba02008-11-07 14:05:41 -0800754 * Cleans up the connector but doesn't free the object.
755 */
756void drm_connector_cleanup(struct drm_connector *connector)
757{
758 struct drm_device *dev = connector->dev;
759 struct drm_display_mode *mode, *t;
760
761 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
762 drm_mode_remove(connector, mode);
763
764 list_for_each_entry_safe(mode, t, &connector->modes, head)
765 drm_mode_remove(connector, mode);
766
Dave Airlief453ba02008-11-07 14:05:41 -0800767 drm_mode_object_put(dev, &connector->base);
768 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000769 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800770}
771EXPORT_SYMBOL(drm_connector_cleanup);
772
Dave Airliecbc7e222012-02-20 14:16:40 +0000773void drm_connector_unplug_all(struct drm_device *dev)
774{
775 struct drm_connector *connector;
776
777 /* taking the mode config mutex ends up in a clash with sysfs */
778 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
779 drm_sysfs_connector_remove(connector);
780
781}
782EXPORT_SYMBOL(drm_connector_unplug_all);
783
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200784int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000785 struct drm_encoder *encoder,
786 const struct drm_encoder_funcs *funcs,
787 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800788{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200789 int ret;
790
Daniel Vetter84849902012-12-02 00:28:11 +0100791 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800792
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200793 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
794 if (ret)
795 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800796
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200797 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800798 encoder->encoder_type = encoder_type;
799 encoder->funcs = funcs;
800
801 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
802 dev->mode_config.num_encoder++;
803
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200804 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100805 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200806
807 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800808}
809EXPORT_SYMBOL(drm_encoder_init);
810
811void drm_encoder_cleanup(struct drm_encoder *encoder)
812{
813 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100814 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800815 drm_mode_object_put(dev, &encoder->base);
816 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000817 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100818 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800819}
820EXPORT_SYMBOL(drm_encoder_cleanup);
821
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300822/**
823 * drm_plane_init - Initialise a new plane object
824 * @dev: DRM device
825 * @plane: plane object to init
826 * @possible_crtcs: bitmask of possible CRTCs
827 * @funcs: callbacks for the new plane
828 * @formats: array of supported formats (%DRM_FORMAT_*)
829 * @format_count: number of elements in @formats
830 * @priv: plane is private (hidden from userspace)?
831 *
832 * Inits a new object created as base part of a driver plane object.
833 *
834 * RETURNS:
835 * Zero on success, error code on failure.
836 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800837int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
838 unsigned long possible_crtcs,
839 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600840 const uint32_t *formats, uint32_t format_count,
841 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800842{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200843 int ret;
844
Daniel Vetter84849902012-12-02 00:28:11 +0100845 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800846
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200847 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
848 if (ret)
849 goto out;
850
Rob Clark4d939142012-05-17 02:23:27 -0600851 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800852 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800853 plane->funcs = funcs;
854 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
855 GFP_KERNEL);
856 if (!plane->format_types) {
857 DRM_DEBUG_KMS("out of memory when allocating plane\n");
858 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200859 ret = -ENOMEM;
860 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800861 }
862
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800863 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800864 plane->format_count = format_count;
865 plane->possible_crtcs = possible_crtcs;
866
Rob Clark0a7eb242011-12-13 20:19:36 -0600867 /* private planes are not exposed to userspace, but depending on
868 * display hardware, might be convenient to allow sharing programming
869 * for the scanout engine with the crtc implementation.
870 */
871 if (!priv) {
872 list_add_tail(&plane->head, &dev->mode_config.plane_list);
873 dev->mode_config.num_plane++;
874 } else {
875 INIT_LIST_HEAD(&plane->head);
876 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800877
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200878 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100879 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800880
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200881 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800882}
883EXPORT_SYMBOL(drm_plane_init);
884
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300885/**
886 * drm_plane_cleanup - Clean up the core plane usage
887 * @plane: plane to cleanup
888 *
889 * This function cleans up @plane and removes it from the DRM mode setting
890 * core. Note that the function does *not* free the plane structure itself,
891 * this is the responsibility of the caller.
892 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800893void drm_plane_cleanup(struct drm_plane *plane)
894{
895 struct drm_device *dev = plane->dev;
896
Daniel Vetter84849902012-12-02 00:28:11 +0100897 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800898 kfree(plane->format_types);
899 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600900 /* if not added to a list, it must be a private plane */
901 if (!list_empty(&plane->head)) {
902 list_del(&plane->head);
903 dev->mode_config.num_plane--;
904 }
Daniel Vetter84849902012-12-02 00:28:11 +0100905 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800906}
907EXPORT_SYMBOL(drm_plane_cleanup);
908
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300909/**
910 * drm_plane_force_disable - Forcibly disable a plane
911 * @plane: plane to disable
912 *
913 * Forces the plane to be disabled.
914 *
915 * Used when the plane's current framebuffer is destroyed,
916 * and when restoring fbdev mode.
917 */
Ville Syrjälä9125e612013-06-03 16:10:40 +0300918void drm_plane_force_disable(struct drm_plane *plane)
919{
920 int ret;
921
922 if (!plane->fb)
923 return;
924
925 ret = plane->funcs->disable_plane(plane);
926 if (ret)
927 DRM_ERROR("failed to disable plane with busy fb\n");
928 /* disconnect the plane from the fb and crtc: */
929 __drm_framebuffer_unreference(plane->fb);
930 plane->fb = NULL;
931 plane->crtc = NULL;
932}
933EXPORT_SYMBOL(drm_plane_force_disable);
934
Dave Airlief453ba02008-11-07 14:05:41 -0800935/**
936 * drm_mode_create - create a new display mode
937 * @dev: DRM device
938 *
Dave Airlief453ba02008-11-07 14:05:41 -0800939 * Create a new drm_display_mode, give it an ID, and return it.
940 *
941 * RETURNS:
942 * Pointer to new mode on success, NULL on error.
943 */
944struct drm_display_mode *drm_mode_create(struct drm_device *dev)
945{
946 struct drm_display_mode *nmode;
947
948 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
949 if (!nmode)
950 return NULL;
951
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200952 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
953 kfree(nmode);
954 return NULL;
955 }
956
Dave Airlief453ba02008-11-07 14:05:41 -0800957 return nmode;
958}
959EXPORT_SYMBOL(drm_mode_create);
960
961/**
962 * drm_mode_destroy - remove a mode
963 * @dev: DRM device
964 * @mode: mode to remove
965 *
Dave Airlief453ba02008-11-07 14:05:41 -0800966 * Free @mode's unique identifier, then free it.
967 */
968void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
969{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200970 if (!mode)
971 return;
972
Dave Airlief453ba02008-11-07 14:05:41 -0800973 drm_mode_object_put(dev, &mode->base);
974
975 kfree(mode);
976}
977EXPORT_SYMBOL(drm_mode_destroy);
978
979static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
980{
981 struct drm_property *edid;
982 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800983
984 /*
985 * Standard properties (apply to all connectors)
986 */
987 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
988 DRM_MODE_PROP_IMMUTABLE,
989 "EDID", 0);
990 dev->mode_config.edid_property = edid;
991
Sascha Hauer4a67d392012-02-06 10:58:17 +0100992 dpms = drm_property_create_enum(dev, 0,
993 "DPMS", drm_dpms_enum_list,
994 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800995 dev->mode_config.dpms_property = dpms;
996
997 return 0;
998}
999
1000/**
1001 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1002 * @dev: DRM device
1003 *
1004 * Called by a driver the first time a DVI-I connector is made.
1005 */
1006int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1007{
1008 struct drm_property *dvi_i_selector;
1009 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001010
1011 if (dev->mode_config.dvi_i_select_subconnector_property)
1012 return 0;
1013
1014 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001015 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001016 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001017 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001018 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001019 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1020
Sascha Hauer4a67d392012-02-06 10:58:17 +01001021 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001022 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001023 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001024 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001025 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1026
1027 return 0;
1028}
1029EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1030
1031/**
1032 * drm_create_tv_properties - create TV specific connector properties
1033 * @dev: DRM device
1034 * @num_modes: number of different TV formats (modes) supported
1035 * @modes: array of pointers to strings containing name of each format
1036 *
1037 * Called by a driver's TV initialization routine, this function creates
1038 * the TV specific connector properties for a given device. Caller is
1039 * responsible for allocating a list of format names and passing them to
1040 * this routine.
1041 */
1042int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1043 char *modes[])
1044{
1045 struct drm_property *tv_selector;
1046 struct drm_property *tv_subconnector;
1047 int i;
1048
1049 if (dev->mode_config.tv_select_subconnector_property)
1050 return 0;
1051
1052 /*
1053 * Basic connector properties
1054 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001055 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001056 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001057 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001058 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001059 dev->mode_config.tv_select_subconnector_property = tv_selector;
1060
1061 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001062 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1063 "subconnector",
1064 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001065 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001066 dev->mode_config.tv_subconnector_property = tv_subconnector;
1067
1068 /*
1069 * Other, TV specific properties: margins & TV modes.
1070 */
1071 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001072 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001073
1074 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001075 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001076
1077 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001078 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001079
1080 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001081 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001082
1083 dev->mode_config.tv_mode_property =
1084 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1085 "mode", num_modes);
1086 for (i = 0; i < num_modes; i++)
1087 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1088 i, modes[i]);
1089
Francisco Jerezb6b79022009-08-02 04:19:20 +02001090 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001091 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001092
1093 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001094 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001095
1096 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001097 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001098
Francisco Jereza75f0232009-08-12 02:30:10 +02001099 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001100 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001101
1102 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001103 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001104
1105 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001106 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001107
Dave Airlief453ba02008-11-07 14:05:41 -08001108 return 0;
1109}
1110EXPORT_SYMBOL(drm_mode_create_tv_properties);
1111
1112/**
1113 * drm_mode_create_scaling_mode_property - create scaling mode property
1114 * @dev: DRM device
1115 *
1116 * Called by a driver the first time it's needed, must be attached to desired
1117 * connectors.
1118 */
1119int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1120{
1121 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001122
1123 if (dev->mode_config.scaling_mode_property)
1124 return 0;
1125
1126 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001127 drm_property_create_enum(dev, 0, "scaling mode",
1128 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001129 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001130
1131 dev->mode_config.scaling_mode_property = scaling_mode;
1132
1133 return 0;
1134}
1135EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1136
1137/**
1138 * drm_mode_create_dithering_property - create dithering property
1139 * @dev: DRM device
1140 *
1141 * Called by a driver the first time it's needed, must be attached to desired
1142 * connectors.
1143 */
1144int drm_mode_create_dithering_property(struct drm_device *dev)
1145{
1146 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001147
1148 if (dev->mode_config.dithering_mode_property)
1149 return 0;
1150
1151 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001152 drm_property_create_enum(dev, 0, "dithering",
1153 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001154 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001155 dev->mode_config.dithering_mode_property = dithering_mode;
1156
1157 return 0;
1158}
1159EXPORT_SYMBOL(drm_mode_create_dithering_property);
1160
1161/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001162 * drm_mode_create_dirty_property - create dirty property
1163 * @dev: DRM device
1164 *
1165 * Called by a driver the first time it's needed, must be attached to desired
1166 * connectors.
1167 */
1168int drm_mode_create_dirty_info_property(struct drm_device *dev)
1169{
1170 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001171
1172 if (dev->mode_config.dirty_info_property)
1173 return 0;
1174
1175 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001176 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001177 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001178 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001179 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001180 dev->mode_config.dirty_info_property = dirty_info;
1181
1182 return 0;
1183}
1184EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1185
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001186static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001187{
1188 uint32_t total_objects = 0;
1189
1190 total_objects += dev->mode_config.num_crtc;
1191 total_objects += dev->mode_config.num_connector;
1192 total_objects += dev->mode_config.num_encoder;
1193
Dave Airlief453ba02008-11-07 14:05:41 -08001194 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1195 if (!group->id_list)
1196 return -ENOMEM;
1197
1198 group->num_crtcs = 0;
1199 group->num_connectors = 0;
1200 group->num_encoders = 0;
1201 return 0;
1202}
1203
1204int drm_mode_group_init_legacy_group(struct drm_device *dev,
1205 struct drm_mode_group *group)
1206{
1207 struct drm_crtc *crtc;
1208 struct drm_encoder *encoder;
1209 struct drm_connector *connector;
1210 int ret;
1211
1212 if ((ret = drm_mode_group_init(dev, group)))
1213 return ret;
1214
1215 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1216 group->id_list[group->num_crtcs++] = crtc->base.id;
1217
1218 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1219 group->id_list[group->num_crtcs + group->num_encoders++] =
1220 encoder->base.id;
1221
1222 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1223 group->id_list[group->num_crtcs + group->num_encoders +
1224 group->num_connectors++] = connector->base.id;
1225
1226 return 0;
1227}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001228EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001229
1230/**
Dave Airlief453ba02008-11-07 14:05:41 -08001231 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1232 * @out: drm_mode_modeinfo struct to return to the user
1233 * @in: drm_display_mode to use
1234 *
Dave Airlief453ba02008-11-07 14:05:41 -08001235 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1236 * the user.
1237 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001238static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1239 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001240{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001241 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1242 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1243 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1244 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1245 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1246 "timing values too large for mode info\n");
1247
Dave Airlief453ba02008-11-07 14:05:41 -08001248 out->clock = in->clock;
1249 out->hdisplay = in->hdisplay;
1250 out->hsync_start = in->hsync_start;
1251 out->hsync_end = in->hsync_end;
1252 out->htotal = in->htotal;
1253 out->hskew = in->hskew;
1254 out->vdisplay = in->vdisplay;
1255 out->vsync_start = in->vsync_start;
1256 out->vsync_end = in->vsync_end;
1257 out->vtotal = in->vtotal;
1258 out->vscan = in->vscan;
1259 out->vrefresh = in->vrefresh;
1260 out->flags = in->flags;
1261 out->type = in->type;
1262 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1263 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1264}
1265
1266/**
1267 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1268 * @out: drm_display_mode to return to the user
1269 * @in: drm_mode_modeinfo to use
1270 *
Dave Airlief453ba02008-11-07 14:05:41 -08001271 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1272 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001273 *
1274 * RETURNS:
1275 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001276 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001277static int drm_crtc_convert_umode(struct drm_display_mode *out,
1278 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001279{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001280 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1281 return -ERANGE;
1282
Dave Airlief453ba02008-11-07 14:05:41 -08001283 out->clock = in->clock;
1284 out->hdisplay = in->hdisplay;
1285 out->hsync_start = in->hsync_start;
1286 out->hsync_end = in->hsync_end;
1287 out->htotal = in->htotal;
1288 out->hskew = in->hskew;
1289 out->vdisplay = in->vdisplay;
1290 out->vsync_start = in->vsync_start;
1291 out->vsync_end = in->vsync_end;
1292 out->vtotal = in->vtotal;
1293 out->vscan = in->vscan;
1294 out->vrefresh = in->vrefresh;
1295 out->flags = in->flags;
1296 out->type = in->type;
1297 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1298 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001299
1300 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001301}
1302
1303/**
1304 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001305 * @dev: drm device for the ioctl
1306 * @data: data pointer for the ioctl
1307 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001308 *
Dave Airlief453ba02008-11-07 14:05:41 -08001309 * Construct a set of configuration description structures and return
1310 * them to the user, including CRTC, connector and framebuffer configuration.
1311 *
1312 * Called by the user via ioctl.
1313 *
1314 * RETURNS:
1315 * Zero on success, errno on failure.
1316 */
1317int drm_mode_getresources(struct drm_device *dev, void *data,
1318 struct drm_file *file_priv)
1319{
1320 struct drm_mode_card_res *card_res = data;
1321 struct list_head *lh;
1322 struct drm_framebuffer *fb;
1323 struct drm_connector *connector;
1324 struct drm_crtc *crtc;
1325 struct drm_encoder *encoder;
1326 int ret = 0;
1327 int connector_count = 0;
1328 int crtc_count = 0;
1329 int fb_count = 0;
1330 int encoder_count = 0;
1331 int copied = 0, i;
1332 uint32_t __user *fb_id;
1333 uint32_t __user *crtc_id;
1334 uint32_t __user *connector_id;
1335 uint32_t __user *encoder_id;
1336 struct drm_mode_group *mode_group;
1337
Dave Airliefb3b06c2011-02-08 13:55:21 +10001338 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1339 return -EINVAL;
1340
Dave Airlief453ba02008-11-07 14:05:41 -08001341
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001342 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001343 /*
1344 * For the non-control nodes we need to limit the list of resources
1345 * by IDs in the group list for this node
1346 */
1347 list_for_each(lh, &file_priv->fbs)
1348 fb_count++;
1349
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001350 /* handle this in 4 parts */
1351 /* FBs */
1352 if (card_res->count_fbs >= fb_count) {
1353 copied = 0;
1354 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1355 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1356 if (put_user(fb->base.id, fb_id + copied)) {
1357 mutex_unlock(&file_priv->fbs_lock);
1358 return -EFAULT;
1359 }
1360 copied++;
1361 }
1362 }
1363 card_res->count_fbs = fb_count;
1364 mutex_unlock(&file_priv->fbs_lock);
1365
1366 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001367 mode_group = &file_priv->master->minor->mode_group;
1368 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1369
1370 list_for_each(lh, &dev->mode_config.crtc_list)
1371 crtc_count++;
1372
1373 list_for_each(lh, &dev->mode_config.connector_list)
1374 connector_count++;
1375
1376 list_for_each(lh, &dev->mode_config.encoder_list)
1377 encoder_count++;
1378 } else {
1379
1380 crtc_count = mode_group->num_crtcs;
1381 connector_count = mode_group->num_connectors;
1382 encoder_count = mode_group->num_encoders;
1383 }
1384
1385 card_res->max_height = dev->mode_config.max_height;
1386 card_res->min_height = dev->mode_config.min_height;
1387 card_res->max_width = dev->mode_config.max_width;
1388 card_res->min_width = dev->mode_config.min_width;
1389
Dave Airlief453ba02008-11-07 14:05:41 -08001390 /* CRTCs */
1391 if (card_res->count_crtcs >= crtc_count) {
1392 copied = 0;
1393 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1394 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1395 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1396 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001397 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001398 if (put_user(crtc->base.id, crtc_id + copied)) {
1399 ret = -EFAULT;
1400 goto out;
1401 }
1402 copied++;
1403 }
1404 } else {
1405 for (i = 0; i < mode_group->num_crtcs; i++) {
1406 if (put_user(mode_group->id_list[i],
1407 crtc_id + copied)) {
1408 ret = -EFAULT;
1409 goto out;
1410 }
1411 copied++;
1412 }
1413 }
1414 }
1415 card_res->count_crtcs = crtc_count;
1416
1417 /* Encoders */
1418 if (card_res->count_encoders >= encoder_count) {
1419 copied = 0;
1420 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1421 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1422 list_for_each_entry(encoder,
1423 &dev->mode_config.encoder_list,
1424 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001425 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1426 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001427 if (put_user(encoder->base.id, encoder_id +
1428 copied)) {
1429 ret = -EFAULT;
1430 goto out;
1431 }
1432 copied++;
1433 }
1434 } else {
1435 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1436 if (put_user(mode_group->id_list[i],
1437 encoder_id + copied)) {
1438 ret = -EFAULT;
1439 goto out;
1440 }
1441 copied++;
1442 }
1443
1444 }
1445 }
1446 card_res->count_encoders = encoder_count;
1447
1448 /* Connectors */
1449 if (card_res->count_connectors >= connector_count) {
1450 copied = 0;
1451 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1452 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1453 list_for_each_entry(connector,
1454 &dev->mode_config.connector_list,
1455 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001456 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1457 connector->base.id,
1458 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001459 if (put_user(connector->base.id,
1460 connector_id + copied)) {
1461 ret = -EFAULT;
1462 goto out;
1463 }
1464 copied++;
1465 }
1466 } else {
1467 int start = mode_group->num_crtcs +
1468 mode_group->num_encoders;
1469 for (i = start; i < start + mode_group->num_connectors; i++) {
1470 if (put_user(mode_group->id_list[i],
1471 connector_id + copied)) {
1472 ret = -EFAULT;
1473 goto out;
1474 }
1475 copied++;
1476 }
1477 }
1478 }
1479 card_res->count_connectors = connector_count;
1480
Jerome Glisse94401062010-07-15 15:43:25 -04001481 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001482 card_res->count_connectors, card_res->count_encoders);
1483
1484out:
Daniel Vetter84849902012-12-02 00:28:11 +01001485 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001486 return ret;
1487}
1488
1489/**
1490 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001491 * @dev: drm device for the ioctl
1492 * @data: data pointer for the ioctl
1493 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001494 *
Dave Airlief453ba02008-11-07 14:05:41 -08001495 * Construct a CRTC configuration structure to return to the user.
1496 *
1497 * Called by the user via ioctl.
1498 *
1499 * RETURNS:
1500 * Zero on success, errno on failure.
1501 */
1502int drm_mode_getcrtc(struct drm_device *dev,
1503 void *data, struct drm_file *file_priv)
1504{
1505 struct drm_mode_crtc *crtc_resp = data;
1506 struct drm_crtc *crtc;
1507 struct drm_mode_object *obj;
1508 int ret = 0;
1509
Dave Airliefb3b06c2011-02-08 13:55:21 +10001510 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1511 return -EINVAL;
1512
Daniel Vetter84849902012-12-02 00:28:11 +01001513 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001514
1515 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1516 DRM_MODE_OBJECT_CRTC);
1517 if (!obj) {
1518 ret = -EINVAL;
1519 goto out;
1520 }
1521 crtc = obj_to_crtc(obj);
1522
1523 crtc_resp->x = crtc->x;
1524 crtc_resp->y = crtc->y;
1525 crtc_resp->gamma_size = crtc->gamma_size;
1526 if (crtc->fb)
1527 crtc_resp->fb_id = crtc->fb->base.id;
1528 else
1529 crtc_resp->fb_id = 0;
1530
1531 if (crtc->enabled) {
1532
1533 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1534 crtc_resp->mode_valid = 1;
1535
1536 } else {
1537 crtc_resp->mode_valid = 0;
1538 }
1539
1540out:
Daniel Vetter84849902012-12-02 00:28:11 +01001541 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001542 return ret;
1543}
1544
1545/**
1546 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001547 * @dev: drm device for the ioctl
1548 * @data: data pointer for the ioctl
1549 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001550 *
Dave Airlief453ba02008-11-07 14:05:41 -08001551 * Construct a connector configuration structure to return to the user.
1552 *
1553 * Called by the user via ioctl.
1554 *
1555 * RETURNS:
1556 * Zero on success, errno on failure.
1557 */
1558int drm_mode_getconnector(struct drm_device *dev, void *data,
1559 struct drm_file *file_priv)
1560{
1561 struct drm_mode_get_connector *out_resp = data;
1562 struct drm_mode_object *obj;
1563 struct drm_connector *connector;
1564 struct drm_display_mode *mode;
1565 int mode_count = 0;
1566 int props_count = 0;
1567 int encoders_count = 0;
1568 int ret = 0;
1569 int copied = 0;
1570 int i;
1571 struct drm_mode_modeinfo u_mode;
1572 struct drm_mode_modeinfo __user *mode_ptr;
1573 uint32_t __user *prop_ptr;
1574 uint64_t __user *prop_values;
1575 uint32_t __user *encoder_ptr;
1576
Dave Airliefb3b06c2011-02-08 13:55:21 +10001577 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1578 return -EINVAL;
1579
Dave Airlief453ba02008-11-07 14:05:41 -08001580 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1581
Jerome Glisse94401062010-07-15 15:43:25 -04001582 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001583
Daniel Vetter7b240562012-12-12 00:35:33 +01001584 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001585
1586 obj = drm_mode_object_find(dev, out_resp->connector_id,
1587 DRM_MODE_OBJECT_CONNECTOR);
1588 if (!obj) {
1589 ret = -EINVAL;
1590 goto out;
1591 }
1592 connector = obj_to_connector(obj);
1593
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001594 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001595
1596 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1597 if (connector->encoder_ids[i] != 0) {
1598 encoders_count++;
1599 }
1600 }
1601
1602 if (out_resp->count_modes == 0) {
1603 connector->funcs->fill_modes(connector,
1604 dev->mode_config.max_width,
1605 dev->mode_config.max_height);
1606 }
1607
1608 /* delayed so we get modes regardless of pre-fill_modes state */
1609 list_for_each_entry(mode, &connector->modes, head)
1610 mode_count++;
1611
1612 out_resp->connector_id = connector->base.id;
1613 out_resp->connector_type = connector->connector_type;
1614 out_resp->connector_type_id = connector->connector_type_id;
1615 out_resp->mm_width = connector->display_info.width_mm;
1616 out_resp->mm_height = connector->display_info.height_mm;
1617 out_resp->subpixel = connector->display_info.subpixel_order;
1618 out_resp->connection = connector->status;
1619 if (connector->encoder)
1620 out_resp->encoder_id = connector->encoder->base.id;
1621 else
1622 out_resp->encoder_id = 0;
1623
1624 /*
1625 * This ioctl is called twice, once to determine how much space is
1626 * needed, and the 2nd time to fill it.
1627 */
1628 if ((out_resp->count_modes >= mode_count) && mode_count) {
1629 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001630 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001631 list_for_each_entry(mode, &connector->modes, head) {
1632 drm_crtc_convert_to_umode(&u_mode, mode);
1633 if (copy_to_user(mode_ptr + copied,
1634 &u_mode, sizeof(u_mode))) {
1635 ret = -EFAULT;
1636 goto out;
1637 }
1638 copied++;
1639 }
1640 }
1641 out_resp->count_modes = mode_count;
1642
1643 if ((out_resp->count_props >= props_count) && props_count) {
1644 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001645 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1646 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001647 for (i = 0; i < connector->properties.count; i++) {
1648 if (put_user(connector->properties.ids[i],
1649 prop_ptr + copied)) {
1650 ret = -EFAULT;
1651 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001652 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001653
1654 if (put_user(connector->properties.values[i],
1655 prop_values + copied)) {
1656 ret = -EFAULT;
1657 goto out;
1658 }
1659 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001660 }
1661 }
1662 out_resp->count_props = props_count;
1663
1664 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1665 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001666 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001667 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1668 if (connector->encoder_ids[i] != 0) {
1669 if (put_user(connector->encoder_ids[i],
1670 encoder_ptr + copied)) {
1671 ret = -EFAULT;
1672 goto out;
1673 }
1674 copied++;
1675 }
1676 }
1677 }
1678 out_resp->count_encoders = encoders_count;
1679
1680out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001681 mutex_unlock(&dev->mode_config.mutex);
1682
Dave Airlief453ba02008-11-07 14:05:41 -08001683 return ret;
1684}
1685
1686int drm_mode_getencoder(struct drm_device *dev, void *data,
1687 struct drm_file *file_priv)
1688{
1689 struct drm_mode_get_encoder *enc_resp = data;
1690 struct drm_mode_object *obj;
1691 struct drm_encoder *encoder;
1692 int ret = 0;
1693
Dave Airliefb3b06c2011-02-08 13:55:21 +10001694 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1695 return -EINVAL;
1696
Daniel Vetter84849902012-12-02 00:28:11 +01001697 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001698 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1699 DRM_MODE_OBJECT_ENCODER);
1700 if (!obj) {
1701 ret = -EINVAL;
1702 goto out;
1703 }
1704 encoder = obj_to_encoder(obj);
1705
1706 if (encoder->crtc)
1707 enc_resp->crtc_id = encoder->crtc->base.id;
1708 else
1709 enc_resp->crtc_id = 0;
1710 enc_resp->encoder_type = encoder->encoder_type;
1711 enc_resp->encoder_id = encoder->base.id;
1712 enc_resp->possible_crtcs = encoder->possible_crtcs;
1713 enc_resp->possible_clones = encoder->possible_clones;
1714
1715out:
Daniel Vetter84849902012-12-02 00:28:11 +01001716 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001717 return ret;
1718}
1719
1720/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001721 * drm_mode_getplane_res - get plane info
1722 * @dev: DRM device
1723 * @data: ioctl data
1724 * @file_priv: DRM file info
1725 *
1726 * Return an plane count and set of IDs.
1727 */
1728int drm_mode_getplane_res(struct drm_device *dev, void *data,
1729 struct drm_file *file_priv)
1730{
1731 struct drm_mode_get_plane_res *plane_resp = data;
1732 struct drm_mode_config *config;
1733 struct drm_plane *plane;
1734 uint32_t __user *plane_ptr;
1735 int copied = 0, ret = 0;
1736
1737 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1738 return -EINVAL;
1739
Daniel Vetter84849902012-12-02 00:28:11 +01001740 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001741 config = &dev->mode_config;
1742
1743 /*
1744 * This ioctl is called twice, once to determine how much space is
1745 * needed, and the 2nd time to fill it.
1746 */
1747 if (config->num_plane &&
1748 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001749 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001750
1751 list_for_each_entry(plane, &config->plane_list, head) {
1752 if (put_user(plane->base.id, plane_ptr + copied)) {
1753 ret = -EFAULT;
1754 goto out;
1755 }
1756 copied++;
1757 }
1758 }
1759 plane_resp->count_planes = config->num_plane;
1760
1761out:
Daniel Vetter84849902012-12-02 00:28:11 +01001762 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001763 return ret;
1764}
1765
1766/**
1767 * drm_mode_getplane - get plane info
1768 * @dev: DRM device
1769 * @data: ioctl data
1770 * @file_priv: DRM file info
1771 *
1772 * Return plane info, including formats supported, gamma size, any
1773 * current fb, etc.
1774 */
1775int drm_mode_getplane(struct drm_device *dev, void *data,
1776 struct drm_file *file_priv)
1777{
1778 struct drm_mode_get_plane *plane_resp = data;
1779 struct drm_mode_object *obj;
1780 struct drm_plane *plane;
1781 uint32_t __user *format_ptr;
1782 int ret = 0;
1783
1784 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1785 return -EINVAL;
1786
Daniel Vetter84849902012-12-02 00:28:11 +01001787 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001788 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1789 DRM_MODE_OBJECT_PLANE);
1790 if (!obj) {
1791 ret = -ENOENT;
1792 goto out;
1793 }
1794 plane = obj_to_plane(obj);
1795
1796 if (plane->crtc)
1797 plane_resp->crtc_id = plane->crtc->base.id;
1798 else
1799 plane_resp->crtc_id = 0;
1800
1801 if (plane->fb)
1802 plane_resp->fb_id = plane->fb->base.id;
1803 else
1804 plane_resp->fb_id = 0;
1805
1806 plane_resp->plane_id = plane->base.id;
1807 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001808 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001809
1810 /*
1811 * This ioctl is called twice, once to determine how much space is
1812 * needed, and the 2nd time to fill it.
1813 */
1814 if (plane->format_count &&
1815 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001816 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001817 if (copy_to_user(format_ptr,
1818 plane->format_types,
1819 sizeof(uint32_t) * plane->format_count)) {
1820 ret = -EFAULT;
1821 goto out;
1822 }
1823 }
1824 plane_resp->count_format_types = plane->format_count;
1825
1826out:
Daniel Vetter84849902012-12-02 00:28:11 +01001827 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001828 return ret;
1829}
1830
1831/**
1832 * drm_mode_setplane - set up or tear down an plane
1833 * @dev: DRM device
1834 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001835 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001836 *
1837 * Set plane info, including placement, fb, scaling, and other factors.
1838 * Or pass a NULL fb to disable.
1839 */
1840int drm_mode_setplane(struct drm_device *dev, void *data,
1841 struct drm_file *file_priv)
1842{
1843 struct drm_mode_set_plane *plane_req = data;
1844 struct drm_mode_object *obj;
1845 struct drm_plane *plane;
1846 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001847 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001848 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001849 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001850 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001851
1852 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1853 return -EINVAL;
1854
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001855 /*
1856 * First, find the plane, crtc, and fb objects. If not available,
1857 * we don't bother to call the driver.
1858 */
1859 obj = drm_mode_object_find(dev, plane_req->plane_id,
1860 DRM_MODE_OBJECT_PLANE);
1861 if (!obj) {
1862 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1863 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001864 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001865 }
1866 plane = obj_to_plane(obj);
1867
1868 /* No fb means shut it down */
1869 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001870 drm_modeset_lock_all(dev);
1871 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001872 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001873 plane->crtc = NULL;
1874 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001875 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001876 goto out;
1877 }
1878
1879 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1880 DRM_MODE_OBJECT_CRTC);
1881 if (!obj) {
1882 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1883 plane_req->crtc_id);
1884 ret = -ENOENT;
1885 goto out;
1886 }
1887 crtc = obj_to_crtc(obj);
1888
Daniel Vetter786b99e2012-12-02 21:53:40 +01001889 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1890 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001891 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1892 plane_req->fb_id);
1893 ret = -ENOENT;
1894 goto out;
1895 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001896
Ville Syrjälä62443be2011-12-20 00:06:47 +02001897 /* Check whether this plane supports the fb pixel format. */
1898 for (i = 0; i < plane->format_count; i++)
1899 if (fb->pixel_format == plane->format_types[i])
1900 break;
1901 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03001902 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1903 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02001904 ret = -EINVAL;
1905 goto out;
1906 }
1907
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001908 fb_width = fb->width << 16;
1909 fb_height = fb->height << 16;
1910
1911 /* Make sure source coordinates are inside the fb. */
1912 if (plane_req->src_w > fb_width ||
1913 plane_req->src_x > fb_width - plane_req->src_w ||
1914 plane_req->src_h > fb_height ||
1915 plane_req->src_y > fb_height - plane_req->src_h) {
1916 DRM_DEBUG_KMS("Invalid source coordinates "
1917 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1918 plane_req->src_w >> 16,
1919 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1920 plane_req->src_h >> 16,
1921 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1922 plane_req->src_x >> 16,
1923 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1924 plane_req->src_y >> 16,
1925 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1926 ret = -ENOSPC;
1927 goto out;
1928 }
1929
Ville Syrjälä687a0402011-12-20 00:06:45 +02001930 /* Give drivers some help against integer overflows */
1931 if (plane_req->crtc_w > INT_MAX ||
1932 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1933 plane_req->crtc_h > INT_MAX ||
1934 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1935 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1936 plane_req->crtc_w, plane_req->crtc_h,
1937 plane_req->crtc_x, plane_req->crtc_y);
1938 ret = -ERANGE;
1939 goto out;
1940 }
1941
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001942 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001943 ret = plane->funcs->update_plane(plane, crtc, fb,
1944 plane_req->crtc_x, plane_req->crtc_y,
1945 plane_req->crtc_w, plane_req->crtc_h,
1946 plane_req->src_x, plane_req->src_y,
1947 plane_req->src_w, plane_req->src_h);
1948 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001949 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001950 plane->crtc = crtc;
1951 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001952 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001953 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001954 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001955
1956out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001957 if (fb)
1958 drm_framebuffer_unreference(fb);
1959 if (old_fb)
1960 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001961
1962 return ret;
1963}
1964
1965/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001966 * drm_mode_set_config_internal - helper to call ->set_config
1967 * @set: modeset config to set
1968 *
1969 * This is a little helper to wrap internal calls to the ->set_config driver
1970 * interface. The only thing it adds is correct refcounting dance.
1971 */
1972int drm_mode_set_config_internal(struct drm_mode_set *set)
1973{
1974 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001975 struct drm_framebuffer *fb;
1976 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001977 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001978
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001979 /*
1980 * NOTE: ->set_config can also disable other crtcs (if we steal all
1981 * connectors from it), hence we need to refcount the fbs across all
1982 * crtcs. Atomic modeset will have saner semantics ...
1983 */
1984 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
1985 tmp->old_fb = tmp->fb;
1986
Daniel Vetterb0d12322012-12-11 01:07:12 +01001987 fb = set->fb;
1988
1989 ret = crtc->funcs->set_config(set);
1990 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02001991 /* crtc->fb must be updated by ->set_config, enforces this. */
1992 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001993 }
Daniel Vettercc85e122013-06-15 00:13:15 +02001994
Daniel Vetter5cef29a2013-06-15 00:13:16 +02001995 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
1996 if (tmp->fb)
1997 drm_framebuffer_reference(tmp->fb);
1998 if (tmp->old_fb)
1999 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002000 }
2001
2002 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002003}
2004EXPORT_SYMBOL(drm_mode_set_config_internal);
2005
2006/**
Dave Airlief453ba02008-11-07 14:05:41 -08002007 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002008 * @dev: drm device for the ioctl
2009 * @data: data pointer for the ioctl
2010 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002011 *
Dave Airlief453ba02008-11-07 14:05:41 -08002012 * Build a new CRTC configuration based on user request.
2013 *
2014 * Called by the user via ioctl.
2015 *
2016 * RETURNS:
2017 * Zero on success, errno on failure.
2018 */
2019int drm_mode_setcrtc(struct drm_device *dev, void *data,
2020 struct drm_file *file_priv)
2021{
2022 struct drm_mode_config *config = &dev->mode_config;
2023 struct drm_mode_crtc *crtc_req = data;
2024 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002025 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002026 struct drm_connector **connector_set = NULL, *connector;
2027 struct drm_framebuffer *fb = NULL;
2028 struct drm_display_mode *mode = NULL;
2029 struct drm_mode_set set;
2030 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002031 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002032 int i;
2033
Dave Airliefb3b06c2011-02-08 13:55:21 +10002034 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2035 return -EINVAL;
2036
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002037 /* For some reason crtc x/y offsets are signed internally. */
2038 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2039 return -ERANGE;
2040
Daniel Vetter84849902012-12-02 00:28:11 +01002041 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002042 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2043 DRM_MODE_OBJECT_CRTC);
2044 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002045 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002046 ret = -EINVAL;
2047 goto out;
2048 }
2049 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002050 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002051
2052 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002053 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002054 /* If we have a mode we need a framebuffer. */
2055 /* If we pass -1, set the mode with the currently bound fb */
2056 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002057 if (!crtc->fb) {
2058 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2059 ret = -EINVAL;
2060 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002061 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002062 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002063 /* Make refcounting symmetric with the lookup path. */
2064 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002065 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002066 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2067 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002068 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2069 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002070 ret = -EINVAL;
2071 goto out;
2072 }
Dave Airlief453ba02008-11-07 14:05:41 -08002073 }
2074
2075 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002076 if (!mode) {
2077 ret = -ENOMEM;
2078 goto out;
2079 }
2080
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002081 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2082 if (ret) {
2083 DRM_DEBUG_KMS("Invalid mode\n");
2084 goto out;
2085 }
2086
Dave Airlief453ba02008-11-07 14:05:41 -08002087 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002088
Rob Clark7c80e122012-09-04 16:35:56 +00002089 hdisplay = mode->hdisplay;
2090 vdisplay = mode->vdisplay;
2091
2092 if (crtc->invert_dimensions)
2093 swap(hdisplay, vdisplay);
2094
2095 if (hdisplay > fb->width ||
2096 vdisplay > fb->height ||
2097 crtc_req->x > fb->width - hdisplay ||
2098 crtc_req->y > fb->height - vdisplay) {
2099 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2100 fb->width, fb->height,
2101 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2102 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002103 ret = -ENOSPC;
2104 goto out;
2105 }
Dave Airlief453ba02008-11-07 14:05:41 -08002106 }
2107
2108 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002109 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002110 ret = -EINVAL;
2111 goto out;
2112 }
2113
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002114 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002115 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002116 crtc_req->count_connectors);
2117 ret = -EINVAL;
2118 goto out;
2119 }
2120
2121 if (crtc_req->count_connectors > 0) {
2122 u32 out_id;
2123
2124 /* Avoid unbounded kernel memory allocation */
2125 if (crtc_req->count_connectors > config->num_connector) {
2126 ret = -EINVAL;
2127 goto out;
2128 }
2129
2130 connector_set = kmalloc(crtc_req->count_connectors *
2131 sizeof(struct drm_connector *),
2132 GFP_KERNEL);
2133 if (!connector_set) {
2134 ret = -ENOMEM;
2135 goto out;
2136 }
2137
2138 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002139 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002140 if (get_user(out_id, &set_connectors_ptr[i])) {
2141 ret = -EFAULT;
2142 goto out;
2143 }
2144
2145 obj = drm_mode_object_find(dev, out_id,
2146 DRM_MODE_OBJECT_CONNECTOR);
2147 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002148 DRM_DEBUG_KMS("Connector id %d unknown\n",
2149 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002150 ret = -EINVAL;
2151 goto out;
2152 }
2153 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002154 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2155 connector->base.id,
2156 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002157
2158 connector_set[i] = connector;
2159 }
2160 }
2161
2162 set.crtc = crtc;
2163 set.x = crtc_req->x;
2164 set.y = crtc_req->y;
2165 set.mode = mode;
2166 set.connectors = connector_set;
2167 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002168 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002169 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002170
2171out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002172 if (fb)
2173 drm_framebuffer_unreference(fb);
2174
Dave Airlief453ba02008-11-07 14:05:41 -08002175 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002176 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002177 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002178 return ret;
2179}
2180
2181int drm_mode_cursor_ioctl(struct drm_device *dev,
2182 void *data, struct drm_file *file_priv)
2183{
2184 struct drm_mode_cursor *req = data;
2185 struct drm_mode_object *obj;
2186 struct drm_crtc *crtc;
2187 int ret = 0;
2188
Dave Airliefb3b06c2011-02-08 13:55:21 +10002189 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2190 return -EINVAL;
2191
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002192 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002193 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002194
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002195 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002196 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002197 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002198 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002199 }
2200 crtc = obj_to_crtc(obj);
2201
Daniel Vetterdac35662012-12-02 15:24:10 +01002202 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002203 if (req->flags & DRM_MODE_CURSOR_BO) {
2204 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002205 ret = -ENXIO;
2206 goto out;
2207 }
2208 /* Turns off the cursor if handle is 0 */
2209 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2210 req->width, req->height);
2211 }
2212
2213 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2214 if (crtc->funcs->cursor_move) {
2215 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2216 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002217 ret = -EFAULT;
2218 goto out;
2219 }
2220 }
2221out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002222 mutex_unlock(&crtc->mutex);
2223
Dave Airlief453ba02008-11-07 14:05:41 -08002224 return ret;
2225}
2226
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002227/* Original addfb only supported RGB formats, so figure out which one */
2228uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2229{
2230 uint32_t fmt;
2231
2232 switch (bpp) {
2233 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002234 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002235 break;
2236 case 16:
2237 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002238 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002239 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002240 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002241 break;
2242 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002243 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002244 break;
2245 case 32:
2246 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002247 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002248 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002249 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002250 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002251 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002252 break;
2253 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002254 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2255 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002256 break;
2257 }
2258
2259 return fmt;
2260}
2261EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2262
Dave Airlief453ba02008-11-07 14:05:41 -08002263/**
2264 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002265 * @dev: drm device for the ioctl
2266 * @data: data pointer for the ioctl
2267 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002268 *
Dave Airlief453ba02008-11-07 14:05:41 -08002269 * Add a new FB to the specified CRTC, given a user request.
2270 *
2271 * Called by the user via ioctl.
2272 *
2273 * RETURNS:
2274 * Zero on success, errno on failure.
2275 */
2276int drm_mode_addfb(struct drm_device *dev,
2277 void *data, struct drm_file *file_priv)
2278{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002279 struct drm_mode_fb_cmd *or = data;
2280 struct drm_mode_fb_cmd2 r = {};
2281 struct drm_mode_config *config = &dev->mode_config;
2282 struct drm_framebuffer *fb;
2283 int ret = 0;
2284
2285 /* Use new struct with format internally */
2286 r.fb_id = or->fb_id;
2287 r.width = or->width;
2288 r.height = or->height;
2289 r.pitches[0] = or->pitch;
2290 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2291 r.handles[0] = or->handle;
2292
2293 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2294 return -EINVAL;
2295
Jesse Barnesacb4b992011-11-07 12:03:23 -08002296 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002297 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002298
2299 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002300 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002301
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002302 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2303 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002304 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002305 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002306 }
2307
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002308 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002309 or->fb_id = fb->base.id;
2310 list_add(&fb->filp_head, &file_priv->fbs);
2311 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002312 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002313
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002314 return ret;
2315}
2316
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002317static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002318{
2319 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2320
2321 switch (format) {
2322 case DRM_FORMAT_C8:
2323 case DRM_FORMAT_RGB332:
2324 case DRM_FORMAT_BGR233:
2325 case DRM_FORMAT_XRGB4444:
2326 case DRM_FORMAT_XBGR4444:
2327 case DRM_FORMAT_RGBX4444:
2328 case DRM_FORMAT_BGRX4444:
2329 case DRM_FORMAT_ARGB4444:
2330 case DRM_FORMAT_ABGR4444:
2331 case DRM_FORMAT_RGBA4444:
2332 case DRM_FORMAT_BGRA4444:
2333 case DRM_FORMAT_XRGB1555:
2334 case DRM_FORMAT_XBGR1555:
2335 case DRM_FORMAT_RGBX5551:
2336 case DRM_FORMAT_BGRX5551:
2337 case DRM_FORMAT_ARGB1555:
2338 case DRM_FORMAT_ABGR1555:
2339 case DRM_FORMAT_RGBA5551:
2340 case DRM_FORMAT_BGRA5551:
2341 case DRM_FORMAT_RGB565:
2342 case DRM_FORMAT_BGR565:
2343 case DRM_FORMAT_RGB888:
2344 case DRM_FORMAT_BGR888:
2345 case DRM_FORMAT_XRGB8888:
2346 case DRM_FORMAT_XBGR8888:
2347 case DRM_FORMAT_RGBX8888:
2348 case DRM_FORMAT_BGRX8888:
2349 case DRM_FORMAT_ARGB8888:
2350 case DRM_FORMAT_ABGR8888:
2351 case DRM_FORMAT_RGBA8888:
2352 case DRM_FORMAT_BGRA8888:
2353 case DRM_FORMAT_XRGB2101010:
2354 case DRM_FORMAT_XBGR2101010:
2355 case DRM_FORMAT_RGBX1010102:
2356 case DRM_FORMAT_BGRX1010102:
2357 case DRM_FORMAT_ARGB2101010:
2358 case DRM_FORMAT_ABGR2101010:
2359 case DRM_FORMAT_RGBA1010102:
2360 case DRM_FORMAT_BGRA1010102:
2361 case DRM_FORMAT_YUYV:
2362 case DRM_FORMAT_YVYU:
2363 case DRM_FORMAT_UYVY:
2364 case DRM_FORMAT_VYUY:
2365 case DRM_FORMAT_AYUV:
2366 case DRM_FORMAT_NV12:
2367 case DRM_FORMAT_NV21:
2368 case DRM_FORMAT_NV16:
2369 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002370 case DRM_FORMAT_NV24:
2371 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002372 case DRM_FORMAT_YUV410:
2373 case DRM_FORMAT_YVU410:
2374 case DRM_FORMAT_YUV411:
2375 case DRM_FORMAT_YVU411:
2376 case DRM_FORMAT_YUV420:
2377 case DRM_FORMAT_YVU420:
2378 case DRM_FORMAT_YUV422:
2379 case DRM_FORMAT_YVU422:
2380 case DRM_FORMAT_YUV444:
2381 case DRM_FORMAT_YVU444:
2382 return 0;
2383 default:
2384 return -EINVAL;
2385 }
2386}
2387
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002388static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002389{
2390 int ret, hsub, vsub, num_planes, i;
2391
2392 ret = format_check(r);
2393 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002394 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2395 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002396 return ret;
2397 }
2398
2399 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2400 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2401 num_planes = drm_format_num_planes(r->pixel_format);
2402
2403 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002404 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002405 return -EINVAL;
2406 }
2407
2408 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002409 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002410 return -EINVAL;
2411 }
2412
2413 for (i = 0; i < num_planes; i++) {
2414 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002415 unsigned int height = r->height / (i != 0 ? vsub : 1);
2416 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002417
2418 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002419 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002420 return -EINVAL;
2421 }
2422
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002423 if ((uint64_t) width * cpp > UINT_MAX)
2424 return -ERANGE;
2425
2426 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2427 return -ERANGE;
2428
2429 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002430 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002431 return -EINVAL;
2432 }
2433 }
2434
2435 return 0;
2436}
2437
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002438/**
2439 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002440 * @dev: drm device for the ioctl
2441 * @data: data pointer for the ioctl
2442 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002443 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002444 * Add a new FB to the specified CRTC, given a user request with format.
2445 *
2446 * Called by the user via ioctl.
2447 *
2448 * RETURNS:
2449 * Zero on success, errno on failure.
2450 */
2451int drm_mode_addfb2(struct drm_device *dev,
2452 void *data, struct drm_file *file_priv)
2453{
2454 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002455 struct drm_mode_config *config = &dev->mode_config;
2456 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002457 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002458
Dave Airliefb3b06c2011-02-08 13:55:21 +10002459 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2460 return -EINVAL;
2461
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002462 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2463 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2464 return -EINVAL;
2465 }
2466
Dave Airlief453ba02008-11-07 14:05:41 -08002467 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002468 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002469 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002470 return -EINVAL;
2471 }
2472 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002473 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002474 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002475 return -EINVAL;
2476 }
2477
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002478 ret = framebuffer_check(r);
2479 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002480 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002481
Dave Airlief453ba02008-11-07 14:05:41 -08002482 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002483 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002484 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002485 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002486 }
2487
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002488 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002489 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002490 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002491 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002492 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002493
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002494
Dave Airlief453ba02008-11-07 14:05:41 -08002495 return ret;
2496}
2497
2498/**
2499 * drm_mode_rmfb - remove an FB from the configuration
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 * Remove the FB specified by the user.
2505 *
2506 * Called by the user via ioctl.
2507 *
2508 * RETURNS:
2509 * Zero on success, errno on failure.
2510 */
2511int drm_mode_rmfb(struct drm_device *dev,
2512 void *data, struct drm_file *file_priv)
2513{
Dave Airlief453ba02008-11-07 14:05:41 -08002514 struct drm_framebuffer *fb = NULL;
2515 struct drm_framebuffer *fbl = NULL;
2516 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002517 int found = 0;
2518
Dave Airliefb3b06c2011-02-08 13:55:21 +10002519 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2520 return -EINVAL;
2521
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002522 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002523 mutex_lock(&dev->mode_config.fb_lock);
2524 fb = __drm_framebuffer_lookup(dev, *id);
2525 if (!fb)
2526 goto fail_lookup;
2527
Dave Airlief453ba02008-11-07 14:05:41 -08002528 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2529 if (fb == fbl)
2530 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002531 if (!found)
2532 goto fail_lookup;
2533
2534 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2535 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002536
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002537 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002538 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002539 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002540
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002541 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002542
Daniel Vetter2b677e82012-12-10 21:16:05 +01002543 return 0;
2544
2545fail_lookup:
2546 mutex_unlock(&dev->mode_config.fb_lock);
2547 mutex_unlock(&file_priv->fbs_lock);
2548
2549 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002550}
2551
2552/**
2553 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002554 * @dev: drm device for the ioctl
2555 * @data: data pointer for the ioctl
2556 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002557 *
Dave Airlief453ba02008-11-07 14:05:41 -08002558 * Lookup the FB given its ID and return info about it.
2559 *
2560 * Called by the user via ioctl.
2561 *
2562 * RETURNS:
2563 * Zero on success, errno on failure.
2564 */
2565int drm_mode_getfb(struct drm_device *dev,
2566 void *data, struct drm_file *file_priv)
2567{
2568 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002569 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002570 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002571
Dave Airliefb3b06c2011-02-08 13:55:21 +10002572 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2573 return -EINVAL;
2574
Daniel Vetter786b99e2012-12-02 21:53:40 +01002575 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002576 if (!fb)
2577 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002578
2579 r->height = fb->height;
2580 r->width = fb->width;
2581 r->depth = fb->depth;
2582 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002583 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002584 if (fb->funcs->create_handle)
2585 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2586 else
2587 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002588
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002589 drm_framebuffer_unreference(fb);
2590
Dave Airlief453ba02008-11-07 14:05:41 -08002591 return ret;
2592}
2593
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002594int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2595 void *data, struct drm_file *file_priv)
2596{
2597 struct drm_clip_rect __user *clips_ptr;
2598 struct drm_clip_rect *clips = NULL;
2599 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002600 struct drm_framebuffer *fb;
2601 unsigned flags;
2602 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002603 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002604
Dave Airliefb3b06c2011-02-08 13:55:21 +10002605 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2606 return -EINVAL;
2607
Daniel Vetter786b99e2012-12-02 21:53:40 +01002608 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002609 if (!fb)
2610 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002611
2612 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002613 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002614
2615 if (!num_clips != !clips_ptr) {
2616 ret = -EINVAL;
2617 goto out_err1;
2618 }
2619
2620 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2621
2622 /* If userspace annotates copy, clips must come in pairs */
2623 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2624 ret = -EINVAL;
2625 goto out_err1;
2626 }
2627
2628 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002629 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2630 ret = -EINVAL;
2631 goto out_err1;
2632 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002633 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2634 if (!clips) {
2635 ret = -ENOMEM;
2636 goto out_err1;
2637 }
2638
2639 ret = copy_from_user(clips, clips_ptr,
2640 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002641 if (ret) {
2642 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002643 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002644 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002645 }
2646
2647 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002648 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002649 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2650 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002651 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002652 } else {
2653 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002654 }
2655
2656out_err2:
2657 kfree(clips);
2658out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002659 drm_framebuffer_unreference(fb);
2660
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002661 return ret;
2662}
2663
2664
Dave Airlief453ba02008-11-07 14:05:41 -08002665/**
2666 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002667 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002668 *
Dave Airlief453ba02008-11-07 14:05:41 -08002669 * Destroy all the FBs associated with @filp.
2670 *
2671 * Called by the user via ioctl.
2672 *
2673 * RETURNS:
2674 * Zero on success, errno on failure.
2675 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002676void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002677{
Dave Airlief453ba02008-11-07 14:05:41 -08002678 struct drm_device *dev = priv->minor->dev;
2679 struct drm_framebuffer *fb, *tfb;
2680
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002681 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002682 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002683
2684 mutex_lock(&dev->mode_config.fb_lock);
2685 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2686 __drm_framebuffer_unregister(dev, fb);
2687 mutex_unlock(&dev->mode_config.fb_lock);
2688
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002689 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002690
2691 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002692 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002693 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002694 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002695}
2696
Dave Airlief453ba02008-11-07 14:05:41 -08002697struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2698 const char *name, int num_values)
2699{
2700 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002701 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002702
2703 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2704 if (!property)
2705 return NULL;
2706
2707 if (num_values) {
2708 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2709 if (!property->values)
2710 goto fail;
2711 }
2712
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002713 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2714 if (ret)
2715 goto fail;
2716
Dave Airlief453ba02008-11-07 14:05:41 -08002717 property->flags = flags;
2718 property->num_values = num_values;
2719 INIT_LIST_HEAD(&property->enum_blob_list);
2720
Vinson Lee471dd2e2011-11-10 11:55:40 -08002721 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002722 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002723 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2724 }
Dave Airlief453ba02008-11-07 14:05:41 -08002725
2726 list_add_tail(&property->head, &dev->mode_config.property_list);
2727 return property;
2728fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002729 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002730 kfree(property);
2731 return NULL;
2732}
2733EXPORT_SYMBOL(drm_property_create);
2734
Sascha Hauer4a67d392012-02-06 10:58:17 +01002735struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2736 const char *name,
2737 const struct drm_prop_enum_list *props,
2738 int num_values)
2739{
2740 struct drm_property *property;
2741 int i, ret;
2742
2743 flags |= DRM_MODE_PROP_ENUM;
2744
2745 property = drm_property_create(dev, flags, name, num_values);
2746 if (!property)
2747 return NULL;
2748
2749 for (i = 0; i < num_values; i++) {
2750 ret = drm_property_add_enum(property, i,
2751 props[i].type,
2752 props[i].name);
2753 if (ret) {
2754 drm_property_destroy(dev, property);
2755 return NULL;
2756 }
2757 }
2758
2759 return property;
2760}
2761EXPORT_SYMBOL(drm_property_create_enum);
2762
Rob Clark49e27542012-05-17 02:23:26 -06002763struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2764 int flags, const char *name,
2765 const struct drm_prop_enum_list *props,
2766 int num_values)
2767{
2768 struct drm_property *property;
2769 int i, ret;
2770
2771 flags |= DRM_MODE_PROP_BITMASK;
2772
2773 property = drm_property_create(dev, flags, name, num_values);
2774 if (!property)
2775 return NULL;
2776
2777 for (i = 0; i < num_values; i++) {
2778 ret = drm_property_add_enum(property, i,
2779 props[i].type,
2780 props[i].name);
2781 if (ret) {
2782 drm_property_destroy(dev, property);
2783 return NULL;
2784 }
2785 }
2786
2787 return property;
2788}
2789EXPORT_SYMBOL(drm_property_create_bitmask);
2790
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002791struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2792 const char *name,
2793 uint64_t min, uint64_t max)
2794{
2795 struct drm_property *property;
2796
2797 flags |= DRM_MODE_PROP_RANGE;
2798
2799 property = drm_property_create(dev, flags, name, 2);
2800 if (!property)
2801 return NULL;
2802
2803 property->values[0] = min;
2804 property->values[1] = max;
2805
2806 return property;
2807}
2808EXPORT_SYMBOL(drm_property_create_range);
2809
Dave Airlief453ba02008-11-07 14:05:41 -08002810int drm_property_add_enum(struct drm_property *property, int index,
2811 uint64_t value, const char *name)
2812{
2813 struct drm_property_enum *prop_enum;
2814
Rob Clark49e27542012-05-17 02:23:26 -06002815 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2816 return -EINVAL;
2817
2818 /*
2819 * Bitmask enum properties have the additional constraint of values
2820 * from 0 to 63
2821 */
2822 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002823 return -EINVAL;
2824
2825 if (!list_empty(&property->enum_blob_list)) {
2826 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2827 if (prop_enum->value == value) {
2828 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2829 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2830 return 0;
2831 }
2832 }
2833 }
2834
2835 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2836 if (!prop_enum)
2837 return -ENOMEM;
2838
2839 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2840 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2841 prop_enum->value = value;
2842
2843 property->values[index] = value;
2844 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2845 return 0;
2846}
2847EXPORT_SYMBOL(drm_property_add_enum);
2848
2849void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2850{
2851 struct drm_property_enum *prop_enum, *pt;
2852
2853 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2854 list_del(&prop_enum->head);
2855 kfree(prop_enum);
2856 }
2857
2858 if (property->num_values)
2859 kfree(property->values);
2860 drm_mode_object_put(dev, &property->base);
2861 list_del(&property->head);
2862 kfree(property);
2863}
2864EXPORT_SYMBOL(drm_property_destroy);
2865
Paulo Zanonic5431882012-05-15 18:09:02 -03002866void drm_object_attach_property(struct drm_mode_object *obj,
2867 struct drm_property *property,
2868 uint64_t init_val)
2869{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002870 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002871
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002872 if (count == DRM_OBJECT_MAX_PROPERTY) {
2873 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2874 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2875 "you see this message on the same object type.\n",
2876 obj->type);
2877 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002878 }
2879
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002880 obj->properties->ids[count] = property->base.id;
2881 obj->properties->values[count] = init_val;
2882 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002883}
2884EXPORT_SYMBOL(drm_object_attach_property);
2885
2886int drm_object_property_set_value(struct drm_mode_object *obj,
2887 struct drm_property *property, uint64_t val)
2888{
2889 int i;
2890
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002891 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002892 if (obj->properties->ids[i] == property->base.id) {
2893 obj->properties->values[i] = val;
2894 return 0;
2895 }
2896 }
2897
2898 return -EINVAL;
2899}
2900EXPORT_SYMBOL(drm_object_property_set_value);
2901
2902int drm_object_property_get_value(struct drm_mode_object *obj,
2903 struct drm_property *property, uint64_t *val)
2904{
2905 int i;
2906
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002907 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002908 if (obj->properties->ids[i] == property->base.id) {
2909 *val = obj->properties->values[i];
2910 return 0;
2911 }
2912 }
2913
2914 return -EINVAL;
2915}
2916EXPORT_SYMBOL(drm_object_property_get_value);
2917
Dave Airlief453ba02008-11-07 14:05:41 -08002918int drm_mode_getproperty_ioctl(struct drm_device *dev,
2919 void *data, struct drm_file *file_priv)
2920{
2921 struct drm_mode_object *obj;
2922 struct drm_mode_get_property *out_resp = data;
2923 struct drm_property *property;
2924 int enum_count = 0;
2925 int blob_count = 0;
2926 int value_count = 0;
2927 int ret = 0, i;
2928 int copied;
2929 struct drm_property_enum *prop_enum;
2930 struct drm_mode_property_enum __user *enum_ptr;
2931 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002932 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002933 uint64_t __user *values_ptr;
2934 uint32_t __user *blob_length_ptr;
2935
Dave Airliefb3b06c2011-02-08 13:55:21 +10002936 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2937 return -EINVAL;
2938
Daniel Vetter84849902012-12-02 00:28:11 +01002939 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002940 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2941 if (!obj) {
2942 ret = -EINVAL;
2943 goto done;
2944 }
2945 property = obj_to_property(obj);
2946
Rob Clark49e27542012-05-17 02:23:26 -06002947 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002948 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2949 enum_count++;
2950 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2951 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2952 blob_count++;
2953 }
2954
2955 value_count = property->num_values;
2956
2957 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2958 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2959 out_resp->flags = property->flags;
2960
2961 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002962 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002963 for (i = 0; i < value_count; i++) {
2964 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2965 ret = -EFAULT;
2966 goto done;
2967 }
2968 }
2969 }
2970 out_resp->count_values = value_count;
2971
Rob Clark49e27542012-05-17 02:23:26 -06002972 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002973 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2974 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002975 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002976 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2977
2978 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2979 ret = -EFAULT;
2980 goto done;
2981 }
2982
2983 if (copy_to_user(&enum_ptr[copied].name,
2984 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2985 ret = -EFAULT;
2986 goto done;
2987 }
2988 copied++;
2989 }
2990 }
2991 out_resp->count_enum_blobs = enum_count;
2992 }
2993
2994 if (property->flags & DRM_MODE_PROP_BLOB) {
2995 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2996 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002997 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2998 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002999
3000 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3001 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3002 ret = -EFAULT;
3003 goto done;
3004 }
3005
3006 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3007 ret = -EFAULT;
3008 goto done;
3009 }
3010
3011 copied++;
3012 }
3013 }
3014 out_resp->count_enum_blobs = blob_count;
3015 }
3016done:
Daniel Vetter84849902012-12-02 00:28:11 +01003017 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003018 return ret;
3019}
3020
3021static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3022 void *data)
3023{
3024 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003025 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003026
3027 if (!length || !data)
3028 return NULL;
3029
3030 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3031 if (!blob)
3032 return NULL;
3033
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003034 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3035 if (ret) {
3036 kfree(blob);
3037 return NULL;
3038 }
3039
Dave Airlief453ba02008-11-07 14:05:41 -08003040 blob->length = length;
3041
3042 memcpy(blob->data, data, length);
3043
Dave Airlief453ba02008-11-07 14:05:41 -08003044 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3045 return blob;
3046}
3047
3048static void drm_property_destroy_blob(struct drm_device *dev,
3049 struct drm_property_blob *blob)
3050{
3051 drm_mode_object_put(dev, &blob->base);
3052 list_del(&blob->head);
3053 kfree(blob);
3054}
3055
3056int drm_mode_getblob_ioctl(struct drm_device *dev,
3057 void *data, struct drm_file *file_priv)
3058{
3059 struct drm_mode_object *obj;
3060 struct drm_mode_get_blob *out_resp = data;
3061 struct drm_property_blob *blob;
3062 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003063 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003064
Dave Airliefb3b06c2011-02-08 13:55:21 +10003065 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3066 return -EINVAL;
3067
Daniel Vetter84849902012-12-02 00:28:11 +01003068 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003069 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3070 if (!obj) {
3071 ret = -EINVAL;
3072 goto done;
3073 }
3074 blob = obj_to_blob(obj);
3075
3076 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003077 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003078 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3079 ret = -EFAULT;
3080 goto done;
3081 }
3082 }
3083 out_resp->length = blob->length;
3084
3085done:
Daniel Vetter84849902012-12-02 00:28:11 +01003086 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003087 return ret;
3088}
3089
3090int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3091 struct edid *edid)
3092{
3093 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003094 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003095
3096 if (connector->edid_blob_ptr)
3097 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3098
3099 /* Delete edid, when there is none. */
3100 if (!edid) {
3101 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003102 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003103 return ret;
3104 }
3105
Adam Jackson7466f4c2010-03-29 21:43:23 +00003106 size = EDID_LENGTH * (1 + edid->extensions);
3107 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3108 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003109 if (!connector->edid_blob_ptr)
3110 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003111
Rob Clark58495562012-10-11 20:50:56 -05003112 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003113 dev->mode_config.edid_property,
3114 connector->edid_blob_ptr->base.id);
3115
3116 return ret;
3117}
3118EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3119
Paulo Zanoni26a34812012-05-15 18:08:59 -03003120static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003121 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003122{
3123 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3124 return false;
3125 if (property->flags & DRM_MODE_PROP_RANGE) {
3126 if (value < property->values[0] || value > property->values[1])
3127 return false;
3128 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003129 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3130 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003131 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003132 for (i = 0; i < property->num_values; i++)
3133 valid_mask |= (1ULL << property->values[i]);
3134 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003135 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3136 /* Only the driver knows */
3137 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003138 } else {
3139 int i;
3140 for (i = 0; i < property->num_values; i++)
3141 if (property->values[i] == value)
3142 return true;
3143 return false;
3144 }
3145}
3146
Dave Airlief453ba02008-11-07 14:05:41 -08003147int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3148 void *data, struct drm_file *file_priv)
3149{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003150 struct drm_mode_connector_set_property *conn_set_prop = data;
3151 struct drm_mode_obj_set_property obj_set_prop = {
3152 .value = conn_set_prop->value,
3153 .prop_id = conn_set_prop->prop_id,
3154 .obj_id = conn_set_prop->connector_id,
3155 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3156 };
Dave Airlief453ba02008-11-07 14:05:41 -08003157
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003158 /* It does all the locking and checking we need */
3159 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003160}
3161
Paulo Zanonic5431882012-05-15 18:09:02 -03003162static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3163 struct drm_property *property,
3164 uint64_t value)
3165{
3166 int ret = -EINVAL;
3167 struct drm_connector *connector = obj_to_connector(obj);
3168
3169 /* Do DPMS ourselves */
3170 if (property == connector->dev->mode_config.dpms_property) {
3171 if (connector->funcs->dpms)
3172 (*connector->funcs->dpms)(connector, (int)value);
3173 ret = 0;
3174 } else if (connector->funcs->set_property)
3175 ret = connector->funcs->set_property(connector, property, value);
3176
3177 /* store the property value if successful */
3178 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003179 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003180 return ret;
3181}
3182
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003183static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3184 struct drm_property *property,
3185 uint64_t value)
3186{
3187 int ret = -EINVAL;
3188 struct drm_crtc *crtc = obj_to_crtc(obj);
3189
3190 if (crtc->funcs->set_property)
3191 ret = crtc->funcs->set_property(crtc, property, value);
3192 if (!ret)
3193 drm_object_property_set_value(obj, property, value);
3194
3195 return ret;
3196}
3197
Rob Clark4d939142012-05-17 02:23:27 -06003198static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3199 struct drm_property *property,
3200 uint64_t value)
3201{
3202 int ret = -EINVAL;
3203 struct drm_plane *plane = obj_to_plane(obj);
3204
3205 if (plane->funcs->set_property)
3206 ret = plane->funcs->set_property(plane, property, value);
3207 if (!ret)
3208 drm_object_property_set_value(obj, property, value);
3209
3210 return ret;
3211}
3212
Paulo Zanonic5431882012-05-15 18:09:02 -03003213int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3214 struct drm_file *file_priv)
3215{
3216 struct drm_mode_obj_get_properties *arg = data;
3217 struct drm_mode_object *obj;
3218 int ret = 0;
3219 int i;
3220 int copied = 0;
3221 int props_count = 0;
3222 uint32_t __user *props_ptr;
3223 uint64_t __user *prop_values_ptr;
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 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3231 if (!obj) {
3232 ret = -EINVAL;
3233 goto out;
3234 }
3235 if (!obj->properties) {
3236 ret = -EINVAL;
3237 goto out;
3238 }
3239
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003240 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003241
3242 /* This ioctl is called twice, once to determine how much space is
3243 * needed, and the 2nd time to fill it. */
3244 if ((arg->count_props >= props_count) && props_count) {
3245 copied = 0;
3246 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3247 prop_values_ptr = (uint64_t __user *)(unsigned long)
3248 (arg->prop_values_ptr);
3249 for (i = 0; i < props_count; i++) {
3250 if (put_user(obj->properties->ids[i],
3251 props_ptr + copied)) {
3252 ret = -EFAULT;
3253 goto out;
3254 }
3255 if (put_user(obj->properties->values[i],
3256 prop_values_ptr + copied)) {
3257 ret = -EFAULT;
3258 goto out;
3259 }
3260 copied++;
3261 }
3262 }
3263 arg->count_props = props_count;
3264out:
Daniel Vetter84849902012-12-02 00:28:11 +01003265 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003266 return ret;
3267}
3268
3269int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3270 struct drm_file *file_priv)
3271{
3272 struct drm_mode_obj_set_property *arg = data;
3273 struct drm_mode_object *arg_obj;
3274 struct drm_mode_object *prop_obj;
3275 struct drm_property *property;
3276 int ret = -EINVAL;
3277 int i;
3278
3279 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3280 return -EINVAL;
3281
Daniel Vetter84849902012-12-02 00:28:11 +01003282 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003283
3284 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3285 if (!arg_obj)
3286 goto out;
3287 if (!arg_obj->properties)
3288 goto out;
3289
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003290 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003291 if (arg_obj->properties->ids[i] == arg->prop_id)
3292 break;
3293
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003294 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003295 goto out;
3296
3297 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3298 DRM_MODE_OBJECT_PROPERTY);
3299 if (!prop_obj)
3300 goto out;
3301 property = obj_to_property(prop_obj);
3302
3303 if (!drm_property_change_is_valid(property, arg->value))
3304 goto out;
3305
3306 switch (arg_obj->type) {
3307 case DRM_MODE_OBJECT_CONNECTOR:
3308 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3309 arg->value);
3310 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003311 case DRM_MODE_OBJECT_CRTC:
3312 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3313 break;
Rob Clark4d939142012-05-17 02:23:27 -06003314 case DRM_MODE_OBJECT_PLANE:
3315 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3316 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003317 }
3318
3319out:
Daniel Vetter84849902012-12-02 00:28:11 +01003320 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003321 return ret;
3322}
3323
Dave Airlief453ba02008-11-07 14:05:41 -08003324int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3325 struct drm_encoder *encoder)
3326{
3327 int i;
3328
3329 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3330 if (connector->encoder_ids[i] == 0) {
3331 connector->encoder_ids[i] = encoder->base.id;
3332 return 0;
3333 }
3334 }
3335 return -ENOMEM;
3336}
3337EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3338
3339void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3340 struct drm_encoder *encoder)
3341{
3342 int i;
3343 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3344 if (connector->encoder_ids[i] == encoder->base.id) {
3345 connector->encoder_ids[i] = 0;
3346 if (connector->encoder == encoder)
3347 connector->encoder = NULL;
3348 break;
3349 }
3350 }
3351}
3352EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3353
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003354int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003355 int gamma_size)
3356{
3357 crtc->gamma_size = gamma_size;
3358
3359 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3360 if (!crtc->gamma_store) {
3361 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003362 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003363 }
3364
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003365 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003366}
3367EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3368
3369int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3370 void *data, struct drm_file *file_priv)
3371{
3372 struct drm_mode_crtc_lut *crtc_lut = data;
3373 struct drm_mode_object *obj;
3374 struct drm_crtc *crtc;
3375 void *r_base, *g_base, *b_base;
3376 int size;
3377 int ret = 0;
3378
Dave Airliefb3b06c2011-02-08 13:55:21 +10003379 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3380 return -EINVAL;
3381
Daniel Vetter84849902012-12-02 00:28:11 +01003382 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003383 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3384 if (!obj) {
3385 ret = -EINVAL;
3386 goto out;
3387 }
3388 crtc = obj_to_crtc(obj);
3389
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003390 if (crtc->funcs->gamma_set == NULL) {
3391 ret = -ENOSYS;
3392 goto out;
3393 }
3394
Dave Airlief453ba02008-11-07 14:05:41 -08003395 /* 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_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3404 ret = -EFAULT;
3405 goto out;
3406 }
3407
3408 g_base = r_base + size;
3409 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3410 ret = -EFAULT;
3411 goto out;
3412 }
3413
3414 b_base = g_base + size;
3415 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3416 ret = -EFAULT;
3417 goto out;
3418 }
3419
James Simmons72034252010-08-03 01:33:19 +01003420 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003421
3422out:
Daniel Vetter84849902012-12-02 00:28:11 +01003423 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003424 return ret;
3425
3426}
3427
3428int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3429 void *data, struct drm_file *file_priv)
3430{
3431 struct drm_mode_crtc_lut *crtc_lut = data;
3432 struct drm_mode_object *obj;
3433 struct drm_crtc *crtc;
3434 void *r_base, *g_base, *b_base;
3435 int size;
3436 int ret = 0;
3437
Dave Airliefb3b06c2011-02-08 13:55:21 +10003438 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3439 return -EINVAL;
3440
Daniel Vetter84849902012-12-02 00:28:11 +01003441 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003442 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3443 if (!obj) {
3444 ret = -EINVAL;
3445 goto out;
3446 }
3447 crtc = obj_to_crtc(obj);
3448
3449 /* memcpy into gamma store */
3450 if (crtc_lut->gamma_size != crtc->gamma_size) {
3451 ret = -EINVAL;
3452 goto out;
3453 }
3454
3455 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3456 r_base = crtc->gamma_store;
3457 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3458 ret = -EFAULT;
3459 goto out;
3460 }
3461
3462 g_base = r_base + size;
3463 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3464 ret = -EFAULT;
3465 goto out;
3466 }
3467
3468 b_base = g_base + size;
3469 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3470 ret = -EFAULT;
3471 goto out;
3472 }
3473out:
Daniel Vetter84849902012-12-02 00:28:11 +01003474 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003475 return ret;
3476}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003477
3478int drm_mode_page_flip_ioctl(struct drm_device *dev,
3479 void *data, struct drm_file *file_priv)
3480{
3481 struct drm_mode_crtc_page_flip *page_flip = data;
3482 struct drm_mode_object *obj;
3483 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003484 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003485 struct drm_pending_vblank_event *e = NULL;
3486 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003487 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003488 int ret = -EINVAL;
3489
3490 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3491 page_flip->reserved != 0)
3492 return -EINVAL;
3493
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003494 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3495 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003496 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003497 crtc = obj_to_crtc(obj);
3498
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003499 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003500 if (crtc->fb == NULL) {
3501 /* The framebuffer is currently unbound, presumably
3502 * due to a hotplug event, that userspace has not
3503 * yet discovered.
3504 */
3505 ret = -EBUSY;
3506 goto out;
3507 }
3508
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003509 if (crtc->funcs->page_flip == NULL)
3510 goto out;
3511
Daniel Vetter786b99e2012-12-02 21:53:40 +01003512 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3513 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003514 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003515
Rob Clark7c80e122012-09-04 16:35:56 +00003516 hdisplay = crtc->mode.hdisplay;
3517 vdisplay = crtc->mode.vdisplay;
3518
3519 if (crtc->invert_dimensions)
3520 swap(hdisplay, vdisplay);
3521
3522 if (hdisplay > fb->width ||
3523 vdisplay > fb->height ||
3524 crtc->x > fb->width - hdisplay ||
3525 crtc->y > fb->height - vdisplay) {
3526 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3527 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3528 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003529 ret = -ENOSPC;
3530 goto out;
3531 }
3532
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003533 if (crtc->fb->pixel_format != fb->pixel_format) {
3534 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3535 ret = -EINVAL;
3536 goto out;
3537 }
3538
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003539 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3540 ret = -ENOMEM;
3541 spin_lock_irqsave(&dev->event_lock, flags);
3542 if (file_priv->event_space < sizeof e->event) {
3543 spin_unlock_irqrestore(&dev->event_lock, flags);
3544 goto out;
3545 }
3546 file_priv->event_space -= sizeof e->event;
3547 spin_unlock_irqrestore(&dev->event_lock, flags);
3548
3549 e = kzalloc(sizeof *e, GFP_KERNEL);
3550 if (e == NULL) {
3551 spin_lock_irqsave(&dev->event_lock, flags);
3552 file_priv->event_space += sizeof e->event;
3553 spin_unlock_irqrestore(&dev->event_lock, flags);
3554 goto out;
3555 }
3556
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003557 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003558 e->event.base.length = sizeof e->event;
3559 e->event.user_data = page_flip->user_data;
3560 e->base.event = &e->event.base;
3561 e->base.file_priv = file_priv;
3562 e->base.destroy =
3563 (void (*) (struct drm_pending_event *)) kfree;
3564 }
3565
Daniel Vetterb0d12322012-12-11 01:07:12 +01003566 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003567 ret = crtc->funcs->page_flip(crtc, fb, e);
3568 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003569 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3570 spin_lock_irqsave(&dev->event_lock, flags);
3571 file_priv->event_space += sizeof e->event;
3572 spin_unlock_irqrestore(&dev->event_lock, flags);
3573 kfree(e);
3574 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003575 /* Keep the old fb, don't unref it. */
3576 old_fb = NULL;
3577 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003578 /*
3579 * Warn if the driver hasn't properly updated the crtc->fb
3580 * field to reflect that the new framebuffer is now used.
3581 * Failing to do so will screw with the reference counting
3582 * on framebuffers.
3583 */
3584 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003585 /* Unref only the old framebuffer. */
3586 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003587 }
3588
3589out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003590 if (fb)
3591 drm_framebuffer_unreference(fb);
3592 if (old_fb)
3593 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003594 mutex_unlock(&crtc->mutex);
3595
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003596 return ret;
3597}
Chris Wilsoneb033552011-01-24 15:11:08 +00003598
3599void drm_mode_config_reset(struct drm_device *dev)
3600{
3601 struct drm_crtc *crtc;
3602 struct drm_encoder *encoder;
3603 struct drm_connector *connector;
3604
3605 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3606 if (crtc->funcs->reset)
3607 crtc->funcs->reset(crtc);
3608
3609 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3610 if (encoder->funcs->reset)
3611 encoder->funcs->reset(encoder);
3612
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003613 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3614 connector->status = connector_status_unknown;
3615
Chris Wilsoneb033552011-01-24 15:11:08 +00003616 if (connector->funcs->reset)
3617 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003618 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003619}
3620EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003621
3622int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3623 void *data, struct drm_file *file_priv)
3624{
3625 struct drm_mode_create_dumb *args = data;
3626
3627 if (!dev->driver->dumb_create)
3628 return -ENOSYS;
3629 return dev->driver->dumb_create(file_priv, dev, args);
3630}
3631
3632int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3633 void *data, struct drm_file *file_priv)
3634{
3635 struct drm_mode_map_dumb *args = data;
3636
3637 /* call driver ioctl to get mmap offset */
3638 if (!dev->driver->dumb_map_offset)
3639 return -ENOSYS;
3640
3641 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3642}
3643
3644int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3645 void *data, struct drm_file *file_priv)
3646{
3647 struct drm_mode_destroy_dumb *args = data;
3648
3649 if (!dev->driver->dumb_destroy)
3650 return -ENOSYS;
3651
3652 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3653}
Dave Airlie248dbc22011-11-29 20:02:54 +00003654
3655/*
3656 * Just need to support RGB formats here for compat with code that doesn't
3657 * use pixel formats directly yet.
3658 */
3659void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3660 int *bpp)
3661{
3662 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003663 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003664 case DRM_FORMAT_RGB332:
3665 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003666 *depth = 8;
3667 *bpp = 8;
3668 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003669 case DRM_FORMAT_XRGB1555:
3670 case DRM_FORMAT_XBGR1555:
3671 case DRM_FORMAT_RGBX5551:
3672 case DRM_FORMAT_BGRX5551:
3673 case DRM_FORMAT_ARGB1555:
3674 case DRM_FORMAT_ABGR1555:
3675 case DRM_FORMAT_RGBA5551:
3676 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003677 *depth = 15;
3678 *bpp = 16;
3679 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003680 case DRM_FORMAT_RGB565:
3681 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003682 *depth = 16;
3683 *bpp = 16;
3684 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003685 case DRM_FORMAT_RGB888:
3686 case DRM_FORMAT_BGR888:
3687 *depth = 24;
3688 *bpp = 24;
3689 break;
3690 case DRM_FORMAT_XRGB8888:
3691 case DRM_FORMAT_XBGR8888:
3692 case DRM_FORMAT_RGBX8888:
3693 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003694 *depth = 24;
3695 *bpp = 32;
3696 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003697 case DRM_FORMAT_XRGB2101010:
3698 case DRM_FORMAT_XBGR2101010:
3699 case DRM_FORMAT_RGBX1010102:
3700 case DRM_FORMAT_BGRX1010102:
3701 case DRM_FORMAT_ARGB2101010:
3702 case DRM_FORMAT_ABGR2101010:
3703 case DRM_FORMAT_RGBA1010102:
3704 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003705 *depth = 30;
3706 *bpp = 32;
3707 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003708 case DRM_FORMAT_ARGB8888:
3709 case DRM_FORMAT_ABGR8888:
3710 case DRM_FORMAT_RGBA8888:
3711 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003712 *depth = 32;
3713 *bpp = 32;
3714 break;
3715 default:
3716 DRM_DEBUG_KMS("unsupported pixel format\n");
3717 *depth = 0;
3718 *bpp = 0;
3719 break;
3720 }
3721}
3722EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003723
3724/**
3725 * drm_format_num_planes - get the number of planes for format
3726 * @format: pixel format (DRM_FORMAT_*)
3727 *
3728 * RETURNS:
3729 * The number of planes used by the specified pixel format.
3730 */
3731int drm_format_num_planes(uint32_t format)
3732{
3733 switch (format) {
3734 case DRM_FORMAT_YUV410:
3735 case DRM_FORMAT_YVU410:
3736 case DRM_FORMAT_YUV411:
3737 case DRM_FORMAT_YVU411:
3738 case DRM_FORMAT_YUV420:
3739 case DRM_FORMAT_YVU420:
3740 case DRM_FORMAT_YUV422:
3741 case DRM_FORMAT_YVU422:
3742 case DRM_FORMAT_YUV444:
3743 case DRM_FORMAT_YVU444:
3744 return 3;
3745 case DRM_FORMAT_NV12:
3746 case DRM_FORMAT_NV21:
3747 case DRM_FORMAT_NV16:
3748 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003749 case DRM_FORMAT_NV24:
3750 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003751 return 2;
3752 default:
3753 return 1;
3754 }
3755}
3756EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003757
3758/**
3759 * drm_format_plane_cpp - determine the bytes per pixel value
3760 * @format: pixel format (DRM_FORMAT_*)
3761 * @plane: plane index
3762 *
3763 * RETURNS:
3764 * The bytes per pixel value for the specified plane.
3765 */
3766int drm_format_plane_cpp(uint32_t format, int plane)
3767{
3768 unsigned int depth;
3769 int bpp;
3770
3771 if (plane >= drm_format_num_planes(format))
3772 return 0;
3773
3774 switch (format) {
3775 case DRM_FORMAT_YUYV:
3776 case DRM_FORMAT_YVYU:
3777 case DRM_FORMAT_UYVY:
3778 case DRM_FORMAT_VYUY:
3779 return 2;
3780 case DRM_FORMAT_NV12:
3781 case DRM_FORMAT_NV21:
3782 case DRM_FORMAT_NV16:
3783 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003784 case DRM_FORMAT_NV24:
3785 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003786 return plane ? 2 : 1;
3787 case DRM_FORMAT_YUV410:
3788 case DRM_FORMAT_YVU410:
3789 case DRM_FORMAT_YUV411:
3790 case DRM_FORMAT_YVU411:
3791 case DRM_FORMAT_YUV420:
3792 case DRM_FORMAT_YVU420:
3793 case DRM_FORMAT_YUV422:
3794 case DRM_FORMAT_YVU422:
3795 case DRM_FORMAT_YUV444:
3796 case DRM_FORMAT_YVU444:
3797 return 1;
3798 default:
3799 drm_fb_get_bpp_depth(format, &depth, &bpp);
3800 return bpp >> 3;
3801 }
3802}
3803EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003804
3805/**
3806 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3807 * @format: pixel format (DRM_FORMAT_*)
3808 *
3809 * RETURNS:
3810 * The horizontal chroma subsampling factor for the
3811 * specified pixel format.
3812 */
3813int drm_format_horz_chroma_subsampling(uint32_t format)
3814{
3815 switch (format) {
3816 case DRM_FORMAT_YUV411:
3817 case DRM_FORMAT_YVU411:
3818 case DRM_FORMAT_YUV410:
3819 case DRM_FORMAT_YVU410:
3820 return 4;
3821 case DRM_FORMAT_YUYV:
3822 case DRM_FORMAT_YVYU:
3823 case DRM_FORMAT_UYVY:
3824 case DRM_FORMAT_VYUY:
3825 case DRM_FORMAT_NV12:
3826 case DRM_FORMAT_NV21:
3827 case DRM_FORMAT_NV16:
3828 case DRM_FORMAT_NV61:
3829 case DRM_FORMAT_YUV422:
3830 case DRM_FORMAT_YVU422:
3831 case DRM_FORMAT_YUV420:
3832 case DRM_FORMAT_YVU420:
3833 return 2;
3834 default:
3835 return 1;
3836 }
3837}
3838EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3839
3840/**
3841 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3842 * @format: pixel format (DRM_FORMAT_*)
3843 *
3844 * RETURNS:
3845 * The vertical chroma subsampling factor for the
3846 * specified pixel format.
3847 */
3848int drm_format_vert_chroma_subsampling(uint32_t format)
3849{
3850 switch (format) {
3851 case DRM_FORMAT_YUV410:
3852 case DRM_FORMAT_YVU410:
3853 return 4;
3854 case DRM_FORMAT_YUV420:
3855 case DRM_FORMAT_YVU420:
3856 case DRM_FORMAT_NV12:
3857 case DRM_FORMAT_NV21:
3858 return 2;
3859 default:
3860 return 1;
3861 }
3862}
3863EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003864
3865/**
3866 * drm_mode_config_init - initialize DRM mode_configuration structure
3867 * @dev: DRM device
3868 *
3869 * Initialize @dev's mode_config structure, used for tracking the graphics
3870 * configuration of @dev.
3871 *
3872 * Since this initializes the modeset locks, no locking is possible. Which is no
3873 * problem, since this should happen single threaded at init time. It is the
3874 * driver's problem to ensure this guarantee.
3875 *
3876 */
3877void drm_mode_config_init(struct drm_device *dev)
3878{
3879 mutex_init(&dev->mode_config.mutex);
3880 mutex_init(&dev->mode_config.idr_mutex);
3881 mutex_init(&dev->mode_config.fb_lock);
3882 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3883 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3884 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3885 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3886 INIT_LIST_HEAD(&dev->mode_config.property_list);
3887 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3888 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3889 idr_init(&dev->mode_config.crtc_idr);
3890
3891 drm_modeset_lock_all(dev);
3892 drm_mode_create_standard_connector_properties(dev);
3893 drm_modeset_unlock_all(dev);
3894
3895 /* Just to be sure */
3896 dev->mode_config.num_fb = 0;
3897 dev->mode_config.num_connector = 0;
3898 dev->mode_config.num_crtc = 0;
3899 dev->mode_config.num_encoder = 0;
3900}
3901EXPORT_SYMBOL(drm_mode_config_init);
3902
3903/**
3904 * drm_mode_config_cleanup - free up DRM mode_config info
3905 * @dev: DRM device
3906 *
3907 * Free up all the connectors and CRTCs associated with this DRM device, then
3908 * free up the framebuffers and associated buffer objects.
3909 *
3910 * Note that since this /should/ happen single-threaded at driver/device
3911 * teardown time, no locking is required. It's the driver's job to ensure that
3912 * this guarantee actually holds true.
3913 *
3914 * FIXME: cleanup any dangling user buffer objects too
3915 */
3916void drm_mode_config_cleanup(struct drm_device *dev)
3917{
3918 struct drm_connector *connector, *ot;
3919 struct drm_crtc *crtc, *ct;
3920 struct drm_encoder *encoder, *enct;
3921 struct drm_framebuffer *fb, *fbt;
3922 struct drm_property *property, *pt;
3923 struct drm_property_blob *blob, *bt;
3924 struct drm_plane *plane, *plt;
3925
3926 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3927 head) {
3928 encoder->funcs->destroy(encoder);
3929 }
3930
3931 list_for_each_entry_safe(connector, ot,
3932 &dev->mode_config.connector_list, head) {
3933 connector->funcs->destroy(connector);
3934 }
3935
3936 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3937 head) {
3938 drm_property_destroy(dev, property);
3939 }
3940
3941 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3942 head) {
3943 drm_property_destroy_blob(dev, blob);
3944 }
3945
3946 /*
3947 * Single-threaded teardown context, so it's not required to grab the
3948 * fb_lock to protect against concurrent fb_list access. Contrary, it
3949 * would actually deadlock with the drm_framebuffer_cleanup function.
3950 *
3951 * Also, if there are any framebuffers left, that's a driver leak now,
3952 * so politely WARN about this.
3953 */
3954 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3955 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3956 drm_framebuffer_remove(fb);
3957 }
3958
3959 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3960 head) {
3961 plane->funcs->destroy(plane);
3962 }
3963
3964 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3965 crtc->funcs->destroy(crtc);
3966 }
3967
3968 idr_destroy(&dev->mode_config.crtc_idr);
3969}
3970EXPORT_SYMBOL(drm_mode_config_cleanup);