blob: 2fbee61d632de6b60ad951172e984afc5326cecd [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,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303398 int num_props,
3399 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003400{
3401 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303402 int i, ret, index = 0;
3403 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003404
3405 flags |= DRM_MODE_PROP_BITMASK;
3406
3407 property = drm_property_create(dev, flags, name, num_values);
3408 if (!property)
3409 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303410 for (i = 0; i < num_props; i++) {
3411 if (!(supported_bits & (1ULL << props[i].type)))
3412 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003413
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303414 if (WARN_ON(index >= num_values)) {
3415 drm_property_destroy(dev, property);
3416 return NULL;
3417 }
3418
3419 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003420 props[i].type,
3421 props[i].name);
3422 if (ret) {
3423 drm_property_destroy(dev, property);
3424 return NULL;
3425 }
3426 }
3427
3428 return property;
3429}
3430EXPORT_SYMBOL(drm_property_create_bitmask);
3431
Rob Clarkebc44cf2012-09-12 22:22:31 -05003432static struct drm_property *property_create_range(struct drm_device *dev,
3433 int flags, const char *name,
3434 uint64_t min, uint64_t max)
3435{
3436 struct drm_property *property;
3437
3438 property = drm_property_create(dev, flags, name, 2);
3439 if (!property)
3440 return NULL;
3441
3442 property->values[0] = min;
3443 property->values[1] = max;
3444
3445 return property;
3446}
3447
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003448/**
3449 * drm_property_create - create a new ranged property type
3450 * @dev: drm device
3451 * @flags: flags specifying the property type
3452 * @name: name of the property
3453 * @min: minimum value of the property
3454 * @max: maximum value of the property
3455 *
3456 * This creates a new generic drm property which can then be attached to a drm
3457 * object with drm_object_attach_property. The returned property object must be
3458 * freed with drm_property_destroy.
3459 *
3460 * Userspace is allowed to set any interger value in the (min, max) range
3461 * inclusive.
3462 *
3463 * Returns:
3464 * A pointer to the newly created property on success, NULL on failure.
3465 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003466struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3467 const char *name,
3468 uint64_t min, uint64_t max)
3469{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003470 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3471 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003472}
3473EXPORT_SYMBOL(drm_property_create_range);
3474
Rob Clarkebc44cf2012-09-12 22:22:31 -05003475struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3476 int flags, const char *name,
3477 int64_t min, int64_t max)
3478{
3479 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3480 name, I642U64(min), I642U64(max));
3481}
3482EXPORT_SYMBOL(drm_property_create_signed_range);
3483
Rob Clark98f75de2014-05-30 11:37:03 -04003484struct drm_property *drm_property_create_object(struct drm_device *dev,
3485 int flags, const char *name, uint32_t type)
3486{
3487 struct drm_property *property;
3488
3489 flags |= DRM_MODE_PROP_OBJECT;
3490
3491 property = drm_property_create(dev, flags, name, 1);
3492 if (!property)
3493 return NULL;
3494
3495 property->values[0] = type;
3496
3497 return property;
3498}
3499EXPORT_SYMBOL(drm_property_create_object);
3500
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003501/**
3502 * drm_property_add_enum - add a possible value to an enumeration property
3503 * @property: enumeration property to change
3504 * @index: index of the new enumeration
3505 * @value: value of the new enumeration
3506 * @name: symbolic name of the new enumeration
3507 *
3508 * This functions adds enumerations to a property.
3509 *
3510 * It's use is deprecated, drivers should use one of the more specific helpers
3511 * to directly create the property with all enumerations already attached.
3512 *
3513 * Returns:
3514 * Zero on success, error code on failure.
3515 */
Dave Airlief453ba02008-11-07 14:05:41 -08003516int drm_property_add_enum(struct drm_property *property, int index,
3517 uint64_t value, const char *name)
3518{
3519 struct drm_property_enum *prop_enum;
3520
Rob Clark5ea22f22014-05-30 11:34:01 -04003521 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3522 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003523 return -EINVAL;
3524
3525 /*
3526 * Bitmask enum properties have the additional constraint of values
3527 * from 0 to 63
3528 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003529 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3530 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003531 return -EINVAL;
3532
3533 if (!list_empty(&property->enum_blob_list)) {
3534 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3535 if (prop_enum->value == value) {
3536 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3537 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3538 return 0;
3539 }
3540 }
3541 }
3542
3543 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3544 if (!prop_enum)
3545 return -ENOMEM;
3546
3547 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3548 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3549 prop_enum->value = value;
3550
3551 property->values[index] = value;
3552 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3553 return 0;
3554}
3555EXPORT_SYMBOL(drm_property_add_enum);
3556
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003557/**
3558 * drm_property_destroy - destroy a drm property
3559 * @dev: drm device
3560 * @property: property to destry
3561 *
3562 * This function frees a property including any attached resources like
3563 * enumeration values.
3564 */
Dave Airlief453ba02008-11-07 14:05:41 -08003565void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3566{
3567 struct drm_property_enum *prop_enum, *pt;
3568
3569 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3570 list_del(&prop_enum->head);
3571 kfree(prop_enum);
3572 }
3573
3574 if (property->num_values)
3575 kfree(property->values);
3576 drm_mode_object_put(dev, &property->base);
3577 list_del(&property->head);
3578 kfree(property);
3579}
3580EXPORT_SYMBOL(drm_property_destroy);
3581
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003582/**
3583 * drm_object_attach_property - attach a property to a modeset object
3584 * @obj: drm modeset object
3585 * @property: property to attach
3586 * @init_val: initial value of the property
3587 *
3588 * This attaches the given property to the modeset object with the given initial
3589 * value. Currently this function cannot fail since the properties are stored in
3590 * a statically sized array.
3591 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003592void drm_object_attach_property(struct drm_mode_object *obj,
3593 struct drm_property *property,
3594 uint64_t init_val)
3595{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003596 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003597
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003598 if (count == DRM_OBJECT_MAX_PROPERTY) {
3599 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3600 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3601 "you see this message on the same object type.\n",
3602 obj->type);
3603 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003604 }
3605
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003606 obj->properties->ids[count] = property->base.id;
3607 obj->properties->values[count] = init_val;
3608 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003609}
3610EXPORT_SYMBOL(drm_object_attach_property);
3611
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003612/**
3613 * drm_object_property_set_value - set the value of a property
3614 * @obj: drm mode object to set property value for
3615 * @property: property to set
3616 * @val: value the property should be set to
3617 *
3618 * This functions sets a given property on a given object. This function only
3619 * changes the software state of the property, it does not call into the
3620 * driver's ->set_property callback.
3621 *
3622 * Returns:
3623 * Zero on success, error code on failure.
3624 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003625int drm_object_property_set_value(struct drm_mode_object *obj,
3626 struct drm_property *property, uint64_t val)
3627{
3628 int i;
3629
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003630 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003631 if (obj->properties->ids[i] == property->base.id) {
3632 obj->properties->values[i] = val;
3633 return 0;
3634 }
3635 }
3636
3637 return -EINVAL;
3638}
3639EXPORT_SYMBOL(drm_object_property_set_value);
3640
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003641/**
3642 * drm_object_property_get_value - retrieve the value of a property
3643 * @obj: drm mode object to get property value from
3644 * @property: property to retrieve
3645 * @val: storage for the property value
3646 *
3647 * This function retrieves the softare state of the given property for the given
3648 * property. Since there is no driver callback to retrieve the current property
3649 * value this might be out of sync with the hardware, depending upon the driver
3650 * and property.
3651 *
3652 * Returns:
3653 * Zero on success, error code on failure.
3654 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003655int drm_object_property_get_value(struct drm_mode_object *obj,
3656 struct drm_property *property, uint64_t *val)
3657{
3658 int i;
3659
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003660 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003661 if (obj->properties->ids[i] == property->base.id) {
3662 *val = obj->properties->values[i];
3663 return 0;
3664 }
3665 }
3666
3667 return -EINVAL;
3668}
3669EXPORT_SYMBOL(drm_object_property_get_value);
3670
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003671/**
3672 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3673 * @dev: DRM device
3674 * @data: ioctl data
3675 * @file_priv: DRM file info
3676 *
3677 * This function retrieves the current value for an connectors's property.
3678 *
3679 * Called by the user via ioctl.
3680 *
3681 * Returns:
3682 * Zero on success, errno on failure.
3683 */
Dave Airlief453ba02008-11-07 14:05:41 -08003684int drm_mode_getproperty_ioctl(struct drm_device *dev,
3685 void *data, struct drm_file *file_priv)
3686{
Dave Airlief453ba02008-11-07 14:05:41 -08003687 struct drm_mode_get_property *out_resp = data;
3688 struct drm_property *property;
3689 int enum_count = 0;
3690 int blob_count = 0;
3691 int value_count = 0;
3692 int ret = 0, i;
3693 int copied;
3694 struct drm_property_enum *prop_enum;
3695 struct drm_mode_property_enum __user *enum_ptr;
3696 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003697 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003698 uint64_t __user *values_ptr;
3699 uint32_t __user *blob_length_ptr;
3700
Dave Airliefb3b06c2011-02-08 13:55:21 +10003701 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3702 return -EINVAL;
3703
Daniel Vetter84849902012-12-02 00:28:11 +01003704 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003705 property = drm_property_find(dev, out_resp->prop_id);
3706 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003707 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003708 goto done;
3709 }
Dave Airlief453ba02008-11-07 14:05:41 -08003710
Rob Clark5ea22f22014-05-30 11:34:01 -04003711 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3712 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003713 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3714 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003715 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003716 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3717 blob_count++;
3718 }
3719
3720 value_count = property->num_values;
3721
3722 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3723 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3724 out_resp->flags = property->flags;
3725
3726 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003727 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003728 for (i = 0; i < value_count; i++) {
3729 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3730 ret = -EFAULT;
3731 goto done;
3732 }
3733 }
3734 }
3735 out_resp->count_values = value_count;
3736
Rob Clark5ea22f22014-05-30 11:34:01 -04003737 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3738 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003739 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3740 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003741 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003742 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3743
3744 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3745 ret = -EFAULT;
3746 goto done;
3747 }
3748
3749 if (copy_to_user(&enum_ptr[copied].name,
3750 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3751 ret = -EFAULT;
3752 goto done;
3753 }
3754 copied++;
3755 }
3756 }
3757 out_resp->count_enum_blobs = enum_count;
3758 }
3759
Rob Clark5ea22f22014-05-30 11:34:01 -04003760 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003761 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3762 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003763 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3764 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003765
3766 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3767 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3768 ret = -EFAULT;
3769 goto done;
3770 }
3771
3772 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3773 ret = -EFAULT;
3774 goto done;
3775 }
3776
3777 copied++;
3778 }
3779 }
3780 out_resp->count_enum_blobs = blob_count;
3781 }
3782done:
Daniel Vetter84849902012-12-02 00:28:11 +01003783 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003784 return ret;
3785}
3786
3787static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3788 void *data)
3789{
3790 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003791 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003792
3793 if (!length || !data)
3794 return NULL;
3795
3796 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3797 if (!blob)
3798 return NULL;
3799
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003800 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3801 if (ret) {
3802 kfree(blob);
3803 return NULL;
3804 }
3805
Dave Airlief453ba02008-11-07 14:05:41 -08003806 blob->length = length;
3807
3808 memcpy(blob->data, data, length);
3809
Dave Airlief453ba02008-11-07 14:05:41 -08003810 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3811 return blob;
3812}
3813
3814static void drm_property_destroy_blob(struct drm_device *dev,
3815 struct drm_property_blob *blob)
3816{
3817 drm_mode_object_put(dev, &blob->base);
3818 list_del(&blob->head);
3819 kfree(blob);
3820}
3821
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003822/**
3823 * drm_mode_getblob_ioctl - get the contents of a blob property value
3824 * @dev: DRM device
3825 * @data: ioctl data
3826 * @file_priv: DRM file info
3827 *
3828 * This function retrieves the contents of a blob property. The value stored in
3829 * an object's blob property is just a normal modeset object id.
3830 *
3831 * Called by the user via ioctl.
3832 *
3833 * Returns:
3834 * Zero on success, errno on failure.
3835 */
Dave Airlief453ba02008-11-07 14:05:41 -08003836int drm_mode_getblob_ioctl(struct drm_device *dev,
3837 void *data, struct drm_file *file_priv)
3838{
Dave Airlief453ba02008-11-07 14:05:41 -08003839 struct drm_mode_get_blob *out_resp = data;
3840 struct drm_property_blob *blob;
3841 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003842 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003843
Dave Airliefb3b06c2011-02-08 13:55:21 +10003844 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3845 return -EINVAL;
3846
Daniel Vetter84849902012-12-02 00:28:11 +01003847 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003848 blob = drm_property_blob_find(dev, out_resp->blob_id);
3849 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003850 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003851 goto done;
3852 }
Dave Airlief453ba02008-11-07 14:05:41 -08003853
3854 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003855 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003856 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3857 ret = -EFAULT;
3858 goto done;
3859 }
3860 }
3861 out_resp->length = blob->length;
3862
3863done:
Daniel Vetter84849902012-12-02 00:28:11 +01003864 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003865 return ret;
3866}
3867
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003868/**
3869 * drm_mode_connector_update_edid_property - update the edid property of a connector
3870 * @connector: drm connector
3871 * @edid: new value of the edid property
3872 *
3873 * This function creates a new blob modeset object and assigns its id to the
3874 * connector's edid property.
3875 *
3876 * Returns:
3877 * Zero on success, errno on failure.
3878 */
Dave Airlief453ba02008-11-07 14:05:41 -08003879int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3880 struct edid *edid)
3881{
3882 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003883 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003884
3885 if (connector->edid_blob_ptr)
3886 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3887
3888 /* Delete edid, when there is none. */
3889 if (!edid) {
3890 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003891 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003892 return ret;
3893 }
3894
Adam Jackson7466f4c2010-03-29 21:43:23 +00003895 size = EDID_LENGTH * (1 + edid->extensions);
3896 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3897 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003898 if (!connector->edid_blob_ptr)
3899 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003900
Rob Clark58495562012-10-11 20:50:56 -05003901 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003902 dev->mode_config.edid_property,
3903 connector->edid_blob_ptr->base.id);
3904
3905 return ret;
3906}
3907EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3908
Paulo Zanoni26a34812012-05-15 18:08:59 -03003909static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003910 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003911{
3912 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3913 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003914
3915 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003916 if (value < property->values[0] || value > property->values[1])
3917 return false;
3918 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003919 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3920 int64_t svalue = U642I64(value);
3921 if (svalue < U642I64(property->values[0]) ||
3922 svalue > U642I64(property->values[1]))
3923 return false;
3924 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003925 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06003926 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003927 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003928 for (i = 0; i < property->num_values; i++)
3929 valid_mask |= (1ULL << property->values[i]);
3930 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003931 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003932 /* Only the driver knows */
3933 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04003934 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3935 struct drm_mode_object *obj;
3936 /* a zero value for an object property translates to null: */
3937 if (value == 0)
3938 return true;
3939 /*
3940 * NOTE: use _object_find() directly to bypass restriction on
3941 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3942 * object this could race against object finalization, so it
3943 * simply tells us that the object *was* valid. Which is good
3944 * enough.
3945 */
3946 obj = _object_find(property->dev, value, property->values[0]);
3947 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003948 } else {
3949 int i;
3950 for (i = 0; i < property->num_values; i++)
3951 if (property->values[i] == value)
3952 return true;
3953 return false;
3954 }
3955}
3956
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003957/**
3958 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3959 * @dev: DRM device
3960 * @data: ioctl data
3961 * @file_priv: DRM file info
3962 *
3963 * This function sets the current value for a connectors's property. It also
3964 * calls into a driver's ->set_property callback to update the hardware state
3965 *
3966 * Called by the user via ioctl.
3967 *
3968 * Returns:
3969 * Zero on success, errno on failure.
3970 */
Dave Airlief453ba02008-11-07 14:05:41 -08003971int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3972 void *data, struct drm_file *file_priv)
3973{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003974 struct drm_mode_connector_set_property *conn_set_prop = data;
3975 struct drm_mode_obj_set_property obj_set_prop = {
3976 .value = conn_set_prop->value,
3977 .prop_id = conn_set_prop->prop_id,
3978 .obj_id = conn_set_prop->connector_id,
3979 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3980 };
Dave Airlief453ba02008-11-07 14:05:41 -08003981
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003982 /* It does all the locking and checking we need */
3983 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003984}
3985
Paulo Zanonic5431882012-05-15 18:09:02 -03003986static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3987 struct drm_property *property,
3988 uint64_t value)
3989{
3990 int ret = -EINVAL;
3991 struct drm_connector *connector = obj_to_connector(obj);
3992
3993 /* Do DPMS ourselves */
3994 if (property == connector->dev->mode_config.dpms_property) {
3995 if (connector->funcs->dpms)
3996 (*connector->funcs->dpms)(connector, (int)value);
3997 ret = 0;
3998 } else if (connector->funcs->set_property)
3999 ret = connector->funcs->set_property(connector, property, value);
4000
4001 /* store the property value if successful */
4002 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004003 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004004 return ret;
4005}
4006
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004007static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4008 struct drm_property *property,
4009 uint64_t value)
4010{
4011 int ret = -EINVAL;
4012 struct drm_crtc *crtc = obj_to_crtc(obj);
4013
4014 if (crtc->funcs->set_property)
4015 ret = crtc->funcs->set_property(crtc, property, value);
4016 if (!ret)
4017 drm_object_property_set_value(obj, property, value);
4018
4019 return ret;
4020}
4021
Rob Clark4d939142012-05-17 02:23:27 -06004022static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4023 struct drm_property *property,
4024 uint64_t value)
4025{
4026 int ret = -EINVAL;
4027 struct drm_plane *plane = obj_to_plane(obj);
4028
4029 if (plane->funcs->set_property)
4030 ret = plane->funcs->set_property(plane, property, value);
4031 if (!ret)
4032 drm_object_property_set_value(obj, property, value);
4033
4034 return ret;
4035}
4036
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004037/**
4038 * drm_mode_getproperty_ioctl - get the current value of a object's property
4039 * @dev: DRM device
4040 * @data: ioctl data
4041 * @file_priv: DRM file info
4042 *
4043 * This function retrieves the current value for an object's property. Compared
4044 * to the connector specific ioctl this one is extended to also work on crtc and
4045 * plane objects.
4046 *
4047 * Called by the user via ioctl.
4048 *
4049 * Returns:
4050 * Zero on success, errno on failure.
4051 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004052int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4053 struct drm_file *file_priv)
4054{
4055 struct drm_mode_obj_get_properties *arg = data;
4056 struct drm_mode_object *obj;
4057 int ret = 0;
4058 int i;
4059 int copied = 0;
4060 int props_count = 0;
4061 uint32_t __user *props_ptr;
4062 uint64_t __user *prop_values_ptr;
4063
4064 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4065 return -EINVAL;
4066
Daniel Vetter84849902012-12-02 00:28:11 +01004067 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004068
4069 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4070 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004071 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004072 goto out;
4073 }
4074 if (!obj->properties) {
4075 ret = -EINVAL;
4076 goto out;
4077 }
4078
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004079 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004080
4081 /* This ioctl is called twice, once to determine how much space is
4082 * needed, and the 2nd time to fill it. */
4083 if ((arg->count_props >= props_count) && props_count) {
4084 copied = 0;
4085 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4086 prop_values_ptr = (uint64_t __user *)(unsigned long)
4087 (arg->prop_values_ptr);
4088 for (i = 0; i < props_count; i++) {
4089 if (put_user(obj->properties->ids[i],
4090 props_ptr + copied)) {
4091 ret = -EFAULT;
4092 goto out;
4093 }
4094 if (put_user(obj->properties->values[i],
4095 prop_values_ptr + copied)) {
4096 ret = -EFAULT;
4097 goto out;
4098 }
4099 copied++;
4100 }
4101 }
4102 arg->count_props = props_count;
4103out:
Daniel Vetter84849902012-12-02 00:28:11 +01004104 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004105 return ret;
4106}
4107
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004108/**
4109 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4110 * @dev: DRM device
4111 * @data: ioctl data
4112 * @file_priv: DRM file info
4113 *
4114 * This function sets the current value for an object's property. It also calls
4115 * into a driver's ->set_property callback to update the hardware state.
4116 * Compared to the connector specific ioctl this one is extended to also work on
4117 * crtc and plane objects.
4118 *
4119 * Called by the user via ioctl.
4120 *
4121 * Returns:
4122 * Zero on success, errno on failure.
4123 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004124int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4125 struct drm_file *file_priv)
4126{
4127 struct drm_mode_obj_set_property *arg = data;
4128 struct drm_mode_object *arg_obj;
4129 struct drm_mode_object *prop_obj;
4130 struct drm_property *property;
4131 int ret = -EINVAL;
4132 int i;
4133
4134 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4135 return -EINVAL;
4136
Daniel Vetter84849902012-12-02 00:28:11 +01004137 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004138
4139 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004140 if (!arg_obj) {
4141 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004142 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004143 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004144 if (!arg_obj->properties)
4145 goto out;
4146
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004147 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004148 if (arg_obj->properties->ids[i] == arg->prop_id)
4149 break;
4150
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004151 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004152 goto out;
4153
4154 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4155 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004156 if (!prop_obj) {
4157 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004158 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004159 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004160 property = obj_to_property(prop_obj);
4161
4162 if (!drm_property_change_is_valid(property, arg->value))
4163 goto out;
4164
4165 switch (arg_obj->type) {
4166 case DRM_MODE_OBJECT_CONNECTOR:
4167 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4168 arg->value);
4169 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004170 case DRM_MODE_OBJECT_CRTC:
4171 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4172 break;
Rob Clark4d939142012-05-17 02:23:27 -06004173 case DRM_MODE_OBJECT_PLANE:
4174 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4175 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004176 }
4177
4178out:
Daniel Vetter84849902012-12-02 00:28:11 +01004179 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004180 return ret;
4181}
4182
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004183/**
4184 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4185 * @connector: connector to attach
4186 * @encoder: encoder to attach @connector to
4187 *
4188 * This function links up a connector to an encoder. Note that the routing
4189 * restrictions between encoders and crtcs are exposed to userspace through the
4190 * possible_clones and possible_crtcs bitmasks.
4191 *
4192 * Returns:
4193 * Zero on success, errno on failure.
4194 */
Dave Airlief453ba02008-11-07 14:05:41 -08004195int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4196 struct drm_encoder *encoder)
4197{
4198 int i;
4199
4200 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4201 if (connector->encoder_ids[i] == 0) {
4202 connector->encoder_ids[i] = encoder->base.id;
4203 return 0;
4204 }
4205 }
4206 return -ENOMEM;
4207}
4208EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4209
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004210/**
4211 * drm_mode_crtc_set_gamma_size - set the gamma table size
4212 * @crtc: CRTC to set the gamma table size for
4213 * @gamma_size: size of the gamma table
4214 *
4215 * Drivers which support gamma tables should set this to the supported gamma
4216 * table size when initializing the CRTC. Currently the drm core only supports a
4217 * fixed gamma table size.
4218 *
4219 * Returns:
4220 * Zero on success, errno on failure.
4221 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004222int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004223 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004224{
4225 crtc->gamma_size = gamma_size;
4226
4227 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4228 if (!crtc->gamma_store) {
4229 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004230 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004231 }
4232
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004233 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004234}
4235EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4236
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004237/**
4238 * drm_mode_gamma_set_ioctl - set the gamma table
4239 * @dev: DRM device
4240 * @data: ioctl data
4241 * @file_priv: DRM file info
4242 *
4243 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4244 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4245 *
4246 * Called by the user via ioctl.
4247 *
4248 * Returns:
4249 * Zero on success, errno on failure.
4250 */
Dave Airlief453ba02008-11-07 14:05:41 -08004251int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4252 void *data, struct drm_file *file_priv)
4253{
4254 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004255 struct drm_crtc *crtc;
4256 void *r_base, *g_base, *b_base;
4257 int size;
4258 int ret = 0;
4259
Dave Airliefb3b06c2011-02-08 13:55:21 +10004260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4261 return -EINVAL;
4262
Daniel Vetter84849902012-12-02 00:28:11 +01004263 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004264 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4265 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004266 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004267 goto out;
4268 }
Dave Airlief453ba02008-11-07 14:05:41 -08004269
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004270 if (crtc->funcs->gamma_set == NULL) {
4271 ret = -ENOSYS;
4272 goto out;
4273 }
4274
Dave Airlief453ba02008-11-07 14:05:41 -08004275 /* memcpy into gamma store */
4276 if (crtc_lut->gamma_size != crtc->gamma_size) {
4277 ret = -EINVAL;
4278 goto out;
4279 }
4280
4281 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4282 r_base = crtc->gamma_store;
4283 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4284 ret = -EFAULT;
4285 goto out;
4286 }
4287
4288 g_base = r_base + size;
4289 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4290 ret = -EFAULT;
4291 goto out;
4292 }
4293
4294 b_base = g_base + size;
4295 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4296 ret = -EFAULT;
4297 goto out;
4298 }
4299
James Simmons72034252010-08-03 01:33:19 +01004300 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004301
4302out:
Daniel Vetter84849902012-12-02 00:28:11 +01004303 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004304 return ret;
4305
4306}
4307
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004308/**
4309 * drm_mode_gamma_get_ioctl - get the gamma table
4310 * @dev: DRM device
4311 * @data: ioctl data
4312 * @file_priv: DRM file info
4313 *
4314 * Copy the current gamma table into the storage provided. This also provides
4315 * the gamma table size the driver expects, which can be used to size the
4316 * allocated storage.
4317 *
4318 * Called by the user via ioctl.
4319 *
4320 * Returns:
4321 * Zero on success, errno on failure.
4322 */
Dave Airlief453ba02008-11-07 14:05:41 -08004323int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4324 void *data, struct drm_file *file_priv)
4325{
4326 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004327 struct drm_crtc *crtc;
4328 void *r_base, *g_base, *b_base;
4329 int size;
4330 int ret = 0;
4331
Dave Airliefb3b06c2011-02-08 13:55:21 +10004332 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4333 return -EINVAL;
4334
Daniel Vetter84849902012-12-02 00:28:11 +01004335 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004336 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4337 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004338 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004339 goto out;
4340 }
Dave Airlief453ba02008-11-07 14:05:41 -08004341
4342 /* memcpy into gamma store */
4343 if (crtc_lut->gamma_size != crtc->gamma_size) {
4344 ret = -EINVAL;
4345 goto out;
4346 }
4347
4348 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4349 r_base = crtc->gamma_store;
4350 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4351 ret = -EFAULT;
4352 goto out;
4353 }
4354
4355 g_base = r_base + size;
4356 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4357 ret = -EFAULT;
4358 goto out;
4359 }
4360
4361 b_base = g_base + size;
4362 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4363 ret = -EFAULT;
4364 goto out;
4365 }
4366out:
Daniel Vetter84849902012-12-02 00:28:11 +01004367 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004368 return ret;
4369}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004370
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004371/**
4372 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4373 * @dev: DRM device
4374 * @data: ioctl data
4375 * @file_priv: DRM file info
4376 *
4377 * This schedules an asynchronous update on a given CRTC, called page flip.
4378 * Optionally a drm event is generated to signal the completion of the event.
4379 * Generic drivers cannot assume that a pageflip with changed framebuffer
4380 * properties (including driver specific metadata like tiling layout) will work,
4381 * but some drivers support e.g. pixel format changes through the pageflip
4382 * ioctl.
4383 *
4384 * Called by the user via ioctl.
4385 *
4386 * Returns:
4387 * Zero on success, errno on failure.
4388 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004389int drm_mode_page_flip_ioctl(struct drm_device *dev,
4390 void *data, struct drm_file *file_priv)
4391{
4392 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004393 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004394 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004395 struct drm_pending_vblank_event *e = NULL;
4396 unsigned long flags;
4397 int ret = -EINVAL;
4398
4399 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4400 page_flip->reserved != 0)
4401 return -EINVAL;
4402
Keith Packard62f21042013-07-22 18:50:00 -07004403 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4404 return -EINVAL;
4405
Rob Clarka2b34e22013-10-05 16:36:52 -04004406 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4407 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004408 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004409
Rob Clark51fd3712013-11-19 12:10:12 -05004410 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004411 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004412 /* The framebuffer is currently unbound, presumably
4413 * due to a hotplug event, that userspace has not
4414 * yet discovered.
4415 */
4416 ret = -EBUSY;
4417 goto out;
4418 }
4419
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004420 if (crtc->funcs->page_flip == NULL)
4421 goto out;
4422
Daniel Vetter786b99e2012-12-02 21:53:40 +01004423 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004424 if (!fb) {
4425 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004426 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004427 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004428
Damien Lespiauc11e9282013-09-25 16:45:30 +01004429 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4430 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004431 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004432
Matt Roperf4510a22014-04-01 15:22:40 -07004433 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004434 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4435 ret = -EINVAL;
4436 goto out;
4437 }
4438
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004439 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4440 ret = -ENOMEM;
4441 spin_lock_irqsave(&dev->event_lock, flags);
4442 if (file_priv->event_space < sizeof e->event) {
4443 spin_unlock_irqrestore(&dev->event_lock, flags);
4444 goto out;
4445 }
4446 file_priv->event_space -= sizeof e->event;
4447 spin_unlock_irqrestore(&dev->event_lock, flags);
4448
4449 e = kzalloc(sizeof *e, GFP_KERNEL);
4450 if (e == NULL) {
4451 spin_lock_irqsave(&dev->event_lock, flags);
4452 file_priv->event_space += sizeof e->event;
4453 spin_unlock_irqrestore(&dev->event_lock, flags);
4454 goto out;
4455 }
4456
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004457 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004458 e->event.base.length = sizeof e->event;
4459 e->event.user_data = page_flip->user_data;
4460 e->base.event = &e->event.base;
4461 e->base.file_priv = file_priv;
4462 e->base.destroy =
4463 (void (*) (struct drm_pending_event *)) kfree;
4464 }
4465
Matt Roperf4510a22014-04-01 15:22:40 -07004466 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004467 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004468 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004469 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4470 spin_lock_irqsave(&dev->event_lock, flags);
4471 file_priv->event_space += sizeof e->event;
4472 spin_unlock_irqrestore(&dev->event_lock, flags);
4473 kfree(e);
4474 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004475 /* Keep the old fb, don't unref it. */
4476 old_fb = NULL;
4477 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004478 /*
4479 * Warn if the driver hasn't properly updated the crtc->fb
4480 * field to reflect that the new framebuffer is now used.
4481 * Failing to do so will screw with the reference counting
4482 * on framebuffers.
4483 */
Matt Roperf4510a22014-04-01 15:22:40 -07004484 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004485 /* Unref only the old framebuffer. */
4486 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004487 }
4488
4489out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004490 if (fb)
4491 drm_framebuffer_unreference(fb);
4492 if (old_fb)
4493 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004494 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004495
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004496 return ret;
4497}
Chris Wilsoneb033552011-01-24 15:11:08 +00004498
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004499/**
4500 * drm_mode_config_reset - call ->reset callbacks
4501 * @dev: drm device
4502 *
4503 * This functions calls all the crtc's, encoder's and connector's ->reset
4504 * callback. Drivers can use this in e.g. their driver load or resume code to
4505 * reset hardware and software state.
4506 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004507void drm_mode_config_reset(struct drm_device *dev)
4508{
4509 struct drm_crtc *crtc;
4510 struct drm_encoder *encoder;
4511 struct drm_connector *connector;
4512
4513 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4514 if (crtc->funcs->reset)
4515 crtc->funcs->reset(crtc);
4516
4517 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4518 if (encoder->funcs->reset)
4519 encoder->funcs->reset(encoder);
4520
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004521 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4522 connector->status = connector_status_unknown;
4523
Chris Wilsoneb033552011-01-24 15:11:08 +00004524 if (connector->funcs->reset)
4525 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004526 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004527}
4528EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004529
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004530/**
4531 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4532 * @dev: DRM device
4533 * @data: ioctl data
4534 * @file_priv: DRM file info
4535 *
4536 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4537 * TTM or something else entirely) and returns the resulting buffer handle. This
4538 * handle can then be wrapped up into a framebuffer modeset object.
4539 *
4540 * Note that userspace is not allowed to use such objects for render
4541 * acceleration - drivers must create their own private ioctls for such a use
4542 * case.
4543 *
4544 * Called by the user via ioctl.
4545 *
4546 * Returns:
4547 * Zero on success, errno on failure.
4548 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004549int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4550 void *data, struct drm_file *file_priv)
4551{
4552 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004553 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004554
4555 if (!dev->driver->dumb_create)
4556 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004557 if (!args->width || !args->height || !args->bpp)
4558 return -EINVAL;
4559
4560 /* overflow checks for 32bit size calculations */
4561 cpp = DIV_ROUND_UP(args->bpp, 8);
4562 if (cpp > 0xffffffffU / args->width)
4563 return -EINVAL;
4564 stride = cpp * args->width;
4565 if (args->height > 0xffffffffU / stride)
4566 return -EINVAL;
4567
4568 /* test for wrap-around */
4569 size = args->height * stride;
4570 if (PAGE_ALIGN(size) == 0)
4571 return -EINVAL;
4572
Dave Airlieff72145b2011-02-07 12:16:14 +10004573 return dev->driver->dumb_create(file_priv, dev, args);
4574}
4575
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004576/**
4577 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4578 * @dev: DRM device
4579 * @data: ioctl data
4580 * @file_priv: DRM file info
4581 *
4582 * Allocate an offset in the drm device node's address space to be able to
4583 * memory map a dumb buffer.
4584 *
4585 * Called by the user via ioctl.
4586 *
4587 * Returns:
4588 * Zero on success, errno on failure.
4589 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004590int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4591 void *data, struct drm_file *file_priv)
4592{
4593 struct drm_mode_map_dumb *args = data;
4594
4595 /* call driver ioctl to get mmap offset */
4596 if (!dev->driver->dumb_map_offset)
4597 return -ENOSYS;
4598
4599 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4600}
4601
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004602/**
4603 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4604 * @dev: DRM device
4605 * @data: ioctl data
4606 * @file_priv: DRM file info
4607 *
4608 * This destroys the userspace handle for the given dumb backing storage buffer.
4609 * Since buffer objects must be reference counted in the kernel a buffer object
4610 * won't be immediately freed if a framebuffer modeset object still uses it.
4611 *
4612 * Called by the user via ioctl.
4613 *
4614 * Returns:
4615 * Zero on success, errno on failure.
4616 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004617int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4618 void *data, struct drm_file *file_priv)
4619{
4620 struct drm_mode_destroy_dumb *args = data;
4621
4622 if (!dev->driver->dumb_destroy)
4623 return -ENOSYS;
4624
4625 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4626}
Dave Airlie248dbc22011-11-29 20:02:54 +00004627
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004628/**
4629 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4630 * @format: pixel format (DRM_FORMAT_*)
4631 * @depth: storage for the depth value
4632 * @bpp: storage for the bpp value
4633 *
4634 * This only supports RGB formats here for compat with code that doesn't use
4635 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004636 */
4637void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4638 int *bpp)
4639{
4640 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004641 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004642 case DRM_FORMAT_RGB332:
4643 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004644 *depth = 8;
4645 *bpp = 8;
4646 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004647 case DRM_FORMAT_XRGB1555:
4648 case DRM_FORMAT_XBGR1555:
4649 case DRM_FORMAT_RGBX5551:
4650 case DRM_FORMAT_BGRX5551:
4651 case DRM_FORMAT_ARGB1555:
4652 case DRM_FORMAT_ABGR1555:
4653 case DRM_FORMAT_RGBA5551:
4654 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004655 *depth = 15;
4656 *bpp = 16;
4657 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004658 case DRM_FORMAT_RGB565:
4659 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004660 *depth = 16;
4661 *bpp = 16;
4662 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004663 case DRM_FORMAT_RGB888:
4664 case DRM_FORMAT_BGR888:
4665 *depth = 24;
4666 *bpp = 24;
4667 break;
4668 case DRM_FORMAT_XRGB8888:
4669 case DRM_FORMAT_XBGR8888:
4670 case DRM_FORMAT_RGBX8888:
4671 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004672 *depth = 24;
4673 *bpp = 32;
4674 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004675 case DRM_FORMAT_XRGB2101010:
4676 case DRM_FORMAT_XBGR2101010:
4677 case DRM_FORMAT_RGBX1010102:
4678 case DRM_FORMAT_BGRX1010102:
4679 case DRM_FORMAT_ARGB2101010:
4680 case DRM_FORMAT_ABGR2101010:
4681 case DRM_FORMAT_RGBA1010102:
4682 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004683 *depth = 30;
4684 *bpp = 32;
4685 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004686 case DRM_FORMAT_ARGB8888:
4687 case DRM_FORMAT_ABGR8888:
4688 case DRM_FORMAT_RGBA8888:
4689 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004690 *depth = 32;
4691 *bpp = 32;
4692 break;
4693 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004694 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4695 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004696 *depth = 0;
4697 *bpp = 0;
4698 break;
4699 }
4700}
4701EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004702
4703/**
4704 * drm_format_num_planes - get the number of planes for format
4705 * @format: pixel format (DRM_FORMAT_*)
4706 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004707 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004708 * The number of planes used by the specified pixel format.
4709 */
4710int drm_format_num_planes(uint32_t format)
4711{
4712 switch (format) {
4713 case DRM_FORMAT_YUV410:
4714 case DRM_FORMAT_YVU410:
4715 case DRM_FORMAT_YUV411:
4716 case DRM_FORMAT_YVU411:
4717 case DRM_FORMAT_YUV420:
4718 case DRM_FORMAT_YVU420:
4719 case DRM_FORMAT_YUV422:
4720 case DRM_FORMAT_YVU422:
4721 case DRM_FORMAT_YUV444:
4722 case DRM_FORMAT_YVU444:
4723 return 3;
4724 case DRM_FORMAT_NV12:
4725 case DRM_FORMAT_NV21:
4726 case DRM_FORMAT_NV16:
4727 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004728 case DRM_FORMAT_NV24:
4729 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004730 return 2;
4731 default:
4732 return 1;
4733 }
4734}
4735EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004736
4737/**
4738 * drm_format_plane_cpp - determine the bytes per pixel value
4739 * @format: pixel format (DRM_FORMAT_*)
4740 * @plane: plane index
4741 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004742 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004743 * The bytes per pixel value for the specified plane.
4744 */
4745int drm_format_plane_cpp(uint32_t format, int plane)
4746{
4747 unsigned int depth;
4748 int bpp;
4749
4750 if (plane >= drm_format_num_planes(format))
4751 return 0;
4752
4753 switch (format) {
4754 case DRM_FORMAT_YUYV:
4755 case DRM_FORMAT_YVYU:
4756 case DRM_FORMAT_UYVY:
4757 case DRM_FORMAT_VYUY:
4758 return 2;
4759 case DRM_FORMAT_NV12:
4760 case DRM_FORMAT_NV21:
4761 case DRM_FORMAT_NV16:
4762 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004763 case DRM_FORMAT_NV24:
4764 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004765 return plane ? 2 : 1;
4766 case DRM_FORMAT_YUV410:
4767 case DRM_FORMAT_YVU410:
4768 case DRM_FORMAT_YUV411:
4769 case DRM_FORMAT_YVU411:
4770 case DRM_FORMAT_YUV420:
4771 case DRM_FORMAT_YVU420:
4772 case DRM_FORMAT_YUV422:
4773 case DRM_FORMAT_YVU422:
4774 case DRM_FORMAT_YUV444:
4775 case DRM_FORMAT_YVU444:
4776 return 1;
4777 default:
4778 drm_fb_get_bpp_depth(format, &depth, &bpp);
4779 return bpp >> 3;
4780 }
4781}
4782EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004783
4784/**
4785 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4786 * @format: pixel format (DRM_FORMAT_*)
4787 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004788 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004789 * The horizontal chroma subsampling factor for the
4790 * specified pixel format.
4791 */
4792int drm_format_horz_chroma_subsampling(uint32_t format)
4793{
4794 switch (format) {
4795 case DRM_FORMAT_YUV411:
4796 case DRM_FORMAT_YVU411:
4797 case DRM_FORMAT_YUV410:
4798 case DRM_FORMAT_YVU410:
4799 return 4;
4800 case DRM_FORMAT_YUYV:
4801 case DRM_FORMAT_YVYU:
4802 case DRM_FORMAT_UYVY:
4803 case DRM_FORMAT_VYUY:
4804 case DRM_FORMAT_NV12:
4805 case DRM_FORMAT_NV21:
4806 case DRM_FORMAT_NV16:
4807 case DRM_FORMAT_NV61:
4808 case DRM_FORMAT_YUV422:
4809 case DRM_FORMAT_YVU422:
4810 case DRM_FORMAT_YUV420:
4811 case DRM_FORMAT_YVU420:
4812 return 2;
4813 default:
4814 return 1;
4815 }
4816}
4817EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4818
4819/**
4820 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4821 * @format: pixel format (DRM_FORMAT_*)
4822 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004823 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004824 * The vertical chroma subsampling factor for the
4825 * specified pixel format.
4826 */
4827int drm_format_vert_chroma_subsampling(uint32_t format)
4828{
4829 switch (format) {
4830 case DRM_FORMAT_YUV410:
4831 case DRM_FORMAT_YVU410:
4832 return 4;
4833 case DRM_FORMAT_YUV420:
4834 case DRM_FORMAT_YVU420:
4835 case DRM_FORMAT_NV12:
4836 case DRM_FORMAT_NV21:
4837 return 2;
4838 default:
4839 return 1;
4840 }
4841}
4842EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004843
4844/**
4845 * drm_mode_config_init - initialize DRM mode_configuration structure
4846 * @dev: DRM device
4847 *
4848 * Initialize @dev's mode_config structure, used for tracking the graphics
4849 * configuration of @dev.
4850 *
4851 * Since this initializes the modeset locks, no locking is possible. Which is no
4852 * problem, since this should happen single threaded at init time. It is the
4853 * driver's problem to ensure this guarantee.
4854 *
4855 */
4856void drm_mode_config_init(struct drm_device *dev)
4857{
4858 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004859 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004860 mutex_init(&dev->mode_config.idr_mutex);
4861 mutex_init(&dev->mode_config.fb_lock);
4862 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4863 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4864 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004865 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004866 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4867 INIT_LIST_HEAD(&dev->mode_config.property_list);
4868 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4869 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4870 idr_init(&dev->mode_config.crtc_idr);
4871
4872 drm_modeset_lock_all(dev);
4873 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004874 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004875 drm_modeset_unlock_all(dev);
4876
4877 /* Just to be sure */
4878 dev->mode_config.num_fb = 0;
4879 dev->mode_config.num_connector = 0;
4880 dev->mode_config.num_crtc = 0;
4881 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004882 dev->mode_config.num_overlay_plane = 0;
4883 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004884}
4885EXPORT_SYMBOL(drm_mode_config_init);
4886
4887/**
4888 * drm_mode_config_cleanup - free up DRM mode_config info
4889 * @dev: DRM device
4890 *
4891 * Free up all the connectors and CRTCs associated with this DRM device, then
4892 * free up the framebuffers and associated buffer objects.
4893 *
4894 * Note that since this /should/ happen single-threaded at driver/device
4895 * teardown time, no locking is required. It's the driver's job to ensure that
4896 * this guarantee actually holds true.
4897 *
4898 * FIXME: cleanup any dangling user buffer objects too
4899 */
4900void drm_mode_config_cleanup(struct drm_device *dev)
4901{
4902 struct drm_connector *connector, *ot;
4903 struct drm_crtc *crtc, *ct;
4904 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004905 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004906 struct drm_framebuffer *fb, *fbt;
4907 struct drm_property *property, *pt;
4908 struct drm_property_blob *blob, *bt;
4909 struct drm_plane *plane, *plt;
4910
4911 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4912 head) {
4913 encoder->funcs->destroy(encoder);
4914 }
4915
Sean Paul3b336ec2013-08-14 16:47:37 -04004916 list_for_each_entry_safe(bridge, brt,
4917 &dev->mode_config.bridge_list, head) {
4918 bridge->funcs->destroy(bridge);
4919 }
4920
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004921 list_for_each_entry_safe(connector, ot,
4922 &dev->mode_config.connector_list, head) {
4923 connector->funcs->destroy(connector);
4924 }
4925
4926 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4927 head) {
4928 drm_property_destroy(dev, property);
4929 }
4930
4931 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4932 head) {
4933 drm_property_destroy_blob(dev, blob);
4934 }
4935
4936 /*
4937 * Single-threaded teardown context, so it's not required to grab the
4938 * fb_lock to protect against concurrent fb_list access. Contrary, it
4939 * would actually deadlock with the drm_framebuffer_cleanup function.
4940 *
4941 * Also, if there are any framebuffers left, that's a driver leak now,
4942 * so politely WARN about this.
4943 */
4944 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4945 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4946 drm_framebuffer_remove(fb);
4947 }
4948
4949 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4950 head) {
4951 plane->funcs->destroy(plane);
4952 }
4953
4954 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4955 crtc->funcs->destroy(crtc);
4956 }
4957
4958 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05004959 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004960}
4961EXPORT_SYMBOL(drm_mode_config_cleanup);