blob: 41c7212081b890e237804453f12f5af2ef456ad4 [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,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700730 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700731 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;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700755 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700756 if (primary)
757 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700758 if (cursor)
759 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700760
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200761 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100762 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200763
764 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800765}
Matt Ropere13161a2014-04-01 15:22:38 -0700766EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800767
768/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000769 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800770 * @crtc: CRTC to cleanup
771 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000772 * This function cleans up @crtc and removes it from the DRM mode setting
773 * core. Note that the function does *not* free the crtc structure itself,
774 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800775 */
776void drm_crtc_cleanup(struct drm_crtc *crtc)
777{
778 struct drm_device *dev = crtc->dev;
779
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000780 kfree(crtc->gamma_store);
781 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800782
Rob Clark51fd3712013-11-19 12:10:12 -0500783 drm_modeset_lock_fini(&crtc->mutex);
784
Dave Airlief453ba02008-11-07 14:05:41 -0800785 drm_mode_object_put(dev, &crtc->base);
786 list_del(&crtc->head);
787 dev->mode_config.num_crtc--;
788}
789EXPORT_SYMBOL(drm_crtc_cleanup);
790
791/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000792 * drm_crtc_index - find the index of a registered CRTC
793 * @crtc: CRTC to find index for
794 *
795 * Given a registered CRTC, return the index of that CRTC within a DRM
796 * device's list of CRTCs.
797 */
798unsigned int drm_crtc_index(struct drm_crtc *crtc)
799{
800 unsigned int index = 0;
801 struct drm_crtc *tmp;
802
803 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
804 if (tmp == crtc)
805 return index;
806
807 index++;
808 }
809
810 BUG();
811}
812EXPORT_SYMBOL(drm_crtc_index);
813
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100814/*
Dave Airlief453ba02008-11-07 14:05:41 -0800815 * drm_mode_remove - remove and free a mode
816 * @connector: connector list to modify
817 * @mode: mode to remove
818 *
Dave Airlief453ba02008-11-07 14:05:41 -0800819 * Remove @mode from @connector's mode list, then free it.
820 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100821static void drm_mode_remove(struct drm_connector *connector,
822 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800823{
824 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100825 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800826}
Dave Airlief453ba02008-11-07 14:05:41 -0800827
828/**
829 * drm_connector_init - Init a preallocated connector
830 * @dev: DRM device
831 * @connector: the connector to init
832 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100833 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800834 *
Dave Airlief453ba02008-11-07 14:05:41 -0800835 * Initialises a preallocated connector. Connectors should be
836 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200837 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100838 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200839 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800840 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200841int drm_connector_init(struct drm_device *dev,
842 struct drm_connector *connector,
843 const struct drm_connector_funcs *funcs,
844 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800845{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200846 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400847 struct ida *connector_ida =
848 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200849
Daniel Vetter84849902012-12-02 00:28:11 +0100850 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800851
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200852 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
853 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300854 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200855
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300856 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800857 connector->dev = dev;
858 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800859 connector->connector_type = connector_type;
860 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400861 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
862 if (connector->connector_type_id < 0) {
863 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300864 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400865 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300866 connector->name =
867 kasprintf(GFP_KERNEL, "%s-%d",
868 drm_connector_enum_list[connector_type].name,
869 connector->connector_type_id);
870 if (!connector->name) {
871 ret = -ENOMEM;
872 goto out_put;
873 }
874
Dave Airlief453ba02008-11-07 14:05:41 -0800875 INIT_LIST_HEAD(&connector->probed_modes);
876 INIT_LIST_HEAD(&connector->modes);
877 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000878 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800879
880 list_add_tail(&connector->head, &dev->mode_config.connector_list);
881 dev->mode_config.num_connector++;
882
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200883 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500884 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200885 dev->mode_config.edid_property,
886 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800887
Rob Clark58495562012-10-11 20:50:56 -0500888 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800889 dev->mode_config.dpms_property, 0);
890
Jani Nikula2abdd312014-05-14 16:58:19 +0300891out_put:
892 if (ret)
893 drm_mode_object_put(dev, &connector->base);
894
895out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100896 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200897
898 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800899}
900EXPORT_SYMBOL(drm_connector_init);
901
902/**
903 * drm_connector_cleanup - cleans up an initialised connector
904 * @connector: connector to cleanup
905 *
Dave Airlief453ba02008-11-07 14:05:41 -0800906 * Cleans up the connector but doesn't free the object.
907 */
908void drm_connector_cleanup(struct drm_connector *connector)
909{
910 struct drm_device *dev = connector->dev;
911 struct drm_display_mode *mode, *t;
912
913 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
914 drm_mode_remove(connector, mode);
915
916 list_for_each_entry_safe(mode, t, &connector->modes, head)
917 drm_mode_remove(connector, mode);
918
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400919 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
920 connector->connector_type_id);
921
Dave Airlief453ba02008-11-07 14:05:41 -0800922 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300923 kfree(connector->name);
924 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800925 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000926 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800927}
928EXPORT_SYMBOL(drm_connector_cleanup);
929
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100930/**
931 * drm_connector_unplug_all - unregister connector userspace interfaces
932 * @dev: drm device
933 *
934 * This function unregisters all connector userspace interfaces in sysfs. Should
935 * be call when the device is disconnected, e.g. from an usb driver's
936 * ->disconnect callback.
937 */
Dave Airliecbc7e222012-02-20 14:16:40 +0000938void drm_connector_unplug_all(struct drm_device *dev)
939{
940 struct drm_connector *connector;
941
942 /* taking the mode config mutex ends up in a clash with sysfs */
943 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
944 drm_sysfs_connector_remove(connector);
945
946}
947EXPORT_SYMBOL(drm_connector_unplug_all);
948
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100949/**
950 * drm_bridge_init - initialize a drm transcoder/bridge
951 * @dev: drm device
952 * @bridge: transcoder/bridge to set up
953 * @funcs: bridge function table
954 *
955 * Initialises a preallocated bridge. Bridges should be
956 * subclassed as part of driver connector objects.
957 *
958 * Returns:
959 * Zero on success, error code on failure.
960 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400961int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
962 const struct drm_bridge_funcs *funcs)
963{
964 int ret;
965
966 drm_modeset_lock_all(dev);
967
968 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
969 if (ret)
970 goto out;
971
972 bridge->dev = dev;
973 bridge->funcs = funcs;
974
975 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
976 dev->mode_config.num_bridge++;
977
978 out:
979 drm_modeset_unlock_all(dev);
980 return ret;
981}
982EXPORT_SYMBOL(drm_bridge_init);
983
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100984/**
985 * drm_bridge_cleanup - cleans up an initialised bridge
986 * @bridge: bridge to cleanup
987 *
988 * Cleans up the bridge but doesn't free the object.
989 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400990void drm_bridge_cleanup(struct drm_bridge *bridge)
991{
992 struct drm_device *dev = bridge->dev;
993
994 drm_modeset_lock_all(dev);
995 drm_mode_object_put(dev, &bridge->base);
996 list_del(&bridge->head);
997 dev->mode_config.num_bridge--;
998 drm_modeset_unlock_all(dev);
999}
1000EXPORT_SYMBOL(drm_bridge_cleanup);
1001
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001002/**
1003 * drm_encoder_init - Init a preallocated encoder
1004 * @dev: drm device
1005 * @encoder: the encoder to init
1006 * @funcs: callbacks for this encoder
1007 * @encoder_type: user visible type of the encoder
1008 *
1009 * Initialises a preallocated encoder. Encoder should be
1010 * subclassed as part of driver encoder objects.
1011 *
1012 * Returns:
1013 * Zero on success, error code on failure.
1014 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001015int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001016 struct drm_encoder *encoder,
1017 const struct drm_encoder_funcs *funcs,
1018 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001019{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001020 int ret;
1021
Daniel Vetter84849902012-12-02 00:28:11 +01001022 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001023
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001024 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1025 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001026 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001027
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001028 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001029 encoder->encoder_type = encoder_type;
1030 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001031 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1032 drm_encoder_enum_list[encoder_type].name,
1033 encoder->base.id);
1034 if (!encoder->name) {
1035 ret = -ENOMEM;
1036 goto out_put;
1037 }
Dave Airlief453ba02008-11-07 14:05:41 -08001038
1039 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1040 dev->mode_config.num_encoder++;
1041
Jani Nikulae5748942014-05-14 16:58:20 +03001042out_put:
1043 if (ret)
1044 drm_mode_object_put(dev, &encoder->base);
1045
1046out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001047 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001048
1049 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001050}
1051EXPORT_SYMBOL(drm_encoder_init);
1052
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001053/**
1054 * drm_encoder_cleanup - cleans up an initialised encoder
1055 * @encoder: encoder to cleanup
1056 *
1057 * Cleans up the encoder but doesn't free the object.
1058 */
Dave Airlief453ba02008-11-07 14:05:41 -08001059void drm_encoder_cleanup(struct drm_encoder *encoder)
1060{
1061 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001062 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001063 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001064 kfree(encoder->name);
1065 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001066 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001067 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001068 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001069}
1070EXPORT_SYMBOL(drm_encoder_cleanup);
1071
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001072/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001073 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001074 * @dev: DRM device
1075 * @plane: plane object to init
1076 * @possible_crtcs: bitmask of possible CRTCs
1077 * @funcs: callbacks for the new plane
1078 * @formats: array of supported formats (%DRM_FORMAT_*)
1079 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001080 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001081 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001082 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001083 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001084 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001085 * Zero on success, error code on failure.
1086 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001087int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1088 unsigned long possible_crtcs,
1089 const struct drm_plane_funcs *funcs,
1090 const uint32_t *formats, uint32_t format_count,
1091 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001092{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001093 int ret;
1094
Daniel Vetter84849902012-12-02 00:28:11 +01001095 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001096
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001097 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1098 if (ret)
1099 goto out;
1100
Rob Clark4d939142012-05-17 02:23:27 -06001101 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001102 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001103 plane->funcs = funcs;
1104 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1105 GFP_KERNEL);
1106 if (!plane->format_types) {
1107 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1108 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001109 ret = -ENOMEM;
1110 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001111 }
1112
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001113 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001114 plane->format_count = format_count;
1115 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001116 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001117
Matt Roperdc415ff2014-04-01 15:22:36 -07001118 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1119 dev->mode_config.num_total_plane++;
1120 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1121 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001122
Rob Clark9922ab52014-04-01 20:16:57 -04001123 drm_object_attach_property(&plane->base,
1124 dev->mode_config.plane_type_property,
1125 plane->type);
1126
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001127 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001128 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001129
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001130 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001131}
Matt Roperdc415ff2014-04-01 15:22:36 -07001132EXPORT_SYMBOL(drm_universal_plane_init);
1133
1134/**
1135 * drm_plane_init - Initialize a legacy plane
1136 * @dev: DRM device
1137 * @plane: plane object to init
1138 * @possible_crtcs: bitmask of possible CRTCs
1139 * @funcs: callbacks for the new plane
1140 * @formats: array of supported formats (%DRM_FORMAT_*)
1141 * @format_count: number of elements in @formats
1142 * @is_primary: plane type (primary vs overlay)
1143 *
1144 * Legacy API to initialize a DRM plane.
1145 *
1146 * New drivers should call drm_universal_plane_init() instead.
1147 *
1148 * Returns:
1149 * Zero on success, error code on failure.
1150 */
1151int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1152 unsigned long possible_crtcs,
1153 const struct drm_plane_funcs *funcs,
1154 const uint32_t *formats, uint32_t format_count,
1155 bool is_primary)
1156{
1157 enum drm_plane_type type;
1158
1159 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1160 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1161 formats, format_count, type);
1162}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001163EXPORT_SYMBOL(drm_plane_init);
1164
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001165/**
1166 * drm_plane_cleanup - Clean up the core plane usage
1167 * @plane: plane to cleanup
1168 *
1169 * This function cleans up @plane and removes it from the DRM mode setting
1170 * core. Note that the function does *not* free the plane structure itself,
1171 * this is the responsibility of the caller.
1172 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001173void drm_plane_cleanup(struct drm_plane *plane)
1174{
1175 struct drm_device *dev = plane->dev;
1176
Daniel Vetter84849902012-12-02 00:28:11 +01001177 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001178 kfree(plane->format_types);
1179 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001180
1181 BUG_ON(list_empty(&plane->head));
1182
1183 list_del(&plane->head);
1184 dev->mode_config.num_total_plane--;
1185 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1186 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001187 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001188}
1189EXPORT_SYMBOL(drm_plane_cleanup);
1190
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001191/**
1192 * drm_plane_force_disable - Forcibly disable a plane
1193 * @plane: plane to disable
1194 *
1195 * Forces the plane to be disabled.
1196 *
1197 * Used when the plane's current framebuffer is destroyed,
1198 * and when restoring fbdev mode.
1199 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001200void drm_plane_force_disable(struct drm_plane *plane)
1201{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001202 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001203 int ret;
1204
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001205 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001206 return;
1207
1208 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001209 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001210 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001211 return;
1212 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001213 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001214 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001215 plane->fb = NULL;
1216 plane->crtc = NULL;
1217}
1218EXPORT_SYMBOL(drm_plane_force_disable);
1219
Dave Airlief453ba02008-11-07 14:05:41 -08001220static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1221{
1222 struct drm_property *edid;
1223 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001224
1225 /*
1226 * Standard properties (apply to all connectors)
1227 */
1228 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1229 DRM_MODE_PROP_IMMUTABLE,
1230 "EDID", 0);
1231 dev->mode_config.edid_property = edid;
1232
Sascha Hauer4a67d392012-02-06 10:58:17 +01001233 dpms = drm_property_create_enum(dev, 0,
1234 "DPMS", drm_dpms_enum_list,
1235 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001236 dev->mode_config.dpms_property = dpms;
1237
1238 return 0;
1239}
1240
Rob Clark9922ab52014-04-01 20:16:57 -04001241static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1242{
1243 struct drm_property *type;
1244
1245 /*
1246 * Standard properties (apply to all planes)
1247 */
1248 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1249 "type", drm_plane_type_enum_list,
1250 ARRAY_SIZE(drm_plane_type_enum_list));
1251 dev->mode_config.plane_type_property = type;
1252
1253 return 0;
1254}
1255
Dave Airlief453ba02008-11-07 14:05:41 -08001256/**
1257 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1258 * @dev: DRM device
1259 *
1260 * Called by a driver the first time a DVI-I connector is made.
1261 */
1262int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1263{
1264 struct drm_property *dvi_i_selector;
1265 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001266
1267 if (dev->mode_config.dvi_i_select_subconnector_property)
1268 return 0;
1269
1270 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001271 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001272 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001273 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001274 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001275 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1276
Sascha Hauer4a67d392012-02-06 10:58:17 +01001277 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001278 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001279 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001280 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001281 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1282
1283 return 0;
1284}
1285EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1286
1287/**
1288 * drm_create_tv_properties - create TV specific connector properties
1289 * @dev: DRM device
1290 * @num_modes: number of different TV formats (modes) supported
1291 * @modes: array of pointers to strings containing name of each format
1292 *
1293 * Called by a driver's TV initialization routine, this function creates
1294 * the TV specific connector properties for a given device. Caller is
1295 * responsible for allocating a list of format names and passing them to
1296 * this routine.
1297 */
1298int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1299 char *modes[])
1300{
1301 struct drm_property *tv_selector;
1302 struct drm_property *tv_subconnector;
1303 int i;
1304
1305 if (dev->mode_config.tv_select_subconnector_property)
1306 return 0;
1307
1308 /*
1309 * Basic connector properties
1310 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001311 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001312 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001313 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001314 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001315 dev->mode_config.tv_select_subconnector_property = tv_selector;
1316
1317 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001318 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1319 "subconnector",
1320 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001321 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001322 dev->mode_config.tv_subconnector_property = tv_subconnector;
1323
1324 /*
1325 * Other, TV specific properties: margins & TV modes.
1326 */
1327 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001328 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001329
1330 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001331 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001332
1333 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001334 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001335
1336 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001337 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001338
1339 dev->mode_config.tv_mode_property =
1340 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1341 "mode", num_modes);
1342 for (i = 0; i < num_modes; i++)
1343 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1344 i, modes[i]);
1345
Francisco Jerezb6b79022009-08-02 04:19:20 +02001346 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001347 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001348
1349 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001350 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001351
1352 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001353 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001354
Francisco Jereza75f0232009-08-12 02:30:10 +02001355 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001356 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001357
1358 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001359 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001360
1361 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001362 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001363
Dave Airlief453ba02008-11-07 14:05:41 -08001364 return 0;
1365}
1366EXPORT_SYMBOL(drm_mode_create_tv_properties);
1367
1368/**
1369 * drm_mode_create_scaling_mode_property - create scaling mode property
1370 * @dev: DRM device
1371 *
1372 * Called by a driver the first time it's needed, must be attached to desired
1373 * connectors.
1374 */
1375int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1376{
1377 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001378
1379 if (dev->mode_config.scaling_mode_property)
1380 return 0;
1381
1382 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001383 drm_property_create_enum(dev, 0, "scaling mode",
1384 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001385 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001386
1387 dev->mode_config.scaling_mode_property = scaling_mode;
1388
1389 return 0;
1390}
1391EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1392
1393/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001394 * drm_mode_create_dirty_property - create dirty property
1395 * @dev: DRM device
1396 *
1397 * Called by a driver the first time it's needed, must be attached to desired
1398 * connectors.
1399 */
1400int drm_mode_create_dirty_info_property(struct drm_device *dev)
1401{
1402 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001403
1404 if (dev->mode_config.dirty_info_property)
1405 return 0;
1406
1407 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001408 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001409 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001410 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001411 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001412 dev->mode_config.dirty_info_property = dirty_info;
1413
1414 return 0;
1415}
1416EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1417
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001418static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001419{
1420 uint32_t total_objects = 0;
1421
1422 total_objects += dev->mode_config.num_crtc;
1423 total_objects += dev->mode_config.num_connector;
1424 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001425 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001426
Dave Airlief453ba02008-11-07 14:05:41 -08001427 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1428 if (!group->id_list)
1429 return -ENOMEM;
1430
1431 group->num_crtcs = 0;
1432 group->num_connectors = 0;
1433 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001434 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001435 return 0;
1436}
1437
Dave Airliead222792014-05-02 13:22:19 +10001438void drm_mode_group_destroy(struct drm_mode_group *group)
1439{
1440 kfree(group->id_list);
1441 group->id_list = NULL;
1442}
1443
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001444/*
1445 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1446 * the drm core's responsibility to set up mode control groups.
1447 */
Dave Airlief453ba02008-11-07 14:05:41 -08001448int drm_mode_group_init_legacy_group(struct drm_device *dev,
1449 struct drm_mode_group *group)
1450{
1451 struct drm_crtc *crtc;
1452 struct drm_encoder *encoder;
1453 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001454 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001455 int ret;
1456
1457 if ((ret = drm_mode_group_init(dev, group)))
1458 return ret;
1459
1460 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1461 group->id_list[group->num_crtcs++] = crtc->base.id;
1462
1463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1464 group->id_list[group->num_crtcs + group->num_encoders++] =
1465 encoder->base.id;
1466
1467 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1468 group->id_list[group->num_crtcs + group->num_encoders +
1469 group->num_connectors++] = connector->base.id;
1470
Sean Paul3b336ec2013-08-14 16:47:37 -04001471 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1472 group->id_list[group->num_crtcs + group->num_encoders +
1473 group->num_connectors + group->num_bridges++] =
1474 bridge->base.id;
1475
Dave Airlief453ba02008-11-07 14:05:41 -08001476 return 0;
1477}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001478EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001479
1480/**
Dave Airlief453ba02008-11-07 14:05:41 -08001481 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1482 * @out: drm_mode_modeinfo struct to return to the user
1483 * @in: drm_display_mode to use
1484 *
Dave Airlief453ba02008-11-07 14:05:41 -08001485 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1486 * the user.
1487 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001488static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1489 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001490{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001491 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1492 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1493 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1494 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1495 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1496 "timing values too large for mode info\n");
1497
Dave Airlief453ba02008-11-07 14:05:41 -08001498 out->clock = in->clock;
1499 out->hdisplay = in->hdisplay;
1500 out->hsync_start = in->hsync_start;
1501 out->hsync_end = in->hsync_end;
1502 out->htotal = in->htotal;
1503 out->hskew = in->hskew;
1504 out->vdisplay = in->vdisplay;
1505 out->vsync_start = in->vsync_start;
1506 out->vsync_end = in->vsync_end;
1507 out->vtotal = in->vtotal;
1508 out->vscan = in->vscan;
1509 out->vrefresh = in->vrefresh;
1510 out->flags = in->flags;
1511 out->type = in->type;
1512 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1513 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1514}
1515
1516/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001517 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001518 * @out: drm_display_mode to return to the user
1519 * @in: drm_mode_modeinfo to use
1520 *
Dave Airlief453ba02008-11-07 14:05:41 -08001521 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1522 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001523 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001524 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001525 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001526 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001527static int drm_crtc_convert_umode(struct drm_display_mode *out,
1528 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001529{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001530 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1531 return -ERANGE;
1532
Damien Lespiau5848ad42013-09-27 12:11:50 +01001533 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1534 return -EINVAL;
1535
Dave Airlief453ba02008-11-07 14:05:41 -08001536 out->clock = in->clock;
1537 out->hdisplay = in->hdisplay;
1538 out->hsync_start = in->hsync_start;
1539 out->hsync_end = in->hsync_end;
1540 out->htotal = in->htotal;
1541 out->hskew = in->hskew;
1542 out->vdisplay = in->vdisplay;
1543 out->vsync_start = in->vsync_start;
1544 out->vsync_end = in->vsync_end;
1545 out->vtotal = in->vtotal;
1546 out->vscan = in->vscan;
1547 out->vrefresh = in->vrefresh;
1548 out->flags = in->flags;
1549 out->type = in->type;
1550 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1551 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001552
1553 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001554}
1555
1556/**
1557 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001558 * @dev: drm device for the ioctl
1559 * @data: data pointer for the ioctl
1560 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001561 *
Dave Airlief453ba02008-11-07 14:05:41 -08001562 * Construct a set of configuration description structures and return
1563 * them to the user, including CRTC, connector and framebuffer configuration.
1564 *
1565 * Called by the user via ioctl.
1566 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001567 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001568 * Zero on success, errno on failure.
1569 */
1570int drm_mode_getresources(struct drm_device *dev, void *data,
1571 struct drm_file *file_priv)
1572{
1573 struct drm_mode_card_res *card_res = data;
1574 struct list_head *lh;
1575 struct drm_framebuffer *fb;
1576 struct drm_connector *connector;
1577 struct drm_crtc *crtc;
1578 struct drm_encoder *encoder;
1579 int ret = 0;
1580 int connector_count = 0;
1581 int crtc_count = 0;
1582 int fb_count = 0;
1583 int encoder_count = 0;
1584 int copied = 0, i;
1585 uint32_t __user *fb_id;
1586 uint32_t __user *crtc_id;
1587 uint32_t __user *connector_id;
1588 uint32_t __user *encoder_id;
1589 struct drm_mode_group *mode_group;
1590
Dave Airliefb3b06c2011-02-08 13:55:21 +10001591 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1592 return -EINVAL;
1593
Dave Airlief453ba02008-11-07 14:05:41 -08001594
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001595 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001596 /*
1597 * For the non-control nodes we need to limit the list of resources
1598 * by IDs in the group list for this node
1599 */
1600 list_for_each(lh, &file_priv->fbs)
1601 fb_count++;
1602
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001603 /* handle this in 4 parts */
1604 /* FBs */
1605 if (card_res->count_fbs >= fb_count) {
1606 copied = 0;
1607 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1608 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1609 if (put_user(fb->base.id, fb_id + copied)) {
1610 mutex_unlock(&file_priv->fbs_lock);
1611 return -EFAULT;
1612 }
1613 copied++;
1614 }
1615 }
1616 card_res->count_fbs = fb_count;
1617 mutex_unlock(&file_priv->fbs_lock);
1618
1619 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001620 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001621
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001622 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001623 list_for_each(lh, &dev->mode_config.crtc_list)
1624 crtc_count++;
1625
1626 list_for_each(lh, &dev->mode_config.connector_list)
1627 connector_count++;
1628
1629 list_for_each(lh, &dev->mode_config.encoder_list)
1630 encoder_count++;
1631 } else {
1632
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001633 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001634 crtc_count = mode_group->num_crtcs;
1635 connector_count = mode_group->num_connectors;
1636 encoder_count = mode_group->num_encoders;
1637 }
1638
1639 card_res->max_height = dev->mode_config.max_height;
1640 card_res->min_height = dev->mode_config.min_height;
1641 card_res->max_width = dev->mode_config.max_width;
1642 card_res->min_width = dev->mode_config.min_width;
1643
Dave Airlief453ba02008-11-07 14:05:41 -08001644 /* CRTCs */
1645 if (card_res->count_crtcs >= crtc_count) {
1646 copied = 0;
1647 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001648 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001649 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1650 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001651 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001652 if (put_user(crtc->base.id, crtc_id + copied)) {
1653 ret = -EFAULT;
1654 goto out;
1655 }
1656 copied++;
1657 }
1658 } else {
1659 for (i = 0; i < mode_group->num_crtcs; i++) {
1660 if (put_user(mode_group->id_list[i],
1661 crtc_id + copied)) {
1662 ret = -EFAULT;
1663 goto out;
1664 }
1665 copied++;
1666 }
1667 }
1668 }
1669 card_res->count_crtcs = crtc_count;
1670
1671 /* Encoders */
1672 if (card_res->count_encoders >= encoder_count) {
1673 copied = 0;
1674 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001675 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001676 list_for_each_entry(encoder,
1677 &dev->mode_config.encoder_list,
1678 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001679 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001680 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001681 if (put_user(encoder->base.id, encoder_id +
1682 copied)) {
1683 ret = -EFAULT;
1684 goto out;
1685 }
1686 copied++;
1687 }
1688 } else {
1689 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1690 if (put_user(mode_group->id_list[i],
1691 encoder_id + copied)) {
1692 ret = -EFAULT;
1693 goto out;
1694 }
1695 copied++;
1696 }
1697
1698 }
1699 }
1700 card_res->count_encoders = encoder_count;
1701
1702 /* Connectors */
1703 if (card_res->count_connectors >= connector_count) {
1704 copied = 0;
1705 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001706 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001707 list_for_each_entry(connector,
1708 &dev->mode_config.connector_list,
1709 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001710 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1711 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001712 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001713 if (put_user(connector->base.id,
1714 connector_id + copied)) {
1715 ret = -EFAULT;
1716 goto out;
1717 }
1718 copied++;
1719 }
1720 } else {
1721 int start = mode_group->num_crtcs +
1722 mode_group->num_encoders;
1723 for (i = start; i < start + mode_group->num_connectors; i++) {
1724 if (put_user(mode_group->id_list[i],
1725 connector_id + copied)) {
1726 ret = -EFAULT;
1727 goto out;
1728 }
1729 copied++;
1730 }
1731 }
1732 }
1733 card_res->count_connectors = connector_count;
1734
Jerome Glisse94401062010-07-15 15:43:25 -04001735 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001736 card_res->count_connectors, card_res->count_encoders);
1737
1738out:
Daniel Vetter84849902012-12-02 00:28:11 +01001739 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001740 return ret;
1741}
1742
1743/**
1744 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001745 * @dev: drm device for the ioctl
1746 * @data: data pointer for the ioctl
1747 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001748 *
Dave Airlief453ba02008-11-07 14:05:41 -08001749 * Construct a CRTC configuration structure to return to the user.
1750 *
1751 * Called by the user via ioctl.
1752 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001753 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001754 * Zero on success, errno on failure.
1755 */
1756int drm_mode_getcrtc(struct drm_device *dev,
1757 void *data, struct drm_file *file_priv)
1758{
1759 struct drm_mode_crtc *crtc_resp = data;
1760 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001761 int ret = 0;
1762
Dave Airliefb3b06c2011-02-08 13:55:21 +10001763 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1764 return -EINVAL;
1765
Daniel Vetter84849902012-12-02 00:28:11 +01001766 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001767
Rob Clarka2b34e22013-10-05 16:36:52 -04001768 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1769 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001770 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001771 goto out;
1772 }
Dave Airlief453ba02008-11-07 14:05:41 -08001773
1774 crtc_resp->x = crtc->x;
1775 crtc_resp->y = crtc->y;
1776 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001777 if (crtc->primary->fb)
1778 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001779 else
1780 crtc_resp->fb_id = 0;
1781
1782 if (crtc->enabled) {
1783
1784 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1785 crtc_resp->mode_valid = 1;
1786
1787 } else {
1788 crtc_resp->mode_valid = 0;
1789 }
1790
1791out:
Daniel Vetter84849902012-12-02 00:28:11 +01001792 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001793 return ret;
1794}
1795
Damien Lespiau61d8e322013-09-25 16:45:22 +01001796static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1797 const struct drm_file *file_priv)
1798{
1799 /*
1800 * If user-space hasn't configured the driver to expose the stereo 3D
1801 * modes, don't expose them.
1802 */
1803 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1804 return false;
1805
1806 return true;
1807}
1808
Dave Airlief453ba02008-11-07 14:05:41 -08001809/**
1810 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001811 * @dev: drm device for the ioctl
1812 * @data: data pointer for the ioctl
1813 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001814 *
Dave Airlief453ba02008-11-07 14:05:41 -08001815 * Construct a connector configuration structure to return to the user.
1816 *
1817 * Called by the user via ioctl.
1818 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001819 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001820 * Zero on success, errno on failure.
1821 */
1822int drm_mode_getconnector(struct drm_device *dev, void *data,
1823 struct drm_file *file_priv)
1824{
1825 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001826 struct drm_connector *connector;
1827 struct drm_display_mode *mode;
1828 int mode_count = 0;
1829 int props_count = 0;
1830 int encoders_count = 0;
1831 int ret = 0;
1832 int copied = 0;
1833 int i;
1834 struct drm_mode_modeinfo u_mode;
1835 struct drm_mode_modeinfo __user *mode_ptr;
1836 uint32_t __user *prop_ptr;
1837 uint64_t __user *prop_values;
1838 uint32_t __user *encoder_ptr;
1839
Dave Airliefb3b06c2011-02-08 13:55:21 +10001840 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1841 return -EINVAL;
1842
Dave Airlief453ba02008-11-07 14:05:41 -08001843 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1844
Jerome Glisse94401062010-07-15 15:43:25 -04001845 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001846
Daniel Vetter7b240562012-12-12 00:35:33 +01001847 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001848
Rob Clarka2b34e22013-10-05 16:36:52 -04001849 connector = drm_connector_find(dev, out_resp->connector_id);
1850 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001851 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001852 goto out;
1853 }
Dave Airlief453ba02008-11-07 14:05:41 -08001854
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001855 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001856
1857 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1858 if (connector->encoder_ids[i] != 0) {
1859 encoders_count++;
1860 }
1861 }
1862
1863 if (out_resp->count_modes == 0) {
1864 connector->funcs->fill_modes(connector,
1865 dev->mode_config.max_width,
1866 dev->mode_config.max_height);
1867 }
1868
1869 /* delayed so we get modes regardless of pre-fill_modes state */
1870 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001871 if (drm_mode_expose_to_userspace(mode, file_priv))
1872 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001873
1874 out_resp->connector_id = connector->base.id;
1875 out_resp->connector_type = connector->connector_type;
1876 out_resp->connector_type_id = connector->connector_type_id;
1877 out_resp->mm_width = connector->display_info.width_mm;
1878 out_resp->mm_height = connector->display_info.height_mm;
1879 out_resp->subpixel = connector->display_info.subpixel_order;
1880 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02001881 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08001882 if (connector->encoder)
1883 out_resp->encoder_id = connector->encoder->base.id;
1884 else
1885 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02001886 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001887
1888 /*
1889 * This ioctl is called twice, once to determine how much space is
1890 * needed, and the 2nd time to fill it.
1891 */
1892 if ((out_resp->count_modes >= mode_count) && mode_count) {
1893 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001894 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001895 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001896 if (!drm_mode_expose_to_userspace(mode, file_priv))
1897 continue;
1898
Dave Airlief453ba02008-11-07 14:05:41 -08001899 drm_crtc_convert_to_umode(&u_mode, mode);
1900 if (copy_to_user(mode_ptr + copied,
1901 &u_mode, sizeof(u_mode))) {
1902 ret = -EFAULT;
1903 goto out;
1904 }
1905 copied++;
1906 }
1907 }
1908 out_resp->count_modes = mode_count;
1909
1910 if ((out_resp->count_props >= props_count) && props_count) {
1911 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001912 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1913 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001914 for (i = 0; i < connector->properties.count; i++) {
1915 if (put_user(connector->properties.ids[i],
1916 prop_ptr + copied)) {
1917 ret = -EFAULT;
1918 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001919 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001920
1921 if (put_user(connector->properties.values[i],
1922 prop_values + copied)) {
1923 ret = -EFAULT;
1924 goto out;
1925 }
1926 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001927 }
1928 }
1929 out_resp->count_props = props_count;
1930
1931 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1932 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001933 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001934 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1935 if (connector->encoder_ids[i] != 0) {
1936 if (put_user(connector->encoder_ids[i],
1937 encoder_ptr + copied)) {
1938 ret = -EFAULT;
1939 goto out;
1940 }
1941 copied++;
1942 }
1943 }
1944 }
1945 out_resp->count_encoders = encoders_count;
1946
1947out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001948 mutex_unlock(&dev->mode_config.mutex);
1949
Dave Airlief453ba02008-11-07 14:05:41 -08001950 return ret;
1951}
1952
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001953/**
1954 * drm_mode_getencoder - get encoder configuration
1955 * @dev: drm device for the ioctl
1956 * @data: data pointer for the ioctl
1957 * @file_priv: drm file for the ioctl call
1958 *
1959 * Construct a encoder configuration structure to return to the user.
1960 *
1961 * Called by the user via ioctl.
1962 *
1963 * Returns:
1964 * Zero on success, errno on failure.
1965 */
Dave Airlief453ba02008-11-07 14:05:41 -08001966int drm_mode_getencoder(struct drm_device *dev, void *data,
1967 struct drm_file *file_priv)
1968{
1969 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001970 struct drm_encoder *encoder;
1971 int ret = 0;
1972
Dave Airliefb3b06c2011-02-08 13:55:21 +10001973 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1974 return -EINVAL;
1975
Daniel Vetter84849902012-12-02 00:28:11 +01001976 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04001977 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1978 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001979 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001980 goto out;
1981 }
Dave Airlief453ba02008-11-07 14:05:41 -08001982
1983 if (encoder->crtc)
1984 enc_resp->crtc_id = encoder->crtc->base.id;
1985 else
1986 enc_resp->crtc_id = 0;
1987 enc_resp->encoder_type = encoder->encoder_type;
1988 enc_resp->encoder_id = encoder->base.id;
1989 enc_resp->possible_crtcs = encoder->possible_crtcs;
1990 enc_resp->possible_clones = encoder->possible_clones;
1991
1992out:
Daniel Vetter84849902012-12-02 00:28:11 +01001993 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001994 return ret;
1995}
1996
1997/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001998 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001999 * @dev: DRM device
2000 * @data: ioctl data
2001 * @file_priv: DRM file info
2002 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002003 * Construct a list of plane ids to return to the user.
2004 *
2005 * Called by the user via ioctl.
2006 *
2007 * Returns:
2008 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002009 */
2010int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002011 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002012{
2013 struct drm_mode_get_plane_res *plane_resp = data;
2014 struct drm_mode_config *config;
2015 struct drm_plane *plane;
2016 uint32_t __user *plane_ptr;
2017 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002018 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002019
2020 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2021 return -EINVAL;
2022
Daniel Vetter84849902012-12-02 00:28:11 +01002023 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002024 config = &dev->mode_config;
2025
Matt Roper681e7ec2014-04-01 15:22:42 -07002026 if (file_priv->universal_planes)
2027 num_planes = config->num_total_plane;
2028 else
2029 num_planes = config->num_overlay_plane;
2030
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002031 /*
2032 * This ioctl is called twice, once to determine how much space is
2033 * needed, and the 2nd time to fill it.
2034 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002035 if (num_planes &&
2036 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002037 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002038
2039 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002040 /*
2041 * Unless userspace set the 'universal planes'
2042 * capability bit, only advertise overlays.
2043 */
2044 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2045 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002046 continue;
2047
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002048 if (put_user(plane->base.id, plane_ptr + copied)) {
2049 ret = -EFAULT;
2050 goto out;
2051 }
2052 copied++;
2053 }
2054 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002055 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002056
2057out:
Daniel Vetter84849902012-12-02 00:28:11 +01002058 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002059 return ret;
2060}
2061
2062/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002063 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002064 * @dev: DRM device
2065 * @data: ioctl data
2066 * @file_priv: DRM file info
2067 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002068 * Construct a plane configuration structure to return to the user.
2069 *
2070 * Called by the user via ioctl.
2071 *
2072 * Returns:
2073 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002074 */
2075int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002076 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002077{
2078 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002079 struct drm_plane *plane;
2080 uint32_t __user *format_ptr;
2081 int ret = 0;
2082
2083 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2084 return -EINVAL;
2085
Daniel Vetter84849902012-12-02 00:28:11 +01002086 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002087 plane = drm_plane_find(dev, plane_resp->plane_id);
2088 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002089 ret = -ENOENT;
2090 goto out;
2091 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002092
2093 if (plane->crtc)
2094 plane_resp->crtc_id = plane->crtc->base.id;
2095 else
2096 plane_resp->crtc_id = 0;
2097
2098 if (plane->fb)
2099 plane_resp->fb_id = plane->fb->base.id;
2100 else
2101 plane_resp->fb_id = 0;
2102
2103 plane_resp->plane_id = plane->base.id;
2104 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002105 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002106
2107 /*
2108 * This ioctl is called twice, once to determine how much space is
2109 * needed, and the 2nd time to fill it.
2110 */
2111 if (plane->format_count &&
2112 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002113 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002114 if (copy_to_user(format_ptr,
2115 plane->format_types,
2116 sizeof(uint32_t) * plane->format_count)) {
2117 ret = -EFAULT;
2118 goto out;
2119 }
2120 }
2121 plane_resp->count_format_types = plane->format_count;
2122
2123out:
Daniel Vetter84849902012-12-02 00:28:11 +01002124 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002125 return ret;
2126}
2127
Matt Roperb36552b2014-06-10 08:28:09 -07002128/*
2129 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002130 *
Matt Roperb36552b2014-06-10 08:28:09 -07002131 * Note that we assume an extra reference has already been taken on fb. If the
2132 * update fails, this reference will be dropped before return; if it succeeds,
2133 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002134 *
Matt Roperb36552b2014-06-10 08:28:09 -07002135 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002136 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002137static int setplane_internal(struct drm_plane *plane,
2138 struct drm_crtc *crtc,
Matt Roperb36552b2014-06-10 08:28:09 -07002139 struct drm_framebuffer *fb,
2140 int32_t crtc_x, int32_t crtc_y,
2141 uint32_t crtc_w, uint32_t crtc_h,
2142 /* src_{x,y,w,h} values are 16.16 fixed point */
2143 uint32_t src_x, uint32_t src_y,
2144 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002145{
Chris Wilson17cfd912014-06-13 15:22:28 +01002146 struct drm_device *dev = plane->dev;
Matt Roperb36552b2014-06-10 08:28:09 -07002147 struct drm_framebuffer *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002148 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002149 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002150 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002151
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002152 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002153 if (!fb) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002154 drm_modeset_lock_all(dev);
2155 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002156 ret = plane->funcs->disable_plane(plane);
2157 if (!ret) {
2158 plane->crtc = NULL;
2159 plane->fb = NULL;
2160 } else {
2161 old_fb = NULL;
2162 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002163 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002164 goto out;
2165 }
2166
Matt Roper7f994f32014-05-29 08:06:51 -07002167 /* Check whether this plane is usable on this CRTC */
2168 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2169 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2170 ret = -EINVAL;
2171 goto out;
2172 }
2173
Ville Syrjälä62443be2011-12-20 00:06:47 +02002174 /* Check whether this plane supports the fb pixel format. */
2175 for (i = 0; i < plane->format_count; i++)
2176 if (fb->pixel_format == plane->format_types[i])
2177 break;
2178 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002179 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2180 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002181 ret = -EINVAL;
2182 goto out;
2183 }
2184
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002185 fb_width = fb->width << 16;
2186 fb_height = fb->height << 16;
2187
2188 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002189 if (src_w > fb_width ||
2190 src_x > fb_width - src_w ||
2191 src_h > fb_height ||
2192 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002193 DRM_DEBUG_KMS("Invalid source coordinates "
2194 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002195 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2196 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2197 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2198 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002199 ret = -ENOSPC;
2200 goto out;
2201 }
2202
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002203 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002204 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002205 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002206 crtc_x, crtc_y, crtc_w, crtc_h,
2207 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002208 if (!ret) {
2209 plane->crtc = crtc;
2210 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002211 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002212 } else {
2213 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002214 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002215 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002216
2217out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002218 if (fb)
2219 drm_framebuffer_unreference(fb);
2220 if (old_fb)
2221 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002222
2223 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002224
2225}
2226
2227/**
2228 * drm_mode_setplane - configure a plane's configuration
2229 * @dev: DRM device
2230 * @data: ioctl data*
2231 * @file_priv: DRM file info
2232 *
2233 * Set plane configuration, including placement, fb, scaling, and other factors.
2234 * Or pass a NULL fb to disable (planes may be disabled without providing a
2235 * valid crtc).
2236 *
2237 * Returns:
2238 * Zero on success, errno on failure.
2239 */
2240int drm_mode_setplane(struct drm_device *dev, void *data,
2241 struct drm_file *file_priv)
2242{
2243 struct drm_mode_set_plane *plane_req = data;
2244 struct drm_mode_object *obj;
2245 struct drm_plane *plane;
2246 struct drm_crtc *crtc = NULL;
2247 struct drm_framebuffer *fb = NULL;
2248
2249 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2250 return -EINVAL;
2251
2252 /* Give drivers some help against integer overflows */
2253 if (plane_req->crtc_w > INT_MAX ||
2254 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2255 plane_req->crtc_h > INT_MAX ||
2256 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2257 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2258 plane_req->crtc_w, plane_req->crtc_h,
2259 plane_req->crtc_x, plane_req->crtc_y);
2260 return -ERANGE;
2261 }
2262
2263 /*
2264 * First, find the plane, crtc, and fb objects. If not available,
2265 * we don't bother to call the driver.
2266 */
2267 obj = drm_mode_object_find(dev, plane_req->plane_id,
2268 DRM_MODE_OBJECT_PLANE);
2269 if (!obj) {
2270 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2271 plane_req->plane_id);
2272 return -ENOENT;
2273 }
2274 plane = obj_to_plane(obj);
2275
2276 if (plane_req->fb_id) {
2277 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2278 if (!fb) {
2279 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2280 plane_req->fb_id);
2281 return -ENOENT;
2282 }
2283
2284 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2285 DRM_MODE_OBJECT_CRTC);
2286 if (!obj) {
2287 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2288 plane_req->crtc_id);
2289 return -ENOENT;
2290 }
2291 crtc = obj_to_crtc(obj);
2292 }
2293
Matt Roper161d0dc2014-06-10 08:28:10 -07002294 /*
2295 * setplane_internal will take care of deref'ing either the old or new
2296 * framebuffer depending on success.
2297 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002298 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002299 plane_req->crtc_x, plane_req->crtc_y,
2300 plane_req->crtc_w, plane_req->crtc_h,
2301 plane_req->src_x, plane_req->src_y,
2302 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002303}
2304
2305/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002306 * drm_mode_set_config_internal - helper to call ->set_config
2307 * @set: modeset config to set
2308 *
2309 * This is a little helper to wrap internal calls to the ->set_config driver
2310 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002311 *
2312 * Returns:
2313 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002314 */
2315int drm_mode_set_config_internal(struct drm_mode_set *set)
2316{
2317 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002318 struct drm_framebuffer *fb;
2319 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002320 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002321
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002322 /*
2323 * NOTE: ->set_config can also disable other crtcs (if we steal all
2324 * connectors from it), hence we need to refcount the fbs across all
2325 * crtcs. Atomic modeset will have saner semantics ...
2326 */
2327 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002328 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002329
Daniel Vetterb0d12322012-12-11 01:07:12 +01002330 fb = set->fb;
2331
2332 ret = crtc->funcs->set_config(set);
2333 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002334 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002335 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002336 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002337
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002338 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002339 if (tmp->primary->fb)
2340 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002341 if (tmp->old_fb)
2342 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002343 }
2344
2345 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002346}
2347EXPORT_SYMBOL(drm_mode_set_config_internal);
2348
Matt Roperaf936292014-04-01 15:22:34 -07002349/**
2350 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2351 * CRTC viewport
2352 * @crtc: CRTC that framebuffer will be displayed on
2353 * @x: x panning
2354 * @y: y panning
2355 * @mode: mode that framebuffer will be displayed under
2356 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002357 */
Matt Roperaf936292014-04-01 15:22:34 -07002358int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2359 int x, int y,
2360 const struct drm_display_mode *mode,
2361 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002362
2363{
2364 int hdisplay, vdisplay;
2365
2366 hdisplay = mode->hdisplay;
2367 vdisplay = mode->vdisplay;
2368
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002369 if (drm_mode_is_stereo(mode)) {
2370 struct drm_display_mode adjusted = *mode;
2371
2372 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2373 hdisplay = adjusted.crtc_hdisplay;
2374 vdisplay = adjusted.crtc_vdisplay;
2375 }
2376
Damien Lespiauc11e9282013-09-25 16:45:30 +01002377 if (crtc->invert_dimensions)
2378 swap(hdisplay, vdisplay);
2379
2380 if (hdisplay > fb->width ||
2381 vdisplay > fb->height ||
2382 x > fb->width - hdisplay ||
2383 y > fb->height - vdisplay) {
2384 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2385 fb->width, fb->height, hdisplay, vdisplay, x, y,
2386 crtc->invert_dimensions ? " (inverted)" : "");
2387 return -ENOSPC;
2388 }
2389
2390 return 0;
2391}
Matt Roperaf936292014-04-01 15:22:34 -07002392EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002393
Daniel Vetter2d13b672012-12-11 13:47:23 +01002394/**
Dave Airlief453ba02008-11-07 14:05:41 -08002395 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002396 * @dev: drm device for the ioctl
2397 * @data: data pointer for the ioctl
2398 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002399 *
Dave Airlief453ba02008-11-07 14:05:41 -08002400 * Build a new CRTC configuration based on user request.
2401 *
2402 * Called by the user via ioctl.
2403 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002404 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002405 * Zero on success, errno on failure.
2406 */
2407int drm_mode_setcrtc(struct drm_device *dev, void *data,
2408 struct drm_file *file_priv)
2409{
2410 struct drm_mode_config *config = &dev->mode_config;
2411 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002412 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002413 struct drm_connector **connector_set = NULL, *connector;
2414 struct drm_framebuffer *fb = NULL;
2415 struct drm_display_mode *mode = NULL;
2416 struct drm_mode_set set;
2417 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002418 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002419 int i;
2420
Dave Airliefb3b06c2011-02-08 13:55:21 +10002421 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2422 return -EINVAL;
2423
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002424 /* For some reason crtc x/y offsets are signed internally. */
2425 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2426 return -ERANGE;
2427
Daniel Vetter84849902012-12-02 00:28:11 +01002428 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002429 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2430 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002431 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002432 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002433 goto out;
2434 }
Jerome Glisse94401062010-07-15 15:43:25 -04002435 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002436
2437 if (crtc_req->mode_valid) {
2438 /* If we have a mode we need a framebuffer. */
2439 /* If we pass -1, set the mode with the currently bound fb */
2440 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002441 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002442 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2443 ret = -EINVAL;
2444 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002445 }
Matt Roperf4510a22014-04-01 15:22:40 -07002446 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002447 /* Make refcounting symmetric with the lookup path. */
2448 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002449 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002450 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2451 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002452 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2453 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002454 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002455 goto out;
2456 }
Dave Airlief453ba02008-11-07 14:05:41 -08002457 }
2458
2459 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002460 if (!mode) {
2461 ret = -ENOMEM;
2462 goto out;
2463 }
2464
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002465 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2466 if (ret) {
2467 DRM_DEBUG_KMS("Invalid mode\n");
2468 goto out;
2469 }
2470
Dave Airlief453ba02008-11-07 14:05:41 -08002471 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002472
Damien Lespiauc11e9282013-09-25 16:45:30 +01002473 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2474 mode, fb);
2475 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002476 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002477
Dave Airlief453ba02008-11-07 14:05:41 -08002478 }
2479
2480 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002481 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002482 ret = -EINVAL;
2483 goto out;
2484 }
2485
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002486 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002487 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002488 crtc_req->count_connectors);
2489 ret = -EINVAL;
2490 goto out;
2491 }
2492
2493 if (crtc_req->count_connectors > 0) {
2494 u32 out_id;
2495
2496 /* Avoid unbounded kernel memory allocation */
2497 if (crtc_req->count_connectors > config->num_connector) {
2498 ret = -EINVAL;
2499 goto out;
2500 }
2501
2502 connector_set = kmalloc(crtc_req->count_connectors *
2503 sizeof(struct drm_connector *),
2504 GFP_KERNEL);
2505 if (!connector_set) {
2506 ret = -ENOMEM;
2507 goto out;
2508 }
2509
2510 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002511 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002512 if (get_user(out_id, &set_connectors_ptr[i])) {
2513 ret = -EFAULT;
2514 goto out;
2515 }
2516
Rob Clarka2b34e22013-10-05 16:36:52 -04002517 connector = drm_connector_find(dev, out_id);
2518 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002519 DRM_DEBUG_KMS("Connector id %d unknown\n",
2520 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002521 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002522 goto out;
2523 }
Jerome Glisse94401062010-07-15 15:43:25 -04002524 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2525 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002526 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002527
2528 connector_set[i] = connector;
2529 }
2530 }
2531
2532 set.crtc = crtc;
2533 set.x = crtc_req->x;
2534 set.y = crtc_req->y;
2535 set.mode = mode;
2536 set.connectors = connector_set;
2537 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002538 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002539 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002540
2541out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002542 if (fb)
2543 drm_framebuffer_unreference(fb);
2544
Dave Airlief453ba02008-11-07 14:05:41 -08002545 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002546 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002547 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002548 return ret;
2549}
2550
Matt Roper161d0dc2014-06-10 08:28:10 -07002551/**
2552 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2553 * universal plane handler call
2554 * @crtc: crtc to update cursor for
2555 * @req: data pointer for the ioctl
2556 * @file_priv: drm file for the ioctl call
2557 *
2558 * Legacy cursor ioctl's work directly with driver buffer handles. To
2559 * translate legacy ioctl calls into universal plane handler calls, we need to
2560 * wrap the native buffer handle in a drm_framebuffer.
2561 *
2562 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2563 * buffer with a pitch of 4*width; the universal plane interface should be used
2564 * directly in cases where the hardware can support other buffer settings and
2565 * userspace wants to make use of these capabilities.
2566 *
2567 * Returns:
2568 * Zero on success, errno on failure.
2569 */
2570static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2571 struct drm_mode_cursor2 *req,
2572 struct drm_file *file_priv)
2573{
2574 struct drm_device *dev = crtc->dev;
2575 struct drm_framebuffer *fb = NULL;
2576 struct drm_mode_fb_cmd2 fbreq = {
2577 .width = req->width,
2578 .height = req->height,
2579 .pixel_format = DRM_FORMAT_ARGB8888,
2580 .pitches = { req->width * 4 },
2581 .handles = { req->handle },
2582 };
2583 int32_t crtc_x, crtc_y;
2584 uint32_t crtc_w = 0, crtc_h = 0;
2585 uint32_t src_w = 0, src_h = 0;
2586 int ret = 0;
2587
2588 BUG_ON(!crtc->cursor);
2589
2590 /*
2591 * Obtain fb we'll be using (either new or existing) and take an extra
2592 * reference to it if fb != null. setplane will take care of dropping
2593 * the reference if the plane update fails.
2594 */
2595 if (req->flags & DRM_MODE_CURSOR_BO) {
2596 if (req->handle) {
2597 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2598 if (IS_ERR(fb)) {
2599 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2600 return PTR_ERR(fb);
2601 }
2602
2603 drm_framebuffer_reference(fb);
2604 } else {
2605 fb = NULL;
2606 }
2607 } else {
2608 mutex_lock(&dev->mode_config.mutex);
2609 fb = crtc->cursor->fb;
2610 if (fb)
2611 drm_framebuffer_reference(fb);
2612 mutex_unlock(&dev->mode_config.mutex);
2613 }
2614
2615 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2616 crtc_x = req->x;
2617 crtc_y = req->y;
2618 } else {
2619 crtc_x = crtc->cursor_x;
2620 crtc_y = crtc->cursor_y;
2621 }
2622
2623 if (fb) {
2624 crtc_w = fb->width;
2625 crtc_h = fb->height;
2626 src_w = fb->width << 16;
2627 src_h = fb->height << 16;
2628 }
2629
2630 /*
2631 * setplane_internal will take care of deref'ing either the old or new
2632 * framebuffer depending on success.
2633 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002634 ret = setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002635 crtc_x, crtc_y, crtc_w, crtc_h,
2636 0, 0, src_w, src_h);
2637
2638 /* Update successful; save new cursor position, if necessary */
2639 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2640 crtc->cursor_x = req->x;
2641 crtc->cursor_y = req->y;
2642 }
2643
2644 return ret;
2645}
2646
Dave Airlie4c813d42013-06-20 11:48:52 +10002647static int drm_mode_cursor_common(struct drm_device *dev,
2648 struct drm_mode_cursor2 *req,
2649 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002650{
Dave Airlief453ba02008-11-07 14:05:41 -08002651 struct drm_crtc *crtc;
2652 int ret = 0;
2653
Dave Airliefb3b06c2011-02-08 13:55:21 +10002654 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2655 return -EINVAL;
2656
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002657 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002658 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002659
Rob Clarka2b34e22013-10-05 16:36:52 -04002660 crtc = drm_crtc_find(dev, req->crtc_id);
2661 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002662 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002663 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002664 }
Dave Airlief453ba02008-11-07 14:05:41 -08002665
Matt Roper161d0dc2014-06-10 08:28:10 -07002666 /*
2667 * If this crtc has a universal cursor plane, call that plane's update
2668 * handler rather than using legacy cursor handlers.
2669 */
2670 if (crtc->cursor)
2671 return drm_mode_cursor_universal(crtc, req, file_priv);
2672
Rob Clark51fd3712013-11-19 12:10:12 -05002673 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002674 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002675 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002676 ret = -ENXIO;
2677 goto out;
2678 }
2679 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002680 if (crtc->funcs->cursor_set2)
2681 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2682 req->width, req->height, req->hot_x, req->hot_y);
2683 else
2684 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2685 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002686 }
2687
2688 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2689 if (crtc->funcs->cursor_move) {
2690 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2691 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002692 ret = -EFAULT;
2693 goto out;
2694 }
2695 }
2696out:
Rob Clark51fd3712013-11-19 12:10:12 -05002697 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002698
Dave Airlief453ba02008-11-07 14:05:41 -08002699 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002700
2701}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002702
2703
2704/**
2705 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2706 * @dev: drm device for the ioctl
2707 * @data: data pointer for the ioctl
2708 * @file_priv: drm file for the ioctl call
2709 *
2710 * Set the cursor configuration based on user request.
2711 *
2712 * Called by the user via ioctl.
2713 *
2714 * Returns:
2715 * Zero on success, errno on failure.
2716 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002717int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002718 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002719{
2720 struct drm_mode_cursor *req = data;
2721 struct drm_mode_cursor2 new_req;
2722
2723 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2724 new_req.hot_x = new_req.hot_y = 0;
2725
2726 return drm_mode_cursor_common(dev, &new_req, file_priv);
2727}
2728
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002729/**
2730 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2731 * @dev: drm device for the ioctl
2732 * @data: data pointer for the ioctl
2733 * @file_priv: drm file for the ioctl call
2734 *
2735 * Set the cursor configuration based on user request. This implements the 2nd
2736 * version of the cursor ioctl, which allows userspace to additionally specify
2737 * the hotspot of the pointer.
2738 *
2739 * Called by the user via ioctl.
2740 *
2741 * Returns:
2742 * Zero on success, errno on failure.
2743 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002744int drm_mode_cursor2_ioctl(struct drm_device *dev,
2745 void *data, struct drm_file *file_priv)
2746{
2747 struct drm_mode_cursor2 *req = data;
2748 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002749}
2750
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002751/**
2752 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2753 * @bpp: bits per pixels
2754 * @depth: bit depth per pixel
2755 *
2756 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2757 * Useful in fbdev emulation code, since that deals in those values.
2758 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002759uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2760{
2761 uint32_t fmt;
2762
2763 switch (bpp) {
2764 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002765 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002766 break;
2767 case 16:
2768 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002769 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002770 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002771 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002772 break;
2773 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002774 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002775 break;
2776 case 32:
2777 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002778 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002779 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002780 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002781 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002782 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002783 break;
2784 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002785 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2786 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002787 break;
2788 }
2789
2790 return fmt;
2791}
2792EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2793
Dave Airlief453ba02008-11-07 14:05:41 -08002794/**
2795 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002796 * @dev: drm device for the ioctl
2797 * @data: data pointer for the ioctl
2798 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002799 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002800 * Add a new FB to the specified CRTC, given a user request. This is the
2801 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002802 *
2803 * Called by the user via ioctl.
2804 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002805 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002806 * Zero on success, errno on failure.
2807 */
2808int drm_mode_addfb(struct drm_device *dev,
2809 void *data, struct drm_file *file_priv)
2810{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002811 struct drm_mode_fb_cmd *or = data;
2812 struct drm_mode_fb_cmd2 r = {};
2813 struct drm_mode_config *config = &dev->mode_config;
2814 struct drm_framebuffer *fb;
2815 int ret = 0;
2816
2817 /* Use new struct with format internally */
2818 r.fb_id = or->fb_id;
2819 r.width = or->width;
2820 r.height = or->height;
2821 r.pitches[0] = or->pitch;
2822 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2823 r.handles[0] = or->handle;
2824
2825 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2826 return -EINVAL;
2827
Jesse Barnesacb4b992011-11-07 12:03:23 -08002828 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002829 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002830
2831 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002832 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002833
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002834 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2835 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002836 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002837 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002838 }
2839
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002840 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002841 or->fb_id = fb->base.id;
2842 list_add(&fb->filp_head, &file_priv->fbs);
2843 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002844 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002845
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002846 return ret;
2847}
2848
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002849static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002850{
2851 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2852
2853 switch (format) {
2854 case DRM_FORMAT_C8:
2855 case DRM_FORMAT_RGB332:
2856 case DRM_FORMAT_BGR233:
2857 case DRM_FORMAT_XRGB4444:
2858 case DRM_FORMAT_XBGR4444:
2859 case DRM_FORMAT_RGBX4444:
2860 case DRM_FORMAT_BGRX4444:
2861 case DRM_FORMAT_ARGB4444:
2862 case DRM_FORMAT_ABGR4444:
2863 case DRM_FORMAT_RGBA4444:
2864 case DRM_FORMAT_BGRA4444:
2865 case DRM_FORMAT_XRGB1555:
2866 case DRM_FORMAT_XBGR1555:
2867 case DRM_FORMAT_RGBX5551:
2868 case DRM_FORMAT_BGRX5551:
2869 case DRM_FORMAT_ARGB1555:
2870 case DRM_FORMAT_ABGR1555:
2871 case DRM_FORMAT_RGBA5551:
2872 case DRM_FORMAT_BGRA5551:
2873 case DRM_FORMAT_RGB565:
2874 case DRM_FORMAT_BGR565:
2875 case DRM_FORMAT_RGB888:
2876 case DRM_FORMAT_BGR888:
2877 case DRM_FORMAT_XRGB8888:
2878 case DRM_FORMAT_XBGR8888:
2879 case DRM_FORMAT_RGBX8888:
2880 case DRM_FORMAT_BGRX8888:
2881 case DRM_FORMAT_ARGB8888:
2882 case DRM_FORMAT_ABGR8888:
2883 case DRM_FORMAT_RGBA8888:
2884 case DRM_FORMAT_BGRA8888:
2885 case DRM_FORMAT_XRGB2101010:
2886 case DRM_FORMAT_XBGR2101010:
2887 case DRM_FORMAT_RGBX1010102:
2888 case DRM_FORMAT_BGRX1010102:
2889 case DRM_FORMAT_ARGB2101010:
2890 case DRM_FORMAT_ABGR2101010:
2891 case DRM_FORMAT_RGBA1010102:
2892 case DRM_FORMAT_BGRA1010102:
2893 case DRM_FORMAT_YUYV:
2894 case DRM_FORMAT_YVYU:
2895 case DRM_FORMAT_UYVY:
2896 case DRM_FORMAT_VYUY:
2897 case DRM_FORMAT_AYUV:
2898 case DRM_FORMAT_NV12:
2899 case DRM_FORMAT_NV21:
2900 case DRM_FORMAT_NV16:
2901 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002902 case DRM_FORMAT_NV24:
2903 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002904 case DRM_FORMAT_YUV410:
2905 case DRM_FORMAT_YVU410:
2906 case DRM_FORMAT_YUV411:
2907 case DRM_FORMAT_YVU411:
2908 case DRM_FORMAT_YUV420:
2909 case DRM_FORMAT_YVU420:
2910 case DRM_FORMAT_YUV422:
2911 case DRM_FORMAT_YVU422:
2912 case DRM_FORMAT_YUV444:
2913 case DRM_FORMAT_YVU444:
2914 return 0;
2915 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002916 DRM_DEBUG_KMS("invalid pixel format %s\n",
2917 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002918 return -EINVAL;
2919 }
2920}
2921
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002922static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002923{
2924 int ret, hsub, vsub, num_planes, i;
2925
2926 ret = format_check(r);
2927 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002928 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2929 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002930 return ret;
2931 }
2932
2933 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2934 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2935 num_planes = drm_format_num_planes(r->pixel_format);
2936
2937 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002938 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002939 return -EINVAL;
2940 }
2941
2942 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002943 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002944 return -EINVAL;
2945 }
2946
2947 for (i = 0; i < num_planes; i++) {
2948 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002949 unsigned int height = r->height / (i != 0 ? vsub : 1);
2950 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002951
2952 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002953 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002954 return -EINVAL;
2955 }
2956
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002957 if ((uint64_t) width * cpp > UINT_MAX)
2958 return -ERANGE;
2959
2960 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2961 return -ERANGE;
2962
2963 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002964 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002965 return -EINVAL;
2966 }
2967 }
2968
2969 return 0;
2970}
2971
Matt Roperc394c2b2014-06-10 08:28:08 -07002972static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
2973 struct drm_mode_fb_cmd2 *r,
2974 struct drm_file *file_priv)
2975{
2976 struct drm_mode_config *config = &dev->mode_config;
2977 struct drm_framebuffer *fb;
2978 int ret;
2979
2980 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2981 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2982 return ERR_PTR(-EINVAL);
2983 }
2984
2985 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2986 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2987 r->width, config->min_width, config->max_width);
2988 return ERR_PTR(-EINVAL);
2989 }
2990 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2991 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2992 r->height, config->min_height, config->max_height);
2993 return ERR_PTR(-EINVAL);
2994 }
2995
2996 ret = framebuffer_check(r);
2997 if (ret)
2998 return ERR_PTR(ret);
2999
3000 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3001 if (IS_ERR(fb)) {
3002 DRM_DEBUG_KMS("could not create framebuffer\n");
3003 return fb;
3004 }
3005
3006 mutex_lock(&file_priv->fbs_lock);
3007 r->fb_id = fb->base.id;
3008 list_add(&fb->filp_head, &file_priv->fbs);
3009 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3010 mutex_unlock(&file_priv->fbs_lock);
3011
3012 return fb;
3013}
3014
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003015/**
3016 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003017 * @dev: drm device for the ioctl
3018 * @data: data pointer for the ioctl
3019 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003020 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003021 * Add a new FB to the specified CRTC, given a user request with format. This is
3022 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3023 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003024 *
3025 * Called by the user via ioctl.
3026 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003027 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003028 * Zero on success, errno on failure.
3029 */
3030int drm_mode_addfb2(struct drm_device *dev,
3031 void *data, struct drm_file *file_priv)
3032{
Dave Airlief453ba02008-11-07 14:05:41 -08003033 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003034
Dave Airliefb3b06c2011-02-08 13:55:21 +10003035 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3036 return -EINVAL;
3037
Matt Roperc394c2b2014-06-10 08:28:08 -07003038 fb = add_framebuffer_internal(dev, data, file_priv);
3039 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003040 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003041
Matt Roperc394c2b2014-06-10 08:28:08 -07003042 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003043}
3044
3045/**
3046 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003047 * @dev: drm device for the ioctl
3048 * @data: data pointer for the ioctl
3049 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003050 *
Dave Airlief453ba02008-11-07 14:05:41 -08003051 * Remove the FB specified by the user.
3052 *
3053 * Called by the user via ioctl.
3054 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003055 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003056 * Zero on success, errno on failure.
3057 */
3058int drm_mode_rmfb(struct drm_device *dev,
3059 void *data, struct drm_file *file_priv)
3060{
Dave Airlief453ba02008-11-07 14:05:41 -08003061 struct drm_framebuffer *fb = NULL;
3062 struct drm_framebuffer *fbl = NULL;
3063 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003064 int found = 0;
3065
Dave Airliefb3b06c2011-02-08 13:55:21 +10003066 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3067 return -EINVAL;
3068
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003069 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003070 mutex_lock(&dev->mode_config.fb_lock);
3071 fb = __drm_framebuffer_lookup(dev, *id);
3072 if (!fb)
3073 goto fail_lookup;
3074
Dave Airlief453ba02008-11-07 14:05:41 -08003075 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3076 if (fb == fbl)
3077 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003078 if (!found)
3079 goto fail_lookup;
3080
3081 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3082 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003083
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003084 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003085 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003086 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003087
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003088 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003089
Daniel Vetter2b677e82012-12-10 21:16:05 +01003090 return 0;
3091
3092fail_lookup:
3093 mutex_unlock(&dev->mode_config.fb_lock);
3094 mutex_unlock(&file_priv->fbs_lock);
3095
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003096 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003097}
3098
3099/**
3100 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003101 * @dev: drm device for the ioctl
3102 * @data: data pointer for the ioctl
3103 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003104 *
Dave Airlief453ba02008-11-07 14:05:41 -08003105 * Lookup the FB given its ID and return info about it.
3106 *
3107 * Called by the user via ioctl.
3108 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003109 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003110 * Zero on success, errno on failure.
3111 */
3112int drm_mode_getfb(struct drm_device *dev,
3113 void *data, struct drm_file *file_priv)
3114{
3115 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003116 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003117 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003118
Dave Airliefb3b06c2011-02-08 13:55:21 +10003119 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3120 return -EINVAL;
3121
Daniel Vetter786b99e2012-12-02 21:53:40 +01003122 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003123 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003124 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003125
3126 r->height = fb->height;
3127 r->width = fb->width;
3128 r->depth = fb->depth;
3129 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003130 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003131 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003132 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003133 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003134 ret = fb->funcs->create_handle(fb, file_priv,
3135 &r->handle);
3136 } else {
3137 /* GET_FB() is an unprivileged ioctl so we must not
3138 * return a buffer-handle to non-master processes! For
3139 * backwards-compatibility reasons, we cannot make
3140 * GET_FB() privileged, so just return an invalid handle
3141 * for non-masters. */
3142 r->handle = 0;
3143 ret = 0;
3144 }
3145 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003146 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003147 }
Dave Airlief453ba02008-11-07 14:05:41 -08003148
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003149 drm_framebuffer_unreference(fb);
3150
Dave Airlief453ba02008-11-07 14:05:41 -08003151 return ret;
3152}
3153
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003154/**
3155 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3156 * @dev: drm device for the ioctl
3157 * @data: data pointer for the ioctl
3158 * @file_priv: drm file for the ioctl call
3159 *
3160 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3161 * rectangle list. Generic userspace which does frontbuffer rendering must call
3162 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3163 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3164 *
3165 * Modesetting drivers which always update the frontbuffer do not need to
3166 * implement the corresponding ->dirty framebuffer callback.
3167 *
3168 * Called by the user via ioctl.
3169 *
3170 * Returns:
3171 * Zero on success, errno on failure.
3172 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003173int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3174 void *data, struct drm_file *file_priv)
3175{
3176 struct drm_clip_rect __user *clips_ptr;
3177 struct drm_clip_rect *clips = NULL;
3178 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003179 struct drm_framebuffer *fb;
3180 unsigned flags;
3181 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003182 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003183
Dave Airliefb3b06c2011-02-08 13:55:21 +10003184 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3185 return -EINVAL;
3186
Daniel Vetter786b99e2012-12-02 21:53:40 +01003187 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003188 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003189 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003190
3191 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003192 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003193
3194 if (!num_clips != !clips_ptr) {
3195 ret = -EINVAL;
3196 goto out_err1;
3197 }
3198
3199 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3200
3201 /* If userspace annotates copy, clips must come in pairs */
3202 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3203 ret = -EINVAL;
3204 goto out_err1;
3205 }
3206
3207 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003208 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3209 ret = -EINVAL;
3210 goto out_err1;
3211 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003212 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3213 if (!clips) {
3214 ret = -ENOMEM;
3215 goto out_err1;
3216 }
3217
3218 ret = copy_from_user(clips, clips_ptr,
3219 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003220 if (ret) {
3221 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003222 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003223 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003224 }
3225
3226 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003227 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3228 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003229 } else {
3230 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003231 }
3232
3233out_err2:
3234 kfree(clips);
3235out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003236 drm_framebuffer_unreference(fb);
3237
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003238 return ret;
3239}
3240
3241
Dave Airlief453ba02008-11-07 14:05:41 -08003242/**
3243 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003244 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003245 *
Dave Airlief453ba02008-11-07 14:05:41 -08003246 * Destroy all the FBs associated with @filp.
3247 *
3248 * Called by the user via ioctl.
3249 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003250 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003251 * Zero on success, errno on failure.
3252 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003253void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003254{
Dave Airlief453ba02008-11-07 14:05:41 -08003255 struct drm_device *dev = priv->minor->dev;
3256 struct drm_framebuffer *fb, *tfb;
3257
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003258 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003259 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003260
3261 mutex_lock(&dev->mode_config.fb_lock);
3262 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3263 __drm_framebuffer_unregister(dev, fb);
3264 mutex_unlock(&dev->mode_config.fb_lock);
3265
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003266 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003267
3268 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003269 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003270 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003271 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003272}
3273
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003274/**
3275 * drm_property_create - create a new property type
3276 * @dev: drm device
3277 * @flags: flags specifying the property type
3278 * @name: name of the property
3279 * @num_values: number of pre-defined values
3280 *
3281 * This creates a new generic drm property which can then be attached to a drm
3282 * object with drm_object_attach_property. The returned property object must be
3283 * freed with drm_property_destroy.
3284 *
3285 * Returns:
3286 * A pointer to the newly created property on success, NULL on failure.
3287 */
Dave Airlief453ba02008-11-07 14:05:41 -08003288struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3289 const char *name, int num_values)
3290{
3291 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003292 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003293
3294 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3295 if (!property)
3296 return NULL;
3297
Rob Clark98f75de2014-05-30 11:37:03 -04003298 property->dev = dev;
3299
Dave Airlief453ba02008-11-07 14:05:41 -08003300 if (num_values) {
3301 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3302 if (!property->values)
3303 goto fail;
3304 }
3305
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003306 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3307 if (ret)
3308 goto fail;
3309
Dave Airlief453ba02008-11-07 14:05:41 -08003310 property->flags = flags;
3311 property->num_values = num_values;
3312 INIT_LIST_HEAD(&property->enum_blob_list);
3313
Vinson Lee471dd2e2011-11-10 11:55:40 -08003314 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003315 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003316 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3317 }
Dave Airlief453ba02008-11-07 14:05:41 -08003318
3319 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003320
3321 WARN_ON(!drm_property_type_valid(property));
3322
Dave Airlief453ba02008-11-07 14:05:41 -08003323 return property;
3324fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003325 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003326 kfree(property);
3327 return NULL;
3328}
3329EXPORT_SYMBOL(drm_property_create);
3330
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003331/**
3332 * drm_property_create - create a new enumeration property type
3333 * @dev: drm device
3334 * @flags: flags specifying the property type
3335 * @name: name of the property
3336 * @props: enumeration lists with property values
3337 * @num_values: number of pre-defined values
3338 *
3339 * This creates a new generic drm property which can then be attached to a drm
3340 * object with drm_object_attach_property. The returned property object must be
3341 * freed with drm_property_destroy.
3342 *
3343 * Userspace is only allowed to set one of the predefined values for enumeration
3344 * properties.
3345 *
3346 * Returns:
3347 * A pointer to the newly created property on success, NULL on failure.
3348 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003349struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3350 const char *name,
3351 const struct drm_prop_enum_list *props,
3352 int num_values)
3353{
3354 struct drm_property *property;
3355 int i, ret;
3356
3357 flags |= DRM_MODE_PROP_ENUM;
3358
3359 property = drm_property_create(dev, flags, name, num_values);
3360 if (!property)
3361 return NULL;
3362
3363 for (i = 0; i < num_values; i++) {
3364 ret = drm_property_add_enum(property, i,
3365 props[i].type,
3366 props[i].name);
3367 if (ret) {
3368 drm_property_destroy(dev, property);
3369 return NULL;
3370 }
3371 }
3372
3373 return property;
3374}
3375EXPORT_SYMBOL(drm_property_create_enum);
3376
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003377/**
3378 * drm_property_create - create a new bitmask property type
3379 * @dev: drm device
3380 * @flags: flags specifying the property type
3381 * @name: name of the property
3382 * @props: enumeration lists with property bitflags
3383 * @num_values: number of pre-defined values
3384 *
3385 * This creates a new generic drm property which can then be attached to a drm
3386 * object with drm_object_attach_property. The returned property object must be
3387 * freed with drm_property_destroy.
3388 *
3389 * Compared to plain enumeration properties userspace is allowed to set any
3390 * or'ed together combination of the predefined property bitflag values
3391 *
3392 * Returns:
3393 * A pointer to the newly created property on success, NULL on failure.
3394 */
Rob Clark49e27542012-05-17 02:23:26 -06003395struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3396 int flags, const char *name,
3397 const struct drm_prop_enum_list *props,
3398 int num_values)
3399{
3400 struct drm_property *property;
3401 int i, ret;
3402
3403 flags |= DRM_MODE_PROP_BITMASK;
3404
3405 property = drm_property_create(dev, flags, name, num_values);
3406 if (!property)
3407 return NULL;
3408
3409 for (i = 0; i < num_values; i++) {
3410 ret = drm_property_add_enum(property, i,
3411 props[i].type,
3412 props[i].name);
3413 if (ret) {
3414 drm_property_destroy(dev, property);
3415 return NULL;
3416 }
3417 }
3418
3419 return property;
3420}
3421EXPORT_SYMBOL(drm_property_create_bitmask);
3422
Rob Clarkebc44cf2012-09-12 22:22:31 -05003423static struct drm_property *property_create_range(struct drm_device *dev,
3424 int flags, const char *name,
3425 uint64_t min, uint64_t max)
3426{
3427 struct drm_property *property;
3428
3429 property = drm_property_create(dev, flags, name, 2);
3430 if (!property)
3431 return NULL;
3432
3433 property->values[0] = min;
3434 property->values[1] = max;
3435
3436 return property;
3437}
3438
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003439/**
3440 * drm_property_create - create a new ranged property type
3441 * @dev: drm device
3442 * @flags: flags specifying the property type
3443 * @name: name of the property
3444 * @min: minimum value of the property
3445 * @max: maximum value of the property
3446 *
3447 * This creates a new generic drm property which can then be attached to a drm
3448 * object with drm_object_attach_property. The returned property object must be
3449 * freed with drm_property_destroy.
3450 *
3451 * Userspace is allowed to set any interger value in the (min, max) range
3452 * inclusive.
3453 *
3454 * Returns:
3455 * A pointer to the newly created property on success, NULL on failure.
3456 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003457struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3458 const char *name,
3459 uint64_t min, uint64_t max)
3460{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003461 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3462 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003463}
3464EXPORT_SYMBOL(drm_property_create_range);
3465
Rob Clarkebc44cf2012-09-12 22:22:31 -05003466struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3467 int flags, const char *name,
3468 int64_t min, int64_t max)
3469{
3470 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3471 name, I642U64(min), I642U64(max));
3472}
3473EXPORT_SYMBOL(drm_property_create_signed_range);
3474
Rob Clark98f75de2014-05-30 11:37:03 -04003475struct drm_property *drm_property_create_object(struct drm_device *dev,
3476 int flags, const char *name, uint32_t type)
3477{
3478 struct drm_property *property;
3479
3480 flags |= DRM_MODE_PROP_OBJECT;
3481
3482 property = drm_property_create(dev, flags, name, 1);
3483 if (!property)
3484 return NULL;
3485
3486 property->values[0] = type;
3487
3488 return property;
3489}
3490EXPORT_SYMBOL(drm_property_create_object);
3491
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003492/**
3493 * drm_property_add_enum - add a possible value to an enumeration property
3494 * @property: enumeration property to change
3495 * @index: index of the new enumeration
3496 * @value: value of the new enumeration
3497 * @name: symbolic name of the new enumeration
3498 *
3499 * This functions adds enumerations to a property.
3500 *
3501 * It's use is deprecated, drivers should use one of the more specific helpers
3502 * to directly create the property with all enumerations already attached.
3503 *
3504 * Returns:
3505 * Zero on success, error code on failure.
3506 */
Dave Airlief453ba02008-11-07 14:05:41 -08003507int drm_property_add_enum(struct drm_property *property, int index,
3508 uint64_t value, const char *name)
3509{
3510 struct drm_property_enum *prop_enum;
3511
Rob Clark5ea22f22014-05-30 11:34:01 -04003512 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3513 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003514 return -EINVAL;
3515
3516 /*
3517 * Bitmask enum properties have the additional constraint of values
3518 * from 0 to 63
3519 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003520 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3521 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003522 return -EINVAL;
3523
3524 if (!list_empty(&property->enum_blob_list)) {
3525 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3526 if (prop_enum->value == value) {
3527 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3528 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3529 return 0;
3530 }
3531 }
3532 }
3533
3534 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3535 if (!prop_enum)
3536 return -ENOMEM;
3537
3538 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3539 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3540 prop_enum->value = value;
3541
3542 property->values[index] = value;
3543 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3544 return 0;
3545}
3546EXPORT_SYMBOL(drm_property_add_enum);
3547
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003548/**
3549 * drm_property_destroy - destroy a drm property
3550 * @dev: drm device
3551 * @property: property to destry
3552 *
3553 * This function frees a property including any attached resources like
3554 * enumeration values.
3555 */
Dave Airlief453ba02008-11-07 14:05:41 -08003556void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3557{
3558 struct drm_property_enum *prop_enum, *pt;
3559
3560 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3561 list_del(&prop_enum->head);
3562 kfree(prop_enum);
3563 }
3564
3565 if (property->num_values)
3566 kfree(property->values);
3567 drm_mode_object_put(dev, &property->base);
3568 list_del(&property->head);
3569 kfree(property);
3570}
3571EXPORT_SYMBOL(drm_property_destroy);
3572
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003573/**
3574 * drm_object_attach_property - attach a property to a modeset object
3575 * @obj: drm modeset object
3576 * @property: property to attach
3577 * @init_val: initial value of the property
3578 *
3579 * This attaches the given property to the modeset object with the given initial
3580 * value. Currently this function cannot fail since the properties are stored in
3581 * a statically sized array.
3582 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003583void drm_object_attach_property(struct drm_mode_object *obj,
3584 struct drm_property *property,
3585 uint64_t init_val)
3586{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003587 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003588
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003589 if (count == DRM_OBJECT_MAX_PROPERTY) {
3590 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3591 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3592 "you see this message on the same object type.\n",
3593 obj->type);
3594 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003595 }
3596
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003597 obj->properties->ids[count] = property->base.id;
3598 obj->properties->values[count] = init_val;
3599 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003600}
3601EXPORT_SYMBOL(drm_object_attach_property);
3602
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003603/**
3604 * drm_object_property_set_value - set the value of a property
3605 * @obj: drm mode object to set property value for
3606 * @property: property to set
3607 * @val: value the property should be set to
3608 *
3609 * This functions sets a given property on a given object. This function only
3610 * changes the software state of the property, it does not call into the
3611 * driver's ->set_property callback.
3612 *
3613 * Returns:
3614 * Zero on success, error code on failure.
3615 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003616int drm_object_property_set_value(struct drm_mode_object *obj,
3617 struct drm_property *property, uint64_t val)
3618{
3619 int i;
3620
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003621 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003622 if (obj->properties->ids[i] == property->base.id) {
3623 obj->properties->values[i] = val;
3624 return 0;
3625 }
3626 }
3627
3628 return -EINVAL;
3629}
3630EXPORT_SYMBOL(drm_object_property_set_value);
3631
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003632/**
3633 * drm_object_property_get_value - retrieve the value of a property
3634 * @obj: drm mode object to get property value from
3635 * @property: property to retrieve
3636 * @val: storage for the property value
3637 *
3638 * This function retrieves the softare state of the given property for the given
3639 * property. Since there is no driver callback to retrieve the current property
3640 * value this might be out of sync with the hardware, depending upon the driver
3641 * and property.
3642 *
3643 * Returns:
3644 * Zero on success, error code on failure.
3645 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003646int drm_object_property_get_value(struct drm_mode_object *obj,
3647 struct drm_property *property, uint64_t *val)
3648{
3649 int i;
3650
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003651 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003652 if (obj->properties->ids[i] == property->base.id) {
3653 *val = obj->properties->values[i];
3654 return 0;
3655 }
3656 }
3657
3658 return -EINVAL;
3659}
3660EXPORT_SYMBOL(drm_object_property_get_value);
3661
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003662/**
3663 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3664 * @dev: DRM device
3665 * @data: ioctl data
3666 * @file_priv: DRM file info
3667 *
3668 * This function retrieves the current value for an connectors's property.
3669 *
3670 * Called by the user via ioctl.
3671 *
3672 * Returns:
3673 * Zero on success, errno on failure.
3674 */
Dave Airlief453ba02008-11-07 14:05:41 -08003675int drm_mode_getproperty_ioctl(struct drm_device *dev,
3676 void *data, struct drm_file *file_priv)
3677{
Dave Airlief453ba02008-11-07 14:05:41 -08003678 struct drm_mode_get_property *out_resp = data;
3679 struct drm_property *property;
3680 int enum_count = 0;
3681 int blob_count = 0;
3682 int value_count = 0;
3683 int ret = 0, i;
3684 int copied;
3685 struct drm_property_enum *prop_enum;
3686 struct drm_mode_property_enum __user *enum_ptr;
3687 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003688 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003689 uint64_t __user *values_ptr;
3690 uint32_t __user *blob_length_ptr;
3691
Dave Airliefb3b06c2011-02-08 13:55:21 +10003692 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3693 return -EINVAL;
3694
Daniel Vetter84849902012-12-02 00:28:11 +01003695 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003696 property = drm_property_find(dev, out_resp->prop_id);
3697 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003698 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003699 goto done;
3700 }
Dave Airlief453ba02008-11-07 14:05:41 -08003701
Rob Clark5ea22f22014-05-30 11:34:01 -04003702 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3703 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003704 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3705 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003706 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003707 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3708 blob_count++;
3709 }
3710
3711 value_count = property->num_values;
3712
3713 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3714 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3715 out_resp->flags = property->flags;
3716
3717 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003718 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003719 for (i = 0; i < value_count; i++) {
3720 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3721 ret = -EFAULT;
3722 goto done;
3723 }
3724 }
3725 }
3726 out_resp->count_values = value_count;
3727
Rob Clark5ea22f22014-05-30 11:34:01 -04003728 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3729 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003730 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3731 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003732 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003733 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3734
3735 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3736 ret = -EFAULT;
3737 goto done;
3738 }
3739
3740 if (copy_to_user(&enum_ptr[copied].name,
3741 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3742 ret = -EFAULT;
3743 goto done;
3744 }
3745 copied++;
3746 }
3747 }
3748 out_resp->count_enum_blobs = enum_count;
3749 }
3750
Rob Clark5ea22f22014-05-30 11:34:01 -04003751 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003752 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3753 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003754 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3755 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003756
3757 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3758 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3759 ret = -EFAULT;
3760 goto done;
3761 }
3762
3763 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3764 ret = -EFAULT;
3765 goto done;
3766 }
3767
3768 copied++;
3769 }
3770 }
3771 out_resp->count_enum_blobs = blob_count;
3772 }
3773done:
Daniel Vetter84849902012-12-02 00:28:11 +01003774 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003775 return ret;
3776}
3777
3778static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3779 void *data)
3780{
3781 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003782 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003783
3784 if (!length || !data)
3785 return NULL;
3786
3787 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3788 if (!blob)
3789 return NULL;
3790
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003791 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3792 if (ret) {
3793 kfree(blob);
3794 return NULL;
3795 }
3796
Dave Airlief453ba02008-11-07 14:05:41 -08003797 blob->length = length;
3798
3799 memcpy(blob->data, data, length);
3800
Dave Airlief453ba02008-11-07 14:05:41 -08003801 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3802 return blob;
3803}
3804
3805static void drm_property_destroy_blob(struct drm_device *dev,
3806 struct drm_property_blob *blob)
3807{
3808 drm_mode_object_put(dev, &blob->base);
3809 list_del(&blob->head);
3810 kfree(blob);
3811}
3812
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003813/**
3814 * drm_mode_getblob_ioctl - get the contents of a blob property value
3815 * @dev: DRM device
3816 * @data: ioctl data
3817 * @file_priv: DRM file info
3818 *
3819 * This function retrieves the contents of a blob property. The value stored in
3820 * an object's blob property is just a normal modeset object id.
3821 *
3822 * Called by the user via ioctl.
3823 *
3824 * Returns:
3825 * Zero on success, errno on failure.
3826 */
Dave Airlief453ba02008-11-07 14:05:41 -08003827int drm_mode_getblob_ioctl(struct drm_device *dev,
3828 void *data, struct drm_file *file_priv)
3829{
Dave Airlief453ba02008-11-07 14:05:41 -08003830 struct drm_mode_get_blob *out_resp = data;
3831 struct drm_property_blob *blob;
3832 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003833 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003834
Dave Airliefb3b06c2011-02-08 13:55:21 +10003835 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3836 return -EINVAL;
3837
Daniel Vetter84849902012-12-02 00:28:11 +01003838 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003839 blob = drm_property_blob_find(dev, out_resp->blob_id);
3840 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003841 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003842 goto done;
3843 }
Dave Airlief453ba02008-11-07 14:05:41 -08003844
3845 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003846 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003847 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3848 ret = -EFAULT;
3849 goto done;
3850 }
3851 }
3852 out_resp->length = blob->length;
3853
3854done:
Daniel Vetter84849902012-12-02 00:28:11 +01003855 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003856 return ret;
3857}
3858
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003859/**
3860 * drm_mode_connector_update_edid_property - update the edid property of a connector
3861 * @connector: drm connector
3862 * @edid: new value of the edid property
3863 *
3864 * This function creates a new blob modeset object and assigns its id to the
3865 * connector's edid property.
3866 *
3867 * Returns:
3868 * Zero on success, errno on failure.
3869 */
Dave Airlief453ba02008-11-07 14:05:41 -08003870int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3871 struct edid *edid)
3872{
3873 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003874 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003875
3876 if (connector->edid_blob_ptr)
3877 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3878
3879 /* Delete edid, when there is none. */
3880 if (!edid) {
3881 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003882 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003883 return ret;
3884 }
3885
Adam Jackson7466f4c2010-03-29 21:43:23 +00003886 size = EDID_LENGTH * (1 + edid->extensions);
3887 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3888 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003889 if (!connector->edid_blob_ptr)
3890 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003891
Rob Clark58495562012-10-11 20:50:56 -05003892 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003893 dev->mode_config.edid_property,
3894 connector->edid_blob_ptr->base.id);
3895
3896 return ret;
3897}
3898EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3899
Paulo Zanoni26a34812012-05-15 18:08:59 -03003900static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003901 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003902{
3903 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3904 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003905
3906 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003907 if (value < property->values[0] || value > property->values[1])
3908 return false;
3909 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003910 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3911 int64_t svalue = U642I64(value);
3912 if (svalue < U642I64(property->values[0]) ||
3913 svalue > U642I64(property->values[1]))
3914 return false;
3915 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003916 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06003917 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003918 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003919 for (i = 0; i < property->num_values; i++)
3920 valid_mask |= (1ULL << property->values[i]);
3921 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003922 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003923 /* Only the driver knows */
3924 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04003925 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3926 struct drm_mode_object *obj;
3927 /* a zero value for an object property translates to null: */
3928 if (value == 0)
3929 return true;
3930 /*
3931 * NOTE: use _object_find() directly to bypass restriction on
3932 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3933 * object this could race against object finalization, so it
3934 * simply tells us that the object *was* valid. Which is good
3935 * enough.
3936 */
3937 obj = _object_find(property->dev, value, property->values[0]);
3938 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003939 } else {
3940 int i;
3941 for (i = 0; i < property->num_values; i++)
3942 if (property->values[i] == value)
3943 return true;
3944 return false;
3945 }
3946}
3947
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003948/**
3949 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3950 * @dev: DRM device
3951 * @data: ioctl data
3952 * @file_priv: DRM file info
3953 *
3954 * This function sets the current value for a connectors's property. It also
3955 * calls into a driver's ->set_property callback to update the hardware state
3956 *
3957 * Called by the user via ioctl.
3958 *
3959 * Returns:
3960 * Zero on success, errno on failure.
3961 */
Dave Airlief453ba02008-11-07 14:05:41 -08003962int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3963 void *data, struct drm_file *file_priv)
3964{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003965 struct drm_mode_connector_set_property *conn_set_prop = data;
3966 struct drm_mode_obj_set_property obj_set_prop = {
3967 .value = conn_set_prop->value,
3968 .prop_id = conn_set_prop->prop_id,
3969 .obj_id = conn_set_prop->connector_id,
3970 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3971 };
Dave Airlief453ba02008-11-07 14:05:41 -08003972
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003973 /* It does all the locking and checking we need */
3974 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003975}
3976
Paulo Zanonic5431882012-05-15 18:09:02 -03003977static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3978 struct drm_property *property,
3979 uint64_t value)
3980{
3981 int ret = -EINVAL;
3982 struct drm_connector *connector = obj_to_connector(obj);
3983
3984 /* Do DPMS ourselves */
3985 if (property == connector->dev->mode_config.dpms_property) {
3986 if (connector->funcs->dpms)
3987 (*connector->funcs->dpms)(connector, (int)value);
3988 ret = 0;
3989 } else if (connector->funcs->set_property)
3990 ret = connector->funcs->set_property(connector, property, value);
3991
3992 /* store the property value if successful */
3993 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003994 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003995 return ret;
3996}
3997
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003998static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3999 struct drm_property *property,
4000 uint64_t value)
4001{
4002 int ret = -EINVAL;
4003 struct drm_crtc *crtc = obj_to_crtc(obj);
4004
4005 if (crtc->funcs->set_property)
4006 ret = crtc->funcs->set_property(crtc, property, value);
4007 if (!ret)
4008 drm_object_property_set_value(obj, property, value);
4009
4010 return ret;
4011}
4012
Rob Clark4d939142012-05-17 02:23:27 -06004013static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4014 struct drm_property *property,
4015 uint64_t value)
4016{
4017 int ret = -EINVAL;
4018 struct drm_plane *plane = obj_to_plane(obj);
4019
4020 if (plane->funcs->set_property)
4021 ret = plane->funcs->set_property(plane, property, value);
4022 if (!ret)
4023 drm_object_property_set_value(obj, property, value);
4024
4025 return ret;
4026}
4027
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004028/**
4029 * drm_mode_getproperty_ioctl - get the current value of a object's property
4030 * @dev: DRM device
4031 * @data: ioctl data
4032 * @file_priv: DRM file info
4033 *
4034 * This function retrieves the current value for an object's property. Compared
4035 * to the connector specific ioctl this one is extended to also work on crtc and
4036 * plane objects.
4037 *
4038 * Called by the user via ioctl.
4039 *
4040 * Returns:
4041 * Zero on success, errno on failure.
4042 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004043int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4044 struct drm_file *file_priv)
4045{
4046 struct drm_mode_obj_get_properties *arg = data;
4047 struct drm_mode_object *obj;
4048 int ret = 0;
4049 int i;
4050 int copied = 0;
4051 int props_count = 0;
4052 uint32_t __user *props_ptr;
4053 uint64_t __user *prop_values_ptr;
4054
4055 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4056 return -EINVAL;
4057
Daniel Vetter84849902012-12-02 00:28:11 +01004058 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004059
4060 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4061 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004062 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004063 goto out;
4064 }
4065 if (!obj->properties) {
4066 ret = -EINVAL;
4067 goto out;
4068 }
4069
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004070 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004071
4072 /* This ioctl is called twice, once to determine how much space is
4073 * needed, and the 2nd time to fill it. */
4074 if ((arg->count_props >= props_count) && props_count) {
4075 copied = 0;
4076 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4077 prop_values_ptr = (uint64_t __user *)(unsigned long)
4078 (arg->prop_values_ptr);
4079 for (i = 0; i < props_count; i++) {
4080 if (put_user(obj->properties->ids[i],
4081 props_ptr + copied)) {
4082 ret = -EFAULT;
4083 goto out;
4084 }
4085 if (put_user(obj->properties->values[i],
4086 prop_values_ptr + copied)) {
4087 ret = -EFAULT;
4088 goto out;
4089 }
4090 copied++;
4091 }
4092 }
4093 arg->count_props = props_count;
4094out:
Daniel Vetter84849902012-12-02 00:28:11 +01004095 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004096 return ret;
4097}
4098
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004099/**
4100 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4101 * @dev: DRM device
4102 * @data: ioctl data
4103 * @file_priv: DRM file info
4104 *
4105 * This function sets the current value for an object's property. It also calls
4106 * into a driver's ->set_property callback to update the hardware state.
4107 * Compared to the connector specific ioctl this one is extended to also work on
4108 * crtc and plane objects.
4109 *
4110 * Called by the user via ioctl.
4111 *
4112 * Returns:
4113 * Zero on success, errno on failure.
4114 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004115int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4116 struct drm_file *file_priv)
4117{
4118 struct drm_mode_obj_set_property *arg = data;
4119 struct drm_mode_object *arg_obj;
4120 struct drm_mode_object *prop_obj;
4121 struct drm_property *property;
4122 int ret = -EINVAL;
4123 int i;
4124
4125 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4126 return -EINVAL;
4127
Daniel Vetter84849902012-12-02 00:28:11 +01004128 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004129
4130 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004131 if (!arg_obj) {
4132 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004133 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004134 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004135 if (!arg_obj->properties)
4136 goto out;
4137
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004138 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004139 if (arg_obj->properties->ids[i] == arg->prop_id)
4140 break;
4141
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004142 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004143 goto out;
4144
4145 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4146 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004147 if (!prop_obj) {
4148 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004149 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004150 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004151 property = obj_to_property(prop_obj);
4152
4153 if (!drm_property_change_is_valid(property, arg->value))
4154 goto out;
4155
4156 switch (arg_obj->type) {
4157 case DRM_MODE_OBJECT_CONNECTOR:
4158 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4159 arg->value);
4160 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004161 case DRM_MODE_OBJECT_CRTC:
4162 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4163 break;
Rob Clark4d939142012-05-17 02:23:27 -06004164 case DRM_MODE_OBJECT_PLANE:
4165 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4166 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004167 }
4168
4169out:
Daniel Vetter84849902012-12-02 00:28:11 +01004170 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004171 return ret;
4172}
4173
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004174/**
4175 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4176 * @connector: connector to attach
4177 * @encoder: encoder to attach @connector to
4178 *
4179 * This function links up a connector to an encoder. Note that the routing
4180 * restrictions between encoders and crtcs are exposed to userspace through the
4181 * possible_clones and possible_crtcs bitmasks.
4182 *
4183 * Returns:
4184 * Zero on success, errno on failure.
4185 */
Dave Airlief453ba02008-11-07 14:05:41 -08004186int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4187 struct drm_encoder *encoder)
4188{
4189 int i;
4190
4191 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4192 if (connector->encoder_ids[i] == 0) {
4193 connector->encoder_ids[i] = encoder->base.id;
4194 return 0;
4195 }
4196 }
4197 return -ENOMEM;
4198}
4199EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4200
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004201/**
4202 * drm_mode_crtc_set_gamma_size - set the gamma table size
4203 * @crtc: CRTC to set the gamma table size for
4204 * @gamma_size: size of the gamma table
4205 *
4206 * Drivers which support gamma tables should set this to the supported gamma
4207 * table size when initializing the CRTC. Currently the drm core only supports a
4208 * fixed gamma table size.
4209 *
4210 * Returns:
4211 * Zero on success, errno on failure.
4212 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004213int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004214 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004215{
4216 crtc->gamma_size = gamma_size;
4217
4218 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4219 if (!crtc->gamma_store) {
4220 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004221 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004222 }
4223
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004224 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004225}
4226EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4227
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004228/**
4229 * drm_mode_gamma_set_ioctl - set the gamma table
4230 * @dev: DRM device
4231 * @data: ioctl data
4232 * @file_priv: DRM file info
4233 *
4234 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4235 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4236 *
4237 * Called by the user via ioctl.
4238 *
4239 * Returns:
4240 * Zero on success, errno on failure.
4241 */
Dave Airlief453ba02008-11-07 14:05:41 -08004242int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4243 void *data, struct drm_file *file_priv)
4244{
4245 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004246 struct drm_crtc *crtc;
4247 void *r_base, *g_base, *b_base;
4248 int size;
4249 int ret = 0;
4250
Dave Airliefb3b06c2011-02-08 13:55:21 +10004251 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4252 return -EINVAL;
4253
Daniel Vetter84849902012-12-02 00:28:11 +01004254 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004255 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4256 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004257 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004258 goto out;
4259 }
Dave Airlief453ba02008-11-07 14:05:41 -08004260
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004261 if (crtc->funcs->gamma_set == NULL) {
4262 ret = -ENOSYS;
4263 goto out;
4264 }
4265
Dave Airlief453ba02008-11-07 14:05:41 -08004266 /* memcpy into gamma store */
4267 if (crtc_lut->gamma_size != crtc->gamma_size) {
4268 ret = -EINVAL;
4269 goto out;
4270 }
4271
4272 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4273 r_base = crtc->gamma_store;
4274 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4275 ret = -EFAULT;
4276 goto out;
4277 }
4278
4279 g_base = r_base + size;
4280 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4281 ret = -EFAULT;
4282 goto out;
4283 }
4284
4285 b_base = g_base + size;
4286 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4287 ret = -EFAULT;
4288 goto out;
4289 }
4290
James Simmons72034252010-08-03 01:33:19 +01004291 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004292
4293out:
Daniel Vetter84849902012-12-02 00:28:11 +01004294 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004295 return ret;
4296
4297}
4298
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004299/**
4300 * drm_mode_gamma_get_ioctl - get the gamma table
4301 * @dev: DRM device
4302 * @data: ioctl data
4303 * @file_priv: DRM file info
4304 *
4305 * Copy the current gamma table into the storage provided. This also provides
4306 * the gamma table size the driver expects, which can be used to size the
4307 * allocated storage.
4308 *
4309 * Called by the user via ioctl.
4310 *
4311 * Returns:
4312 * Zero on success, errno on failure.
4313 */
Dave Airlief453ba02008-11-07 14:05:41 -08004314int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4315 void *data, struct drm_file *file_priv)
4316{
4317 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004318 struct drm_crtc *crtc;
4319 void *r_base, *g_base, *b_base;
4320 int size;
4321 int ret = 0;
4322
Dave Airliefb3b06c2011-02-08 13:55:21 +10004323 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4324 return -EINVAL;
4325
Daniel Vetter84849902012-12-02 00:28:11 +01004326 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004327 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4328 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004329 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004330 goto out;
4331 }
Dave Airlief453ba02008-11-07 14:05:41 -08004332
4333 /* memcpy into gamma store */
4334 if (crtc_lut->gamma_size != crtc->gamma_size) {
4335 ret = -EINVAL;
4336 goto out;
4337 }
4338
4339 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4340 r_base = crtc->gamma_store;
4341 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4342 ret = -EFAULT;
4343 goto out;
4344 }
4345
4346 g_base = r_base + size;
4347 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4348 ret = -EFAULT;
4349 goto out;
4350 }
4351
4352 b_base = g_base + size;
4353 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4354 ret = -EFAULT;
4355 goto out;
4356 }
4357out:
Daniel Vetter84849902012-12-02 00:28:11 +01004358 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004359 return ret;
4360}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004361
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004362/**
4363 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4364 * @dev: DRM device
4365 * @data: ioctl data
4366 * @file_priv: DRM file info
4367 *
4368 * This schedules an asynchronous update on a given CRTC, called page flip.
4369 * Optionally a drm event is generated to signal the completion of the event.
4370 * Generic drivers cannot assume that a pageflip with changed framebuffer
4371 * properties (including driver specific metadata like tiling layout) will work,
4372 * but some drivers support e.g. pixel format changes through the pageflip
4373 * ioctl.
4374 *
4375 * Called by the user via ioctl.
4376 *
4377 * Returns:
4378 * Zero on success, errno on failure.
4379 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004380int drm_mode_page_flip_ioctl(struct drm_device *dev,
4381 void *data, struct drm_file *file_priv)
4382{
4383 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004384 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004385 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004386 struct drm_pending_vblank_event *e = NULL;
4387 unsigned long flags;
4388 int ret = -EINVAL;
4389
4390 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4391 page_flip->reserved != 0)
4392 return -EINVAL;
4393
Keith Packard62f21042013-07-22 18:50:00 -07004394 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4395 return -EINVAL;
4396
Rob Clarka2b34e22013-10-05 16:36:52 -04004397 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4398 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004399 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004400
Rob Clark51fd3712013-11-19 12:10:12 -05004401 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004402 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004403 /* The framebuffer is currently unbound, presumably
4404 * due to a hotplug event, that userspace has not
4405 * yet discovered.
4406 */
4407 ret = -EBUSY;
4408 goto out;
4409 }
4410
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004411 if (crtc->funcs->page_flip == NULL)
4412 goto out;
4413
Daniel Vetter786b99e2012-12-02 21:53:40 +01004414 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004415 if (!fb) {
4416 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004417 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004418 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004419
Damien Lespiauc11e9282013-09-25 16:45:30 +01004420 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4421 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004422 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004423
Matt Roperf4510a22014-04-01 15:22:40 -07004424 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004425 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4426 ret = -EINVAL;
4427 goto out;
4428 }
4429
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004430 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4431 ret = -ENOMEM;
4432 spin_lock_irqsave(&dev->event_lock, flags);
4433 if (file_priv->event_space < sizeof e->event) {
4434 spin_unlock_irqrestore(&dev->event_lock, flags);
4435 goto out;
4436 }
4437 file_priv->event_space -= sizeof e->event;
4438 spin_unlock_irqrestore(&dev->event_lock, flags);
4439
4440 e = kzalloc(sizeof *e, GFP_KERNEL);
4441 if (e == NULL) {
4442 spin_lock_irqsave(&dev->event_lock, flags);
4443 file_priv->event_space += sizeof e->event;
4444 spin_unlock_irqrestore(&dev->event_lock, flags);
4445 goto out;
4446 }
4447
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004448 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004449 e->event.base.length = sizeof e->event;
4450 e->event.user_data = page_flip->user_data;
4451 e->base.event = &e->event.base;
4452 e->base.file_priv = file_priv;
4453 e->base.destroy =
4454 (void (*) (struct drm_pending_event *)) kfree;
4455 }
4456
Matt Roperf4510a22014-04-01 15:22:40 -07004457 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004458 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004459 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004460 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4461 spin_lock_irqsave(&dev->event_lock, flags);
4462 file_priv->event_space += sizeof e->event;
4463 spin_unlock_irqrestore(&dev->event_lock, flags);
4464 kfree(e);
4465 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004466 /* Keep the old fb, don't unref it. */
4467 old_fb = NULL;
4468 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004469 /*
4470 * Warn if the driver hasn't properly updated the crtc->fb
4471 * field to reflect that the new framebuffer is now used.
4472 * Failing to do so will screw with the reference counting
4473 * on framebuffers.
4474 */
Matt Roperf4510a22014-04-01 15:22:40 -07004475 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004476 /* Unref only the old framebuffer. */
4477 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004478 }
4479
4480out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004481 if (fb)
4482 drm_framebuffer_unreference(fb);
4483 if (old_fb)
4484 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004485 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004486
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004487 return ret;
4488}
Chris Wilsoneb033552011-01-24 15:11:08 +00004489
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004490/**
4491 * drm_mode_config_reset - call ->reset callbacks
4492 * @dev: drm device
4493 *
4494 * This functions calls all the crtc's, encoder's and connector's ->reset
4495 * callback. Drivers can use this in e.g. their driver load or resume code to
4496 * reset hardware and software state.
4497 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004498void drm_mode_config_reset(struct drm_device *dev)
4499{
4500 struct drm_crtc *crtc;
4501 struct drm_encoder *encoder;
4502 struct drm_connector *connector;
4503
4504 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4505 if (crtc->funcs->reset)
4506 crtc->funcs->reset(crtc);
4507
4508 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4509 if (encoder->funcs->reset)
4510 encoder->funcs->reset(encoder);
4511
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004512 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4513 connector->status = connector_status_unknown;
4514
Chris Wilsoneb033552011-01-24 15:11:08 +00004515 if (connector->funcs->reset)
4516 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004517 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004518}
4519EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004520
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004521/**
4522 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4523 * @dev: DRM device
4524 * @data: ioctl data
4525 * @file_priv: DRM file info
4526 *
4527 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4528 * TTM or something else entirely) and returns the resulting buffer handle. This
4529 * handle can then be wrapped up into a framebuffer modeset object.
4530 *
4531 * Note that userspace is not allowed to use such objects for render
4532 * acceleration - drivers must create their own private ioctls for such a use
4533 * case.
4534 *
4535 * Called by the user via ioctl.
4536 *
4537 * Returns:
4538 * Zero on success, errno on failure.
4539 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004540int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4541 void *data, struct drm_file *file_priv)
4542{
4543 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004544 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004545
4546 if (!dev->driver->dumb_create)
4547 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004548 if (!args->width || !args->height || !args->bpp)
4549 return -EINVAL;
4550
4551 /* overflow checks for 32bit size calculations */
4552 cpp = DIV_ROUND_UP(args->bpp, 8);
4553 if (cpp > 0xffffffffU / args->width)
4554 return -EINVAL;
4555 stride = cpp * args->width;
4556 if (args->height > 0xffffffffU / stride)
4557 return -EINVAL;
4558
4559 /* test for wrap-around */
4560 size = args->height * stride;
4561 if (PAGE_ALIGN(size) == 0)
4562 return -EINVAL;
4563
Dave Airlieff72145b2011-02-07 12:16:14 +10004564 return dev->driver->dumb_create(file_priv, dev, args);
4565}
4566
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004567/**
4568 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4569 * @dev: DRM device
4570 * @data: ioctl data
4571 * @file_priv: DRM file info
4572 *
4573 * Allocate an offset in the drm device node's address space to be able to
4574 * memory map a dumb buffer.
4575 *
4576 * Called by the user via ioctl.
4577 *
4578 * Returns:
4579 * Zero on success, errno on failure.
4580 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004581int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4582 void *data, struct drm_file *file_priv)
4583{
4584 struct drm_mode_map_dumb *args = data;
4585
4586 /* call driver ioctl to get mmap offset */
4587 if (!dev->driver->dumb_map_offset)
4588 return -ENOSYS;
4589
4590 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4591}
4592
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004593/**
4594 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4595 * @dev: DRM device
4596 * @data: ioctl data
4597 * @file_priv: DRM file info
4598 *
4599 * This destroys the userspace handle for the given dumb backing storage buffer.
4600 * Since buffer objects must be reference counted in the kernel a buffer object
4601 * won't be immediately freed if a framebuffer modeset object still uses it.
4602 *
4603 * Called by the user via ioctl.
4604 *
4605 * Returns:
4606 * Zero on success, errno on failure.
4607 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004608int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4609 void *data, struct drm_file *file_priv)
4610{
4611 struct drm_mode_destroy_dumb *args = data;
4612
4613 if (!dev->driver->dumb_destroy)
4614 return -ENOSYS;
4615
4616 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4617}
Dave Airlie248dbc22011-11-29 20:02:54 +00004618
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004619/**
4620 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4621 * @format: pixel format (DRM_FORMAT_*)
4622 * @depth: storage for the depth value
4623 * @bpp: storage for the bpp value
4624 *
4625 * This only supports RGB formats here for compat with code that doesn't use
4626 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004627 */
4628void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4629 int *bpp)
4630{
4631 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004632 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004633 case DRM_FORMAT_RGB332:
4634 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004635 *depth = 8;
4636 *bpp = 8;
4637 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004638 case DRM_FORMAT_XRGB1555:
4639 case DRM_FORMAT_XBGR1555:
4640 case DRM_FORMAT_RGBX5551:
4641 case DRM_FORMAT_BGRX5551:
4642 case DRM_FORMAT_ARGB1555:
4643 case DRM_FORMAT_ABGR1555:
4644 case DRM_FORMAT_RGBA5551:
4645 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004646 *depth = 15;
4647 *bpp = 16;
4648 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004649 case DRM_FORMAT_RGB565:
4650 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004651 *depth = 16;
4652 *bpp = 16;
4653 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004654 case DRM_FORMAT_RGB888:
4655 case DRM_FORMAT_BGR888:
4656 *depth = 24;
4657 *bpp = 24;
4658 break;
4659 case DRM_FORMAT_XRGB8888:
4660 case DRM_FORMAT_XBGR8888:
4661 case DRM_FORMAT_RGBX8888:
4662 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004663 *depth = 24;
4664 *bpp = 32;
4665 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004666 case DRM_FORMAT_XRGB2101010:
4667 case DRM_FORMAT_XBGR2101010:
4668 case DRM_FORMAT_RGBX1010102:
4669 case DRM_FORMAT_BGRX1010102:
4670 case DRM_FORMAT_ARGB2101010:
4671 case DRM_FORMAT_ABGR2101010:
4672 case DRM_FORMAT_RGBA1010102:
4673 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004674 *depth = 30;
4675 *bpp = 32;
4676 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004677 case DRM_FORMAT_ARGB8888:
4678 case DRM_FORMAT_ABGR8888:
4679 case DRM_FORMAT_RGBA8888:
4680 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004681 *depth = 32;
4682 *bpp = 32;
4683 break;
4684 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004685 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4686 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004687 *depth = 0;
4688 *bpp = 0;
4689 break;
4690 }
4691}
4692EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004693
4694/**
4695 * drm_format_num_planes - get the number of planes for format
4696 * @format: pixel format (DRM_FORMAT_*)
4697 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004698 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004699 * The number of planes used by the specified pixel format.
4700 */
4701int drm_format_num_planes(uint32_t format)
4702{
4703 switch (format) {
4704 case DRM_FORMAT_YUV410:
4705 case DRM_FORMAT_YVU410:
4706 case DRM_FORMAT_YUV411:
4707 case DRM_FORMAT_YVU411:
4708 case DRM_FORMAT_YUV420:
4709 case DRM_FORMAT_YVU420:
4710 case DRM_FORMAT_YUV422:
4711 case DRM_FORMAT_YVU422:
4712 case DRM_FORMAT_YUV444:
4713 case DRM_FORMAT_YVU444:
4714 return 3;
4715 case DRM_FORMAT_NV12:
4716 case DRM_FORMAT_NV21:
4717 case DRM_FORMAT_NV16:
4718 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004719 case DRM_FORMAT_NV24:
4720 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004721 return 2;
4722 default:
4723 return 1;
4724 }
4725}
4726EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004727
4728/**
4729 * drm_format_plane_cpp - determine the bytes per pixel value
4730 * @format: pixel format (DRM_FORMAT_*)
4731 * @plane: plane index
4732 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004733 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004734 * The bytes per pixel value for the specified plane.
4735 */
4736int drm_format_plane_cpp(uint32_t format, int plane)
4737{
4738 unsigned int depth;
4739 int bpp;
4740
4741 if (plane >= drm_format_num_planes(format))
4742 return 0;
4743
4744 switch (format) {
4745 case DRM_FORMAT_YUYV:
4746 case DRM_FORMAT_YVYU:
4747 case DRM_FORMAT_UYVY:
4748 case DRM_FORMAT_VYUY:
4749 return 2;
4750 case DRM_FORMAT_NV12:
4751 case DRM_FORMAT_NV21:
4752 case DRM_FORMAT_NV16:
4753 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004754 case DRM_FORMAT_NV24:
4755 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004756 return plane ? 2 : 1;
4757 case DRM_FORMAT_YUV410:
4758 case DRM_FORMAT_YVU410:
4759 case DRM_FORMAT_YUV411:
4760 case DRM_FORMAT_YVU411:
4761 case DRM_FORMAT_YUV420:
4762 case DRM_FORMAT_YVU420:
4763 case DRM_FORMAT_YUV422:
4764 case DRM_FORMAT_YVU422:
4765 case DRM_FORMAT_YUV444:
4766 case DRM_FORMAT_YVU444:
4767 return 1;
4768 default:
4769 drm_fb_get_bpp_depth(format, &depth, &bpp);
4770 return bpp >> 3;
4771 }
4772}
4773EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004774
4775/**
4776 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4777 * @format: pixel format (DRM_FORMAT_*)
4778 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004779 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004780 * The horizontal chroma subsampling factor for the
4781 * specified pixel format.
4782 */
4783int drm_format_horz_chroma_subsampling(uint32_t format)
4784{
4785 switch (format) {
4786 case DRM_FORMAT_YUV411:
4787 case DRM_FORMAT_YVU411:
4788 case DRM_FORMAT_YUV410:
4789 case DRM_FORMAT_YVU410:
4790 return 4;
4791 case DRM_FORMAT_YUYV:
4792 case DRM_FORMAT_YVYU:
4793 case DRM_FORMAT_UYVY:
4794 case DRM_FORMAT_VYUY:
4795 case DRM_FORMAT_NV12:
4796 case DRM_FORMAT_NV21:
4797 case DRM_FORMAT_NV16:
4798 case DRM_FORMAT_NV61:
4799 case DRM_FORMAT_YUV422:
4800 case DRM_FORMAT_YVU422:
4801 case DRM_FORMAT_YUV420:
4802 case DRM_FORMAT_YVU420:
4803 return 2;
4804 default:
4805 return 1;
4806 }
4807}
4808EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4809
4810/**
4811 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4812 * @format: pixel format (DRM_FORMAT_*)
4813 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004814 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004815 * The vertical chroma subsampling factor for the
4816 * specified pixel format.
4817 */
4818int drm_format_vert_chroma_subsampling(uint32_t format)
4819{
4820 switch (format) {
4821 case DRM_FORMAT_YUV410:
4822 case DRM_FORMAT_YVU410:
4823 return 4;
4824 case DRM_FORMAT_YUV420:
4825 case DRM_FORMAT_YVU420:
4826 case DRM_FORMAT_NV12:
4827 case DRM_FORMAT_NV21:
4828 return 2;
4829 default:
4830 return 1;
4831 }
4832}
4833EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004834
4835/**
4836 * drm_mode_config_init - initialize DRM mode_configuration structure
4837 * @dev: DRM device
4838 *
4839 * Initialize @dev's mode_config structure, used for tracking the graphics
4840 * configuration of @dev.
4841 *
4842 * Since this initializes the modeset locks, no locking is possible. Which is no
4843 * problem, since this should happen single threaded at init time. It is the
4844 * driver's problem to ensure this guarantee.
4845 *
4846 */
4847void drm_mode_config_init(struct drm_device *dev)
4848{
4849 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004850 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004851 mutex_init(&dev->mode_config.idr_mutex);
4852 mutex_init(&dev->mode_config.fb_lock);
4853 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4854 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4855 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004856 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004857 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4858 INIT_LIST_HEAD(&dev->mode_config.property_list);
4859 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4860 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4861 idr_init(&dev->mode_config.crtc_idr);
4862
4863 drm_modeset_lock_all(dev);
4864 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004865 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004866 drm_modeset_unlock_all(dev);
4867
4868 /* Just to be sure */
4869 dev->mode_config.num_fb = 0;
4870 dev->mode_config.num_connector = 0;
4871 dev->mode_config.num_crtc = 0;
4872 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004873 dev->mode_config.num_overlay_plane = 0;
4874 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004875}
4876EXPORT_SYMBOL(drm_mode_config_init);
4877
4878/**
4879 * drm_mode_config_cleanup - free up DRM mode_config info
4880 * @dev: DRM device
4881 *
4882 * Free up all the connectors and CRTCs associated with this DRM device, then
4883 * free up the framebuffers and associated buffer objects.
4884 *
4885 * Note that since this /should/ happen single-threaded at driver/device
4886 * teardown time, no locking is required. It's the driver's job to ensure that
4887 * this guarantee actually holds true.
4888 *
4889 * FIXME: cleanup any dangling user buffer objects too
4890 */
4891void drm_mode_config_cleanup(struct drm_device *dev)
4892{
4893 struct drm_connector *connector, *ot;
4894 struct drm_crtc *crtc, *ct;
4895 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004896 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004897 struct drm_framebuffer *fb, *fbt;
4898 struct drm_property *property, *pt;
4899 struct drm_property_blob *blob, *bt;
4900 struct drm_plane *plane, *plt;
4901
4902 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4903 head) {
4904 encoder->funcs->destroy(encoder);
4905 }
4906
Sean Paul3b336ec2013-08-14 16:47:37 -04004907 list_for_each_entry_safe(bridge, brt,
4908 &dev->mode_config.bridge_list, head) {
4909 bridge->funcs->destroy(bridge);
4910 }
4911
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004912 list_for_each_entry_safe(connector, ot,
4913 &dev->mode_config.connector_list, head) {
4914 connector->funcs->destroy(connector);
4915 }
4916
4917 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4918 head) {
4919 drm_property_destroy(dev, property);
4920 }
4921
4922 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4923 head) {
4924 drm_property_destroy_blob(dev, blob);
4925 }
4926
4927 /*
4928 * Single-threaded teardown context, so it's not required to grab the
4929 * fb_lock to protect against concurrent fb_list access. Contrary, it
4930 * would actually deadlock with the drm_framebuffer_cleanup function.
4931 *
4932 * Also, if there are any framebuffers left, that's a driver leak now,
4933 * so politely WARN about this.
4934 */
4935 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4936 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4937 drm_framebuffer_remove(fb);
4938 }
4939
4940 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4941 head) {
4942 plane->funcs->destroy(plane);
4943 }
4944
4945 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4946 crtc->funcs->destroy(crtc);
4947 }
4948
4949 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05004950 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004951}
4952EXPORT_SYMBOL(drm_mode_config_cleanup);