blob: d86d254693e49cfd133d94d4ea2748f2d03724c7 [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
Matt Roperb36552b2014-06-10 08:28:09 -07002125/*
2126 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002127 *
Matt Roperb36552b2014-06-10 08:28:09 -07002128 * Note that we assume an extra reference has already been taken on fb. If the
2129 * update fails, this reference will be dropped before return; if it succeeds,
2130 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002131 *
Matt Roperb36552b2014-06-10 08:28:09 -07002132 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002133 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002134static int setplane_internal(struct drm_plane *plane,
2135 struct drm_crtc *crtc,
Matt Roperb36552b2014-06-10 08:28:09 -07002136 struct drm_framebuffer *fb,
2137 int32_t crtc_x, int32_t crtc_y,
2138 uint32_t crtc_w, uint32_t crtc_h,
2139 /* src_{x,y,w,h} values are 16.16 fixed point */
2140 uint32_t src_x, uint32_t src_y,
2141 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002142{
Chris Wilson17cfd912014-06-13 15:22:28 +01002143 struct drm_device *dev = plane->dev;
Matt Roperb36552b2014-06-10 08:28:09 -07002144 struct drm_framebuffer *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002145 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002146 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002147 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002148
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002149 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002150 if (!fb) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002151 drm_modeset_lock_all(dev);
2152 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002153 ret = plane->funcs->disable_plane(plane);
2154 if (!ret) {
2155 plane->crtc = NULL;
2156 plane->fb = NULL;
2157 } else {
2158 old_fb = NULL;
2159 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002160 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002161 goto out;
2162 }
2163
Matt Roper7f994f32014-05-29 08:06:51 -07002164 /* Check whether this plane is usable on this CRTC */
2165 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2166 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2167 ret = -EINVAL;
2168 goto out;
2169 }
2170
Ville Syrjälä62443be2011-12-20 00:06:47 +02002171 /* Check whether this plane supports the fb pixel format. */
2172 for (i = 0; i < plane->format_count; i++)
2173 if (fb->pixel_format == plane->format_types[i])
2174 break;
2175 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002176 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2177 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002178 ret = -EINVAL;
2179 goto out;
2180 }
2181
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002182 fb_width = fb->width << 16;
2183 fb_height = fb->height << 16;
2184
2185 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002186 if (src_w > fb_width ||
2187 src_x > fb_width - src_w ||
2188 src_h > fb_height ||
2189 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002190 DRM_DEBUG_KMS("Invalid source coordinates "
2191 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002192 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2193 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2194 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2195 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002196 ret = -ENOSPC;
2197 goto out;
2198 }
2199
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002200 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002201 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002202 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002203 crtc_x, crtc_y, crtc_w, crtc_h,
2204 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002205 if (!ret) {
2206 plane->crtc = crtc;
2207 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002208 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002209 } else {
2210 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002211 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002212 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002213
2214out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002215 if (fb)
2216 drm_framebuffer_unreference(fb);
2217 if (old_fb)
2218 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002219
2220 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002221
2222}
2223
2224/**
2225 * drm_mode_setplane - configure a plane's configuration
2226 * @dev: DRM device
2227 * @data: ioctl data*
2228 * @file_priv: DRM file info
2229 *
2230 * Set plane configuration, including placement, fb, scaling, and other factors.
2231 * Or pass a NULL fb to disable (planes may be disabled without providing a
2232 * valid crtc).
2233 *
2234 * Returns:
2235 * Zero on success, errno on failure.
2236 */
2237int drm_mode_setplane(struct drm_device *dev, void *data,
2238 struct drm_file *file_priv)
2239{
2240 struct drm_mode_set_plane *plane_req = data;
2241 struct drm_mode_object *obj;
2242 struct drm_plane *plane;
2243 struct drm_crtc *crtc = NULL;
2244 struct drm_framebuffer *fb = NULL;
2245
2246 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2247 return -EINVAL;
2248
2249 /* Give drivers some help against integer overflows */
2250 if (plane_req->crtc_w > INT_MAX ||
2251 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2252 plane_req->crtc_h > INT_MAX ||
2253 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2254 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2255 plane_req->crtc_w, plane_req->crtc_h,
2256 plane_req->crtc_x, plane_req->crtc_y);
2257 return -ERANGE;
2258 }
2259
2260 /*
2261 * First, find the plane, crtc, and fb objects. If not available,
2262 * we don't bother to call the driver.
2263 */
2264 obj = drm_mode_object_find(dev, plane_req->plane_id,
2265 DRM_MODE_OBJECT_PLANE);
2266 if (!obj) {
2267 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2268 plane_req->plane_id);
2269 return -ENOENT;
2270 }
2271 plane = obj_to_plane(obj);
2272
2273 if (plane_req->fb_id) {
2274 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2275 if (!fb) {
2276 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2277 plane_req->fb_id);
2278 return -ENOENT;
2279 }
2280
2281 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2282 DRM_MODE_OBJECT_CRTC);
2283 if (!obj) {
2284 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2285 plane_req->crtc_id);
2286 return -ENOENT;
2287 }
2288 crtc = obj_to_crtc(obj);
2289 }
2290
Matt Roper161d0dc2014-06-10 08:28:10 -07002291 /*
2292 * setplane_internal will take care of deref'ing either the old or new
2293 * framebuffer depending on success.
2294 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002295 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002296 plane_req->crtc_x, plane_req->crtc_y,
2297 plane_req->crtc_w, plane_req->crtc_h,
2298 plane_req->src_x, plane_req->src_y,
2299 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002300}
2301
2302/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002303 * drm_mode_set_config_internal - helper to call ->set_config
2304 * @set: modeset config to set
2305 *
2306 * This is a little helper to wrap internal calls to the ->set_config driver
2307 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002308 *
2309 * Returns:
2310 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002311 */
2312int drm_mode_set_config_internal(struct drm_mode_set *set)
2313{
2314 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002315 struct drm_framebuffer *fb;
2316 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002317 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002318
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002319 /*
2320 * NOTE: ->set_config can also disable other crtcs (if we steal all
2321 * connectors from it), hence we need to refcount the fbs across all
2322 * crtcs. Atomic modeset will have saner semantics ...
2323 */
2324 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002325 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002326
Daniel Vetterb0d12322012-12-11 01:07:12 +01002327 fb = set->fb;
2328
2329 ret = crtc->funcs->set_config(set);
2330 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002331 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002332 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002333 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002334
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002335 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002336 if (tmp->primary->fb)
2337 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002338 if (tmp->old_fb)
2339 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002340 }
2341
2342 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002343}
2344EXPORT_SYMBOL(drm_mode_set_config_internal);
2345
Matt Roperaf936292014-04-01 15:22:34 -07002346/**
2347 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2348 * CRTC viewport
2349 * @crtc: CRTC that framebuffer will be displayed on
2350 * @x: x panning
2351 * @y: y panning
2352 * @mode: mode that framebuffer will be displayed under
2353 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002354 */
Matt Roperaf936292014-04-01 15:22:34 -07002355int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2356 int x, int y,
2357 const struct drm_display_mode *mode,
2358 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002359
2360{
2361 int hdisplay, vdisplay;
2362
2363 hdisplay = mode->hdisplay;
2364 vdisplay = mode->vdisplay;
2365
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002366 if (drm_mode_is_stereo(mode)) {
2367 struct drm_display_mode adjusted = *mode;
2368
2369 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2370 hdisplay = adjusted.crtc_hdisplay;
2371 vdisplay = adjusted.crtc_vdisplay;
2372 }
2373
Damien Lespiauc11e9282013-09-25 16:45:30 +01002374 if (crtc->invert_dimensions)
2375 swap(hdisplay, vdisplay);
2376
2377 if (hdisplay > fb->width ||
2378 vdisplay > fb->height ||
2379 x > fb->width - hdisplay ||
2380 y > fb->height - vdisplay) {
2381 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2382 fb->width, fb->height, hdisplay, vdisplay, x, y,
2383 crtc->invert_dimensions ? " (inverted)" : "");
2384 return -ENOSPC;
2385 }
2386
2387 return 0;
2388}
Matt Roperaf936292014-04-01 15:22:34 -07002389EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002390
Daniel Vetter2d13b672012-12-11 13:47:23 +01002391/**
Dave Airlief453ba02008-11-07 14:05:41 -08002392 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002393 * @dev: drm device for the ioctl
2394 * @data: data pointer for the ioctl
2395 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002396 *
Dave Airlief453ba02008-11-07 14:05:41 -08002397 * Build a new CRTC configuration based on user request.
2398 *
2399 * Called by the user via ioctl.
2400 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002401 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002402 * Zero on success, errno on failure.
2403 */
2404int drm_mode_setcrtc(struct drm_device *dev, void *data,
2405 struct drm_file *file_priv)
2406{
2407 struct drm_mode_config *config = &dev->mode_config;
2408 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002409 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002410 struct drm_connector **connector_set = NULL, *connector;
2411 struct drm_framebuffer *fb = NULL;
2412 struct drm_display_mode *mode = NULL;
2413 struct drm_mode_set set;
2414 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002415 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002416 int i;
2417
Dave Airliefb3b06c2011-02-08 13:55:21 +10002418 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2419 return -EINVAL;
2420
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002421 /* For some reason crtc x/y offsets are signed internally. */
2422 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2423 return -ERANGE;
2424
Daniel Vetter84849902012-12-02 00:28:11 +01002425 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002426 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2427 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002428 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002429 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002430 goto out;
2431 }
Jerome Glisse94401062010-07-15 15:43:25 -04002432 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002433
2434 if (crtc_req->mode_valid) {
2435 /* If we have a mode we need a framebuffer. */
2436 /* If we pass -1, set the mode with the currently bound fb */
2437 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002438 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002439 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2440 ret = -EINVAL;
2441 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002442 }
Matt Roperf4510a22014-04-01 15:22:40 -07002443 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002444 /* Make refcounting symmetric with the lookup path. */
2445 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002446 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002447 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2448 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002449 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2450 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002451 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002452 goto out;
2453 }
Dave Airlief453ba02008-11-07 14:05:41 -08002454 }
2455
2456 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002457 if (!mode) {
2458 ret = -ENOMEM;
2459 goto out;
2460 }
2461
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002462 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2463 if (ret) {
2464 DRM_DEBUG_KMS("Invalid mode\n");
2465 goto out;
2466 }
2467
Dave Airlief453ba02008-11-07 14:05:41 -08002468 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002469
Damien Lespiauc11e9282013-09-25 16:45:30 +01002470 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2471 mode, fb);
2472 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002473 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002474
Dave Airlief453ba02008-11-07 14:05:41 -08002475 }
2476
2477 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002478 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002479 ret = -EINVAL;
2480 goto out;
2481 }
2482
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002483 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002484 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002485 crtc_req->count_connectors);
2486 ret = -EINVAL;
2487 goto out;
2488 }
2489
2490 if (crtc_req->count_connectors > 0) {
2491 u32 out_id;
2492
2493 /* Avoid unbounded kernel memory allocation */
2494 if (crtc_req->count_connectors > config->num_connector) {
2495 ret = -EINVAL;
2496 goto out;
2497 }
2498
2499 connector_set = kmalloc(crtc_req->count_connectors *
2500 sizeof(struct drm_connector *),
2501 GFP_KERNEL);
2502 if (!connector_set) {
2503 ret = -ENOMEM;
2504 goto out;
2505 }
2506
2507 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002508 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002509 if (get_user(out_id, &set_connectors_ptr[i])) {
2510 ret = -EFAULT;
2511 goto out;
2512 }
2513
Rob Clarka2b34e22013-10-05 16:36:52 -04002514 connector = drm_connector_find(dev, out_id);
2515 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002516 DRM_DEBUG_KMS("Connector id %d unknown\n",
2517 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002518 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002519 goto out;
2520 }
Jerome Glisse94401062010-07-15 15:43:25 -04002521 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2522 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002523 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002524
2525 connector_set[i] = connector;
2526 }
2527 }
2528
2529 set.crtc = crtc;
2530 set.x = crtc_req->x;
2531 set.y = crtc_req->y;
2532 set.mode = mode;
2533 set.connectors = connector_set;
2534 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002535 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002536 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002537
2538out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002539 if (fb)
2540 drm_framebuffer_unreference(fb);
2541
Dave Airlief453ba02008-11-07 14:05:41 -08002542 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002543 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002544 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002545 return ret;
2546}
2547
Matt Roper161d0dc2014-06-10 08:28:10 -07002548/**
2549 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2550 * universal plane handler call
2551 * @crtc: crtc to update cursor for
2552 * @req: data pointer for the ioctl
2553 * @file_priv: drm file for the ioctl call
2554 *
2555 * Legacy cursor ioctl's work directly with driver buffer handles. To
2556 * translate legacy ioctl calls into universal plane handler calls, we need to
2557 * wrap the native buffer handle in a drm_framebuffer.
2558 *
2559 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2560 * buffer with a pitch of 4*width; the universal plane interface should be used
2561 * directly in cases where the hardware can support other buffer settings and
2562 * userspace wants to make use of these capabilities.
2563 *
2564 * Returns:
2565 * Zero on success, errno on failure.
2566 */
2567static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2568 struct drm_mode_cursor2 *req,
2569 struct drm_file *file_priv)
2570{
2571 struct drm_device *dev = crtc->dev;
2572 struct drm_framebuffer *fb = NULL;
2573 struct drm_mode_fb_cmd2 fbreq = {
2574 .width = req->width,
2575 .height = req->height,
2576 .pixel_format = DRM_FORMAT_ARGB8888,
2577 .pitches = { req->width * 4 },
2578 .handles = { req->handle },
2579 };
2580 int32_t crtc_x, crtc_y;
2581 uint32_t crtc_w = 0, crtc_h = 0;
2582 uint32_t src_w = 0, src_h = 0;
2583 int ret = 0;
2584
2585 BUG_ON(!crtc->cursor);
2586
2587 /*
2588 * Obtain fb we'll be using (either new or existing) and take an extra
2589 * reference to it if fb != null. setplane will take care of dropping
2590 * the reference if the plane update fails.
2591 */
2592 if (req->flags & DRM_MODE_CURSOR_BO) {
2593 if (req->handle) {
2594 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2595 if (IS_ERR(fb)) {
2596 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2597 return PTR_ERR(fb);
2598 }
2599
2600 drm_framebuffer_reference(fb);
2601 } else {
2602 fb = NULL;
2603 }
2604 } else {
2605 mutex_lock(&dev->mode_config.mutex);
2606 fb = crtc->cursor->fb;
2607 if (fb)
2608 drm_framebuffer_reference(fb);
2609 mutex_unlock(&dev->mode_config.mutex);
2610 }
2611
2612 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2613 crtc_x = req->x;
2614 crtc_y = req->y;
2615 } else {
2616 crtc_x = crtc->cursor_x;
2617 crtc_y = crtc->cursor_y;
2618 }
2619
2620 if (fb) {
2621 crtc_w = fb->width;
2622 crtc_h = fb->height;
2623 src_w = fb->width << 16;
2624 src_h = fb->height << 16;
2625 }
2626
2627 /*
2628 * setplane_internal will take care of deref'ing either the old or new
2629 * framebuffer depending on success.
2630 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002631 ret = setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002632 crtc_x, crtc_y, crtc_w, crtc_h,
2633 0, 0, src_w, src_h);
2634
2635 /* Update successful; save new cursor position, if necessary */
2636 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2637 crtc->cursor_x = req->x;
2638 crtc->cursor_y = req->y;
2639 }
2640
2641 return ret;
2642}
2643
Dave Airlie4c813d42013-06-20 11:48:52 +10002644static int drm_mode_cursor_common(struct drm_device *dev,
2645 struct drm_mode_cursor2 *req,
2646 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002647{
Dave Airlief453ba02008-11-07 14:05:41 -08002648 struct drm_crtc *crtc;
2649 int ret = 0;
2650
Dave Airliefb3b06c2011-02-08 13:55:21 +10002651 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2652 return -EINVAL;
2653
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002654 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002655 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002656
Rob Clarka2b34e22013-10-05 16:36:52 -04002657 crtc = drm_crtc_find(dev, req->crtc_id);
2658 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002659 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002660 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002661 }
Dave Airlief453ba02008-11-07 14:05:41 -08002662
Matt Roper161d0dc2014-06-10 08:28:10 -07002663 /*
2664 * If this crtc has a universal cursor plane, call that plane's update
2665 * handler rather than using legacy cursor handlers.
2666 */
2667 if (crtc->cursor)
2668 return drm_mode_cursor_universal(crtc, req, file_priv);
2669
Rob Clark51fd3712013-11-19 12:10:12 -05002670 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002671 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002672 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002673 ret = -ENXIO;
2674 goto out;
2675 }
2676 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002677 if (crtc->funcs->cursor_set2)
2678 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2679 req->width, req->height, req->hot_x, req->hot_y);
2680 else
2681 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2682 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002683 }
2684
2685 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2686 if (crtc->funcs->cursor_move) {
2687 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2688 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002689 ret = -EFAULT;
2690 goto out;
2691 }
2692 }
2693out:
Rob Clark51fd3712013-11-19 12:10:12 -05002694 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002695
Dave Airlief453ba02008-11-07 14:05:41 -08002696 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002697
2698}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002699
2700
2701/**
2702 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2703 * @dev: drm device for the ioctl
2704 * @data: data pointer for the ioctl
2705 * @file_priv: drm file for the ioctl call
2706 *
2707 * Set the cursor configuration based on user request.
2708 *
2709 * Called by the user via ioctl.
2710 *
2711 * Returns:
2712 * Zero on success, errno on failure.
2713 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002714int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002715 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002716{
2717 struct drm_mode_cursor *req = data;
2718 struct drm_mode_cursor2 new_req;
2719
2720 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2721 new_req.hot_x = new_req.hot_y = 0;
2722
2723 return drm_mode_cursor_common(dev, &new_req, file_priv);
2724}
2725
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002726/**
2727 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2728 * @dev: drm device for the ioctl
2729 * @data: data pointer for the ioctl
2730 * @file_priv: drm file for the ioctl call
2731 *
2732 * Set the cursor configuration based on user request. This implements the 2nd
2733 * version of the cursor ioctl, which allows userspace to additionally specify
2734 * the hotspot of the pointer.
2735 *
2736 * Called by the user via ioctl.
2737 *
2738 * Returns:
2739 * Zero on success, errno on failure.
2740 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002741int drm_mode_cursor2_ioctl(struct drm_device *dev,
2742 void *data, struct drm_file *file_priv)
2743{
2744 struct drm_mode_cursor2 *req = data;
2745 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002746}
2747
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002748/**
2749 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2750 * @bpp: bits per pixels
2751 * @depth: bit depth per pixel
2752 *
2753 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2754 * Useful in fbdev emulation code, since that deals in those values.
2755 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002756uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2757{
2758 uint32_t fmt;
2759
2760 switch (bpp) {
2761 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002762 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002763 break;
2764 case 16:
2765 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002766 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002767 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002768 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002769 break;
2770 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002771 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002772 break;
2773 case 32:
2774 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002775 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002776 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002777 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002778 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002779 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002780 break;
2781 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002782 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2783 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002784 break;
2785 }
2786
2787 return fmt;
2788}
2789EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2790
Dave Airlief453ba02008-11-07 14:05:41 -08002791/**
2792 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002793 * @dev: drm device for the ioctl
2794 * @data: data pointer for the ioctl
2795 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002796 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002797 * Add a new FB to the specified CRTC, given a user request. This is the
2798 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002799 *
2800 * Called by the user via ioctl.
2801 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002802 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002803 * Zero on success, errno on failure.
2804 */
2805int drm_mode_addfb(struct drm_device *dev,
2806 void *data, struct drm_file *file_priv)
2807{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002808 struct drm_mode_fb_cmd *or = data;
2809 struct drm_mode_fb_cmd2 r = {};
2810 struct drm_mode_config *config = &dev->mode_config;
2811 struct drm_framebuffer *fb;
2812 int ret = 0;
2813
2814 /* Use new struct with format internally */
2815 r.fb_id = or->fb_id;
2816 r.width = or->width;
2817 r.height = or->height;
2818 r.pitches[0] = or->pitch;
2819 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2820 r.handles[0] = or->handle;
2821
2822 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2823 return -EINVAL;
2824
Jesse Barnesacb4b992011-11-07 12:03:23 -08002825 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002826 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002827
2828 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002829 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002830
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002831 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2832 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002833 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002834 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002835 }
2836
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002837 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002838 or->fb_id = fb->base.id;
2839 list_add(&fb->filp_head, &file_priv->fbs);
2840 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002841 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002842
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002843 return ret;
2844}
2845
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002846static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002847{
2848 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2849
2850 switch (format) {
2851 case DRM_FORMAT_C8:
2852 case DRM_FORMAT_RGB332:
2853 case DRM_FORMAT_BGR233:
2854 case DRM_FORMAT_XRGB4444:
2855 case DRM_FORMAT_XBGR4444:
2856 case DRM_FORMAT_RGBX4444:
2857 case DRM_FORMAT_BGRX4444:
2858 case DRM_FORMAT_ARGB4444:
2859 case DRM_FORMAT_ABGR4444:
2860 case DRM_FORMAT_RGBA4444:
2861 case DRM_FORMAT_BGRA4444:
2862 case DRM_FORMAT_XRGB1555:
2863 case DRM_FORMAT_XBGR1555:
2864 case DRM_FORMAT_RGBX5551:
2865 case DRM_FORMAT_BGRX5551:
2866 case DRM_FORMAT_ARGB1555:
2867 case DRM_FORMAT_ABGR1555:
2868 case DRM_FORMAT_RGBA5551:
2869 case DRM_FORMAT_BGRA5551:
2870 case DRM_FORMAT_RGB565:
2871 case DRM_FORMAT_BGR565:
2872 case DRM_FORMAT_RGB888:
2873 case DRM_FORMAT_BGR888:
2874 case DRM_FORMAT_XRGB8888:
2875 case DRM_FORMAT_XBGR8888:
2876 case DRM_FORMAT_RGBX8888:
2877 case DRM_FORMAT_BGRX8888:
2878 case DRM_FORMAT_ARGB8888:
2879 case DRM_FORMAT_ABGR8888:
2880 case DRM_FORMAT_RGBA8888:
2881 case DRM_FORMAT_BGRA8888:
2882 case DRM_FORMAT_XRGB2101010:
2883 case DRM_FORMAT_XBGR2101010:
2884 case DRM_FORMAT_RGBX1010102:
2885 case DRM_FORMAT_BGRX1010102:
2886 case DRM_FORMAT_ARGB2101010:
2887 case DRM_FORMAT_ABGR2101010:
2888 case DRM_FORMAT_RGBA1010102:
2889 case DRM_FORMAT_BGRA1010102:
2890 case DRM_FORMAT_YUYV:
2891 case DRM_FORMAT_YVYU:
2892 case DRM_FORMAT_UYVY:
2893 case DRM_FORMAT_VYUY:
2894 case DRM_FORMAT_AYUV:
2895 case DRM_FORMAT_NV12:
2896 case DRM_FORMAT_NV21:
2897 case DRM_FORMAT_NV16:
2898 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002899 case DRM_FORMAT_NV24:
2900 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002901 case DRM_FORMAT_YUV410:
2902 case DRM_FORMAT_YVU410:
2903 case DRM_FORMAT_YUV411:
2904 case DRM_FORMAT_YVU411:
2905 case DRM_FORMAT_YUV420:
2906 case DRM_FORMAT_YVU420:
2907 case DRM_FORMAT_YUV422:
2908 case DRM_FORMAT_YVU422:
2909 case DRM_FORMAT_YUV444:
2910 case DRM_FORMAT_YVU444:
2911 return 0;
2912 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002913 DRM_DEBUG_KMS("invalid pixel format %s\n",
2914 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002915 return -EINVAL;
2916 }
2917}
2918
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002919static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002920{
2921 int ret, hsub, vsub, num_planes, i;
2922
2923 ret = format_check(r);
2924 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002925 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2926 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002927 return ret;
2928 }
2929
2930 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2931 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2932 num_planes = drm_format_num_planes(r->pixel_format);
2933
2934 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002935 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002936 return -EINVAL;
2937 }
2938
2939 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002940 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002941 return -EINVAL;
2942 }
2943
2944 for (i = 0; i < num_planes; i++) {
2945 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002946 unsigned int height = r->height / (i != 0 ? vsub : 1);
2947 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002948
2949 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002950 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002951 return -EINVAL;
2952 }
2953
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002954 if ((uint64_t) width * cpp > UINT_MAX)
2955 return -ERANGE;
2956
2957 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2958 return -ERANGE;
2959
2960 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002961 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002962 return -EINVAL;
2963 }
2964 }
2965
2966 return 0;
2967}
2968
Matt Roperc394c2b2014-06-10 08:28:08 -07002969static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
2970 struct drm_mode_fb_cmd2 *r,
2971 struct drm_file *file_priv)
2972{
2973 struct drm_mode_config *config = &dev->mode_config;
2974 struct drm_framebuffer *fb;
2975 int ret;
2976
2977 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2978 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2979 return ERR_PTR(-EINVAL);
2980 }
2981
2982 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2983 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2984 r->width, config->min_width, config->max_width);
2985 return ERR_PTR(-EINVAL);
2986 }
2987 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2988 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2989 r->height, config->min_height, config->max_height);
2990 return ERR_PTR(-EINVAL);
2991 }
2992
2993 ret = framebuffer_check(r);
2994 if (ret)
2995 return ERR_PTR(ret);
2996
2997 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2998 if (IS_ERR(fb)) {
2999 DRM_DEBUG_KMS("could not create framebuffer\n");
3000 return fb;
3001 }
3002
3003 mutex_lock(&file_priv->fbs_lock);
3004 r->fb_id = fb->base.id;
3005 list_add(&fb->filp_head, &file_priv->fbs);
3006 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3007 mutex_unlock(&file_priv->fbs_lock);
3008
3009 return fb;
3010}
3011
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003012/**
3013 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003014 * @dev: drm device for the ioctl
3015 * @data: data pointer for the ioctl
3016 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003017 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003018 * Add a new FB to the specified CRTC, given a user request with format. This is
3019 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3020 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003021 *
3022 * Called by the user via ioctl.
3023 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003024 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003025 * Zero on success, errno on failure.
3026 */
3027int drm_mode_addfb2(struct drm_device *dev,
3028 void *data, struct drm_file *file_priv)
3029{
Dave Airlief453ba02008-11-07 14:05:41 -08003030 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003031
Dave Airliefb3b06c2011-02-08 13:55:21 +10003032 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3033 return -EINVAL;
3034
Matt Roperc394c2b2014-06-10 08:28:08 -07003035 fb = add_framebuffer_internal(dev, data, file_priv);
3036 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003037 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003038
Matt Roperc394c2b2014-06-10 08:28:08 -07003039 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003040}
3041
3042/**
3043 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003044 * @dev: drm device for the ioctl
3045 * @data: data pointer for the ioctl
3046 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003047 *
Dave Airlief453ba02008-11-07 14:05:41 -08003048 * Remove the FB specified by the user.
3049 *
3050 * Called by the user via ioctl.
3051 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003052 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003053 * Zero on success, errno on failure.
3054 */
3055int drm_mode_rmfb(struct drm_device *dev,
3056 void *data, struct drm_file *file_priv)
3057{
Dave Airlief453ba02008-11-07 14:05:41 -08003058 struct drm_framebuffer *fb = NULL;
3059 struct drm_framebuffer *fbl = NULL;
3060 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003061 int found = 0;
3062
Dave Airliefb3b06c2011-02-08 13:55:21 +10003063 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3064 return -EINVAL;
3065
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003066 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003067 mutex_lock(&dev->mode_config.fb_lock);
3068 fb = __drm_framebuffer_lookup(dev, *id);
3069 if (!fb)
3070 goto fail_lookup;
3071
Dave Airlief453ba02008-11-07 14:05:41 -08003072 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3073 if (fb == fbl)
3074 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003075 if (!found)
3076 goto fail_lookup;
3077
3078 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3079 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003080
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003081 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003082 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003083 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003084
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003085 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003086
Daniel Vetter2b677e82012-12-10 21:16:05 +01003087 return 0;
3088
3089fail_lookup:
3090 mutex_unlock(&dev->mode_config.fb_lock);
3091 mutex_unlock(&file_priv->fbs_lock);
3092
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003093 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003094}
3095
3096/**
3097 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003098 * @dev: drm device for the ioctl
3099 * @data: data pointer for the ioctl
3100 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003101 *
Dave Airlief453ba02008-11-07 14:05:41 -08003102 * Lookup the FB given its ID and return info about it.
3103 *
3104 * Called by the user via ioctl.
3105 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003106 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003107 * Zero on success, errno on failure.
3108 */
3109int drm_mode_getfb(struct drm_device *dev,
3110 void *data, struct drm_file *file_priv)
3111{
3112 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003113 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003114 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003115
Dave Airliefb3b06c2011-02-08 13:55:21 +10003116 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3117 return -EINVAL;
3118
Daniel Vetter786b99e2012-12-02 21:53:40 +01003119 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003120 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003121 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003122
3123 r->height = fb->height;
3124 r->width = fb->width;
3125 r->depth = fb->depth;
3126 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003127 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003128 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003129 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003130 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003131 ret = fb->funcs->create_handle(fb, file_priv,
3132 &r->handle);
3133 } else {
3134 /* GET_FB() is an unprivileged ioctl so we must not
3135 * return a buffer-handle to non-master processes! For
3136 * backwards-compatibility reasons, we cannot make
3137 * GET_FB() privileged, so just return an invalid handle
3138 * for non-masters. */
3139 r->handle = 0;
3140 ret = 0;
3141 }
3142 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003143 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003144 }
Dave Airlief453ba02008-11-07 14:05:41 -08003145
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003146 drm_framebuffer_unreference(fb);
3147
Dave Airlief453ba02008-11-07 14:05:41 -08003148 return ret;
3149}
3150
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003151/**
3152 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3153 * @dev: drm device for the ioctl
3154 * @data: data pointer for the ioctl
3155 * @file_priv: drm file for the ioctl call
3156 *
3157 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3158 * rectangle list. Generic userspace which does frontbuffer rendering must call
3159 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3160 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3161 *
3162 * Modesetting drivers which always update the frontbuffer do not need to
3163 * implement the corresponding ->dirty framebuffer callback.
3164 *
3165 * Called by the user via ioctl.
3166 *
3167 * Returns:
3168 * Zero on success, errno on failure.
3169 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003170int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3171 void *data, struct drm_file *file_priv)
3172{
3173 struct drm_clip_rect __user *clips_ptr;
3174 struct drm_clip_rect *clips = NULL;
3175 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003176 struct drm_framebuffer *fb;
3177 unsigned flags;
3178 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003179 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003180
Dave Airliefb3b06c2011-02-08 13:55:21 +10003181 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3182 return -EINVAL;
3183
Daniel Vetter786b99e2012-12-02 21:53:40 +01003184 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003185 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003186 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003187
3188 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003189 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003190
3191 if (!num_clips != !clips_ptr) {
3192 ret = -EINVAL;
3193 goto out_err1;
3194 }
3195
3196 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3197
3198 /* If userspace annotates copy, clips must come in pairs */
3199 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3200 ret = -EINVAL;
3201 goto out_err1;
3202 }
3203
3204 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003205 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3206 ret = -EINVAL;
3207 goto out_err1;
3208 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003209 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3210 if (!clips) {
3211 ret = -ENOMEM;
3212 goto out_err1;
3213 }
3214
3215 ret = copy_from_user(clips, clips_ptr,
3216 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003217 if (ret) {
3218 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003219 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003220 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003221 }
3222
3223 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003224 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3225 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003226 } else {
3227 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003228 }
3229
3230out_err2:
3231 kfree(clips);
3232out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003233 drm_framebuffer_unreference(fb);
3234
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003235 return ret;
3236}
3237
3238
Dave Airlief453ba02008-11-07 14:05:41 -08003239/**
3240 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003241 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003242 *
Dave Airlief453ba02008-11-07 14:05:41 -08003243 * Destroy all the FBs associated with @filp.
3244 *
3245 * Called by the user via ioctl.
3246 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003247 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003248 * Zero on success, errno on failure.
3249 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003250void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003251{
Dave Airlief453ba02008-11-07 14:05:41 -08003252 struct drm_device *dev = priv->minor->dev;
3253 struct drm_framebuffer *fb, *tfb;
3254
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003255 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003256 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003257
3258 mutex_lock(&dev->mode_config.fb_lock);
3259 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3260 __drm_framebuffer_unregister(dev, fb);
3261 mutex_unlock(&dev->mode_config.fb_lock);
3262
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003263 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003264
3265 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003266 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003267 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003268 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003269}
3270
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003271/**
3272 * drm_property_create - create a new property type
3273 * @dev: drm device
3274 * @flags: flags specifying the property type
3275 * @name: name of the property
3276 * @num_values: number of pre-defined values
3277 *
3278 * This creates a new generic drm property which can then be attached to a drm
3279 * object with drm_object_attach_property. The returned property object must be
3280 * freed with drm_property_destroy.
3281 *
3282 * Returns:
3283 * A pointer to the newly created property on success, NULL on failure.
3284 */
Dave Airlief453ba02008-11-07 14:05:41 -08003285struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3286 const char *name, int num_values)
3287{
3288 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003289 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003290
3291 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3292 if (!property)
3293 return NULL;
3294
Rob Clark98f75de2014-05-30 11:37:03 -04003295 property->dev = dev;
3296
Dave Airlief453ba02008-11-07 14:05:41 -08003297 if (num_values) {
3298 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3299 if (!property->values)
3300 goto fail;
3301 }
3302
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003303 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3304 if (ret)
3305 goto fail;
3306
Dave Airlief453ba02008-11-07 14:05:41 -08003307 property->flags = flags;
3308 property->num_values = num_values;
3309 INIT_LIST_HEAD(&property->enum_blob_list);
3310
Vinson Lee471dd2e2011-11-10 11:55:40 -08003311 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003312 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003313 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3314 }
Dave Airlief453ba02008-11-07 14:05:41 -08003315
3316 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003317
3318 WARN_ON(!drm_property_type_valid(property));
3319
Dave Airlief453ba02008-11-07 14:05:41 -08003320 return property;
3321fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003322 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003323 kfree(property);
3324 return NULL;
3325}
3326EXPORT_SYMBOL(drm_property_create);
3327
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003328/**
3329 * drm_property_create - create a new enumeration property type
3330 * @dev: drm device
3331 * @flags: flags specifying the property type
3332 * @name: name of the property
3333 * @props: enumeration lists with property values
3334 * @num_values: number of pre-defined values
3335 *
3336 * This creates a new generic drm property which can then be attached to a drm
3337 * object with drm_object_attach_property. The returned property object must be
3338 * freed with drm_property_destroy.
3339 *
3340 * Userspace is only allowed to set one of the predefined values for enumeration
3341 * properties.
3342 *
3343 * Returns:
3344 * A pointer to the newly created property on success, NULL on failure.
3345 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003346struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3347 const char *name,
3348 const struct drm_prop_enum_list *props,
3349 int num_values)
3350{
3351 struct drm_property *property;
3352 int i, ret;
3353
3354 flags |= DRM_MODE_PROP_ENUM;
3355
3356 property = drm_property_create(dev, flags, name, num_values);
3357 if (!property)
3358 return NULL;
3359
3360 for (i = 0; i < num_values; i++) {
3361 ret = drm_property_add_enum(property, i,
3362 props[i].type,
3363 props[i].name);
3364 if (ret) {
3365 drm_property_destroy(dev, property);
3366 return NULL;
3367 }
3368 }
3369
3370 return property;
3371}
3372EXPORT_SYMBOL(drm_property_create_enum);
3373
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003374/**
3375 * drm_property_create - create a new bitmask property type
3376 * @dev: drm device
3377 * @flags: flags specifying the property type
3378 * @name: name of the property
3379 * @props: enumeration lists with property bitflags
3380 * @num_values: number of pre-defined values
3381 *
3382 * This creates a new generic drm property which can then be attached to a drm
3383 * object with drm_object_attach_property. The returned property object must be
3384 * freed with drm_property_destroy.
3385 *
3386 * Compared to plain enumeration properties userspace is allowed to set any
3387 * or'ed together combination of the predefined property bitflag values
3388 *
3389 * Returns:
3390 * A pointer to the newly created property on success, NULL on failure.
3391 */
Rob Clark49e27542012-05-17 02:23:26 -06003392struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3393 int flags, const char *name,
3394 const struct drm_prop_enum_list *props,
3395 int num_values)
3396{
3397 struct drm_property *property;
3398 int i, ret;
3399
3400 flags |= DRM_MODE_PROP_BITMASK;
3401
3402 property = drm_property_create(dev, flags, name, num_values);
3403 if (!property)
3404 return NULL;
3405
3406 for (i = 0; i < num_values; i++) {
3407 ret = drm_property_add_enum(property, i,
3408 props[i].type,
3409 props[i].name);
3410 if (ret) {
3411 drm_property_destroy(dev, property);
3412 return NULL;
3413 }
3414 }
3415
3416 return property;
3417}
3418EXPORT_SYMBOL(drm_property_create_bitmask);
3419
Rob Clarkebc44cf2012-09-12 22:22:31 -05003420static struct drm_property *property_create_range(struct drm_device *dev,
3421 int flags, const char *name,
3422 uint64_t min, uint64_t max)
3423{
3424 struct drm_property *property;
3425
3426 property = drm_property_create(dev, flags, name, 2);
3427 if (!property)
3428 return NULL;
3429
3430 property->values[0] = min;
3431 property->values[1] = max;
3432
3433 return property;
3434}
3435
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003436/**
3437 * drm_property_create - create a new ranged property type
3438 * @dev: drm device
3439 * @flags: flags specifying the property type
3440 * @name: name of the property
3441 * @min: minimum value of the property
3442 * @max: maximum value of the property
3443 *
3444 * This creates a new generic drm property which can then be attached to a drm
3445 * object with drm_object_attach_property. The returned property object must be
3446 * freed with drm_property_destroy.
3447 *
3448 * Userspace is allowed to set any interger value in the (min, max) range
3449 * inclusive.
3450 *
3451 * Returns:
3452 * A pointer to the newly created property on success, NULL on failure.
3453 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003454struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3455 const char *name,
3456 uint64_t min, uint64_t max)
3457{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003458 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3459 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003460}
3461EXPORT_SYMBOL(drm_property_create_range);
3462
Rob Clarkebc44cf2012-09-12 22:22:31 -05003463struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3464 int flags, const char *name,
3465 int64_t min, int64_t max)
3466{
3467 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3468 name, I642U64(min), I642U64(max));
3469}
3470EXPORT_SYMBOL(drm_property_create_signed_range);
3471
Rob Clark98f75de2014-05-30 11:37:03 -04003472struct drm_property *drm_property_create_object(struct drm_device *dev,
3473 int flags, const char *name, uint32_t type)
3474{
3475 struct drm_property *property;
3476
3477 flags |= DRM_MODE_PROP_OBJECT;
3478
3479 property = drm_property_create(dev, flags, name, 1);
3480 if (!property)
3481 return NULL;
3482
3483 property->values[0] = type;
3484
3485 return property;
3486}
3487EXPORT_SYMBOL(drm_property_create_object);
3488
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003489/**
3490 * drm_property_add_enum - add a possible value to an enumeration property
3491 * @property: enumeration property to change
3492 * @index: index of the new enumeration
3493 * @value: value of the new enumeration
3494 * @name: symbolic name of the new enumeration
3495 *
3496 * This functions adds enumerations to a property.
3497 *
3498 * It's use is deprecated, drivers should use one of the more specific helpers
3499 * to directly create the property with all enumerations already attached.
3500 *
3501 * Returns:
3502 * Zero on success, error code on failure.
3503 */
Dave Airlief453ba02008-11-07 14:05:41 -08003504int drm_property_add_enum(struct drm_property *property, int index,
3505 uint64_t value, const char *name)
3506{
3507 struct drm_property_enum *prop_enum;
3508
Rob Clark5ea22f22014-05-30 11:34:01 -04003509 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3510 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003511 return -EINVAL;
3512
3513 /*
3514 * Bitmask enum properties have the additional constraint of values
3515 * from 0 to 63
3516 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003517 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3518 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003519 return -EINVAL;
3520
3521 if (!list_empty(&property->enum_blob_list)) {
3522 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3523 if (prop_enum->value == value) {
3524 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3525 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3526 return 0;
3527 }
3528 }
3529 }
3530
3531 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3532 if (!prop_enum)
3533 return -ENOMEM;
3534
3535 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3536 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3537 prop_enum->value = value;
3538
3539 property->values[index] = value;
3540 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3541 return 0;
3542}
3543EXPORT_SYMBOL(drm_property_add_enum);
3544
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003545/**
3546 * drm_property_destroy - destroy a drm property
3547 * @dev: drm device
3548 * @property: property to destry
3549 *
3550 * This function frees a property including any attached resources like
3551 * enumeration values.
3552 */
Dave Airlief453ba02008-11-07 14:05:41 -08003553void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3554{
3555 struct drm_property_enum *prop_enum, *pt;
3556
3557 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3558 list_del(&prop_enum->head);
3559 kfree(prop_enum);
3560 }
3561
3562 if (property->num_values)
3563 kfree(property->values);
3564 drm_mode_object_put(dev, &property->base);
3565 list_del(&property->head);
3566 kfree(property);
3567}
3568EXPORT_SYMBOL(drm_property_destroy);
3569
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003570/**
3571 * drm_object_attach_property - attach a property to a modeset object
3572 * @obj: drm modeset object
3573 * @property: property to attach
3574 * @init_val: initial value of the property
3575 *
3576 * This attaches the given property to the modeset object with the given initial
3577 * value. Currently this function cannot fail since the properties are stored in
3578 * a statically sized array.
3579 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003580void drm_object_attach_property(struct drm_mode_object *obj,
3581 struct drm_property *property,
3582 uint64_t init_val)
3583{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003584 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003585
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003586 if (count == DRM_OBJECT_MAX_PROPERTY) {
3587 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3588 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3589 "you see this message on the same object type.\n",
3590 obj->type);
3591 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003592 }
3593
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003594 obj->properties->ids[count] = property->base.id;
3595 obj->properties->values[count] = init_val;
3596 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003597}
3598EXPORT_SYMBOL(drm_object_attach_property);
3599
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003600/**
3601 * drm_object_property_set_value - set the value of a property
3602 * @obj: drm mode object to set property value for
3603 * @property: property to set
3604 * @val: value the property should be set to
3605 *
3606 * This functions sets a given property on a given object. This function only
3607 * changes the software state of the property, it does not call into the
3608 * driver's ->set_property callback.
3609 *
3610 * Returns:
3611 * Zero on success, error code on failure.
3612 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003613int drm_object_property_set_value(struct drm_mode_object *obj,
3614 struct drm_property *property, uint64_t val)
3615{
3616 int i;
3617
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003618 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003619 if (obj->properties->ids[i] == property->base.id) {
3620 obj->properties->values[i] = val;
3621 return 0;
3622 }
3623 }
3624
3625 return -EINVAL;
3626}
3627EXPORT_SYMBOL(drm_object_property_set_value);
3628
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003629/**
3630 * drm_object_property_get_value - retrieve the value of a property
3631 * @obj: drm mode object to get property value from
3632 * @property: property to retrieve
3633 * @val: storage for the property value
3634 *
3635 * This function retrieves the softare state of the given property for the given
3636 * property. Since there is no driver callback to retrieve the current property
3637 * value this might be out of sync with the hardware, depending upon the driver
3638 * and property.
3639 *
3640 * Returns:
3641 * Zero on success, error code on failure.
3642 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003643int drm_object_property_get_value(struct drm_mode_object *obj,
3644 struct drm_property *property, uint64_t *val)
3645{
3646 int i;
3647
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003648 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003649 if (obj->properties->ids[i] == property->base.id) {
3650 *val = obj->properties->values[i];
3651 return 0;
3652 }
3653 }
3654
3655 return -EINVAL;
3656}
3657EXPORT_SYMBOL(drm_object_property_get_value);
3658
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003659/**
3660 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3661 * @dev: DRM device
3662 * @data: ioctl data
3663 * @file_priv: DRM file info
3664 *
3665 * This function retrieves the current value for an connectors's property.
3666 *
3667 * Called by the user via ioctl.
3668 *
3669 * Returns:
3670 * Zero on success, errno on failure.
3671 */
Dave Airlief453ba02008-11-07 14:05:41 -08003672int drm_mode_getproperty_ioctl(struct drm_device *dev,
3673 void *data, struct drm_file *file_priv)
3674{
Dave Airlief453ba02008-11-07 14:05:41 -08003675 struct drm_mode_get_property *out_resp = data;
3676 struct drm_property *property;
3677 int enum_count = 0;
3678 int blob_count = 0;
3679 int value_count = 0;
3680 int ret = 0, i;
3681 int copied;
3682 struct drm_property_enum *prop_enum;
3683 struct drm_mode_property_enum __user *enum_ptr;
3684 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003685 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003686 uint64_t __user *values_ptr;
3687 uint32_t __user *blob_length_ptr;
3688
Dave Airliefb3b06c2011-02-08 13:55:21 +10003689 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3690 return -EINVAL;
3691
Daniel Vetter84849902012-12-02 00:28:11 +01003692 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003693 property = drm_property_find(dev, out_resp->prop_id);
3694 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003695 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003696 goto done;
3697 }
Dave Airlief453ba02008-11-07 14:05:41 -08003698
Rob Clark5ea22f22014-05-30 11:34:01 -04003699 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3700 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003701 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3702 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003703 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003704 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3705 blob_count++;
3706 }
3707
3708 value_count = property->num_values;
3709
3710 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3711 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3712 out_resp->flags = property->flags;
3713
3714 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003715 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003716 for (i = 0; i < value_count; i++) {
3717 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3718 ret = -EFAULT;
3719 goto done;
3720 }
3721 }
3722 }
3723 out_resp->count_values = value_count;
3724
Rob Clark5ea22f22014-05-30 11:34:01 -04003725 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3726 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003727 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3728 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003729 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003730 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3731
3732 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3733 ret = -EFAULT;
3734 goto done;
3735 }
3736
3737 if (copy_to_user(&enum_ptr[copied].name,
3738 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3739 ret = -EFAULT;
3740 goto done;
3741 }
3742 copied++;
3743 }
3744 }
3745 out_resp->count_enum_blobs = enum_count;
3746 }
3747
Rob Clark5ea22f22014-05-30 11:34:01 -04003748 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003749 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3750 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003751 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3752 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003753
3754 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3755 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3756 ret = -EFAULT;
3757 goto done;
3758 }
3759
3760 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3761 ret = -EFAULT;
3762 goto done;
3763 }
3764
3765 copied++;
3766 }
3767 }
3768 out_resp->count_enum_blobs = blob_count;
3769 }
3770done:
Daniel Vetter84849902012-12-02 00:28:11 +01003771 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003772 return ret;
3773}
3774
3775static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3776 void *data)
3777{
3778 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003779 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003780
3781 if (!length || !data)
3782 return NULL;
3783
3784 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3785 if (!blob)
3786 return NULL;
3787
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003788 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3789 if (ret) {
3790 kfree(blob);
3791 return NULL;
3792 }
3793
Dave Airlief453ba02008-11-07 14:05:41 -08003794 blob->length = length;
3795
3796 memcpy(blob->data, data, length);
3797
Dave Airlief453ba02008-11-07 14:05:41 -08003798 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3799 return blob;
3800}
3801
3802static void drm_property_destroy_blob(struct drm_device *dev,
3803 struct drm_property_blob *blob)
3804{
3805 drm_mode_object_put(dev, &blob->base);
3806 list_del(&blob->head);
3807 kfree(blob);
3808}
3809
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003810/**
3811 * drm_mode_getblob_ioctl - get the contents of a blob property value
3812 * @dev: DRM device
3813 * @data: ioctl data
3814 * @file_priv: DRM file info
3815 *
3816 * This function retrieves the contents of a blob property. The value stored in
3817 * an object's blob property is just a normal modeset object id.
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_getblob_ioctl(struct drm_device *dev,
3825 void *data, struct drm_file *file_priv)
3826{
Dave Airlief453ba02008-11-07 14:05:41 -08003827 struct drm_mode_get_blob *out_resp = data;
3828 struct drm_property_blob *blob;
3829 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003830 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003831
Dave Airliefb3b06c2011-02-08 13:55:21 +10003832 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3833 return -EINVAL;
3834
Daniel Vetter84849902012-12-02 00:28:11 +01003835 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003836 blob = drm_property_blob_find(dev, out_resp->blob_id);
3837 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003838 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003839 goto done;
3840 }
Dave Airlief453ba02008-11-07 14:05:41 -08003841
3842 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003843 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003844 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3845 ret = -EFAULT;
3846 goto done;
3847 }
3848 }
3849 out_resp->length = blob->length;
3850
3851done:
Daniel Vetter84849902012-12-02 00:28:11 +01003852 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003853 return ret;
3854}
3855
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003856/**
3857 * drm_mode_connector_update_edid_property - update the edid property of a connector
3858 * @connector: drm connector
3859 * @edid: new value of the edid property
3860 *
3861 * This function creates a new blob modeset object and assigns its id to the
3862 * connector's edid property.
3863 *
3864 * Returns:
3865 * Zero on success, errno on failure.
3866 */
Dave Airlief453ba02008-11-07 14:05:41 -08003867int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3868 struct edid *edid)
3869{
3870 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003871 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003872
3873 if (connector->edid_blob_ptr)
3874 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3875
3876 /* Delete edid, when there is none. */
3877 if (!edid) {
3878 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003879 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003880 return ret;
3881 }
3882
Adam Jackson7466f4c2010-03-29 21:43:23 +00003883 size = EDID_LENGTH * (1 + edid->extensions);
3884 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3885 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003886 if (!connector->edid_blob_ptr)
3887 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003888
Rob Clark58495562012-10-11 20:50:56 -05003889 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003890 dev->mode_config.edid_property,
3891 connector->edid_blob_ptr->base.id);
3892
3893 return ret;
3894}
3895EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3896
Paulo Zanoni26a34812012-05-15 18:08:59 -03003897static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003898 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003899{
3900 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3901 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003902
3903 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003904 if (value < property->values[0] || value > property->values[1])
3905 return false;
3906 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003907 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3908 int64_t svalue = U642I64(value);
3909 if (svalue < U642I64(property->values[0]) ||
3910 svalue > U642I64(property->values[1]))
3911 return false;
3912 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003913 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06003914 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003915 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003916 for (i = 0; i < property->num_values; i++)
3917 valid_mask |= (1ULL << property->values[i]);
3918 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003919 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003920 /* Only the driver knows */
3921 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04003922 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3923 struct drm_mode_object *obj;
3924 /* a zero value for an object property translates to null: */
3925 if (value == 0)
3926 return true;
3927 /*
3928 * NOTE: use _object_find() directly to bypass restriction on
3929 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3930 * object this could race against object finalization, so it
3931 * simply tells us that the object *was* valid. Which is good
3932 * enough.
3933 */
3934 obj = _object_find(property->dev, value, property->values[0]);
3935 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003936 } else {
3937 int i;
3938 for (i = 0; i < property->num_values; i++)
3939 if (property->values[i] == value)
3940 return true;
3941 return false;
3942 }
3943}
3944
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003945/**
3946 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3947 * @dev: DRM device
3948 * @data: ioctl data
3949 * @file_priv: DRM file info
3950 *
3951 * This function sets the current value for a connectors's property. It also
3952 * calls into a driver's ->set_property callback to update the hardware state
3953 *
3954 * Called by the user via ioctl.
3955 *
3956 * Returns:
3957 * Zero on success, errno on failure.
3958 */
Dave Airlief453ba02008-11-07 14:05:41 -08003959int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3960 void *data, struct drm_file *file_priv)
3961{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003962 struct drm_mode_connector_set_property *conn_set_prop = data;
3963 struct drm_mode_obj_set_property obj_set_prop = {
3964 .value = conn_set_prop->value,
3965 .prop_id = conn_set_prop->prop_id,
3966 .obj_id = conn_set_prop->connector_id,
3967 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3968 };
Dave Airlief453ba02008-11-07 14:05:41 -08003969
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003970 /* It does all the locking and checking we need */
3971 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003972}
3973
Paulo Zanonic5431882012-05-15 18:09:02 -03003974static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3975 struct drm_property *property,
3976 uint64_t value)
3977{
3978 int ret = -EINVAL;
3979 struct drm_connector *connector = obj_to_connector(obj);
3980
3981 /* Do DPMS ourselves */
3982 if (property == connector->dev->mode_config.dpms_property) {
3983 if (connector->funcs->dpms)
3984 (*connector->funcs->dpms)(connector, (int)value);
3985 ret = 0;
3986 } else if (connector->funcs->set_property)
3987 ret = connector->funcs->set_property(connector, property, value);
3988
3989 /* store the property value if successful */
3990 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003991 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003992 return ret;
3993}
3994
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003995static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3996 struct drm_property *property,
3997 uint64_t value)
3998{
3999 int ret = -EINVAL;
4000 struct drm_crtc *crtc = obj_to_crtc(obj);
4001
4002 if (crtc->funcs->set_property)
4003 ret = crtc->funcs->set_property(crtc, property, value);
4004 if (!ret)
4005 drm_object_property_set_value(obj, property, value);
4006
4007 return ret;
4008}
4009
Rob Clark4d939142012-05-17 02:23:27 -06004010static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4011 struct drm_property *property,
4012 uint64_t value)
4013{
4014 int ret = -EINVAL;
4015 struct drm_plane *plane = obj_to_plane(obj);
4016
4017 if (plane->funcs->set_property)
4018 ret = plane->funcs->set_property(plane, property, value);
4019 if (!ret)
4020 drm_object_property_set_value(obj, property, value);
4021
4022 return ret;
4023}
4024
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004025/**
4026 * drm_mode_getproperty_ioctl - get the current value of a object's property
4027 * @dev: DRM device
4028 * @data: ioctl data
4029 * @file_priv: DRM file info
4030 *
4031 * This function retrieves the current value for an object's property. Compared
4032 * to the connector specific ioctl this one is extended to also work on crtc and
4033 * plane objects.
4034 *
4035 * Called by the user via ioctl.
4036 *
4037 * Returns:
4038 * Zero on success, errno on failure.
4039 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004040int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4041 struct drm_file *file_priv)
4042{
4043 struct drm_mode_obj_get_properties *arg = data;
4044 struct drm_mode_object *obj;
4045 int ret = 0;
4046 int i;
4047 int copied = 0;
4048 int props_count = 0;
4049 uint32_t __user *props_ptr;
4050 uint64_t __user *prop_values_ptr;
4051
4052 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4053 return -EINVAL;
4054
Daniel Vetter84849902012-12-02 00:28:11 +01004055 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004056
4057 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4058 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004059 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004060 goto out;
4061 }
4062 if (!obj->properties) {
4063 ret = -EINVAL;
4064 goto out;
4065 }
4066
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004067 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004068
4069 /* This ioctl is called twice, once to determine how much space is
4070 * needed, and the 2nd time to fill it. */
4071 if ((arg->count_props >= props_count) && props_count) {
4072 copied = 0;
4073 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4074 prop_values_ptr = (uint64_t __user *)(unsigned long)
4075 (arg->prop_values_ptr);
4076 for (i = 0; i < props_count; i++) {
4077 if (put_user(obj->properties->ids[i],
4078 props_ptr + copied)) {
4079 ret = -EFAULT;
4080 goto out;
4081 }
4082 if (put_user(obj->properties->values[i],
4083 prop_values_ptr + copied)) {
4084 ret = -EFAULT;
4085 goto out;
4086 }
4087 copied++;
4088 }
4089 }
4090 arg->count_props = props_count;
4091out:
Daniel Vetter84849902012-12-02 00:28:11 +01004092 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004093 return ret;
4094}
4095
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004096/**
4097 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4098 * @dev: DRM device
4099 * @data: ioctl data
4100 * @file_priv: DRM file info
4101 *
4102 * This function sets the current value for an object's property. It also calls
4103 * into a driver's ->set_property callback to update the hardware state.
4104 * Compared to the connector specific ioctl this one is extended to also work on
4105 * crtc and plane objects.
4106 *
4107 * Called by the user via ioctl.
4108 *
4109 * Returns:
4110 * Zero on success, errno on failure.
4111 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004112int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4113 struct drm_file *file_priv)
4114{
4115 struct drm_mode_obj_set_property *arg = data;
4116 struct drm_mode_object *arg_obj;
4117 struct drm_mode_object *prop_obj;
4118 struct drm_property *property;
4119 int ret = -EINVAL;
4120 int i;
4121
4122 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4123 return -EINVAL;
4124
Daniel Vetter84849902012-12-02 00:28:11 +01004125 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004126
4127 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004128 if (!arg_obj) {
4129 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004130 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004131 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004132 if (!arg_obj->properties)
4133 goto out;
4134
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004135 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004136 if (arg_obj->properties->ids[i] == arg->prop_id)
4137 break;
4138
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004139 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004140 goto out;
4141
4142 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4143 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004144 if (!prop_obj) {
4145 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004146 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004147 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004148 property = obj_to_property(prop_obj);
4149
4150 if (!drm_property_change_is_valid(property, arg->value))
4151 goto out;
4152
4153 switch (arg_obj->type) {
4154 case DRM_MODE_OBJECT_CONNECTOR:
4155 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4156 arg->value);
4157 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004158 case DRM_MODE_OBJECT_CRTC:
4159 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4160 break;
Rob Clark4d939142012-05-17 02:23:27 -06004161 case DRM_MODE_OBJECT_PLANE:
4162 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4163 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004164 }
4165
4166out:
Daniel Vetter84849902012-12-02 00:28:11 +01004167 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004168 return ret;
4169}
4170
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004171/**
4172 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4173 * @connector: connector to attach
4174 * @encoder: encoder to attach @connector to
4175 *
4176 * This function links up a connector to an encoder. Note that the routing
4177 * restrictions between encoders and crtcs are exposed to userspace through the
4178 * possible_clones and possible_crtcs bitmasks.
4179 *
4180 * Returns:
4181 * Zero on success, errno on failure.
4182 */
Dave Airlief453ba02008-11-07 14:05:41 -08004183int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4184 struct drm_encoder *encoder)
4185{
4186 int i;
4187
4188 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4189 if (connector->encoder_ids[i] == 0) {
4190 connector->encoder_ids[i] = encoder->base.id;
4191 return 0;
4192 }
4193 }
4194 return -ENOMEM;
4195}
4196EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4197
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004198/**
4199 * drm_mode_crtc_set_gamma_size - set the gamma table size
4200 * @crtc: CRTC to set the gamma table size for
4201 * @gamma_size: size of the gamma table
4202 *
4203 * Drivers which support gamma tables should set this to the supported gamma
4204 * table size when initializing the CRTC. Currently the drm core only supports a
4205 * fixed gamma table size.
4206 *
4207 * Returns:
4208 * Zero on success, errno on failure.
4209 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004210int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004211 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004212{
4213 crtc->gamma_size = gamma_size;
4214
4215 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4216 if (!crtc->gamma_store) {
4217 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004218 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004219 }
4220
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004221 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004222}
4223EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4224
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004225/**
4226 * drm_mode_gamma_set_ioctl - set the gamma table
4227 * @dev: DRM device
4228 * @data: ioctl data
4229 * @file_priv: DRM file info
4230 *
4231 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4232 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4233 *
4234 * Called by the user via ioctl.
4235 *
4236 * Returns:
4237 * Zero on success, errno on failure.
4238 */
Dave Airlief453ba02008-11-07 14:05:41 -08004239int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4240 void *data, struct drm_file *file_priv)
4241{
4242 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004243 struct drm_crtc *crtc;
4244 void *r_base, *g_base, *b_base;
4245 int size;
4246 int ret = 0;
4247
Dave Airliefb3b06c2011-02-08 13:55:21 +10004248 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4249 return -EINVAL;
4250
Daniel Vetter84849902012-12-02 00:28:11 +01004251 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004252 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4253 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004254 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004255 goto out;
4256 }
Dave Airlief453ba02008-11-07 14:05:41 -08004257
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004258 if (crtc->funcs->gamma_set == NULL) {
4259 ret = -ENOSYS;
4260 goto out;
4261 }
4262
Dave Airlief453ba02008-11-07 14:05:41 -08004263 /* memcpy into gamma store */
4264 if (crtc_lut->gamma_size != crtc->gamma_size) {
4265 ret = -EINVAL;
4266 goto out;
4267 }
4268
4269 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4270 r_base = crtc->gamma_store;
4271 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4272 ret = -EFAULT;
4273 goto out;
4274 }
4275
4276 g_base = r_base + size;
4277 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4278 ret = -EFAULT;
4279 goto out;
4280 }
4281
4282 b_base = g_base + size;
4283 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4284 ret = -EFAULT;
4285 goto out;
4286 }
4287
James Simmons72034252010-08-03 01:33:19 +01004288 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004289
4290out:
Daniel Vetter84849902012-12-02 00:28:11 +01004291 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004292 return ret;
4293
4294}
4295
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004296/**
4297 * drm_mode_gamma_get_ioctl - get the gamma table
4298 * @dev: DRM device
4299 * @data: ioctl data
4300 * @file_priv: DRM file info
4301 *
4302 * Copy the current gamma table into the storage provided. This also provides
4303 * the gamma table size the driver expects, which can be used to size the
4304 * allocated storage.
4305 *
4306 * Called by the user via ioctl.
4307 *
4308 * Returns:
4309 * Zero on success, errno on failure.
4310 */
Dave Airlief453ba02008-11-07 14:05:41 -08004311int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4312 void *data, struct drm_file *file_priv)
4313{
4314 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004315 struct drm_crtc *crtc;
4316 void *r_base, *g_base, *b_base;
4317 int size;
4318 int ret = 0;
4319
Dave Airliefb3b06c2011-02-08 13:55:21 +10004320 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4321 return -EINVAL;
4322
Daniel Vetter84849902012-12-02 00:28:11 +01004323 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004324 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4325 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004326 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004327 goto out;
4328 }
Dave Airlief453ba02008-11-07 14:05:41 -08004329
4330 /* memcpy into gamma store */
4331 if (crtc_lut->gamma_size != crtc->gamma_size) {
4332 ret = -EINVAL;
4333 goto out;
4334 }
4335
4336 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4337 r_base = crtc->gamma_store;
4338 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4339 ret = -EFAULT;
4340 goto out;
4341 }
4342
4343 g_base = r_base + size;
4344 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4345 ret = -EFAULT;
4346 goto out;
4347 }
4348
4349 b_base = g_base + size;
4350 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4351 ret = -EFAULT;
4352 goto out;
4353 }
4354out:
Daniel Vetter84849902012-12-02 00:28:11 +01004355 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004356 return ret;
4357}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004358
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004359/**
4360 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4361 * @dev: DRM device
4362 * @data: ioctl data
4363 * @file_priv: DRM file info
4364 *
4365 * This schedules an asynchronous update on a given CRTC, called page flip.
4366 * Optionally a drm event is generated to signal the completion of the event.
4367 * Generic drivers cannot assume that a pageflip with changed framebuffer
4368 * properties (including driver specific metadata like tiling layout) will work,
4369 * but some drivers support e.g. pixel format changes through the pageflip
4370 * ioctl.
4371 *
4372 * Called by the user via ioctl.
4373 *
4374 * Returns:
4375 * Zero on success, errno on failure.
4376 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004377int drm_mode_page_flip_ioctl(struct drm_device *dev,
4378 void *data, struct drm_file *file_priv)
4379{
4380 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004381 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004382 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004383 struct drm_pending_vblank_event *e = NULL;
4384 unsigned long flags;
4385 int ret = -EINVAL;
4386
4387 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4388 page_flip->reserved != 0)
4389 return -EINVAL;
4390
Keith Packard62f21042013-07-22 18:50:00 -07004391 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4392 return -EINVAL;
4393
Rob Clarka2b34e22013-10-05 16:36:52 -04004394 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4395 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004396 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004397
Rob Clark51fd3712013-11-19 12:10:12 -05004398 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004399 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004400 /* The framebuffer is currently unbound, presumably
4401 * due to a hotplug event, that userspace has not
4402 * yet discovered.
4403 */
4404 ret = -EBUSY;
4405 goto out;
4406 }
4407
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004408 if (crtc->funcs->page_flip == NULL)
4409 goto out;
4410
Daniel Vetter786b99e2012-12-02 21:53:40 +01004411 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004412 if (!fb) {
4413 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004414 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004415 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004416
Damien Lespiauc11e9282013-09-25 16:45:30 +01004417 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4418 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004419 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004420
Matt Roperf4510a22014-04-01 15:22:40 -07004421 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004422 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4423 ret = -EINVAL;
4424 goto out;
4425 }
4426
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004427 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4428 ret = -ENOMEM;
4429 spin_lock_irqsave(&dev->event_lock, flags);
4430 if (file_priv->event_space < sizeof e->event) {
4431 spin_unlock_irqrestore(&dev->event_lock, flags);
4432 goto out;
4433 }
4434 file_priv->event_space -= sizeof e->event;
4435 spin_unlock_irqrestore(&dev->event_lock, flags);
4436
4437 e = kzalloc(sizeof *e, GFP_KERNEL);
4438 if (e == NULL) {
4439 spin_lock_irqsave(&dev->event_lock, flags);
4440 file_priv->event_space += sizeof e->event;
4441 spin_unlock_irqrestore(&dev->event_lock, flags);
4442 goto out;
4443 }
4444
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004445 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004446 e->event.base.length = sizeof e->event;
4447 e->event.user_data = page_flip->user_data;
4448 e->base.event = &e->event.base;
4449 e->base.file_priv = file_priv;
4450 e->base.destroy =
4451 (void (*) (struct drm_pending_event *)) kfree;
4452 }
4453
Matt Roperf4510a22014-04-01 15:22:40 -07004454 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004455 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004456 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004457 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4458 spin_lock_irqsave(&dev->event_lock, flags);
4459 file_priv->event_space += sizeof e->event;
4460 spin_unlock_irqrestore(&dev->event_lock, flags);
4461 kfree(e);
4462 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004463 /* Keep the old fb, don't unref it. */
4464 old_fb = NULL;
4465 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004466 /*
4467 * Warn if the driver hasn't properly updated the crtc->fb
4468 * field to reflect that the new framebuffer is now used.
4469 * Failing to do so will screw with the reference counting
4470 * on framebuffers.
4471 */
Matt Roperf4510a22014-04-01 15:22:40 -07004472 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004473 /* Unref only the old framebuffer. */
4474 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004475 }
4476
4477out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004478 if (fb)
4479 drm_framebuffer_unreference(fb);
4480 if (old_fb)
4481 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004482 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004483
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004484 return ret;
4485}
Chris Wilsoneb033552011-01-24 15:11:08 +00004486
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004487/**
4488 * drm_mode_config_reset - call ->reset callbacks
4489 * @dev: drm device
4490 *
4491 * This functions calls all the crtc's, encoder's and connector's ->reset
4492 * callback. Drivers can use this in e.g. their driver load or resume code to
4493 * reset hardware and software state.
4494 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004495void drm_mode_config_reset(struct drm_device *dev)
4496{
4497 struct drm_crtc *crtc;
4498 struct drm_encoder *encoder;
4499 struct drm_connector *connector;
4500
4501 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4502 if (crtc->funcs->reset)
4503 crtc->funcs->reset(crtc);
4504
4505 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4506 if (encoder->funcs->reset)
4507 encoder->funcs->reset(encoder);
4508
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004509 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4510 connector->status = connector_status_unknown;
4511
Chris Wilsoneb033552011-01-24 15:11:08 +00004512 if (connector->funcs->reset)
4513 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004514 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004515}
4516EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004517
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004518/**
4519 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4520 * @dev: DRM device
4521 * @data: ioctl data
4522 * @file_priv: DRM file info
4523 *
4524 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4525 * TTM or something else entirely) and returns the resulting buffer handle. This
4526 * handle can then be wrapped up into a framebuffer modeset object.
4527 *
4528 * Note that userspace is not allowed to use such objects for render
4529 * acceleration - drivers must create their own private ioctls for such a use
4530 * case.
4531 *
4532 * Called by the user via ioctl.
4533 *
4534 * Returns:
4535 * Zero on success, errno on failure.
4536 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004537int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4538 void *data, struct drm_file *file_priv)
4539{
4540 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004541 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004542
4543 if (!dev->driver->dumb_create)
4544 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004545 if (!args->width || !args->height || !args->bpp)
4546 return -EINVAL;
4547
4548 /* overflow checks for 32bit size calculations */
4549 cpp = DIV_ROUND_UP(args->bpp, 8);
4550 if (cpp > 0xffffffffU / args->width)
4551 return -EINVAL;
4552 stride = cpp * args->width;
4553 if (args->height > 0xffffffffU / stride)
4554 return -EINVAL;
4555
4556 /* test for wrap-around */
4557 size = args->height * stride;
4558 if (PAGE_ALIGN(size) == 0)
4559 return -EINVAL;
4560
Dave Airlieff72145b2011-02-07 12:16:14 +10004561 return dev->driver->dumb_create(file_priv, dev, args);
4562}
4563
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004564/**
4565 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4566 * @dev: DRM device
4567 * @data: ioctl data
4568 * @file_priv: DRM file info
4569 *
4570 * Allocate an offset in the drm device node's address space to be able to
4571 * memory map a dumb buffer.
4572 *
4573 * Called by the user via ioctl.
4574 *
4575 * Returns:
4576 * Zero on success, errno on failure.
4577 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004578int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4579 void *data, struct drm_file *file_priv)
4580{
4581 struct drm_mode_map_dumb *args = data;
4582
4583 /* call driver ioctl to get mmap offset */
4584 if (!dev->driver->dumb_map_offset)
4585 return -ENOSYS;
4586
4587 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4588}
4589
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004590/**
4591 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4592 * @dev: DRM device
4593 * @data: ioctl data
4594 * @file_priv: DRM file info
4595 *
4596 * This destroys the userspace handle for the given dumb backing storage buffer.
4597 * Since buffer objects must be reference counted in the kernel a buffer object
4598 * won't be immediately freed if a framebuffer modeset object still uses it.
4599 *
4600 * Called by the user via ioctl.
4601 *
4602 * Returns:
4603 * Zero on success, errno on failure.
4604 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004605int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4606 void *data, struct drm_file *file_priv)
4607{
4608 struct drm_mode_destroy_dumb *args = data;
4609
4610 if (!dev->driver->dumb_destroy)
4611 return -ENOSYS;
4612
4613 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4614}
Dave Airlie248dbc22011-11-29 20:02:54 +00004615
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004616/**
4617 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4618 * @format: pixel format (DRM_FORMAT_*)
4619 * @depth: storage for the depth value
4620 * @bpp: storage for the bpp value
4621 *
4622 * This only supports RGB formats here for compat with code that doesn't use
4623 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004624 */
4625void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4626 int *bpp)
4627{
4628 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004629 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004630 case DRM_FORMAT_RGB332:
4631 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004632 *depth = 8;
4633 *bpp = 8;
4634 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004635 case DRM_FORMAT_XRGB1555:
4636 case DRM_FORMAT_XBGR1555:
4637 case DRM_FORMAT_RGBX5551:
4638 case DRM_FORMAT_BGRX5551:
4639 case DRM_FORMAT_ARGB1555:
4640 case DRM_FORMAT_ABGR1555:
4641 case DRM_FORMAT_RGBA5551:
4642 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004643 *depth = 15;
4644 *bpp = 16;
4645 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004646 case DRM_FORMAT_RGB565:
4647 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004648 *depth = 16;
4649 *bpp = 16;
4650 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004651 case DRM_FORMAT_RGB888:
4652 case DRM_FORMAT_BGR888:
4653 *depth = 24;
4654 *bpp = 24;
4655 break;
4656 case DRM_FORMAT_XRGB8888:
4657 case DRM_FORMAT_XBGR8888:
4658 case DRM_FORMAT_RGBX8888:
4659 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004660 *depth = 24;
4661 *bpp = 32;
4662 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004663 case DRM_FORMAT_XRGB2101010:
4664 case DRM_FORMAT_XBGR2101010:
4665 case DRM_FORMAT_RGBX1010102:
4666 case DRM_FORMAT_BGRX1010102:
4667 case DRM_FORMAT_ARGB2101010:
4668 case DRM_FORMAT_ABGR2101010:
4669 case DRM_FORMAT_RGBA1010102:
4670 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004671 *depth = 30;
4672 *bpp = 32;
4673 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004674 case DRM_FORMAT_ARGB8888:
4675 case DRM_FORMAT_ABGR8888:
4676 case DRM_FORMAT_RGBA8888:
4677 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004678 *depth = 32;
4679 *bpp = 32;
4680 break;
4681 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004682 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4683 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004684 *depth = 0;
4685 *bpp = 0;
4686 break;
4687 }
4688}
4689EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004690
4691/**
4692 * drm_format_num_planes - get the number of planes for format
4693 * @format: pixel format (DRM_FORMAT_*)
4694 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004695 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004696 * The number of planes used by the specified pixel format.
4697 */
4698int drm_format_num_planes(uint32_t format)
4699{
4700 switch (format) {
4701 case DRM_FORMAT_YUV410:
4702 case DRM_FORMAT_YVU410:
4703 case DRM_FORMAT_YUV411:
4704 case DRM_FORMAT_YVU411:
4705 case DRM_FORMAT_YUV420:
4706 case DRM_FORMAT_YVU420:
4707 case DRM_FORMAT_YUV422:
4708 case DRM_FORMAT_YVU422:
4709 case DRM_FORMAT_YUV444:
4710 case DRM_FORMAT_YVU444:
4711 return 3;
4712 case DRM_FORMAT_NV12:
4713 case DRM_FORMAT_NV21:
4714 case DRM_FORMAT_NV16:
4715 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004716 case DRM_FORMAT_NV24:
4717 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004718 return 2;
4719 default:
4720 return 1;
4721 }
4722}
4723EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004724
4725/**
4726 * drm_format_plane_cpp - determine the bytes per pixel value
4727 * @format: pixel format (DRM_FORMAT_*)
4728 * @plane: plane index
4729 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004730 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004731 * The bytes per pixel value for the specified plane.
4732 */
4733int drm_format_plane_cpp(uint32_t format, int plane)
4734{
4735 unsigned int depth;
4736 int bpp;
4737
4738 if (plane >= drm_format_num_planes(format))
4739 return 0;
4740
4741 switch (format) {
4742 case DRM_FORMAT_YUYV:
4743 case DRM_FORMAT_YVYU:
4744 case DRM_FORMAT_UYVY:
4745 case DRM_FORMAT_VYUY:
4746 return 2;
4747 case DRM_FORMAT_NV12:
4748 case DRM_FORMAT_NV21:
4749 case DRM_FORMAT_NV16:
4750 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004751 case DRM_FORMAT_NV24:
4752 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004753 return plane ? 2 : 1;
4754 case DRM_FORMAT_YUV410:
4755 case DRM_FORMAT_YVU410:
4756 case DRM_FORMAT_YUV411:
4757 case DRM_FORMAT_YVU411:
4758 case DRM_FORMAT_YUV420:
4759 case DRM_FORMAT_YVU420:
4760 case DRM_FORMAT_YUV422:
4761 case DRM_FORMAT_YVU422:
4762 case DRM_FORMAT_YUV444:
4763 case DRM_FORMAT_YVU444:
4764 return 1;
4765 default:
4766 drm_fb_get_bpp_depth(format, &depth, &bpp);
4767 return bpp >> 3;
4768 }
4769}
4770EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004771
4772/**
4773 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4774 * @format: pixel format (DRM_FORMAT_*)
4775 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004776 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004777 * The horizontal chroma subsampling factor for the
4778 * specified pixel format.
4779 */
4780int drm_format_horz_chroma_subsampling(uint32_t format)
4781{
4782 switch (format) {
4783 case DRM_FORMAT_YUV411:
4784 case DRM_FORMAT_YVU411:
4785 case DRM_FORMAT_YUV410:
4786 case DRM_FORMAT_YVU410:
4787 return 4;
4788 case DRM_FORMAT_YUYV:
4789 case DRM_FORMAT_YVYU:
4790 case DRM_FORMAT_UYVY:
4791 case DRM_FORMAT_VYUY:
4792 case DRM_FORMAT_NV12:
4793 case DRM_FORMAT_NV21:
4794 case DRM_FORMAT_NV16:
4795 case DRM_FORMAT_NV61:
4796 case DRM_FORMAT_YUV422:
4797 case DRM_FORMAT_YVU422:
4798 case DRM_FORMAT_YUV420:
4799 case DRM_FORMAT_YVU420:
4800 return 2;
4801 default:
4802 return 1;
4803 }
4804}
4805EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4806
4807/**
4808 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4809 * @format: pixel format (DRM_FORMAT_*)
4810 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004811 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004812 * The vertical chroma subsampling factor for the
4813 * specified pixel format.
4814 */
4815int drm_format_vert_chroma_subsampling(uint32_t format)
4816{
4817 switch (format) {
4818 case DRM_FORMAT_YUV410:
4819 case DRM_FORMAT_YVU410:
4820 return 4;
4821 case DRM_FORMAT_YUV420:
4822 case DRM_FORMAT_YVU420:
4823 case DRM_FORMAT_NV12:
4824 case DRM_FORMAT_NV21:
4825 return 2;
4826 default:
4827 return 1;
4828 }
4829}
4830EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004831
4832/**
4833 * drm_mode_config_init - initialize DRM mode_configuration structure
4834 * @dev: DRM device
4835 *
4836 * Initialize @dev's mode_config structure, used for tracking the graphics
4837 * configuration of @dev.
4838 *
4839 * Since this initializes the modeset locks, no locking is possible. Which is no
4840 * problem, since this should happen single threaded at init time. It is the
4841 * driver's problem to ensure this guarantee.
4842 *
4843 */
4844void drm_mode_config_init(struct drm_device *dev)
4845{
4846 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004847 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004848 mutex_init(&dev->mode_config.idr_mutex);
4849 mutex_init(&dev->mode_config.fb_lock);
4850 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4851 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4852 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004853 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004854 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4855 INIT_LIST_HEAD(&dev->mode_config.property_list);
4856 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4857 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4858 idr_init(&dev->mode_config.crtc_idr);
4859
4860 drm_modeset_lock_all(dev);
4861 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004862 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004863 drm_modeset_unlock_all(dev);
4864
4865 /* Just to be sure */
4866 dev->mode_config.num_fb = 0;
4867 dev->mode_config.num_connector = 0;
4868 dev->mode_config.num_crtc = 0;
4869 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004870 dev->mode_config.num_overlay_plane = 0;
4871 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004872}
4873EXPORT_SYMBOL(drm_mode_config_init);
4874
4875/**
4876 * drm_mode_config_cleanup - free up DRM mode_config info
4877 * @dev: DRM device
4878 *
4879 * Free up all the connectors and CRTCs associated with this DRM device, then
4880 * free up the framebuffers and associated buffer objects.
4881 *
4882 * Note that since this /should/ happen single-threaded at driver/device
4883 * teardown time, no locking is required. It's the driver's job to ensure that
4884 * this guarantee actually holds true.
4885 *
4886 * FIXME: cleanup any dangling user buffer objects too
4887 */
4888void drm_mode_config_cleanup(struct drm_device *dev)
4889{
4890 struct drm_connector *connector, *ot;
4891 struct drm_crtc *crtc, *ct;
4892 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004893 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004894 struct drm_framebuffer *fb, *fbt;
4895 struct drm_property *property, *pt;
4896 struct drm_property_blob *blob, *bt;
4897 struct drm_plane *plane, *plt;
4898
4899 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4900 head) {
4901 encoder->funcs->destroy(encoder);
4902 }
4903
Sean Paul3b336ec2013-08-14 16:47:37 -04004904 list_for_each_entry_safe(bridge, brt,
4905 &dev->mode_config.bridge_list, head) {
4906 bridge->funcs->destroy(bridge);
4907 }
4908
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004909 list_for_each_entry_safe(connector, ot,
4910 &dev->mode_config.connector_list, head) {
4911 connector->funcs->destroy(connector);
4912 }
4913
4914 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4915 head) {
4916 drm_property_destroy(dev, property);
4917 }
4918
4919 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4920 head) {
4921 drm_property_destroy_blob(dev, blob);
4922 }
4923
4924 /*
4925 * Single-threaded teardown context, so it's not required to grab the
4926 * fb_lock to protect against concurrent fb_list access. Contrary, it
4927 * would actually deadlock with the drm_framebuffer_cleanup function.
4928 *
4929 * Also, if there are any framebuffers left, that's a driver leak now,
4930 * so politely WARN about this.
4931 */
4932 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4933 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4934 drm_framebuffer_remove(fb);
4935 }
4936
4937 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4938 head) {
4939 plane->funcs->destroy(plane);
4940 }
4941
4942 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4943 crtc->funcs->destroy(crtc);
4944 }
4945
4946 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05004947 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004948}
4949EXPORT_SYMBOL(drm_mode_config_cleanup);