blob: c3a5a8f0825b3a073dc5b368dbd69c109b9c37cb [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 Airlie2ee39452014-07-24 11:53:45 +1000364/*
365 * Internal function to assign a slot in the object idr and optionally
366 * register the object into the idr.
367 */
368static int drm_mode_object_get_reg(struct drm_device *dev,
369 struct drm_mode_object *obj,
370 uint32_t obj_type,
371 bool register_obj)
372{
373 int ret;
374
375 mutex_lock(&dev->mode_config.idr_mutex);
376 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
377 if (ret >= 0) {
378 /*
379 * Set up the object linking under the protection of the idr
380 * lock so that other users can't see inconsistent state.
381 */
382 obj->id = ret;
383 obj->type = obj_type;
384 }
385 mutex_unlock(&dev->mode_config.idr_mutex);
386
387 return ret < 0 ? ret : 0;
388}
389
Dave Airlief453ba02008-11-07 14:05:41 -0800390/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100391 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800392 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100393 * @obj: object pointer, used to generate unique ID
394 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800395 *
Dave Airlief453ba02008-11-07 14:05:41 -0800396 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100397 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
398 * modeset identifiers are _not_ reference counted. Hence don't use this for
399 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800400 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100401 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800402 * New unique (relative to other objects in @dev) integer identifier for the
403 * object.
404 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100405int drm_mode_object_get(struct drm_device *dev,
406 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800407{
Dave Airlie2ee39452014-07-24 11:53:45 +1000408 return drm_mode_object_get_reg(dev, obj, obj_type, true);
409}
Dave Airlief453ba02008-11-07 14:05:41 -0800410
Dave Airlie2ee39452014-07-24 11:53:45 +1000411static void drm_mode_object_register(struct drm_device *dev,
412 struct drm_mode_object *obj)
413{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000414 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000415 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000416 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800417}
418
419/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100420 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800421 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100422 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800423 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100424 * Free @id from @dev's unique identifier pool. Note that despite the _get
425 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
426 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800427 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100428void drm_mode_object_put(struct drm_device *dev,
429 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800430{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000431 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800432 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000433 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800434}
435
Rob Clark98f75de2014-05-30 11:37:03 -0400436static struct drm_mode_object *_object_find(struct drm_device *dev,
437 uint32_t id, uint32_t type)
438{
439 struct drm_mode_object *obj = NULL;
440
441 mutex_lock(&dev->mode_config.idr_mutex);
442 obj = idr_find(&dev->mode_config.crtc_idr, id);
443 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
444 (obj->id != id))
445 obj = NULL;
446 mutex_unlock(&dev->mode_config.idr_mutex);
447
448 return obj;
449}
450
Daniel Vetter786b99e2012-12-02 21:53:40 +0100451/**
452 * drm_mode_object_find - look up a drm object with static lifetime
453 * @dev: drm device
454 * @id: id of the mode object
455 * @type: type of the mode object
456 *
457 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400458 * are reference counted, they need special treatment. Even with
459 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
460 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100461 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200462struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
463 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800464{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000465 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800466
Daniel Vetter786b99e2012-12-02 21:53:40 +0100467 /* Framebuffers are reference counted and need their own lookup
468 * function.*/
469 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400470 obj = _object_find(dev, id, type);
471 /* don't leak out unref'd fb's */
472 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000473 obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800474 return obj;
475}
476EXPORT_SYMBOL(drm_mode_object_find);
477
478/**
Dave Airlief453ba02008-11-07 14:05:41 -0800479 * drm_framebuffer_init - initialize a framebuffer
480 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100481 * @fb: framebuffer to be initialized
482 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800483 *
Dave Airlief453ba02008-11-07 14:05:41 -0800484 * Allocates an ID for the framebuffer's parent mode object, sets its mode
485 * functions & device file and adds it to the master fd list.
486 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100487 * IMPORTANT:
488 * This functions publishes the fb and makes it available for concurrent access
489 * by other users. Which means by this point the fb _must_ be fully set up -
490 * since all the fb attributes are invariant over its lifetime, no further
491 * locking but only correct reference counting is required.
492 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100493 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200494 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800495 */
496int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
497 const struct drm_framebuffer_funcs *funcs)
498{
499 int ret;
500
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100501 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000502 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100503 INIT_LIST_HEAD(&fb->filp_head);
504 fb->dev = dev;
505 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000506
Dave Airlief453ba02008-11-07 14:05:41 -0800507 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200508 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100509 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800510
Daniel Vetter2b677e82012-12-10 21:16:05 +0100511 /* Grab the idr reference. */
512 drm_framebuffer_reference(fb);
513
Dave Airlief453ba02008-11-07 14:05:41 -0800514 dev->mode_config.num_fb++;
515 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100516out:
517 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800518
519 return 0;
520}
521EXPORT_SYMBOL(drm_framebuffer_init);
522
Rob Clarkf7eff602012-09-05 21:48:38 +0000523static void drm_framebuffer_free(struct kref *kref)
524{
525 struct drm_framebuffer *fb =
526 container_of(kref, struct drm_framebuffer, refcount);
527 fb->funcs->destroy(fb);
528}
529
Daniel Vetter2b677e82012-12-10 21:16:05 +0100530static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
531 uint32_t id)
532{
533 struct drm_mode_object *obj = NULL;
534 struct drm_framebuffer *fb;
535
536 mutex_lock(&dev->mode_config.idr_mutex);
537 obj = idr_find(&dev->mode_config.crtc_idr, id);
538 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
539 fb = NULL;
540 else
541 fb = obj_to_fb(obj);
542 mutex_unlock(&dev->mode_config.idr_mutex);
543
544 return fb;
545}
546
Rob Clarkf7eff602012-09-05 21:48:38 +0000547/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100548 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
549 * @dev: drm device
550 * @id: id of the fb object
551 *
552 * If successful, this grabs an additional reference to the framebuffer -
553 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100554 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100555 */
556struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
557 uint32_t id)
558{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100559 struct drm_framebuffer *fb;
560
561 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100562 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100563 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000564 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100565 mutex_unlock(&dev->mode_config.fb_lock);
566
567 return fb;
568}
569EXPORT_SYMBOL(drm_framebuffer_lookup);
570
571/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000572 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100573 * @fb: framebuffer to unref
574 *
575 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000576 */
577void drm_framebuffer_unreference(struct drm_framebuffer *fb)
578{
Rob Clark82912722014-03-18 10:07:08 -0400579 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000580 kref_put(&fb->refcount, drm_framebuffer_free);
581}
582EXPORT_SYMBOL(drm_framebuffer_unreference);
583
584/**
585 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100586 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100587 *
588 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000589 */
590void drm_framebuffer_reference(struct drm_framebuffer *fb)
591{
Rob Clark82912722014-03-18 10:07:08 -0400592 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000593 kref_get(&fb->refcount);
594}
595EXPORT_SYMBOL(drm_framebuffer_reference);
596
Daniel Vetter2b677e82012-12-10 21:16:05 +0100597static void drm_framebuffer_free_bug(struct kref *kref)
598{
599 BUG();
600}
601
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100602static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
603{
Rob Clark82912722014-03-18 10:07:08 -0400604 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100605 kref_put(&fb->refcount, drm_framebuffer_free_bug);
606}
607
Daniel Vetter2b677e82012-12-10 21:16:05 +0100608/* dev->mode_config.fb_lock must be held! */
609static void __drm_framebuffer_unregister(struct drm_device *dev,
610 struct drm_framebuffer *fb)
611{
612 mutex_lock(&dev->mode_config.idr_mutex);
613 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
614 mutex_unlock(&dev->mode_config.idr_mutex);
615
616 fb->base.id = 0;
617
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100618 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100619}
620
Dave Airlief453ba02008-11-07 14:05:41 -0800621/**
Daniel Vetter36206362012-12-10 20:42:17 +0100622 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
623 * @fb: fb to unregister
624 *
625 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
626 * those used for fbdev. Note that the caller must hold a reference of it's own,
627 * i.e. the object may not be destroyed through this call (since it'll lead to a
628 * locking inversion).
629 */
630void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
631{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100632 struct drm_device *dev = fb->dev;
633
634 mutex_lock(&dev->mode_config.fb_lock);
635 /* Mark fb as reaped and drop idr ref. */
636 __drm_framebuffer_unregister(dev, fb);
637 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100638}
639EXPORT_SYMBOL(drm_framebuffer_unregister_private);
640
641/**
Dave Airlief453ba02008-11-07 14:05:41 -0800642 * drm_framebuffer_cleanup - remove a framebuffer object
643 * @fb: framebuffer to remove
644 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100645 * Cleanup framebuffer. This function is intended to be used from the drivers
646 * ->destroy callback. It can also be used to clean up driver private
647 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100648 *
649 * Note that this function does not remove the fb from active usuage - if it is
650 * still used anywhere, hilarity can ensue since userspace could call getfb on
651 * the id and get back -EINVAL. Obviously no concern at driver unload time.
652 *
653 * Also, the framebuffer will not be removed from the lookup idr - for
654 * user-created framebuffers this will happen in in the rmfb ioctl. For
655 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
656 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800657 */
658void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
659{
660 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100661
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100662 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000663 list_del(&fb->head);
664 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100665 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000666}
667EXPORT_SYMBOL(drm_framebuffer_cleanup);
668
669/**
670 * drm_framebuffer_remove - remove and unreference a framebuffer object
671 * @fb: framebuffer to remove
672 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000673 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100674 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100675 * passed-in framebuffer. Might take the modeset locks.
676 *
677 * Note that this function optimizes the cleanup away if the caller holds the
678 * last reference to the framebuffer. It is also guaranteed to not take the
679 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000680 */
681void drm_framebuffer_remove(struct drm_framebuffer *fb)
682{
683 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800684 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800685 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000686 struct drm_mode_set set;
687 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800688
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100689 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100690
Daniel Vetterb62584e2012-12-11 16:51:35 +0100691 /*
692 * drm ABI mandates that we remove any deleted framebuffers from active
693 * useage. But since most sane clients only remove framebuffers they no
694 * longer need, try to optimize this away.
695 *
696 * Since we're holding a reference ourselves, observing a refcount of 1
697 * means that we're the last holder and can skip it. Also, the refcount
698 * can never increase from 1 again, so we don't need any barriers or
699 * locks.
700 *
701 * Note that userspace could try to race with use and instate a new
702 * usage _after_ we've cleared all current ones. End result will be an
703 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
704 * in this manner.
705 */
706 if (atomic_read(&fb->refcount.refcount) > 1) {
707 drm_modeset_lock_all(dev);
708 /* remove from any CRTC */
709 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700710 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100711 /* should turn off the crtc */
712 memset(&set, 0, sizeof(struct drm_mode_set));
713 set.crtc = crtc;
714 set.fb = NULL;
715 ret = drm_mode_set_config_internal(&set);
716 if (ret)
717 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
718 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000719 }
Dave Airlief453ba02008-11-07 14:05:41 -0800720
Daniel Vetterb62584e2012-12-11 16:51:35 +0100721 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300722 if (plane->fb == fb)
723 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800724 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100725 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800726 }
727
Rob Clarkf7eff602012-09-05 21:48:38 +0000728 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800729}
Rob Clarkf7eff602012-09-05 21:48:38 +0000730EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800731
Rob Clark51fd3712013-11-19 12:10:12 -0500732DEFINE_WW_CLASS(crtc_ww_class);
733
Dave Airlief453ba02008-11-07 14:05:41 -0800734/**
Matt Ropere13161a2014-04-01 15:22:38 -0700735 * drm_crtc_init_with_planes - Initialise a new CRTC object with
736 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800737 * @dev: DRM device
738 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700739 * @primary: Primary plane for CRTC
740 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800741 * @funcs: callbacks for the new CRTC
742 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000743 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200744 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100745 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200746 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800747 */
Matt Ropere13161a2014-04-01 15:22:38 -0700748int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
749 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700750 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700751 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800752{
Rob Clark51fd3712013-11-19 12:10:12 -0500753 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200754 int ret;
755
Dave Airlief453ba02008-11-07 14:05:41 -0800756 crtc->dev = dev;
757 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000758 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800759
Daniel Vetter84849902012-12-02 00:28:11 +0100760 drm_modeset_lock_all(dev);
Rob Clark51fd3712013-11-19 12:10:12 -0500761 drm_modeset_lock_init(&crtc->mutex);
762 /* dropped by _unlock_all(): */
763 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200764
765 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
766 if (ret)
767 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800768
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300769 crtc->base.properties = &crtc->properties;
770
Rob Clark51fd3712013-11-19 12:10:12 -0500771 list_add_tail(&crtc->head, &config->crtc_list);
772 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200773
Matt Ropere13161a2014-04-01 15:22:38 -0700774 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700775 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700776 if (primary)
777 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700778 if (cursor)
779 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700780
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200781 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100782 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200783
784 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800785}
Matt Ropere13161a2014-04-01 15:22:38 -0700786EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800787
788/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000789 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800790 * @crtc: CRTC to cleanup
791 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000792 * This function cleans up @crtc and removes it from the DRM mode setting
793 * core. Note that the function does *not* free the crtc structure itself,
794 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800795 */
796void drm_crtc_cleanup(struct drm_crtc *crtc)
797{
798 struct drm_device *dev = crtc->dev;
799
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000800 kfree(crtc->gamma_store);
801 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800802
Rob Clark51fd3712013-11-19 12:10:12 -0500803 drm_modeset_lock_fini(&crtc->mutex);
804
Dave Airlief453ba02008-11-07 14:05:41 -0800805 drm_mode_object_put(dev, &crtc->base);
806 list_del(&crtc->head);
807 dev->mode_config.num_crtc--;
808}
809EXPORT_SYMBOL(drm_crtc_cleanup);
810
811/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000812 * drm_crtc_index - find the index of a registered CRTC
813 * @crtc: CRTC to find index for
814 *
815 * Given a registered CRTC, return the index of that CRTC within a DRM
816 * device's list of CRTCs.
817 */
818unsigned int drm_crtc_index(struct drm_crtc *crtc)
819{
820 unsigned int index = 0;
821 struct drm_crtc *tmp;
822
823 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
824 if (tmp == crtc)
825 return index;
826
827 index++;
828 }
829
830 BUG();
831}
832EXPORT_SYMBOL(drm_crtc_index);
833
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100834/*
Dave Airlief453ba02008-11-07 14:05:41 -0800835 * drm_mode_remove - remove and free a mode
836 * @connector: connector list to modify
837 * @mode: mode to remove
838 *
Dave Airlief453ba02008-11-07 14:05:41 -0800839 * Remove @mode from @connector's mode list, then free it.
840 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100841static void drm_mode_remove(struct drm_connector *connector,
842 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800843{
844 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100845 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800846}
Dave Airlief453ba02008-11-07 14:05:41 -0800847
848/**
849 * drm_connector_init - Init a preallocated connector
850 * @dev: DRM device
851 * @connector: the connector to init
852 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100853 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800854 *
Dave Airlief453ba02008-11-07 14:05:41 -0800855 * Initialises a preallocated connector. Connectors should be
856 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200857 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100858 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200859 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800860 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200861int drm_connector_init(struct drm_device *dev,
862 struct drm_connector *connector,
863 const struct drm_connector_funcs *funcs,
864 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800865{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200866 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400867 struct ida *connector_ida =
868 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200869
Daniel Vetter84849902012-12-02 00:28:11 +0100870 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800871
Dave Airlie2ee39452014-07-24 11:53:45 +1000872 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200873 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300874 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200875
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300876 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800877 connector->dev = dev;
878 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800879 connector->connector_type = connector_type;
880 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400881 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
882 if (connector->connector_type_id < 0) {
883 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300884 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400885 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300886 connector->name =
887 kasprintf(GFP_KERNEL, "%s-%d",
888 drm_connector_enum_list[connector_type].name,
889 connector->connector_type_id);
890 if (!connector->name) {
891 ret = -ENOMEM;
892 goto out_put;
893 }
894
Dave Airlief453ba02008-11-07 14:05:41 -0800895 INIT_LIST_HEAD(&connector->probed_modes);
896 INIT_LIST_HEAD(&connector->modes);
897 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000898 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800899
900 list_add_tail(&connector->head, &dev->mode_config.connector_list);
901 dev->mode_config.num_connector++;
902
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200903 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500904 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200905 dev->mode_config.edid_property,
906 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800907
Rob Clark58495562012-10-11 20:50:56 -0500908 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800909 dev->mode_config.dpms_property, 0);
910
Thomas Wood30f65702014-06-18 17:52:32 +0100911 connector->debugfs_entry = NULL;
912
Jani Nikula2abdd312014-05-14 16:58:19 +0300913out_put:
914 if (ret)
915 drm_mode_object_put(dev, &connector->base);
916
917out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100918 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200919
920 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800921}
922EXPORT_SYMBOL(drm_connector_init);
923
924/**
925 * drm_connector_cleanup - cleans up an initialised connector
926 * @connector: connector to cleanup
927 *
Dave Airlief453ba02008-11-07 14:05:41 -0800928 * Cleans up the connector but doesn't free the object.
929 */
930void drm_connector_cleanup(struct drm_connector *connector)
931{
932 struct drm_device *dev = connector->dev;
933 struct drm_display_mode *mode, *t;
934
935 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
936 drm_mode_remove(connector, mode);
937
938 list_for_each_entry_safe(mode, t, &connector->modes, head)
939 drm_mode_remove(connector, mode);
940
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400941 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
942 connector->connector_type_id);
943
Dave Airlief453ba02008-11-07 14:05:41 -0800944 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300945 kfree(connector->name);
946 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800947 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000948 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800949}
950EXPORT_SYMBOL(drm_connector_cleanup);
951
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100952/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100953 * drm_connector_register - register a connector
954 * @connector: the connector to register
955 *
956 * Register userspace interfaces for a connector
957 *
958 * Returns:
959 * Zero on success, error code on failure.
960 */
961int drm_connector_register(struct drm_connector *connector)
962{
Thomas Wood30f65702014-06-18 17:52:32 +0100963 int ret;
964
Dave Airlie2ee39452014-07-24 11:53:45 +1000965 drm_mode_object_register(connector->dev, &connector->base);
966
Thomas Wood30f65702014-06-18 17:52:32 +0100967 ret = drm_sysfs_connector_add(connector);
968 if (ret)
969 return ret;
970
971 ret = drm_debugfs_connector_add(connector);
972 if (ret) {
973 drm_sysfs_connector_remove(connector);
974 return ret;
975 }
976
977 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100978}
979EXPORT_SYMBOL(drm_connector_register);
980
981/**
982 * drm_connector_unregister - unregister a connector
983 * @connector: the connector to unregister
984 *
985 * Unregister userspace interfaces for a connector
986 */
987void drm_connector_unregister(struct drm_connector *connector)
988{
989 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +0100990 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100991}
992EXPORT_SYMBOL(drm_connector_unregister);
993
994
995/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100996 * drm_connector_unplug_all - unregister connector userspace interfaces
997 * @dev: drm device
998 *
999 * This function unregisters all connector userspace interfaces in sysfs. Should
1000 * be call when the device is disconnected, e.g. from an usb driver's
1001 * ->disconnect callback.
1002 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001003void drm_connector_unplug_all(struct drm_device *dev)
1004{
1005 struct drm_connector *connector;
1006
1007 /* taking the mode config mutex ends up in a clash with sysfs */
1008 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001009 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001010
1011}
1012EXPORT_SYMBOL(drm_connector_unplug_all);
1013
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001014/**
1015 * drm_bridge_init - initialize a drm transcoder/bridge
1016 * @dev: drm device
1017 * @bridge: transcoder/bridge to set up
1018 * @funcs: bridge function table
1019 *
1020 * Initialises a preallocated bridge. Bridges should be
1021 * subclassed as part of driver connector objects.
1022 *
1023 * Returns:
1024 * Zero on success, error code on failure.
1025 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001026int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1027 const struct drm_bridge_funcs *funcs)
1028{
1029 int ret;
1030
1031 drm_modeset_lock_all(dev);
1032
1033 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1034 if (ret)
1035 goto out;
1036
1037 bridge->dev = dev;
1038 bridge->funcs = funcs;
1039
1040 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1041 dev->mode_config.num_bridge++;
1042
1043 out:
1044 drm_modeset_unlock_all(dev);
1045 return ret;
1046}
1047EXPORT_SYMBOL(drm_bridge_init);
1048
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001049/**
1050 * drm_bridge_cleanup - cleans up an initialised bridge
1051 * @bridge: bridge to cleanup
1052 *
1053 * Cleans up the bridge but doesn't free the object.
1054 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001055void drm_bridge_cleanup(struct drm_bridge *bridge)
1056{
1057 struct drm_device *dev = bridge->dev;
1058
1059 drm_modeset_lock_all(dev);
1060 drm_mode_object_put(dev, &bridge->base);
1061 list_del(&bridge->head);
1062 dev->mode_config.num_bridge--;
1063 drm_modeset_unlock_all(dev);
1064}
1065EXPORT_SYMBOL(drm_bridge_cleanup);
1066
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001067/**
1068 * drm_encoder_init - Init a preallocated encoder
1069 * @dev: drm device
1070 * @encoder: the encoder to init
1071 * @funcs: callbacks for this encoder
1072 * @encoder_type: user visible type of the encoder
1073 *
1074 * Initialises a preallocated encoder. Encoder should be
1075 * subclassed as part of driver encoder objects.
1076 *
1077 * Returns:
1078 * Zero on success, error code on failure.
1079 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001080int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001081 struct drm_encoder *encoder,
1082 const struct drm_encoder_funcs *funcs,
1083 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001084{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001085 int ret;
1086
Daniel Vetter84849902012-12-02 00:28:11 +01001087 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001088
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001089 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1090 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001091 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001092
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001093 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001094 encoder->encoder_type = encoder_type;
1095 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001096 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1097 drm_encoder_enum_list[encoder_type].name,
1098 encoder->base.id);
1099 if (!encoder->name) {
1100 ret = -ENOMEM;
1101 goto out_put;
1102 }
Dave Airlief453ba02008-11-07 14:05:41 -08001103
1104 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1105 dev->mode_config.num_encoder++;
1106
Jani Nikulae5748942014-05-14 16:58:20 +03001107out_put:
1108 if (ret)
1109 drm_mode_object_put(dev, &encoder->base);
1110
1111out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001112 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001113
1114 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001115}
1116EXPORT_SYMBOL(drm_encoder_init);
1117
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001118/**
1119 * drm_encoder_cleanup - cleans up an initialised encoder
1120 * @encoder: encoder to cleanup
1121 *
1122 * Cleans up the encoder but doesn't free the object.
1123 */
Dave Airlief453ba02008-11-07 14:05:41 -08001124void drm_encoder_cleanup(struct drm_encoder *encoder)
1125{
1126 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001127 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001128 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001129 kfree(encoder->name);
1130 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001131 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001132 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001133 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001134}
1135EXPORT_SYMBOL(drm_encoder_cleanup);
1136
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001137/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001138 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001139 * @dev: DRM device
1140 * @plane: plane object to init
1141 * @possible_crtcs: bitmask of possible CRTCs
1142 * @funcs: callbacks for the new plane
1143 * @formats: array of supported formats (%DRM_FORMAT_*)
1144 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001145 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001146 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001147 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001148 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001149 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001150 * Zero on success, error code on failure.
1151 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001152int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1153 unsigned long possible_crtcs,
1154 const struct drm_plane_funcs *funcs,
1155 const uint32_t *formats, uint32_t format_count,
1156 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001157{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001158 int ret;
1159
Daniel Vetter84849902012-12-02 00:28:11 +01001160 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001161
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001162 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1163 if (ret)
1164 goto out;
1165
Rob Clark4d939142012-05-17 02:23:27 -06001166 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001167 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001168 plane->funcs = funcs;
1169 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1170 GFP_KERNEL);
1171 if (!plane->format_types) {
1172 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1173 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001174 ret = -ENOMEM;
1175 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001176 }
1177
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001178 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001179 plane->format_count = format_count;
1180 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001181 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001182
Matt Roperdc415ff2014-04-01 15:22:36 -07001183 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1184 dev->mode_config.num_total_plane++;
1185 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1186 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001187
Rob Clark9922ab52014-04-01 20:16:57 -04001188 drm_object_attach_property(&plane->base,
1189 dev->mode_config.plane_type_property,
1190 plane->type);
1191
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001192 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001193 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001194
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001195 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001196}
Matt Roperdc415ff2014-04-01 15:22:36 -07001197EXPORT_SYMBOL(drm_universal_plane_init);
1198
1199/**
1200 * drm_plane_init - Initialize a legacy plane
1201 * @dev: DRM device
1202 * @plane: plane object to init
1203 * @possible_crtcs: bitmask of possible CRTCs
1204 * @funcs: callbacks for the new plane
1205 * @formats: array of supported formats (%DRM_FORMAT_*)
1206 * @format_count: number of elements in @formats
1207 * @is_primary: plane type (primary vs overlay)
1208 *
1209 * Legacy API to initialize a DRM plane.
1210 *
1211 * New drivers should call drm_universal_plane_init() instead.
1212 *
1213 * Returns:
1214 * Zero on success, error code on failure.
1215 */
1216int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1217 unsigned long possible_crtcs,
1218 const struct drm_plane_funcs *funcs,
1219 const uint32_t *formats, uint32_t format_count,
1220 bool is_primary)
1221{
1222 enum drm_plane_type type;
1223
1224 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1225 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1226 formats, format_count, type);
1227}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001228EXPORT_SYMBOL(drm_plane_init);
1229
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001230/**
1231 * drm_plane_cleanup - Clean up the core plane usage
1232 * @plane: plane to cleanup
1233 *
1234 * This function cleans up @plane and removes it from the DRM mode setting
1235 * core. Note that the function does *not* free the plane structure itself,
1236 * this is the responsibility of the caller.
1237 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001238void drm_plane_cleanup(struct drm_plane *plane)
1239{
1240 struct drm_device *dev = plane->dev;
1241
Daniel Vetter84849902012-12-02 00:28:11 +01001242 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001243 kfree(plane->format_types);
1244 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001245
1246 BUG_ON(list_empty(&plane->head));
1247
1248 list_del(&plane->head);
1249 dev->mode_config.num_total_plane--;
1250 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1251 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001252 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001253}
1254EXPORT_SYMBOL(drm_plane_cleanup);
1255
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001256/**
1257 * drm_plane_force_disable - Forcibly disable a plane
1258 * @plane: plane to disable
1259 *
1260 * Forces the plane to be disabled.
1261 *
1262 * Used when the plane's current framebuffer is destroyed,
1263 * and when restoring fbdev mode.
1264 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001265void drm_plane_force_disable(struct drm_plane *plane)
1266{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001267 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001268 int ret;
1269
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001270 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001271 return;
1272
1273 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001274 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001275 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001276 return;
1277 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001278 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001279 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001280 plane->fb = NULL;
1281 plane->crtc = NULL;
1282}
1283EXPORT_SYMBOL(drm_plane_force_disable);
1284
Dave Airlief453ba02008-11-07 14:05:41 -08001285static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1286{
1287 struct drm_property *edid;
1288 struct drm_property *dpms;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001289 struct drm_property *dev_path;
Dave Airlief453ba02008-11-07 14:05:41 -08001290
1291 /*
1292 * Standard properties (apply to all connectors)
1293 */
1294 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1295 DRM_MODE_PROP_IMMUTABLE,
1296 "EDID", 0);
1297 dev->mode_config.edid_property = edid;
1298
Sascha Hauer4a67d392012-02-06 10:58:17 +01001299 dpms = drm_property_create_enum(dev, 0,
1300 "DPMS", drm_dpms_enum_list,
1301 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001302 dev->mode_config.dpms_property = dpms;
1303
Dave Airlie43aba7e2014-06-05 14:01:31 +10001304 dev_path = drm_property_create(dev,
1305 DRM_MODE_PROP_BLOB |
1306 DRM_MODE_PROP_IMMUTABLE,
1307 "PATH", 0);
1308 dev->mode_config.path_property = dev_path;
1309
Dave Airlief453ba02008-11-07 14:05:41 -08001310 return 0;
1311}
1312
Rob Clark9922ab52014-04-01 20:16:57 -04001313static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1314{
1315 struct drm_property *type;
1316
1317 /*
1318 * Standard properties (apply to all planes)
1319 */
1320 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1321 "type", drm_plane_type_enum_list,
1322 ARRAY_SIZE(drm_plane_type_enum_list));
1323 dev->mode_config.plane_type_property = type;
1324
1325 return 0;
1326}
1327
Dave Airlief453ba02008-11-07 14:05:41 -08001328/**
1329 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1330 * @dev: DRM device
1331 *
1332 * Called by a driver the first time a DVI-I connector is made.
1333 */
1334int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1335{
1336 struct drm_property *dvi_i_selector;
1337 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001338
1339 if (dev->mode_config.dvi_i_select_subconnector_property)
1340 return 0;
1341
1342 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001343 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001344 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001345 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001346 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001347 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1348
Sascha Hauer4a67d392012-02-06 10:58:17 +01001349 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001350 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001351 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001352 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001353 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1354
1355 return 0;
1356}
1357EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1358
1359/**
1360 * drm_create_tv_properties - create TV specific connector properties
1361 * @dev: DRM device
1362 * @num_modes: number of different TV formats (modes) supported
1363 * @modes: array of pointers to strings containing name of each format
1364 *
1365 * Called by a driver's TV initialization routine, this function creates
1366 * the TV specific connector properties for a given device. Caller is
1367 * responsible for allocating a list of format names and passing them to
1368 * this routine.
1369 */
1370int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1371 char *modes[])
1372{
1373 struct drm_property *tv_selector;
1374 struct drm_property *tv_subconnector;
1375 int i;
1376
1377 if (dev->mode_config.tv_select_subconnector_property)
1378 return 0;
1379
1380 /*
1381 * Basic connector properties
1382 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001383 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001384 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001385 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001386 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001387 dev->mode_config.tv_select_subconnector_property = tv_selector;
1388
1389 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001390 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1391 "subconnector",
1392 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001393 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001394 dev->mode_config.tv_subconnector_property = tv_subconnector;
1395
1396 /*
1397 * Other, TV specific properties: margins & TV modes.
1398 */
1399 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001400 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001401
1402 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001403 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001404
1405 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001406 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001407
1408 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001409 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001410
1411 dev->mode_config.tv_mode_property =
1412 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1413 "mode", num_modes);
1414 for (i = 0; i < num_modes; i++)
1415 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1416 i, modes[i]);
1417
Francisco Jerezb6b79022009-08-02 04:19:20 +02001418 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001419 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001420
1421 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001422 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001423
1424 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001425 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001426
Francisco Jereza75f0232009-08-12 02:30:10 +02001427 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001428 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001429
1430 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001431 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001432
1433 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001434 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001435
Dave Airlief453ba02008-11-07 14:05:41 -08001436 return 0;
1437}
1438EXPORT_SYMBOL(drm_mode_create_tv_properties);
1439
1440/**
1441 * drm_mode_create_scaling_mode_property - create scaling mode property
1442 * @dev: DRM device
1443 *
1444 * Called by a driver the first time it's needed, must be attached to desired
1445 * connectors.
1446 */
1447int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1448{
1449 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001450
1451 if (dev->mode_config.scaling_mode_property)
1452 return 0;
1453
1454 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001455 drm_property_create_enum(dev, 0, "scaling mode",
1456 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001457 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001458
1459 dev->mode_config.scaling_mode_property = scaling_mode;
1460
1461 return 0;
1462}
1463EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1464
1465/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001466 * drm_mode_create_dirty_property - create dirty property
1467 * @dev: DRM device
1468 *
1469 * Called by a driver the first time it's needed, must be attached to desired
1470 * connectors.
1471 */
1472int drm_mode_create_dirty_info_property(struct drm_device *dev)
1473{
1474 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001475
1476 if (dev->mode_config.dirty_info_property)
1477 return 0;
1478
1479 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001480 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001481 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001482 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001483 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001484 dev->mode_config.dirty_info_property = dirty_info;
1485
1486 return 0;
1487}
1488EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1489
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001490static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001491{
1492 uint32_t total_objects = 0;
1493
1494 total_objects += dev->mode_config.num_crtc;
1495 total_objects += dev->mode_config.num_connector;
1496 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001497 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001498
Dave Airlief453ba02008-11-07 14:05:41 -08001499 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1500 if (!group->id_list)
1501 return -ENOMEM;
1502
1503 group->num_crtcs = 0;
1504 group->num_connectors = 0;
1505 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001506 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001507 return 0;
1508}
1509
Dave Airliead222792014-05-02 13:22:19 +10001510void drm_mode_group_destroy(struct drm_mode_group *group)
1511{
1512 kfree(group->id_list);
1513 group->id_list = NULL;
1514}
1515
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001516/*
1517 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1518 * the drm core's responsibility to set up mode control groups.
1519 */
Dave Airlief453ba02008-11-07 14:05:41 -08001520int drm_mode_group_init_legacy_group(struct drm_device *dev,
1521 struct drm_mode_group *group)
1522{
1523 struct drm_crtc *crtc;
1524 struct drm_encoder *encoder;
1525 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001526 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001527 int ret;
1528
1529 if ((ret = drm_mode_group_init(dev, group)))
1530 return ret;
1531
1532 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1533 group->id_list[group->num_crtcs++] = crtc->base.id;
1534
1535 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1536 group->id_list[group->num_crtcs + group->num_encoders++] =
1537 encoder->base.id;
1538
1539 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1540 group->id_list[group->num_crtcs + group->num_encoders +
1541 group->num_connectors++] = connector->base.id;
1542
Sean Paul3b336ec2013-08-14 16:47:37 -04001543 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1544 group->id_list[group->num_crtcs + group->num_encoders +
1545 group->num_connectors + group->num_bridges++] =
1546 bridge->base.id;
1547
Dave Airlief453ba02008-11-07 14:05:41 -08001548 return 0;
1549}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001550EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001551
Dave Airlie2390cd12014-06-05 14:01:29 +10001552void drm_reinit_primary_mode_group(struct drm_device *dev)
1553{
1554 drm_modeset_lock_all(dev);
1555 drm_mode_group_destroy(&dev->primary->mode_group);
1556 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1557 drm_modeset_unlock_all(dev);
1558}
1559EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1560
Dave Airlief453ba02008-11-07 14:05:41 -08001561/**
Dave Airlief453ba02008-11-07 14:05:41 -08001562 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1563 * @out: drm_mode_modeinfo struct to return to the user
1564 * @in: drm_display_mode to use
1565 *
Dave Airlief453ba02008-11-07 14:05:41 -08001566 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1567 * the user.
1568 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001569static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1570 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001571{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001572 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1573 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1574 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1575 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1576 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1577 "timing values too large for mode info\n");
1578
Dave Airlief453ba02008-11-07 14:05:41 -08001579 out->clock = in->clock;
1580 out->hdisplay = in->hdisplay;
1581 out->hsync_start = in->hsync_start;
1582 out->hsync_end = in->hsync_end;
1583 out->htotal = in->htotal;
1584 out->hskew = in->hskew;
1585 out->vdisplay = in->vdisplay;
1586 out->vsync_start = in->vsync_start;
1587 out->vsync_end = in->vsync_end;
1588 out->vtotal = in->vtotal;
1589 out->vscan = in->vscan;
1590 out->vrefresh = in->vrefresh;
1591 out->flags = in->flags;
1592 out->type = in->type;
1593 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1594 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1595}
1596
1597/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001598 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001599 * @out: drm_display_mode to return to the user
1600 * @in: drm_mode_modeinfo to use
1601 *
Dave Airlief453ba02008-11-07 14:05:41 -08001602 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1603 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001604 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001605 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001606 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001607 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001608static int drm_crtc_convert_umode(struct drm_display_mode *out,
1609 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001610{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001611 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1612 return -ERANGE;
1613
Damien Lespiau5848ad42013-09-27 12:11:50 +01001614 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1615 return -EINVAL;
1616
Dave Airlief453ba02008-11-07 14:05:41 -08001617 out->clock = in->clock;
1618 out->hdisplay = in->hdisplay;
1619 out->hsync_start = in->hsync_start;
1620 out->hsync_end = in->hsync_end;
1621 out->htotal = in->htotal;
1622 out->hskew = in->hskew;
1623 out->vdisplay = in->vdisplay;
1624 out->vsync_start = in->vsync_start;
1625 out->vsync_end = in->vsync_end;
1626 out->vtotal = in->vtotal;
1627 out->vscan = in->vscan;
1628 out->vrefresh = in->vrefresh;
1629 out->flags = in->flags;
1630 out->type = in->type;
1631 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1632 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001633
1634 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001635}
1636
1637/**
1638 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001639 * @dev: drm device for the ioctl
1640 * @data: data pointer for the ioctl
1641 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001642 *
Dave Airlief453ba02008-11-07 14:05:41 -08001643 * Construct a set of configuration description structures and return
1644 * them to the user, including CRTC, connector and framebuffer configuration.
1645 *
1646 * Called by the user via ioctl.
1647 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001648 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001649 * Zero on success, errno on failure.
1650 */
1651int drm_mode_getresources(struct drm_device *dev, void *data,
1652 struct drm_file *file_priv)
1653{
1654 struct drm_mode_card_res *card_res = data;
1655 struct list_head *lh;
1656 struct drm_framebuffer *fb;
1657 struct drm_connector *connector;
1658 struct drm_crtc *crtc;
1659 struct drm_encoder *encoder;
1660 int ret = 0;
1661 int connector_count = 0;
1662 int crtc_count = 0;
1663 int fb_count = 0;
1664 int encoder_count = 0;
1665 int copied = 0, i;
1666 uint32_t __user *fb_id;
1667 uint32_t __user *crtc_id;
1668 uint32_t __user *connector_id;
1669 uint32_t __user *encoder_id;
1670 struct drm_mode_group *mode_group;
1671
Dave Airliefb3b06c2011-02-08 13:55:21 +10001672 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1673 return -EINVAL;
1674
Dave Airlief453ba02008-11-07 14:05:41 -08001675
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001676 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001677 /*
1678 * For the non-control nodes we need to limit the list of resources
1679 * by IDs in the group list for this node
1680 */
1681 list_for_each(lh, &file_priv->fbs)
1682 fb_count++;
1683
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001684 /* handle this in 4 parts */
1685 /* FBs */
1686 if (card_res->count_fbs >= fb_count) {
1687 copied = 0;
1688 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1689 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1690 if (put_user(fb->base.id, fb_id + copied)) {
1691 mutex_unlock(&file_priv->fbs_lock);
1692 return -EFAULT;
1693 }
1694 copied++;
1695 }
1696 }
1697 card_res->count_fbs = fb_count;
1698 mutex_unlock(&file_priv->fbs_lock);
1699
1700 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001701 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001702
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001703 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001704 list_for_each(lh, &dev->mode_config.crtc_list)
1705 crtc_count++;
1706
1707 list_for_each(lh, &dev->mode_config.connector_list)
1708 connector_count++;
1709
1710 list_for_each(lh, &dev->mode_config.encoder_list)
1711 encoder_count++;
1712 } else {
1713
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001714 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001715 crtc_count = mode_group->num_crtcs;
1716 connector_count = mode_group->num_connectors;
1717 encoder_count = mode_group->num_encoders;
1718 }
1719
1720 card_res->max_height = dev->mode_config.max_height;
1721 card_res->min_height = dev->mode_config.min_height;
1722 card_res->max_width = dev->mode_config.max_width;
1723 card_res->min_width = dev->mode_config.min_width;
1724
Dave Airlief453ba02008-11-07 14:05:41 -08001725 /* CRTCs */
1726 if (card_res->count_crtcs >= crtc_count) {
1727 copied = 0;
1728 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001729 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001730 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1731 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001732 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001733 if (put_user(crtc->base.id, crtc_id + copied)) {
1734 ret = -EFAULT;
1735 goto out;
1736 }
1737 copied++;
1738 }
1739 } else {
1740 for (i = 0; i < mode_group->num_crtcs; i++) {
1741 if (put_user(mode_group->id_list[i],
1742 crtc_id + copied)) {
1743 ret = -EFAULT;
1744 goto out;
1745 }
1746 copied++;
1747 }
1748 }
1749 }
1750 card_res->count_crtcs = crtc_count;
1751
1752 /* Encoders */
1753 if (card_res->count_encoders >= encoder_count) {
1754 copied = 0;
1755 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001756 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001757 list_for_each_entry(encoder,
1758 &dev->mode_config.encoder_list,
1759 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001760 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001761 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001762 if (put_user(encoder->base.id, encoder_id +
1763 copied)) {
1764 ret = -EFAULT;
1765 goto out;
1766 }
1767 copied++;
1768 }
1769 } else {
1770 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1771 if (put_user(mode_group->id_list[i],
1772 encoder_id + copied)) {
1773 ret = -EFAULT;
1774 goto out;
1775 }
1776 copied++;
1777 }
1778
1779 }
1780 }
1781 card_res->count_encoders = encoder_count;
1782
1783 /* Connectors */
1784 if (card_res->count_connectors >= connector_count) {
1785 copied = 0;
1786 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001787 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001788 list_for_each_entry(connector,
1789 &dev->mode_config.connector_list,
1790 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001791 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1792 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001793 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001794 if (put_user(connector->base.id,
1795 connector_id + copied)) {
1796 ret = -EFAULT;
1797 goto out;
1798 }
1799 copied++;
1800 }
1801 } else {
1802 int start = mode_group->num_crtcs +
1803 mode_group->num_encoders;
1804 for (i = start; i < start + mode_group->num_connectors; i++) {
1805 if (put_user(mode_group->id_list[i],
1806 connector_id + copied)) {
1807 ret = -EFAULT;
1808 goto out;
1809 }
1810 copied++;
1811 }
1812 }
1813 }
1814 card_res->count_connectors = connector_count;
1815
Jerome Glisse94401062010-07-15 15:43:25 -04001816 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001817 card_res->count_connectors, card_res->count_encoders);
1818
1819out:
Daniel Vetter84849902012-12-02 00:28:11 +01001820 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001821 return ret;
1822}
1823
1824/**
1825 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001826 * @dev: drm device for the ioctl
1827 * @data: data pointer for the ioctl
1828 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001829 *
Dave Airlief453ba02008-11-07 14:05:41 -08001830 * Construct a CRTC configuration structure to return to the user.
1831 *
1832 * Called by the user via ioctl.
1833 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001834 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001835 * Zero on success, errno on failure.
1836 */
1837int drm_mode_getcrtc(struct drm_device *dev,
1838 void *data, struct drm_file *file_priv)
1839{
1840 struct drm_mode_crtc *crtc_resp = data;
1841 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001842 int ret = 0;
1843
Dave Airliefb3b06c2011-02-08 13:55:21 +10001844 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1845 return -EINVAL;
1846
Daniel Vetter84849902012-12-02 00:28:11 +01001847 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001848
Rob Clarka2b34e22013-10-05 16:36:52 -04001849 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1850 if (!crtc) {
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
1855 crtc_resp->x = crtc->x;
1856 crtc_resp->y = crtc->y;
1857 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001858 if (crtc->primary->fb)
1859 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001860 else
1861 crtc_resp->fb_id = 0;
1862
1863 if (crtc->enabled) {
1864
1865 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1866 crtc_resp->mode_valid = 1;
1867
1868 } else {
1869 crtc_resp->mode_valid = 0;
1870 }
1871
1872out:
Daniel Vetter84849902012-12-02 00:28:11 +01001873 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001874 return ret;
1875}
1876
Damien Lespiau61d8e322013-09-25 16:45:22 +01001877static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1878 const struct drm_file *file_priv)
1879{
1880 /*
1881 * If user-space hasn't configured the driver to expose the stereo 3D
1882 * modes, don't expose them.
1883 */
1884 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1885 return false;
1886
1887 return true;
1888}
1889
Dave Airlief453ba02008-11-07 14:05:41 -08001890/**
1891 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001892 * @dev: drm device for the ioctl
1893 * @data: data pointer for the ioctl
1894 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001895 *
Dave Airlief453ba02008-11-07 14:05:41 -08001896 * Construct a connector configuration structure to return to the user.
1897 *
1898 * Called by the user via ioctl.
1899 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001900 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001901 * Zero on success, errno on failure.
1902 */
1903int drm_mode_getconnector(struct drm_device *dev, void *data,
1904 struct drm_file *file_priv)
1905{
1906 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001907 struct drm_connector *connector;
1908 struct drm_display_mode *mode;
1909 int mode_count = 0;
1910 int props_count = 0;
1911 int encoders_count = 0;
1912 int ret = 0;
1913 int copied = 0;
1914 int i;
1915 struct drm_mode_modeinfo u_mode;
1916 struct drm_mode_modeinfo __user *mode_ptr;
1917 uint32_t __user *prop_ptr;
1918 uint64_t __user *prop_values;
1919 uint32_t __user *encoder_ptr;
1920
Dave Airliefb3b06c2011-02-08 13:55:21 +10001921 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1922 return -EINVAL;
1923
Dave Airlief453ba02008-11-07 14:05:41 -08001924 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1925
Jerome Glisse94401062010-07-15 15:43:25 -04001926 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001927
Daniel Vetter7b240562012-12-12 00:35:33 +01001928 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001929
Rob Clarka2b34e22013-10-05 16:36:52 -04001930 connector = drm_connector_find(dev, out_resp->connector_id);
1931 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001932 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001933 goto out;
1934 }
Dave Airlief453ba02008-11-07 14:05:41 -08001935
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001936 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001937
1938 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1939 if (connector->encoder_ids[i] != 0) {
1940 encoders_count++;
1941 }
1942 }
1943
1944 if (out_resp->count_modes == 0) {
1945 connector->funcs->fill_modes(connector,
1946 dev->mode_config.max_width,
1947 dev->mode_config.max_height);
1948 }
1949
1950 /* delayed so we get modes regardless of pre-fill_modes state */
1951 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001952 if (drm_mode_expose_to_userspace(mode, file_priv))
1953 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001954
1955 out_resp->connector_id = connector->base.id;
1956 out_resp->connector_type = connector->connector_type;
1957 out_resp->connector_type_id = connector->connector_type_id;
1958 out_resp->mm_width = connector->display_info.width_mm;
1959 out_resp->mm_height = connector->display_info.height_mm;
1960 out_resp->subpixel = connector->display_info.subpixel_order;
1961 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02001962 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08001963 if (connector->encoder)
1964 out_resp->encoder_id = connector->encoder->base.id;
1965 else
1966 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02001967 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001968
1969 /*
1970 * This ioctl is called twice, once to determine how much space is
1971 * needed, and the 2nd time to fill it.
1972 */
1973 if ((out_resp->count_modes >= mode_count) && mode_count) {
1974 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001975 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001976 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001977 if (!drm_mode_expose_to_userspace(mode, file_priv))
1978 continue;
1979
Dave Airlief453ba02008-11-07 14:05:41 -08001980 drm_crtc_convert_to_umode(&u_mode, mode);
1981 if (copy_to_user(mode_ptr + copied,
1982 &u_mode, sizeof(u_mode))) {
1983 ret = -EFAULT;
1984 goto out;
1985 }
1986 copied++;
1987 }
1988 }
1989 out_resp->count_modes = mode_count;
1990
1991 if ((out_resp->count_props >= props_count) && props_count) {
1992 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001993 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1994 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001995 for (i = 0; i < connector->properties.count; i++) {
1996 if (put_user(connector->properties.ids[i],
1997 prop_ptr + copied)) {
1998 ret = -EFAULT;
1999 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002000 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002001
2002 if (put_user(connector->properties.values[i],
2003 prop_values + copied)) {
2004 ret = -EFAULT;
2005 goto out;
2006 }
2007 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002008 }
2009 }
2010 out_resp->count_props = props_count;
2011
2012 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2013 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002014 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002015 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2016 if (connector->encoder_ids[i] != 0) {
2017 if (put_user(connector->encoder_ids[i],
2018 encoder_ptr + copied)) {
2019 ret = -EFAULT;
2020 goto out;
2021 }
2022 copied++;
2023 }
2024 }
2025 }
2026 out_resp->count_encoders = encoders_count;
2027
2028out:
Daniel Vetter7b240562012-12-12 00:35:33 +01002029 mutex_unlock(&dev->mode_config.mutex);
2030
Dave Airlief453ba02008-11-07 14:05:41 -08002031 return ret;
2032}
2033
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002034/**
2035 * drm_mode_getencoder - get encoder configuration
2036 * @dev: drm device for the ioctl
2037 * @data: data pointer for the ioctl
2038 * @file_priv: drm file for the ioctl call
2039 *
2040 * Construct a encoder configuration structure to return to the user.
2041 *
2042 * Called by the user via ioctl.
2043 *
2044 * Returns:
2045 * Zero on success, errno on failure.
2046 */
Dave Airlief453ba02008-11-07 14:05:41 -08002047int drm_mode_getencoder(struct drm_device *dev, void *data,
2048 struct drm_file *file_priv)
2049{
2050 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002051 struct drm_encoder *encoder;
2052 int ret = 0;
2053
Dave Airliefb3b06c2011-02-08 13:55:21 +10002054 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2055 return -EINVAL;
2056
Daniel Vetter84849902012-12-02 00:28:11 +01002057 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002058 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2059 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002060 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002061 goto out;
2062 }
Dave Airlief453ba02008-11-07 14:05:41 -08002063
2064 if (encoder->crtc)
2065 enc_resp->crtc_id = encoder->crtc->base.id;
2066 else
2067 enc_resp->crtc_id = 0;
2068 enc_resp->encoder_type = encoder->encoder_type;
2069 enc_resp->encoder_id = encoder->base.id;
2070 enc_resp->possible_crtcs = encoder->possible_crtcs;
2071 enc_resp->possible_clones = encoder->possible_clones;
2072
2073out:
Daniel Vetter84849902012-12-02 00:28:11 +01002074 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002075 return ret;
2076}
2077
2078/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002079 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002080 * @dev: DRM device
2081 * @data: ioctl data
2082 * @file_priv: DRM file info
2083 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002084 * Construct a list of plane ids to return to the user.
2085 *
2086 * Called by the user via ioctl.
2087 *
2088 * Returns:
2089 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002090 */
2091int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002092 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002093{
2094 struct drm_mode_get_plane_res *plane_resp = data;
2095 struct drm_mode_config *config;
2096 struct drm_plane *plane;
2097 uint32_t __user *plane_ptr;
2098 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002099 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002100
2101 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2102 return -EINVAL;
2103
Daniel Vetter84849902012-12-02 00:28:11 +01002104 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002105 config = &dev->mode_config;
2106
Matt Roper681e7ec2014-04-01 15:22:42 -07002107 if (file_priv->universal_planes)
2108 num_planes = config->num_total_plane;
2109 else
2110 num_planes = config->num_overlay_plane;
2111
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002112 /*
2113 * This ioctl is called twice, once to determine how much space is
2114 * needed, and the 2nd time to fill it.
2115 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002116 if (num_planes &&
2117 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002118 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002119
2120 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002121 /*
2122 * Unless userspace set the 'universal planes'
2123 * capability bit, only advertise overlays.
2124 */
2125 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2126 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002127 continue;
2128
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002129 if (put_user(plane->base.id, plane_ptr + copied)) {
2130 ret = -EFAULT;
2131 goto out;
2132 }
2133 copied++;
2134 }
2135 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002136 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002137
2138out:
Daniel Vetter84849902012-12-02 00:28:11 +01002139 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002140 return ret;
2141}
2142
2143/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002144 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002145 * @dev: DRM device
2146 * @data: ioctl data
2147 * @file_priv: DRM file info
2148 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002149 * Construct a plane configuration structure to return to the user.
2150 *
2151 * Called by the user via ioctl.
2152 *
2153 * Returns:
2154 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002155 */
2156int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002157 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002158{
2159 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002160 struct drm_plane *plane;
2161 uint32_t __user *format_ptr;
2162 int ret = 0;
2163
2164 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2165 return -EINVAL;
2166
Daniel Vetter84849902012-12-02 00:28:11 +01002167 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002168 plane = drm_plane_find(dev, plane_resp->plane_id);
2169 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002170 ret = -ENOENT;
2171 goto out;
2172 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002173
2174 if (plane->crtc)
2175 plane_resp->crtc_id = plane->crtc->base.id;
2176 else
2177 plane_resp->crtc_id = 0;
2178
2179 if (plane->fb)
2180 plane_resp->fb_id = plane->fb->base.id;
2181 else
2182 plane_resp->fb_id = 0;
2183
2184 plane_resp->plane_id = plane->base.id;
2185 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002186 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002187
2188 /*
2189 * This ioctl is called twice, once to determine how much space is
2190 * needed, and the 2nd time to fill it.
2191 */
2192 if (plane->format_count &&
2193 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002194 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002195 if (copy_to_user(format_ptr,
2196 plane->format_types,
2197 sizeof(uint32_t) * plane->format_count)) {
2198 ret = -EFAULT;
2199 goto out;
2200 }
2201 }
2202 plane_resp->count_format_types = plane->format_count;
2203
2204out:
Daniel Vetter84849902012-12-02 00:28:11 +01002205 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002206 return ret;
2207}
2208
Matt Roperb36552b2014-06-10 08:28:09 -07002209/*
2210 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002211 *
Matt Roperb36552b2014-06-10 08:28:09 -07002212 * Note that we assume an extra reference has already been taken on fb. If the
2213 * update fails, this reference will be dropped before return; if it succeeds,
2214 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002215 *
Matt Roperb36552b2014-06-10 08:28:09 -07002216 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002217 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002218static int setplane_internal(struct drm_plane *plane,
2219 struct drm_crtc *crtc,
Matt Roperb36552b2014-06-10 08:28:09 -07002220 struct drm_framebuffer *fb,
2221 int32_t crtc_x, int32_t crtc_y,
2222 uint32_t crtc_w, uint32_t crtc_h,
2223 /* src_{x,y,w,h} values are 16.16 fixed point */
2224 uint32_t src_x, uint32_t src_y,
2225 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002226{
Chris Wilson17cfd912014-06-13 15:22:28 +01002227 struct drm_device *dev = plane->dev;
Matt Roperb36552b2014-06-10 08:28:09 -07002228 struct drm_framebuffer *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002229 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002230 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002231 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002232
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002233 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002234 if (!fb) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002235 drm_modeset_lock_all(dev);
2236 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002237 ret = plane->funcs->disable_plane(plane);
2238 if (!ret) {
2239 plane->crtc = NULL;
2240 plane->fb = NULL;
2241 } else {
2242 old_fb = NULL;
2243 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002244 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002245 goto out;
2246 }
2247
Matt Roper7f994f32014-05-29 08:06:51 -07002248 /* Check whether this plane is usable on this CRTC */
2249 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2250 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2251 ret = -EINVAL;
2252 goto out;
2253 }
2254
Ville Syrjälä62443be2011-12-20 00:06:47 +02002255 /* Check whether this plane supports the fb pixel format. */
2256 for (i = 0; i < plane->format_count; i++)
2257 if (fb->pixel_format == plane->format_types[i])
2258 break;
2259 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002260 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2261 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002262 ret = -EINVAL;
2263 goto out;
2264 }
2265
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002266 fb_width = fb->width << 16;
2267 fb_height = fb->height << 16;
2268
2269 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002270 if (src_w > fb_width ||
2271 src_x > fb_width - src_w ||
2272 src_h > fb_height ||
2273 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002274 DRM_DEBUG_KMS("Invalid source coordinates "
2275 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002276 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2277 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2278 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2279 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002280 ret = -ENOSPC;
2281 goto out;
2282 }
2283
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002284 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002285 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002286 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002287 crtc_x, crtc_y, crtc_w, crtc_h,
2288 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002289 if (!ret) {
2290 plane->crtc = crtc;
2291 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002292 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002293 } else {
2294 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002295 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002296 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002297
2298out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002299 if (fb)
2300 drm_framebuffer_unreference(fb);
2301 if (old_fb)
2302 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002303
2304 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002305
2306}
2307
2308/**
2309 * drm_mode_setplane - configure a plane's configuration
2310 * @dev: DRM device
2311 * @data: ioctl data*
2312 * @file_priv: DRM file info
2313 *
2314 * Set plane configuration, including placement, fb, scaling, and other factors.
2315 * Or pass a NULL fb to disable (planes may be disabled without providing a
2316 * valid crtc).
2317 *
2318 * Returns:
2319 * Zero on success, errno on failure.
2320 */
2321int drm_mode_setplane(struct drm_device *dev, void *data,
2322 struct drm_file *file_priv)
2323{
2324 struct drm_mode_set_plane *plane_req = data;
2325 struct drm_mode_object *obj;
2326 struct drm_plane *plane;
2327 struct drm_crtc *crtc = NULL;
2328 struct drm_framebuffer *fb = NULL;
2329
2330 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2331 return -EINVAL;
2332
2333 /* Give drivers some help against integer overflows */
2334 if (plane_req->crtc_w > INT_MAX ||
2335 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2336 plane_req->crtc_h > INT_MAX ||
2337 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2338 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2339 plane_req->crtc_w, plane_req->crtc_h,
2340 plane_req->crtc_x, plane_req->crtc_y);
2341 return -ERANGE;
2342 }
2343
2344 /*
2345 * First, find the plane, crtc, and fb objects. If not available,
2346 * we don't bother to call the driver.
2347 */
2348 obj = drm_mode_object_find(dev, plane_req->plane_id,
2349 DRM_MODE_OBJECT_PLANE);
2350 if (!obj) {
2351 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2352 plane_req->plane_id);
2353 return -ENOENT;
2354 }
2355 plane = obj_to_plane(obj);
2356
2357 if (plane_req->fb_id) {
2358 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2359 if (!fb) {
2360 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2361 plane_req->fb_id);
2362 return -ENOENT;
2363 }
2364
2365 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2366 DRM_MODE_OBJECT_CRTC);
2367 if (!obj) {
2368 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2369 plane_req->crtc_id);
2370 return -ENOENT;
2371 }
2372 crtc = obj_to_crtc(obj);
2373 }
2374
Matt Roper161d0dc2014-06-10 08:28:10 -07002375 /*
2376 * setplane_internal will take care of deref'ing either the old or new
2377 * framebuffer depending on success.
2378 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002379 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002380 plane_req->crtc_x, plane_req->crtc_y,
2381 plane_req->crtc_w, plane_req->crtc_h,
2382 plane_req->src_x, plane_req->src_y,
2383 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002384}
2385
2386/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002387 * drm_mode_set_config_internal - helper to call ->set_config
2388 * @set: modeset config to set
2389 *
2390 * This is a little helper to wrap internal calls to the ->set_config driver
2391 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002392 *
2393 * Returns:
2394 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002395 */
2396int drm_mode_set_config_internal(struct drm_mode_set *set)
2397{
2398 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002399 struct drm_framebuffer *fb;
2400 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002401 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002402
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002403 /*
2404 * NOTE: ->set_config can also disable other crtcs (if we steal all
2405 * connectors from it), hence we need to refcount the fbs across all
2406 * crtcs. Atomic modeset will have saner semantics ...
2407 */
2408 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002409 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002410
Daniel Vetterb0d12322012-12-11 01:07:12 +01002411 fb = set->fb;
2412
2413 ret = crtc->funcs->set_config(set);
2414 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002415 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002416 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002417 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002418
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002419 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002420 if (tmp->primary->fb)
2421 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002422 if (tmp->old_fb)
2423 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002424 }
2425
2426 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002427}
2428EXPORT_SYMBOL(drm_mode_set_config_internal);
2429
Matt Roperaf936292014-04-01 15:22:34 -07002430/**
2431 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2432 * CRTC viewport
2433 * @crtc: CRTC that framebuffer will be displayed on
2434 * @x: x panning
2435 * @y: y panning
2436 * @mode: mode that framebuffer will be displayed under
2437 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002438 */
Matt Roperaf936292014-04-01 15:22:34 -07002439int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2440 int x, int y,
2441 const struct drm_display_mode *mode,
2442 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002443
2444{
2445 int hdisplay, vdisplay;
2446
2447 hdisplay = mode->hdisplay;
2448 vdisplay = mode->vdisplay;
2449
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002450 if (drm_mode_is_stereo(mode)) {
2451 struct drm_display_mode adjusted = *mode;
2452
2453 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2454 hdisplay = adjusted.crtc_hdisplay;
2455 vdisplay = adjusted.crtc_vdisplay;
2456 }
2457
Damien Lespiauc11e9282013-09-25 16:45:30 +01002458 if (crtc->invert_dimensions)
2459 swap(hdisplay, vdisplay);
2460
2461 if (hdisplay > fb->width ||
2462 vdisplay > fb->height ||
2463 x > fb->width - hdisplay ||
2464 y > fb->height - vdisplay) {
2465 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2466 fb->width, fb->height, hdisplay, vdisplay, x, y,
2467 crtc->invert_dimensions ? " (inverted)" : "");
2468 return -ENOSPC;
2469 }
2470
2471 return 0;
2472}
Matt Roperaf936292014-04-01 15:22:34 -07002473EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002474
Daniel Vetter2d13b672012-12-11 13:47:23 +01002475/**
Dave Airlief453ba02008-11-07 14:05:41 -08002476 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002477 * @dev: drm device for the ioctl
2478 * @data: data pointer for the ioctl
2479 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002480 *
Dave Airlief453ba02008-11-07 14:05:41 -08002481 * Build a new CRTC configuration based on user request.
2482 *
2483 * Called by the user via ioctl.
2484 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002485 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002486 * Zero on success, errno on failure.
2487 */
2488int drm_mode_setcrtc(struct drm_device *dev, void *data,
2489 struct drm_file *file_priv)
2490{
2491 struct drm_mode_config *config = &dev->mode_config;
2492 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002493 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002494 struct drm_connector **connector_set = NULL, *connector;
2495 struct drm_framebuffer *fb = NULL;
2496 struct drm_display_mode *mode = NULL;
2497 struct drm_mode_set set;
2498 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002499 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002500 int i;
2501
Dave Airliefb3b06c2011-02-08 13:55:21 +10002502 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2503 return -EINVAL;
2504
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002505 /* For some reason crtc x/y offsets are signed internally. */
2506 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2507 return -ERANGE;
2508
Daniel Vetter84849902012-12-02 00:28:11 +01002509 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002510 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2511 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002512 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002513 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002514 goto out;
2515 }
Jerome Glisse94401062010-07-15 15:43:25 -04002516 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002517
2518 if (crtc_req->mode_valid) {
2519 /* If we have a mode we need a framebuffer. */
2520 /* If we pass -1, set the mode with the currently bound fb */
2521 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002522 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002523 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2524 ret = -EINVAL;
2525 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002526 }
Matt Roperf4510a22014-04-01 15:22:40 -07002527 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002528 /* Make refcounting symmetric with the lookup path. */
2529 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002530 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002531 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2532 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002533 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2534 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002535 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002536 goto out;
2537 }
Dave Airlief453ba02008-11-07 14:05:41 -08002538 }
2539
2540 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002541 if (!mode) {
2542 ret = -ENOMEM;
2543 goto out;
2544 }
2545
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002546 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2547 if (ret) {
2548 DRM_DEBUG_KMS("Invalid mode\n");
2549 goto out;
2550 }
2551
Dave Airlief453ba02008-11-07 14:05:41 -08002552 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002553
Damien Lespiauc11e9282013-09-25 16:45:30 +01002554 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2555 mode, fb);
2556 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002557 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002558
Dave Airlief453ba02008-11-07 14:05:41 -08002559 }
2560
2561 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002562 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002563 ret = -EINVAL;
2564 goto out;
2565 }
2566
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002567 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002568 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002569 crtc_req->count_connectors);
2570 ret = -EINVAL;
2571 goto out;
2572 }
2573
2574 if (crtc_req->count_connectors > 0) {
2575 u32 out_id;
2576
2577 /* Avoid unbounded kernel memory allocation */
2578 if (crtc_req->count_connectors > config->num_connector) {
2579 ret = -EINVAL;
2580 goto out;
2581 }
2582
2583 connector_set = kmalloc(crtc_req->count_connectors *
2584 sizeof(struct drm_connector *),
2585 GFP_KERNEL);
2586 if (!connector_set) {
2587 ret = -ENOMEM;
2588 goto out;
2589 }
2590
2591 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002592 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002593 if (get_user(out_id, &set_connectors_ptr[i])) {
2594 ret = -EFAULT;
2595 goto out;
2596 }
2597
Rob Clarka2b34e22013-10-05 16:36:52 -04002598 connector = drm_connector_find(dev, out_id);
2599 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002600 DRM_DEBUG_KMS("Connector id %d unknown\n",
2601 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002602 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002603 goto out;
2604 }
Jerome Glisse94401062010-07-15 15:43:25 -04002605 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2606 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002607 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002608
2609 connector_set[i] = connector;
2610 }
2611 }
2612
2613 set.crtc = crtc;
2614 set.x = crtc_req->x;
2615 set.y = crtc_req->y;
2616 set.mode = mode;
2617 set.connectors = connector_set;
2618 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002619 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002620 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002621
2622out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002623 if (fb)
2624 drm_framebuffer_unreference(fb);
2625
Dave Airlief453ba02008-11-07 14:05:41 -08002626 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002627 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002628 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002629 return ret;
2630}
2631
Matt Roper161d0dc2014-06-10 08:28:10 -07002632/**
2633 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2634 * universal plane handler call
2635 * @crtc: crtc to update cursor for
2636 * @req: data pointer for the ioctl
2637 * @file_priv: drm file for the ioctl call
2638 *
2639 * Legacy cursor ioctl's work directly with driver buffer handles. To
2640 * translate legacy ioctl calls into universal plane handler calls, we need to
2641 * wrap the native buffer handle in a drm_framebuffer.
2642 *
2643 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2644 * buffer with a pitch of 4*width; the universal plane interface should be used
2645 * directly in cases where the hardware can support other buffer settings and
2646 * userspace wants to make use of these capabilities.
2647 *
2648 * Returns:
2649 * Zero on success, errno on failure.
2650 */
2651static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2652 struct drm_mode_cursor2 *req,
2653 struct drm_file *file_priv)
2654{
2655 struct drm_device *dev = crtc->dev;
2656 struct drm_framebuffer *fb = NULL;
2657 struct drm_mode_fb_cmd2 fbreq = {
2658 .width = req->width,
2659 .height = req->height,
2660 .pixel_format = DRM_FORMAT_ARGB8888,
2661 .pitches = { req->width * 4 },
2662 .handles = { req->handle },
2663 };
2664 int32_t crtc_x, crtc_y;
2665 uint32_t crtc_w = 0, crtc_h = 0;
2666 uint32_t src_w = 0, src_h = 0;
2667 int ret = 0;
2668
2669 BUG_ON(!crtc->cursor);
2670
2671 /*
2672 * Obtain fb we'll be using (either new or existing) and take an extra
2673 * reference to it if fb != null. setplane will take care of dropping
2674 * the reference if the plane update fails.
2675 */
2676 if (req->flags & DRM_MODE_CURSOR_BO) {
2677 if (req->handle) {
2678 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2679 if (IS_ERR(fb)) {
2680 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2681 return PTR_ERR(fb);
2682 }
2683
2684 drm_framebuffer_reference(fb);
2685 } else {
2686 fb = NULL;
2687 }
2688 } else {
2689 mutex_lock(&dev->mode_config.mutex);
2690 fb = crtc->cursor->fb;
2691 if (fb)
2692 drm_framebuffer_reference(fb);
2693 mutex_unlock(&dev->mode_config.mutex);
2694 }
2695
2696 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2697 crtc_x = req->x;
2698 crtc_y = req->y;
2699 } else {
2700 crtc_x = crtc->cursor_x;
2701 crtc_y = crtc->cursor_y;
2702 }
2703
2704 if (fb) {
2705 crtc_w = fb->width;
2706 crtc_h = fb->height;
2707 src_w = fb->width << 16;
2708 src_h = fb->height << 16;
2709 }
2710
2711 /*
2712 * setplane_internal will take care of deref'ing either the old or new
2713 * framebuffer depending on success.
2714 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002715 ret = setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002716 crtc_x, crtc_y, crtc_w, crtc_h,
2717 0, 0, src_w, src_h);
2718
2719 /* Update successful; save new cursor position, if necessary */
2720 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2721 crtc->cursor_x = req->x;
2722 crtc->cursor_y = req->y;
2723 }
2724
2725 return ret;
2726}
2727
Dave Airlie4c813d42013-06-20 11:48:52 +10002728static int drm_mode_cursor_common(struct drm_device *dev,
2729 struct drm_mode_cursor2 *req,
2730 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002731{
Dave Airlief453ba02008-11-07 14:05:41 -08002732 struct drm_crtc *crtc;
2733 int ret = 0;
2734
Dave Airliefb3b06c2011-02-08 13:55:21 +10002735 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2736 return -EINVAL;
2737
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002738 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002739 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002740
Rob Clarka2b34e22013-10-05 16:36:52 -04002741 crtc = drm_crtc_find(dev, req->crtc_id);
2742 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002743 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002744 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002745 }
Dave Airlief453ba02008-11-07 14:05:41 -08002746
Matt Roper161d0dc2014-06-10 08:28:10 -07002747 /*
2748 * If this crtc has a universal cursor plane, call that plane's update
2749 * handler rather than using legacy cursor handlers.
2750 */
2751 if (crtc->cursor)
2752 return drm_mode_cursor_universal(crtc, req, file_priv);
2753
Rob Clark51fd3712013-11-19 12:10:12 -05002754 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002755 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002756 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002757 ret = -ENXIO;
2758 goto out;
2759 }
2760 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002761 if (crtc->funcs->cursor_set2)
2762 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2763 req->width, req->height, req->hot_x, req->hot_y);
2764 else
2765 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2766 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002767 }
2768
2769 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2770 if (crtc->funcs->cursor_move) {
2771 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2772 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002773 ret = -EFAULT;
2774 goto out;
2775 }
2776 }
2777out:
Rob Clark51fd3712013-11-19 12:10:12 -05002778 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002779
Dave Airlief453ba02008-11-07 14:05:41 -08002780 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002781
2782}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002783
2784
2785/**
2786 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2787 * @dev: drm device for the ioctl
2788 * @data: data pointer for the ioctl
2789 * @file_priv: drm file for the ioctl call
2790 *
2791 * Set the cursor configuration based on user request.
2792 *
2793 * Called by the user via ioctl.
2794 *
2795 * Returns:
2796 * Zero on success, errno on failure.
2797 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002798int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002799 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002800{
2801 struct drm_mode_cursor *req = data;
2802 struct drm_mode_cursor2 new_req;
2803
2804 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2805 new_req.hot_x = new_req.hot_y = 0;
2806
2807 return drm_mode_cursor_common(dev, &new_req, file_priv);
2808}
2809
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002810/**
2811 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2812 * @dev: drm device for the ioctl
2813 * @data: data pointer for the ioctl
2814 * @file_priv: drm file for the ioctl call
2815 *
2816 * Set the cursor configuration based on user request. This implements the 2nd
2817 * version of the cursor ioctl, which allows userspace to additionally specify
2818 * the hotspot of the pointer.
2819 *
2820 * Called by the user via ioctl.
2821 *
2822 * Returns:
2823 * Zero on success, errno on failure.
2824 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002825int drm_mode_cursor2_ioctl(struct drm_device *dev,
2826 void *data, struct drm_file *file_priv)
2827{
2828 struct drm_mode_cursor2 *req = data;
2829 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002830}
2831
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002832/**
2833 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2834 * @bpp: bits per pixels
2835 * @depth: bit depth per pixel
2836 *
2837 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2838 * Useful in fbdev emulation code, since that deals in those values.
2839 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002840uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2841{
2842 uint32_t fmt;
2843
2844 switch (bpp) {
2845 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002846 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002847 break;
2848 case 16:
2849 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002850 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002851 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002852 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002853 break;
2854 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002855 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002856 break;
2857 case 32:
2858 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002859 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002860 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002861 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002862 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002863 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002864 break;
2865 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002866 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2867 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002868 break;
2869 }
2870
2871 return fmt;
2872}
2873EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2874
Dave Airlief453ba02008-11-07 14:05:41 -08002875/**
2876 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002877 * @dev: drm device for the ioctl
2878 * @data: data pointer for the ioctl
2879 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002880 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002881 * Add a new FB to the specified CRTC, given a user request. This is the
2882 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002883 *
2884 * Called by the user via ioctl.
2885 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002886 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002887 * Zero on success, errno on failure.
2888 */
2889int drm_mode_addfb(struct drm_device *dev,
2890 void *data, struct drm_file *file_priv)
2891{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002892 struct drm_mode_fb_cmd *or = data;
2893 struct drm_mode_fb_cmd2 r = {};
2894 struct drm_mode_config *config = &dev->mode_config;
2895 struct drm_framebuffer *fb;
2896 int ret = 0;
2897
2898 /* Use new struct with format internally */
2899 r.fb_id = or->fb_id;
2900 r.width = or->width;
2901 r.height = or->height;
2902 r.pitches[0] = or->pitch;
2903 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2904 r.handles[0] = or->handle;
2905
2906 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2907 return -EINVAL;
2908
Jesse Barnesacb4b992011-11-07 12:03:23 -08002909 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002910 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002911
2912 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002913 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002914
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002915 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2916 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002917 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002918 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002919 }
2920
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002921 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002922 or->fb_id = fb->base.id;
2923 list_add(&fb->filp_head, &file_priv->fbs);
2924 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002925 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002926
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002927 return ret;
2928}
2929
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002930static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002931{
2932 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2933
2934 switch (format) {
2935 case DRM_FORMAT_C8:
2936 case DRM_FORMAT_RGB332:
2937 case DRM_FORMAT_BGR233:
2938 case DRM_FORMAT_XRGB4444:
2939 case DRM_FORMAT_XBGR4444:
2940 case DRM_FORMAT_RGBX4444:
2941 case DRM_FORMAT_BGRX4444:
2942 case DRM_FORMAT_ARGB4444:
2943 case DRM_FORMAT_ABGR4444:
2944 case DRM_FORMAT_RGBA4444:
2945 case DRM_FORMAT_BGRA4444:
2946 case DRM_FORMAT_XRGB1555:
2947 case DRM_FORMAT_XBGR1555:
2948 case DRM_FORMAT_RGBX5551:
2949 case DRM_FORMAT_BGRX5551:
2950 case DRM_FORMAT_ARGB1555:
2951 case DRM_FORMAT_ABGR1555:
2952 case DRM_FORMAT_RGBA5551:
2953 case DRM_FORMAT_BGRA5551:
2954 case DRM_FORMAT_RGB565:
2955 case DRM_FORMAT_BGR565:
2956 case DRM_FORMAT_RGB888:
2957 case DRM_FORMAT_BGR888:
2958 case DRM_FORMAT_XRGB8888:
2959 case DRM_FORMAT_XBGR8888:
2960 case DRM_FORMAT_RGBX8888:
2961 case DRM_FORMAT_BGRX8888:
2962 case DRM_FORMAT_ARGB8888:
2963 case DRM_FORMAT_ABGR8888:
2964 case DRM_FORMAT_RGBA8888:
2965 case DRM_FORMAT_BGRA8888:
2966 case DRM_FORMAT_XRGB2101010:
2967 case DRM_FORMAT_XBGR2101010:
2968 case DRM_FORMAT_RGBX1010102:
2969 case DRM_FORMAT_BGRX1010102:
2970 case DRM_FORMAT_ARGB2101010:
2971 case DRM_FORMAT_ABGR2101010:
2972 case DRM_FORMAT_RGBA1010102:
2973 case DRM_FORMAT_BGRA1010102:
2974 case DRM_FORMAT_YUYV:
2975 case DRM_FORMAT_YVYU:
2976 case DRM_FORMAT_UYVY:
2977 case DRM_FORMAT_VYUY:
2978 case DRM_FORMAT_AYUV:
2979 case DRM_FORMAT_NV12:
2980 case DRM_FORMAT_NV21:
2981 case DRM_FORMAT_NV16:
2982 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002983 case DRM_FORMAT_NV24:
2984 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002985 case DRM_FORMAT_YUV410:
2986 case DRM_FORMAT_YVU410:
2987 case DRM_FORMAT_YUV411:
2988 case DRM_FORMAT_YVU411:
2989 case DRM_FORMAT_YUV420:
2990 case DRM_FORMAT_YVU420:
2991 case DRM_FORMAT_YUV422:
2992 case DRM_FORMAT_YVU422:
2993 case DRM_FORMAT_YUV444:
2994 case DRM_FORMAT_YVU444:
2995 return 0;
2996 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002997 DRM_DEBUG_KMS("invalid pixel format %s\n",
2998 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002999 return -EINVAL;
3000 }
3001}
3002
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003003static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003004{
3005 int ret, hsub, vsub, num_planes, i;
3006
3007 ret = format_check(r);
3008 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003009 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3010 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003011 return ret;
3012 }
3013
3014 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3015 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3016 num_planes = drm_format_num_planes(r->pixel_format);
3017
3018 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003019 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003020 return -EINVAL;
3021 }
3022
3023 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003024 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003025 return -EINVAL;
3026 }
3027
3028 for (i = 0; i < num_planes; i++) {
3029 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003030 unsigned int height = r->height / (i != 0 ? vsub : 1);
3031 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003032
3033 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003034 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003035 return -EINVAL;
3036 }
3037
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003038 if ((uint64_t) width * cpp > UINT_MAX)
3039 return -ERANGE;
3040
3041 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3042 return -ERANGE;
3043
3044 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003045 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003046 return -EINVAL;
3047 }
3048 }
3049
3050 return 0;
3051}
3052
Matt Roperc394c2b2014-06-10 08:28:08 -07003053static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3054 struct drm_mode_fb_cmd2 *r,
3055 struct drm_file *file_priv)
3056{
3057 struct drm_mode_config *config = &dev->mode_config;
3058 struct drm_framebuffer *fb;
3059 int ret;
3060
3061 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3062 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3063 return ERR_PTR(-EINVAL);
3064 }
3065
3066 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3067 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3068 r->width, config->min_width, config->max_width);
3069 return ERR_PTR(-EINVAL);
3070 }
3071 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3072 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3073 r->height, config->min_height, config->max_height);
3074 return ERR_PTR(-EINVAL);
3075 }
3076
3077 ret = framebuffer_check(r);
3078 if (ret)
3079 return ERR_PTR(ret);
3080
3081 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3082 if (IS_ERR(fb)) {
3083 DRM_DEBUG_KMS("could not create framebuffer\n");
3084 return fb;
3085 }
3086
3087 mutex_lock(&file_priv->fbs_lock);
3088 r->fb_id = fb->base.id;
3089 list_add(&fb->filp_head, &file_priv->fbs);
3090 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3091 mutex_unlock(&file_priv->fbs_lock);
3092
3093 return fb;
3094}
3095
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003096/**
3097 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003098 * @dev: drm device for the ioctl
3099 * @data: data pointer for the ioctl
3100 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003101 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003102 * Add a new FB to the specified CRTC, given a user request with format. This is
3103 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3104 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003105 *
3106 * Called by the user via ioctl.
3107 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003108 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003109 * Zero on success, errno on failure.
3110 */
3111int drm_mode_addfb2(struct drm_device *dev,
3112 void *data, struct drm_file *file_priv)
3113{
Dave Airlief453ba02008-11-07 14:05:41 -08003114 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003115
Dave Airliefb3b06c2011-02-08 13:55:21 +10003116 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3117 return -EINVAL;
3118
Matt Roperc394c2b2014-06-10 08:28:08 -07003119 fb = add_framebuffer_internal(dev, data, file_priv);
3120 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003121 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003122
Matt Roperc394c2b2014-06-10 08:28:08 -07003123 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003124}
3125
3126/**
3127 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003128 * @dev: drm device for the ioctl
3129 * @data: data pointer for the ioctl
3130 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003131 *
Dave Airlief453ba02008-11-07 14:05:41 -08003132 * Remove the FB specified by the user.
3133 *
3134 * Called by the user via ioctl.
3135 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003136 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003137 * Zero on success, errno on failure.
3138 */
3139int drm_mode_rmfb(struct drm_device *dev,
3140 void *data, struct drm_file *file_priv)
3141{
Dave Airlief453ba02008-11-07 14:05:41 -08003142 struct drm_framebuffer *fb = NULL;
3143 struct drm_framebuffer *fbl = NULL;
3144 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003145 int found = 0;
3146
Dave Airliefb3b06c2011-02-08 13:55:21 +10003147 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3148 return -EINVAL;
3149
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003150 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003151 mutex_lock(&dev->mode_config.fb_lock);
3152 fb = __drm_framebuffer_lookup(dev, *id);
3153 if (!fb)
3154 goto fail_lookup;
3155
Dave Airlief453ba02008-11-07 14:05:41 -08003156 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3157 if (fb == fbl)
3158 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003159 if (!found)
3160 goto fail_lookup;
3161
3162 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3163 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003164
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003165 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003166 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003167 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003168
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003169 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003170
Daniel Vetter2b677e82012-12-10 21:16:05 +01003171 return 0;
3172
3173fail_lookup:
3174 mutex_unlock(&dev->mode_config.fb_lock);
3175 mutex_unlock(&file_priv->fbs_lock);
3176
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003177 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003178}
3179
3180/**
3181 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003182 * @dev: drm device for the ioctl
3183 * @data: data pointer for the ioctl
3184 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003185 *
Dave Airlief453ba02008-11-07 14:05:41 -08003186 * Lookup the FB given its ID and return info about it.
3187 *
3188 * Called by the user via ioctl.
3189 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003190 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003191 * Zero on success, errno on failure.
3192 */
3193int drm_mode_getfb(struct drm_device *dev,
3194 void *data, struct drm_file *file_priv)
3195{
3196 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003197 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003198 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003199
Dave Airliefb3b06c2011-02-08 13:55:21 +10003200 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3201 return -EINVAL;
3202
Daniel Vetter786b99e2012-12-02 21:53:40 +01003203 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003204 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003205 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003206
3207 r->height = fb->height;
3208 r->width = fb->width;
3209 r->depth = fb->depth;
3210 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003211 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003212 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003213 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003214 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003215 ret = fb->funcs->create_handle(fb, file_priv,
3216 &r->handle);
3217 } else {
3218 /* GET_FB() is an unprivileged ioctl so we must not
3219 * return a buffer-handle to non-master processes! For
3220 * backwards-compatibility reasons, we cannot make
3221 * GET_FB() privileged, so just return an invalid handle
3222 * for non-masters. */
3223 r->handle = 0;
3224 ret = 0;
3225 }
3226 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003227 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003228 }
Dave Airlief453ba02008-11-07 14:05:41 -08003229
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003230 drm_framebuffer_unreference(fb);
3231
Dave Airlief453ba02008-11-07 14:05:41 -08003232 return ret;
3233}
3234
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003235/**
3236 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3237 * @dev: drm device for the ioctl
3238 * @data: data pointer for the ioctl
3239 * @file_priv: drm file for the ioctl call
3240 *
3241 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3242 * rectangle list. Generic userspace which does frontbuffer rendering must call
3243 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3244 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3245 *
3246 * Modesetting drivers which always update the frontbuffer do not need to
3247 * implement the corresponding ->dirty framebuffer callback.
3248 *
3249 * Called by the user via ioctl.
3250 *
3251 * Returns:
3252 * Zero on success, errno on failure.
3253 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003254int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3255 void *data, struct drm_file *file_priv)
3256{
3257 struct drm_clip_rect __user *clips_ptr;
3258 struct drm_clip_rect *clips = NULL;
3259 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003260 struct drm_framebuffer *fb;
3261 unsigned flags;
3262 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003263 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003264
Dave Airliefb3b06c2011-02-08 13:55:21 +10003265 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3266 return -EINVAL;
3267
Daniel Vetter786b99e2012-12-02 21:53:40 +01003268 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003269 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003270 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003271
3272 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003273 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003274
3275 if (!num_clips != !clips_ptr) {
3276 ret = -EINVAL;
3277 goto out_err1;
3278 }
3279
3280 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3281
3282 /* If userspace annotates copy, clips must come in pairs */
3283 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3284 ret = -EINVAL;
3285 goto out_err1;
3286 }
3287
3288 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003289 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3290 ret = -EINVAL;
3291 goto out_err1;
3292 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003293 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3294 if (!clips) {
3295 ret = -ENOMEM;
3296 goto out_err1;
3297 }
3298
3299 ret = copy_from_user(clips, clips_ptr,
3300 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003301 if (ret) {
3302 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003303 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003304 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003305 }
3306
3307 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003308 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3309 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003310 } else {
3311 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003312 }
3313
3314out_err2:
3315 kfree(clips);
3316out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003317 drm_framebuffer_unreference(fb);
3318
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003319 return ret;
3320}
3321
3322
Dave Airlief453ba02008-11-07 14:05:41 -08003323/**
3324 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003325 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003326 *
Dave Airlief453ba02008-11-07 14:05:41 -08003327 * Destroy all the FBs associated with @filp.
3328 *
3329 * Called by the user via ioctl.
3330 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003331 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003332 * Zero on success, errno on failure.
3333 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003334void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003335{
Dave Airlief453ba02008-11-07 14:05:41 -08003336 struct drm_device *dev = priv->minor->dev;
3337 struct drm_framebuffer *fb, *tfb;
3338
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003339 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003340 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003341
3342 mutex_lock(&dev->mode_config.fb_lock);
3343 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3344 __drm_framebuffer_unregister(dev, fb);
3345 mutex_unlock(&dev->mode_config.fb_lock);
3346
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003347 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003348
3349 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003350 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003351 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003352 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003353}
3354
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003355/**
3356 * drm_property_create - create a new property type
3357 * @dev: drm device
3358 * @flags: flags specifying the property type
3359 * @name: name of the property
3360 * @num_values: number of pre-defined values
3361 *
3362 * This creates a new generic drm property which can then be attached to a drm
3363 * object with drm_object_attach_property. The returned property object must be
3364 * freed with drm_property_destroy.
3365 *
3366 * Returns:
3367 * A pointer to the newly created property on success, NULL on failure.
3368 */
Dave Airlief453ba02008-11-07 14:05:41 -08003369struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3370 const char *name, int num_values)
3371{
3372 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003373 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003374
3375 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3376 if (!property)
3377 return NULL;
3378
Rob Clark98f75de2014-05-30 11:37:03 -04003379 property->dev = dev;
3380
Dave Airlief453ba02008-11-07 14:05:41 -08003381 if (num_values) {
3382 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3383 if (!property->values)
3384 goto fail;
3385 }
3386
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003387 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3388 if (ret)
3389 goto fail;
3390
Dave Airlief453ba02008-11-07 14:05:41 -08003391 property->flags = flags;
3392 property->num_values = num_values;
3393 INIT_LIST_HEAD(&property->enum_blob_list);
3394
Vinson Lee471dd2e2011-11-10 11:55:40 -08003395 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003396 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003397 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3398 }
Dave Airlief453ba02008-11-07 14:05:41 -08003399
3400 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003401
3402 WARN_ON(!drm_property_type_valid(property));
3403
Dave Airlief453ba02008-11-07 14:05:41 -08003404 return property;
3405fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003406 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003407 kfree(property);
3408 return NULL;
3409}
3410EXPORT_SYMBOL(drm_property_create);
3411
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003412/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003413 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003414 * @dev: drm device
3415 * @flags: flags specifying the property type
3416 * @name: name of the property
3417 * @props: enumeration lists with property values
3418 * @num_values: number of pre-defined values
3419 *
3420 * This creates a new generic drm property which can then be attached to a drm
3421 * object with drm_object_attach_property. The returned property object must be
3422 * freed with drm_property_destroy.
3423 *
3424 * Userspace is only allowed to set one of the predefined values for enumeration
3425 * properties.
3426 *
3427 * Returns:
3428 * A pointer to the newly created property on success, NULL on failure.
3429 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003430struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3431 const char *name,
3432 const struct drm_prop_enum_list *props,
3433 int num_values)
3434{
3435 struct drm_property *property;
3436 int i, ret;
3437
3438 flags |= DRM_MODE_PROP_ENUM;
3439
3440 property = drm_property_create(dev, flags, name, num_values);
3441 if (!property)
3442 return NULL;
3443
3444 for (i = 0; i < num_values; i++) {
3445 ret = drm_property_add_enum(property, i,
3446 props[i].type,
3447 props[i].name);
3448 if (ret) {
3449 drm_property_destroy(dev, property);
3450 return NULL;
3451 }
3452 }
3453
3454 return property;
3455}
3456EXPORT_SYMBOL(drm_property_create_enum);
3457
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003458/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003459 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003460 * @dev: drm device
3461 * @flags: flags specifying the property type
3462 * @name: name of the property
3463 * @props: enumeration lists with property bitflags
3464 * @num_values: number of pre-defined values
3465 *
3466 * This creates a new generic drm property which can then be attached to a drm
3467 * object with drm_object_attach_property. The returned property object must be
3468 * freed with drm_property_destroy.
3469 *
3470 * Compared to plain enumeration properties userspace is allowed to set any
3471 * or'ed together combination of the predefined property bitflag values
3472 *
3473 * Returns:
3474 * A pointer to the newly created property on success, NULL on failure.
3475 */
Rob Clark49e27542012-05-17 02:23:26 -06003476struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3477 int flags, const char *name,
3478 const struct drm_prop_enum_list *props,
3479 int num_values)
3480{
3481 struct drm_property *property;
3482 int i, ret;
3483
3484 flags |= DRM_MODE_PROP_BITMASK;
3485
3486 property = drm_property_create(dev, flags, name, num_values);
3487 if (!property)
3488 return NULL;
3489
3490 for (i = 0; i < num_values; i++) {
3491 ret = drm_property_add_enum(property, i,
3492 props[i].type,
3493 props[i].name);
3494 if (ret) {
3495 drm_property_destroy(dev, property);
3496 return NULL;
3497 }
3498 }
3499
3500 return property;
3501}
3502EXPORT_SYMBOL(drm_property_create_bitmask);
3503
Rob Clarkebc44cf2012-09-12 22:22:31 -05003504static struct drm_property *property_create_range(struct drm_device *dev,
3505 int flags, const char *name,
3506 uint64_t min, uint64_t max)
3507{
3508 struct drm_property *property;
3509
3510 property = drm_property_create(dev, flags, name, 2);
3511 if (!property)
3512 return NULL;
3513
3514 property->values[0] = min;
3515 property->values[1] = max;
3516
3517 return property;
3518}
3519
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003520/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003521 * drm_property_create_range - create a new ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003522 * @dev: drm device
3523 * @flags: flags specifying the property type
3524 * @name: name of the property
3525 * @min: minimum value of the property
3526 * @max: maximum value of the property
3527 *
3528 * This creates a new generic drm property which can then be attached to a drm
3529 * object with drm_object_attach_property. The returned property object must be
3530 * freed with drm_property_destroy.
3531 *
3532 * Userspace is allowed to set any interger value in the (min, max) range
3533 * inclusive.
3534 *
3535 * Returns:
3536 * A pointer to the newly created property on success, NULL on failure.
3537 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003538struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3539 const char *name,
3540 uint64_t min, uint64_t max)
3541{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003542 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3543 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003544}
3545EXPORT_SYMBOL(drm_property_create_range);
3546
Rob Clarkebc44cf2012-09-12 22:22:31 -05003547struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3548 int flags, const char *name,
3549 int64_t min, int64_t max)
3550{
3551 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3552 name, I642U64(min), I642U64(max));
3553}
3554EXPORT_SYMBOL(drm_property_create_signed_range);
3555
Rob Clark98f75de2014-05-30 11:37:03 -04003556struct drm_property *drm_property_create_object(struct drm_device *dev,
3557 int flags, const char *name, uint32_t type)
3558{
3559 struct drm_property *property;
3560
3561 flags |= DRM_MODE_PROP_OBJECT;
3562
3563 property = drm_property_create(dev, flags, name, 1);
3564 if (!property)
3565 return NULL;
3566
3567 property->values[0] = type;
3568
3569 return property;
3570}
3571EXPORT_SYMBOL(drm_property_create_object);
3572
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003573/**
3574 * drm_property_add_enum - add a possible value to an enumeration property
3575 * @property: enumeration property to change
3576 * @index: index of the new enumeration
3577 * @value: value of the new enumeration
3578 * @name: symbolic name of the new enumeration
3579 *
3580 * This functions adds enumerations to a property.
3581 *
3582 * It's use is deprecated, drivers should use one of the more specific helpers
3583 * to directly create the property with all enumerations already attached.
3584 *
3585 * Returns:
3586 * Zero on success, error code on failure.
3587 */
Dave Airlief453ba02008-11-07 14:05:41 -08003588int drm_property_add_enum(struct drm_property *property, int index,
3589 uint64_t value, const char *name)
3590{
3591 struct drm_property_enum *prop_enum;
3592
Rob Clark5ea22f22014-05-30 11:34:01 -04003593 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3594 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003595 return -EINVAL;
3596
3597 /*
3598 * Bitmask enum properties have the additional constraint of values
3599 * from 0 to 63
3600 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003601 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3602 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003603 return -EINVAL;
3604
3605 if (!list_empty(&property->enum_blob_list)) {
3606 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3607 if (prop_enum->value == value) {
3608 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3609 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3610 return 0;
3611 }
3612 }
3613 }
3614
3615 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3616 if (!prop_enum)
3617 return -ENOMEM;
3618
3619 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3620 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3621 prop_enum->value = value;
3622
3623 property->values[index] = value;
3624 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3625 return 0;
3626}
3627EXPORT_SYMBOL(drm_property_add_enum);
3628
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003629/**
3630 * drm_property_destroy - destroy a drm property
3631 * @dev: drm device
3632 * @property: property to destry
3633 *
3634 * This function frees a property including any attached resources like
3635 * enumeration values.
3636 */
Dave Airlief453ba02008-11-07 14:05:41 -08003637void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3638{
3639 struct drm_property_enum *prop_enum, *pt;
3640
3641 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3642 list_del(&prop_enum->head);
3643 kfree(prop_enum);
3644 }
3645
3646 if (property->num_values)
3647 kfree(property->values);
3648 drm_mode_object_put(dev, &property->base);
3649 list_del(&property->head);
3650 kfree(property);
3651}
3652EXPORT_SYMBOL(drm_property_destroy);
3653
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003654/**
3655 * drm_object_attach_property - attach a property to a modeset object
3656 * @obj: drm modeset object
3657 * @property: property to attach
3658 * @init_val: initial value of the property
3659 *
3660 * This attaches the given property to the modeset object with the given initial
3661 * value. Currently this function cannot fail since the properties are stored in
3662 * a statically sized array.
3663 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003664void drm_object_attach_property(struct drm_mode_object *obj,
3665 struct drm_property *property,
3666 uint64_t init_val)
3667{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003668 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003669
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003670 if (count == DRM_OBJECT_MAX_PROPERTY) {
3671 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3672 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3673 "you see this message on the same object type.\n",
3674 obj->type);
3675 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003676 }
3677
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003678 obj->properties->ids[count] = property->base.id;
3679 obj->properties->values[count] = init_val;
3680 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003681}
3682EXPORT_SYMBOL(drm_object_attach_property);
3683
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003684/**
3685 * drm_object_property_set_value - set the value of a property
3686 * @obj: drm mode object to set property value for
3687 * @property: property to set
3688 * @val: value the property should be set to
3689 *
3690 * This functions sets a given property on a given object. This function only
3691 * changes the software state of the property, it does not call into the
3692 * driver's ->set_property callback.
3693 *
3694 * Returns:
3695 * Zero on success, error code on failure.
3696 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003697int drm_object_property_set_value(struct drm_mode_object *obj,
3698 struct drm_property *property, uint64_t val)
3699{
3700 int i;
3701
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003702 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003703 if (obj->properties->ids[i] == property->base.id) {
3704 obj->properties->values[i] = val;
3705 return 0;
3706 }
3707 }
3708
3709 return -EINVAL;
3710}
3711EXPORT_SYMBOL(drm_object_property_set_value);
3712
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003713/**
3714 * drm_object_property_get_value - retrieve the value of a property
3715 * @obj: drm mode object to get property value from
3716 * @property: property to retrieve
3717 * @val: storage for the property value
3718 *
3719 * This function retrieves the softare state of the given property for the given
3720 * property. Since there is no driver callback to retrieve the current property
3721 * value this might be out of sync with the hardware, depending upon the driver
3722 * and property.
3723 *
3724 * Returns:
3725 * Zero on success, error code on failure.
3726 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003727int drm_object_property_get_value(struct drm_mode_object *obj,
3728 struct drm_property *property, uint64_t *val)
3729{
3730 int i;
3731
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003732 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003733 if (obj->properties->ids[i] == property->base.id) {
3734 *val = obj->properties->values[i];
3735 return 0;
3736 }
3737 }
3738
3739 return -EINVAL;
3740}
3741EXPORT_SYMBOL(drm_object_property_get_value);
3742
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003743/**
3744 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3745 * @dev: DRM device
3746 * @data: ioctl data
3747 * @file_priv: DRM file info
3748 *
3749 * This function retrieves the current value for an connectors's property.
3750 *
3751 * Called by the user via ioctl.
3752 *
3753 * Returns:
3754 * Zero on success, errno on failure.
3755 */
Dave Airlief453ba02008-11-07 14:05:41 -08003756int drm_mode_getproperty_ioctl(struct drm_device *dev,
3757 void *data, struct drm_file *file_priv)
3758{
Dave Airlief453ba02008-11-07 14:05:41 -08003759 struct drm_mode_get_property *out_resp = data;
3760 struct drm_property *property;
3761 int enum_count = 0;
3762 int blob_count = 0;
3763 int value_count = 0;
3764 int ret = 0, i;
3765 int copied;
3766 struct drm_property_enum *prop_enum;
3767 struct drm_mode_property_enum __user *enum_ptr;
3768 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003769 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003770 uint64_t __user *values_ptr;
3771 uint32_t __user *blob_length_ptr;
3772
Dave Airliefb3b06c2011-02-08 13:55:21 +10003773 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3774 return -EINVAL;
3775
Daniel Vetter84849902012-12-02 00:28:11 +01003776 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003777 property = drm_property_find(dev, out_resp->prop_id);
3778 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003779 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003780 goto done;
3781 }
Dave Airlief453ba02008-11-07 14:05:41 -08003782
Rob Clark5ea22f22014-05-30 11:34:01 -04003783 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3784 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003785 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3786 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003787 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003788 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3789 blob_count++;
3790 }
3791
3792 value_count = property->num_values;
3793
3794 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3795 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3796 out_resp->flags = property->flags;
3797
3798 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003799 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003800 for (i = 0; i < value_count; i++) {
3801 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3802 ret = -EFAULT;
3803 goto done;
3804 }
3805 }
3806 }
3807 out_resp->count_values = value_count;
3808
Rob Clark5ea22f22014-05-30 11:34:01 -04003809 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3810 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003811 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3812 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003813 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003814 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3815
3816 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3817 ret = -EFAULT;
3818 goto done;
3819 }
3820
3821 if (copy_to_user(&enum_ptr[copied].name,
3822 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3823 ret = -EFAULT;
3824 goto done;
3825 }
3826 copied++;
3827 }
3828 }
3829 out_resp->count_enum_blobs = enum_count;
3830 }
3831
Rob Clark5ea22f22014-05-30 11:34:01 -04003832 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003833 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3834 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003835 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3836 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003837
3838 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3839 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3840 ret = -EFAULT;
3841 goto done;
3842 }
3843
3844 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3845 ret = -EFAULT;
3846 goto done;
3847 }
3848
3849 copied++;
3850 }
3851 }
3852 out_resp->count_enum_blobs = blob_count;
3853 }
3854done:
Daniel Vetter84849902012-12-02 00:28:11 +01003855 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003856 return ret;
3857}
3858
3859static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3860 void *data)
3861{
3862 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003863 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003864
3865 if (!length || !data)
3866 return NULL;
3867
3868 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3869 if (!blob)
3870 return NULL;
3871
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003872 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3873 if (ret) {
3874 kfree(blob);
3875 return NULL;
3876 }
3877
Dave Airlief453ba02008-11-07 14:05:41 -08003878 blob->length = length;
3879
3880 memcpy(blob->data, data, length);
3881
Dave Airlief453ba02008-11-07 14:05:41 -08003882 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3883 return blob;
3884}
3885
3886static void drm_property_destroy_blob(struct drm_device *dev,
3887 struct drm_property_blob *blob)
3888{
3889 drm_mode_object_put(dev, &blob->base);
3890 list_del(&blob->head);
3891 kfree(blob);
3892}
3893
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003894/**
3895 * drm_mode_getblob_ioctl - get the contents of a blob property value
3896 * @dev: DRM device
3897 * @data: ioctl data
3898 * @file_priv: DRM file info
3899 *
3900 * This function retrieves the contents of a blob property. The value stored in
3901 * an object's blob property is just a normal modeset object id.
3902 *
3903 * Called by the user via ioctl.
3904 *
3905 * Returns:
3906 * Zero on success, errno on failure.
3907 */
Dave Airlief453ba02008-11-07 14:05:41 -08003908int drm_mode_getblob_ioctl(struct drm_device *dev,
3909 void *data, struct drm_file *file_priv)
3910{
Dave Airlief453ba02008-11-07 14:05:41 -08003911 struct drm_mode_get_blob *out_resp = data;
3912 struct drm_property_blob *blob;
3913 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003914 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003915
Dave Airliefb3b06c2011-02-08 13:55:21 +10003916 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3917 return -EINVAL;
3918
Daniel Vetter84849902012-12-02 00:28:11 +01003919 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003920 blob = drm_property_blob_find(dev, out_resp->blob_id);
3921 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003922 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003923 goto done;
3924 }
Dave Airlief453ba02008-11-07 14:05:41 -08003925
3926 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003927 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003928 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3929 ret = -EFAULT;
3930 goto done;
3931 }
3932 }
3933 out_resp->length = blob->length;
3934
3935done:
Daniel Vetter84849902012-12-02 00:28:11 +01003936 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003937 return ret;
3938}
3939
Dave Airlie43aba7e2014-06-05 14:01:31 +10003940int drm_mode_connector_set_path_property(struct drm_connector *connector,
3941 char *path)
3942{
3943 struct drm_device *dev = connector->dev;
3944 int ret, size;
3945 size = strlen(path) + 1;
3946
3947 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
3948 size, path);
3949 if (!connector->path_blob_ptr)
3950 return -EINVAL;
3951
3952 ret = drm_object_property_set_value(&connector->base,
3953 dev->mode_config.path_property,
3954 connector->path_blob_ptr->base.id);
3955 return ret;
3956}
3957EXPORT_SYMBOL(drm_mode_connector_set_path_property);
3958
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003959/**
3960 * drm_mode_connector_update_edid_property - update the edid property of a connector
3961 * @connector: drm connector
3962 * @edid: new value of the edid property
3963 *
3964 * This function creates a new blob modeset object and assigns its id to the
3965 * connector's edid property.
3966 *
3967 * Returns:
3968 * Zero on success, errno on failure.
3969 */
Dave Airlief453ba02008-11-07 14:05:41 -08003970int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3971 struct edid *edid)
3972{
3973 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003974 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003975
Thomas Wood4cf2b282014-06-18 17:52:33 +01003976 /* ignore requests to set edid when overridden */
3977 if (connector->override_edid)
3978 return 0;
3979
Dave Airlief453ba02008-11-07 14:05:41 -08003980 if (connector->edid_blob_ptr)
3981 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3982
3983 /* Delete edid, when there is none. */
3984 if (!edid) {
3985 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003986 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003987 return ret;
3988 }
3989
Adam Jackson7466f4c2010-03-29 21:43:23 +00003990 size = EDID_LENGTH * (1 + edid->extensions);
3991 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3992 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003993 if (!connector->edid_blob_ptr)
3994 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003995
Rob Clark58495562012-10-11 20:50:56 -05003996 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003997 dev->mode_config.edid_property,
3998 connector->edid_blob_ptr->base.id);
3999
4000 return ret;
4001}
4002EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4003
Paulo Zanoni26a34812012-05-15 18:08:59 -03004004static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004005 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004006{
4007 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4008 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004009
4010 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004011 if (value < property->values[0] || value > property->values[1])
4012 return false;
4013 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004014 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4015 int64_t svalue = U642I64(value);
4016 if (svalue < U642I64(property->values[0]) ||
4017 svalue > U642I64(property->values[1]))
4018 return false;
4019 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004020 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06004021 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004022 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06004023 for (i = 0; i < property->num_values; i++)
4024 valid_mask |= (1ULL << property->values[i]);
4025 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004026 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004027 /* Only the driver knows */
4028 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004029 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4030 struct drm_mode_object *obj;
4031 /* a zero value for an object property translates to null: */
4032 if (value == 0)
4033 return true;
4034 /*
4035 * NOTE: use _object_find() directly to bypass restriction on
4036 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4037 * object this could race against object finalization, so it
4038 * simply tells us that the object *was* valid. Which is good
4039 * enough.
4040 */
4041 obj = _object_find(property->dev, value, property->values[0]);
4042 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004043 } else {
4044 int i;
4045 for (i = 0; i < property->num_values; i++)
4046 if (property->values[i] == value)
4047 return true;
4048 return false;
4049 }
4050}
4051
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004052/**
4053 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4054 * @dev: DRM device
4055 * @data: ioctl data
4056 * @file_priv: DRM file info
4057 *
4058 * This function sets the current value for a connectors's property. It also
4059 * calls into a driver's ->set_property callback to update the hardware state
4060 *
4061 * Called by the user via ioctl.
4062 *
4063 * Returns:
4064 * Zero on success, errno on failure.
4065 */
Dave Airlief453ba02008-11-07 14:05:41 -08004066int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4067 void *data, struct drm_file *file_priv)
4068{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004069 struct drm_mode_connector_set_property *conn_set_prop = data;
4070 struct drm_mode_obj_set_property obj_set_prop = {
4071 .value = conn_set_prop->value,
4072 .prop_id = conn_set_prop->prop_id,
4073 .obj_id = conn_set_prop->connector_id,
4074 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4075 };
Dave Airlief453ba02008-11-07 14:05:41 -08004076
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004077 /* It does all the locking and checking we need */
4078 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004079}
4080
Paulo Zanonic5431882012-05-15 18:09:02 -03004081static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4082 struct drm_property *property,
4083 uint64_t value)
4084{
4085 int ret = -EINVAL;
4086 struct drm_connector *connector = obj_to_connector(obj);
4087
4088 /* Do DPMS ourselves */
4089 if (property == connector->dev->mode_config.dpms_property) {
4090 if (connector->funcs->dpms)
4091 (*connector->funcs->dpms)(connector, (int)value);
4092 ret = 0;
4093 } else if (connector->funcs->set_property)
4094 ret = connector->funcs->set_property(connector, property, value);
4095
4096 /* store the property value if successful */
4097 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004098 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004099 return ret;
4100}
4101
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004102static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4103 struct drm_property *property,
4104 uint64_t value)
4105{
4106 int ret = -EINVAL;
4107 struct drm_crtc *crtc = obj_to_crtc(obj);
4108
4109 if (crtc->funcs->set_property)
4110 ret = crtc->funcs->set_property(crtc, property, value);
4111 if (!ret)
4112 drm_object_property_set_value(obj, property, value);
4113
4114 return ret;
4115}
4116
Rob Clark4d939142012-05-17 02:23:27 -06004117static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4118 struct drm_property *property,
4119 uint64_t value)
4120{
4121 int ret = -EINVAL;
4122 struct drm_plane *plane = obj_to_plane(obj);
4123
4124 if (plane->funcs->set_property)
4125 ret = plane->funcs->set_property(plane, property, value);
4126 if (!ret)
4127 drm_object_property_set_value(obj, property, value);
4128
4129 return ret;
4130}
4131
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004132/**
4133 * drm_mode_getproperty_ioctl - get the current value of a object's property
4134 * @dev: DRM device
4135 * @data: ioctl data
4136 * @file_priv: DRM file info
4137 *
4138 * This function retrieves the current value for an object's property. Compared
4139 * to the connector specific ioctl this one is extended to also work on crtc and
4140 * plane objects.
4141 *
4142 * Called by the user via ioctl.
4143 *
4144 * Returns:
4145 * Zero on success, errno on failure.
4146 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004147int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4148 struct drm_file *file_priv)
4149{
4150 struct drm_mode_obj_get_properties *arg = data;
4151 struct drm_mode_object *obj;
4152 int ret = 0;
4153 int i;
4154 int copied = 0;
4155 int props_count = 0;
4156 uint32_t __user *props_ptr;
4157 uint64_t __user *prop_values_ptr;
4158
4159 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4160 return -EINVAL;
4161
Daniel Vetter84849902012-12-02 00:28:11 +01004162 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004163
4164 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4165 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004166 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004167 goto out;
4168 }
4169 if (!obj->properties) {
4170 ret = -EINVAL;
4171 goto out;
4172 }
4173
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004174 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004175
4176 /* This ioctl is called twice, once to determine how much space is
4177 * needed, and the 2nd time to fill it. */
4178 if ((arg->count_props >= props_count) && props_count) {
4179 copied = 0;
4180 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4181 prop_values_ptr = (uint64_t __user *)(unsigned long)
4182 (arg->prop_values_ptr);
4183 for (i = 0; i < props_count; i++) {
4184 if (put_user(obj->properties->ids[i],
4185 props_ptr + copied)) {
4186 ret = -EFAULT;
4187 goto out;
4188 }
4189 if (put_user(obj->properties->values[i],
4190 prop_values_ptr + copied)) {
4191 ret = -EFAULT;
4192 goto out;
4193 }
4194 copied++;
4195 }
4196 }
4197 arg->count_props = props_count;
4198out:
Daniel Vetter84849902012-12-02 00:28:11 +01004199 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004200 return ret;
4201}
4202
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004203/**
4204 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4205 * @dev: DRM device
4206 * @data: ioctl data
4207 * @file_priv: DRM file info
4208 *
4209 * This function sets the current value for an object's property. It also calls
4210 * into a driver's ->set_property callback to update the hardware state.
4211 * Compared to the connector specific ioctl this one is extended to also work on
4212 * crtc and plane objects.
4213 *
4214 * Called by the user via ioctl.
4215 *
4216 * Returns:
4217 * Zero on success, errno on failure.
4218 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004219int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4220 struct drm_file *file_priv)
4221{
4222 struct drm_mode_obj_set_property *arg = data;
4223 struct drm_mode_object *arg_obj;
4224 struct drm_mode_object *prop_obj;
4225 struct drm_property *property;
4226 int ret = -EINVAL;
4227 int i;
4228
4229 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4230 return -EINVAL;
4231
Daniel Vetter84849902012-12-02 00:28:11 +01004232 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004233
4234 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004235 if (!arg_obj) {
4236 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004237 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004238 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004239 if (!arg_obj->properties)
4240 goto out;
4241
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004242 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004243 if (arg_obj->properties->ids[i] == arg->prop_id)
4244 break;
4245
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004246 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004247 goto out;
4248
4249 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4250 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004251 if (!prop_obj) {
4252 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004253 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004254 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004255 property = obj_to_property(prop_obj);
4256
4257 if (!drm_property_change_is_valid(property, arg->value))
4258 goto out;
4259
4260 switch (arg_obj->type) {
4261 case DRM_MODE_OBJECT_CONNECTOR:
4262 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4263 arg->value);
4264 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004265 case DRM_MODE_OBJECT_CRTC:
4266 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4267 break;
Rob Clark4d939142012-05-17 02:23:27 -06004268 case DRM_MODE_OBJECT_PLANE:
4269 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4270 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004271 }
4272
4273out:
Daniel Vetter84849902012-12-02 00:28:11 +01004274 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004275 return ret;
4276}
4277
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004278/**
4279 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4280 * @connector: connector to attach
4281 * @encoder: encoder to attach @connector to
4282 *
4283 * This function links up a connector to an encoder. Note that the routing
4284 * restrictions between encoders and crtcs are exposed to userspace through the
4285 * possible_clones and possible_crtcs bitmasks.
4286 *
4287 * Returns:
4288 * Zero on success, errno on failure.
4289 */
Dave Airlief453ba02008-11-07 14:05:41 -08004290int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4291 struct drm_encoder *encoder)
4292{
4293 int i;
4294
4295 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4296 if (connector->encoder_ids[i] == 0) {
4297 connector->encoder_ids[i] = encoder->base.id;
4298 return 0;
4299 }
4300 }
4301 return -ENOMEM;
4302}
4303EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4304
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004305/**
4306 * drm_mode_crtc_set_gamma_size - set the gamma table size
4307 * @crtc: CRTC to set the gamma table size for
4308 * @gamma_size: size of the gamma table
4309 *
4310 * Drivers which support gamma tables should set this to the supported gamma
4311 * table size when initializing the CRTC. Currently the drm core only supports a
4312 * fixed gamma table size.
4313 *
4314 * Returns:
4315 * Zero on success, errno on failure.
4316 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004317int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004318 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004319{
4320 crtc->gamma_size = gamma_size;
4321
4322 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4323 if (!crtc->gamma_store) {
4324 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004325 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004326 }
4327
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004328 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004329}
4330EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4331
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004332/**
4333 * drm_mode_gamma_set_ioctl - set the gamma table
4334 * @dev: DRM device
4335 * @data: ioctl data
4336 * @file_priv: DRM file info
4337 *
4338 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4339 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4340 *
4341 * Called by the user via ioctl.
4342 *
4343 * Returns:
4344 * Zero on success, errno on failure.
4345 */
Dave Airlief453ba02008-11-07 14:05:41 -08004346int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4347 void *data, struct drm_file *file_priv)
4348{
4349 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004350 struct drm_crtc *crtc;
4351 void *r_base, *g_base, *b_base;
4352 int size;
4353 int ret = 0;
4354
Dave Airliefb3b06c2011-02-08 13:55:21 +10004355 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4356 return -EINVAL;
4357
Daniel Vetter84849902012-12-02 00:28:11 +01004358 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004359 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4360 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004361 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004362 goto out;
4363 }
Dave Airlief453ba02008-11-07 14:05:41 -08004364
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004365 if (crtc->funcs->gamma_set == NULL) {
4366 ret = -ENOSYS;
4367 goto out;
4368 }
4369
Dave Airlief453ba02008-11-07 14:05:41 -08004370 /* memcpy into gamma store */
4371 if (crtc_lut->gamma_size != crtc->gamma_size) {
4372 ret = -EINVAL;
4373 goto out;
4374 }
4375
4376 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4377 r_base = crtc->gamma_store;
4378 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4379 ret = -EFAULT;
4380 goto out;
4381 }
4382
4383 g_base = r_base + size;
4384 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4385 ret = -EFAULT;
4386 goto out;
4387 }
4388
4389 b_base = g_base + size;
4390 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4391 ret = -EFAULT;
4392 goto out;
4393 }
4394
James Simmons72034252010-08-03 01:33:19 +01004395 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004396
4397out:
Daniel Vetter84849902012-12-02 00:28:11 +01004398 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004399 return ret;
4400
4401}
4402
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004403/**
4404 * drm_mode_gamma_get_ioctl - get the gamma table
4405 * @dev: DRM device
4406 * @data: ioctl data
4407 * @file_priv: DRM file info
4408 *
4409 * Copy the current gamma table into the storage provided. This also provides
4410 * the gamma table size the driver expects, which can be used to size the
4411 * allocated storage.
4412 *
4413 * Called by the user via ioctl.
4414 *
4415 * Returns:
4416 * Zero on success, errno on failure.
4417 */
Dave Airlief453ba02008-11-07 14:05:41 -08004418int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4419 void *data, struct drm_file *file_priv)
4420{
4421 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004422 struct drm_crtc *crtc;
4423 void *r_base, *g_base, *b_base;
4424 int size;
4425 int ret = 0;
4426
Dave Airliefb3b06c2011-02-08 13:55:21 +10004427 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4428 return -EINVAL;
4429
Daniel Vetter84849902012-12-02 00:28:11 +01004430 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004431 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4432 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004433 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004434 goto out;
4435 }
Dave Airlief453ba02008-11-07 14:05:41 -08004436
4437 /* memcpy into gamma store */
4438 if (crtc_lut->gamma_size != crtc->gamma_size) {
4439 ret = -EINVAL;
4440 goto out;
4441 }
4442
4443 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4444 r_base = crtc->gamma_store;
4445 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4446 ret = -EFAULT;
4447 goto out;
4448 }
4449
4450 g_base = r_base + size;
4451 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4452 ret = -EFAULT;
4453 goto out;
4454 }
4455
4456 b_base = g_base + size;
4457 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4458 ret = -EFAULT;
4459 goto out;
4460 }
4461out:
Daniel Vetter84849902012-12-02 00:28:11 +01004462 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004463 return ret;
4464}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004465
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004466/**
4467 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4468 * @dev: DRM device
4469 * @data: ioctl data
4470 * @file_priv: DRM file info
4471 *
4472 * This schedules an asynchronous update on a given CRTC, called page flip.
4473 * Optionally a drm event is generated to signal the completion of the event.
4474 * Generic drivers cannot assume that a pageflip with changed framebuffer
4475 * properties (including driver specific metadata like tiling layout) will work,
4476 * but some drivers support e.g. pixel format changes through the pageflip
4477 * ioctl.
4478 *
4479 * Called by the user via ioctl.
4480 *
4481 * Returns:
4482 * Zero on success, errno on failure.
4483 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004484int drm_mode_page_flip_ioctl(struct drm_device *dev,
4485 void *data, struct drm_file *file_priv)
4486{
4487 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004488 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004489 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004490 struct drm_pending_vblank_event *e = NULL;
4491 unsigned long flags;
4492 int ret = -EINVAL;
4493
4494 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4495 page_flip->reserved != 0)
4496 return -EINVAL;
4497
Keith Packard62f21042013-07-22 18:50:00 -07004498 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4499 return -EINVAL;
4500
Rob Clarka2b34e22013-10-05 16:36:52 -04004501 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4502 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004503 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004504
Rob Clark51fd3712013-11-19 12:10:12 -05004505 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004506 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004507 /* The framebuffer is currently unbound, presumably
4508 * due to a hotplug event, that userspace has not
4509 * yet discovered.
4510 */
4511 ret = -EBUSY;
4512 goto out;
4513 }
4514
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004515 if (crtc->funcs->page_flip == NULL)
4516 goto out;
4517
Daniel Vetter786b99e2012-12-02 21:53:40 +01004518 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004519 if (!fb) {
4520 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004521 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004522 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004523
Damien Lespiauc11e9282013-09-25 16:45:30 +01004524 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4525 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004526 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004527
Matt Roperf4510a22014-04-01 15:22:40 -07004528 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004529 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4530 ret = -EINVAL;
4531 goto out;
4532 }
4533
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004534 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4535 ret = -ENOMEM;
4536 spin_lock_irqsave(&dev->event_lock, flags);
4537 if (file_priv->event_space < sizeof e->event) {
4538 spin_unlock_irqrestore(&dev->event_lock, flags);
4539 goto out;
4540 }
4541 file_priv->event_space -= sizeof e->event;
4542 spin_unlock_irqrestore(&dev->event_lock, flags);
4543
4544 e = kzalloc(sizeof *e, GFP_KERNEL);
4545 if (e == NULL) {
4546 spin_lock_irqsave(&dev->event_lock, flags);
4547 file_priv->event_space += sizeof e->event;
4548 spin_unlock_irqrestore(&dev->event_lock, flags);
4549 goto out;
4550 }
4551
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004552 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004553 e->event.base.length = sizeof e->event;
4554 e->event.user_data = page_flip->user_data;
4555 e->base.event = &e->event.base;
4556 e->base.file_priv = file_priv;
4557 e->base.destroy =
4558 (void (*) (struct drm_pending_event *)) kfree;
4559 }
4560
Matt Roperf4510a22014-04-01 15:22:40 -07004561 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004562 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004563 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004564 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4565 spin_lock_irqsave(&dev->event_lock, flags);
4566 file_priv->event_space += sizeof e->event;
4567 spin_unlock_irqrestore(&dev->event_lock, flags);
4568 kfree(e);
4569 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004570 /* Keep the old fb, don't unref it. */
4571 old_fb = NULL;
4572 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004573 /*
4574 * Warn if the driver hasn't properly updated the crtc->fb
4575 * field to reflect that the new framebuffer is now used.
4576 * Failing to do so will screw with the reference counting
4577 * on framebuffers.
4578 */
Matt Roperf4510a22014-04-01 15:22:40 -07004579 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004580 /* Unref only the old framebuffer. */
4581 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004582 }
4583
4584out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004585 if (fb)
4586 drm_framebuffer_unreference(fb);
4587 if (old_fb)
4588 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004589 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004590
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004591 return ret;
4592}
Chris Wilsoneb033552011-01-24 15:11:08 +00004593
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004594/**
4595 * drm_mode_config_reset - call ->reset callbacks
4596 * @dev: drm device
4597 *
4598 * This functions calls all the crtc's, encoder's and connector's ->reset
4599 * callback. Drivers can use this in e.g. their driver load or resume code to
4600 * reset hardware and software state.
4601 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004602void drm_mode_config_reset(struct drm_device *dev)
4603{
4604 struct drm_crtc *crtc;
4605 struct drm_encoder *encoder;
4606 struct drm_connector *connector;
4607
4608 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4609 if (crtc->funcs->reset)
4610 crtc->funcs->reset(crtc);
4611
4612 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4613 if (encoder->funcs->reset)
4614 encoder->funcs->reset(encoder);
4615
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004616 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4617 connector->status = connector_status_unknown;
4618
Chris Wilsoneb033552011-01-24 15:11:08 +00004619 if (connector->funcs->reset)
4620 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004621 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004622}
4623EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004624
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004625/**
4626 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4627 * @dev: DRM device
4628 * @data: ioctl data
4629 * @file_priv: DRM file info
4630 *
4631 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4632 * TTM or something else entirely) and returns the resulting buffer handle. This
4633 * handle can then be wrapped up into a framebuffer modeset object.
4634 *
4635 * Note that userspace is not allowed to use such objects for render
4636 * acceleration - drivers must create their own private ioctls for such a use
4637 * case.
4638 *
4639 * Called by the user via ioctl.
4640 *
4641 * Returns:
4642 * Zero on success, errno on failure.
4643 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004644int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4645 void *data, struct drm_file *file_priv)
4646{
4647 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004648 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004649
4650 if (!dev->driver->dumb_create)
4651 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004652 if (!args->width || !args->height || !args->bpp)
4653 return -EINVAL;
4654
4655 /* overflow checks for 32bit size calculations */
4656 cpp = DIV_ROUND_UP(args->bpp, 8);
4657 if (cpp > 0xffffffffU / args->width)
4658 return -EINVAL;
4659 stride = cpp * args->width;
4660 if (args->height > 0xffffffffU / stride)
4661 return -EINVAL;
4662
4663 /* test for wrap-around */
4664 size = args->height * stride;
4665 if (PAGE_ALIGN(size) == 0)
4666 return -EINVAL;
4667
Dave Airlieff72145b2011-02-07 12:16:14 +10004668 return dev->driver->dumb_create(file_priv, dev, args);
4669}
4670
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004671/**
4672 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4673 * @dev: DRM device
4674 * @data: ioctl data
4675 * @file_priv: DRM file info
4676 *
4677 * Allocate an offset in the drm device node's address space to be able to
4678 * memory map a dumb buffer.
4679 *
4680 * Called by the user via ioctl.
4681 *
4682 * Returns:
4683 * Zero on success, errno on failure.
4684 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004685int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4686 void *data, struct drm_file *file_priv)
4687{
4688 struct drm_mode_map_dumb *args = data;
4689
4690 /* call driver ioctl to get mmap offset */
4691 if (!dev->driver->dumb_map_offset)
4692 return -ENOSYS;
4693
4694 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4695}
4696
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004697/**
4698 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4699 * @dev: DRM device
4700 * @data: ioctl data
4701 * @file_priv: DRM file info
4702 *
4703 * This destroys the userspace handle for the given dumb backing storage buffer.
4704 * Since buffer objects must be reference counted in the kernel a buffer object
4705 * won't be immediately freed if a framebuffer modeset object still uses it.
4706 *
4707 * Called by the user via ioctl.
4708 *
4709 * Returns:
4710 * Zero on success, errno on failure.
4711 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004712int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4713 void *data, struct drm_file *file_priv)
4714{
4715 struct drm_mode_destroy_dumb *args = data;
4716
4717 if (!dev->driver->dumb_destroy)
4718 return -ENOSYS;
4719
4720 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4721}
Dave Airlie248dbc22011-11-29 20:02:54 +00004722
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004723/**
4724 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4725 * @format: pixel format (DRM_FORMAT_*)
4726 * @depth: storage for the depth value
4727 * @bpp: storage for the bpp value
4728 *
4729 * This only supports RGB formats here for compat with code that doesn't use
4730 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004731 */
4732void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4733 int *bpp)
4734{
4735 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004736 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004737 case DRM_FORMAT_RGB332:
4738 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004739 *depth = 8;
4740 *bpp = 8;
4741 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004742 case DRM_FORMAT_XRGB1555:
4743 case DRM_FORMAT_XBGR1555:
4744 case DRM_FORMAT_RGBX5551:
4745 case DRM_FORMAT_BGRX5551:
4746 case DRM_FORMAT_ARGB1555:
4747 case DRM_FORMAT_ABGR1555:
4748 case DRM_FORMAT_RGBA5551:
4749 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004750 *depth = 15;
4751 *bpp = 16;
4752 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004753 case DRM_FORMAT_RGB565:
4754 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004755 *depth = 16;
4756 *bpp = 16;
4757 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004758 case DRM_FORMAT_RGB888:
4759 case DRM_FORMAT_BGR888:
4760 *depth = 24;
4761 *bpp = 24;
4762 break;
4763 case DRM_FORMAT_XRGB8888:
4764 case DRM_FORMAT_XBGR8888:
4765 case DRM_FORMAT_RGBX8888:
4766 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004767 *depth = 24;
4768 *bpp = 32;
4769 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004770 case DRM_FORMAT_XRGB2101010:
4771 case DRM_FORMAT_XBGR2101010:
4772 case DRM_FORMAT_RGBX1010102:
4773 case DRM_FORMAT_BGRX1010102:
4774 case DRM_FORMAT_ARGB2101010:
4775 case DRM_FORMAT_ABGR2101010:
4776 case DRM_FORMAT_RGBA1010102:
4777 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004778 *depth = 30;
4779 *bpp = 32;
4780 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004781 case DRM_FORMAT_ARGB8888:
4782 case DRM_FORMAT_ABGR8888:
4783 case DRM_FORMAT_RGBA8888:
4784 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004785 *depth = 32;
4786 *bpp = 32;
4787 break;
4788 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004789 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4790 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004791 *depth = 0;
4792 *bpp = 0;
4793 break;
4794 }
4795}
4796EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004797
4798/**
4799 * drm_format_num_planes - get the number of planes for format
4800 * @format: pixel format (DRM_FORMAT_*)
4801 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004802 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004803 * The number of planes used by the specified pixel format.
4804 */
4805int drm_format_num_planes(uint32_t format)
4806{
4807 switch (format) {
4808 case DRM_FORMAT_YUV410:
4809 case DRM_FORMAT_YVU410:
4810 case DRM_FORMAT_YUV411:
4811 case DRM_FORMAT_YVU411:
4812 case DRM_FORMAT_YUV420:
4813 case DRM_FORMAT_YVU420:
4814 case DRM_FORMAT_YUV422:
4815 case DRM_FORMAT_YVU422:
4816 case DRM_FORMAT_YUV444:
4817 case DRM_FORMAT_YVU444:
4818 return 3;
4819 case DRM_FORMAT_NV12:
4820 case DRM_FORMAT_NV21:
4821 case DRM_FORMAT_NV16:
4822 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004823 case DRM_FORMAT_NV24:
4824 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004825 return 2;
4826 default:
4827 return 1;
4828 }
4829}
4830EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004831
4832/**
4833 * drm_format_plane_cpp - determine the bytes per pixel value
4834 * @format: pixel format (DRM_FORMAT_*)
4835 * @plane: plane index
4836 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004837 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004838 * The bytes per pixel value for the specified plane.
4839 */
4840int drm_format_plane_cpp(uint32_t format, int plane)
4841{
4842 unsigned int depth;
4843 int bpp;
4844
4845 if (plane >= drm_format_num_planes(format))
4846 return 0;
4847
4848 switch (format) {
4849 case DRM_FORMAT_YUYV:
4850 case DRM_FORMAT_YVYU:
4851 case DRM_FORMAT_UYVY:
4852 case DRM_FORMAT_VYUY:
4853 return 2;
4854 case DRM_FORMAT_NV12:
4855 case DRM_FORMAT_NV21:
4856 case DRM_FORMAT_NV16:
4857 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004858 case DRM_FORMAT_NV24:
4859 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004860 return plane ? 2 : 1;
4861 case DRM_FORMAT_YUV410:
4862 case DRM_FORMAT_YVU410:
4863 case DRM_FORMAT_YUV411:
4864 case DRM_FORMAT_YVU411:
4865 case DRM_FORMAT_YUV420:
4866 case DRM_FORMAT_YVU420:
4867 case DRM_FORMAT_YUV422:
4868 case DRM_FORMAT_YVU422:
4869 case DRM_FORMAT_YUV444:
4870 case DRM_FORMAT_YVU444:
4871 return 1;
4872 default:
4873 drm_fb_get_bpp_depth(format, &depth, &bpp);
4874 return bpp >> 3;
4875 }
4876}
4877EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004878
4879/**
4880 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4881 * @format: pixel format (DRM_FORMAT_*)
4882 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004883 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004884 * The horizontal chroma subsampling factor for the
4885 * specified pixel format.
4886 */
4887int drm_format_horz_chroma_subsampling(uint32_t format)
4888{
4889 switch (format) {
4890 case DRM_FORMAT_YUV411:
4891 case DRM_FORMAT_YVU411:
4892 case DRM_FORMAT_YUV410:
4893 case DRM_FORMAT_YVU410:
4894 return 4;
4895 case DRM_FORMAT_YUYV:
4896 case DRM_FORMAT_YVYU:
4897 case DRM_FORMAT_UYVY:
4898 case DRM_FORMAT_VYUY:
4899 case DRM_FORMAT_NV12:
4900 case DRM_FORMAT_NV21:
4901 case DRM_FORMAT_NV16:
4902 case DRM_FORMAT_NV61:
4903 case DRM_FORMAT_YUV422:
4904 case DRM_FORMAT_YVU422:
4905 case DRM_FORMAT_YUV420:
4906 case DRM_FORMAT_YVU420:
4907 return 2;
4908 default:
4909 return 1;
4910 }
4911}
4912EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4913
4914/**
4915 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4916 * @format: pixel format (DRM_FORMAT_*)
4917 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004918 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004919 * The vertical chroma subsampling factor for the
4920 * specified pixel format.
4921 */
4922int drm_format_vert_chroma_subsampling(uint32_t format)
4923{
4924 switch (format) {
4925 case DRM_FORMAT_YUV410:
4926 case DRM_FORMAT_YVU410:
4927 return 4;
4928 case DRM_FORMAT_YUV420:
4929 case DRM_FORMAT_YVU420:
4930 case DRM_FORMAT_NV12:
4931 case DRM_FORMAT_NV21:
4932 return 2;
4933 default:
4934 return 1;
4935 }
4936}
4937EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004938
4939/**
4940 * drm_mode_config_init - initialize DRM mode_configuration structure
4941 * @dev: DRM device
4942 *
4943 * Initialize @dev's mode_config structure, used for tracking the graphics
4944 * configuration of @dev.
4945 *
4946 * Since this initializes the modeset locks, no locking is possible. Which is no
4947 * problem, since this should happen single threaded at init time. It is the
4948 * driver's problem to ensure this guarantee.
4949 *
4950 */
4951void drm_mode_config_init(struct drm_device *dev)
4952{
4953 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004954 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004955 mutex_init(&dev->mode_config.idr_mutex);
4956 mutex_init(&dev->mode_config.fb_lock);
4957 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4958 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4959 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004960 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004961 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4962 INIT_LIST_HEAD(&dev->mode_config.property_list);
4963 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4964 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4965 idr_init(&dev->mode_config.crtc_idr);
4966
4967 drm_modeset_lock_all(dev);
4968 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004969 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004970 drm_modeset_unlock_all(dev);
4971
4972 /* Just to be sure */
4973 dev->mode_config.num_fb = 0;
4974 dev->mode_config.num_connector = 0;
4975 dev->mode_config.num_crtc = 0;
4976 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004977 dev->mode_config.num_overlay_plane = 0;
4978 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004979}
4980EXPORT_SYMBOL(drm_mode_config_init);
4981
4982/**
4983 * drm_mode_config_cleanup - free up DRM mode_config info
4984 * @dev: DRM device
4985 *
4986 * Free up all the connectors and CRTCs associated with this DRM device, then
4987 * free up the framebuffers and associated buffer objects.
4988 *
4989 * Note that since this /should/ happen single-threaded at driver/device
4990 * teardown time, no locking is required. It's the driver's job to ensure that
4991 * this guarantee actually holds true.
4992 *
4993 * FIXME: cleanup any dangling user buffer objects too
4994 */
4995void drm_mode_config_cleanup(struct drm_device *dev)
4996{
4997 struct drm_connector *connector, *ot;
4998 struct drm_crtc *crtc, *ct;
4999 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04005000 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005001 struct drm_framebuffer *fb, *fbt;
5002 struct drm_property *property, *pt;
5003 struct drm_property_blob *blob, *bt;
5004 struct drm_plane *plane, *plt;
5005
5006 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5007 head) {
5008 encoder->funcs->destroy(encoder);
5009 }
5010
Sean Paul3b336ec2013-08-14 16:47:37 -04005011 list_for_each_entry_safe(bridge, brt,
5012 &dev->mode_config.bridge_list, head) {
5013 bridge->funcs->destroy(bridge);
5014 }
5015
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005016 list_for_each_entry_safe(connector, ot,
5017 &dev->mode_config.connector_list, head) {
5018 connector->funcs->destroy(connector);
5019 }
5020
5021 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5022 head) {
5023 drm_property_destroy(dev, property);
5024 }
5025
5026 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5027 head) {
5028 drm_property_destroy_blob(dev, blob);
5029 }
5030
5031 /*
5032 * Single-threaded teardown context, so it's not required to grab the
5033 * fb_lock to protect against concurrent fb_list access. Contrary, it
5034 * would actually deadlock with the drm_framebuffer_cleanup function.
5035 *
5036 * Also, if there are any framebuffers left, that's a driver leak now,
5037 * so politely WARN about this.
5038 */
5039 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5040 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5041 drm_framebuffer_remove(fb);
5042 }
5043
5044 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5045 head) {
5046 plane->funcs->destroy(plane);
5047 }
5048
5049 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5050 crtc->funcs->destroy(crtc);
5051 }
5052
5053 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005054 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005055}
5056EXPORT_SYMBOL(drm_mode_config_cleanup);