blob: 5a88267fa8dd6597b307c550612f38a682ea9b52 [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>
Rob Clark51fd3712013-11-19 12:10:12 -050040#include <drm/drm_modeset_lock.h>
Dave Airlief453ba02008-11-07 14:05:41 -080041
Daniel Vetter8bd441b2014-01-23 15:35:24 +010042#include "drm_crtc_internal.h"
43
Matt Roperc394c2b2014-06-10 08:28:08 -070044static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
45 struct drm_mode_fb_cmd2 *r,
46 struct drm_file *file_priv);
47
Daniel Vetter84849902012-12-02 00:28:11 +010048/**
49 * drm_modeset_lock_all - take all modeset locks
50 * @dev: drm device
51 *
52 * This function takes all modeset locks, suitable where a more fine-grained
Daniel Vetterc8e32cc2014-03-10 21:33:02 +010053 * scheme isn't (yet) implemented. Locks must be dropped with
54 * drm_modeset_unlock_all.
Daniel Vetter84849902012-12-02 00:28:11 +010055 */
56void drm_modeset_lock_all(struct drm_device *dev)
57{
Rob Clark51fd3712013-11-19 12:10:12 -050058 struct drm_mode_config *config = &dev->mode_config;
59 struct drm_modeset_acquire_ctx *ctx;
60 int ret;
Daniel Vetter29494c12012-12-02 02:18:25 +010061
Rob Clark51fd3712013-11-19 12:10:12 -050062 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
63 if (WARN_ON(!ctx))
64 return;
Daniel Vetter29494c12012-12-02 02:18:25 +010065
Rob Clark51fd3712013-11-19 12:10:12 -050066 mutex_lock(&config->mutex);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020067
Rob Clark51fd3712013-11-19 12:10:12 -050068 drm_modeset_acquire_init(ctx, 0);
69
70retry:
71 ret = drm_modeset_lock(&config->connection_mutex, ctx);
72 if (ret)
73 goto fail;
74 ret = drm_modeset_lock_all_crtcs(dev, ctx);
75 if (ret)
76 goto fail;
77
78 WARN_ON(config->acquire_ctx);
79
80 /* now we hold the locks, so now that it is safe, stash the
81 * ctx for drm_modeset_unlock_all():
82 */
83 config->acquire_ctx = ctx;
84
85 drm_warn_on_modeset_not_all_locked(dev);
86
87 return;
88
89fail:
90 if (ret == -EDEADLK) {
91 drm_modeset_backoff(ctx);
92 goto retry;
93 }
Daniel Vetter84849902012-12-02 00:28:11 +010094}
95EXPORT_SYMBOL(drm_modeset_lock_all);
96
97/**
98 * drm_modeset_unlock_all - drop all modeset locks
99 * @dev: device
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100100 *
101 * This function drop all modeset locks taken by drm_modeset_lock_all.
Daniel Vetter84849902012-12-02 00:28:11 +0100102 */
103void drm_modeset_unlock_all(struct drm_device *dev)
104{
Rob Clark51fd3712013-11-19 12:10:12 -0500105 struct drm_mode_config *config = &dev->mode_config;
106 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter29494c12012-12-02 02:18:25 +0100107
Rob Clark51fd3712013-11-19 12:10:12 -0500108 if (WARN_ON(!ctx))
109 return;
Daniel Vetter29494c12012-12-02 02:18:25 +0100110
Rob Clark51fd3712013-11-19 12:10:12 -0500111 config->acquire_ctx = NULL;
112 drm_modeset_drop_locks(ctx);
113 drm_modeset_acquire_fini(ctx);
114
115 kfree(ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +0200116
Daniel Vetter84849902012-12-02 00:28:11 +0100117 mutex_unlock(&dev->mode_config.mutex);
118}
119EXPORT_SYMBOL(drm_modeset_unlock_all);
120
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100121/**
122 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
123 * @dev: device
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100124 *
125 * Useful as a debug assert.
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100126 */
127void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
128{
129 struct drm_crtc *crtc;
130
Daniel Vettera9b054e2013-05-02 09:43:05 +0200131 /* Locking is currently fubar in the panic handler. */
132 if (oops_in_progress)
133 return;
134
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100135 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Rob Clark51fd3712013-11-19 12:10:12 -0500136 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100137
Rob Clark51fd3712013-11-19 12:10:12 -0500138 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100139 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
140}
141EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
142
Dave Airlief453ba02008-11-07 14:05:41 -0800143/* Avoid boilerplate. I'm tired of typing. */
144#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000145 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -0800146 { \
147 int i; \
148 for (i = 0; i < ARRAY_SIZE(list); i++) { \
149 if (list[i].type == val) \
150 return list[i].name; \
151 } \
152 return "(unknown)"; \
153 }
154
155/*
156 * Global properties
157 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000158static const struct drm_prop_enum_list drm_dpms_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800159{ { DRM_MODE_DPMS_ON, "On" },
160 { DRM_MODE_DPMS_STANDBY, "Standby" },
161 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
162 { DRM_MODE_DPMS_OFF, "Off" }
163};
164
165DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
166
Rob Clark9922ab52014-04-01 20:16:57 -0400167static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
168{
169 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
170 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
171 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
172};
173
Dave Airlief453ba02008-11-07 14:05:41 -0800174/*
175 * Optional properties
176 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000177static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800178{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700179 { DRM_MODE_SCALE_NONE, "None" },
180 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
181 { DRM_MODE_SCALE_CENTER, "Center" },
182 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800183};
184
Dave Airlief453ba02008-11-07 14:05:41 -0800185/*
186 * Non-global properties, but "required" for certain connectors.
187 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000188static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800189{
190 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
191 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
192 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
193};
194
195DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
196
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000197static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800198{
199 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
200 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
201 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
202};
203
204DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
205 drm_dvi_i_subconnector_enum_list)
206
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000207static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800208{
209 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
210 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
211 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
212 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200213 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800214};
215
216DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
217
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000218static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800219{
220 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
221 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
222 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
223 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200224 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800225};
226
227DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
228 drm_tv_subconnector_enum_list)
229
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000230static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000231 { DRM_MODE_DIRTY_OFF, "Off" },
232 { DRM_MODE_DIRTY_ON, "On" },
233 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
234};
235
Dave Airlief453ba02008-11-07 14:05:41 -0800236struct drm_conn_prop_enum_list {
237 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000238 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400239 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800240};
241
242/*
243 * Connector and encoder types.
244 */
245static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400246{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
247 { DRM_MODE_CONNECTOR_VGA, "VGA" },
248 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
249 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
250 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
251 { DRM_MODE_CONNECTOR_Composite, "Composite" },
252 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
253 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
254 { DRM_MODE_CONNECTOR_Component, "Component" },
255 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
256 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
257 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
258 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
259 { DRM_MODE_CONNECTOR_TV, "TV" },
260 { DRM_MODE_CONNECTOR_eDP, "eDP" },
261 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300262 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800263};
264
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000265static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800266{ { DRM_MODE_ENCODER_NONE, "None" },
267 { DRM_MODE_ENCODER_DAC, "DAC" },
268 { DRM_MODE_ENCODER_TMDS, "TMDS" },
269 { DRM_MODE_ENCODER_LVDS, "LVDS" },
270 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200271 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300272 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000273 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800274};
275
Jesse Barnesac1bb362014-02-10 15:32:44 -0800276static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
277{
278 { SubPixelUnknown, "Unknown" },
279 { SubPixelHorizontalRGB, "Horizontal RGB" },
280 { SubPixelHorizontalBGR, "Horizontal BGR" },
281 { SubPixelVerticalRGB, "Vertical RGB" },
282 { SubPixelVerticalBGR, "Vertical BGR" },
283 { SubPixelNone, "None" },
284};
285
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400286void drm_connector_ida_init(void)
287{
288 int i;
289
290 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
291 ida_init(&drm_connector_enum_list[i].ida);
292}
293
294void drm_connector_ida_destroy(void)
295{
296 int i;
297
298 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
299 ida_destroy(&drm_connector_enum_list[i].ida);
300}
301
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100302/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100303 * drm_get_connector_status_name - return a string for connector status
304 * @status: connector status to compute name of
305 *
306 * In contrast to the other drm_get_*_name functions this one here returns a
307 * const pointer and hence is threadsafe.
308 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000309const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800310{
311 if (status == connector_status_connected)
312 return "connected";
313 else if (status == connector_status_disconnected)
314 return "disconnected";
315 else
316 return "unknown";
317}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000318EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800319
Jesse Barnesac1bb362014-02-10 15:32:44 -0800320/**
321 * drm_get_subpixel_order_name - return a string for a given subpixel enum
322 * @order: enum of subpixel_order
323 *
324 * Note you could abuse this and return something out of bounds, but that
325 * would be a caller error. No unscrubbed user data should make it here.
326 */
327const char *drm_get_subpixel_order_name(enum subpixel_order order)
328{
329 return drm_subpixel_enum_list[order].name;
330}
331EXPORT_SYMBOL(drm_get_subpixel_order_name);
332
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300333static char printable_char(int c)
334{
335 return isascii(c) && isprint(c) ? c : '?';
336}
337
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100338/**
339 * drm_get_format_name - return a string for drm fourcc format
340 * @format: format to compute name of
341 *
342 * Note that the buffer used by this function is globally shared and owned by
343 * the function itself.
344 *
345 * FIXME: This isn't really multithreading safe.
346 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000347const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300348{
349 static char buf[32];
350
351 snprintf(buf, sizeof(buf),
352 "%c%c%c%c %s-endian (0x%08x)",
353 printable_char(format & 0xff),
354 printable_char((format >> 8) & 0xff),
355 printable_char((format >> 16) & 0xff),
356 printable_char((format >> 24) & 0x7f),
357 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
358 format);
359
360 return buf;
361}
362EXPORT_SYMBOL(drm_get_format_name);
363
Dave Airlief453ba02008-11-07 14:05:41 -0800364/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100365 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800366 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100367 * @obj: object pointer, used to generate unique ID
368 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800369 *
Dave Airlief453ba02008-11-07 14:05:41 -0800370 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100371 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
372 * modeset identifiers are _not_ reference counted. Hence don't use this for
373 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800374 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100375 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800376 * New unique (relative to other objects in @dev) integer identifier for the
377 * object.
378 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100379int drm_mode_object_get(struct drm_device *dev,
380 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800381{
Dave Airlief453ba02008-11-07 14:05:41 -0800382 int ret;
383
Jesse Barnesad2563c2009-01-19 17:21:45 +1000384 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800385 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
386 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100387 /*
388 * Set up the object linking under the protection of the idr
389 * lock so that other users can't see inconsistent state.
390 */
Tejun Heo2e928812013-02-27 17:04:08 -0800391 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100392 obj->type = obj_type;
393 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000394 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100395
Tejun Heo2e928812013-02-27 17:04:08 -0800396 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800397}
398
399/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100400 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800401 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100402 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800403 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100404 * Free @id from @dev's unique identifier pool. Note that despite the _get
405 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
406 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800407 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100408void drm_mode_object_put(struct drm_device *dev,
409 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800410{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000411 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800412 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000413 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800414}
415
Rob Clark98f75de2014-05-30 11:37:03 -0400416static struct drm_mode_object *_object_find(struct drm_device *dev,
417 uint32_t id, uint32_t type)
418{
419 struct drm_mode_object *obj = NULL;
420
421 mutex_lock(&dev->mode_config.idr_mutex);
422 obj = idr_find(&dev->mode_config.crtc_idr, id);
423 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
424 (obj->id != id))
425 obj = NULL;
426 mutex_unlock(&dev->mode_config.idr_mutex);
427
428 return obj;
429}
430
Daniel Vetter786b99e2012-12-02 21:53:40 +0100431/**
432 * drm_mode_object_find - look up a drm object with static lifetime
433 * @dev: drm device
434 * @id: id of the mode object
435 * @type: type of the mode object
436 *
437 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400438 * are reference counted, they need special treatment. Even with
439 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
440 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100441 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200442struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
443 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800444{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000445 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800446
Daniel Vetter786b99e2012-12-02 21:53:40 +0100447 /* Framebuffers are reference counted and need their own lookup
448 * function.*/
449 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400450 obj = _object_find(dev, id, type);
451 /* don't leak out unref'd fb's */
452 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000453 obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800454 return obj;
455}
456EXPORT_SYMBOL(drm_mode_object_find);
457
458/**
Dave Airlief453ba02008-11-07 14:05:41 -0800459 * drm_framebuffer_init - initialize a framebuffer
460 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100461 * @fb: framebuffer to be initialized
462 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800463 *
Dave Airlief453ba02008-11-07 14:05:41 -0800464 * Allocates an ID for the framebuffer's parent mode object, sets its mode
465 * functions & device file and adds it to the master fd list.
466 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100467 * IMPORTANT:
468 * This functions publishes the fb and makes it available for concurrent access
469 * by other users. Which means by this point the fb _must_ be fully set up -
470 * since all the fb attributes are invariant over its lifetime, no further
471 * locking but only correct reference counting is required.
472 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100473 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200474 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800475 */
476int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
477 const struct drm_framebuffer_funcs *funcs)
478{
479 int ret;
480
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100481 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000482 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100483 INIT_LIST_HEAD(&fb->filp_head);
484 fb->dev = dev;
485 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000486
Dave Airlief453ba02008-11-07 14:05:41 -0800487 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200488 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100489 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800490
Daniel Vetter2b677e82012-12-10 21:16:05 +0100491 /* Grab the idr reference. */
492 drm_framebuffer_reference(fb);
493
Dave Airlief453ba02008-11-07 14:05:41 -0800494 dev->mode_config.num_fb++;
495 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100496out:
497 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800498
499 return 0;
500}
501EXPORT_SYMBOL(drm_framebuffer_init);
502
Rob Clarkf7eff602012-09-05 21:48:38 +0000503static void drm_framebuffer_free(struct kref *kref)
504{
505 struct drm_framebuffer *fb =
506 container_of(kref, struct drm_framebuffer, refcount);
507 fb->funcs->destroy(fb);
508}
509
Daniel Vetter2b677e82012-12-10 21:16:05 +0100510static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
511 uint32_t id)
512{
513 struct drm_mode_object *obj = NULL;
514 struct drm_framebuffer *fb;
515
516 mutex_lock(&dev->mode_config.idr_mutex);
517 obj = idr_find(&dev->mode_config.crtc_idr, id);
518 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
519 fb = NULL;
520 else
521 fb = obj_to_fb(obj);
522 mutex_unlock(&dev->mode_config.idr_mutex);
523
524 return fb;
525}
526
Rob Clarkf7eff602012-09-05 21:48:38 +0000527/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100528 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
529 * @dev: drm device
530 * @id: id of the fb object
531 *
532 * If successful, this grabs an additional reference to the framebuffer -
533 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100534 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100535 */
536struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
537 uint32_t id)
538{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100539 struct drm_framebuffer *fb;
540
541 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100542 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100543 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000544 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100545 mutex_unlock(&dev->mode_config.fb_lock);
546
547 return fb;
548}
549EXPORT_SYMBOL(drm_framebuffer_lookup);
550
551/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000552 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100553 * @fb: framebuffer to unref
554 *
555 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000556 */
557void drm_framebuffer_unreference(struct drm_framebuffer *fb)
558{
Rob Clark82912722014-03-18 10:07:08 -0400559 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000560 kref_put(&fb->refcount, drm_framebuffer_free);
561}
562EXPORT_SYMBOL(drm_framebuffer_unreference);
563
564/**
565 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100566 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100567 *
568 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000569 */
570void drm_framebuffer_reference(struct drm_framebuffer *fb)
571{
Rob Clark82912722014-03-18 10:07:08 -0400572 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000573 kref_get(&fb->refcount);
574}
575EXPORT_SYMBOL(drm_framebuffer_reference);
576
Daniel Vetter2b677e82012-12-10 21:16:05 +0100577static void drm_framebuffer_free_bug(struct kref *kref)
578{
579 BUG();
580}
581
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100582static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
583{
Rob Clark82912722014-03-18 10:07:08 -0400584 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100585 kref_put(&fb->refcount, drm_framebuffer_free_bug);
586}
587
Daniel Vetter2b677e82012-12-10 21:16:05 +0100588/* dev->mode_config.fb_lock must be held! */
589static void __drm_framebuffer_unregister(struct drm_device *dev,
590 struct drm_framebuffer *fb)
591{
592 mutex_lock(&dev->mode_config.idr_mutex);
593 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
594 mutex_unlock(&dev->mode_config.idr_mutex);
595
596 fb->base.id = 0;
597
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100598 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100599}
600
Dave Airlief453ba02008-11-07 14:05:41 -0800601/**
Daniel Vetter36206362012-12-10 20:42:17 +0100602 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
603 * @fb: fb to unregister
604 *
605 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
606 * those used for fbdev. Note that the caller must hold a reference of it's own,
607 * i.e. the object may not be destroyed through this call (since it'll lead to a
608 * locking inversion).
609 */
610void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
611{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100612 struct drm_device *dev = fb->dev;
613
614 mutex_lock(&dev->mode_config.fb_lock);
615 /* Mark fb as reaped and drop idr ref. */
616 __drm_framebuffer_unregister(dev, fb);
617 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100618}
619EXPORT_SYMBOL(drm_framebuffer_unregister_private);
620
621/**
Dave Airlief453ba02008-11-07 14:05:41 -0800622 * drm_framebuffer_cleanup - remove a framebuffer object
623 * @fb: framebuffer to remove
624 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100625 * Cleanup framebuffer. This function is intended to be used from the drivers
626 * ->destroy callback. It can also be used to clean up driver private
627 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100628 *
629 * Note that this function does not remove the fb from active usuage - if it is
630 * still used anywhere, hilarity can ensue since userspace could call getfb on
631 * the id and get back -EINVAL. Obviously no concern at driver unload time.
632 *
633 * Also, the framebuffer will not be removed from the lookup idr - for
634 * user-created framebuffers this will happen in in the rmfb ioctl. For
635 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
636 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800637 */
638void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
639{
640 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100641
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100642 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000643 list_del(&fb->head);
644 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100645 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000646}
647EXPORT_SYMBOL(drm_framebuffer_cleanup);
648
649/**
650 * drm_framebuffer_remove - remove and unreference a framebuffer object
651 * @fb: framebuffer to remove
652 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000653 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100654 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100655 * passed-in framebuffer. Might take the modeset locks.
656 *
657 * Note that this function optimizes the cleanup away if the caller holds the
658 * last reference to the framebuffer. It is also guaranteed to not take the
659 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000660 */
661void drm_framebuffer_remove(struct drm_framebuffer *fb)
662{
663 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800664 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800665 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000666 struct drm_mode_set set;
667 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800668
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100669 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100670
Daniel Vetterb62584e2012-12-11 16:51:35 +0100671 /*
672 * drm ABI mandates that we remove any deleted framebuffers from active
673 * useage. But since most sane clients only remove framebuffers they no
674 * longer need, try to optimize this away.
675 *
676 * Since we're holding a reference ourselves, observing a refcount of 1
677 * means that we're the last holder and can skip it. Also, the refcount
678 * can never increase from 1 again, so we don't need any barriers or
679 * locks.
680 *
681 * Note that userspace could try to race with use and instate a new
682 * usage _after_ we've cleared all current ones. End result will be an
683 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
684 * in this manner.
685 */
686 if (atomic_read(&fb->refcount.refcount) > 1) {
687 drm_modeset_lock_all(dev);
688 /* remove from any CRTC */
689 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700690 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100691 /* should turn off the crtc */
692 memset(&set, 0, sizeof(struct drm_mode_set));
693 set.crtc = crtc;
694 set.fb = NULL;
695 ret = drm_mode_set_config_internal(&set);
696 if (ret)
697 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
698 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000699 }
Dave Airlief453ba02008-11-07 14:05:41 -0800700
Daniel Vetterb62584e2012-12-11 16:51:35 +0100701 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300702 if (plane->fb == fb)
703 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800704 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100705 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800706 }
707
Rob Clarkf7eff602012-09-05 21:48:38 +0000708 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800709}
Rob Clarkf7eff602012-09-05 21:48:38 +0000710EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800711
Rob Clark51fd3712013-11-19 12:10:12 -0500712DEFINE_WW_CLASS(crtc_ww_class);
713
Dave Airlief453ba02008-11-07 14:05:41 -0800714/**
Matt Ropere13161a2014-04-01 15:22:38 -0700715 * drm_crtc_init_with_planes - Initialise a new CRTC object with
716 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800717 * @dev: DRM device
718 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700719 * @primary: Primary plane for CRTC
720 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800721 * @funcs: callbacks for the new CRTC
722 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000723 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200724 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100725 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200726 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800727 */
Matt Ropere13161a2014-04-01 15:22:38 -0700728int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
729 struct drm_plane *primary,
730 void *cursor,
731 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800732{
Rob Clark51fd3712013-11-19 12:10:12 -0500733 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200734 int ret;
735
Dave Airlief453ba02008-11-07 14:05:41 -0800736 crtc->dev = dev;
737 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000738 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800739
Daniel Vetter84849902012-12-02 00:28:11 +0100740 drm_modeset_lock_all(dev);
Rob Clark51fd3712013-11-19 12:10:12 -0500741 drm_modeset_lock_init(&crtc->mutex);
742 /* dropped by _unlock_all(): */
743 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200744
745 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
746 if (ret)
747 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800748
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300749 crtc->base.properties = &crtc->properties;
750
Rob Clark51fd3712013-11-19 12:10:12 -0500751 list_add_tail(&crtc->head, &config->crtc_list);
752 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200753
Matt Ropere13161a2014-04-01 15:22:38 -0700754 crtc->primary = primary;
755 if (primary)
756 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
757
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200758 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100759 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200760
761 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800762}
Matt Ropere13161a2014-04-01 15:22:38 -0700763EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800764
765/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000766 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800767 * @crtc: CRTC to cleanup
768 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000769 * This function cleans up @crtc and removes it from the DRM mode setting
770 * core. Note that the function does *not* free the crtc structure itself,
771 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800772 */
773void drm_crtc_cleanup(struct drm_crtc *crtc)
774{
775 struct drm_device *dev = crtc->dev;
776
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000777 kfree(crtc->gamma_store);
778 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800779
Rob Clark51fd3712013-11-19 12:10:12 -0500780 drm_modeset_lock_fini(&crtc->mutex);
781
Dave Airlief453ba02008-11-07 14:05:41 -0800782 drm_mode_object_put(dev, &crtc->base);
783 list_del(&crtc->head);
784 dev->mode_config.num_crtc--;
785}
786EXPORT_SYMBOL(drm_crtc_cleanup);
787
788/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000789 * drm_crtc_index - find the index of a registered CRTC
790 * @crtc: CRTC to find index for
791 *
792 * Given a registered CRTC, return the index of that CRTC within a DRM
793 * device's list of CRTCs.
794 */
795unsigned int drm_crtc_index(struct drm_crtc *crtc)
796{
797 unsigned int index = 0;
798 struct drm_crtc *tmp;
799
800 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
801 if (tmp == crtc)
802 return index;
803
804 index++;
805 }
806
807 BUG();
808}
809EXPORT_SYMBOL(drm_crtc_index);
810
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100811/*
Dave Airlief453ba02008-11-07 14:05:41 -0800812 * drm_mode_remove - remove and free a mode
813 * @connector: connector list to modify
814 * @mode: mode to remove
815 *
Dave Airlief453ba02008-11-07 14:05:41 -0800816 * Remove @mode from @connector's mode list, then free it.
817 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100818static void drm_mode_remove(struct drm_connector *connector,
819 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800820{
821 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100822 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800823}
Dave Airlief453ba02008-11-07 14:05:41 -0800824
825/**
826 * drm_connector_init - Init a preallocated connector
827 * @dev: DRM device
828 * @connector: the connector to init
829 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100830 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800831 *
Dave Airlief453ba02008-11-07 14:05:41 -0800832 * Initialises a preallocated connector. Connectors should be
833 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200834 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100835 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200836 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800837 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200838int drm_connector_init(struct drm_device *dev,
839 struct drm_connector *connector,
840 const struct drm_connector_funcs *funcs,
841 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800842{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200843 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400844 struct ida *connector_ida =
845 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200846
Daniel Vetter84849902012-12-02 00:28:11 +0100847 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800848
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200849 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
850 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300851 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200852
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300853 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800854 connector->dev = dev;
855 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800856 connector->connector_type = connector_type;
857 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400858 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
859 if (connector->connector_type_id < 0) {
860 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300861 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400862 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300863 connector->name =
864 kasprintf(GFP_KERNEL, "%s-%d",
865 drm_connector_enum_list[connector_type].name,
866 connector->connector_type_id);
867 if (!connector->name) {
868 ret = -ENOMEM;
869 goto out_put;
870 }
871
Dave Airlief453ba02008-11-07 14:05:41 -0800872 INIT_LIST_HEAD(&connector->probed_modes);
873 INIT_LIST_HEAD(&connector->modes);
874 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000875 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800876
877 list_add_tail(&connector->head, &dev->mode_config.connector_list);
878 dev->mode_config.num_connector++;
879
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200880 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500881 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200882 dev->mode_config.edid_property,
883 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800884
Rob Clark58495562012-10-11 20:50:56 -0500885 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800886 dev->mode_config.dpms_property, 0);
887
Jani Nikula2abdd312014-05-14 16:58:19 +0300888out_put:
889 if (ret)
890 drm_mode_object_put(dev, &connector->base);
891
892out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100893 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200894
895 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800896}
897EXPORT_SYMBOL(drm_connector_init);
898
899/**
900 * drm_connector_cleanup - cleans up an initialised connector
901 * @connector: connector to cleanup
902 *
Dave Airlief453ba02008-11-07 14:05:41 -0800903 * Cleans up the connector but doesn't free the object.
904 */
905void drm_connector_cleanup(struct drm_connector *connector)
906{
907 struct drm_device *dev = connector->dev;
908 struct drm_display_mode *mode, *t;
909
910 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
911 drm_mode_remove(connector, mode);
912
913 list_for_each_entry_safe(mode, t, &connector->modes, head)
914 drm_mode_remove(connector, mode);
915
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400916 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
917 connector->connector_type_id);
918
Dave Airlief453ba02008-11-07 14:05:41 -0800919 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300920 kfree(connector->name);
921 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800922 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000923 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800924}
925EXPORT_SYMBOL(drm_connector_cleanup);
926
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100927/**
928 * drm_connector_unplug_all - unregister connector userspace interfaces
929 * @dev: drm device
930 *
931 * This function unregisters all connector userspace interfaces in sysfs. Should
932 * be call when the device is disconnected, e.g. from an usb driver's
933 * ->disconnect callback.
934 */
Dave Airliecbc7e222012-02-20 14:16:40 +0000935void drm_connector_unplug_all(struct drm_device *dev)
936{
937 struct drm_connector *connector;
938
939 /* taking the mode config mutex ends up in a clash with sysfs */
940 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
941 drm_sysfs_connector_remove(connector);
942
943}
944EXPORT_SYMBOL(drm_connector_unplug_all);
945
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100946/**
947 * drm_bridge_init - initialize a drm transcoder/bridge
948 * @dev: drm device
949 * @bridge: transcoder/bridge to set up
950 * @funcs: bridge function table
951 *
952 * Initialises a preallocated bridge. Bridges should be
953 * subclassed as part of driver connector objects.
954 *
955 * Returns:
956 * Zero on success, error code on failure.
957 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400958int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
959 const struct drm_bridge_funcs *funcs)
960{
961 int ret;
962
963 drm_modeset_lock_all(dev);
964
965 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
966 if (ret)
967 goto out;
968
969 bridge->dev = dev;
970 bridge->funcs = funcs;
971
972 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
973 dev->mode_config.num_bridge++;
974
975 out:
976 drm_modeset_unlock_all(dev);
977 return ret;
978}
979EXPORT_SYMBOL(drm_bridge_init);
980
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100981/**
982 * drm_bridge_cleanup - cleans up an initialised bridge
983 * @bridge: bridge to cleanup
984 *
985 * Cleans up the bridge but doesn't free the object.
986 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400987void drm_bridge_cleanup(struct drm_bridge *bridge)
988{
989 struct drm_device *dev = bridge->dev;
990
991 drm_modeset_lock_all(dev);
992 drm_mode_object_put(dev, &bridge->base);
993 list_del(&bridge->head);
994 dev->mode_config.num_bridge--;
995 drm_modeset_unlock_all(dev);
996}
997EXPORT_SYMBOL(drm_bridge_cleanup);
998
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100999/**
1000 * drm_encoder_init - Init a preallocated encoder
1001 * @dev: drm device
1002 * @encoder: the encoder to init
1003 * @funcs: callbacks for this encoder
1004 * @encoder_type: user visible type of the encoder
1005 *
1006 * Initialises a preallocated encoder. Encoder should be
1007 * subclassed as part of driver encoder objects.
1008 *
1009 * Returns:
1010 * Zero on success, error code on failure.
1011 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001012int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001013 struct drm_encoder *encoder,
1014 const struct drm_encoder_funcs *funcs,
1015 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001016{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001017 int ret;
1018
Daniel Vetter84849902012-12-02 00:28:11 +01001019 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001020
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001021 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1022 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001023 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001024
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001025 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001026 encoder->encoder_type = encoder_type;
1027 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001028 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1029 drm_encoder_enum_list[encoder_type].name,
1030 encoder->base.id);
1031 if (!encoder->name) {
1032 ret = -ENOMEM;
1033 goto out_put;
1034 }
Dave Airlief453ba02008-11-07 14:05:41 -08001035
1036 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1037 dev->mode_config.num_encoder++;
1038
Jani Nikulae5748942014-05-14 16:58:20 +03001039out_put:
1040 if (ret)
1041 drm_mode_object_put(dev, &encoder->base);
1042
1043out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001044 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001045
1046 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001047}
1048EXPORT_SYMBOL(drm_encoder_init);
1049
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001050/**
1051 * drm_encoder_cleanup - cleans up an initialised encoder
1052 * @encoder: encoder to cleanup
1053 *
1054 * Cleans up the encoder but doesn't free the object.
1055 */
Dave Airlief453ba02008-11-07 14:05:41 -08001056void drm_encoder_cleanup(struct drm_encoder *encoder)
1057{
1058 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001059 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001060 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001061 kfree(encoder->name);
1062 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001063 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001064 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001065 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001066}
1067EXPORT_SYMBOL(drm_encoder_cleanup);
1068
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001069/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001070 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001071 * @dev: DRM device
1072 * @plane: plane object to init
1073 * @possible_crtcs: bitmask of possible CRTCs
1074 * @funcs: callbacks for the new plane
1075 * @formats: array of supported formats (%DRM_FORMAT_*)
1076 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001077 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001078 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001079 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001080 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001081 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001082 * Zero on success, error code on failure.
1083 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001084int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1085 unsigned long possible_crtcs,
1086 const struct drm_plane_funcs *funcs,
1087 const uint32_t *formats, uint32_t format_count,
1088 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001089{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001090 int ret;
1091
Daniel Vetter84849902012-12-02 00:28:11 +01001092 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001093
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001094 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1095 if (ret)
1096 goto out;
1097
Rob Clark4d939142012-05-17 02:23:27 -06001098 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001099 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001100 plane->funcs = funcs;
1101 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1102 GFP_KERNEL);
1103 if (!plane->format_types) {
1104 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1105 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001106 ret = -ENOMEM;
1107 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001108 }
1109
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001110 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001111 plane->format_count = format_count;
1112 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001113 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001114
Matt Roperdc415ff2014-04-01 15:22:36 -07001115 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1116 dev->mode_config.num_total_plane++;
1117 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1118 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001119
Rob Clark9922ab52014-04-01 20:16:57 -04001120 drm_object_attach_property(&plane->base,
1121 dev->mode_config.plane_type_property,
1122 plane->type);
1123
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001124 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001125 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001126
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001127 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001128}
Matt Roperdc415ff2014-04-01 15:22:36 -07001129EXPORT_SYMBOL(drm_universal_plane_init);
1130
1131/**
1132 * drm_plane_init - Initialize a legacy plane
1133 * @dev: DRM device
1134 * @plane: plane object to init
1135 * @possible_crtcs: bitmask of possible CRTCs
1136 * @funcs: callbacks for the new plane
1137 * @formats: array of supported formats (%DRM_FORMAT_*)
1138 * @format_count: number of elements in @formats
1139 * @is_primary: plane type (primary vs overlay)
1140 *
1141 * Legacy API to initialize a DRM plane.
1142 *
1143 * New drivers should call drm_universal_plane_init() instead.
1144 *
1145 * Returns:
1146 * Zero on success, error code on failure.
1147 */
1148int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1149 unsigned long possible_crtcs,
1150 const struct drm_plane_funcs *funcs,
1151 const uint32_t *formats, uint32_t format_count,
1152 bool is_primary)
1153{
1154 enum drm_plane_type type;
1155
1156 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1157 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1158 formats, format_count, type);
1159}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001160EXPORT_SYMBOL(drm_plane_init);
1161
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001162/**
1163 * drm_plane_cleanup - Clean up the core plane usage
1164 * @plane: plane to cleanup
1165 *
1166 * This function cleans up @plane and removes it from the DRM mode setting
1167 * core. Note that the function does *not* free the plane structure itself,
1168 * this is the responsibility of the caller.
1169 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001170void drm_plane_cleanup(struct drm_plane *plane)
1171{
1172 struct drm_device *dev = plane->dev;
1173
Daniel Vetter84849902012-12-02 00:28:11 +01001174 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001175 kfree(plane->format_types);
1176 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001177
1178 BUG_ON(list_empty(&plane->head));
1179
1180 list_del(&plane->head);
1181 dev->mode_config.num_total_plane--;
1182 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1183 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001184 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001185}
1186EXPORT_SYMBOL(drm_plane_cleanup);
1187
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001188/**
1189 * drm_plane_force_disable - Forcibly disable a plane
1190 * @plane: plane to disable
1191 *
1192 * Forces the plane to be disabled.
1193 *
1194 * Used when the plane's current framebuffer is destroyed,
1195 * and when restoring fbdev mode.
1196 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001197void drm_plane_force_disable(struct drm_plane *plane)
1198{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001199 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001200 int ret;
1201
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001202 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001203 return;
1204
1205 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001206 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001207 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001208 return;
1209 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001210 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001211 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001212 plane->fb = NULL;
1213 plane->crtc = NULL;
1214}
1215EXPORT_SYMBOL(drm_plane_force_disable);
1216
Dave Airlief453ba02008-11-07 14:05:41 -08001217static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1218{
1219 struct drm_property *edid;
1220 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001221
1222 /*
1223 * Standard properties (apply to all connectors)
1224 */
1225 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1226 DRM_MODE_PROP_IMMUTABLE,
1227 "EDID", 0);
1228 dev->mode_config.edid_property = edid;
1229
Sascha Hauer4a67d392012-02-06 10:58:17 +01001230 dpms = drm_property_create_enum(dev, 0,
1231 "DPMS", drm_dpms_enum_list,
1232 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001233 dev->mode_config.dpms_property = dpms;
1234
1235 return 0;
1236}
1237
Rob Clark9922ab52014-04-01 20:16:57 -04001238static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1239{
1240 struct drm_property *type;
1241
1242 /*
1243 * Standard properties (apply to all planes)
1244 */
1245 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1246 "type", drm_plane_type_enum_list,
1247 ARRAY_SIZE(drm_plane_type_enum_list));
1248 dev->mode_config.plane_type_property = type;
1249
1250 return 0;
1251}
1252
Dave Airlief453ba02008-11-07 14:05:41 -08001253/**
1254 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1255 * @dev: DRM device
1256 *
1257 * Called by a driver the first time a DVI-I connector is made.
1258 */
1259int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1260{
1261 struct drm_property *dvi_i_selector;
1262 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001263
1264 if (dev->mode_config.dvi_i_select_subconnector_property)
1265 return 0;
1266
1267 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001268 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001269 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001270 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001271 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001272 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1273
Sascha Hauer4a67d392012-02-06 10:58:17 +01001274 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001275 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001276 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001277 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001278 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1279
1280 return 0;
1281}
1282EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1283
1284/**
1285 * drm_create_tv_properties - create TV specific connector properties
1286 * @dev: DRM device
1287 * @num_modes: number of different TV formats (modes) supported
1288 * @modes: array of pointers to strings containing name of each format
1289 *
1290 * Called by a driver's TV initialization routine, this function creates
1291 * the TV specific connector properties for a given device. Caller is
1292 * responsible for allocating a list of format names and passing them to
1293 * this routine.
1294 */
1295int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1296 char *modes[])
1297{
1298 struct drm_property *tv_selector;
1299 struct drm_property *tv_subconnector;
1300 int i;
1301
1302 if (dev->mode_config.tv_select_subconnector_property)
1303 return 0;
1304
1305 /*
1306 * Basic connector properties
1307 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001308 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001309 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001310 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001311 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001312 dev->mode_config.tv_select_subconnector_property = tv_selector;
1313
1314 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001315 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1316 "subconnector",
1317 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001318 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001319 dev->mode_config.tv_subconnector_property = tv_subconnector;
1320
1321 /*
1322 * Other, TV specific properties: margins & TV modes.
1323 */
1324 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001325 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001326
1327 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001328 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001329
1330 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001331 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001332
1333 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001334 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001335
1336 dev->mode_config.tv_mode_property =
1337 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1338 "mode", num_modes);
1339 for (i = 0; i < num_modes; i++)
1340 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1341 i, modes[i]);
1342
Francisco Jerezb6b79022009-08-02 04:19:20 +02001343 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001344 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001345
1346 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001347 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001348
1349 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001350 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001351
Francisco Jereza75f0232009-08-12 02:30:10 +02001352 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001353 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001354
1355 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001356 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001357
1358 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001359 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001360
Dave Airlief453ba02008-11-07 14:05:41 -08001361 return 0;
1362}
1363EXPORT_SYMBOL(drm_mode_create_tv_properties);
1364
1365/**
1366 * drm_mode_create_scaling_mode_property - create scaling mode property
1367 * @dev: DRM device
1368 *
1369 * Called by a driver the first time it's needed, must be attached to desired
1370 * connectors.
1371 */
1372int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1373{
1374 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001375
1376 if (dev->mode_config.scaling_mode_property)
1377 return 0;
1378
1379 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001380 drm_property_create_enum(dev, 0, "scaling mode",
1381 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001382 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001383
1384 dev->mode_config.scaling_mode_property = scaling_mode;
1385
1386 return 0;
1387}
1388EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1389
1390/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001391 * drm_mode_create_dirty_property - create dirty property
1392 * @dev: DRM device
1393 *
1394 * Called by a driver the first time it's needed, must be attached to desired
1395 * connectors.
1396 */
1397int drm_mode_create_dirty_info_property(struct drm_device *dev)
1398{
1399 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001400
1401 if (dev->mode_config.dirty_info_property)
1402 return 0;
1403
1404 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001405 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001406 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001407 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001408 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001409 dev->mode_config.dirty_info_property = dirty_info;
1410
1411 return 0;
1412}
1413EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1414
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001415static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001416{
1417 uint32_t total_objects = 0;
1418
1419 total_objects += dev->mode_config.num_crtc;
1420 total_objects += dev->mode_config.num_connector;
1421 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001422 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001423
Dave Airlief453ba02008-11-07 14:05:41 -08001424 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1425 if (!group->id_list)
1426 return -ENOMEM;
1427
1428 group->num_crtcs = 0;
1429 group->num_connectors = 0;
1430 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001431 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001432 return 0;
1433}
1434
Dave Airliead222792014-05-02 13:22:19 +10001435void drm_mode_group_destroy(struct drm_mode_group *group)
1436{
1437 kfree(group->id_list);
1438 group->id_list = NULL;
1439}
1440
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001441/*
1442 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1443 * the drm core's responsibility to set up mode control groups.
1444 */
Dave Airlief453ba02008-11-07 14:05:41 -08001445int drm_mode_group_init_legacy_group(struct drm_device *dev,
1446 struct drm_mode_group *group)
1447{
1448 struct drm_crtc *crtc;
1449 struct drm_encoder *encoder;
1450 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001451 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001452 int ret;
1453
1454 if ((ret = drm_mode_group_init(dev, group)))
1455 return ret;
1456
1457 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1458 group->id_list[group->num_crtcs++] = crtc->base.id;
1459
1460 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1461 group->id_list[group->num_crtcs + group->num_encoders++] =
1462 encoder->base.id;
1463
1464 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1465 group->id_list[group->num_crtcs + group->num_encoders +
1466 group->num_connectors++] = connector->base.id;
1467
Sean Paul3b336ec2013-08-14 16:47:37 -04001468 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1469 group->id_list[group->num_crtcs + group->num_encoders +
1470 group->num_connectors + group->num_bridges++] =
1471 bridge->base.id;
1472
Dave Airlief453ba02008-11-07 14:05:41 -08001473 return 0;
1474}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001475EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001476
1477/**
Dave Airlief453ba02008-11-07 14:05:41 -08001478 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1479 * @out: drm_mode_modeinfo struct to return to the user
1480 * @in: drm_display_mode to use
1481 *
Dave Airlief453ba02008-11-07 14:05:41 -08001482 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1483 * the user.
1484 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001485static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1486 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001487{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001488 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1489 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1490 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1491 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1492 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1493 "timing values too large for mode info\n");
1494
Dave Airlief453ba02008-11-07 14:05:41 -08001495 out->clock = in->clock;
1496 out->hdisplay = in->hdisplay;
1497 out->hsync_start = in->hsync_start;
1498 out->hsync_end = in->hsync_end;
1499 out->htotal = in->htotal;
1500 out->hskew = in->hskew;
1501 out->vdisplay = in->vdisplay;
1502 out->vsync_start = in->vsync_start;
1503 out->vsync_end = in->vsync_end;
1504 out->vtotal = in->vtotal;
1505 out->vscan = in->vscan;
1506 out->vrefresh = in->vrefresh;
1507 out->flags = in->flags;
1508 out->type = in->type;
1509 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1510 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1511}
1512
1513/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001514 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001515 * @out: drm_display_mode to return to the user
1516 * @in: drm_mode_modeinfo to use
1517 *
Dave Airlief453ba02008-11-07 14:05:41 -08001518 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1519 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001520 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001521 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001522 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001523 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001524static int drm_crtc_convert_umode(struct drm_display_mode *out,
1525 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001526{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001527 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1528 return -ERANGE;
1529
Damien Lespiau5848ad42013-09-27 12:11:50 +01001530 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1531 return -EINVAL;
1532
Dave Airlief453ba02008-11-07 14:05:41 -08001533 out->clock = in->clock;
1534 out->hdisplay = in->hdisplay;
1535 out->hsync_start = in->hsync_start;
1536 out->hsync_end = in->hsync_end;
1537 out->htotal = in->htotal;
1538 out->hskew = in->hskew;
1539 out->vdisplay = in->vdisplay;
1540 out->vsync_start = in->vsync_start;
1541 out->vsync_end = in->vsync_end;
1542 out->vtotal = in->vtotal;
1543 out->vscan = in->vscan;
1544 out->vrefresh = in->vrefresh;
1545 out->flags = in->flags;
1546 out->type = in->type;
1547 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1548 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001549
1550 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001551}
1552
1553/**
1554 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001555 * @dev: drm device for the ioctl
1556 * @data: data pointer for the ioctl
1557 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001558 *
Dave Airlief453ba02008-11-07 14:05:41 -08001559 * Construct a set of configuration description structures and return
1560 * them to the user, including CRTC, connector and framebuffer configuration.
1561 *
1562 * Called by the user via ioctl.
1563 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001564 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001565 * Zero on success, errno on failure.
1566 */
1567int drm_mode_getresources(struct drm_device *dev, void *data,
1568 struct drm_file *file_priv)
1569{
1570 struct drm_mode_card_res *card_res = data;
1571 struct list_head *lh;
1572 struct drm_framebuffer *fb;
1573 struct drm_connector *connector;
1574 struct drm_crtc *crtc;
1575 struct drm_encoder *encoder;
1576 int ret = 0;
1577 int connector_count = 0;
1578 int crtc_count = 0;
1579 int fb_count = 0;
1580 int encoder_count = 0;
1581 int copied = 0, i;
1582 uint32_t __user *fb_id;
1583 uint32_t __user *crtc_id;
1584 uint32_t __user *connector_id;
1585 uint32_t __user *encoder_id;
1586 struct drm_mode_group *mode_group;
1587
Dave Airliefb3b06c2011-02-08 13:55:21 +10001588 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1589 return -EINVAL;
1590
Dave Airlief453ba02008-11-07 14:05:41 -08001591
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001592 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001593 /*
1594 * For the non-control nodes we need to limit the list of resources
1595 * by IDs in the group list for this node
1596 */
1597 list_for_each(lh, &file_priv->fbs)
1598 fb_count++;
1599
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001600 /* handle this in 4 parts */
1601 /* FBs */
1602 if (card_res->count_fbs >= fb_count) {
1603 copied = 0;
1604 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1605 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1606 if (put_user(fb->base.id, fb_id + copied)) {
1607 mutex_unlock(&file_priv->fbs_lock);
1608 return -EFAULT;
1609 }
1610 copied++;
1611 }
1612 }
1613 card_res->count_fbs = fb_count;
1614 mutex_unlock(&file_priv->fbs_lock);
1615
1616 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001617 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001618
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001619 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001620 list_for_each(lh, &dev->mode_config.crtc_list)
1621 crtc_count++;
1622
1623 list_for_each(lh, &dev->mode_config.connector_list)
1624 connector_count++;
1625
1626 list_for_each(lh, &dev->mode_config.encoder_list)
1627 encoder_count++;
1628 } else {
1629
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001630 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001631 crtc_count = mode_group->num_crtcs;
1632 connector_count = mode_group->num_connectors;
1633 encoder_count = mode_group->num_encoders;
1634 }
1635
1636 card_res->max_height = dev->mode_config.max_height;
1637 card_res->min_height = dev->mode_config.min_height;
1638 card_res->max_width = dev->mode_config.max_width;
1639 card_res->min_width = dev->mode_config.min_width;
1640
Dave Airlief453ba02008-11-07 14:05:41 -08001641 /* CRTCs */
1642 if (card_res->count_crtcs >= crtc_count) {
1643 copied = 0;
1644 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001645 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001646 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1647 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001648 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001649 if (put_user(crtc->base.id, crtc_id + copied)) {
1650 ret = -EFAULT;
1651 goto out;
1652 }
1653 copied++;
1654 }
1655 } else {
1656 for (i = 0; i < mode_group->num_crtcs; i++) {
1657 if (put_user(mode_group->id_list[i],
1658 crtc_id + copied)) {
1659 ret = -EFAULT;
1660 goto out;
1661 }
1662 copied++;
1663 }
1664 }
1665 }
1666 card_res->count_crtcs = crtc_count;
1667
1668 /* Encoders */
1669 if (card_res->count_encoders >= encoder_count) {
1670 copied = 0;
1671 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001672 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001673 list_for_each_entry(encoder,
1674 &dev->mode_config.encoder_list,
1675 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001676 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001677 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001678 if (put_user(encoder->base.id, encoder_id +
1679 copied)) {
1680 ret = -EFAULT;
1681 goto out;
1682 }
1683 copied++;
1684 }
1685 } else {
1686 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1687 if (put_user(mode_group->id_list[i],
1688 encoder_id + copied)) {
1689 ret = -EFAULT;
1690 goto out;
1691 }
1692 copied++;
1693 }
1694
1695 }
1696 }
1697 card_res->count_encoders = encoder_count;
1698
1699 /* Connectors */
1700 if (card_res->count_connectors >= connector_count) {
1701 copied = 0;
1702 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001703 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001704 list_for_each_entry(connector,
1705 &dev->mode_config.connector_list,
1706 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001707 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1708 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001709 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001710 if (put_user(connector->base.id,
1711 connector_id + copied)) {
1712 ret = -EFAULT;
1713 goto out;
1714 }
1715 copied++;
1716 }
1717 } else {
1718 int start = mode_group->num_crtcs +
1719 mode_group->num_encoders;
1720 for (i = start; i < start + mode_group->num_connectors; i++) {
1721 if (put_user(mode_group->id_list[i],
1722 connector_id + copied)) {
1723 ret = -EFAULT;
1724 goto out;
1725 }
1726 copied++;
1727 }
1728 }
1729 }
1730 card_res->count_connectors = connector_count;
1731
Jerome Glisse94401062010-07-15 15:43:25 -04001732 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001733 card_res->count_connectors, card_res->count_encoders);
1734
1735out:
Daniel Vetter84849902012-12-02 00:28:11 +01001736 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001737 return ret;
1738}
1739
1740/**
1741 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001742 * @dev: drm device for the ioctl
1743 * @data: data pointer for the ioctl
1744 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001745 *
Dave Airlief453ba02008-11-07 14:05:41 -08001746 * Construct a CRTC configuration structure to return to the user.
1747 *
1748 * Called by the user via ioctl.
1749 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001750 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001751 * Zero on success, errno on failure.
1752 */
1753int drm_mode_getcrtc(struct drm_device *dev,
1754 void *data, struct drm_file *file_priv)
1755{
1756 struct drm_mode_crtc *crtc_resp = data;
1757 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001758 int ret = 0;
1759
Dave Airliefb3b06c2011-02-08 13:55:21 +10001760 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1761 return -EINVAL;
1762
Daniel Vetter84849902012-12-02 00:28:11 +01001763 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001764
Rob Clarka2b34e22013-10-05 16:36:52 -04001765 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1766 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001767 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001768 goto out;
1769 }
Dave Airlief453ba02008-11-07 14:05:41 -08001770
1771 crtc_resp->x = crtc->x;
1772 crtc_resp->y = crtc->y;
1773 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001774 if (crtc->primary->fb)
1775 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001776 else
1777 crtc_resp->fb_id = 0;
1778
1779 if (crtc->enabled) {
1780
1781 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1782 crtc_resp->mode_valid = 1;
1783
1784 } else {
1785 crtc_resp->mode_valid = 0;
1786 }
1787
1788out:
Daniel Vetter84849902012-12-02 00:28:11 +01001789 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001790 return ret;
1791}
1792
Damien Lespiau61d8e322013-09-25 16:45:22 +01001793static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1794 const struct drm_file *file_priv)
1795{
1796 /*
1797 * If user-space hasn't configured the driver to expose the stereo 3D
1798 * modes, don't expose them.
1799 */
1800 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1801 return false;
1802
1803 return true;
1804}
1805
Dave Airlief453ba02008-11-07 14:05:41 -08001806/**
1807 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001808 * @dev: drm device for the ioctl
1809 * @data: data pointer for the ioctl
1810 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001811 *
Dave Airlief453ba02008-11-07 14:05:41 -08001812 * Construct a connector configuration structure to return to the user.
1813 *
1814 * Called by the user via ioctl.
1815 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001816 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001817 * Zero on success, errno on failure.
1818 */
1819int drm_mode_getconnector(struct drm_device *dev, void *data,
1820 struct drm_file *file_priv)
1821{
1822 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001823 struct drm_connector *connector;
1824 struct drm_display_mode *mode;
1825 int mode_count = 0;
1826 int props_count = 0;
1827 int encoders_count = 0;
1828 int ret = 0;
1829 int copied = 0;
1830 int i;
1831 struct drm_mode_modeinfo u_mode;
1832 struct drm_mode_modeinfo __user *mode_ptr;
1833 uint32_t __user *prop_ptr;
1834 uint64_t __user *prop_values;
1835 uint32_t __user *encoder_ptr;
1836
Dave Airliefb3b06c2011-02-08 13:55:21 +10001837 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1838 return -EINVAL;
1839
Dave Airlief453ba02008-11-07 14:05:41 -08001840 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1841
Jerome Glisse94401062010-07-15 15:43:25 -04001842 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001843
Daniel Vetter7b240562012-12-12 00:35:33 +01001844 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001845
Rob Clarka2b34e22013-10-05 16:36:52 -04001846 connector = drm_connector_find(dev, out_resp->connector_id);
1847 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001848 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001849 goto out;
1850 }
Dave Airlief453ba02008-11-07 14:05:41 -08001851
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001852 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001853
1854 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1855 if (connector->encoder_ids[i] != 0) {
1856 encoders_count++;
1857 }
1858 }
1859
1860 if (out_resp->count_modes == 0) {
1861 connector->funcs->fill_modes(connector,
1862 dev->mode_config.max_width,
1863 dev->mode_config.max_height);
1864 }
1865
1866 /* delayed so we get modes regardless of pre-fill_modes state */
1867 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001868 if (drm_mode_expose_to_userspace(mode, file_priv))
1869 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001870
1871 out_resp->connector_id = connector->base.id;
1872 out_resp->connector_type = connector->connector_type;
1873 out_resp->connector_type_id = connector->connector_type_id;
1874 out_resp->mm_width = connector->display_info.width_mm;
1875 out_resp->mm_height = connector->display_info.height_mm;
1876 out_resp->subpixel = connector->display_info.subpixel_order;
1877 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02001878 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08001879 if (connector->encoder)
1880 out_resp->encoder_id = connector->encoder->base.id;
1881 else
1882 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02001883 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001884
1885 /*
1886 * This ioctl is called twice, once to determine how much space is
1887 * needed, and the 2nd time to fill it.
1888 */
1889 if ((out_resp->count_modes >= mode_count) && mode_count) {
1890 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001891 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001892 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001893 if (!drm_mode_expose_to_userspace(mode, file_priv))
1894 continue;
1895
Dave Airlief453ba02008-11-07 14:05:41 -08001896 drm_crtc_convert_to_umode(&u_mode, mode);
1897 if (copy_to_user(mode_ptr + copied,
1898 &u_mode, sizeof(u_mode))) {
1899 ret = -EFAULT;
1900 goto out;
1901 }
1902 copied++;
1903 }
1904 }
1905 out_resp->count_modes = mode_count;
1906
1907 if ((out_resp->count_props >= props_count) && props_count) {
1908 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001909 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1910 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001911 for (i = 0; i < connector->properties.count; i++) {
1912 if (put_user(connector->properties.ids[i],
1913 prop_ptr + copied)) {
1914 ret = -EFAULT;
1915 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001916 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001917
1918 if (put_user(connector->properties.values[i],
1919 prop_values + copied)) {
1920 ret = -EFAULT;
1921 goto out;
1922 }
1923 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001924 }
1925 }
1926 out_resp->count_props = props_count;
1927
1928 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1929 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001930 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001931 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1932 if (connector->encoder_ids[i] != 0) {
1933 if (put_user(connector->encoder_ids[i],
1934 encoder_ptr + copied)) {
1935 ret = -EFAULT;
1936 goto out;
1937 }
1938 copied++;
1939 }
1940 }
1941 }
1942 out_resp->count_encoders = encoders_count;
1943
1944out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001945 mutex_unlock(&dev->mode_config.mutex);
1946
Dave Airlief453ba02008-11-07 14:05:41 -08001947 return ret;
1948}
1949
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001950/**
1951 * drm_mode_getencoder - get encoder configuration
1952 * @dev: drm device for the ioctl
1953 * @data: data pointer for the ioctl
1954 * @file_priv: drm file for the ioctl call
1955 *
1956 * Construct a encoder configuration structure to return to the user.
1957 *
1958 * Called by the user via ioctl.
1959 *
1960 * Returns:
1961 * Zero on success, errno on failure.
1962 */
Dave Airlief453ba02008-11-07 14:05:41 -08001963int drm_mode_getencoder(struct drm_device *dev, void *data,
1964 struct drm_file *file_priv)
1965{
1966 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001967 struct drm_encoder *encoder;
1968 int ret = 0;
1969
Dave Airliefb3b06c2011-02-08 13:55:21 +10001970 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1971 return -EINVAL;
1972
Daniel Vetter84849902012-12-02 00:28:11 +01001973 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04001974 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1975 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001976 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001977 goto out;
1978 }
Dave Airlief453ba02008-11-07 14:05:41 -08001979
1980 if (encoder->crtc)
1981 enc_resp->crtc_id = encoder->crtc->base.id;
1982 else
1983 enc_resp->crtc_id = 0;
1984 enc_resp->encoder_type = encoder->encoder_type;
1985 enc_resp->encoder_id = encoder->base.id;
1986 enc_resp->possible_crtcs = encoder->possible_crtcs;
1987 enc_resp->possible_clones = encoder->possible_clones;
1988
1989out:
Daniel Vetter84849902012-12-02 00:28:11 +01001990 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001991 return ret;
1992}
1993
1994/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001995 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001996 * @dev: DRM device
1997 * @data: ioctl data
1998 * @file_priv: DRM file info
1999 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002000 * Construct a list of plane ids to return to the user.
2001 *
2002 * Called by the user via ioctl.
2003 *
2004 * Returns:
2005 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002006 */
2007int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002008 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002009{
2010 struct drm_mode_get_plane_res *plane_resp = data;
2011 struct drm_mode_config *config;
2012 struct drm_plane *plane;
2013 uint32_t __user *plane_ptr;
2014 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002015 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002016
2017 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2018 return -EINVAL;
2019
Daniel Vetter84849902012-12-02 00:28:11 +01002020 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002021 config = &dev->mode_config;
2022
Matt Roper681e7ec2014-04-01 15:22:42 -07002023 if (file_priv->universal_planes)
2024 num_planes = config->num_total_plane;
2025 else
2026 num_planes = config->num_overlay_plane;
2027
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002028 /*
2029 * This ioctl is called twice, once to determine how much space is
2030 * needed, and the 2nd time to fill it.
2031 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002032 if (num_planes &&
2033 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002034 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002035
2036 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002037 /*
2038 * Unless userspace set the 'universal planes'
2039 * capability bit, only advertise overlays.
2040 */
2041 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2042 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002043 continue;
2044
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002045 if (put_user(plane->base.id, plane_ptr + copied)) {
2046 ret = -EFAULT;
2047 goto out;
2048 }
2049 copied++;
2050 }
2051 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002052 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002053
2054out:
Daniel Vetter84849902012-12-02 00:28:11 +01002055 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002056 return ret;
2057}
2058
2059/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002060 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002061 * @dev: DRM device
2062 * @data: ioctl data
2063 * @file_priv: DRM file info
2064 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002065 * Construct a plane configuration structure to return to the user.
2066 *
2067 * Called by the user via ioctl.
2068 *
2069 * Returns:
2070 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002071 */
2072int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002073 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002074{
2075 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002076 struct drm_plane *plane;
2077 uint32_t __user *format_ptr;
2078 int ret = 0;
2079
2080 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2081 return -EINVAL;
2082
Daniel Vetter84849902012-12-02 00:28:11 +01002083 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002084 plane = drm_plane_find(dev, plane_resp->plane_id);
2085 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002086 ret = -ENOENT;
2087 goto out;
2088 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002089
2090 if (plane->crtc)
2091 plane_resp->crtc_id = plane->crtc->base.id;
2092 else
2093 plane_resp->crtc_id = 0;
2094
2095 if (plane->fb)
2096 plane_resp->fb_id = plane->fb->base.id;
2097 else
2098 plane_resp->fb_id = 0;
2099
2100 plane_resp->plane_id = plane->base.id;
2101 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002102 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002103
2104 /*
2105 * This ioctl is called twice, once to determine how much space is
2106 * needed, and the 2nd time to fill it.
2107 */
2108 if (plane->format_count &&
2109 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002110 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002111 if (copy_to_user(format_ptr,
2112 plane->format_types,
2113 sizeof(uint32_t) * plane->format_count)) {
2114 ret = -EFAULT;
2115 goto out;
2116 }
2117 }
2118 plane_resp->count_format_types = plane->format_count;
2119
2120out:
Daniel Vetter84849902012-12-02 00:28:11 +01002121 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002122 return ret;
2123}
2124
2125/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002126 * drm_mode_setplane - configure a plane's configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002127 * @dev: DRM device
2128 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002129 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002130 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002131 * Set plane configuration, including placement, fb, scaling, and other factors.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002132 * Or pass a NULL fb to disable.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002133 *
2134 * Returns:
2135 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002136 */
2137int drm_mode_setplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002138 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002139{
2140 struct drm_mode_set_plane *plane_req = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002141 struct drm_plane *plane;
2142 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002143 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002144 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002145 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002146 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002147
2148 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2149 return -EINVAL;
2150
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002151 /*
2152 * First, find the plane, crtc, and fb objects. If not available,
2153 * we don't bother to call the driver.
2154 */
Rob Clarka2b34e22013-10-05 16:36:52 -04002155 plane = drm_plane_find(dev, plane_req->plane_id);
2156 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002157 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2158 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002159 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002160 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002161
2162 /* No fb means shut it down */
2163 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002164 drm_modeset_lock_all(dev);
2165 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002166 ret = plane->funcs->disable_plane(plane);
2167 if (!ret) {
2168 plane->crtc = NULL;
2169 plane->fb = NULL;
2170 } else {
2171 old_fb = NULL;
2172 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002173 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002174 goto out;
2175 }
2176
Rob Clarka2b34e22013-10-05 16:36:52 -04002177 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2178 if (!crtc) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002179 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2180 plane_req->crtc_id);
2181 ret = -ENOENT;
2182 goto out;
2183 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002184
Matt Roper7f994f32014-05-29 08:06:51 -07002185 /* Check whether this plane is usable on this CRTC */
2186 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2187 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2188 ret = -EINVAL;
2189 goto out;
2190 }
2191
Daniel Vetter786b99e2012-12-02 21:53:40 +01002192 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2193 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002194 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2195 plane_req->fb_id);
2196 ret = -ENOENT;
2197 goto out;
2198 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002199
Ville Syrjälä62443be2011-12-20 00:06:47 +02002200 /* Check whether this plane supports the fb pixel format. */
2201 for (i = 0; i < plane->format_count; i++)
2202 if (fb->pixel_format == plane->format_types[i])
2203 break;
2204 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002205 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2206 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002207 ret = -EINVAL;
2208 goto out;
2209 }
2210
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002211 fb_width = fb->width << 16;
2212 fb_height = fb->height << 16;
2213
2214 /* Make sure source coordinates are inside the fb. */
2215 if (plane_req->src_w > fb_width ||
2216 plane_req->src_x > fb_width - plane_req->src_w ||
2217 plane_req->src_h > fb_height ||
2218 plane_req->src_y > fb_height - plane_req->src_h) {
2219 DRM_DEBUG_KMS("Invalid source coordinates "
2220 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2221 plane_req->src_w >> 16,
2222 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2223 plane_req->src_h >> 16,
2224 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2225 plane_req->src_x >> 16,
2226 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2227 plane_req->src_y >> 16,
2228 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2229 ret = -ENOSPC;
2230 goto out;
2231 }
2232
Ville Syrjälä687a0402011-12-20 00:06:45 +02002233 /* Give drivers some help against integer overflows */
2234 if (plane_req->crtc_w > INT_MAX ||
2235 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2236 plane_req->crtc_h > INT_MAX ||
2237 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2238 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2239 plane_req->crtc_w, plane_req->crtc_h,
2240 plane_req->crtc_x, plane_req->crtc_y);
2241 ret = -ERANGE;
2242 goto out;
2243 }
2244
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002245 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002246 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002247 ret = plane->funcs->update_plane(plane, crtc, fb,
2248 plane_req->crtc_x, plane_req->crtc_y,
2249 plane_req->crtc_w, plane_req->crtc_h,
2250 plane_req->src_x, plane_req->src_y,
2251 plane_req->src_w, plane_req->src_h);
2252 if (!ret) {
2253 plane->crtc = crtc;
2254 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002255 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002256 } else {
2257 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002258 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002259 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002260
2261out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002262 if (fb)
2263 drm_framebuffer_unreference(fb);
2264 if (old_fb)
2265 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002266
2267 return ret;
2268}
2269
2270/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002271 * drm_mode_set_config_internal - helper to call ->set_config
2272 * @set: modeset config to set
2273 *
2274 * This is a little helper to wrap internal calls to the ->set_config driver
2275 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002276 *
2277 * Returns:
2278 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002279 */
2280int drm_mode_set_config_internal(struct drm_mode_set *set)
2281{
2282 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002283 struct drm_framebuffer *fb;
2284 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002285 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002286
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002287 /*
2288 * NOTE: ->set_config can also disable other crtcs (if we steal all
2289 * connectors from it), hence we need to refcount the fbs across all
2290 * crtcs. Atomic modeset will have saner semantics ...
2291 */
2292 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002293 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002294
Daniel Vetterb0d12322012-12-11 01:07:12 +01002295 fb = set->fb;
2296
2297 ret = crtc->funcs->set_config(set);
2298 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002299 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002300 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002301 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002302
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002303 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002304 if (tmp->primary->fb)
2305 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002306 if (tmp->old_fb)
2307 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002308 }
2309
2310 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002311}
2312EXPORT_SYMBOL(drm_mode_set_config_internal);
2313
Matt Roperaf936292014-04-01 15:22:34 -07002314/**
2315 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2316 * CRTC viewport
2317 * @crtc: CRTC that framebuffer will be displayed on
2318 * @x: x panning
2319 * @y: y panning
2320 * @mode: mode that framebuffer will be displayed under
2321 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002322 */
Matt Roperaf936292014-04-01 15:22:34 -07002323int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2324 int x, int y,
2325 const struct drm_display_mode *mode,
2326 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002327
2328{
2329 int hdisplay, vdisplay;
2330
2331 hdisplay = mode->hdisplay;
2332 vdisplay = mode->vdisplay;
2333
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002334 if (drm_mode_is_stereo(mode)) {
2335 struct drm_display_mode adjusted = *mode;
2336
2337 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2338 hdisplay = adjusted.crtc_hdisplay;
2339 vdisplay = adjusted.crtc_vdisplay;
2340 }
2341
Damien Lespiauc11e9282013-09-25 16:45:30 +01002342 if (crtc->invert_dimensions)
2343 swap(hdisplay, vdisplay);
2344
2345 if (hdisplay > fb->width ||
2346 vdisplay > fb->height ||
2347 x > fb->width - hdisplay ||
2348 y > fb->height - vdisplay) {
2349 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2350 fb->width, fb->height, hdisplay, vdisplay, x, y,
2351 crtc->invert_dimensions ? " (inverted)" : "");
2352 return -ENOSPC;
2353 }
2354
2355 return 0;
2356}
Matt Roperaf936292014-04-01 15:22:34 -07002357EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002358
Daniel Vetter2d13b672012-12-11 13:47:23 +01002359/**
Dave Airlief453ba02008-11-07 14:05:41 -08002360 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002361 * @dev: drm device for the ioctl
2362 * @data: data pointer for the ioctl
2363 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002364 *
Dave Airlief453ba02008-11-07 14:05:41 -08002365 * Build a new CRTC configuration based on user request.
2366 *
2367 * Called by the user via ioctl.
2368 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002369 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002370 * Zero on success, errno on failure.
2371 */
2372int drm_mode_setcrtc(struct drm_device *dev, void *data,
2373 struct drm_file *file_priv)
2374{
2375 struct drm_mode_config *config = &dev->mode_config;
2376 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002377 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002378 struct drm_connector **connector_set = NULL, *connector;
2379 struct drm_framebuffer *fb = NULL;
2380 struct drm_display_mode *mode = NULL;
2381 struct drm_mode_set set;
2382 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002383 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002384 int i;
2385
Dave Airliefb3b06c2011-02-08 13:55:21 +10002386 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2387 return -EINVAL;
2388
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002389 /* For some reason crtc x/y offsets are signed internally. */
2390 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2391 return -ERANGE;
2392
Daniel Vetter84849902012-12-02 00:28:11 +01002393 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002394 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2395 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002396 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002397 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002398 goto out;
2399 }
Jerome Glisse94401062010-07-15 15:43:25 -04002400 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002401
2402 if (crtc_req->mode_valid) {
2403 /* If we have a mode we need a framebuffer. */
2404 /* If we pass -1, set the mode with the currently bound fb */
2405 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002406 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002407 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2408 ret = -EINVAL;
2409 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002410 }
Matt Roperf4510a22014-04-01 15:22:40 -07002411 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002412 /* Make refcounting symmetric with the lookup path. */
2413 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002414 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002415 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2416 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002417 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2418 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002419 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002420 goto out;
2421 }
Dave Airlief453ba02008-11-07 14:05:41 -08002422 }
2423
2424 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002425 if (!mode) {
2426 ret = -ENOMEM;
2427 goto out;
2428 }
2429
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002430 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2431 if (ret) {
2432 DRM_DEBUG_KMS("Invalid mode\n");
2433 goto out;
2434 }
2435
Dave Airlief453ba02008-11-07 14:05:41 -08002436 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002437
Damien Lespiauc11e9282013-09-25 16:45:30 +01002438 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2439 mode, fb);
2440 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002441 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002442
Dave Airlief453ba02008-11-07 14:05:41 -08002443 }
2444
2445 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002446 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002447 ret = -EINVAL;
2448 goto out;
2449 }
2450
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002451 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002452 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002453 crtc_req->count_connectors);
2454 ret = -EINVAL;
2455 goto out;
2456 }
2457
2458 if (crtc_req->count_connectors > 0) {
2459 u32 out_id;
2460
2461 /* Avoid unbounded kernel memory allocation */
2462 if (crtc_req->count_connectors > config->num_connector) {
2463 ret = -EINVAL;
2464 goto out;
2465 }
2466
2467 connector_set = kmalloc(crtc_req->count_connectors *
2468 sizeof(struct drm_connector *),
2469 GFP_KERNEL);
2470 if (!connector_set) {
2471 ret = -ENOMEM;
2472 goto out;
2473 }
2474
2475 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002476 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002477 if (get_user(out_id, &set_connectors_ptr[i])) {
2478 ret = -EFAULT;
2479 goto out;
2480 }
2481
Rob Clarka2b34e22013-10-05 16:36:52 -04002482 connector = drm_connector_find(dev, out_id);
2483 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002484 DRM_DEBUG_KMS("Connector id %d unknown\n",
2485 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002486 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002487 goto out;
2488 }
Jerome Glisse94401062010-07-15 15:43:25 -04002489 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2490 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002491 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002492
2493 connector_set[i] = connector;
2494 }
2495 }
2496
2497 set.crtc = crtc;
2498 set.x = crtc_req->x;
2499 set.y = crtc_req->y;
2500 set.mode = mode;
2501 set.connectors = connector_set;
2502 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002503 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002504 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002505
2506out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002507 if (fb)
2508 drm_framebuffer_unreference(fb);
2509
Dave Airlief453ba02008-11-07 14:05:41 -08002510 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002511 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002512 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002513 return ret;
2514}
2515
Dave Airlie4c813d42013-06-20 11:48:52 +10002516static int drm_mode_cursor_common(struct drm_device *dev,
2517 struct drm_mode_cursor2 *req,
2518 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002519{
Dave Airlief453ba02008-11-07 14:05:41 -08002520 struct drm_crtc *crtc;
2521 int ret = 0;
2522
Dave Airliefb3b06c2011-02-08 13:55:21 +10002523 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2524 return -EINVAL;
2525
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002526 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002527 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002528
Rob Clarka2b34e22013-10-05 16:36:52 -04002529 crtc = drm_crtc_find(dev, req->crtc_id);
2530 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002531 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002532 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002533 }
Dave Airlief453ba02008-11-07 14:05:41 -08002534
Rob Clark51fd3712013-11-19 12:10:12 -05002535 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002536 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002537 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002538 ret = -ENXIO;
2539 goto out;
2540 }
2541 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002542 if (crtc->funcs->cursor_set2)
2543 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2544 req->width, req->height, req->hot_x, req->hot_y);
2545 else
2546 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2547 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002548 }
2549
2550 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2551 if (crtc->funcs->cursor_move) {
2552 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2553 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002554 ret = -EFAULT;
2555 goto out;
2556 }
2557 }
2558out:
Rob Clark51fd3712013-11-19 12:10:12 -05002559 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002560
Dave Airlief453ba02008-11-07 14:05:41 -08002561 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002562
2563}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002564
2565
2566/**
2567 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2568 * @dev: drm device for the ioctl
2569 * @data: data pointer for the ioctl
2570 * @file_priv: drm file for the ioctl call
2571 *
2572 * Set the cursor configuration based on user request.
2573 *
2574 * Called by the user via ioctl.
2575 *
2576 * Returns:
2577 * Zero on success, errno on failure.
2578 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002579int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002580 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002581{
2582 struct drm_mode_cursor *req = data;
2583 struct drm_mode_cursor2 new_req;
2584
2585 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2586 new_req.hot_x = new_req.hot_y = 0;
2587
2588 return drm_mode_cursor_common(dev, &new_req, file_priv);
2589}
2590
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002591/**
2592 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2593 * @dev: drm device for the ioctl
2594 * @data: data pointer for the ioctl
2595 * @file_priv: drm file for the ioctl call
2596 *
2597 * Set the cursor configuration based on user request. This implements the 2nd
2598 * version of the cursor ioctl, which allows userspace to additionally specify
2599 * the hotspot of the pointer.
2600 *
2601 * Called by the user via ioctl.
2602 *
2603 * Returns:
2604 * Zero on success, errno on failure.
2605 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002606int drm_mode_cursor2_ioctl(struct drm_device *dev,
2607 void *data, struct drm_file *file_priv)
2608{
2609 struct drm_mode_cursor2 *req = data;
2610 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002611}
2612
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002613/**
2614 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2615 * @bpp: bits per pixels
2616 * @depth: bit depth per pixel
2617 *
2618 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2619 * Useful in fbdev emulation code, since that deals in those values.
2620 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002621uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2622{
2623 uint32_t fmt;
2624
2625 switch (bpp) {
2626 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002627 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002628 break;
2629 case 16:
2630 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002631 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002632 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002633 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002634 break;
2635 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002636 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002637 break;
2638 case 32:
2639 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002640 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002641 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002642 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002643 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002644 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002645 break;
2646 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002647 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2648 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002649 break;
2650 }
2651
2652 return fmt;
2653}
2654EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2655
Dave Airlief453ba02008-11-07 14:05:41 -08002656/**
2657 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002658 * @dev: drm device for the ioctl
2659 * @data: data pointer for the ioctl
2660 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002661 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002662 * Add a new FB to the specified CRTC, given a user request. This is the
2663 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002664 *
2665 * Called by the user via ioctl.
2666 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002667 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002668 * Zero on success, errno on failure.
2669 */
2670int drm_mode_addfb(struct drm_device *dev,
2671 void *data, struct drm_file *file_priv)
2672{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002673 struct drm_mode_fb_cmd *or = data;
2674 struct drm_mode_fb_cmd2 r = {};
2675 struct drm_mode_config *config = &dev->mode_config;
2676 struct drm_framebuffer *fb;
2677 int ret = 0;
2678
2679 /* Use new struct with format internally */
2680 r.fb_id = or->fb_id;
2681 r.width = or->width;
2682 r.height = or->height;
2683 r.pitches[0] = or->pitch;
2684 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2685 r.handles[0] = or->handle;
2686
2687 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2688 return -EINVAL;
2689
Jesse Barnesacb4b992011-11-07 12:03:23 -08002690 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002691 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002692
2693 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002694 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002695
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002696 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2697 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002698 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002699 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002700 }
2701
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002702 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002703 or->fb_id = fb->base.id;
2704 list_add(&fb->filp_head, &file_priv->fbs);
2705 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002706 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002707
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002708 return ret;
2709}
2710
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002711static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002712{
2713 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2714
2715 switch (format) {
2716 case DRM_FORMAT_C8:
2717 case DRM_FORMAT_RGB332:
2718 case DRM_FORMAT_BGR233:
2719 case DRM_FORMAT_XRGB4444:
2720 case DRM_FORMAT_XBGR4444:
2721 case DRM_FORMAT_RGBX4444:
2722 case DRM_FORMAT_BGRX4444:
2723 case DRM_FORMAT_ARGB4444:
2724 case DRM_FORMAT_ABGR4444:
2725 case DRM_FORMAT_RGBA4444:
2726 case DRM_FORMAT_BGRA4444:
2727 case DRM_FORMAT_XRGB1555:
2728 case DRM_FORMAT_XBGR1555:
2729 case DRM_FORMAT_RGBX5551:
2730 case DRM_FORMAT_BGRX5551:
2731 case DRM_FORMAT_ARGB1555:
2732 case DRM_FORMAT_ABGR1555:
2733 case DRM_FORMAT_RGBA5551:
2734 case DRM_FORMAT_BGRA5551:
2735 case DRM_FORMAT_RGB565:
2736 case DRM_FORMAT_BGR565:
2737 case DRM_FORMAT_RGB888:
2738 case DRM_FORMAT_BGR888:
2739 case DRM_FORMAT_XRGB8888:
2740 case DRM_FORMAT_XBGR8888:
2741 case DRM_FORMAT_RGBX8888:
2742 case DRM_FORMAT_BGRX8888:
2743 case DRM_FORMAT_ARGB8888:
2744 case DRM_FORMAT_ABGR8888:
2745 case DRM_FORMAT_RGBA8888:
2746 case DRM_FORMAT_BGRA8888:
2747 case DRM_FORMAT_XRGB2101010:
2748 case DRM_FORMAT_XBGR2101010:
2749 case DRM_FORMAT_RGBX1010102:
2750 case DRM_FORMAT_BGRX1010102:
2751 case DRM_FORMAT_ARGB2101010:
2752 case DRM_FORMAT_ABGR2101010:
2753 case DRM_FORMAT_RGBA1010102:
2754 case DRM_FORMAT_BGRA1010102:
2755 case DRM_FORMAT_YUYV:
2756 case DRM_FORMAT_YVYU:
2757 case DRM_FORMAT_UYVY:
2758 case DRM_FORMAT_VYUY:
2759 case DRM_FORMAT_AYUV:
2760 case DRM_FORMAT_NV12:
2761 case DRM_FORMAT_NV21:
2762 case DRM_FORMAT_NV16:
2763 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002764 case DRM_FORMAT_NV24:
2765 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002766 case DRM_FORMAT_YUV410:
2767 case DRM_FORMAT_YVU410:
2768 case DRM_FORMAT_YUV411:
2769 case DRM_FORMAT_YVU411:
2770 case DRM_FORMAT_YUV420:
2771 case DRM_FORMAT_YVU420:
2772 case DRM_FORMAT_YUV422:
2773 case DRM_FORMAT_YVU422:
2774 case DRM_FORMAT_YUV444:
2775 case DRM_FORMAT_YVU444:
2776 return 0;
2777 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002778 DRM_DEBUG_KMS("invalid pixel format %s\n",
2779 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002780 return -EINVAL;
2781 }
2782}
2783
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002784static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002785{
2786 int ret, hsub, vsub, num_planes, i;
2787
2788 ret = format_check(r);
2789 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002790 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2791 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002792 return ret;
2793 }
2794
2795 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2796 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2797 num_planes = drm_format_num_planes(r->pixel_format);
2798
2799 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002800 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002801 return -EINVAL;
2802 }
2803
2804 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002805 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002806 return -EINVAL;
2807 }
2808
2809 for (i = 0; i < num_planes; i++) {
2810 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002811 unsigned int height = r->height / (i != 0 ? vsub : 1);
2812 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002813
2814 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002815 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002816 return -EINVAL;
2817 }
2818
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002819 if ((uint64_t) width * cpp > UINT_MAX)
2820 return -ERANGE;
2821
2822 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2823 return -ERANGE;
2824
2825 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002826 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002827 return -EINVAL;
2828 }
2829 }
2830
2831 return 0;
2832}
2833
Matt Roperc394c2b2014-06-10 08:28:08 -07002834static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
2835 struct drm_mode_fb_cmd2 *r,
2836 struct drm_file *file_priv)
2837{
2838 struct drm_mode_config *config = &dev->mode_config;
2839 struct drm_framebuffer *fb;
2840 int ret;
2841
2842 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2843 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2844 return ERR_PTR(-EINVAL);
2845 }
2846
2847 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2848 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2849 r->width, config->min_width, config->max_width);
2850 return ERR_PTR(-EINVAL);
2851 }
2852 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2853 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2854 r->height, config->min_height, config->max_height);
2855 return ERR_PTR(-EINVAL);
2856 }
2857
2858 ret = framebuffer_check(r);
2859 if (ret)
2860 return ERR_PTR(ret);
2861
2862 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2863 if (IS_ERR(fb)) {
2864 DRM_DEBUG_KMS("could not create framebuffer\n");
2865 return fb;
2866 }
2867
2868 mutex_lock(&file_priv->fbs_lock);
2869 r->fb_id = fb->base.id;
2870 list_add(&fb->filp_head, &file_priv->fbs);
2871 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2872 mutex_unlock(&file_priv->fbs_lock);
2873
2874 return fb;
2875}
2876
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002877/**
2878 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002879 * @dev: drm device for the ioctl
2880 * @data: data pointer for the ioctl
2881 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002882 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002883 * Add a new FB to the specified CRTC, given a user request with format. This is
2884 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2885 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002886 *
2887 * Called by the user via ioctl.
2888 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002889 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002890 * Zero on success, errno on failure.
2891 */
2892int drm_mode_addfb2(struct drm_device *dev,
2893 void *data, struct drm_file *file_priv)
2894{
Dave Airlief453ba02008-11-07 14:05:41 -08002895 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002896
Dave Airliefb3b06c2011-02-08 13:55:21 +10002897 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2898 return -EINVAL;
2899
Matt Roperc394c2b2014-06-10 08:28:08 -07002900 fb = add_framebuffer_internal(dev, data, file_priv);
2901 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002902 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002903
Matt Roperc394c2b2014-06-10 08:28:08 -07002904 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002905}
2906
2907/**
2908 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002909 * @dev: drm device for the ioctl
2910 * @data: data pointer for the ioctl
2911 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002912 *
Dave Airlief453ba02008-11-07 14:05:41 -08002913 * Remove the FB specified by the user.
2914 *
2915 * Called by the user via ioctl.
2916 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002917 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002918 * Zero on success, errno on failure.
2919 */
2920int drm_mode_rmfb(struct drm_device *dev,
2921 void *data, struct drm_file *file_priv)
2922{
Dave Airlief453ba02008-11-07 14:05:41 -08002923 struct drm_framebuffer *fb = NULL;
2924 struct drm_framebuffer *fbl = NULL;
2925 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002926 int found = 0;
2927
Dave Airliefb3b06c2011-02-08 13:55:21 +10002928 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2929 return -EINVAL;
2930
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002931 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002932 mutex_lock(&dev->mode_config.fb_lock);
2933 fb = __drm_framebuffer_lookup(dev, *id);
2934 if (!fb)
2935 goto fail_lookup;
2936
Dave Airlief453ba02008-11-07 14:05:41 -08002937 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2938 if (fb == fbl)
2939 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002940 if (!found)
2941 goto fail_lookup;
2942
2943 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2944 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002945
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002946 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002947 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002948 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002949
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002950 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002951
Daniel Vetter2b677e82012-12-10 21:16:05 +01002952 return 0;
2953
2954fail_lookup:
2955 mutex_unlock(&dev->mode_config.fb_lock);
2956 mutex_unlock(&file_priv->fbs_lock);
2957
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002958 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002959}
2960
2961/**
2962 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002963 * @dev: drm device for the ioctl
2964 * @data: data pointer for the ioctl
2965 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002966 *
Dave Airlief453ba02008-11-07 14:05:41 -08002967 * Lookup the FB given its ID and return info about it.
2968 *
2969 * Called by the user via ioctl.
2970 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002971 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002972 * Zero on success, errno on failure.
2973 */
2974int drm_mode_getfb(struct drm_device *dev,
2975 void *data, struct drm_file *file_priv)
2976{
2977 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002978 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002979 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002980
Dave Airliefb3b06c2011-02-08 13:55:21 +10002981 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2982 return -EINVAL;
2983
Daniel Vetter786b99e2012-12-02 21:53:40 +01002984 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002985 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002986 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002987
2988 r->height = fb->height;
2989 r->width = fb->width;
2990 r->depth = fb->depth;
2991 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002992 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02002993 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01002994 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01002995 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02002996 ret = fb->funcs->create_handle(fb, file_priv,
2997 &r->handle);
2998 } else {
2999 /* GET_FB() is an unprivileged ioctl so we must not
3000 * return a buffer-handle to non-master processes! For
3001 * backwards-compatibility reasons, we cannot make
3002 * GET_FB() privileged, so just return an invalid handle
3003 * for non-masters. */
3004 r->handle = 0;
3005 ret = 0;
3006 }
3007 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003008 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003009 }
Dave Airlief453ba02008-11-07 14:05:41 -08003010
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003011 drm_framebuffer_unreference(fb);
3012
Dave Airlief453ba02008-11-07 14:05:41 -08003013 return ret;
3014}
3015
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003016/**
3017 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3018 * @dev: drm device for the ioctl
3019 * @data: data pointer for the ioctl
3020 * @file_priv: drm file for the ioctl call
3021 *
3022 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3023 * rectangle list. Generic userspace which does frontbuffer rendering must call
3024 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3025 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3026 *
3027 * Modesetting drivers which always update the frontbuffer do not need to
3028 * implement the corresponding ->dirty framebuffer callback.
3029 *
3030 * Called by the user via ioctl.
3031 *
3032 * Returns:
3033 * Zero on success, errno on failure.
3034 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003035int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3036 void *data, struct drm_file *file_priv)
3037{
3038 struct drm_clip_rect __user *clips_ptr;
3039 struct drm_clip_rect *clips = NULL;
3040 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003041 struct drm_framebuffer *fb;
3042 unsigned flags;
3043 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003044 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003045
Dave Airliefb3b06c2011-02-08 13:55:21 +10003046 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3047 return -EINVAL;
3048
Daniel Vetter786b99e2012-12-02 21:53:40 +01003049 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003050 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003051 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003052
3053 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003054 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003055
3056 if (!num_clips != !clips_ptr) {
3057 ret = -EINVAL;
3058 goto out_err1;
3059 }
3060
3061 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3062
3063 /* If userspace annotates copy, clips must come in pairs */
3064 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3065 ret = -EINVAL;
3066 goto out_err1;
3067 }
3068
3069 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003070 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3071 ret = -EINVAL;
3072 goto out_err1;
3073 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003074 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3075 if (!clips) {
3076 ret = -ENOMEM;
3077 goto out_err1;
3078 }
3079
3080 ret = copy_from_user(clips, clips_ptr,
3081 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003082 if (ret) {
3083 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003084 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003085 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003086 }
3087
3088 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003089 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3090 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003091 } else {
3092 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003093 }
3094
3095out_err2:
3096 kfree(clips);
3097out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003098 drm_framebuffer_unreference(fb);
3099
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003100 return ret;
3101}
3102
3103
Dave Airlief453ba02008-11-07 14:05:41 -08003104/**
3105 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003106 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003107 *
Dave Airlief453ba02008-11-07 14:05:41 -08003108 * Destroy all the FBs associated with @filp.
3109 *
3110 * Called by the user via ioctl.
3111 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003112 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003113 * Zero on success, errno on failure.
3114 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003115void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003116{
Dave Airlief453ba02008-11-07 14:05:41 -08003117 struct drm_device *dev = priv->minor->dev;
3118 struct drm_framebuffer *fb, *tfb;
3119
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003120 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003121 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003122
3123 mutex_lock(&dev->mode_config.fb_lock);
3124 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3125 __drm_framebuffer_unregister(dev, fb);
3126 mutex_unlock(&dev->mode_config.fb_lock);
3127
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003128 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003129
3130 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003131 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003132 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003133 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003134}
3135
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003136/**
3137 * drm_property_create - create a new property type
3138 * @dev: drm device
3139 * @flags: flags specifying the property type
3140 * @name: name of the property
3141 * @num_values: number of pre-defined values
3142 *
3143 * This creates a new generic drm property which can then be attached to a drm
3144 * object with drm_object_attach_property. The returned property object must be
3145 * freed with drm_property_destroy.
3146 *
3147 * Returns:
3148 * A pointer to the newly created property on success, NULL on failure.
3149 */
Dave Airlief453ba02008-11-07 14:05:41 -08003150struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3151 const char *name, int num_values)
3152{
3153 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003154 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003155
3156 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3157 if (!property)
3158 return NULL;
3159
Rob Clark98f75de2014-05-30 11:37:03 -04003160 property->dev = dev;
3161
Dave Airlief453ba02008-11-07 14:05:41 -08003162 if (num_values) {
3163 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3164 if (!property->values)
3165 goto fail;
3166 }
3167
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003168 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3169 if (ret)
3170 goto fail;
3171
Dave Airlief453ba02008-11-07 14:05:41 -08003172 property->flags = flags;
3173 property->num_values = num_values;
3174 INIT_LIST_HEAD(&property->enum_blob_list);
3175
Vinson Lee471dd2e2011-11-10 11:55:40 -08003176 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003177 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003178 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3179 }
Dave Airlief453ba02008-11-07 14:05:41 -08003180
3181 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003182
3183 WARN_ON(!drm_property_type_valid(property));
3184
Dave Airlief453ba02008-11-07 14:05:41 -08003185 return property;
3186fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003187 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003188 kfree(property);
3189 return NULL;
3190}
3191EXPORT_SYMBOL(drm_property_create);
3192
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003193/**
3194 * drm_property_create - create a new enumeration property type
3195 * @dev: drm device
3196 * @flags: flags specifying the property type
3197 * @name: name of the property
3198 * @props: enumeration lists with property values
3199 * @num_values: number of pre-defined values
3200 *
3201 * This creates a new generic drm property which can then be attached to a drm
3202 * object with drm_object_attach_property. The returned property object must be
3203 * freed with drm_property_destroy.
3204 *
3205 * Userspace is only allowed to set one of the predefined values for enumeration
3206 * properties.
3207 *
3208 * Returns:
3209 * A pointer to the newly created property on success, NULL on failure.
3210 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003211struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3212 const char *name,
3213 const struct drm_prop_enum_list *props,
3214 int num_values)
3215{
3216 struct drm_property *property;
3217 int i, ret;
3218
3219 flags |= DRM_MODE_PROP_ENUM;
3220
3221 property = drm_property_create(dev, flags, name, num_values);
3222 if (!property)
3223 return NULL;
3224
3225 for (i = 0; i < num_values; i++) {
3226 ret = drm_property_add_enum(property, i,
3227 props[i].type,
3228 props[i].name);
3229 if (ret) {
3230 drm_property_destroy(dev, property);
3231 return NULL;
3232 }
3233 }
3234
3235 return property;
3236}
3237EXPORT_SYMBOL(drm_property_create_enum);
3238
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003239/**
3240 * drm_property_create - create a new bitmask property type
3241 * @dev: drm device
3242 * @flags: flags specifying the property type
3243 * @name: name of the property
3244 * @props: enumeration lists with property bitflags
3245 * @num_values: number of pre-defined values
3246 *
3247 * This creates a new generic drm property which can then be attached to a drm
3248 * object with drm_object_attach_property. The returned property object must be
3249 * freed with drm_property_destroy.
3250 *
3251 * Compared to plain enumeration properties userspace is allowed to set any
3252 * or'ed together combination of the predefined property bitflag values
3253 *
3254 * Returns:
3255 * A pointer to the newly created property on success, NULL on failure.
3256 */
Rob Clark49e27542012-05-17 02:23:26 -06003257struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3258 int flags, const char *name,
3259 const struct drm_prop_enum_list *props,
3260 int num_values)
3261{
3262 struct drm_property *property;
3263 int i, ret;
3264
3265 flags |= DRM_MODE_PROP_BITMASK;
3266
3267 property = drm_property_create(dev, flags, name, num_values);
3268 if (!property)
3269 return NULL;
3270
3271 for (i = 0; i < num_values; i++) {
3272 ret = drm_property_add_enum(property, i,
3273 props[i].type,
3274 props[i].name);
3275 if (ret) {
3276 drm_property_destroy(dev, property);
3277 return NULL;
3278 }
3279 }
3280
3281 return property;
3282}
3283EXPORT_SYMBOL(drm_property_create_bitmask);
3284
Rob Clarkebc44cf2012-09-12 22:22:31 -05003285static struct drm_property *property_create_range(struct drm_device *dev,
3286 int flags, const char *name,
3287 uint64_t min, uint64_t max)
3288{
3289 struct drm_property *property;
3290
3291 property = drm_property_create(dev, flags, name, 2);
3292 if (!property)
3293 return NULL;
3294
3295 property->values[0] = min;
3296 property->values[1] = max;
3297
3298 return property;
3299}
3300
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003301/**
3302 * drm_property_create - create a new ranged property type
3303 * @dev: drm device
3304 * @flags: flags specifying the property type
3305 * @name: name of the property
3306 * @min: minimum value of the property
3307 * @max: maximum value of the property
3308 *
3309 * This creates a new generic drm property which can then be attached to a drm
3310 * object with drm_object_attach_property. The returned property object must be
3311 * freed with drm_property_destroy.
3312 *
3313 * Userspace is allowed to set any interger value in the (min, max) range
3314 * inclusive.
3315 *
3316 * Returns:
3317 * A pointer to the newly created property on success, NULL on failure.
3318 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003319struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3320 const char *name,
3321 uint64_t min, uint64_t max)
3322{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003323 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3324 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003325}
3326EXPORT_SYMBOL(drm_property_create_range);
3327
Rob Clarkebc44cf2012-09-12 22:22:31 -05003328struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3329 int flags, const char *name,
3330 int64_t min, int64_t max)
3331{
3332 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3333 name, I642U64(min), I642U64(max));
3334}
3335EXPORT_SYMBOL(drm_property_create_signed_range);
3336
Rob Clark98f75de2014-05-30 11:37:03 -04003337struct drm_property *drm_property_create_object(struct drm_device *dev,
3338 int flags, const char *name, uint32_t type)
3339{
3340 struct drm_property *property;
3341
3342 flags |= DRM_MODE_PROP_OBJECT;
3343
3344 property = drm_property_create(dev, flags, name, 1);
3345 if (!property)
3346 return NULL;
3347
3348 property->values[0] = type;
3349
3350 return property;
3351}
3352EXPORT_SYMBOL(drm_property_create_object);
3353
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003354/**
3355 * drm_property_add_enum - add a possible value to an enumeration property
3356 * @property: enumeration property to change
3357 * @index: index of the new enumeration
3358 * @value: value of the new enumeration
3359 * @name: symbolic name of the new enumeration
3360 *
3361 * This functions adds enumerations to a property.
3362 *
3363 * It's use is deprecated, drivers should use one of the more specific helpers
3364 * to directly create the property with all enumerations already attached.
3365 *
3366 * Returns:
3367 * Zero on success, error code on failure.
3368 */
Dave Airlief453ba02008-11-07 14:05:41 -08003369int drm_property_add_enum(struct drm_property *property, int index,
3370 uint64_t value, const char *name)
3371{
3372 struct drm_property_enum *prop_enum;
3373
Rob Clark5ea22f22014-05-30 11:34:01 -04003374 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3375 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003376 return -EINVAL;
3377
3378 /*
3379 * Bitmask enum properties have the additional constraint of values
3380 * from 0 to 63
3381 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003382 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3383 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003384 return -EINVAL;
3385
3386 if (!list_empty(&property->enum_blob_list)) {
3387 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3388 if (prop_enum->value == value) {
3389 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3390 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3391 return 0;
3392 }
3393 }
3394 }
3395
3396 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3397 if (!prop_enum)
3398 return -ENOMEM;
3399
3400 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3401 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3402 prop_enum->value = value;
3403
3404 property->values[index] = value;
3405 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3406 return 0;
3407}
3408EXPORT_SYMBOL(drm_property_add_enum);
3409
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003410/**
3411 * drm_property_destroy - destroy a drm property
3412 * @dev: drm device
3413 * @property: property to destry
3414 *
3415 * This function frees a property including any attached resources like
3416 * enumeration values.
3417 */
Dave Airlief453ba02008-11-07 14:05:41 -08003418void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3419{
3420 struct drm_property_enum *prop_enum, *pt;
3421
3422 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3423 list_del(&prop_enum->head);
3424 kfree(prop_enum);
3425 }
3426
3427 if (property->num_values)
3428 kfree(property->values);
3429 drm_mode_object_put(dev, &property->base);
3430 list_del(&property->head);
3431 kfree(property);
3432}
3433EXPORT_SYMBOL(drm_property_destroy);
3434
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003435/**
3436 * drm_object_attach_property - attach a property to a modeset object
3437 * @obj: drm modeset object
3438 * @property: property to attach
3439 * @init_val: initial value of the property
3440 *
3441 * This attaches the given property to the modeset object with the given initial
3442 * value. Currently this function cannot fail since the properties are stored in
3443 * a statically sized array.
3444 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003445void drm_object_attach_property(struct drm_mode_object *obj,
3446 struct drm_property *property,
3447 uint64_t init_val)
3448{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003449 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003450
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003451 if (count == DRM_OBJECT_MAX_PROPERTY) {
3452 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3453 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3454 "you see this message on the same object type.\n",
3455 obj->type);
3456 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003457 }
3458
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003459 obj->properties->ids[count] = property->base.id;
3460 obj->properties->values[count] = init_val;
3461 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003462}
3463EXPORT_SYMBOL(drm_object_attach_property);
3464
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003465/**
3466 * drm_object_property_set_value - set the value of a property
3467 * @obj: drm mode object to set property value for
3468 * @property: property to set
3469 * @val: value the property should be set to
3470 *
3471 * This functions sets a given property on a given object. This function only
3472 * changes the software state of the property, it does not call into the
3473 * driver's ->set_property callback.
3474 *
3475 * Returns:
3476 * Zero on success, error code on failure.
3477 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003478int drm_object_property_set_value(struct drm_mode_object *obj,
3479 struct drm_property *property, uint64_t val)
3480{
3481 int i;
3482
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003483 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003484 if (obj->properties->ids[i] == property->base.id) {
3485 obj->properties->values[i] = val;
3486 return 0;
3487 }
3488 }
3489
3490 return -EINVAL;
3491}
3492EXPORT_SYMBOL(drm_object_property_set_value);
3493
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003494/**
3495 * drm_object_property_get_value - retrieve the value of a property
3496 * @obj: drm mode object to get property value from
3497 * @property: property to retrieve
3498 * @val: storage for the property value
3499 *
3500 * This function retrieves the softare state of the given property for the given
3501 * property. Since there is no driver callback to retrieve the current property
3502 * value this might be out of sync with the hardware, depending upon the driver
3503 * and property.
3504 *
3505 * Returns:
3506 * Zero on success, error code on failure.
3507 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003508int drm_object_property_get_value(struct drm_mode_object *obj,
3509 struct drm_property *property, uint64_t *val)
3510{
3511 int i;
3512
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003513 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003514 if (obj->properties->ids[i] == property->base.id) {
3515 *val = obj->properties->values[i];
3516 return 0;
3517 }
3518 }
3519
3520 return -EINVAL;
3521}
3522EXPORT_SYMBOL(drm_object_property_get_value);
3523
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003524/**
3525 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3526 * @dev: DRM device
3527 * @data: ioctl data
3528 * @file_priv: DRM file info
3529 *
3530 * This function retrieves the current value for an connectors's property.
3531 *
3532 * Called by the user via ioctl.
3533 *
3534 * Returns:
3535 * Zero on success, errno on failure.
3536 */
Dave Airlief453ba02008-11-07 14:05:41 -08003537int drm_mode_getproperty_ioctl(struct drm_device *dev,
3538 void *data, struct drm_file *file_priv)
3539{
Dave Airlief453ba02008-11-07 14:05:41 -08003540 struct drm_mode_get_property *out_resp = data;
3541 struct drm_property *property;
3542 int enum_count = 0;
3543 int blob_count = 0;
3544 int value_count = 0;
3545 int ret = 0, i;
3546 int copied;
3547 struct drm_property_enum *prop_enum;
3548 struct drm_mode_property_enum __user *enum_ptr;
3549 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003550 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003551 uint64_t __user *values_ptr;
3552 uint32_t __user *blob_length_ptr;
3553
Dave Airliefb3b06c2011-02-08 13:55:21 +10003554 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3555 return -EINVAL;
3556
Daniel Vetter84849902012-12-02 00:28:11 +01003557 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003558 property = drm_property_find(dev, out_resp->prop_id);
3559 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003560 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003561 goto done;
3562 }
Dave Airlief453ba02008-11-07 14:05:41 -08003563
Rob Clark5ea22f22014-05-30 11:34:01 -04003564 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3565 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003566 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3567 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003568 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003569 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3570 blob_count++;
3571 }
3572
3573 value_count = property->num_values;
3574
3575 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3576 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3577 out_resp->flags = property->flags;
3578
3579 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003580 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003581 for (i = 0; i < value_count; i++) {
3582 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3583 ret = -EFAULT;
3584 goto done;
3585 }
3586 }
3587 }
3588 out_resp->count_values = value_count;
3589
Rob Clark5ea22f22014-05-30 11:34:01 -04003590 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3591 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003592 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3593 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003594 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003595 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3596
3597 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3598 ret = -EFAULT;
3599 goto done;
3600 }
3601
3602 if (copy_to_user(&enum_ptr[copied].name,
3603 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3604 ret = -EFAULT;
3605 goto done;
3606 }
3607 copied++;
3608 }
3609 }
3610 out_resp->count_enum_blobs = enum_count;
3611 }
3612
Rob Clark5ea22f22014-05-30 11:34:01 -04003613 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003614 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3615 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003616 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3617 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003618
3619 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3620 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3621 ret = -EFAULT;
3622 goto done;
3623 }
3624
3625 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3626 ret = -EFAULT;
3627 goto done;
3628 }
3629
3630 copied++;
3631 }
3632 }
3633 out_resp->count_enum_blobs = blob_count;
3634 }
3635done:
Daniel Vetter84849902012-12-02 00:28:11 +01003636 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003637 return ret;
3638}
3639
3640static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3641 void *data)
3642{
3643 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003644 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003645
3646 if (!length || !data)
3647 return NULL;
3648
3649 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3650 if (!blob)
3651 return NULL;
3652
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003653 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3654 if (ret) {
3655 kfree(blob);
3656 return NULL;
3657 }
3658
Dave Airlief453ba02008-11-07 14:05:41 -08003659 blob->length = length;
3660
3661 memcpy(blob->data, data, length);
3662
Dave Airlief453ba02008-11-07 14:05:41 -08003663 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3664 return blob;
3665}
3666
3667static void drm_property_destroy_blob(struct drm_device *dev,
3668 struct drm_property_blob *blob)
3669{
3670 drm_mode_object_put(dev, &blob->base);
3671 list_del(&blob->head);
3672 kfree(blob);
3673}
3674
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003675/**
3676 * drm_mode_getblob_ioctl - get the contents of a blob property value
3677 * @dev: DRM device
3678 * @data: ioctl data
3679 * @file_priv: DRM file info
3680 *
3681 * This function retrieves the contents of a blob property. The value stored in
3682 * an object's blob property is just a normal modeset object id.
3683 *
3684 * Called by the user via ioctl.
3685 *
3686 * Returns:
3687 * Zero on success, errno on failure.
3688 */
Dave Airlief453ba02008-11-07 14:05:41 -08003689int drm_mode_getblob_ioctl(struct drm_device *dev,
3690 void *data, struct drm_file *file_priv)
3691{
Dave Airlief453ba02008-11-07 14:05:41 -08003692 struct drm_mode_get_blob *out_resp = data;
3693 struct drm_property_blob *blob;
3694 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003695 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003696
Dave Airliefb3b06c2011-02-08 13:55:21 +10003697 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3698 return -EINVAL;
3699
Daniel Vetter84849902012-12-02 00:28:11 +01003700 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003701 blob = drm_property_blob_find(dev, out_resp->blob_id);
3702 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003703 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003704 goto done;
3705 }
Dave Airlief453ba02008-11-07 14:05:41 -08003706
3707 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003708 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003709 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3710 ret = -EFAULT;
3711 goto done;
3712 }
3713 }
3714 out_resp->length = blob->length;
3715
3716done:
Daniel Vetter84849902012-12-02 00:28:11 +01003717 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003718 return ret;
3719}
3720
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003721/**
3722 * drm_mode_connector_update_edid_property - update the edid property of a connector
3723 * @connector: drm connector
3724 * @edid: new value of the edid property
3725 *
3726 * This function creates a new blob modeset object and assigns its id to the
3727 * connector's edid property.
3728 *
3729 * Returns:
3730 * Zero on success, errno on failure.
3731 */
Dave Airlief453ba02008-11-07 14:05:41 -08003732int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3733 struct edid *edid)
3734{
3735 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003736 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003737
3738 if (connector->edid_blob_ptr)
3739 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3740
3741 /* Delete edid, when there is none. */
3742 if (!edid) {
3743 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003744 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003745 return ret;
3746 }
3747
Adam Jackson7466f4c2010-03-29 21:43:23 +00003748 size = EDID_LENGTH * (1 + edid->extensions);
3749 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3750 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003751 if (!connector->edid_blob_ptr)
3752 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003753
Rob Clark58495562012-10-11 20:50:56 -05003754 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003755 dev->mode_config.edid_property,
3756 connector->edid_blob_ptr->base.id);
3757
3758 return ret;
3759}
3760EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3761
Paulo Zanoni26a34812012-05-15 18:08:59 -03003762static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003763 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003764{
3765 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3766 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003767
3768 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003769 if (value < property->values[0] || value > property->values[1])
3770 return false;
3771 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003772 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3773 int64_t svalue = U642I64(value);
3774 if (svalue < U642I64(property->values[0]) ||
3775 svalue > U642I64(property->values[1]))
3776 return false;
3777 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003778 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06003779 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003780 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003781 for (i = 0; i < property->num_values; i++)
3782 valid_mask |= (1ULL << property->values[i]);
3783 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003784 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003785 /* Only the driver knows */
3786 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04003787 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3788 struct drm_mode_object *obj;
3789 /* a zero value for an object property translates to null: */
3790 if (value == 0)
3791 return true;
3792 /*
3793 * NOTE: use _object_find() directly to bypass restriction on
3794 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3795 * object this could race against object finalization, so it
3796 * simply tells us that the object *was* valid. Which is good
3797 * enough.
3798 */
3799 obj = _object_find(property->dev, value, property->values[0]);
3800 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003801 } else {
3802 int i;
3803 for (i = 0; i < property->num_values; i++)
3804 if (property->values[i] == value)
3805 return true;
3806 return false;
3807 }
3808}
3809
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003810/**
3811 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3812 * @dev: DRM device
3813 * @data: ioctl data
3814 * @file_priv: DRM file info
3815 *
3816 * This function sets the current value for a connectors's property. It also
3817 * calls into a driver's ->set_property callback to update the hardware state
3818 *
3819 * Called by the user via ioctl.
3820 *
3821 * Returns:
3822 * Zero on success, errno on failure.
3823 */
Dave Airlief453ba02008-11-07 14:05:41 -08003824int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3825 void *data, struct drm_file *file_priv)
3826{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003827 struct drm_mode_connector_set_property *conn_set_prop = data;
3828 struct drm_mode_obj_set_property obj_set_prop = {
3829 .value = conn_set_prop->value,
3830 .prop_id = conn_set_prop->prop_id,
3831 .obj_id = conn_set_prop->connector_id,
3832 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3833 };
Dave Airlief453ba02008-11-07 14:05:41 -08003834
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003835 /* It does all the locking and checking we need */
3836 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003837}
3838
Paulo Zanonic5431882012-05-15 18:09:02 -03003839static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3840 struct drm_property *property,
3841 uint64_t value)
3842{
3843 int ret = -EINVAL;
3844 struct drm_connector *connector = obj_to_connector(obj);
3845
3846 /* Do DPMS ourselves */
3847 if (property == connector->dev->mode_config.dpms_property) {
3848 if (connector->funcs->dpms)
3849 (*connector->funcs->dpms)(connector, (int)value);
3850 ret = 0;
3851 } else if (connector->funcs->set_property)
3852 ret = connector->funcs->set_property(connector, property, value);
3853
3854 /* store the property value if successful */
3855 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003856 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003857 return ret;
3858}
3859
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003860static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3861 struct drm_property *property,
3862 uint64_t value)
3863{
3864 int ret = -EINVAL;
3865 struct drm_crtc *crtc = obj_to_crtc(obj);
3866
3867 if (crtc->funcs->set_property)
3868 ret = crtc->funcs->set_property(crtc, property, value);
3869 if (!ret)
3870 drm_object_property_set_value(obj, property, value);
3871
3872 return ret;
3873}
3874
Rob Clark4d939142012-05-17 02:23:27 -06003875static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3876 struct drm_property *property,
3877 uint64_t value)
3878{
3879 int ret = -EINVAL;
3880 struct drm_plane *plane = obj_to_plane(obj);
3881
3882 if (plane->funcs->set_property)
3883 ret = plane->funcs->set_property(plane, property, value);
3884 if (!ret)
3885 drm_object_property_set_value(obj, property, value);
3886
3887 return ret;
3888}
3889
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003890/**
3891 * drm_mode_getproperty_ioctl - get the current value of a object's property
3892 * @dev: DRM device
3893 * @data: ioctl data
3894 * @file_priv: DRM file info
3895 *
3896 * This function retrieves the current value for an object's property. Compared
3897 * to the connector specific ioctl this one is extended to also work on crtc and
3898 * plane objects.
3899 *
3900 * Called by the user via ioctl.
3901 *
3902 * Returns:
3903 * Zero on success, errno on failure.
3904 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003905int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3906 struct drm_file *file_priv)
3907{
3908 struct drm_mode_obj_get_properties *arg = data;
3909 struct drm_mode_object *obj;
3910 int ret = 0;
3911 int i;
3912 int copied = 0;
3913 int props_count = 0;
3914 uint32_t __user *props_ptr;
3915 uint64_t __user *prop_values_ptr;
3916
3917 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3918 return -EINVAL;
3919
Daniel Vetter84849902012-12-02 00:28:11 +01003920 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003921
3922 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3923 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003924 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003925 goto out;
3926 }
3927 if (!obj->properties) {
3928 ret = -EINVAL;
3929 goto out;
3930 }
3931
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003932 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003933
3934 /* This ioctl is called twice, once to determine how much space is
3935 * needed, and the 2nd time to fill it. */
3936 if ((arg->count_props >= props_count) && props_count) {
3937 copied = 0;
3938 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3939 prop_values_ptr = (uint64_t __user *)(unsigned long)
3940 (arg->prop_values_ptr);
3941 for (i = 0; i < props_count; i++) {
3942 if (put_user(obj->properties->ids[i],
3943 props_ptr + copied)) {
3944 ret = -EFAULT;
3945 goto out;
3946 }
3947 if (put_user(obj->properties->values[i],
3948 prop_values_ptr + copied)) {
3949 ret = -EFAULT;
3950 goto out;
3951 }
3952 copied++;
3953 }
3954 }
3955 arg->count_props = props_count;
3956out:
Daniel Vetter84849902012-12-02 00:28:11 +01003957 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003958 return ret;
3959}
3960
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003961/**
3962 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3963 * @dev: DRM device
3964 * @data: ioctl data
3965 * @file_priv: DRM file info
3966 *
3967 * This function sets the current value for an object's property. It also calls
3968 * into a driver's ->set_property callback to update the hardware state.
3969 * Compared to the connector specific ioctl this one is extended to also work on
3970 * crtc and plane objects.
3971 *
3972 * Called by the user via ioctl.
3973 *
3974 * Returns:
3975 * Zero on success, errno on failure.
3976 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003977int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3978 struct drm_file *file_priv)
3979{
3980 struct drm_mode_obj_set_property *arg = data;
3981 struct drm_mode_object *arg_obj;
3982 struct drm_mode_object *prop_obj;
3983 struct drm_property *property;
3984 int ret = -EINVAL;
3985 int i;
3986
3987 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3988 return -EINVAL;
3989
Daniel Vetter84849902012-12-02 00:28:11 +01003990 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003991
3992 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003993 if (!arg_obj) {
3994 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03003995 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003996 }
Paulo Zanonic5431882012-05-15 18:09:02 -03003997 if (!arg_obj->properties)
3998 goto out;
3999
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004000 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004001 if (arg_obj->properties->ids[i] == arg->prop_id)
4002 break;
4003
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004004 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004005 goto out;
4006
4007 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4008 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004009 if (!prop_obj) {
4010 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004011 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004012 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004013 property = obj_to_property(prop_obj);
4014
4015 if (!drm_property_change_is_valid(property, arg->value))
4016 goto out;
4017
4018 switch (arg_obj->type) {
4019 case DRM_MODE_OBJECT_CONNECTOR:
4020 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4021 arg->value);
4022 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004023 case DRM_MODE_OBJECT_CRTC:
4024 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4025 break;
Rob Clark4d939142012-05-17 02:23:27 -06004026 case DRM_MODE_OBJECT_PLANE:
4027 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4028 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004029 }
4030
4031out:
Daniel Vetter84849902012-12-02 00:28:11 +01004032 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004033 return ret;
4034}
4035
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004036/**
4037 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4038 * @connector: connector to attach
4039 * @encoder: encoder to attach @connector to
4040 *
4041 * This function links up a connector to an encoder. Note that the routing
4042 * restrictions between encoders and crtcs are exposed to userspace through the
4043 * possible_clones and possible_crtcs bitmasks.
4044 *
4045 * Returns:
4046 * Zero on success, errno on failure.
4047 */
Dave Airlief453ba02008-11-07 14:05:41 -08004048int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4049 struct drm_encoder *encoder)
4050{
4051 int i;
4052
4053 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4054 if (connector->encoder_ids[i] == 0) {
4055 connector->encoder_ids[i] = encoder->base.id;
4056 return 0;
4057 }
4058 }
4059 return -ENOMEM;
4060}
4061EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4062
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004063/**
4064 * drm_mode_crtc_set_gamma_size - set the gamma table size
4065 * @crtc: CRTC to set the gamma table size for
4066 * @gamma_size: size of the gamma table
4067 *
4068 * Drivers which support gamma tables should set this to the supported gamma
4069 * table size when initializing the CRTC. Currently the drm core only supports a
4070 * fixed gamma table size.
4071 *
4072 * Returns:
4073 * Zero on success, errno on failure.
4074 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004075int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004076 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004077{
4078 crtc->gamma_size = gamma_size;
4079
4080 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4081 if (!crtc->gamma_store) {
4082 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004083 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004084 }
4085
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004086 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004087}
4088EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4089
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004090/**
4091 * drm_mode_gamma_set_ioctl - set the gamma table
4092 * @dev: DRM device
4093 * @data: ioctl data
4094 * @file_priv: DRM file info
4095 *
4096 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4097 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4098 *
4099 * Called by the user via ioctl.
4100 *
4101 * Returns:
4102 * Zero on success, errno on failure.
4103 */
Dave Airlief453ba02008-11-07 14:05:41 -08004104int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4105 void *data, struct drm_file *file_priv)
4106{
4107 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004108 struct drm_crtc *crtc;
4109 void *r_base, *g_base, *b_base;
4110 int size;
4111 int ret = 0;
4112
Dave Airliefb3b06c2011-02-08 13:55:21 +10004113 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4114 return -EINVAL;
4115
Daniel Vetter84849902012-12-02 00:28:11 +01004116 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004117 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4118 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004119 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004120 goto out;
4121 }
Dave Airlief453ba02008-11-07 14:05:41 -08004122
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004123 if (crtc->funcs->gamma_set == NULL) {
4124 ret = -ENOSYS;
4125 goto out;
4126 }
4127
Dave Airlief453ba02008-11-07 14:05:41 -08004128 /* memcpy into gamma store */
4129 if (crtc_lut->gamma_size != crtc->gamma_size) {
4130 ret = -EINVAL;
4131 goto out;
4132 }
4133
4134 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4135 r_base = crtc->gamma_store;
4136 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4137 ret = -EFAULT;
4138 goto out;
4139 }
4140
4141 g_base = r_base + size;
4142 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4143 ret = -EFAULT;
4144 goto out;
4145 }
4146
4147 b_base = g_base + size;
4148 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4149 ret = -EFAULT;
4150 goto out;
4151 }
4152
James Simmons72034252010-08-03 01:33:19 +01004153 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004154
4155out:
Daniel Vetter84849902012-12-02 00:28:11 +01004156 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004157 return ret;
4158
4159}
4160
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004161/**
4162 * drm_mode_gamma_get_ioctl - get the gamma table
4163 * @dev: DRM device
4164 * @data: ioctl data
4165 * @file_priv: DRM file info
4166 *
4167 * Copy the current gamma table into the storage provided. This also provides
4168 * the gamma table size the driver expects, which can be used to size the
4169 * allocated storage.
4170 *
4171 * Called by the user via ioctl.
4172 *
4173 * Returns:
4174 * Zero on success, errno on failure.
4175 */
Dave Airlief453ba02008-11-07 14:05:41 -08004176int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4177 void *data, struct drm_file *file_priv)
4178{
4179 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004180 struct drm_crtc *crtc;
4181 void *r_base, *g_base, *b_base;
4182 int size;
4183 int ret = 0;
4184
Dave Airliefb3b06c2011-02-08 13:55:21 +10004185 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4186 return -EINVAL;
4187
Daniel Vetter84849902012-12-02 00:28:11 +01004188 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004189 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4190 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004191 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004192 goto out;
4193 }
Dave Airlief453ba02008-11-07 14:05:41 -08004194
4195 /* memcpy into gamma store */
4196 if (crtc_lut->gamma_size != crtc->gamma_size) {
4197 ret = -EINVAL;
4198 goto out;
4199 }
4200
4201 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4202 r_base = crtc->gamma_store;
4203 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4204 ret = -EFAULT;
4205 goto out;
4206 }
4207
4208 g_base = r_base + size;
4209 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4210 ret = -EFAULT;
4211 goto out;
4212 }
4213
4214 b_base = g_base + size;
4215 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4216 ret = -EFAULT;
4217 goto out;
4218 }
4219out:
Daniel Vetter84849902012-12-02 00:28:11 +01004220 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004221 return ret;
4222}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004223
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004224/**
4225 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4226 * @dev: DRM device
4227 * @data: ioctl data
4228 * @file_priv: DRM file info
4229 *
4230 * This schedules an asynchronous update on a given CRTC, called page flip.
4231 * Optionally a drm event is generated to signal the completion of the event.
4232 * Generic drivers cannot assume that a pageflip with changed framebuffer
4233 * properties (including driver specific metadata like tiling layout) will work,
4234 * but some drivers support e.g. pixel format changes through the pageflip
4235 * ioctl.
4236 *
4237 * Called by the user via ioctl.
4238 *
4239 * Returns:
4240 * Zero on success, errno on failure.
4241 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004242int drm_mode_page_flip_ioctl(struct drm_device *dev,
4243 void *data, struct drm_file *file_priv)
4244{
4245 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004246 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004247 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004248 struct drm_pending_vblank_event *e = NULL;
4249 unsigned long flags;
4250 int ret = -EINVAL;
4251
4252 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4253 page_flip->reserved != 0)
4254 return -EINVAL;
4255
Keith Packard62f21042013-07-22 18:50:00 -07004256 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4257 return -EINVAL;
4258
Rob Clarka2b34e22013-10-05 16:36:52 -04004259 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4260 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004261 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004262
Rob Clark51fd3712013-11-19 12:10:12 -05004263 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004264 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004265 /* The framebuffer is currently unbound, presumably
4266 * due to a hotplug event, that userspace has not
4267 * yet discovered.
4268 */
4269 ret = -EBUSY;
4270 goto out;
4271 }
4272
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004273 if (crtc->funcs->page_flip == NULL)
4274 goto out;
4275
Daniel Vetter786b99e2012-12-02 21:53:40 +01004276 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004277 if (!fb) {
4278 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004279 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004280 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004281
Damien Lespiauc11e9282013-09-25 16:45:30 +01004282 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4283 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004284 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004285
Matt Roperf4510a22014-04-01 15:22:40 -07004286 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004287 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4288 ret = -EINVAL;
4289 goto out;
4290 }
4291
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004292 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4293 ret = -ENOMEM;
4294 spin_lock_irqsave(&dev->event_lock, flags);
4295 if (file_priv->event_space < sizeof e->event) {
4296 spin_unlock_irqrestore(&dev->event_lock, flags);
4297 goto out;
4298 }
4299 file_priv->event_space -= sizeof e->event;
4300 spin_unlock_irqrestore(&dev->event_lock, flags);
4301
4302 e = kzalloc(sizeof *e, GFP_KERNEL);
4303 if (e == NULL) {
4304 spin_lock_irqsave(&dev->event_lock, flags);
4305 file_priv->event_space += sizeof e->event;
4306 spin_unlock_irqrestore(&dev->event_lock, flags);
4307 goto out;
4308 }
4309
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004310 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004311 e->event.base.length = sizeof e->event;
4312 e->event.user_data = page_flip->user_data;
4313 e->base.event = &e->event.base;
4314 e->base.file_priv = file_priv;
4315 e->base.destroy =
4316 (void (*) (struct drm_pending_event *)) kfree;
4317 }
4318
Matt Roperf4510a22014-04-01 15:22:40 -07004319 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004320 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004321 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004322 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4323 spin_lock_irqsave(&dev->event_lock, flags);
4324 file_priv->event_space += sizeof e->event;
4325 spin_unlock_irqrestore(&dev->event_lock, flags);
4326 kfree(e);
4327 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004328 /* Keep the old fb, don't unref it. */
4329 old_fb = NULL;
4330 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004331 /*
4332 * Warn if the driver hasn't properly updated the crtc->fb
4333 * field to reflect that the new framebuffer is now used.
4334 * Failing to do so will screw with the reference counting
4335 * on framebuffers.
4336 */
Matt Roperf4510a22014-04-01 15:22:40 -07004337 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004338 /* Unref only the old framebuffer. */
4339 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004340 }
4341
4342out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004343 if (fb)
4344 drm_framebuffer_unreference(fb);
4345 if (old_fb)
4346 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004347 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004348
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004349 return ret;
4350}
Chris Wilsoneb033552011-01-24 15:11:08 +00004351
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004352/**
4353 * drm_mode_config_reset - call ->reset callbacks
4354 * @dev: drm device
4355 *
4356 * This functions calls all the crtc's, encoder's and connector's ->reset
4357 * callback. Drivers can use this in e.g. their driver load or resume code to
4358 * reset hardware and software state.
4359 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004360void drm_mode_config_reset(struct drm_device *dev)
4361{
4362 struct drm_crtc *crtc;
4363 struct drm_encoder *encoder;
4364 struct drm_connector *connector;
4365
4366 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4367 if (crtc->funcs->reset)
4368 crtc->funcs->reset(crtc);
4369
4370 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4371 if (encoder->funcs->reset)
4372 encoder->funcs->reset(encoder);
4373
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004374 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4375 connector->status = connector_status_unknown;
4376
Chris Wilsoneb033552011-01-24 15:11:08 +00004377 if (connector->funcs->reset)
4378 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004379 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004380}
4381EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004382
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004383/**
4384 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4385 * @dev: DRM device
4386 * @data: ioctl data
4387 * @file_priv: DRM file info
4388 *
4389 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4390 * TTM or something else entirely) and returns the resulting buffer handle. This
4391 * handle can then be wrapped up into a framebuffer modeset object.
4392 *
4393 * Note that userspace is not allowed to use such objects for render
4394 * acceleration - drivers must create their own private ioctls for such a use
4395 * case.
4396 *
4397 * Called by the user via ioctl.
4398 *
4399 * Returns:
4400 * Zero on success, errno on failure.
4401 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004402int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4403 void *data, struct drm_file *file_priv)
4404{
4405 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004406 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004407
4408 if (!dev->driver->dumb_create)
4409 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004410 if (!args->width || !args->height || !args->bpp)
4411 return -EINVAL;
4412
4413 /* overflow checks for 32bit size calculations */
4414 cpp = DIV_ROUND_UP(args->bpp, 8);
4415 if (cpp > 0xffffffffU / args->width)
4416 return -EINVAL;
4417 stride = cpp * args->width;
4418 if (args->height > 0xffffffffU / stride)
4419 return -EINVAL;
4420
4421 /* test for wrap-around */
4422 size = args->height * stride;
4423 if (PAGE_ALIGN(size) == 0)
4424 return -EINVAL;
4425
Dave Airlieff72145b2011-02-07 12:16:14 +10004426 return dev->driver->dumb_create(file_priv, dev, args);
4427}
4428
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004429/**
4430 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4431 * @dev: DRM device
4432 * @data: ioctl data
4433 * @file_priv: DRM file info
4434 *
4435 * Allocate an offset in the drm device node's address space to be able to
4436 * memory map a dumb buffer.
4437 *
4438 * Called by the user via ioctl.
4439 *
4440 * Returns:
4441 * Zero on success, errno on failure.
4442 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004443int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4444 void *data, struct drm_file *file_priv)
4445{
4446 struct drm_mode_map_dumb *args = data;
4447
4448 /* call driver ioctl to get mmap offset */
4449 if (!dev->driver->dumb_map_offset)
4450 return -ENOSYS;
4451
4452 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4453}
4454
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004455/**
4456 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4457 * @dev: DRM device
4458 * @data: ioctl data
4459 * @file_priv: DRM file info
4460 *
4461 * This destroys the userspace handle for the given dumb backing storage buffer.
4462 * Since buffer objects must be reference counted in the kernel a buffer object
4463 * won't be immediately freed if a framebuffer modeset object still uses it.
4464 *
4465 * Called by the user via ioctl.
4466 *
4467 * Returns:
4468 * Zero on success, errno on failure.
4469 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004470int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4471 void *data, struct drm_file *file_priv)
4472{
4473 struct drm_mode_destroy_dumb *args = data;
4474
4475 if (!dev->driver->dumb_destroy)
4476 return -ENOSYS;
4477
4478 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4479}
Dave Airlie248dbc22011-11-29 20:02:54 +00004480
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004481/**
4482 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4483 * @format: pixel format (DRM_FORMAT_*)
4484 * @depth: storage for the depth value
4485 * @bpp: storage for the bpp value
4486 *
4487 * This only supports RGB formats here for compat with code that doesn't use
4488 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004489 */
4490void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4491 int *bpp)
4492{
4493 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004494 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004495 case DRM_FORMAT_RGB332:
4496 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004497 *depth = 8;
4498 *bpp = 8;
4499 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004500 case DRM_FORMAT_XRGB1555:
4501 case DRM_FORMAT_XBGR1555:
4502 case DRM_FORMAT_RGBX5551:
4503 case DRM_FORMAT_BGRX5551:
4504 case DRM_FORMAT_ARGB1555:
4505 case DRM_FORMAT_ABGR1555:
4506 case DRM_FORMAT_RGBA5551:
4507 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004508 *depth = 15;
4509 *bpp = 16;
4510 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004511 case DRM_FORMAT_RGB565:
4512 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004513 *depth = 16;
4514 *bpp = 16;
4515 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004516 case DRM_FORMAT_RGB888:
4517 case DRM_FORMAT_BGR888:
4518 *depth = 24;
4519 *bpp = 24;
4520 break;
4521 case DRM_FORMAT_XRGB8888:
4522 case DRM_FORMAT_XBGR8888:
4523 case DRM_FORMAT_RGBX8888:
4524 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004525 *depth = 24;
4526 *bpp = 32;
4527 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004528 case DRM_FORMAT_XRGB2101010:
4529 case DRM_FORMAT_XBGR2101010:
4530 case DRM_FORMAT_RGBX1010102:
4531 case DRM_FORMAT_BGRX1010102:
4532 case DRM_FORMAT_ARGB2101010:
4533 case DRM_FORMAT_ABGR2101010:
4534 case DRM_FORMAT_RGBA1010102:
4535 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004536 *depth = 30;
4537 *bpp = 32;
4538 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004539 case DRM_FORMAT_ARGB8888:
4540 case DRM_FORMAT_ABGR8888:
4541 case DRM_FORMAT_RGBA8888:
4542 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004543 *depth = 32;
4544 *bpp = 32;
4545 break;
4546 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004547 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4548 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004549 *depth = 0;
4550 *bpp = 0;
4551 break;
4552 }
4553}
4554EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004555
4556/**
4557 * drm_format_num_planes - get the number of planes for format
4558 * @format: pixel format (DRM_FORMAT_*)
4559 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004560 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004561 * The number of planes used by the specified pixel format.
4562 */
4563int drm_format_num_planes(uint32_t format)
4564{
4565 switch (format) {
4566 case DRM_FORMAT_YUV410:
4567 case DRM_FORMAT_YVU410:
4568 case DRM_FORMAT_YUV411:
4569 case DRM_FORMAT_YVU411:
4570 case DRM_FORMAT_YUV420:
4571 case DRM_FORMAT_YVU420:
4572 case DRM_FORMAT_YUV422:
4573 case DRM_FORMAT_YVU422:
4574 case DRM_FORMAT_YUV444:
4575 case DRM_FORMAT_YVU444:
4576 return 3;
4577 case DRM_FORMAT_NV12:
4578 case DRM_FORMAT_NV21:
4579 case DRM_FORMAT_NV16:
4580 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004581 case DRM_FORMAT_NV24:
4582 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004583 return 2;
4584 default:
4585 return 1;
4586 }
4587}
4588EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004589
4590/**
4591 * drm_format_plane_cpp - determine the bytes per pixel value
4592 * @format: pixel format (DRM_FORMAT_*)
4593 * @plane: plane index
4594 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004595 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004596 * The bytes per pixel value for the specified plane.
4597 */
4598int drm_format_plane_cpp(uint32_t format, int plane)
4599{
4600 unsigned int depth;
4601 int bpp;
4602
4603 if (plane >= drm_format_num_planes(format))
4604 return 0;
4605
4606 switch (format) {
4607 case DRM_FORMAT_YUYV:
4608 case DRM_FORMAT_YVYU:
4609 case DRM_FORMAT_UYVY:
4610 case DRM_FORMAT_VYUY:
4611 return 2;
4612 case DRM_FORMAT_NV12:
4613 case DRM_FORMAT_NV21:
4614 case DRM_FORMAT_NV16:
4615 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004616 case DRM_FORMAT_NV24:
4617 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004618 return plane ? 2 : 1;
4619 case DRM_FORMAT_YUV410:
4620 case DRM_FORMAT_YVU410:
4621 case DRM_FORMAT_YUV411:
4622 case DRM_FORMAT_YVU411:
4623 case DRM_FORMAT_YUV420:
4624 case DRM_FORMAT_YVU420:
4625 case DRM_FORMAT_YUV422:
4626 case DRM_FORMAT_YVU422:
4627 case DRM_FORMAT_YUV444:
4628 case DRM_FORMAT_YVU444:
4629 return 1;
4630 default:
4631 drm_fb_get_bpp_depth(format, &depth, &bpp);
4632 return bpp >> 3;
4633 }
4634}
4635EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004636
4637/**
4638 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4639 * @format: pixel format (DRM_FORMAT_*)
4640 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004641 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004642 * The horizontal chroma subsampling factor for the
4643 * specified pixel format.
4644 */
4645int drm_format_horz_chroma_subsampling(uint32_t format)
4646{
4647 switch (format) {
4648 case DRM_FORMAT_YUV411:
4649 case DRM_FORMAT_YVU411:
4650 case DRM_FORMAT_YUV410:
4651 case DRM_FORMAT_YVU410:
4652 return 4;
4653 case DRM_FORMAT_YUYV:
4654 case DRM_FORMAT_YVYU:
4655 case DRM_FORMAT_UYVY:
4656 case DRM_FORMAT_VYUY:
4657 case DRM_FORMAT_NV12:
4658 case DRM_FORMAT_NV21:
4659 case DRM_FORMAT_NV16:
4660 case DRM_FORMAT_NV61:
4661 case DRM_FORMAT_YUV422:
4662 case DRM_FORMAT_YVU422:
4663 case DRM_FORMAT_YUV420:
4664 case DRM_FORMAT_YVU420:
4665 return 2;
4666 default:
4667 return 1;
4668 }
4669}
4670EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4671
4672/**
4673 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4674 * @format: pixel format (DRM_FORMAT_*)
4675 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004676 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004677 * The vertical chroma subsampling factor for the
4678 * specified pixel format.
4679 */
4680int drm_format_vert_chroma_subsampling(uint32_t format)
4681{
4682 switch (format) {
4683 case DRM_FORMAT_YUV410:
4684 case DRM_FORMAT_YVU410:
4685 return 4;
4686 case DRM_FORMAT_YUV420:
4687 case DRM_FORMAT_YVU420:
4688 case DRM_FORMAT_NV12:
4689 case DRM_FORMAT_NV21:
4690 return 2;
4691 default:
4692 return 1;
4693 }
4694}
4695EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004696
4697/**
4698 * drm_mode_config_init - initialize DRM mode_configuration structure
4699 * @dev: DRM device
4700 *
4701 * Initialize @dev's mode_config structure, used for tracking the graphics
4702 * configuration of @dev.
4703 *
4704 * Since this initializes the modeset locks, no locking is possible. Which is no
4705 * problem, since this should happen single threaded at init time. It is the
4706 * driver's problem to ensure this guarantee.
4707 *
4708 */
4709void drm_mode_config_init(struct drm_device *dev)
4710{
4711 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004712 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004713 mutex_init(&dev->mode_config.idr_mutex);
4714 mutex_init(&dev->mode_config.fb_lock);
4715 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4716 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4717 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004718 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004719 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4720 INIT_LIST_HEAD(&dev->mode_config.property_list);
4721 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4722 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4723 idr_init(&dev->mode_config.crtc_idr);
4724
4725 drm_modeset_lock_all(dev);
4726 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004727 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004728 drm_modeset_unlock_all(dev);
4729
4730 /* Just to be sure */
4731 dev->mode_config.num_fb = 0;
4732 dev->mode_config.num_connector = 0;
4733 dev->mode_config.num_crtc = 0;
4734 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004735 dev->mode_config.num_overlay_plane = 0;
4736 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004737}
4738EXPORT_SYMBOL(drm_mode_config_init);
4739
4740/**
4741 * drm_mode_config_cleanup - free up DRM mode_config info
4742 * @dev: DRM device
4743 *
4744 * Free up all the connectors and CRTCs associated with this DRM device, then
4745 * free up the framebuffers and associated buffer objects.
4746 *
4747 * Note that since this /should/ happen single-threaded at driver/device
4748 * teardown time, no locking is required. It's the driver's job to ensure that
4749 * this guarantee actually holds true.
4750 *
4751 * FIXME: cleanup any dangling user buffer objects too
4752 */
4753void drm_mode_config_cleanup(struct drm_device *dev)
4754{
4755 struct drm_connector *connector, *ot;
4756 struct drm_crtc *crtc, *ct;
4757 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004758 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004759 struct drm_framebuffer *fb, *fbt;
4760 struct drm_property *property, *pt;
4761 struct drm_property_blob *blob, *bt;
4762 struct drm_plane *plane, *plt;
4763
4764 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4765 head) {
4766 encoder->funcs->destroy(encoder);
4767 }
4768
Sean Paul3b336ec2013-08-14 16:47:37 -04004769 list_for_each_entry_safe(bridge, brt,
4770 &dev->mode_config.bridge_list, head) {
4771 bridge->funcs->destroy(bridge);
4772 }
4773
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004774 list_for_each_entry_safe(connector, ot,
4775 &dev->mode_config.connector_list, head) {
4776 connector->funcs->destroy(connector);
4777 }
4778
4779 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4780 head) {
4781 drm_property_destroy(dev, property);
4782 }
4783
4784 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4785 head) {
4786 drm_property_destroy_blob(dev, blob);
4787 }
4788
4789 /*
4790 * Single-threaded teardown context, so it's not required to grab the
4791 * fb_lock to protect against concurrent fb_list access. Contrary, it
4792 * would actually deadlock with the drm_framebuffer_cleanup function.
4793 *
4794 * Also, if there are any framebuffers left, that's a driver leak now,
4795 * so politely WARN about this.
4796 */
4797 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4798 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4799 drm_framebuffer_remove(fb);
4800 }
4801
4802 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4803 head) {
4804 plane->funcs->destroy(plane);
4805 }
4806
4807 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4808 crtc->funcs->destroy(crtc);
4809 }
4810
4811 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05004812 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004813}
4814EXPORT_SYMBOL(drm_mode_config_cleanup);