blob: a6917645fb4a7bbf4dbeafb2a606fbaa4be58ad8 [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;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400189 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800190};
191
192/*
193 * Connector and encoder types.
194 */
195static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400196{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
197 { DRM_MODE_CONNECTOR_VGA, "VGA" },
198 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
199 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
200 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
201 { DRM_MODE_CONNECTOR_Composite, "Composite" },
202 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
203 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
204 { DRM_MODE_CONNECTOR_Component, "Component" },
205 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
206 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
207 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
208 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
209 { DRM_MODE_CONNECTOR_TV, "TV" },
210 { DRM_MODE_CONNECTOR_eDP, "eDP" },
211 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
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
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400223void drm_connector_ida_init(void)
224{
225 int i;
226
227 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
228 ida_init(&drm_connector_enum_list[i].ida);
229}
230
231void drm_connector_ida_destroy(void)
232{
233 int i;
234
235 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
236 ida_destroy(&drm_connector_enum_list[i].ida);
237}
238
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000239const char *drm_get_encoder_name(const struct drm_encoder *encoder)
Dave Airlief453ba02008-11-07 14:05:41 -0800240{
241 static char buf[32];
242
243 snprintf(buf, 32, "%s-%d",
244 drm_encoder_enum_list[encoder->encoder_type].name,
245 encoder->base.id);
246 return buf;
247}
Dave Airlie13a81952009-09-07 15:45:33 +1000248EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800249
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000250const char *drm_get_connector_name(const struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -0800251{
252 static char buf[32];
253
254 snprintf(buf, 32, "%s-%d",
255 drm_connector_enum_list[connector->connector_type].name,
256 connector->connector_type_id);
257 return buf;
258}
259EXPORT_SYMBOL(drm_get_connector_name);
260
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000261const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800262{
263 if (status == connector_status_connected)
264 return "connected";
265 else if (status == connector_status_disconnected)
266 return "disconnected";
267 else
268 return "unknown";
269}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000270EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800271
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300272static char printable_char(int c)
273{
274 return isascii(c) && isprint(c) ? c : '?';
275}
276
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000277const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300278{
279 static char buf[32];
280
281 snprintf(buf, sizeof(buf),
282 "%c%c%c%c %s-endian (0x%08x)",
283 printable_char(format & 0xff),
284 printable_char((format >> 8) & 0xff),
285 printable_char((format >> 16) & 0xff),
286 printable_char((format >> 24) & 0x7f),
287 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
288 format);
289
290 return buf;
291}
292EXPORT_SYMBOL(drm_get_format_name);
293
Dave Airlief453ba02008-11-07 14:05:41 -0800294/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100295 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800296 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100297 * @obj: object pointer, used to generate unique ID
298 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800299 *
Dave Airlief453ba02008-11-07 14:05:41 -0800300 * Create a unique identifier based on @ptr in @dev's identifier space. Used
301 * for tracking modes, CRTCs and connectors.
302 *
303 * RETURNS:
304 * New unique (relative to other objects in @dev) integer identifier for the
305 * object.
306 */
307static int drm_mode_object_get(struct drm_device *dev,
308 struct drm_mode_object *obj, uint32_t obj_type)
309{
Dave Airlief453ba02008-11-07 14:05:41 -0800310 int ret;
311
Jesse Barnesad2563c2009-01-19 17:21:45 +1000312 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800313 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
314 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100315 /*
316 * Set up the object linking under the protection of the idr
317 * lock so that other users can't see inconsistent state.
318 */
Tejun Heo2e928812013-02-27 17:04:08 -0800319 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100320 obj->type = obj_type;
321 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000322 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100323
Tejun Heo2e928812013-02-27 17:04:08 -0800324 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800325}
326
327/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100328 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800329 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100330 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800331 *
Dave Airlief453ba02008-11-07 14:05:41 -0800332 * Free @id from @dev's unique identifier pool.
333 */
334static void drm_mode_object_put(struct drm_device *dev,
335 struct drm_mode_object *object)
336{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000337 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800338 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000339 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800340}
341
Daniel Vetter786b99e2012-12-02 21:53:40 +0100342/**
343 * drm_mode_object_find - look up a drm object with static lifetime
344 * @dev: drm device
345 * @id: id of the mode object
346 * @type: type of the mode object
347 *
348 * Note that framebuffers cannot be looked up with this functions - since those
349 * are reference counted, they need special treatment.
350 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200351struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
352 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800353{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000354 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800355
Daniel Vetter786b99e2012-12-02 21:53:40 +0100356 /* Framebuffers are reference counted and need their own lookup
357 * function.*/
358 WARN_ON(type == DRM_MODE_OBJECT_FB);
359
Jesse Barnesad2563c2009-01-19 17:21:45 +1000360 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800361 obj = idr_find(&dev->mode_config.crtc_idr, id);
362 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000363 obj = NULL;
364 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800365
366 return obj;
367}
368EXPORT_SYMBOL(drm_mode_object_find);
369
370/**
Dave Airlief453ba02008-11-07 14:05:41 -0800371 * drm_framebuffer_init - initialize a framebuffer
372 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100373 * @fb: framebuffer to be initialized
374 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800375 *
Dave Airlief453ba02008-11-07 14:05:41 -0800376 * Allocates an ID for the framebuffer's parent mode object, sets its mode
377 * functions & device file and adds it to the master fd list.
378 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100379 * IMPORTANT:
380 * This functions publishes the fb and makes it available for concurrent access
381 * by other users. Which means by this point the fb _must_ be fully set up -
382 * since all the fb attributes are invariant over its lifetime, no further
383 * locking but only correct reference counting is required.
384 *
Dave Airlief453ba02008-11-07 14:05:41 -0800385 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200386 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800387 */
388int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
389 const struct drm_framebuffer_funcs *funcs)
390{
391 int ret;
392
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100393 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000394 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100395 INIT_LIST_HEAD(&fb->filp_head);
396 fb->dev = dev;
397 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000398
Dave Airlief453ba02008-11-07 14:05:41 -0800399 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200400 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100401 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800402
Daniel Vetter2b677e82012-12-10 21:16:05 +0100403 /* Grab the idr reference. */
404 drm_framebuffer_reference(fb);
405
Dave Airlief453ba02008-11-07 14:05:41 -0800406 dev->mode_config.num_fb++;
407 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100408out:
409 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800410
411 return 0;
412}
413EXPORT_SYMBOL(drm_framebuffer_init);
414
Rob Clarkf7eff602012-09-05 21:48:38 +0000415static void drm_framebuffer_free(struct kref *kref)
416{
417 struct drm_framebuffer *fb =
418 container_of(kref, struct drm_framebuffer, refcount);
419 fb->funcs->destroy(fb);
420}
421
Daniel Vetter2b677e82012-12-10 21:16:05 +0100422static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
423 uint32_t id)
424{
425 struct drm_mode_object *obj = NULL;
426 struct drm_framebuffer *fb;
427
428 mutex_lock(&dev->mode_config.idr_mutex);
429 obj = idr_find(&dev->mode_config.crtc_idr, id);
430 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
431 fb = NULL;
432 else
433 fb = obj_to_fb(obj);
434 mutex_unlock(&dev->mode_config.idr_mutex);
435
436 return fb;
437}
438
Rob Clarkf7eff602012-09-05 21:48:38 +0000439/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100440 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
441 * @dev: drm device
442 * @id: id of the fb object
443 *
444 * If successful, this grabs an additional reference to the framebuffer -
445 * callers need to make sure to eventually unreference the returned framebuffer
446 * again.
447 */
448struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
449 uint32_t id)
450{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100451 struct drm_framebuffer *fb;
452
453 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100454 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100455 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000456 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100457 mutex_unlock(&dev->mode_config.fb_lock);
458
459 return fb;
460}
461EXPORT_SYMBOL(drm_framebuffer_lookup);
462
463/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000464 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100465 * @fb: framebuffer to unref
466 *
467 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000468 */
469void drm_framebuffer_unreference(struct drm_framebuffer *fb)
470{
Rob Clarkf7eff602012-09-05 21:48:38 +0000471 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000472 kref_put(&fb->refcount, drm_framebuffer_free);
473}
474EXPORT_SYMBOL(drm_framebuffer_unreference);
475
476/**
477 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100478 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000479 */
480void drm_framebuffer_reference(struct drm_framebuffer *fb)
481{
482 DRM_DEBUG("FB ID: %d\n", fb->base.id);
483 kref_get(&fb->refcount);
484}
485EXPORT_SYMBOL(drm_framebuffer_reference);
486
Daniel Vetter2b677e82012-12-10 21:16:05 +0100487static void drm_framebuffer_free_bug(struct kref *kref)
488{
489 BUG();
490}
491
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100492static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
493{
494 DRM_DEBUG("FB ID: %d\n", fb->base.id);
495 kref_put(&fb->refcount, drm_framebuffer_free_bug);
496}
497
Daniel Vetter2b677e82012-12-10 21:16:05 +0100498/* dev->mode_config.fb_lock must be held! */
499static void __drm_framebuffer_unregister(struct drm_device *dev,
500 struct drm_framebuffer *fb)
501{
502 mutex_lock(&dev->mode_config.idr_mutex);
503 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
504 mutex_unlock(&dev->mode_config.idr_mutex);
505
506 fb->base.id = 0;
507
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100508 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100509}
510
Dave Airlief453ba02008-11-07 14:05:41 -0800511/**
Daniel Vetter36206362012-12-10 20:42:17 +0100512 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
513 * @fb: fb to unregister
514 *
515 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
516 * those used for fbdev. Note that the caller must hold a reference of it's own,
517 * i.e. the object may not be destroyed through this call (since it'll lead to a
518 * locking inversion).
519 */
520void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
521{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100522 struct drm_device *dev = fb->dev;
523
524 mutex_lock(&dev->mode_config.fb_lock);
525 /* Mark fb as reaped and drop idr ref. */
526 __drm_framebuffer_unregister(dev, fb);
527 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100528}
529EXPORT_SYMBOL(drm_framebuffer_unregister_private);
530
531/**
Dave Airlief453ba02008-11-07 14:05:41 -0800532 * drm_framebuffer_cleanup - remove a framebuffer object
533 * @fb: framebuffer to remove
534 *
Daniel Vetter36206362012-12-10 20:42:17 +0100535 * Cleanup references to a user-created framebuffer. This function is intended
536 * to be used from the drivers ->destroy callback.
537 *
538 * Note that this function does not remove the fb from active usuage - if it is
539 * still used anywhere, hilarity can ensue since userspace could call getfb on
540 * the id and get back -EINVAL. Obviously no concern at driver unload time.
541 *
542 * Also, the framebuffer will not be removed from the lookup idr - for
543 * user-created framebuffers this will happen in in the rmfb ioctl. For
544 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
545 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800546 */
547void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
548{
549 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100550
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100551 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000552 list_del(&fb->head);
553 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100554 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000555}
556EXPORT_SYMBOL(drm_framebuffer_cleanup);
557
558/**
559 * drm_framebuffer_remove - remove and unreference a framebuffer object
560 * @fb: framebuffer to remove
561 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000562 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100563 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100564 * passed-in framebuffer. Might take the modeset locks.
565 *
566 * Note that this function optimizes the cleanup away if the caller holds the
567 * last reference to the framebuffer. It is also guaranteed to not take the
568 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000569 */
570void drm_framebuffer_remove(struct drm_framebuffer *fb)
571{
572 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800573 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800574 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000575 struct drm_mode_set set;
576 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800577
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100578 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100579
Daniel Vetterb62584e2012-12-11 16:51:35 +0100580 /*
581 * drm ABI mandates that we remove any deleted framebuffers from active
582 * useage. But since most sane clients only remove framebuffers they no
583 * longer need, try to optimize this away.
584 *
585 * Since we're holding a reference ourselves, observing a refcount of 1
586 * means that we're the last holder and can skip it. Also, the refcount
587 * can never increase from 1 again, so we don't need any barriers or
588 * locks.
589 *
590 * Note that userspace could try to race with use and instate a new
591 * usage _after_ we've cleared all current ones. End result will be an
592 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
593 * in this manner.
594 */
595 if (atomic_read(&fb->refcount.refcount) > 1) {
596 drm_modeset_lock_all(dev);
597 /* remove from any CRTC */
598 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
599 if (crtc->fb == fb) {
600 /* should turn off the crtc */
601 memset(&set, 0, sizeof(struct drm_mode_set));
602 set.crtc = crtc;
603 set.fb = NULL;
604 ret = drm_mode_set_config_internal(&set);
605 if (ret)
606 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
607 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000608 }
Dave Airlief453ba02008-11-07 14:05:41 -0800609
Daniel Vetterb62584e2012-12-11 16:51:35 +0100610 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300611 if (plane->fb == fb)
612 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800613 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100614 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800615 }
616
Rob Clarkf7eff602012-09-05 21:48:38 +0000617 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800618}
Rob Clarkf7eff602012-09-05 21:48:38 +0000619EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800620
621/**
622 * drm_crtc_init - Initialise a new CRTC object
623 * @dev: DRM device
624 * @crtc: CRTC object to init
625 * @funcs: callbacks for the new CRTC
626 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000627 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200628 *
629 * RETURNS:
630 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800631 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200632int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800633 const struct drm_crtc_funcs *funcs)
634{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200635 int ret;
636
Dave Airlief453ba02008-11-07 14:05:41 -0800637 crtc->dev = dev;
638 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000639 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800640
Daniel Vetter84849902012-12-02 00:28:11 +0100641 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100642 mutex_init(&crtc->mutex);
643 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200644
645 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
646 if (ret)
647 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800648
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300649 crtc->base.properties = &crtc->properties;
650
Dave Airlief453ba02008-11-07 14:05:41 -0800651 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
652 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200653
654 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100655 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200656
657 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800658}
659EXPORT_SYMBOL(drm_crtc_init);
660
661/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000662 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800663 * @crtc: CRTC to cleanup
664 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000665 * This function cleans up @crtc and removes it from the DRM mode setting
666 * core. Note that the function does *not* free the crtc structure itself,
667 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800668 */
669void drm_crtc_cleanup(struct drm_crtc *crtc)
670{
671 struct drm_device *dev = crtc->dev;
672
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000673 kfree(crtc->gamma_store);
674 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800675
676 drm_mode_object_put(dev, &crtc->base);
677 list_del(&crtc->head);
678 dev->mode_config.num_crtc--;
679}
680EXPORT_SYMBOL(drm_crtc_cleanup);
681
682/**
683 * drm_mode_probed_add - add a mode to a connector's probed mode list
684 * @connector: connector the new mode
685 * @mode: mode data
686 *
Dave Airlief453ba02008-11-07 14:05:41 -0800687 * Add @mode to @connector's mode list for later use.
688 */
689void drm_mode_probed_add(struct drm_connector *connector,
690 struct drm_display_mode *mode)
691{
Ville Syrjälä990256a2013-05-31 12:17:07 +0000692 list_add_tail(&mode->head, &connector->probed_modes);
Dave Airlief453ba02008-11-07 14:05:41 -0800693}
694EXPORT_SYMBOL(drm_mode_probed_add);
695
696/**
697 * drm_mode_remove - remove and free a mode
698 * @connector: connector list to modify
699 * @mode: mode to remove
700 *
Dave Airlief453ba02008-11-07 14:05:41 -0800701 * Remove @mode from @connector's mode list, then free it.
702 */
703void drm_mode_remove(struct drm_connector *connector,
704 struct drm_display_mode *mode)
705{
706 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100707 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800708}
709EXPORT_SYMBOL(drm_mode_remove);
710
711/**
712 * drm_connector_init - Init a preallocated connector
713 * @dev: DRM device
714 * @connector: the connector to init
715 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100716 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800717 *
Dave Airlief453ba02008-11-07 14:05:41 -0800718 * Initialises a preallocated connector. Connectors should be
719 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200720 *
721 * RETURNS:
722 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800723 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200724int drm_connector_init(struct drm_device *dev,
725 struct drm_connector *connector,
726 const struct drm_connector_funcs *funcs,
727 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800728{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200729 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400730 struct ida *connector_ida =
731 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200732
Daniel Vetter84849902012-12-02 00:28:11 +0100733 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800734
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200735 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
736 if (ret)
737 goto out;
738
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300739 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800740 connector->dev = dev;
741 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800742 connector->connector_type = connector_type;
743 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400744 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
745 if (connector->connector_type_id < 0) {
746 ret = connector->connector_type_id;
747 drm_mode_object_put(dev, &connector->base);
748 goto out;
749 }
Dave Airlief453ba02008-11-07 14:05:41 -0800750 INIT_LIST_HEAD(&connector->probed_modes);
751 INIT_LIST_HEAD(&connector->modes);
752 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000753 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800754
755 list_add_tail(&connector->head, &dev->mode_config.connector_list);
756 dev->mode_config.num_connector++;
757
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200758 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500759 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200760 dev->mode_config.edid_property,
761 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800762
Rob Clark58495562012-10-11 20:50:56 -0500763 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800764 dev->mode_config.dpms_property, 0);
765
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200766 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100767 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200768
769 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800770}
771EXPORT_SYMBOL(drm_connector_init);
772
773/**
774 * drm_connector_cleanup - cleans up an initialised connector
775 * @connector: connector to cleanup
776 *
Dave Airlief453ba02008-11-07 14:05:41 -0800777 * Cleans up the connector but doesn't free the object.
778 */
779void drm_connector_cleanup(struct drm_connector *connector)
780{
781 struct drm_device *dev = connector->dev;
782 struct drm_display_mode *mode, *t;
783
784 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
785 drm_mode_remove(connector, mode);
786
787 list_for_each_entry_safe(mode, t, &connector->modes, head)
788 drm_mode_remove(connector, mode);
789
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400790 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
791 connector->connector_type_id);
792
Dave Airlief453ba02008-11-07 14:05:41 -0800793 drm_mode_object_put(dev, &connector->base);
794 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000795 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800796}
797EXPORT_SYMBOL(drm_connector_cleanup);
798
Dave Airliecbc7e222012-02-20 14:16:40 +0000799void drm_connector_unplug_all(struct drm_device *dev)
800{
801 struct drm_connector *connector;
802
803 /* taking the mode config mutex ends up in a clash with sysfs */
804 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
805 drm_sysfs_connector_remove(connector);
806
807}
808EXPORT_SYMBOL(drm_connector_unplug_all);
809
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200810int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000811 struct drm_encoder *encoder,
812 const struct drm_encoder_funcs *funcs,
813 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800814{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200815 int ret;
816
Daniel Vetter84849902012-12-02 00:28:11 +0100817 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800818
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200819 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
820 if (ret)
821 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800822
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200823 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800824 encoder->encoder_type = encoder_type;
825 encoder->funcs = funcs;
826
827 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
828 dev->mode_config.num_encoder++;
829
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200830 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100831 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200832
833 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800834}
835EXPORT_SYMBOL(drm_encoder_init);
836
837void drm_encoder_cleanup(struct drm_encoder *encoder)
838{
839 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100840 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800841 drm_mode_object_put(dev, &encoder->base);
842 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000843 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100844 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800845}
846EXPORT_SYMBOL(drm_encoder_cleanup);
847
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300848/**
849 * drm_plane_init - Initialise a new plane object
850 * @dev: DRM device
851 * @plane: plane object to init
852 * @possible_crtcs: bitmask of possible CRTCs
853 * @funcs: callbacks for the new plane
854 * @formats: array of supported formats (%DRM_FORMAT_*)
855 * @format_count: number of elements in @formats
856 * @priv: plane is private (hidden from userspace)?
857 *
858 * Inits a new object created as base part of a driver plane object.
859 *
860 * RETURNS:
861 * Zero on success, error code on failure.
862 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800863int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
864 unsigned long possible_crtcs,
865 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600866 const uint32_t *formats, uint32_t format_count,
867 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800868{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200869 int ret;
870
Daniel Vetter84849902012-12-02 00:28:11 +0100871 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800872
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200873 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
874 if (ret)
875 goto out;
876
Rob Clark4d939142012-05-17 02:23:27 -0600877 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800878 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800879 plane->funcs = funcs;
880 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
881 GFP_KERNEL);
882 if (!plane->format_types) {
883 DRM_DEBUG_KMS("out of memory when allocating plane\n");
884 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200885 ret = -ENOMEM;
886 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800887 }
888
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800889 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800890 plane->format_count = format_count;
891 plane->possible_crtcs = possible_crtcs;
892
Rob Clark0a7eb242011-12-13 20:19:36 -0600893 /* private planes are not exposed to userspace, but depending on
894 * display hardware, might be convenient to allow sharing programming
895 * for the scanout engine with the crtc implementation.
896 */
897 if (!priv) {
898 list_add_tail(&plane->head, &dev->mode_config.plane_list);
899 dev->mode_config.num_plane++;
900 } else {
901 INIT_LIST_HEAD(&plane->head);
902 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800903
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200904 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100905 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800906
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200907 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800908}
909EXPORT_SYMBOL(drm_plane_init);
910
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300911/**
912 * drm_plane_cleanup - Clean up the core plane usage
913 * @plane: plane to cleanup
914 *
915 * This function cleans up @plane and removes it from the DRM mode setting
916 * core. Note that the function does *not* free the plane structure itself,
917 * this is the responsibility of the caller.
918 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800919void drm_plane_cleanup(struct drm_plane *plane)
920{
921 struct drm_device *dev = plane->dev;
922
Daniel Vetter84849902012-12-02 00:28:11 +0100923 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800924 kfree(plane->format_types);
925 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600926 /* if not added to a list, it must be a private plane */
927 if (!list_empty(&plane->head)) {
928 list_del(&plane->head);
929 dev->mode_config.num_plane--;
930 }
Daniel Vetter84849902012-12-02 00:28:11 +0100931 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800932}
933EXPORT_SYMBOL(drm_plane_cleanup);
934
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +0300935/**
936 * drm_plane_force_disable - Forcibly disable a plane
937 * @plane: plane to disable
938 *
939 * Forces the plane to be disabled.
940 *
941 * Used when the plane's current framebuffer is destroyed,
942 * and when restoring fbdev mode.
943 */
Ville Syrjälä9125e612013-06-03 16:10:40 +0300944void drm_plane_force_disable(struct drm_plane *plane)
945{
946 int ret;
947
948 if (!plane->fb)
949 return;
950
951 ret = plane->funcs->disable_plane(plane);
952 if (ret)
953 DRM_ERROR("failed to disable plane with busy fb\n");
954 /* disconnect the plane from the fb and crtc: */
955 __drm_framebuffer_unreference(plane->fb);
956 plane->fb = NULL;
957 plane->crtc = NULL;
958}
959EXPORT_SYMBOL(drm_plane_force_disable);
960
Dave Airlief453ba02008-11-07 14:05:41 -0800961/**
962 * drm_mode_create - create a new display mode
963 * @dev: DRM device
964 *
Dave Airlief453ba02008-11-07 14:05:41 -0800965 * Create a new drm_display_mode, give it an ID, and return it.
966 *
967 * RETURNS:
968 * Pointer to new mode on success, NULL on error.
969 */
970struct drm_display_mode *drm_mode_create(struct drm_device *dev)
971{
972 struct drm_display_mode *nmode;
973
974 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
975 if (!nmode)
976 return NULL;
977
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200978 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
979 kfree(nmode);
980 return NULL;
981 }
982
Dave Airlief453ba02008-11-07 14:05:41 -0800983 return nmode;
984}
985EXPORT_SYMBOL(drm_mode_create);
986
987/**
988 * drm_mode_destroy - remove a mode
989 * @dev: DRM device
990 * @mode: mode to remove
991 *
Dave Airlief453ba02008-11-07 14:05:41 -0800992 * Free @mode's unique identifier, then free it.
993 */
994void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
995{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200996 if (!mode)
997 return;
998
Dave Airlief453ba02008-11-07 14:05:41 -0800999 drm_mode_object_put(dev, &mode->base);
1000
1001 kfree(mode);
1002}
1003EXPORT_SYMBOL(drm_mode_destroy);
1004
1005static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1006{
1007 struct drm_property *edid;
1008 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001009
1010 /*
1011 * Standard properties (apply to all connectors)
1012 */
1013 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1014 DRM_MODE_PROP_IMMUTABLE,
1015 "EDID", 0);
1016 dev->mode_config.edid_property = edid;
1017
Sascha Hauer4a67d392012-02-06 10:58:17 +01001018 dpms = drm_property_create_enum(dev, 0,
1019 "DPMS", drm_dpms_enum_list,
1020 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001021 dev->mode_config.dpms_property = dpms;
1022
1023 return 0;
1024}
1025
1026/**
1027 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1028 * @dev: DRM device
1029 *
1030 * Called by a driver the first time a DVI-I connector is made.
1031 */
1032int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1033{
1034 struct drm_property *dvi_i_selector;
1035 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001036
1037 if (dev->mode_config.dvi_i_select_subconnector_property)
1038 return 0;
1039
1040 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001041 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001042 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001043 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001044 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001045 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1046
Sascha Hauer4a67d392012-02-06 10:58:17 +01001047 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001048 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001049 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001050 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001051 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1052
1053 return 0;
1054}
1055EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1056
1057/**
1058 * drm_create_tv_properties - create TV specific connector properties
1059 * @dev: DRM device
1060 * @num_modes: number of different TV formats (modes) supported
1061 * @modes: array of pointers to strings containing name of each format
1062 *
1063 * Called by a driver's TV initialization routine, this function creates
1064 * the TV specific connector properties for a given device. Caller is
1065 * responsible for allocating a list of format names and passing them to
1066 * this routine.
1067 */
1068int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1069 char *modes[])
1070{
1071 struct drm_property *tv_selector;
1072 struct drm_property *tv_subconnector;
1073 int i;
1074
1075 if (dev->mode_config.tv_select_subconnector_property)
1076 return 0;
1077
1078 /*
1079 * Basic connector properties
1080 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001081 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001082 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001083 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001084 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001085 dev->mode_config.tv_select_subconnector_property = tv_selector;
1086
1087 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001088 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1089 "subconnector",
1090 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001091 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001092 dev->mode_config.tv_subconnector_property = tv_subconnector;
1093
1094 /*
1095 * Other, TV specific properties: margins & TV modes.
1096 */
1097 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001098 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001099
1100 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001101 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001102
1103 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001104 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001105
1106 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001107 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001108
1109 dev->mode_config.tv_mode_property =
1110 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1111 "mode", num_modes);
1112 for (i = 0; i < num_modes; i++)
1113 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1114 i, modes[i]);
1115
Francisco Jerezb6b79022009-08-02 04:19:20 +02001116 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001117 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001118
1119 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001120 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001121
1122 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001123 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001124
Francisco Jereza75f0232009-08-12 02:30:10 +02001125 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001126 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001127
1128 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001129 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001130
1131 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001132 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001133
Dave Airlief453ba02008-11-07 14:05:41 -08001134 return 0;
1135}
1136EXPORT_SYMBOL(drm_mode_create_tv_properties);
1137
1138/**
1139 * drm_mode_create_scaling_mode_property - create scaling mode property
1140 * @dev: DRM device
1141 *
1142 * Called by a driver the first time it's needed, must be attached to desired
1143 * connectors.
1144 */
1145int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1146{
1147 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001148
1149 if (dev->mode_config.scaling_mode_property)
1150 return 0;
1151
1152 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001153 drm_property_create_enum(dev, 0, "scaling mode",
1154 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001155 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001156
1157 dev->mode_config.scaling_mode_property = scaling_mode;
1158
1159 return 0;
1160}
1161EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1162
1163/**
1164 * drm_mode_create_dithering_property - create dithering property
1165 * @dev: DRM device
1166 *
1167 * Called by a driver the first time it's needed, must be attached to desired
1168 * connectors.
1169 */
1170int drm_mode_create_dithering_property(struct drm_device *dev)
1171{
1172 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001173
1174 if (dev->mode_config.dithering_mode_property)
1175 return 0;
1176
1177 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001178 drm_property_create_enum(dev, 0, "dithering",
1179 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001180 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001181 dev->mode_config.dithering_mode_property = dithering_mode;
1182
1183 return 0;
1184}
1185EXPORT_SYMBOL(drm_mode_create_dithering_property);
1186
1187/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001188 * drm_mode_create_dirty_property - create dirty property
1189 * @dev: DRM device
1190 *
1191 * Called by a driver the first time it's needed, must be attached to desired
1192 * connectors.
1193 */
1194int drm_mode_create_dirty_info_property(struct drm_device *dev)
1195{
1196 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001197
1198 if (dev->mode_config.dirty_info_property)
1199 return 0;
1200
1201 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001202 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001203 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001204 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001205 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001206 dev->mode_config.dirty_info_property = dirty_info;
1207
1208 return 0;
1209}
1210EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1211
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001212static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001213{
1214 uint32_t total_objects = 0;
1215
1216 total_objects += dev->mode_config.num_crtc;
1217 total_objects += dev->mode_config.num_connector;
1218 total_objects += dev->mode_config.num_encoder;
1219
Dave Airlief453ba02008-11-07 14:05:41 -08001220 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1221 if (!group->id_list)
1222 return -ENOMEM;
1223
1224 group->num_crtcs = 0;
1225 group->num_connectors = 0;
1226 group->num_encoders = 0;
1227 return 0;
1228}
1229
1230int drm_mode_group_init_legacy_group(struct drm_device *dev,
1231 struct drm_mode_group *group)
1232{
1233 struct drm_crtc *crtc;
1234 struct drm_encoder *encoder;
1235 struct drm_connector *connector;
1236 int ret;
1237
1238 if ((ret = drm_mode_group_init(dev, group)))
1239 return ret;
1240
1241 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1242 group->id_list[group->num_crtcs++] = crtc->base.id;
1243
1244 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1245 group->id_list[group->num_crtcs + group->num_encoders++] =
1246 encoder->base.id;
1247
1248 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1249 group->id_list[group->num_crtcs + group->num_encoders +
1250 group->num_connectors++] = connector->base.id;
1251
1252 return 0;
1253}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001254EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001255
1256/**
Dave Airlief453ba02008-11-07 14:05:41 -08001257 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1258 * @out: drm_mode_modeinfo struct to return to the user
1259 * @in: drm_display_mode to use
1260 *
Dave Airlief453ba02008-11-07 14:05:41 -08001261 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1262 * the user.
1263 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001264static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1265 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001266{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001267 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1268 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1269 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1270 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1271 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1272 "timing values too large for mode info\n");
1273
Dave Airlief453ba02008-11-07 14:05:41 -08001274 out->clock = in->clock;
1275 out->hdisplay = in->hdisplay;
1276 out->hsync_start = in->hsync_start;
1277 out->hsync_end = in->hsync_end;
1278 out->htotal = in->htotal;
1279 out->hskew = in->hskew;
1280 out->vdisplay = in->vdisplay;
1281 out->vsync_start = in->vsync_start;
1282 out->vsync_end = in->vsync_end;
1283 out->vtotal = in->vtotal;
1284 out->vscan = in->vscan;
1285 out->vrefresh = in->vrefresh;
1286 out->flags = in->flags;
1287 out->type = in->type;
1288 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1289 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1290}
1291
1292/**
1293 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1294 * @out: drm_display_mode to return to the user
1295 * @in: drm_mode_modeinfo to use
1296 *
Dave Airlief453ba02008-11-07 14:05:41 -08001297 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1298 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001299 *
1300 * RETURNS:
1301 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001302 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001303static int drm_crtc_convert_umode(struct drm_display_mode *out,
1304 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001305{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001306 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1307 return -ERANGE;
1308
Dave Airlief453ba02008-11-07 14:05:41 -08001309 out->clock = in->clock;
1310 out->hdisplay = in->hdisplay;
1311 out->hsync_start = in->hsync_start;
1312 out->hsync_end = in->hsync_end;
1313 out->htotal = in->htotal;
1314 out->hskew = in->hskew;
1315 out->vdisplay = in->vdisplay;
1316 out->vsync_start = in->vsync_start;
1317 out->vsync_end = in->vsync_end;
1318 out->vtotal = in->vtotal;
1319 out->vscan = in->vscan;
1320 out->vrefresh = in->vrefresh;
1321 out->flags = in->flags;
1322 out->type = in->type;
1323 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1324 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001325
1326 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001327}
1328
1329/**
1330 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001331 * @dev: drm device for the ioctl
1332 * @data: data pointer for the ioctl
1333 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001334 *
Dave Airlief453ba02008-11-07 14:05:41 -08001335 * Construct a set of configuration description structures and return
1336 * them to the user, including CRTC, connector and framebuffer configuration.
1337 *
1338 * Called by the user via ioctl.
1339 *
1340 * RETURNS:
1341 * Zero on success, errno on failure.
1342 */
1343int drm_mode_getresources(struct drm_device *dev, void *data,
1344 struct drm_file *file_priv)
1345{
1346 struct drm_mode_card_res *card_res = data;
1347 struct list_head *lh;
1348 struct drm_framebuffer *fb;
1349 struct drm_connector *connector;
1350 struct drm_crtc *crtc;
1351 struct drm_encoder *encoder;
1352 int ret = 0;
1353 int connector_count = 0;
1354 int crtc_count = 0;
1355 int fb_count = 0;
1356 int encoder_count = 0;
1357 int copied = 0, i;
1358 uint32_t __user *fb_id;
1359 uint32_t __user *crtc_id;
1360 uint32_t __user *connector_id;
1361 uint32_t __user *encoder_id;
1362 struct drm_mode_group *mode_group;
1363
Dave Airliefb3b06c2011-02-08 13:55:21 +10001364 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1365 return -EINVAL;
1366
Dave Airlief453ba02008-11-07 14:05:41 -08001367
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001368 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001369 /*
1370 * For the non-control nodes we need to limit the list of resources
1371 * by IDs in the group list for this node
1372 */
1373 list_for_each(lh, &file_priv->fbs)
1374 fb_count++;
1375
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001376 /* handle this in 4 parts */
1377 /* FBs */
1378 if (card_res->count_fbs >= fb_count) {
1379 copied = 0;
1380 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1381 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1382 if (put_user(fb->base.id, fb_id + copied)) {
1383 mutex_unlock(&file_priv->fbs_lock);
1384 return -EFAULT;
1385 }
1386 copied++;
1387 }
1388 }
1389 card_res->count_fbs = fb_count;
1390 mutex_unlock(&file_priv->fbs_lock);
1391
1392 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001393 mode_group = &file_priv->master->minor->mode_group;
1394 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1395
1396 list_for_each(lh, &dev->mode_config.crtc_list)
1397 crtc_count++;
1398
1399 list_for_each(lh, &dev->mode_config.connector_list)
1400 connector_count++;
1401
1402 list_for_each(lh, &dev->mode_config.encoder_list)
1403 encoder_count++;
1404 } else {
1405
1406 crtc_count = mode_group->num_crtcs;
1407 connector_count = mode_group->num_connectors;
1408 encoder_count = mode_group->num_encoders;
1409 }
1410
1411 card_res->max_height = dev->mode_config.max_height;
1412 card_res->min_height = dev->mode_config.min_height;
1413 card_res->max_width = dev->mode_config.max_width;
1414 card_res->min_width = dev->mode_config.min_width;
1415
Dave Airlief453ba02008-11-07 14:05:41 -08001416 /* CRTCs */
1417 if (card_res->count_crtcs >= crtc_count) {
1418 copied = 0;
1419 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1420 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1421 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1422 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001423 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001424 if (put_user(crtc->base.id, crtc_id + copied)) {
1425 ret = -EFAULT;
1426 goto out;
1427 }
1428 copied++;
1429 }
1430 } else {
1431 for (i = 0; i < mode_group->num_crtcs; i++) {
1432 if (put_user(mode_group->id_list[i],
1433 crtc_id + copied)) {
1434 ret = -EFAULT;
1435 goto out;
1436 }
1437 copied++;
1438 }
1439 }
1440 }
1441 card_res->count_crtcs = crtc_count;
1442
1443 /* Encoders */
1444 if (card_res->count_encoders >= encoder_count) {
1445 copied = 0;
1446 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1447 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1448 list_for_each_entry(encoder,
1449 &dev->mode_config.encoder_list,
1450 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001451 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1452 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001453 if (put_user(encoder->base.id, encoder_id +
1454 copied)) {
1455 ret = -EFAULT;
1456 goto out;
1457 }
1458 copied++;
1459 }
1460 } else {
1461 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1462 if (put_user(mode_group->id_list[i],
1463 encoder_id + copied)) {
1464 ret = -EFAULT;
1465 goto out;
1466 }
1467 copied++;
1468 }
1469
1470 }
1471 }
1472 card_res->count_encoders = encoder_count;
1473
1474 /* Connectors */
1475 if (card_res->count_connectors >= connector_count) {
1476 copied = 0;
1477 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1478 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1479 list_for_each_entry(connector,
1480 &dev->mode_config.connector_list,
1481 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001482 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1483 connector->base.id,
1484 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001485 if (put_user(connector->base.id,
1486 connector_id + copied)) {
1487 ret = -EFAULT;
1488 goto out;
1489 }
1490 copied++;
1491 }
1492 } else {
1493 int start = mode_group->num_crtcs +
1494 mode_group->num_encoders;
1495 for (i = start; i < start + mode_group->num_connectors; i++) {
1496 if (put_user(mode_group->id_list[i],
1497 connector_id + copied)) {
1498 ret = -EFAULT;
1499 goto out;
1500 }
1501 copied++;
1502 }
1503 }
1504 }
1505 card_res->count_connectors = connector_count;
1506
Jerome Glisse94401062010-07-15 15:43:25 -04001507 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001508 card_res->count_connectors, card_res->count_encoders);
1509
1510out:
Daniel Vetter84849902012-12-02 00:28:11 +01001511 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001512 return ret;
1513}
1514
1515/**
1516 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001517 * @dev: drm device for the ioctl
1518 * @data: data pointer for the ioctl
1519 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001520 *
Dave Airlief453ba02008-11-07 14:05:41 -08001521 * Construct a CRTC configuration structure to return to the user.
1522 *
1523 * Called by the user via ioctl.
1524 *
1525 * RETURNS:
1526 * Zero on success, errno on failure.
1527 */
1528int drm_mode_getcrtc(struct drm_device *dev,
1529 void *data, struct drm_file *file_priv)
1530{
1531 struct drm_mode_crtc *crtc_resp = data;
1532 struct drm_crtc *crtc;
1533 struct drm_mode_object *obj;
1534 int ret = 0;
1535
Dave Airliefb3b06c2011-02-08 13:55:21 +10001536 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1537 return -EINVAL;
1538
Daniel Vetter84849902012-12-02 00:28:11 +01001539 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001540
1541 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1542 DRM_MODE_OBJECT_CRTC);
1543 if (!obj) {
1544 ret = -EINVAL;
1545 goto out;
1546 }
1547 crtc = obj_to_crtc(obj);
1548
1549 crtc_resp->x = crtc->x;
1550 crtc_resp->y = crtc->y;
1551 crtc_resp->gamma_size = crtc->gamma_size;
1552 if (crtc->fb)
1553 crtc_resp->fb_id = crtc->fb->base.id;
1554 else
1555 crtc_resp->fb_id = 0;
1556
1557 if (crtc->enabled) {
1558
1559 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1560 crtc_resp->mode_valid = 1;
1561
1562 } else {
1563 crtc_resp->mode_valid = 0;
1564 }
1565
1566out:
Daniel Vetter84849902012-12-02 00:28:11 +01001567 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001568 return ret;
1569}
1570
1571/**
1572 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001573 * @dev: drm device for the ioctl
1574 * @data: data pointer for the ioctl
1575 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001576 *
Dave Airlief453ba02008-11-07 14:05:41 -08001577 * Construct a connector configuration structure to return to the user.
1578 *
1579 * Called by the user via ioctl.
1580 *
1581 * RETURNS:
1582 * Zero on success, errno on failure.
1583 */
1584int drm_mode_getconnector(struct drm_device *dev, void *data,
1585 struct drm_file *file_priv)
1586{
1587 struct drm_mode_get_connector *out_resp = data;
1588 struct drm_mode_object *obj;
1589 struct drm_connector *connector;
1590 struct drm_display_mode *mode;
1591 int mode_count = 0;
1592 int props_count = 0;
1593 int encoders_count = 0;
1594 int ret = 0;
1595 int copied = 0;
1596 int i;
1597 struct drm_mode_modeinfo u_mode;
1598 struct drm_mode_modeinfo __user *mode_ptr;
1599 uint32_t __user *prop_ptr;
1600 uint64_t __user *prop_values;
1601 uint32_t __user *encoder_ptr;
1602
Dave Airliefb3b06c2011-02-08 13:55:21 +10001603 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1604 return -EINVAL;
1605
Dave Airlief453ba02008-11-07 14:05:41 -08001606 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1607
Jerome Glisse94401062010-07-15 15:43:25 -04001608 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001609
Daniel Vetter7b240562012-12-12 00:35:33 +01001610 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001611
1612 obj = drm_mode_object_find(dev, out_resp->connector_id,
1613 DRM_MODE_OBJECT_CONNECTOR);
1614 if (!obj) {
1615 ret = -EINVAL;
1616 goto out;
1617 }
1618 connector = obj_to_connector(obj);
1619
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001620 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001621
1622 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1623 if (connector->encoder_ids[i] != 0) {
1624 encoders_count++;
1625 }
1626 }
1627
1628 if (out_resp->count_modes == 0) {
1629 connector->funcs->fill_modes(connector,
1630 dev->mode_config.max_width,
1631 dev->mode_config.max_height);
1632 }
1633
1634 /* delayed so we get modes regardless of pre-fill_modes state */
1635 list_for_each_entry(mode, &connector->modes, head)
1636 mode_count++;
1637
1638 out_resp->connector_id = connector->base.id;
1639 out_resp->connector_type = connector->connector_type;
1640 out_resp->connector_type_id = connector->connector_type_id;
1641 out_resp->mm_width = connector->display_info.width_mm;
1642 out_resp->mm_height = connector->display_info.height_mm;
1643 out_resp->subpixel = connector->display_info.subpixel_order;
1644 out_resp->connection = connector->status;
1645 if (connector->encoder)
1646 out_resp->encoder_id = connector->encoder->base.id;
1647 else
1648 out_resp->encoder_id = 0;
1649
1650 /*
1651 * This ioctl is called twice, once to determine how much space is
1652 * needed, and the 2nd time to fill it.
1653 */
1654 if ((out_resp->count_modes >= mode_count) && mode_count) {
1655 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001656 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001657 list_for_each_entry(mode, &connector->modes, head) {
1658 drm_crtc_convert_to_umode(&u_mode, mode);
1659 if (copy_to_user(mode_ptr + copied,
1660 &u_mode, sizeof(u_mode))) {
1661 ret = -EFAULT;
1662 goto out;
1663 }
1664 copied++;
1665 }
1666 }
1667 out_resp->count_modes = mode_count;
1668
1669 if ((out_resp->count_props >= props_count) && props_count) {
1670 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001671 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1672 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001673 for (i = 0; i < connector->properties.count; i++) {
1674 if (put_user(connector->properties.ids[i],
1675 prop_ptr + copied)) {
1676 ret = -EFAULT;
1677 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001678 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001679
1680 if (put_user(connector->properties.values[i],
1681 prop_values + copied)) {
1682 ret = -EFAULT;
1683 goto out;
1684 }
1685 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001686 }
1687 }
1688 out_resp->count_props = props_count;
1689
1690 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1691 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001692 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001693 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1694 if (connector->encoder_ids[i] != 0) {
1695 if (put_user(connector->encoder_ids[i],
1696 encoder_ptr + copied)) {
1697 ret = -EFAULT;
1698 goto out;
1699 }
1700 copied++;
1701 }
1702 }
1703 }
1704 out_resp->count_encoders = encoders_count;
1705
1706out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001707 mutex_unlock(&dev->mode_config.mutex);
1708
Dave Airlief453ba02008-11-07 14:05:41 -08001709 return ret;
1710}
1711
1712int drm_mode_getencoder(struct drm_device *dev, void *data,
1713 struct drm_file *file_priv)
1714{
1715 struct drm_mode_get_encoder *enc_resp = data;
1716 struct drm_mode_object *obj;
1717 struct drm_encoder *encoder;
1718 int ret = 0;
1719
Dave Airliefb3b06c2011-02-08 13:55:21 +10001720 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1721 return -EINVAL;
1722
Daniel Vetter84849902012-12-02 00:28:11 +01001723 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001724 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1725 DRM_MODE_OBJECT_ENCODER);
1726 if (!obj) {
1727 ret = -EINVAL;
1728 goto out;
1729 }
1730 encoder = obj_to_encoder(obj);
1731
1732 if (encoder->crtc)
1733 enc_resp->crtc_id = encoder->crtc->base.id;
1734 else
1735 enc_resp->crtc_id = 0;
1736 enc_resp->encoder_type = encoder->encoder_type;
1737 enc_resp->encoder_id = encoder->base.id;
1738 enc_resp->possible_crtcs = encoder->possible_crtcs;
1739 enc_resp->possible_clones = encoder->possible_clones;
1740
1741out:
Daniel Vetter84849902012-12-02 00:28:11 +01001742 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001743 return ret;
1744}
1745
1746/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001747 * drm_mode_getplane_res - get plane info
1748 * @dev: DRM device
1749 * @data: ioctl data
1750 * @file_priv: DRM file info
1751 *
1752 * Return an plane count and set of IDs.
1753 */
1754int drm_mode_getplane_res(struct drm_device *dev, void *data,
1755 struct drm_file *file_priv)
1756{
1757 struct drm_mode_get_plane_res *plane_resp = data;
1758 struct drm_mode_config *config;
1759 struct drm_plane *plane;
1760 uint32_t __user *plane_ptr;
1761 int copied = 0, ret = 0;
1762
1763 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1764 return -EINVAL;
1765
Daniel Vetter84849902012-12-02 00:28:11 +01001766 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001767 config = &dev->mode_config;
1768
1769 /*
1770 * This ioctl is called twice, once to determine how much space is
1771 * needed, and the 2nd time to fill it.
1772 */
1773 if (config->num_plane &&
1774 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001775 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001776
1777 list_for_each_entry(plane, &config->plane_list, head) {
1778 if (put_user(plane->base.id, plane_ptr + copied)) {
1779 ret = -EFAULT;
1780 goto out;
1781 }
1782 copied++;
1783 }
1784 }
1785 plane_resp->count_planes = config->num_plane;
1786
1787out:
Daniel Vetter84849902012-12-02 00:28:11 +01001788 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001789 return ret;
1790}
1791
1792/**
1793 * drm_mode_getplane - get plane info
1794 * @dev: DRM device
1795 * @data: ioctl data
1796 * @file_priv: DRM file info
1797 *
1798 * Return plane info, including formats supported, gamma size, any
1799 * current fb, etc.
1800 */
1801int drm_mode_getplane(struct drm_device *dev, void *data,
1802 struct drm_file *file_priv)
1803{
1804 struct drm_mode_get_plane *plane_resp = data;
1805 struct drm_mode_object *obj;
1806 struct drm_plane *plane;
1807 uint32_t __user *format_ptr;
1808 int ret = 0;
1809
1810 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1811 return -EINVAL;
1812
Daniel Vetter84849902012-12-02 00:28:11 +01001813 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001814 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1815 DRM_MODE_OBJECT_PLANE);
1816 if (!obj) {
1817 ret = -ENOENT;
1818 goto out;
1819 }
1820 plane = obj_to_plane(obj);
1821
1822 if (plane->crtc)
1823 plane_resp->crtc_id = plane->crtc->base.id;
1824 else
1825 plane_resp->crtc_id = 0;
1826
1827 if (plane->fb)
1828 plane_resp->fb_id = plane->fb->base.id;
1829 else
1830 plane_resp->fb_id = 0;
1831
1832 plane_resp->plane_id = plane->base.id;
1833 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03001834 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001835
1836 /*
1837 * This ioctl is called twice, once to determine how much space is
1838 * needed, and the 2nd time to fill it.
1839 */
1840 if (plane->format_count &&
1841 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001842 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001843 if (copy_to_user(format_ptr,
1844 plane->format_types,
1845 sizeof(uint32_t) * plane->format_count)) {
1846 ret = -EFAULT;
1847 goto out;
1848 }
1849 }
1850 plane_resp->count_format_types = plane->format_count;
1851
1852out:
Daniel Vetter84849902012-12-02 00:28:11 +01001853 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001854 return ret;
1855}
1856
1857/**
1858 * drm_mode_setplane - set up or tear down an plane
1859 * @dev: DRM device
1860 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001861 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001862 *
1863 * Set plane info, including placement, fb, scaling, and other factors.
1864 * Or pass a NULL fb to disable.
1865 */
1866int drm_mode_setplane(struct drm_device *dev, void *data,
1867 struct drm_file *file_priv)
1868{
1869 struct drm_mode_set_plane *plane_req = data;
1870 struct drm_mode_object *obj;
1871 struct drm_plane *plane;
1872 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001873 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001874 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001875 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001876 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001877
1878 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1879 return -EINVAL;
1880
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001881 /*
1882 * First, find the plane, crtc, and fb objects. If not available,
1883 * we don't bother to call the driver.
1884 */
1885 obj = drm_mode_object_find(dev, plane_req->plane_id,
1886 DRM_MODE_OBJECT_PLANE);
1887 if (!obj) {
1888 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1889 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001890 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001891 }
1892 plane = obj_to_plane(obj);
1893
1894 /* No fb means shut it down */
1895 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001896 drm_modeset_lock_all(dev);
1897 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001898 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001899 plane->crtc = NULL;
1900 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001901 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001902 goto out;
1903 }
1904
1905 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1906 DRM_MODE_OBJECT_CRTC);
1907 if (!obj) {
1908 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1909 plane_req->crtc_id);
1910 ret = -ENOENT;
1911 goto out;
1912 }
1913 crtc = obj_to_crtc(obj);
1914
Daniel Vetter786b99e2012-12-02 21:53:40 +01001915 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1916 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001917 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1918 plane_req->fb_id);
1919 ret = -ENOENT;
1920 goto out;
1921 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001922
Ville Syrjälä62443be2011-12-20 00:06:47 +02001923 /* Check whether this plane supports the fb pixel format. */
1924 for (i = 0; i < plane->format_count; i++)
1925 if (fb->pixel_format == plane->format_types[i])
1926 break;
1927 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03001928 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1929 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02001930 ret = -EINVAL;
1931 goto out;
1932 }
1933
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001934 fb_width = fb->width << 16;
1935 fb_height = fb->height << 16;
1936
1937 /* Make sure source coordinates are inside the fb. */
1938 if (plane_req->src_w > fb_width ||
1939 plane_req->src_x > fb_width - plane_req->src_w ||
1940 plane_req->src_h > fb_height ||
1941 plane_req->src_y > fb_height - plane_req->src_h) {
1942 DRM_DEBUG_KMS("Invalid source coordinates "
1943 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1944 plane_req->src_w >> 16,
1945 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1946 plane_req->src_h >> 16,
1947 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1948 plane_req->src_x >> 16,
1949 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1950 plane_req->src_y >> 16,
1951 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1952 ret = -ENOSPC;
1953 goto out;
1954 }
1955
Ville Syrjälä687a0402011-12-20 00:06:45 +02001956 /* Give drivers some help against integer overflows */
1957 if (plane_req->crtc_w > INT_MAX ||
1958 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1959 plane_req->crtc_h > INT_MAX ||
1960 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1961 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1962 plane_req->crtc_w, plane_req->crtc_h,
1963 plane_req->crtc_x, plane_req->crtc_y);
1964 ret = -ERANGE;
1965 goto out;
1966 }
1967
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001968 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001969 ret = plane->funcs->update_plane(plane, crtc, fb,
1970 plane_req->crtc_x, plane_req->crtc_y,
1971 plane_req->crtc_w, plane_req->crtc_h,
1972 plane_req->src_x, plane_req->src_y,
1973 plane_req->src_w, plane_req->src_h);
1974 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001975 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001976 plane->crtc = crtc;
1977 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001978 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001979 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001980 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001981
1982out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001983 if (fb)
1984 drm_framebuffer_unreference(fb);
1985 if (old_fb)
1986 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001987
1988 return ret;
1989}
1990
1991/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001992 * drm_mode_set_config_internal - helper to call ->set_config
1993 * @set: modeset config to set
1994 *
1995 * This is a little helper to wrap internal calls to the ->set_config driver
1996 * interface. The only thing it adds is correct refcounting dance.
1997 */
1998int drm_mode_set_config_internal(struct drm_mode_set *set)
1999{
2000 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002001 struct drm_framebuffer *fb;
2002 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002003 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002004
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002005 /*
2006 * NOTE: ->set_config can also disable other crtcs (if we steal all
2007 * connectors from it), hence we need to refcount the fbs across all
2008 * crtcs. Atomic modeset will have saner semantics ...
2009 */
2010 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2011 tmp->old_fb = tmp->fb;
2012
Daniel Vetterb0d12322012-12-11 01:07:12 +01002013 fb = set->fb;
2014
2015 ret = crtc->funcs->set_config(set);
2016 if (ret == 0) {
Daniel Vettercc85e122013-06-15 00:13:15 +02002017 /* crtc->fb must be updated by ->set_config, enforces this. */
2018 WARN_ON(fb != crtc->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002019 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002020
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002021 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2022 if (tmp->fb)
2023 drm_framebuffer_reference(tmp->fb);
2024 if (tmp->old_fb)
2025 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002026 }
2027
2028 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002029}
2030EXPORT_SYMBOL(drm_mode_set_config_internal);
2031
2032/**
Dave Airlief453ba02008-11-07 14:05:41 -08002033 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002034 * @dev: drm device for the ioctl
2035 * @data: data pointer for the ioctl
2036 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002037 *
Dave Airlief453ba02008-11-07 14:05:41 -08002038 * Build a new CRTC configuration based on user request.
2039 *
2040 * Called by the user via ioctl.
2041 *
2042 * RETURNS:
2043 * Zero on success, errno on failure.
2044 */
2045int drm_mode_setcrtc(struct drm_device *dev, void *data,
2046 struct drm_file *file_priv)
2047{
2048 struct drm_mode_config *config = &dev->mode_config;
2049 struct drm_mode_crtc *crtc_req = data;
2050 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002051 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002052 struct drm_connector **connector_set = NULL, *connector;
2053 struct drm_framebuffer *fb = NULL;
2054 struct drm_display_mode *mode = NULL;
2055 struct drm_mode_set set;
2056 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002057 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002058 int i;
2059
Dave Airliefb3b06c2011-02-08 13:55:21 +10002060 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2061 return -EINVAL;
2062
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002063 /* For some reason crtc x/y offsets are signed internally. */
2064 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2065 return -ERANGE;
2066
Daniel Vetter84849902012-12-02 00:28:11 +01002067 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002068 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2069 DRM_MODE_OBJECT_CRTC);
2070 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002071 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002072 ret = -EINVAL;
2073 goto out;
2074 }
2075 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002076 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002077
2078 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002079 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002080 /* If we have a mode we need a framebuffer. */
2081 /* If we pass -1, set the mode with the currently bound fb */
2082 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002083 if (!crtc->fb) {
2084 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2085 ret = -EINVAL;
2086 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002087 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002088 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002089 /* Make refcounting symmetric with the lookup path. */
2090 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002091 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002092 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2093 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002094 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2095 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002096 ret = -EINVAL;
2097 goto out;
2098 }
Dave Airlief453ba02008-11-07 14:05:41 -08002099 }
2100
2101 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002102 if (!mode) {
2103 ret = -ENOMEM;
2104 goto out;
2105 }
2106
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002107 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2108 if (ret) {
2109 DRM_DEBUG_KMS("Invalid mode\n");
2110 goto out;
2111 }
2112
Dave Airlief453ba02008-11-07 14:05:41 -08002113 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002114
Rob Clark7c80e122012-09-04 16:35:56 +00002115 hdisplay = mode->hdisplay;
2116 vdisplay = mode->vdisplay;
2117
2118 if (crtc->invert_dimensions)
2119 swap(hdisplay, vdisplay);
2120
2121 if (hdisplay > fb->width ||
2122 vdisplay > fb->height ||
2123 crtc_req->x > fb->width - hdisplay ||
2124 crtc_req->y > fb->height - vdisplay) {
2125 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2126 fb->width, fb->height,
2127 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2128 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002129 ret = -ENOSPC;
2130 goto out;
2131 }
Dave Airlief453ba02008-11-07 14:05:41 -08002132 }
2133
2134 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002135 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002136 ret = -EINVAL;
2137 goto out;
2138 }
2139
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002140 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002141 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002142 crtc_req->count_connectors);
2143 ret = -EINVAL;
2144 goto out;
2145 }
2146
2147 if (crtc_req->count_connectors > 0) {
2148 u32 out_id;
2149
2150 /* Avoid unbounded kernel memory allocation */
2151 if (crtc_req->count_connectors > config->num_connector) {
2152 ret = -EINVAL;
2153 goto out;
2154 }
2155
2156 connector_set = kmalloc(crtc_req->count_connectors *
2157 sizeof(struct drm_connector *),
2158 GFP_KERNEL);
2159 if (!connector_set) {
2160 ret = -ENOMEM;
2161 goto out;
2162 }
2163
2164 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002165 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002166 if (get_user(out_id, &set_connectors_ptr[i])) {
2167 ret = -EFAULT;
2168 goto out;
2169 }
2170
2171 obj = drm_mode_object_find(dev, out_id,
2172 DRM_MODE_OBJECT_CONNECTOR);
2173 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002174 DRM_DEBUG_KMS("Connector id %d unknown\n",
2175 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002176 ret = -EINVAL;
2177 goto out;
2178 }
2179 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002180 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2181 connector->base.id,
2182 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002183
2184 connector_set[i] = connector;
2185 }
2186 }
2187
2188 set.crtc = crtc;
2189 set.x = crtc_req->x;
2190 set.y = crtc_req->y;
2191 set.mode = mode;
2192 set.connectors = connector_set;
2193 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002194 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002195 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002196
2197out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002198 if (fb)
2199 drm_framebuffer_unreference(fb);
2200
Dave Airlief453ba02008-11-07 14:05:41 -08002201 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002202 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002203 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002204 return ret;
2205}
2206
Dave Airlie4c813d42013-06-20 11:48:52 +10002207static int drm_mode_cursor_common(struct drm_device *dev,
2208 struct drm_mode_cursor2 *req,
2209 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002210{
Dave Airlief453ba02008-11-07 14:05:41 -08002211 struct drm_mode_object *obj;
2212 struct drm_crtc *crtc;
2213 int ret = 0;
2214
Dave Airliefb3b06c2011-02-08 13:55:21 +10002215 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2216 return -EINVAL;
2217
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002218 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002219 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002220
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002221 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002222 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002223 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002224 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002225 }
2226 crtc = obj_to_crtc(obj);
2227
Daniel Vetterdac35662012-12-02 15:24:10 +01002228 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002229 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002230 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002231 ret = -ENXIO;
2232 goto out;
2233 }
2234 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002235 if (crtc->funcs->cursor_set2)
2236 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2237 req->width, req->height, req->hot_x, req->hot_y);
2238 else
2239 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2240 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002241 }
2242
2243 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2244 if (crtc->funcs->cursor_move) {
2245 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2246 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002247 ret = -EFAULT;
2248 goto out;
2249 }
2250 }
2251out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002252 mutex_unlock(&crtc->mutex);
2253
Dave Airlief453ba02008-11-07 14:05:41 -08002254 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002255
2256}
2257int drm_mode_cursor_ioctl(struct drm_device *dev,
2258 void *data, struct drm_file *file_priv)
2259{
2260 struct drm_mode_cursor *req = data;
2261 struct drm_mode_cursor2 new_req;
2262
2263 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2264 new_req.hot_x = new_req.hot_y = 0;
2265
2266 return drm_mode_cursor_common(dev, &new_req, file_priv);
2267}
2268
2269int drm_mode_cursor2_ioctl(struct drm_device *dev,
2270 void *data, struct drm_file *file_priv)
2271{
2272 struct drm_mode_cursor2 *req = data;
2273 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002274}
2275
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002276/* Original addfb only supported RGB formats, so figure out which one */
2277uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2278{
2279 uint32_t fmt;
2280
2281 switch (bpp) {
2282 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002283 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002284 break;
2285 case 16:
2286 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002287 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002288 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002289 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002290 break;
2291 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002292 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002293 break;
2294 case 32:
2295 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002296 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002297 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002298 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002299 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002300 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002301 break;
2302 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002303 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2304 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002305 break;
2306 }
2307
2308 return fmt;
2309}
2310EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2311
Dave Airlief453ba02008-11-07 14:05:41 -08002312/**
2313 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002314 * @dev: drm device for the ioctl
2315 * @data: data pointer for the ioctl
2316 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002317 *
Dave Airlief453ba02008-11-07 14:05:41 -08002318 * Add a new FB to the specified CRTC, given a user request.
2319 *
2320 * Called by the user via ioctl.
2321 *
2322 * RETURNS:
2323 * Zero on success, errno on failure.
2324 */
2325int drm_mode_addfb(struct drm_device *dev,
2326 void *data, struct drm_file *file_priv)
2327{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002328 struct drm_mode_fb_cmd *or = data;
2329 struct drm_mode_fb_cmd2 r = {};
2330 struct drm_mode_config *config = &dev->mode_config;
2331 struct drm_framebuffer *fb;
2332 int ret = 0;
2333
2334 /* Use new struct with format internally */
2335 r.fb_id = or->fb_id;
2336 r.width = or->width;
2337 r.height = or->height;
2338 r.pitches[0] = or->pitch;
2339 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2340 r.handles[0] = or->handle;
2341
2342 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2343 return -EINVAL;
2344
Jesse Barnesacb4b992011-11-07 12:03:23 -08002345 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002346 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002347
2348 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002349 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002350
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002351 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2352 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002353 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002354 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002355 }
2356
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002357 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002358 or->fb_id = fb->base.id;
2359 list_add(&fb->filp_head, &file_priv->fbs);
2360 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002361 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002362
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002363 return ret;
2364}
2365
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002366static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002367{
2368 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2369
2370 switch (format) {
2371 case DRM_FORMAT_C8:
2372 case DRM_FORMAT_RGB332:
2373 case DRM_FORMAT_BGR233:
2374 case DRM_FORMAT_XRGB4444:
2375 case DRM_FORMAT_XBGR4444:
2376 case DRM_FORMAT_RGBX4444:
2377 case DRM_FORMAT_BGRX4444:
2378 case DRM_FORMAT_ARGB4444:
2379 case DRM_FORMAT_ABGR4444:
2380 case DRM_FORMAT_RGBA4444:
2381 case DRM_FORMAT_BGRA4444:
2382 case DRM_FORMAT_XRGB1555:
2383 case DRM_FORMAT_XBGR1555:
2384 case DRM_FORMAT_RGBX5551:
2385 case DRM_FORMAT_BGRX5551:
2386 case DRM_FORMAT_ARGB1555:
2387 case DRM_FORMAT_ABGR1555:
2388 case DRM_FORMAT_RGBA5551:
2389 case DRM_FORMAT_BGRA5551:
2390 case DRM_FORMAT_RGB565:
2391 case DRM_FORMAT_BGR565:
2392 case DRM_FORMAT_RGB888:
2393 case DRM_FORMAT_BGR888:
2394 case DRM_FORMAT_XRGB8888:
2395 case DRM_FORMAT_XBGR8888:
2396 case DRM_FORMAT_RGBX8888:
2397 case DRM_FORMAT_BGRX8888:
2398 case DRM_FORMAT_ARGB8888:
2399 case DRM_FORMAT_ABGR8888:
2400 case DRM_FORMAT_RGBA8888:
2401 case DRM_FORMAT_BGRA8888:
2402 case DRM_FORMAT_XRGB2101010:
2403 case DRM_FORMAT_XBGR2101010:
2404 case DRM_FORMAT_RGBX1010102:
2405 case DRM_FORMAT_BGRX1010102:
2406 case DRM_FORMAT_ARGB2101010:
2407 case DRM_FORMAT_ABGR2101010:
2408 case DRM_FORMAT_RGBA1010102:
2409 case DRM_FORMAT_BGRA1010102:
2410 case DRM_FORMAT_YUYV:
2411 case DRM_FORMAT_YVYU:
2412 case DRM_FORMAT_UYVY:
2413 case DRM_FORMAT_VYUY:
2414 case DRM_FORMAT_AYUV:
2415 case DRM_FORMAT_NV12:
2416 case DRM_FORMAT_NV21:
2417 case DRM_FORMAT_NV16:
2418 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002419 case DRM_FORMAT_NV24:
2420 case DRM_FORMAT_NV42:
Ville Syrjälä935b59772011-12-20 00:06:48 +02002421 case DRM_FORMAT_YUV410:
2422 case DRM_FORMAT_YVU410:
2423 case DRM_FORMAT_YUV411:
2424 case DRM_FORMAT_YVU411:
2425 case DRM_FORMAT_YUV420:
2426 case DRM_FORMAT_YVU420:
2427 case DRM_FORMAT_YUV422:
2428 case DRM_FORMAT_YVU422:
2429 case DRM_FORMAT_YUV444:
2430 case DRM_FORMAT_YVU444:
2431 return 0;
2432 default:
2433 return -EINVAL;
2434 }
2435}
2436
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002437static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002438{
2439 int ret, hsub, vsub, num_planes, i;
2440
2441 ret = format_check(r);
2442 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002443 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2444 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002445 return ret;
2446 }
2447
2448 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2449 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2450 num_planes = drm_format_num_planes(r->pixel_format);
2451
2452 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002453 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002454 return -EINVAL;
2455 }
2456
2457 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002458 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002459 return -EINVAL;
2460 }
2461
2462 for (i = 0; i < num_planes; i++) {
2463 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002464 unsigned int height = r->height / (i != 0 ? vsub : 1);
2465 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002466
2467 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002468 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002469 return -EINVAL;
2470 }
2471
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002472 if ((uint64_t) width * cpp > UINT_MAX)
2473 return -ERANGE;
2474
2475 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2476 return -ERANGE;
2477
2478 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002479 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002480 return -EINVAL;
2481 }
2482 }
2483
2484 return 0;
2485}
2486
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002487/**
2488 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002489 * @dev: drm device for the ioctl
2490 * @data: data pointer for the ioctl
2491 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002492 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002493 * Add a new FB to the specified CRTC, given a user request with format.
2494 *
2495 * Called by the user via ioctl.
2496 *
2497 * RETURNS:
2498 * Zero on success, errno on failure.
2499 */
2500int drm_mode_addfb2(struct drm_device *dev,
2501 void *data, struct drm_file *file_priv)
2502{
2503 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002504 struct drm_mode_config *config = &dev->mode_config;
2505 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002506 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002507
Dave Airliefb3b06c2011-02-08 13:55:21 +10002508 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2509 return -EINVAL;
2510
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002511 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2512 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2513 return -EINVAL;
2514 }
2515
Dave Airlief453ba02008-11-07 14:05:41 -08002516 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002517 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002518 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002519 return -EINVAL;
2520 }
2521 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002522 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002523 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002524 return -EINVAL;
2525 }
2526
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002527 ret = framebuffer_check(r);
2528 if (ret)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002529 return ret;
Ville Syrjälä935b59772011-12-20 00:06:48 +02002530
Dave Airlief453ba02008-11-07 14:05:41 -08002531 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002532 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002533 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002534 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002535 }
2536
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002537 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002538 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002539 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002540 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002541 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002542
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002543
Dave Airlief453ba02008-11-07 14:05:41 -08002544 return ret;
2545}
2546
2547/**
2548 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002549 * @dev: drm device for the ioctl
2550 * @data: data pointer for the ioctl
2551 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002552 *
Dave Airlief453ba02008-11-07 14:05:41 -08002553 * Remove the FB specified by the user.
2554 *
2555 * Called by the user via ioctl.
2556 *
2557 * RETURNS:
2558 * Zero on success, errno on failure.
2559 */
2560int drm_mode_rmfb(struct drm_device *dev,
2561 void *data, struct drm_file *file_priv)
2562{
Dave Airlief453ba02008-11-07 14:05:41 -08002563 struct drm_framebuffer *fb = NULL;
2564 struct drm_framebuffer *fbl = NULL;
2565 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002566 int found = 0;
2567
Dave Airliefb3b06c2011-02-08 13:55:21 +10002568 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2569 return -EINVAL;
2570
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002571 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002572 mutex_lock(&dev->mode_config.fb_lock);
2573 fb = __drm_framebuffer_lookup(dev, *id);
2574 if (!fb)
2575 goto fail_lookup;
2576
Dave Airlief453ba02008-11-07 14:05:41 -08002577 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2578 if (fb == fbl)
2579 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002580 if (!found)
2581 goto fail_lookup;
2582
2583 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2584 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002585
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002586 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002587 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002588 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002589
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002590 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002591
Daniel Vetter2b677e82012-12-10 21:16:05 +01002592 return 0;
2593
2594fail_lookup:
2595 mutex_unlock(&dev->mode_config.fb_lock);
2596 mutex_unlock(&file_priv->fbs_lock);
2597
2598 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002599}
2600
2601/**
2602 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002603 * @dev: drm device for the ioctl
2604 * @data: data pointer for the ioctl
2605 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002606 *
Dave Airlief453ba02008-11-07 14:05:41 -08002607 * Lookup the FB given its ID and return info about it.
2608 *
2609 * Called by the user via ioctl.
2610 *
2611 * RETURNS:
2612 * Zero on success, errno on failure.
2613 */
2614int drm_mode_getfb(struct drm_device *dev,
2615 void *data, struct drm_file *file_priv)
2616{
2617 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002618 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002619 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002620
Dave Airliefb3b06c2011-02-08 13:55:21 +10002621 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2622 return -EINVAL;
2623
Daniel Vetter786b99e2012-12-02 21:53:40 +01002624 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002625 if (!fb)
2626 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002627
2628 r->height = fb->height;
2629 r->width = fb->width;
2630 r->depth = fb->depth;
2631 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002632 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002633 if (fb->funcs->create_handle)
2634 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2635 else
2636 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002637
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002638 drm_framebuffer_unreference(fb);
2639
Dave Airlief453ba02008-11-07 14:05:41 -08002640 return ret;
2641}
2642
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002643int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2644 void *data, struct drm_file *file_priv)
2645{
2646 struct drm_clip_rect __user *clips_ptr;
2647 struct drm_clip_rect *clips = NULL;
2648 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002649 struct drm_framebuffer *fb;
2650 unsigned flags;
2651 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002652 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002653
Dave Airliefb3b06c2011-02-08 13:55:21 +10002654 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2655 return -EINVAL;
2656
Daniel Vetter786b99e2012-12-02 21:53:40 +01002657 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002658 if (!fb)
2659 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002660
2661 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002662 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002663
2664 if (!num_clips != !clips_ptr) {
2665 ret = -EINVAL;
2666 goto out_err1;
2667 }
2668
2669 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2670
2671 /* If userspace annotates copy, clips must come in pairs */
2672 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2673 ret = -EINVAL;
2674 goto out_err1;
2675 }
2676
2677 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002678 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2679 ret = -EINVAL;
2680 goto out_err1;
2681 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002682 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2683 if (!clips) {
2684 ret = -ENOMEM;
2685 goto out_err1;
2686 }
2687
2688 ret = copy_from_user(clips, clips_ptr,
2689 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002690 if (ret) {
2691 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002692 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002693 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002694 }
2695
2696 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002697 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002698 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2699 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002700 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002701 } else {
2702 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002703 }
2704
2705out_err2:
2706 kfree(clips);
2707out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002708 drm_framebuffer_unreference(fb);
2709
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002710 return ret;
2711}
2712
2713
Dave Airlief453ba02008-11-07 14:05:41 -08002714/**
2715 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002716 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002717 *
Dave Airlief453ba02008-11-07 14:05:41 -08002718 * Destroy all the FBs associated with @filp.
2719 *
2720 * Called by the user via ioctl.
2721 *
2722 * RETURNS:
2723 * Zero on success, errno on failure.
2724 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002725void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002726{
Dave Airlief453ba02008-11-07 14:05:41 -08002727 struct drm_device *dev = priv->minor->dev;
2728 struct drm_framebuffer *fb, *tfb;
2729
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002730 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002731 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002732
2733 mutex_lock(&dev->mode_config.fb_lock);
2734 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2735 __drm_framebuffer_unregister(dev, fb);
2736 mutex_unlock(&dev->mode_config.fb_lock);
2737
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002738 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002739
2740 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002741 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002742 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002743 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002744}
2745
Dave Airlief453ba02008-11-07 14:05:41 -08002746struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2747 const char *name, int num_values)
2748{
2749 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002750 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002751
2752 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2753 if (!property)
2754 return NULL;
2755
2756 if (num_values) {
2757 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2758 if (!property->values)
2759 goto fail;
2760 }
2761
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002762 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2763 if (ret)
2764 goto fail;
2765
Dave Airlief453ba02008-11-07 14:05:41 -08002766 property->flags = flags;
2767 property->num_values = num_values;
2768 INIT_LIST_HEAD(&property->enum_blob_list);
2769
Vinson Lee471dd2e2011-11-10 11:55:40 -08002770 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002771 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002772 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2773 }
Dave Airlief453ba02008-11-07 14:05:41 -08002774
2775 list_add_tail(&property->head, &dev->mode_config.property_list);
2776 return property;
2777fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002778 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002779 kfree(property);
2780 return NULL;
2781}
2782EXPORT_SYMBOL(drm_property_create);
2783
Sascha Hauer4a67d392012-02-06 10:58:17 +01002784struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2785 const char *name,
2786 const struct drm_prop_enum_list *props,
2787 int num_values)
2788{
2789 struct drm_property *property;
2790 int i, ret;
2791
2792 flags |= DRM_MODE_PROP_ENUM;
2793
2794 property = drm_property_create(dev, flags, name, num_values);
2795 if (!property)
2796 return NULL;
2797
2798 for (i = 0; i < num_values; i++) {
2799 ret = drm_property_add_enum(property, i,
2800 props[i].type,
2801 props[i].name);
2802 if (ret) {
2803 drm_property_destroy(dev, property);
2804 return NULL;
2805 }
2806 }
2807
2808 return property;
2809}
2810EXPORT_SYMBOL(drm_property_create_enum);
2811
Rob Clark49e27542012-05-17 02:23:26 -06002812struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2813 int flags, const char *name,
2814 const struct drm_prop_enum_list *props,
2815 int num_values)
2816{
2817 struct drm_property *property;
2818 int i, ret;
2819
2820 flags |= DRM_MODE_PROP_BITMASK;
2821
2822 property = drm_property_create(dev, flags, name, num_values);
2823 if (!property)
2824 return NULL;
2825
2826 for (i = 0; i < num_values; i++) {
2827 ret = drm_property_add_enum(property, i,
2828 props[i].type,
2829 props[i].name);
2830 if (ret) {
2831 drm_property_destroy(dev, property);
2832 return NULL;
2833 }
2834 }
2835
2836 return property;
2837}
2838EXPORT_SYMBOL(drm_property_create_bitmask);
2839
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002840struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2841 const char *name,
2842 uint64_t min, uint64_t max)
2843{
2844 struct drm_property *property;
2845
2846 flags |= DRM_MODE_PROP_RANGE;
2847
2848 property = drm_property_create(dev, flags, name, 2);
2849 if (!property)
2850 return NULL;
2851
2852 property->values[0] = min;
2853 property->values[1] = max;
2854
2855 return property;
2856}
2857EXPORT_SYMBOL(drm_property_create_range);
2858
Dave Airlief453ba02008-11-07 14:05:41 -08002859int drm_property_add_enum(struct drm_property *property, int index,
2860 uint64_t value, const char *name)
2861{
2862 struct drm_property_enum *prop_enum;
2863
Rob Clark49e27542012-05-17 02:23:26 -06002864 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2865 return -EINVAL;
2866
2867 /*
2868 * Bitmask enum properties have the additional constraint of values
2869 * from 0 to 63
2870 */
2871 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002872 return -EINVAL;
2873
2874 if (!list_empty(&property->enum_blob_list)) {
2875 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2876 if (prop_enum->value == value) {
2877 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2878 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2879 return 0;
2880 }
2881 }
2882 }
2883
2884 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2885 if (!prop_enum)
2886 return -ENOMEM;
2887
2888 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2889 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2890 prop_enum->value = value;
2891
2892 property->values[index] = value;
2893 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2894 return 0;
2895}
2896EXPORT_SYMBOL(drm_property_add_enum);
2897
2898void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2899{
2900 struct drm_property_enum *prop_enum, *pt;
2901
2902 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2903 list_del(&prop_enum->head);
2904 kfree(prop_enum);
2905 }
2906
2907 if (property->num_values)
2908 kfree(property->values);
2909 drm_mode_object_put(dev, &property->base);
2910 list_del(&property->head);
2911 kfree(property);
2912}
2913EXPORT_SYMBOL(drm_property_destroy);
2914
Paulo Zanonic5431882012-05-15 18:09:02 -03002915void drm_object_attach_property(struct drm_mode_object *obj,
2916 struct drm_property *property,
2917 uint64_t init_val)
2918{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002919 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002920
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002921 if (count == DRM_OBJECT_MAX_PROPERTY) {
2922 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2923 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2924 "you see this message on the same object type.\n",
2925 obj->type);
2926 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002927 }
2928
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002929 obj->properties->ids[count] = property->base.id;
2930 obj->properties->values[count] = init_val;
2931 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002932}
2933EXPORT_SYMBOL(drm_object_attach_property);
2934
2935int drm_object_property_set_value(struct drm_mode_object *obj,
2936 struct drm_property *property, uint64_t val)
2937{
2938 int i;
2939
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002940 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002941 if (obj->properties->ids[i] == property->base.id) {
2942 obj->properties->values[i] = val;
2943 return 0;
2944 }
2945 }
2946
2947 return -EINVAL;
2948}
2949EXPORT_SYMBOL(drm_object_property_set_value);
2950
2951int drm_object_property_get_value(struct drm_mode_object *obj,
2952 struct drm_property *property, uint64_t *val)
2953{
2954 int i;
2955
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002956 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002957 if (obj->properties->ids[i] == property->base.id) {
2958 *val = obj->properties->values[i];
2959 return 0;
2960 }
2961 }
2962
2963 return -EINVAL;
2964}
2965EXPORT_SYMBOL(drm_object_property_get_value);
2966
Dave Airlief453ba02008-11-07 14:05:41 -08002967int drm_mode_getproperty_ioctl(struct drm_device *dev,
2968 void *data, struct drm_file *file_priv)
2969{
2970 struct drm_mode_object *obj;
2971 struct drm_mode_get_property *out_resp = data;
2972 struct drm_property *property;
2973 int enum_count = 0;
2974 int blob_count = 0;
2975 int value_count = 0;
2976 int ret = 0, i;
2977 int copied;
2978 struct drm_property_enum *prop_enum;
2979 struct drm_mode_property_enum __user *enum_ptr;
2980 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002981 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002982 uint64_t __user *values_ptr;
2983 uint32_t __user *blob_length_ptr;
2984
Dave Airliefb3b06c2011-02-08 13:55:21 +10002985 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2986 return -EINVAL;
2987
Daniel Vetter84849902012-12-02 00:28:11 +01002988 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002989 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2990 if (!obj) {
2991 ret = -EINVAL;
2992 goto done;
2993 }
2994 property = obj_to_property(obj);
2995
Rob Clark49e27542012-05-17 02:23:26 -06002996 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002997 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2998 enum_count++;
2999 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3000 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3001 blob_count++;
3002 }
3003
3004 value_count = property->num_values;
3005
3006 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3007 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3008 out_resp->flags = property->flags;
3009
3010 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003011 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003012 for (i = 0; i < value_count; i++) {
3013 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3014 ret = -EFAULT;
3015 goto done;
3016 }
3017 }
3018 }
3019 out_resp->count_values = value_count;
3020
Rob Clark49e27542012-05-17 02:23:26 -06003021 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003022 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3023 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003024 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003025 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3026
3027 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3028 ret = -EFAULT;
3029 goto done;
3030 }
3031
3032 if (copy_to_user(&enum_ptr[copied].name,
3033 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3034 ret = -EFAULT;
3035 goto done;
3036 }
3037 copied++;
3038 }
3039 }
3040 out_resp->count_enum_blobs = enum_count;
3041 }
3042
3043 if (property->flags & DRM_MODE_PROP_BLOB) {
3044 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3045 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003046 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3047 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003048
3049 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3050 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3051 ret = -EFAULT;
3052 goto done;
3053 }
3054
3055 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3056 ret = -EFAULT;
3057 goto done;
3058 }
3059
3060 copied++;
3061 }
3062 }
3063 out_resp->count_enum_blobs = blob_count;
3064 }
3065done:
Daniel Vetter84849902012-12-02 00:28:11 +01003066 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003067 return ret;
3068}
3069
3070static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3071 void *data)
3072{
3073 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003074 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003075
3076 if (!length || !data)
3077 return NULL;
3078
3079 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3080 if (!blob)
3081 return NULL;
3082
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003083 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3084 if (ret) {
3085 kfree(blob);
3086 return NULL;
3087 }
3088
Dave Airlief453ba02008-11-07 14:05:41 -08003089 blob->length = length;
3090
3091 memcpy(blob->data, data, length);
3092
Dave Airlief453ba02008-11-07 14:05:41 -08003093 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3094 return blob;
3095}
3096
3097static void drm_property_destroy_blob(struct drm_device *dev,
3098 struct drm_property_blob *blob)
3099{
3100 drm_mode_object_put(dev, &blob->base);
3101 list_del(&blob->head);
3102 kfree(blob);
3103}
3104
3105int drm_mode_getblob_ioctl(struct drm_device *dev,
3106 void *data, struct drm_file *file_priv)
3107{
3108 struct drm_mode_object *obj;
3109 struct drm_mode_get_blob *out_resp = data;
3110 struct drm_property_blob *blob;
3111 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003112 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003113
Dave Airliefb3b06c2011-02-08 13:55:21 +10003114 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3115 return -EINVAL;
3116
Daniel Vetter84849902012-12-02 00:28:11 +01003117 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003118 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3119 if (!obj) {
3120 ret = -EINVAL;
3121 goto done;
3122 }
3123 blob = obj_to_blob(obj);
3124
3125 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003126 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003127 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3128 ret = -EFAULT;
3129 goto done;
3130 }
3131 }
3132 out_resp->length = blob->length;
3133
3134done:
Daniel Vetter84849902012-12-02 00:28:11 +01003135 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003136 return ret;
3137}
3138
3139int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3140 struct edid *edid)
3141{
3142 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003143 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003144
3145 if (connector->edid_blob_ptr)
3146 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3147
3148 /* Delete edid, when there is none. */
3149 if (!edid) {
3150 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003151 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003152 return ret;
3153 }
3154
Adam Jackson7466f4c2010-03-29 21:43:23 +00003155 size = EDID_LENGTH * (1 + edid->extensions);
3156 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3157 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003158 if (!connector->edid_blob_ptr)
3159 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003160
Rob Clark58495562012-10-11 20:50:56 -05003161 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003162 dev->mode_config.edid_property,
3163 connector->edid_blob_ptr->base.id);
3164
3165 return ret;
3166}
3167EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3168
Paulo Zanoni26a34812012-05-15 18:08:59 -03003169static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003170 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003171{
3172 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3173 return false;
3174 if (property->flags & DRM_MODE_PROP_RANGE) {
3175 if (value < property->values[0] || value > property->values[1])
3176 return false;
3177 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003178 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3179 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003180 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003181 for (i = 0; i < property->num_values; i++)
3182 valid_mask |= (1ULL << property->values[i]);
3183 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003184 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3185 /* Only the driver knows */
3186 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003187 } else {
3188 int i;
3189 for (i = 0; i < property->num_values; i++)
3190 if (property->values[i] == value)
3191 return true;
3192 return false;
3193 }
3194}
3195
Dave Airlief453ba02008-11-07 14:05:41 -08003196int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3197 void *data, struct drm_file *file_priv)
3198{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003199 struct drm_mode_connector_set_property *conn_set_prop = data;
3200 struct drm_mode_obj_set_property obj_set_prop = {
3201 .value = conn_set_prop->value,
3202 .prop_id = conn_set_prop->prop_id,
3203 .obj_id = conn_set_prop->connector_id,
3204 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3205 };
Dave Airlief453ba02008-11-07 14:05:41 -08003206
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003207 /* It does all the locking and checking we need */
3208 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003209}
3210
Paulo Zanonic5431882012-05-15 18:09:02 -03003211static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3212 struct drm_property *property,
3213 uint64_t value)
3214{
3215 int ret = -EINVAL;
3216 struct drm_connector *connector = obj_to_connector(obj);
3217
3218 /* Do DPMS ourselves */
3219 if (property == connector->dev->mode_config.dpms_property) {
3220 if (connector->funcs->dpms)
3221 (*connector->funcs->dpms)(connector, (int)value);
3222 ret = 0;
3223 } else if (connector->funcs->set_property)
3224 ret = connector->funcs->set_property(connector, property, value);
3225
3226 /* store the property value if successful */
3227 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003228 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003229 return ret;
3230}
3231
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003232static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3233 struct drm_property *property,
3234 uint64_t value)
3235{
3236 int ret = -EINVAL;
3237 struct drm_crtc *crtc = obj_to_crtc(obj);
3238
3239 if (crtc->funcs->set_property)
3240 ret = crtc->funcs->set_property(crtc, property, value);
3241 if (!ret)
3242 drm_object_property_set_value(obj, property, value);
3243
3244 return ret;
3245}
3246
Rob Clark4d939142012-05-17 02:23:27 -06003247static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3248 struct drm_property *property,
3249 uint64_t value)
3250{
3251 int ret = -EINVAL;
3252 struct drm_plane *plane = obj_to_plane(obj);
3253
3254 if (plane->funcs->set_property)
3255 ret = plane->funcs->set_property(plane, property, value);
3256 if (!ret)
3257 drm_object_property_set_value(obj, property, value);
3258
3259 return ret;
3260}
3261
Paulo Zanonic5431882012-05-15 18:09:02 -03003262int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3263 struct drm_file *file_priv)
3264{
3265 struct drm_mode_obj_get_properties *arg = data;
3266 struct drm_mode_object *obj;
3267 int ret = 0;
3268 int i;
3269 int copied = 0;
3270 int props_count = 0;
3271 uint32_t __user *props_ptr;
3272 uint64_t __user *prop_values_ptr;
3273
3274 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3275 return -EINVAL;
3276
Daniel Vetter84849902012-12-02 00:28:11 +01003277 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003278
3279 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3280 if (!obj) {
3281 ret = -EINVAL;
3282 goto out;
3283 }
3284 if (!obj->properties) {
3285 ret = -EINVAL;
3286 goto out;
3287 }
3288
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003289 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003290
3291 /* This ioctl is called twice, once to determine how much space is
3292 * needed, and the 2nd time to fill it. */
3293 if ((arg->count_props >= props_count) && props_count) {
3294 copied = 0;
3295 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3296 prop_values_ptr = (uint64_t __user *)(unsigned long)
3297 (arg->prop_values_ptr);
3298 for (i = 0; i < props_count; i++) {
3299 if (put_user(obj->properties->ids[i],
3300 props_ptr + copied)) {
3301 ret = -EFAULT;
3302 goto out;
3303 }
3304 if (put_user(obj->properties->values[i],
3305 prop_values_ptr + copied)) {
3306 ret = -EFAULT;
3307 goto out;
3308 }
3309 copied++;
3310 }
3311 }
3312 arg->count_props = props_count;
3313out:
Daniel Vetter84849902012-12-02 00:28:11 +01003314 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003315 return ret;
3316}
3317
3318int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3319 struct drm_file *file_priv)
3320{
3321 struct drm_mode_obj_set_property *arg = data;
3322 struct drm_mode_object *arg_obj;
3323 struct drm_mode_object *prop_obj;
3324 struct drm_property *property;
3325 int ret = -EINVAL;
3326 int i;
3327
3328 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3329 return -EINVAL;
3330
Daniel Vetter84849902012-12-02 00:28:11 +01003331 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003332
3333 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3334 if (!arg_obj)
3335 goto out;
3336 if (!arg_obj->properties)
3337 goto out;
3338
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003339 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003340 if (arg_obj->properties->ids[i] == arg->prop_id)
3341 break;
3342
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003343 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003344 goto out;
3345
3346 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3347 DRM_MODE_OBJECT_PROPERTY);
3348 if (!prop_obj)
3349 goto out;
3350 property = obj_to_property(prop_obj);
3351
3352 if (!drm_property_change_is_valid(property, arg->value))
3353 goto out;
3354
3355 switch (arg_obj->type) {
3356 case DRM_MODE_OBJECT_CONNECTOR:
3357 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3358 arg->value);
3359 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003360 case DRM_MODE_OBJECT_CRTC:
3361 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3362 break;
Rob Clark4d939142012-05-17 02:23:27 -06003363 case DRM_MODE_OBJECT_PLANE:
3364 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3365 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003366 }
3367
3368out:
Daniel Vetter84849902012-12-02 00:28:11 +01003369 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003370 return ret;
3371}
3372
Dave Airlief453ba02008-11-07 14:05:41 -08003373int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3374 struct drm_encoder *encoder)
3375{
3376 int i;
3377
3378 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3379 if (connector->encoder_ids[i] == 0) {
3380 connector->encoder_ids[i] = encoder->base.id;
3381 return 0;
3382 }
3383 }
3384 return -ENOMEM;
3385}
3386EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3387
3388void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3389 struct drm_encoder *encoder)
3390{
3391 int i;
3392 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3393 if (connector->encoder_ids[i] == encoder->base.id) {
3394 connector->encoder_ids[i] = 0;
3395 if (connector->encoder == encoder)
3396 connector->encoder = NULL;
3397 break;
3398 }
3399 }
3400}
3401EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3402
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003403int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003404 int gamma_size)
3405{
3406 crtc->gamma_size = gamma_size;
3407
3408 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3409 if (!crtc->gamma_store) {
3410 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003411 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003412 }
3413
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003414 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003415}
3416EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3417
3418int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3419 void *data, struct drm_file *file_priv)
3420{
3421 struct drm_mode_crtc_lut *crtc_lut = data;
3422 struct drm_mode_object *obj;
3423 struct drm_crtc *crtc;
3424 void *r_base, *g_base, *b_base;
3425 int size;
3426 int ret = 0;
3427
Dave Airliefb3b06c2011-02-08 13:55:21 +10003428 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3429 return -EINVAL;
3430
Daniel Vetter84849902012-12-02 00:28:11 +01003431 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003432 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3433 if (!obj) {
3434 ret = -EINVAL;
3435 goto out;
3436 }
3437 crtc = obj_to_crtc(obj);
3438
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003439 if (crtc->funcs->gamma_set == NULL) {
3440 ret = -ENOSYS;
3441 goto out;
3442 }
3443
Dave Airlief453ba02008-11-07 14:05:41 -08003444 /* memcpy into gamma store */
3445 if (crtc_lut->gamma_size != crtc->gamma_size) {
3446 ret = -EINVAL;
3447 goto out;
3448 }
3449
3450 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3451 r_base = crtc->gamma_store;
3452 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3453 ret = -EFAULT;
3454 goto out;
3455 }
3456
3457 g_base = r_base + size;
3458 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3459 ret = -EFAULT;
3460 goto out;
3461 }
3462
3463 b_base = g_base + size;
3464 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3465 ret = -EFAULT;
3466 goto out;
3467 }
3468
James Simmons72034252010-08-03 01:33:19 +01003469 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003470
3471out:
Daniel Vetter84849902012-12-02 00:28:11 +01003472 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003473 return ret;
3474
3475}
3476
3477int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3478 void *data, struct drm_file *file_priv)
3479{
3480 struct drm_mode_crtc_lut *crtc_lut = data;
3481 struct drm_mode_object *obj;
3482 struct drm_crtc *crtc;
3483 void *r_base, *g_base, *b_base;
3484 int size;
3485 int ret = 0;
3486
Dave Airliefb3b06c2011-02-08 13:55:21 +10003487 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3488 return -EINVAL;
3489
Daniel Vetter84849902012-12-02 00:28:11 +01003490 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003491 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3492 if (!obj) {
3493 ret = -EINVAL;
3494 goto out;
3495 }
3496 crtc = obj_to_crtc(obj);
3497
3498 /* memcpy into gamma store */
3499 if (crtc_lut->gamma_size != crtc->gamma_size) {
3500 ret = -EINVAL;
3501 goto out;
3502 }
3503
3504 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3505 r_base = crtc->gamma_store;
3506 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3507 ret = -EFAULT;
3508 goto out;
3509 }
3510
3511 g_base = r_base + size;
3512 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3513 ret = -EFAULT;
3514 goto out;
3515 }
3516
3517 b_base = g_base + size;
3518 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3519 ret = -EFAULT;
3520 goto out;
3521 }
3522out:
Daniel Vetter84849902012-12-02 00:28:11 +01003523 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003524 return ret;
3525}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003526
3527int drm_mode_page_flip_ioctl(struct drm_device *dev,
3528 void *data, struct drm_file *file_priv)
3529{
3530 struct drm_mode_crtc_page_flip *page_flip = data;
3531 struct drm_mode_object *obj;
3532 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003533 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003534 struct drm_pending_vblank_event *e = NULL;
3535 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003536 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003537 int ret = -EINVAL;
3538
3539 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3540 page_flip->reserved != 0)
3541 return -EINVAL;
3542
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003543 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3544 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003545 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003546 crtc = obj_to_crtc(obj);
3547
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003548 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003549 if (crtc->fb == NULL) {
3550 /* The framebuffer is currently unbound, presumably
3551 * due to a hotplug event, that userspace has not
3552 * yet discovered.
3553 */
3554 ret = -EBUSY;
3555 goto out;
3556 }
3557
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003558 if (crtc->funcs->page_flip == NULL)
3559 goto out;
3560
Daniel Vetter786b99e2012-12-02 21:53:40 +01003561 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3562 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003563 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003564
Rob Clark7c80e122012-09-04 16:35:56 +00003565 hdisplay = crtc->mode.hdisplay;
3566 vdisplay = crtc->mode.vdisplay;
3567
3568 if (crtc->invert_dimensions)
3569 swap(hdisplay, vdisplay);
3570
3571 if (hdisplay > fb->width ||
3572 vdisplay > fb->height ||
3573 crtc->x > fb->width - hdisplay ||
3574 crtc->y > fb->height - vdisplay) {
3575 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3576 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3577 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003578 ret = -ENOSPC;
3579 goto out;
3580 }
3581
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003582 if (crtc->fb->pixel_format != fb->pixel_format) {
3583 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3584 ret = -EINVAL;
3585 goto out;
3586 }
3587
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003588 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3589 ret = -ENOMEM;
3590 spin_lock_irqsave(&dev->event_lock, flags);
3591 if (file_priv->event_space < sizeof e->event) {
3592 spin_unlock_irqrestore(&dev->event_lock, flags);
3593 goto out;
3594 }
3595 file_priv->event_space -= sizeof e->event;
3596 spin_unlock_irqrestore(&dev->event_lock, flags);
3597
3598 e = kzalloc(sizeof *e, GFP_KERNEL);
3599 if (e == NULL) {
3600 spin_lock_irqsave(&dev->event_lock, flags);
3601 file_priv->event_space += sizeof e->event;
3602 spin_unlock_irqrestore(&dev->event_lock, flags);
3603 goto out;
3604 }
3605
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003606 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003607 e->event.base.length = sizeof e->event;
3608 e->event.user_data = page_flip->user_data;
3609 e->base.event = &e->event.base;
3610 e->base.file_priv = file_priv;
3611 e->base.destroy =
3612 (void (*) (struct drm_pending_event *)) kfree;
3613 }
3614
Daniel Vetterb0d12322012-12-11 01:07:12 +01003615 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003616 ret = crtc->funcs->page_flip(crtc, fb, e);
3617 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003618 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3619 spin_lock_irqsave(&dev->event_lock, flags);
3620 file_priv->event_space += sizeof e->event;
3621 spin_unlock_irqrestore(&dev->event_lock, flags);
3622 kfree(e);
3623 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003624 /* Keep the old fb, don't unref it. */
3625 old_fb = NULL;
3626 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003627 /*
3628 * Warn if the driver hasn't properly updated the crtc->fb
3629 * field to reflect that the new framebuffer is now used.
3630 * Failing to do so will screw with the reference counting
3631 * on framebuffers.
3632 */
3633 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003634 /* Unref only the old framebuffer. */
3635 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003636 }
3637
3638out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003639 if (fb)
3640 drm_framebuffer_unreference(fb);
3641 if (old_fb)
3642 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003643 mutex_unlock(&crtc->mutex);
3644
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003645 return ret;
3646}
Chris Wilsoneb033552011-01-24 15:11:08 +00003647
3648void drm_mode_config_reset(struct drm_device *dev)
3649{
3650 struct drm_crtc *crtc;
3651 struct drm_encoder *encoder;
3652 struct drm_connector *connector;
3653
3654 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3655 if (crtc->funcs->reset)
3656 crtc->funcs->reset(crtc);
3657
3658 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3659 if (encoder->funcs->reset)
3660 encoder->funcs->reset(encoder);
3661
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003662 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3663 connector->status = connector_status_unknown;
3664
Chris Wilsoneb033552011-01-24 15:11:08 +00003665 if (connector->funcs->reset)
3666 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003667 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003668}
3669EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003670
3671int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3672 void *data, struct drm_file *file_priv)
3673{
3674 struct drm_mode_create_dumb *args = data;
3675
3676 if (!dev->driver->dumb_create)
3677 return -ENOSYS;
3678 return dev->driver->dumb_create(file_priv, dev, args);
3679}
3680
3681int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3682 void *data, struct drm_file *file_priv)
3683{
3684 struct drm_mode_map_dumb *args = data;
3685
3686 /* call driver ioctl to get mmap offset */
3687 if (!dev->driver->dumb_map_offset)
3688 return -ENOSYS;
3689
3690 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3691}
3692
3693int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3694 void *data, struct drm_file *file_priv)
3695{
3696 struct drm_mode_destroy_dumb *args = data;
3697
3698 if (!dev->driver->dumb_destroy)
3699 return -ENOSYS;
3700
3701 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3702}
Dave Airlie248dbc22011-11-29 20:02:54 +00003703
3704/*
3705 * Just need to support RGB formats here for compat with code that doesn't
3706 * use pixel formats directly yet.
3707 */
3708void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3709 int *bpp)
3710{
3711 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003712 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003713 case DRM_FORMAT_RGB332:
3714 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003715 *depth = 8;
3716 *bpp = 8;
3717 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003718 case DRM_FORMAT_XRGB1555:
3719 case DRM_FORMAT_XBGR1555:
3720 case DRM_FORMAT_RGBX5551:
3721 case DRM_FORMAT_BGRX5551:
3722 case DRM_FORMAT_ARGB1555:
3723 case DRM_FORMAT_ABGR1555:
3724 case DRM_FORMAT_RGBA5551:
3725 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003726 *depth = 15;
3727 *bpp = 16;
3728 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003729 case DRM_FORMAT_RGB565:
3730 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003731 *depth = 16;
3732 *bpp = 16;
3733 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003734 case DRM_FORMAT_RGB888:
3735 case DRM_FORMAT_BGR888:
3736 *depth = 24;
3737 *bpp = 24;
3738 break;
3739 case DRM_FORMAT_XRGB8888:
3740 case DRM_FORMAT_XBGR8888:
3741 case DRM_FORMAT_RGBX8888:
3742 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003743 *depth = 24;
3744 *bpp = 32;
3745 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003746 case DRM_FORMAT_XRGB2101010:
3747 case DRM_FORMAT_XBGR2101010:
3748 case DRM_FORMAT_RGBX1010102:
3749 case DRM_FORMAT_BGRX1010102:
3750 case DRM_FORMAT_ARGB2101010:
3751 case DRM_FORMAT_ABGR2101010:
3752 case DRM_FORMAT_RGBA1010102:
3753 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003754 *depth = 30;
3755 *bpp = 32;
3756 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003757 case DRM_FORMAT_ARGB8888:
3758 case DRM_FORMAT_ABGR8888:
3759 case DRM_FORMAT_RGBA8888:
3760 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003761 *depth = 32;
3762 *bpp = 32;
3763 break;
3764 default:
3765 DRM_DEBUG_KMS("unsupported pixel format\n");
3766 *depth = 0;
3767 *bpp = 0;
3768 break;
3769 }
3770}
3771EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003772
3773/**
3774 * drm_format_num_planes - get the number of planes for format
3775 * @format: pixel format (DRM_FORMAT_*)
3776 *
3777 * RETURNS:
3778 * The number of planes used by the specified pixel format.
3779 */
3780int drm_format_num_planes(uint32_t format)
3781{
3782 switch (format) {
3783 case DRM_FORMAT_YUV410:
3784 case DRM_FORMAT_YVU410:
3785 case DRM_FORMAT_YUV411:
3786 case DRM_FORMAT_YVU411:
3787 case DRM_FORMAT_YUV420:
3788 case DRM_FORMAT_YVU420:
3789 case DRM_FORMAT_YUV422:
3790 case DRM_FORMAT_YVU422:
3791 case DRM_FORMAT_YUV444:
3792 case DRM_FORMAT_YVU444:
3793 return 3;
3794 case DRM_FORMAT_NV12:
3795 case DRM_FORMAT_NV21:
3796 case DRM_FORMAT_NV16:
3797 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003798 case DRM_FORMAT_NV24:
3799 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003800 return 2;
3801 default:
3802 return 1;
3803 }
3804}
3805EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003806
3807/**
3808 * drm_format_plane_cpp - determine the bytes per pixel value
3809 * @format: pixel format (DRM_FORMAT_*)
3810 * @plane: plane index
3811 *
3812 * RETURNS:
3813 * The bytes per pixel value for the specified plane.
3814 */
3815int drm_format_plane_cpp(uint32_t format, int plane)
3816{
3817 unsigned int depth;
3818 int bpp;
3819
3820 if (plane >= drm_format_num_planes(format))
3821 return 0;
3822
3823 switch (format) {
3824 case DRM_FORMAT_YUYV:
3825 case DRM_FORMAT_YVYU:
3826 case DRM_FORMAT_UYVY:
3827 case DRM_FORMAT_VYUY:
3828 return 2;
3829 case DRM_FORMAT_NV12:
3830 case DRM_FORMAT_NV21:
3831 case DRM_FORMAT_NV16:
3832 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003833 case DRM_FORMAT_NV24:
3834 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003835 return plane ? 2 : 1;
3836 case DRM_FORMAT_YUV410:
3837 case DRM_FORMAT_YVU410:
3838 case DRM_FORMAT_YUV411:
3839 case DRM_FORMAT_YVU411:
3840 case DRM_FORMAT_YUV420:
3841 case DRM_FORMAT_YVU420:
3842 case DRM_FORMAT_YUV422:
3843 case DRM_FORMAT_YVU422:
3844 case DRM_FORMAT_YUV444:
3845 case DRM_FORMAT_YVU444:
3846 return 1;
3847 default:
3848 drm_fb_get_bpp_depth(format, &depth, &bpp);
3849 return bpp >> 3;
3850 }
3851}
3852EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003853
3854/**
3855 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3856 * @format: pixel format (DRM_FORMAT_*)
3857 *
3858 * RETURNS:
3859 * The horizontal chroma subsampling factor for the
3860 * specified pixel format.
3861 */
3862int drm_format_horz_chroma_subsampling(uint32_t format)
3863{
3864 switch (format) {
3865 case DRM_FORMAT_YUV411:
3866 case DRM_FORMAT_YVU411:
3867 case DRM_FORMAT_YUV410:
3868 case DRM_FORMAT_YVU410:
3869 return 4;
3870 case DRM_FORMAT_YUYV:
3871 case DRM_FORMAT_YVYU:
3872 case DRM_FORMAT_UYVY:
3873 case DRM_FORMAT_VYUY:
3874 case DRM_FORMAT_NV12:
3875 case DRM_FORMAT_NV21:
3876 case DRM_FORMAT_NV16:
3877 case DRM_FORMAT_NV61:
3878 case DRM_FORMAT_YUV422:
3879 case DRM_FORMAT_YVU422:
3880 case DRM_FORMAT_YUV420:
3881 case DRM_FORMAT_YVU420:
3882 return 2;
3883 default:
3884 return 1;
3885 }
3886}
3887EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3888
3889/**
3890 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3891 * @format: pixel format (DRM_FORMAT_*)
3892 *
3893 * RETURNS:
3894 * The vertical chroma subsampling factor for the
3895 * specified pixel format.
3896 */
3897int drm_format_vert_chroma_subsampling(uint32_t format)
3898{
3899 switch (format) {
3900 case DRM_FORMAT_YUV410:
3901 case DRM_FORMAT_YVU410:
3902 return 4;
3903 case DRM_FORMAT_YUV420:
3904 case DRM_FORMAT_YVU420:
3905 case DRM_FORMAT_NV12:
3906 case DRM_FORMAT_NV21:
3907 return 2;
3908 default:
3909 return 1;
3910 }
3911}
3912EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003913
3914/**
3915 * drm_mode_config_init - initialize DRM mode_configuration structure
3916 * @dev: DRM device
3917 *
3918 * Initialize @dev's mode_config structure, used for tracking the graphics
3919 * configuration of @dev.
3920 *
3921 * Since this initializes the modeset locks, no locking is possible. Which is no
3922 * problem, since this should happen single threaded at init time. It is the
3923 * driver's problem to ensure this guarantee.
3924 *
3925 */
3926void drm_mode_config_init(struct drm_device *dev)
3927{
3928 mutex_init(&dev->mode_config.mutex);
3929 mutex_init(&dev->mode_config.idr_mutex);
3930 mutex_init(&dev->mode_config.fb_lock);
3931 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3932 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3933 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3934 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3935 INIT_LIST_HEAD(&dev->mode_config.property_list);
3936 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3937 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3938 idr_init(&dev->mode_config.crtc_idr);
3939
3940 drm_modeset_lock_all(dev);
3941 drm_mode_create_standard_connector_properties(dev);
3942 drm_modeset_unlock_all(dev);
3943
3944 /* Just to be sure */
3945 dev->mode_config.num_fb = 0;
3946 dev->mode_config.num_connector = 0;
3947 dev->mode_config.num_crtc = 0;
3948 dev->mode_config.num_encoder = 0;
3949}
3950EXPORT_SYMBOL(drm_mode_config_init);
3951
3952/**
3953 * drm_mode_config_cleanup - free up DRM mode_config info
3954 * @dev: DRM device
3955 *
3956 * Free up all the connectors and CRTCs associated with this DRM device, then
3957 * free up the framebuffers and associated buffer objects.
3958 *
3959 * Note that since this /should/ happen single-threaded at driver/device
3960 * teardown time, no locking is required. It's the driver's job to ensure that
3961 * this guarantee actually holds true.
3962 *
3963 * FIXME: cleanup any dangling user buffer objects too
3964 */
3965void drm_mode_config_cleanup(struct drm_device *dev)
3966{
3967 struct drm_connector *connector, *ot;
3968 struct drm_crtc *crtc, *ct;
3969 struct drm_encoder *encoder, *enct;
3970 struct drm_framebuffer *fb, *fbt;
3971 struct drm_property *property, *pt;
3972 struct drm_property_blob *blob, *bt;
3973 struct drm_plane *plane, *plt;
3974
3975 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3976 head) {
3977 encoder->funcs->destroy(encoder);
3978 }
3979
3980 list_for_each_entry_safe(connector, ot,
3981 &dev->mode_config.connector_list, head) {
3982 connector->funcs->destroy(connector);
3983 }
3984
3985 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3986 head) {
3987 drm_property_destroy(dev, property);
3988 }
3989
3990 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3991 head) {
3992 drm_property_destroy_blob(dev, blob);
3993 }
3994
3995 /*
3996 * Single-threaded teardown context, so it's not required to grab the
3997 * fb_lock to protect against concurrent fb_list access. Contrary, it
3998 * would actually deadlock with the drm_framebuffer_cleanup function.
3999 *
4000 * Also, if there are any framebuffers left, that's a driver leak now,
4001 * so politely WARN about this.
4002 */
4003 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4004 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4005 drm_framebuffer_remove(fb);
4006 }
4007
4008 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4009 head) {
4010 plane->funcs->destroy(plane);
4011 }
4012
4013 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4014 crtc->funcs->destroy(crtc);
4015 }
4016
4017 idr_destroy(&dev->mode_config.crtc_idr);
4018}
4019EXPORT_SYMBOL(drm_mode_config_cleanup);