blob: 3c4a62169f288237fa19ade7e0829467d097dd04 [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
Vandana Kannanff587e42014-06-11 10:46:48 +0530185static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
186 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
187 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
188 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
189};
190
Dave Airlief453ba02008-11-07 14:05:41 -0800191/*
192 * Non-global properties, but "required" for certain connectors.
193 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000194static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800195{
196 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
197 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
198 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
199};
200
201DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
202
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000203static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800204{
205 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
206 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
207 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
208};
209
210DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
211 drm_dvi_i_subconnector_enum_list)
212
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000213static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800214{
215 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
216 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
217 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
218 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200219 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800220};
221
222DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
223
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000224static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800225{
226 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
227 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
228 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
229 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200230 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800231};
232
233DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
234 drm_tv_subconnector_enum_list)
235
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000236static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000237 { DRM_MODE_DIRTY_OFF, "Off" },
238 { DRM_MODE_DIRTY_ON, "On" },
239 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
240};
241
Dave Airlief453ba02008-11-07 14:05:41 -0800242struct drm_conn_prop_enum_list {
243 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000244 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400245 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800246};
247
248/*
249 * Connector and encoder types.
250 */
251static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400252{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
253 { DRM_MODE_CONNECTOR_VGA, "VGA" },
254 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
255 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
256 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
257 { DRM_MODE_CONNECTOR_Composite, "Composite" },
258 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
259 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
260 { DRM_MODE_CONNECTOR_Component, "Component" },
261 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
262 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
263 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
264 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
265 { DRM_MODE_CONNECTOR_TV, "TV" },
266 { DRM_MODE_CONNECTOR_eDP, "eDP" },
267 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300268 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800269};
270
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000271static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800272{ { DRM_MODE_ENCODER_NONE, "None" },
273 { DRM_MODE_ENCODER_DAC, "DAC" },
274 { DRM_MODE_ENCODER_TMDS, "TMDS" },
275 { DRM_MODE_ENCODER_LVDS, "LVDS" },
276 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200277 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300278 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000279 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800280};
281
Jesse Barnesac1bb362014-02-10 15:32:44 -0800282static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
283{
284 { SubPixelUnknown, "Unknown" },
285 { SubPixelHorizontalRGB, "Horizontal RGB" },
286 { SubPixelHorizontalBGR, "Horizontal BGR" },
287 { SubPixelVerticalRGB, "Vertical RGB" },
288 { SubPixelVerticalBGR, "Vertical BGR" },
289 { SubPixelNone, "None" },
290};
291
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400292void drm_connector_ida_init(void)
293{
294 int i;
295
296 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
297 ida_init(&drm_connector_enum_list[i].ida);
298}
299
300void drm_connector_ida_destroy(void)
301{
302 int i;
303
304 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
305 ida_destroy(&drm_connector_enum_list[i].ida);
306}
307
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100308/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100309 * drm_get_connector_status_name - return a string for connector status
310 * @status: connector status to compute name of
311 *
312 * In contrast to the other drm_get_*_name functions this one here returns a
313 * const pointer and hence is threadsafe.
314 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000315const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800316{
317 if (status == connector_status_connected)
318 return "connected";
319 else if (status == connector_status_disconnected)
320 return "disconnected";
321 else
322 return "unknown";
323}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000324EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800325
Jesse Barnesac1bb362014-02-10 15:32:44 -0800326/**
327 * drm_get_subpixel_order_name - return a string for a given subpixel enum
328 * @order: enum of subpixel_order
329 *
330 * Note you could abuse this and return something out of bounds, but that
331 * would be a caller error. No unscrubbed user data should make it here.
332 */
333const char *drm_get_subpixel_order_name(enum subpixel_order order)
334{
335 return drm_subpixel_enum_list[order].name;
336}
337EXPORT_SYMBOL(drm_get_subpixel_order_name);
338
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300339static char printable_char(int c)
340{
341 return isascii(c) && isprint(c) ? c : '?';
342}
343
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100344/**
345 * drm_get_format_name - return a string for drm fourcc format
346 * @format: format to compute name of
347 *
348 * Note that the buffer used by this function is globally shared and owned by
349 * the function itself.
350 *
351 * FIXME: This isn't really multithreading safe.
352 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000353const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300354{
355 static char buf[32];
356
357 snprintf(buf, sizeof(buf),
358 "%c%c%c%c %s-endian (0x%08x)",
359 printable_char(format & 0xff),
360 printable_char((format >> 8) & 0xff),
361 printable_char((format >> 16) & 0xff),
362 printable_char((format >> 24) & 0x7f),
363 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
364 format);
365
366 return buf;
367}
368EXPORT_SYMBOL(drm_get_format_name);
369
Dave Airlie2ee39452014-07-24 11:53:45 +1000370/*
371 * Internal function to assign a slot in the object idr and optionally
372 * register the object into the idr.
373 */
374static int drm_mode_object_get_reg(struct drm_device *dev,
375 struct drm_mode_object *obj,
376 uint32_t obj_type,
377 bool register_obj)
378{
379 int ret;
380
381 mutex_lock(&dev->mode_config.idr_mutex);
382 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
383 if (ret >= 0) {
384 /*
385 * Set up the object linking under the protection of the idr
386 * lock so that other users can't see inconsistent state.
387 */
388 obj->id = ret;
389 obj->type = obj_type;
390 }
391 mutex_unlock(&dev->mode_config.idr_mutex);
392
393 return ret < 0 ? ret : 0;
394}
395
Dave Airlief453ba02008-11-07 14:05:41 -0800396/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100397 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800398 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100399 * @obj: object pointer, used to generate unique ID
400 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800401 *
Dave Airlief453ba02008-11-07 14:05:41 -0800402 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100403 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
404 * modeset identifiers are _not_ reference counted. Hence don't use this for
405 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800406 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100407 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800408 * New unique (relative to other objects in @dev) integer identifier for the
409 * object.
410 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100411int drm_mode_object_get(struct drm_device *dev,
412 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800413{
Dave Airlie2ee39452014-07-24 11:53:45 +1000414 return drm_mode_object_get_reg(dev, obj, obj_type, true);
415}
Dave Airlief453ba02008-11-07 14:05:41 -0800416
Dave Airlie2ee39452014-07-24 11:53:45 +1000417static void drm_mode_object_register(struct drm_device *dev,
418 struct drm_mode_object *obj)
419{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000420 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000421 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000422 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800423}
424
425/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100426 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800427 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100428 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800429 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100430 * Free @id from @dev's unique identifier pool. Note that despite the _get
431 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
432 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800433 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100434void drm_mode_object_put(struct drm_device *dev,
435 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800436{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000437 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800438 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000439 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800440}
441
Rob Clark98f75de2014-05-30 11:37:03 -0400442static struct drm_mode_object *_object_find(struct drm_device *dev,
443 uint32_t id, uint32_t type)
444{
445 struct drm_mode_object *obj = NULL;
446
447 mutex_lock(&dev->mode_config.idr_mutex);
448 obj = idr_find(&dev->mode_config.crtc_idr, id);
449 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
450 (obj->id != id))
451 obj = NULL;
452 mutex_unlock(&dev->mode_config.idr_mutex);
453
454 return obj;
455}
456
Daniel Vetter786b99e2012-12-02 21:53:40 +0100457/**
458 * drm_mode_object_find - look up a drm object with static lifetime
459 * @dev: drm device
460 * @id: id of the mode object
461 * @type: type of the mode object
462 *
463 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400464 * are reference counted, they need special treatment. Even with
465 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
466 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100467 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200468struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
469 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800470{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000471 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800472
Daniel Vetter786b99e2012-12-02 21:53:40 +0100473 /* Framebuffers are reference counted and need their own lookup
474 * function.*/
475 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400476 obj = _object_find(dev, id, type);
477 /* don't leak out unref'd fb's */
478 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000479 obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800480 return obj;
481}
482EXPORT_SYMBOL(drm_mode_object_find);
483
484/**
Dave Airlief453ba02008-11-07 14:05:41 -0800485 * drm_framebuffer_init - initialize a framebuffer
486 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100487 * @fb: framebuffer to be initialized
488 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800489 *
Dave Airlief453ba02008-11-07 14:05:41 -0800490 * Allocates an ID for the framebuffer's parent mode object, sets its mode
491 * functions & device file and adds it to the master fd list.
492 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100493 * IMPORTANT:
494 * This functions publishes the fb and makes it available for concurrent access
495 * by other users. Which means by this point the fb _must_ be fully set up -
496 * since all the fb attributes are invariant over its lifetime, no further
497 * locking but only correct reference counting is required.
498 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100499 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200500 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800501 */
502int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
503 const struct drm_framebuffer_funcs *funcs)
504{
505 int ret;
506
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100507 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000508 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100509 INIT_LIST_HEAD(&fb->filp_head);
510 fb->dev = dev;
511 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000512
Dave Airlief453ba02008-11-07 14:05:41 -0800513 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200514 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100515 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800516
Daniel Vetter2b677e82012-12-10 21:16:05 +0100517 /* Grab the idr reference. */
518 drm_framebuffer_reference(fb);
519
Dave Airlief453ba02008-11-07 14:05:41 -0800520 dev->mode_config.num_fb++;
521 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100522out:
523 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800524
525 return 0;
526}
527EXPORT_SYMBOL(drm_framebuffer_init);
528
Rob Clarkf7eff602012-09-05 21:48:38 +0000529static void drm_framebuffer_free(struct kref *kref)
530{
531 struct drm_framebuffer *fb =
532 container_of(kref, struct drm_framebuffer, refcount);
533 fb->funcs->destroy(fb);
534}
535
Daniel Vetter2b677e82012-12-10 21:16:05 +0100536static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
537 uint32_t id)
538{
539 struct drm_mode_object *obj = NULL;
540 struct drm_framebuffer *fb;
541
542 mutex_lock(&dev->mode_config.idr_mutex);
543 obj = idr_find(&dev->mode_config.crtc_idr, id);
544 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
545 fb = NULL;
546 else
547 fb = obj_to_fb(obj);
548 mutex_unlock(&dev->mode_config.idr_mutex);
549
550 return fb;
551}
552
Rob Clarkf7eff602012-09-05 21:48:38 +0000553/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100554 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
555 * @dev: drm device
556 * @id: id of the fb object
557 *
558 * If successful, this grabs an additional reference to the framebuffer -
559 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100560 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100561 */
562struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
563 uint32_t id)
564{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100565 struct drm_framebuffer *fb;
566
567 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100568 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100569 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000570 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100571 mutex_unlock(&dev->mode_config.fb_lock);
572
573 return fb;
574}
575EXPORT_SYMBOL(drm_framebuffer_lookup);
576
577/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000578 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100579 * @fb: framebuffer to unref
580 *
581 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000582 */
583void drm_framebuffer_unreference(struct drm_framebuffer *fb)
584{
Rob Clark82912722014-03-18 10:07:08 -0400585 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000586 kref_put(&fb->refcount, drm_framebuffer_free);
587}
588EXPORT_SYMBOL(drm_framebuffer_unreference);
589
590/**
591 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100592 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100593 *
594 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000595 */
596void drm_framebuffer_reference(struct drm_framebuffer *fb)
597{
Rob Clark82912722014-03-18 10:07:08 -0400598 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000599 kref_get(&fb->refcount);
600}
601EXPORT_SYMBOL(drm_framebuffer_reference);
602
Daniel Vetter2b677e82012-12-10 21:16:05 +0100603static void drm_framebuffer_free_bug(struct kref *kref)
604{
605 BUG();
606}
607
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100608static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
609{
Rob Clark82912722014-03-18 10:07:08 -0400610 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100611 kref_put(&fb->refcount, drm_framebuffer_free_bug);
612}
613
Daniel Vetter2b677e82012-12-10 21:16:05 +0100614/* dev->mode_config.fb_lock must be held! */
615static void __drm_framebuffer_unregister(struct drm_device *dev,
616 struct drm_framebuffer *fb)
617{
618 mutex_lock(&dev->mode_config.idr_mutex);
619 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
620 mutex_unlock(&dev->mode_config.idr_mutex);
621
622 fb->base.id = 0;
623
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100624 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100625}
626
Dave Airlief453ba02008-11-07 14:05:41 -0800627/**
Daniel Vetter36206362012-12-10 20:42:17 +0100628 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
629 * @fb: fb to unregister
630 *
631 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
632 * those used for fbdev. Note that the caller must hold a reference of it's own,
633 * i.e. the object may not be destroyed through this call (since it'll lead to a
634 * locking inversion).
635 */
636void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
637{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100638 struct drm_device *dev = fb->dev;
639
640 mutex_lock(&dev->mode_config.fb_lock);
641 /* Mark fb as reaped and drop idr ref. */
642 __drm_framebuffer_unregister(dev, fb);
643 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100644}
645EXPORT_SYMBOL(drm_framebuffer_unregister_private);
646
647/**
Dave Airlief453ba02008-11-07 14:05:41 -0800648 * drm_framebuffer_cleanup - remove a framebuffer object
649 * @fb: framebuffer to remove
650 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100651 * Cleanup framebuffer. This function is intended to be used from the drivers
652 * ->destroy callback. It can also be used to clean up driver private
653 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100654 *
655 * Note that this function does not remove the fb from active usuage - if it is
656 * still used anywhere, hilarity can ensue since userspace could call getfb on
657 * the id and get back -EINVAL. Obviously no concern at driver unload time.
658 *
659 * Also, the framebuffer will not be removed from the lookup idr - for
660 * user-created framebuffers this will happen in in the rmfb ioctl. For
661 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
662 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800663 */
664void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
665{
666 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100667
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100668 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000669 list_del(&fb->head);
670 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100671 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000672}
673EXPORT_SYMBOL(drm_framebuffer_cleanup);
674
675/**
676 * drm_framebuffer_remove - remove and unreference a framebuffer object
677 * @fb: framebuffer to remove
678 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000679 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100680 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100681 * passed-in framebuffer. Might take the modeset locks.
682 *
683 * Note that this function optimizes the cleanup away if the caller holds the
684 * last reference to the framebuffer. It is also guaranteed to not take the
685 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000686 */
687void drm_framebuffer_remove(struct drm_framebuffer *fb)
688{
689 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800690 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800691 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000692 struct drm_mode_set set;
693 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800694
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100695 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100696
Daniel Vetterb62584e2012-12-11 16:51:35 +0100697 /*
698 * drm ABI mandates that we remove any deleted framebuffers from active
699 * useage. But since most sane clients only remove framebuffers they no
700 * longer need, try to optimize this away.
701 *
702 * Since we're holding a reference ourselves, observing a refcount of 1
703 * means that we're the last holder and can skip it. Also, the refcount
704 * can never increase from 1 again, so we don't need any barriers or
705 * locks.
706 *
707 * Note that userspace could try to race with use and instate a new
708 * usage _after_ we've cleared all current ones. End result will be an
709 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
710 * in this manner.
711 */
712 if (atomic_read(&fb->refcount.refcount) > 1) {
713 drm_modeset_lock_all(dev);
714 /* remove from any CRTC */
715 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700716 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100717 /* should turn off the crtc */
718 memset(&set, 0, sizeof(struct drm_mode_set));
719 set.crtc = crtc;
720 set.fb = NULL;
721 ret = drm_mode_set_config_internal(&set);
722 if (ret)
723 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
724 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000725 }
Dave Airlief453ba02008-11-07 14:05:41 -0800726
Daniel Vetterb62584e2012-12-11 16:51:35 +0100727 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300728 if (plane->fb == fb)
729 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800730 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100731 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800732 }
733
Rob Clarkf7eff602012-09-05 21:48:38 +0000734 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800735}
Rob Clarkf7eff602012-09-05 21:48:38 +0000736EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800737
Rob Clark51fd3712013-11-19 12:10:12 -0500738DEFINE_WW_CLASS(crtc_ww_class);
739
Dave Airlief453ba02008-11-07 14:05:41 -0800740/**
Matt Ropere13161a2014-04-01 15:22:38 -0700741 * drm_crtc_init_with_planes - Initialise a new CRTC object with
742 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800743 * @dev: DRM device
744 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700745 * @primary: Primary plane for CRTC
746 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800747 * @funcs: callbacks for the new CRTC
748 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000749 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200750 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100751 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200752 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800753 */
Matt Ropere13161a2014-04-01 15:22:38 -0700754int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
755 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700756 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700757 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800758{
Rob Clark51fd3712013-11-19 12:10:12 -0500759 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200760 int ret;
761
Dave Airlief453ba02008-11-07 14:05:41 -0800762 crtc->dev = dev;
763 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000764 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800765
Daniel Vetter84849902012-12-02 00:28:11 +0100766 drm_modeset_lock_all(dev);
Rob Clark51fd3712013-11-19 12:10:12 -0500767 drm_modeset_lock_init(&crtc->mutex);
768 /* dropped by _unlock_all(): */
769 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200770
771 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
772 if (ret)
773 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800774
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300775 crtc->base.properties = &crtc->properties;
776
Rob Clark51fd3712013-11-19 12:10:12 -0500777 list_add_tail(&crtc->head, &config->crtc_list);
778 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200779
Matt Ropere13161a2014-04-01 15:22:38 -0700780 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700781 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700782 if (primary)
783 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700784 if (cursor)
785 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700786
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200787 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100788 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200789
790 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800791}
Matt Ropere13161a2014-04-01 15:22:38 -0700792EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800793
794/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000795 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800796 * @crtc: CRTC to cleanup
797 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000798 * This function cleans up @crtc and removes it from the DRM mode setting
799 * core. Note that the function does *not* free the crtc structure itself,
800 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800801 */
802void drm_crtc_cleanup(struct drm_crtc *crtc)
803{
804 struct drm_device *dev = crtc->dev;
805
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000806 kfree(crtc->gamma_store);
807 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800808
Rob Clark51fd3712013-11-19 12:10:12 -0500809 drm_modeset_lock_fini(&crtc->mutex);
810
Dave Airlief453ba02008-11-07 14:05:41 -0800811 drm_mode_object_put(dev, &crtc->base);
812 list_del(&crtc->head);
813 dev->mode_config.num_crtc--;
814}
815EXPORT_SYMBOL(drm_crtc_cleanup);
816
817/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000818 * drm_crtc_index - find the index of a registered CRTC
819 * @crtc: CRTC to find index for
820 *
821 * Given a registered CRTC, return the index of that CRTC within a DRM
822 * device's list of CRTCs.
823 */
824unsigned int drm_crtc_index(struct drm_crtc *crtc)
825{
826 unsigned int index = 0;
827 struct drm_crtc *tmp;
828
829 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
830 if (tmp == crtc)
831 return index;
832
833 index++;
834 }
835
836 BUG();
837}
838EXPORT_SYMBOL(drm_crtc_index);
839
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100840/*
Dave Airlief453ba02008-11-07 14:05:41 -0800841 * drm_mode_remove - remove and free a mode
842 * @connector: connector list to modify
843 * @mode: mode to remove
844 *
Dave Airlief453ba02008-11-07 14:05:41 -0800845 * Remove @mode from @connector's mode list, then free it.
846 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100847static void drm_mode_remove(struct drm_connector *connector,
848 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800849{
850 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100851 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800852}
Dave Airlief453ba02008-11-07 14:05:41 -0800853
854/**
855 * drm_connector_init - Init a preallocated connector
856 * @dev: DRM device
857 * @connector: the connector to init
858 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100859 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800860 *
Dave Airlief453ba02008-11-07 14:05:41 -0800861 * Initialises a preallocated connector. Connectors should be
862 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200863 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100864 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200865 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800866 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200867int drm_connector_init(struct drm_device *dev,
868 struct drm_connector *connector,
869 const struct drm_connector_funcs *funcs,
870 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800871{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200872 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400873 struct ida *connector_ida =
874 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200875
Daniel Vetter84849902012-12-02 00:28:11 +0100876 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800877
Dave Airlie2ee39452014-07-24 11:53:45 +1000878 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200879 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300880 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200881
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300882 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800883 connector->dev = dev;
884 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800885 connector->connector_type = connector_type;
886 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400887 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
888 if (connector->connector_type_id < 0) {
889 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300890 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400891 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300892 connector->name =
893 kasprintf(GFP_KERNEL, "%s-%d",
894 drm_connector_enum_list[connector_type].name,
895 connector->connector_type_id);
896 if (!connector->name) {
897 ret = -ENOMEM;
898 goto out_put;
899 }
900
Dave Airlief453ba02008-11-07 14:05:41 -0800901 INIT_LIST_HEAD(&connector->probed_modes);
902 INIT_LIST_HEAD(&connector->modes);
903 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000904 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800905
906 list_add_tail(&connector->head, &dev->mode_config.connector_list);
907 dev->mode_config.num_connector++;
908
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200909 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500910 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200911 dev->mode_config.edid_property,
912 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800913
Rob Clark58495562012-10-11 20:50:56 -0500914 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800915 dev->mode_config.dpms_property, 0);
916
Thomas Wood30f65702014-06-18 17:52:32 +0100917 connector->debugfs_entry = NULL;
918
Jani Nikula2abdd312014-05-14 16:58:19 +0300919out_put:
920 if (ret)
921 drm_mode_object_put(dev, &connector->base);
922
923out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100924 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200925
926 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800927}
928EXPORT_SYMBOL(drm_connector_init);
929
930/**
931 * drm_connector_cleanup - cleans up an initialised connector
932 * @connector: connector to cleanup
933 *
Dave Airlief453ba02008-11-07 14:05:41 -0800934 * Cleans up the connector but doesn't free the object.
935 */
936void drm_connector_cleanup(struct drm_connector *connector)
937{
938 struct drm_device *dev = connector->dev;
939 struct drm_display_mode *mode, *t;
940
941 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
942 drm_mode_remove(connector, mode);
943
944 list_for_each_entry_safe(mode, t, &connector->modes, head)
945 drm_mode_remove(connector, mode);
946
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400947 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
948 connector->connector_type_id);
949
Dave Airlief453ba02008-11-07 14:05:41 -0800950 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300951 kfree(connector->name);
952 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800953 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000954 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800955}
956EXPORT_SYMBOL(drm_connector_cleanup);
957
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100958/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100959 * drm_connector_register - register a connector
960 * @connector: the connector to register
961 *
962 * Register userspace interfaces for a connector
963 *
964 * Returns:
965 * Zero on success, error code on failure.
966 */
967int drm_connector_register(struct drm_connector *connector)
968{
Thomas Wood30f65702014-06-18 17:52:32 +0100969 int ret;
970
Dave Airlie2ee39452014-07-24 11:53:45 +1000971 drm_mode_object_register(connector->dev, &connector->base);
972
Thomas Wood30f65702014-06-18 17:52:32 +0100973 ret = drm_sysfs_connector_add(connector);
974 if (ret)
975 return ret;
976
977 ret = drm_debugfs_connector_add(connector);
978 if (ret) {
979 drm_sysfs_connector_remove(connector);
980 return ret;
981 }
982
983 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100984}
985EXPORT_SYMBOL(drm_connector_register);
986
987/**
988 * drm_connector_unregister - unregister a connector
989 * @connector: the connector to unregister
990 *
991 * Unregister userspace interfaces for a connector
992 */
993void drm_connector_unregister(struct drm_connector *connector)
994{
995 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +0100996 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100997}
998EXPORT_SYMBOL(drm_connector_unregister);
999
1000
1001/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001002 * drm_connector_unplug_all - unregister connector userspace interfaces
1003 * @dev: drm device
1004 *
1005 * This function unregisters all connector userspace interfaces in sysfs. Should
1006 * be call when the device is disconnected, e.g. from an usb driver's
1007 * ->disconnect callback.
1008 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001009void drm_connector_unplug_all(struct drm_device *dev)
1010{
1011 struct drm_connector *connector;
1012
1013 /* taking the mode config mutex ends up in a clash with sysfs */
1014 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001015 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001016
1017}
1018EXPORT_SYMBOL(drm_connector_unplug_all);
1019
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001020/**
1021 * drm_bridge_init - initialize a drm transcoder/bridge
1022 * @dev: drm device
1023 * @bridge: transcoder/bridge to set up
1024 * @funcs: bridge function table
1025 *
1026 * Initialises a preallocated bridge. Bridges should be
1027 * subclassed as part of driver connector objects.
1028 *
1029 * Returns:
1030 * Zero on success, error code on failure.
1031 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001032int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1033 const struct drm_bridge_funcs *funcs)
1034{
1035 int ret;
1036
1037 drm_modeset_lock_all(dev);
1038
1039 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1040 if (ret)
1041 goto out;
1042
1043 bridge->dev = dev;
1044 bridge->funcs = funcs;
1045
1046 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1047 dev->mode_config.num_bridge++;
1048
1049 out:
1050 drm_modeset_unlock_all(dev);
1051 return ret;
1052}
1053EXPORT_SYMBOL(drm_bridge_init);
1054
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001055/**
1056 * drm_bridge_cleanup - cleans up an initialised bridge
1057 * @bridge: bridge to cleanup
1058 *
1059 * Cleans up the bridge but doesn't free the object.
1060 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001061void drm_bridge_cleanup(struct drm_bridge *bridge)
1062{
1063 struct drm_device *dev = bridge->dev;
1064
1065 drm_modeset_lock_all(dev);
1066 drm_mode_object_put(dev, &bridge->base);
1067 list_del(&bridge->head);
1068 dev->mode_config.num_bridge--;
1069 drm_modeset_unlock_all(dev);
1070}
1071EXPORT_SYMBOL(drm_bridge_cleanup);
1072
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001073/**
1074 * drm_encoder_init - Init a preallocated encoder
1075 * @dev: drm device
1076 * @encoder: the encoder to init
1077 * @funcs: callbacks for this encoder
1078 * @encoder_type: user visible type of the encoder
1079 *
1080 * Initialises a preallocated encoder. Encoder should be
1081 * subclassed as part of driver encoder objects.
1082 *
1083 * Returns:
1084 * Zero on success, error code on failure.
1085 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001086int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001087 struct drm_encoder *encoder,
1088 const struct drm_encoder_funcs *funcs,
1089 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001090{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001091 int ret;
1092
Daniel Vetter84849902012-12-02 00:28:11 +01001093 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001094
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001095 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1096 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001097 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001098
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001099 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001100 encoder->encoder_type = encoder_type;
1101 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001102 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1103 drm_encoder_enum_list[encoder_type].name,
1104 encoder->base.id);
1105 if (!encoder->name) {
1106 ret = -ENOMEM;
1107 goto out_put;
1108 }
Dave Airlief453ba02008-11-07 14:05:41 -08001109
1110 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1111 dev->mode_config.num_encoder++;
1112
Jani Nikulae5748942014-05-14 16:58:20 +03001113out_put:
1114 if (ret)
1115 drm_mode_object_put(dev, &encoder->base);
1116
1117out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001118 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001119
1120 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001121}
1122EXPORT_SYMBOL(drm_encoder_init);
1123
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001124/**
1125 * drm_encoder_cleanup - cleans up an initialised encoder
1126 * @encoder: encoder to cleanup
1127 *
1128 * Cleans up the encoder but doesn't free the object.
1129 */
Dave Airlief453ba02008-11-07 14:05:41 -08001130void drm_encoder_cleanup(struct drm_encoder *encoder)
1131{
1132 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001133 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001134 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001135 kfree(encoder->name);
1136 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001137 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001138 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001139 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001140}
1141EXPORT_SYMBOL(drm_encoder_cleanup);
1142
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001143/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001144 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001145 * @dev: DRM device
1146 * @plane: plane object to init
1147 * @possible_crtcs: bitmask of possible CRTCs
1148 * @funcs: callbacks for the new plane
1149 * @formats: array of supported formats (%DRM_FORMAT_*)
1150 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001151 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001152 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001153 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001154 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001155 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001156 * Zero on success, error code on failure.
1157 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001158int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1159 unsigned long possible_crtcs,
1160 const struct drm_plane_funcs *funcs,
1161 const uint32_t *formats, uint32_t format_count,
1162 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001163{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001164 int ret;
1165
Daniel Vetter84849902012-12-02 00:28:11 +01001166 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001167
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001168 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1169 if (ret)
1170 goto out;
1171
Rob Clark4d939142012-05-17 02:23:27 -06001172 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001173 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001174 plane->funcs = funcs;
1175 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1176 GFP_KERNEL);
1177 if (!plane->format_types) {
1178 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1179 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001180 ret = -ENOMEM;
1181 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001182 }
1183
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001184 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001185 plane->format_count = format_count;
1186 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001187 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001188
Matt Roperdc415ff2014-04-01 15:22:36 -07001189 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1190 dev->mode_config.num_total_plane++;
1191 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1192 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001193
Rob Clark9922ab52014-04-01 20:16:57 -04001194 drm_object_attach_property(&plane->base,
1195 dev->mode_config.plane_type_property,
1196 plane->type);
1197
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001198 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001199 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001200
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001201 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001202}
Matt Roperdc415ff2014-04-01 15:22:36 -07001203EXPORT_SYMBOL(drm_universal_plane_init);
1204
1205/**
1206 * drm_plane_init - Initialize a legacy plane
1207 * @dev: DRM device
1208 * @plane: plane object to init
1209 * @possible_crtcs: bitmask of possible CRTCs
1210 * @funcs: callbacks for the new plane
1211 * @formats: array of supported formats (%DRM_FORMAT_*)
1212 * @format_count: number of elements in @formats
1213 * @is_primary: plane type (primary vs overlay)
1214 *
1215 * Legacy API to initialize a DRM plane.
1216 *
1217 * New drivers should call drm_universal_plane_init() instead.
1218 *
1219 * Returns:
1220 * Zero on success, error code on failure.
1221 */
1222int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1223 unsigned long possible_crtcs,
1224 const struct drm_plane_funcs *funcs,
1225 const uint32_t *formats, uint32_t format_count,
1226 bool is_primary)
1227{
1228 enum drm_plane_type type;
1229
1230 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1231 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1232 formats, format_count, type);
1233}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001234EXPORT_SYMBOL(drm_plane_init);
1235
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001236/**
1237 * drm_plane_cleanup - Clean up the core plane usage
1238 * @plane: plane to cleanup
1239 *
1240 * This function cleans up @plane and removes it from the DRM mode setting
1241 * core. Note that the function does *not* free the plane structure itself,
1242 * this is the responsibility of the caller.
1243 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001244void drm_plane_cleanup(struct drm_plane *plane)
1245{
1246 struct drm_device *dev = plane->dev;
1247
Daniel Vetter84849902012-12-02 00:28:11 +01001248 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001249 kfree(plane->format_types);
1250 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001251
1252 BUG_ON(list_empty(&plane->head));
1253
1254 list_del(&plane->head);
1255 dev->mode_config.num_total_plane--;
1256 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1257 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001258 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001259}
1260EXPORT_SYMBOL(drm_plane_cleanup);
1261
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001262/**
1263 * drm_plane_force_disable - Forcibly disable a plane
1264 * @plane: plane to disable
1265 *
1266 * Forces the plane to be disabled.
1267 *
1268 * Used when the plane's current framebuffer is destroyed,
1269 * and when restoring fbdev mode.
1270 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001271void drm_plane_force_disable(struct drm_plane *plane)
1272{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001273 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001274 int ret;
1275
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001276 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001277 return;
1278
1279 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001280 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001281 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001282 return;
1283 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001284 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001285 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001286 plane->fb = NULL;
1287 plane->crtc = NULL;
1288}
1289EXPORT_SYMBOL(drm_plane_force_disable);
1290
Dave Airlief453ba02008-11-07 14:05:41 -08001291static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1292{
1293 struct drm_property *edid;
1294 struct drm_property *dpms;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001295 struct drm_property *dev_path;
Dave Airlief453ba02008-11-07 14:05:41 -08001296
1297 /*
1298 * Standard properties (apply to all connectors)
1299 */
1300 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1301 DRM_MODE_PROP_IMMUTABLE,
1302 "EDID", 0);
1303 dev->mode_config.edid_property = edid;
1304
Sascha Hauer4a67d392012-02-06 10:58:17 +01001305 dpms = drm_property_create_enum(dev, 0,
1306 "DPMS", drm_dpms_enum_list,
1307 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001308 dev->mode_config.dpms_property = dpms;
1309
Dave Airlie43aba7e2014-06-05 14:01:31 +10001310 dev_path = drm_property_create(dev,
1311 DRM_MODE_PROP_BLOB |
1312 DRM_MODE_PROP_IMMUTABLE,
1313 "PATH", 0);
1314 dev->mode_config.path_property = dev_path;
1315
Dave Airlief453ba02008-11-07 14:05:41 -08001316 return 0;
1317}
1318
Rob Clark9922ab52014-04-01 20:16:57 -04001319static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1320{
1321 struct drm_property *type;
1322
1323 /*
1324 * Standard properties (apply to all planes)
1325 */
1326 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1327 "type", drm_plane_type_enum_list,
1328 ARRAY_SIZE(drm_plane_type_enum_list));
1329 dev->mode_config.plane_type_property = type;
1330
1331 return 0;
1332}
1333
Dave Airlief453ba02008-11-07 14:05:41 -08001334/**
1335 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1336 * @dev: DRM device
1337 *
1338 * Called by a driver the first time a DVI-I connector is made.
1339 */
1340int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1341{
1342 struct drm_property *dvi_i_selector;
1343 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001344
1345 if (dev->mode_config.dvi_i_select_subconnector_property)
1346 return 0;
1347
1348 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001349 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001350 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001351 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001352 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001353 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1354
Sascha Hauer4a67d392012-02-06 10:58:17 +01001355 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001356 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001357 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001358 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001359 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1360
1361 return 0;
1362}
1363EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1364
1365/**
1366 * drm_create_tv_properties - create TV specific connector properties
1367 * @dev: DRM device
1368 * @num_modes: number of different TV formats (modes) supported
1369 * @modes: array of pointers to strings containing name of each format
1370 *
1371 * Called by a driver's TV initialization routine, this function creates
1372 * the TV specific connector properties for a given device. Caller is
1373 * responsible for allocating a list of format names and passing them to
1374 * this routine.
1375 */
1376int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1377 char *modes[])
1378{
1379 struct drm_property *tv_selector;
1380 struct drm_property *tv_subconnector;
1381 int i;
1382
1383 if (dev->mode_config.tv_select_subconnector_property)
1384 return 0;
1385
1386 /*
1387 * Basic connector properties
1388 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001389 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001390 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001391 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001392 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001393 dev->mode_config.tv_select_subconnector_property = tv_selector;
1394
1395 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001396 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1397 "subconnector",
1398 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001399 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001400 dev->mode_config.tv_subconnector_property = tv_subconnector;
1401
1402 /*
1403 * Other, TV specific properties: margins & TV modes.
1404 */
1405 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001406 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001407
1408 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001409 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001410
1411 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001412 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001413
1414 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001415 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001416
1417 dev->mode_config.tv_mode_property =
1418 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1419 "mode", num_modes);
1420 for (i = 0; i < num_modes; i++)
1421 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1422 i, modes[i]);
1423
Francisco Jerezb6b79022009-08-02 04:19:20 +02001424 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001425 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001426
1427 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001428 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001429
1430 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001431 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001432
Francisco Jereza75f0232009-08-12 02:30:10 +02001433 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001434 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001435
1436 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001437 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001438
1439 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001440 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001441
Dave Airlief453ba02008-11-07 14:05:41 -08001442 return 0;
1443}
1444EXPORT_SYMBOL(drm_mode_create_tv_properties);
1445
1446/**
1447 * drm_mode_create_scaling_mode_property - create scaling mode property
1448 * @dev: DRM device
1449 *
1450 * Called by a driver the first time it's needed, must be attached to desired
1451 * connectors.
1452 */
1453int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1454{
1455 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001456
1457 if (dev->mode_config.scaling_mode_property)
1458 return 0;
1459
1460 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001461 drm_property_create_enum(dev, 0, "scaling mode",
1462 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001463 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001464
1465 dev->mode_config.scaling_mode_property = scaling_mode;
1466
1467 return 0;
1468}
1469EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1470
1471/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301472 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1473 * @dev: DRM device
1474 *
1475 * Called by a driver the first time it's needed, must be attached to desired
1476 * connectors.
1477 *
1478 * Returns:
1479 * Zero on success, errno on failure.
1480 */
1481int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1482{
1483 if (dev->mode_config.aspect_ratio_property)
1484 return 0;
1485
1486 dev->mode_config.aspect_ratio_property =
1487 drm_property_create_enum(dev, 0, "aspect ratio",
1488 drm_aspect_ratio_enum_list,
1489 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1490
1491 if (dev->mode_config.aspect_ratio_property == NULL)
1492 return -ENOMEM;
1493
1494 return 0;
1495}
1496EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1497
1498/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001499 * drm_mode_create_dirty_property - create dirty property
1500 * @dev: DRM device
1501 *
1502 * Called by a driver the first time it's needed, must be attached to desired
1503 * connectors.
1504 */
1505int drm_mode_create_dirty_info_property(struct drm_device *dev)
1506{
1507 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001508
1509 if (dev->mode_config.dirty_info_property)
1510 return 0;
1511
1512 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001513 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001514 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001515 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001516 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001517 dev->mode_config.dirty_info_property = dirty_info;
1518
1519 return 0;
1520}
1521EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1522
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001523static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001524{
1525 uint32_t total_objects = 0;
1526
1527 total_objects += dev->mode_config.num_crtc;
1528 total_objects += dev->mode_config.num_connector;
1529 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001530 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001531
Dave Airlief453ba02008-11-07 14:05:41 -08001532 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1533 if (!group->id_list)
1534 return -ENOMEM;
1535
1536 group->num_crtcs = 0;
1537 group->num_connectors = 0;
1538 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001539 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001540 return 0;
1541}
1542
Dave Airliead222792014-05-02 13:22:19 +10001543void drm_mode_group_destroy(struct drm_mode_group *group)
1544{
1545 kfree(group->id_list);
1546 group->id_list = NULL;
1547}
1548
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001549/*
1550 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1551 * the drm core's responsibility to set up mode control groups.
1552 */
Dave Airlief453ba02008-11-07 14:05:41 -08001553int drm_mode_group_init_legacy_group(struct drm_device *dev,
1554 struct drm_mode_group *group)
1555{
1556 struct drm_crtc *crtc;
1557 struct drm_encoder *encoder;
1558 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001559 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001560 int ret;
1561
1562 if ((ret = drm_mode_group_init(dev, group)))
1563 return ret;
1564
1565 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1566 group->id_list[group->num_crtcs++] = crtc->base.id;
1567
1568 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1569 group->id_list[group->num_crtcs + group->num_encoders++] =
1570 encoder->base.id;
1571
1572 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1573 group->id_list[group->num_crtcs + group->num_encoders +
1574 group->num_connectors++] = connector->base.id;
1575
Sean Paul3b336ec2013-08-14 16:47:37 -04001576 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1577 group->id_list[group->num_crtcs + group->num_encoders +
1578 group->num_connectors + group->num_bridges++] =
1579 bridge->base.id;
1580
Dave Airlief453ba02008-11-07 14:05:41 -08001581 return 0;
1582}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001583EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001584
Dave Airlie2390cd12014-06-05 14:01:29 +10001585void drm_reinit_primary_mode_group(struct drm_device *dev)
1586{
1587 drm_modeset_lock_all(dev);
1588 drm_mode_group_destroy(&dev->primary->mode_group);
1589 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1590 drm_modeset_unlock_all(dev);
1591}
1592EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1593
Dave Airlief453ba02008-11-07 14:05:41 -08001594/**
Dave Airlief453ba02008-11-07 14:05:41 -08001595 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1596 * @out: drm_mode_modeinfo struct to return to the user
1597 * @in: drm_display_mode to use
1598 *
Dave Airlief453ba02008-11-07 14:05:41 -08001599 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1600 * the user.
1601 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001602static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1603 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001604{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001605 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1606 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1607 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1608 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1609 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1610 "timing values too large for mode info\n");
1611
Dave Airlief453ba02008-11-07 14:05:41 -08001612 out->clock = in->clock;
1613 out->hdisplay = in->hdisplay;
1614 out->hsync_start = in->hsync_start;
1615 out->hsync_end = in->hsync_end;
1616 out->htotal = in->htotal;
1617 out->hskew = in->hskew;
1618 out->vdisplay = in->vdisplay;
1619 out->vsync_start = in->vsync_start;
1620 out->vsync_end = in->vsync_end;
1621 out->vtotal = in->vtotal;
1622 out->vscan = in->vscan;
1623 out->vrefresh = in->vrefresh;
1624 out->flags = in->flags;
1625 out->type = in->type;
1626 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1627 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1628}
1629
1630/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001631 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001632 * @out: drm_display_mode to return to the user
1633 * @in: drm_mode_modeinfo to use
1634 *
Dave Airlief453ba02008-11-07 14:05:41 -08001635 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1636 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001637 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001638 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001639 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001640 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001641static int drm_crtc_convert_umode(struct drm_display_mode *out,
1642 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001643{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001644 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1645 return -ERANGE;
1646
Damien Lespiau5848ad42013-09-27 12:11:50 +01001647 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1648 return -EINVAL;
1649
Dave Airlief453ba02008-11-07 14:05:41 -08001650 out->clock = in->clock;
1651 out->hdisplay = in->hdisplay;
1652 out->hsync_start = in->hsync_start;
1653 out->hsync_end = in->hsync_end;
1654 out->htotal = in->htotal;
1655 out->hskew = in->hskew;
1656 out->vdisplay = in->vdisplay;
1657 out->vsync_start = in->vsync_start;
1658 out->vsync_end = in->vsync_end;
1659 out->vtotal = in->vtotal;
1660 out->vscan = in->vscan;
1661 out->vrefresh = in->vrefresh;
1662 out->flags = in->flags;
1663 out->type = in->type;
1664 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1665 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001666
1667 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001668}
1669
1670/**
1671 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001672 * @dev: drm device for the ioctl
1673 * @data: data pointer for the ioctl
1674 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001675 *
Dave Airlief453ba02008-11-07 14:05:41 -08001676 * Construct a set of configuration description structures and return
1677 * them to the user, including CRTC, connector and framebuffer configuration.
1678 *
1679 * Called by the user via ioctl.
1680 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001681 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001682 * Zero on success, errno on failure.
1683 */
1684int drm_mode_getresources(struct drm_device *dev, void *data,
1685 struct drm_file *file_priv)
1686{
1687 struct drm_mode_card_res *card_res = data;
1688 struct list_head *lh;
1689 struct drm_framebuffer *fb;
1690 struct drm_connector *connector;
1691 struct drm_crtc *crtc;
1692 struct drm_encoder *encoder;
1693 int ret = 0;
1694 int connector_count = 0;
1695 int crtc_count = 0;
1696 int fb_count = 0;
1697 int encoder_count = 0;
1698 int copied = 0, i;
1699 uint32_t __user *fb_id;
1700 uint32_t __user *crtc_id;
1701 uint32_t __user *connector_id;
1702 uint32_t __user *encoder_id;
1703 struct drm_mode_group *mode_group;
1704
Dave Airliefb3b06c2011-02-08 13:55:21 +10001705 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1706 return -EINVAL;
1707
Dave Airlief453ba02008-11-07 14:05:41 -08001708
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001709 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001710 /*
1711 * For the non-control nodes we need to limit the list of resources
1712 * by IDs in the group list for this node
1713 */
1714 list_for_each(lh, &file_priv->fbs)
1715 fb_count++;
1716
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001717 /* handle this in 4 parts */
1718 /* FBs */
1719 if (card_res->count_fbs >= fb_count) {
1720 copied = 0;
1721 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1722 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1723 if (put_user(fb->base.id, fb_id + copied)) {
1724 mutex_unlock(&file_priv->fbs_lock);
1725 return -EFAULT;
1726 }
1727 copied++;
1728 }
1729 }
1730 card_res->count_fbs = fb_count;
1731 mutex_unlock(&file_priv->fbs_lock);
1732
1733 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001734 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001735
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001736 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001737 list_for_each(lh, &dev->mode_config.crtc_list)
1738 crtc_count++;
1739
1740 list_for_each(lh, &dev->mode_config.connector_list)
1741 connector_count++;
1742
1743 list_for_each(lh, &dev->mode_config.encoder_list)
1744 encoder_count++;
1745 } else {
1746
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001747 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001748 crtc_count = mode_group->num_crtcs;
1749 connector_count = mode_group->num_connectors;
1750 encoder_count = mode_group->num_encoders;
1751 }
1752
1753 card_res->max_height = dev->mode_config.max_height;
1754 card_res->min_height = dev->mode_config.min_height;
1755 card_res->max_width = dev->mode_config.max_width;
1756 card_res->min_width = dev->mode_config.min_width;
1757
Dave Airlief453ba02008-11-07 14:05:41 -08001758 /* CRTCs */
1759 if (card_res->count_crtcs >= crtc_count) {
1760 copied = 0;
1761 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001762 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001763 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1764 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001765 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001766 if (put_user(crtc->base.id, crtc_id + copied)) {
1767 ret = -EFAULT;
1768 goto out;
1769 }
1770 copied++;
1771 }
1772 } else {
1773 for (i = 0; i < mode_group->num_crtcs; i++) {
1774 if (put_user(mode_group->id_list[i],
1775 crtc_id + copied)) {
1776 ret = -EFAULT;
1777 goto out;
1778 }
1779 copied++;
1780 }
1781 }
1782 }
1783 card_res->count_crtcs = crtc_count;
1784
1785 /* Encoders */
1786 if (card_res->count_encoders >= encoder_count) {
1787 copied = 0;
1788 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001789 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001790 list_for_each_entry(encoder,
1791 &dev->mode_config.encoder_list,
1792 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001793 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001794 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001795 if (put_user(encoder->base.id, encoder_id +
1796 copied)) {
1797 ret = -EFAULT;
1798 goto out;
1799 }
1800 copied++;
1801 }
1802 } else {
1803 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1804 if (put_user(mode_group->id_list[i],
1805 encoder_id + copied)) {
1806 ret = -EFAULT;
1807 goto out;
1808 }
1809 copied++;
1810 }
1811
1812 }
1813 }
1814 card_res->count_encoders = encoder_count;
1815
1816 /* Connectors */
1817 if (card_res->count_connectors >= connector_count) {
1818 copied = 0;
1819 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001820 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001821 list_for_each_entry(connector,
1822 &dev->mode_config.connector_list,
1823 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001824 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1825 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001826 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001827 if (put_user(connector->base.id,
1828 connector_id + copied)) {
1829 ret = -EFAULT;
1830 goto out;
1831 }
1832 copied++;
1833 }
1834 } else {
1835 int start = mode_group->num_crtcs +
1836 mode_group->num_encoders;
1837 for (i = start; i < start + mode_group->num_connectors; i++) {
1838 if (put_user(mode_group->id_list[i],
1839 connector_id + copied)) {
1840 ret = -EFAULT;
1841 goto out;
1842 }
1843 copied++;
1844 }
1845 }
1846 }
1847 card_res->count_connectors = connector_count;
1848
Jerome Glisse94401062010-07-15 15:43:25 -04001849 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001850 card_res->count_connectors, card_res->count_encoders);
1851
1852out:
Daniel Vetter84849902012-12-02 00:28:11 +01001853 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001854 return ret;
1855}
1856
1857/**
1858 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001859 * @dev: drm device for the ioctl
1860 * @data: data pointer for the ioctl
1861 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001862 *
Dave Airlief453ba02008-11-07 14:05:41 -08001863 * Construct a CRTC configuration structure to return to the user.
1864 *
1865 * Called by the user via ioctl.
1866 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001867 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001868 * Zero on success, errno on failure.
1869 */
1870int drm_mode_getcrtc(struct drm_device *dev,
1871 void *data, struct drm_file *file_priv)
1872{
1873 struct drm_mode_crtc *crtc_resp = data;
1874 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001875 int ret = 0;
1876
Dave Airliefb3b06c2011-02-08 13:55:21 +10001877 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1878 return -EINVAL;
1879
Daniel Vetter84849902012-12-02 00:28:11 +01001880 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001881
Rob Clarka2b34e22013-10-05 16:36:52 -04001882 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1883 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001884 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001885 goto out;
1886 }
Dave Airlief453ba02008-11-07 14:05:41 -08001887
1888 crtc_resp->x = crtc->x;
1889 crtc_resp->y = crtc->y;
1890 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001891 if (crtc->primary->fb)
1892 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001893 else
1894 crtc_resp->fb_id = 0;
1895
1896 if (crtc->enabled) {
1897
1898 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1899 crtc_resp->mode_valid = 1;
1900
1901 } else {
1902 crtc_resp->mode_valid = 0;
1903 }
1904
1905out:
Daniel Vetter84849902012-12-02 00:28:11 +01001906 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001907 return ret;
1908}
1909
Damien Lespiau61d8e322013-09-25 16:45:22 +01001910static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1911 const struct drm_file *file_priv)
1912{
1913 /*
1914 * If user-space hasn't configured the driver to expose the stereo 3D
1915 * modes, don't expose them.
1916 */
1917 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1918 return false;
1919
1920 return true;
1921}
1922
Dave Airlief453ba02008-11-07 14:05:41 -08001923/**
1924 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001925 * @dev: drm device for the ioctl
1926 * @data: data pointer for the ioctl
1927 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001928 *
Dave Airlief453ba02008-11-07 14:05:41 -08001929 * Construct a connector configuration structure to return to the user.
1930 *
1931 * Called by the user via ioctl.
1932 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001933 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001934 * Zero on success, errno on failure.
1935 */
1936int drm_mode_getconnector(struct drm_device *dev, void *data,
1937 struct drm_file *file_priv)
1938{
1939 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001940 struct drm_connector *connector;
1941 struct drm_display_mode *mode;
1942 int mode_count = 0;
1943 int props_count = 0;
1944 int encoders_count = 0;
1945 int ret = 0;
1946 int copied = 0;
1947 int i;
1948 struct drm_mode_modeinfo u_mode;
1949 struct drm_mode_modeinfo __user *mode_ptr;
1950 uint32_t __user *prop_ptr;
1951 uint64_t __user *prop_values;
1952 uint32_t __user *encoder_ptr;
1953
Dave Airliefb3b06c2011-02-08 13:55:21 +10001954 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1955 return -EINVAL;
1956
Dave Airlief453ba02008-11-07 14:05:41 -08001957 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1958
Jerome Glisse94401062010-07-15 15:43:25 -04001959 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001960
Daniel Vetter7b240562012-12-12 00:35:33 +01001961 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001962
Rob Clarka2b34e22013-10-05 16:36:52 -04001963 connector = drm_connector_find(dev, out_resp->connector_id);
1964 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001965 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001966 goto out;
1967 }
Dave Airlief453ba02008-11-07 14:05:41 -08001968
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001969 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001970
1971 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1972 if (connector->encoder_ids[i] != 0) {
1973 encoders_count++;
1974 }
1975 }
1976
1977 if (out_resp->count_modes == 0) {
1978 connector->funcs->fill_modes(connector,
1979 dev->mode_config.max_width,
1980 dev->mode_config.max_height);
1981 }
1982
1983 /* delayed so we get modes regardless of pre-fill_modes state */
1984 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001985 if (drm_mode_expose_to_userspace(mode, file_priv))
1986 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001987
1988 out_resp->connector_id = connector->base.id;
1989 out_resp->connector_type = connector->connector_type;
1990 out_resp->connector_type_id = connector->connector_type_id;
1991 out_resp->mm_width = connector->display_info.width_mm;
1992 out_resp->mm_height = connector->display_info.height_mm;
1993 out_resp->subpixel = connector->display_info.subpixel_order;
1994 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02001995 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08001996 if (connector->encoder)
1997 out_resp->encoder_id = connector->encoder->base.id;
1998 else
1999 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02002000 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002001
2002 /*
2003 * This ioctl is called twice, once to determine how much space is
2004 * needed, and the 2nd time to fill it.
2005 */
2006 if ((out_resp->count_modes >= mode_count) && mode_count) {
2007 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002008 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002009 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002010 if (!drm_mode_expose_to_userspace(mode, file_priv))
2011 continue;
2012
Dave Airlief453ba02008-11-07 14:05:41 -08002013 drm_crtc_convert_to_umode(&u_mode, mode);
2014 if (copy_to_user(mode_ptr + copied,
2015 &u_mode, sizeof(u_mode))) {
2016 ret = -EFAULT;
2017 goto out;
2018 }
2019 copied++;
2020 }
2021 }
2022 out_resp->count_modes = mode_count;
2023
2024 if ((out_resp->count_props >= props_count) && props_count) {
2025 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002026 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2027 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002028 for (i = 0; i < connector->properties.count; i++) {
2029 if (put_user(connector->properties.ids[i],
2030 prop_ptr + copied)) {
2031 ret = -EFAULT;
2032 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002033 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002034
2035 if (put_user(connector->properties.values[i],
2036 prop_values + copied)) {
2037 ret = -EFAULT;
2038 goto out;
2039 }
2040 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002041 }
2042 }
2043 out_resp->count_props = props_count;
2044
2045 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2046 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002047 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002048 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2049 if (connector->encoder_ids[i] != 0) {
2050 if (put_user(connector->encoder_ids[i],
2051 encoder_ptr + copied)) {
2052 ret = -EFAULT;
2053 goto out;
2054 }
2055 copied++;
2056 }
2057 }
2058 }
2059 out_resp->count_encoders = encoders_count;
2060
2061out:
Daniel Vetter7b240562012-12-12 00:35:33 +01002062 mutex_unlock(&dev->mode_config.mutex);
2063
Dave Airlief453ba02008-11-07 14:05:41 -08002064 return ret;
2065}
2066
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002067/**
2068 * drm_mode_getencoder - get encoder configuration
2069 * @dev: drm device for the ioctl
2070 * @data: data pointer for the ioctl
2071 * @file_priv: drm file for the ioctl call
2072 *
2073 * Construct a encoder configuration structure to return to the user.
2074 *
2075 * Called by the user via ioctl.
2076 *
2077 * Returns:
2078 * Zero on success, errno on failure.
2079 */
Dave Airlief453ba02008-11-07 14:05:41 -08002080int drm_mode_getencoder(struct drm_device *dev, void *data,
2081 struct drm_file *file_priv)
2082{
2083 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002084 struct drm_encoder *encoder;
2085 int ret = 0;
2086
Dave Airliefb3b06c2011-02-08 13:55:21 +10002087 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2088 return -EINVAL;
2089
Daniel Vetter84849902012-12-02 00:28:11 +01002090 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002091 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2092 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002093 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002094 goto out;
2095 }
Dave Airlief453ba02008-11-07 14:05:41 -08002096
2097 if (encoder->crtc)
2098 enc_resp->crtc_id = encoder->crtc->base.id;
2099 else
2100 enc_resp->crtc_id = 0;
2101 enc_resp->encoder_type = encoder->encoder_type;
2102 enc_resp->encoder_id = encoder->base.id;
2103 enc_resp->possible_crtcs = encoder->possible_crtcs;
2104 enc_resp->possible_clones = encoder->possible_clones;
2105
2106out:
Daniel Vetter84849902012-12-02 00:28:11 +01002107 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002108 return ret;
2109}
2110
2111/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002112 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002113 * @dev: DRM device
2114 * @data: ioctl data
2115 * @file_priv: DRM file info
2116 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002117 * Construct a list of plane ids to return to the user.
2118 *
2119 * Called by the user via ioctl.
2120 *
2121 * Returns:
2122 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002123 */
2124int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002125 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002126{
2127 struct drm_mode_get_plane_res *plane_resp = data;
2128 struct drm_mode_config *config;
2129 struct drm_plane *plane;
2130 uint32_t __user *plane_ptr;
2131 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002132 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002133
2134 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2135 return -EINVAL;
2136
Daniel Vetter84849902012-12-02 00:28:11 +01002137 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002138 config = &dev->mode_config;
2139
Matt Roper681e7ec2014-04-01 15:22:42 -07002140 if (file_priv->universal_planes)
2141 num_planes = config->num_total_plane;
2142 else
2143 num_planes = config->num_overlay_plane;
2144
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002145 /*
2146 * This ioctl is called twice, once to determine how much space is
2147 * needed, and the 2nd time to fill it.
2148 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002149 if (num_planes &&
2150 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002151 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002152
2153 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002154 /*
2155 * Unless userspace set the 'universal planes'
2156 * capability bit, only advertise overlays.
2157 */
2158 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2159 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002160 continue;
2161
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002162 if (put_user(plane->base.id, plane_ptr + copied)) {
2163 ret = -EFAULT;
2164 goto out;
2165 }
2166 copied++;
2167 }
2168 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002169 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002170
2171out:
Daniel Vetter84849902012-12-02 00:28:11 +01002172 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002173 return ret;
2174}
2175
2176/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002177 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002178 * @dev: DRM device
2179 * @data: ioctl data
2180 * @file_priv: DRM file info
2181 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002182 * Construct a plane configuration structure to return to the user.
2183 *
2184 * Called by the user via ioctl.
2185 *
2186 * Returns:
2187 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002188 */
2189int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002190 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002191{
2192 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002193 struct drm_plane *plane;
2194 uint32_t __user *format_ptr;
2195 int ret = 0;
2196
2197 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2198 return -EINVAL;
2199
Daniel Vetter84849902012-12-02 00:28:11 +01002200 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002201 plane = drm_plane_find(dev, plane_resp->plane_id);
2202 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002203 ret = -ENOENT;
2204 goto out;
2205 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002206
2207 if (plane->crtc)
2208 plane_resp->crtc_id = plane->crtc->base.id;
2209 else
2210 plane_resp->crtc_id = 0;
2211
2212 if (plane->fb)
2213 plane_resp->fb_id = plane->fb->base.id;
2214 else
2215 plane_resp->fb_id = 0;
2216
2217 plane_resp->plane_id = plane->base.id;
2218 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002219 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002220
2221 /*
2222 * This ioctl is called twice, once to determine how much space is
2223 * needed, and the 2nd time to fill it.
2224 */
2225 if (plane->format_count &&
2226 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002227 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002228 if (copy_to_user(format_ptr,
2229 plane->format_types,
2230 sizeof(uint32_t) * plane->format_count)) {
2231 ret = -EFAULT;
2232 goto out;
2233 }
2234 }
2235 plane_resp->count_format_types = plane->format_count;
2236
2237out:
Daniel Vetter84849902012-12-02 00:28:11 +01002238 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002239 return ret;
2240}
2241
Matt Roperb36552b2014-06-10 08:28:09 -07002242/*
2243 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002244 *
Matt Roperb36552b2014-06-10 08:28:09 -07002245 * Note that we assume an extra reference has already been taken on fb. If the
2246 * update fails, this reference will be dropped before return; if it succeeds,
2247 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002248 *
Matt Roperb36552b2014-06-10 08:28:09 -07002249 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002250 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002251static int setplane_internal(struct drm_plane *plane,
2252 struct drm_crtc *crtc,
Matt Roperb36552b2014-06-10 08:28:09 -07002253 struct drm_framebuffer *fb,
2254 int32_t crtc_x, int32_t crtc_y,
2255 uint32_t crtc_w, uint32_t crtc_h,
2256 /* src_{x,y,w,h} values are 16.16 fixed point */
2257 uint32_t src_x, uint32_t src_y,
2258 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002259{
Chris Wilson17cfd912014-06-13 15:22:28 +01002260 struct drm_device *dev = plane->dev;
Matt Roperb36552b2014-06-10 08:28:09 -07002261 struct drm_framebuffer *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002262 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002263 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002264 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002265
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002266 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002267 if (!fb) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002268 drm_modeset_lock_all(dev);
2269 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002270 ret = plane->funcs->disable_plane(plane);
2271 if (!ret) {
2272 plane->crtc = NULL;
2273 plane->fb = NULL;
2274 } else {
2275 old_fb = NULL;
2276 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002277 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002278 goto out;
2279 }
2280
Matt Roper7f994f32014-05-29 08:06:51 -07002281 /* Check whether this plane is usable on this CRTC */
2282 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2283 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2284 ret = -EINVAL;
2285 goto out;
2286 }
2287
Ville Syrjälä62443be2011-12-20 00:06:47 +02002288 /* Check whether this plane supports the fb pixel format. */
2289 for (i = 0; i < plane->format_count; i++)
2290 if (fb->pixel_format == plane->format_types[i])
2291 break;
2292 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002293 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2294 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002295 ret = -EINVAL;
2296 goto out;
2297 }
2298
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002299 fb_width = fb->width << 16;
2300 fb_height = fb->height << 16;
2301
2302 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002303 if (src_w > fb_width ||
2304 src_x > fb_width - src_w ||
2305 src_h > fb_height ||
2306 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002307 DRM_DEBUG_KMS("Invalid source coordinates "
2308 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002309 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2310 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2311 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2312 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002313 ret = -ENOSPC;
2314 goto out;
2315 }
2316
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002317 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002318 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002319 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002320 crtc_x, crtc_y, crtc_w, crtc_h,
2321 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002322 if (!ret) {
2323 plane->crtc = crtc;
2324 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002325 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002326 } else {
2327 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002328 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002329 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002330
2331out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002332 if (fb)
2333 drm_framebuffer_unreference(fb);
2334 if (old_fb)
2335 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002336
2337 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002338
2339}
2340
2341/**
2342 * drm_mode_setplane - configure a plane's configuration
2343 * @dev: DRM device
2344 * @data: ioctl data*
2345 * @file_priv: DRM file info
2346 *
2347 * Set plane configuration, including placement, fb, scaling, and other factors.
2348 * Or pass a NULL fb to disable (planes may be disabled without providing a
2349 * valid crtc).
2350 *
2351 * Returns:
2352 * Zero on success, errno on failure.
2353 */
2354int drm_mode_setplane(struct drm_device *dev, void *data,
2355 struct drm_file *file_priv)
2356{
2357 struct drm_mode_set_plane *plane_req = data;
2358 struct drm_mode_object *obj;
2359 struct drm_plane *plane;
2360 struct drm_crtc *crtc = NULL;
2361 struct drm_framebuffer *fb = NULL;
2362
2363 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2364 return -EINVAL;
2365
2366 /* Give drivers some help against integer overflows */
2367 if (plane_req->crtc_w > INT_MAX ||
2368 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2369 plane_req->crtc_h > INT_MAX ||
2370 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2371 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2372 plane_req->crtc_w, plane_req->crtc_h,
2373 plane_req->crtc_x, plane_req->crtc_y);
2374 return -ERANGE;
2375 }
2376
2377 /*
2378 * First, find the plane, crtc, and fb objects. If not available,
2379 * we don't bother to call the driver.
2380 */
2381 obj = drm_mode_object_find(dev, plane_req->plane_id,
2382 DRM_MODE_OBJECT_PLANE);
2383 if (!obj) {
2384 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2385 plane_req->plane_id);
2386 return -ENOENT;
2387 }
2388 plane = obj_to_plane(obj);
2389
2390 if (plane_req->fb_id) {
2391 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2392 if (!fb) {
2393 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2394 plane_req->fb_id);
2395 return -ENOENT;
2396 }
2397
2398 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2399 DRM_MODE_OBJECT_CRTC);
2400 if (!obj) {
2401 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2402 plane_req->crtc_id);
2403 return -ENOENT;
2404 }
2405 crtc = obj_to_crtc(obj);
2406 }
2407
Matt Roper161d0dc2014-06-10 08:28:10 -07002408 /*
2409 * setplane_internal will take care of deref'ing either the old or new
2410 * framebuffer depending on success.
2411 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002412 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002413 plane_req->crtc_x, plane_req->crtc_y,
2414 plane_req->crtc_w, plane_req->crtc_h,
2415 plane_req->src_x, plane_req->src_y,
2416 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002417}
2418
2419/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002420 * drm_mode_set_config_internal - helper to call ->set_config
2421 * @set: modeset config to set
2422 *
2423 * This is a little helper to wrap internal calls to the ->set_config driver
2424 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002425 *
2426 * Returns:
2427 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002428 */
2429int drm_mode_set_config_internal(struct drm_mode_set *set)
2430{
2431 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002432 struct drm_framebuffer *fb;
2433 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002434 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002435
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002436 /*
2437 * NOTE: ->set_config can also disable other crtcs (if we steal all
2438 * connectors from it), hence we need to refcount the fbs across all
2439 * crtcs. Atomic modeset will have saner semantics ...
2440 */
2441 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002442 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002443
Daniel Vetterb0d12322012-12-11 01:07:12 +01002444 fb = set->fb;
2445
2446 ret = crtc->funcs->set_config(set);
2447 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002448 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002449 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002450 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002451
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002452 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002453 if (tmp->primary->fb)
2454 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002455 if (tmp->old_fb)
2456 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002457 }
2458
2459 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002460}
2461EXPORT_SYMBOL(drm_mode_set_config_internal);
2462
Matt Roperaf936292014-04-01 15:22:34 -07002463/**
2464 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2465 * CRTC viewport
2466 * @crtc: CRTC that framebuffer will be displayed on
2467 * @x: x panning
2468 * @y: y panning
2469 * @mode: mode that framebuffer will be displayed under
2470 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002471 */
Matt Roperaf936292014-04-01 15:22:34 -07002472int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2473 int x, int y,
2474 const struct drm_display_mode *mode,
2475 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002476
2477{
2478 int hdisplay, vdisplay;
2479
2480 hdisplay = mode->hdisplay;
2481 vdisplay = mode->vdisplay;
2482
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002483 if (drm_mode_is_stereo(mode)) {
2484 struct drm_display_mode adjusted = *mode;
2485
2486 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2487 hdisplay = adjusted.crtc_hdisplay;
2488 vdisplay = adjusted.crtc_vdisplay;
2489 }
2490
Damien Lespiauc11e9282013-09-25 16:45:30 +01002491 if (crtc->invert_dimensions)
2492 swap(hdisplay, vdisplay);
2493
2494 if (hdisplay > fb->width ||
2495 vdisplay > fb->height ||
2496 x > fb->width - hdisplay ||
2497 y > fb->height - vdisplay) {
2498 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2499 fb->width, fb->height, hdisplay, vdisplay, x, y,
2500 crtc->invert_dimensions ? " (inverted)" : "");
2501 return -ENOSPC;
2502 }
2503
2504 return 0;
2505}
Matt Roperaf936292014-04-01 15:22:34 -07002506EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002507
Daniel Vetter2d13b672012-12-11 13:47:23 +01002508/**
Dave Airlief453ba02008-11-07 14:05:41 -08002509 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002510 * @dev: drm device for the ioctl
2511 * @data: data pointer for the ioctl
2512 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002513 *
Dave Airlief453ba02008-11-07 14:05:41 -08002514 * Build a new CRTC configuration based on user request.
2515 *
2516 * Called by the user via ioctl.
2517 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002518 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002519 * Zero on success, errno on failure.
2520 */
2521int drm_mode_setcrtc(struct drm_device *dev, void *data,
2522 struct drm_file *file_priv)
2523{
2524 struct drm_mode_config *config = &dev->mode_config;
2525 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002526 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002527 struct drm_connector **connector_set = NULL, *connector;
2528 struct drm_framebuffer *fb = NULL;
2529 struct drm_display_mode *mode = NULL;
2530 struct drm_mode_set set;
2531 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002532 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002533 int i;
2534
Dave Airliefb3b06c2011-02-08 13:55:21 +10002535 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2536 return -EINVAL;
2537
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002538 /* For some reason crtc x/y offsets are signed internally. */
2539 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2540 return -ERANGE;
2541
Daniel Vetter84849902012-12-02 00:28:11 +01002542 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002543 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2544 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002545 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002546 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002547 goto out;
2548 }
Jerome Glisse94401062010-07-15 15:43:25 -04002549 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002550
2551 if (crtc_req->mode_valid) {
2552 /* If we have a mode we need a framebuffer. */
2553 /* If we pass -1, set the mode with the currently bound fb */
2554 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002555 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002556 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2557 ret = -EINVAL;
2558 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002559 }
Matt Roperf4510a22014-04-01 15:22:40 -07002560 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002561 /* Make refcounting symmetric with the lookup path. */
2562 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002563 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002564 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2565 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002566 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2567 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002568 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002569 goto out;
2570 }
Dave Airlief453ba02008-11-07 14:05:41 -08002571 }
2572
2573 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002574 if (!mode) {
2575 ret = -ENOMEM;
2576 goto out;
2577 }
2578
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002579 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2580 if (ret) {
2581 DRM_DEBUG_KMS("Invalid mode\n");
2582 goto out;
2583 }
2584
Dave Airlief453ba02008-11-07 14:05:41 -08002585 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002586
Damien Lespiauc11e9282013-09-25 16:45:30 +01002587 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2588 mode, fb);
2589 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002590 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002591
Dave Airlief453ba02008-11-07 14:05:41 -08002592 }
2593
2594 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002595 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002596 ret = -EINVAL;
2597 goto out;
2598 }
2599
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002600 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002601 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002602 crtc_req->count_connectors);
2603 ret = -EINVAL;
2604 goto out;
2605 }
2606
2607 if (crtc_req->count_connectors > 0) {
2608 u32 out_id;
2609
2610 /* Avoid unbounded kernel memory allocation */
2611 if (crtc_req->count_connectors > config->num_connector) {
2612 ret = -EINVAL;
2613 goto out;
2614 }
2615
2616 connector_set = kmalloc(crtc_req->count_connectors *
2617 sizeof(struct drm_connector *),
2618 GFP_KERNEL);
2619 if (!connector_set) {
2620 ret = -ENOMEM;
2621 goto out;
2622 }
2623
2624 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002625 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002626 if (get_user(out_id, &set_connectors_ptr[i])) {
2627 ret = -EFAULT;
2628 goto out;
2629 }
2630
Rob Clarka2b34e22013-10-05 16:36:52 -04002631 connector = drm_connector_find(dev, out_id);
2632 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002633 DRM_DEBUG_KMS("Connector id %d unknown\n",
2634 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002635 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002636 goto out;
2637 }
Jerome Glisse94401062010-07-15 15:43:25 -04002638 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2639 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002640 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002641
2642 connector_set[i] = connector;
2643 }
2644 }
2645
2646 set.crtc = crtc;
2647 set.x = crtc_req->x;
2648 set.y = crtc_req->y;
2649 set.mode = mode;
2650 set.connectors = connector_set;
2651 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002652 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002653 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002654
2655out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002656 if (fb)
2657 drm_framebuffer_unreference(fb);
2658
Dave Airlief453ba02008-11-07 14:05:41 -08002659 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002660 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002661 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002662 return ret;
2663}
2664
Matt Roper161d0dc2014-06-10 08:28:10 -07002665/**
2666 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2667 * universal plane handler call
2668 * @crtc: crtc to update cursor for
2669 * @req: data pointer for the ioctl
2670 * @file_priv: drm file for the ioctl call
2671 *
2672 * Legacy cursor ioctl's work directly with driver buffer handles. To
2673 * translate legacy ioctl calls into universal plane handler calls, we need to
2674 * wrap the native buffer handle in a drm_framebuffer.
2675 *
2676 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2677 * buffer with a pitch of 4*width; the universal plane interface should be used
2678 * directly in cases where the hardware can support other buffer settings and
2679 * userspace wants to make use of these capabilities.
2680 *
2681 * Returns:
2682 * Zero on success, errno on failure.
2683 */
2684static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2685 struct drm_mode_cursor2 *req,
2686 struct drm_file *file_priv)
2687{
2688 struct drm_device *dev = crtc->dev;
2689 struct drm_framebuffer *fb = NULL;
2690 struct drm_mode_fb_cmd2 fbreq = {
2691 .width = req->width,
2692 .height = req->height,
2693 .pixel_format = DRM_FORMAT_ARGB8888,
2694 .pitches = { req->width * 4 },
2695 .handles = { req->handle },
2696 };
2697 int32_t crtc_x, crtc_y;
2698 uint32_t crtc_w = 0, crtc_h = 0;
2699 uint32_t src_w = 0, src_h = 0;
2700 int ret = 0;
2701
2702 BUG_ON(!crtc->cursor);
2703
2704 /*
2705 * Obtain fb we'll be using (either new or existing) and take an extra
2706 * reference to it if fb != null. setplane will take care of dropping
2707 * the reference if the plane update fails.
2708 */
2709 if (req->flags & DRM_MODE_CURSOR_BO) {
2710 if (req->handle) {
2711 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2712 if (IS_ERR(fb)) {
2713 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2714 return PTR_ERR(fb);
2715 }
2716
2717 drm_framebuffer_reference(fb);
2718 } else {
2719 fb = NULL;
2720 }
2721 } else {
2722 mutex_lock(&dev->mode_config.mutex);
2723 fb = crtc->cursor->fb;
2724 if (fb)
2725 drm_framebuffer_reference(fb);
2726 mutex_unlock(&dev->mode_config.mutex);
2727 }
2728
2729 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2730 crtc_x = req->x;
2731 crtc_y = req->y;
2732 } else {
2733 crtc_x = crtc->cursor_x;
2734 crtc_y = crtc->cursor_y;
2735 }
2736
2737 if (fb) {
2738 crtc_w = fb->width;
2739 crtc_h = fb->height;
2740 src_w = fb->width << 16;
2741 src_h = fb->height << 16;
2742 }
2743
2744 /*
2745 * setplane_internal will take care of deref'ing either the old or new
2746 * framebuffer depending on success.
2747 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002748 ret = setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002749 crtc_x, crtc_y, crtc_w, crtc_h,
2750 0, 0, src_w, src_h);
2751
2752 /* Update successful; save new cursor position, if necessary */
2753 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2754 crtc->cursor_x = req->x;
2755 crtc->cursor_y = req->y;
2756 }
2757
2758 return ret;
2759}
2760
Dave Airlie4c813d42013-06-20 11:48:52 +10002761static int drm_mode_cursor_common(struct drm_device *dev,
2762 struct drm_mode_cursor2 *req,
2763 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002764{
Dave Airlief453ba02008-11-07 14:05:41 -08002765 struct drm_crtc *crtc;
2766 int ret = 0;
2767
Dave Airliefb3b06c2011-02-08 13:55:21 +10002768 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2769 return -EINVAL;
2770
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002771 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002772 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002773
Rob Clarka2b34e22013-10-05 16:36:52 -04002774 crtc = drm_crtc_find(dev, req->crtc_id);
2775 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002776 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002777 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002778 }
Dave Airlief453ba02008-11-07 14:05:41 -08002779
Matt Roper161d0dc2014-06-10 08:28:10 -07002780 /*
2781 * If this crtc has a universal cursor plane, call that plane's update
2782 * handler rather than using legacy cursor handlers.
2783 */
2784 if (crtc->cursor)
2785 return drm_mode_cursor_universal(crtc, req, file_priv);
2786
Rob Clark51fd3712013-11-19 12:10:12 -05002787 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002788 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002789 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002790 ret = -ENXIO;
2791 goto out;
2792 }
2793 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002794 if (crtc->funcs->cursor_set2)
2795 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2796 req->width, req->height, req->hot_x, req->hot_y);
2797 else
2798 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2799 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002800 }
2801
2802 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2803 if (crtc->funcs->cursor_move) {
2804 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2805 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002806 ret = -EFAULT;
2807 goto out;
2808 }
2809 }
2810out:
Rob Clark51fd3712013-11-19 12:10:12 -05002811 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002812
Dave Airlief453ba02008-11-07 14:05:41 -08002813 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002814
2815}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002816
2817
2818/**
2819 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2820 * @dev: drm device for the ioctl
2821 * @data: data pointer for the ioctl
2822 * @file_priv: drm file for the ioctl call
2823 *
2824 * Set the cursor configuration based on user request.
2825 *
2826 * Called by the user via ioctl.
2827 *
2828 * Returns:
2829 * Zero on success, errno on failure.
2830 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002831int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002832 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002833{
2834 struct drm_mode_cursor *req = data;
2835 struct drm_mode_cursor2 new_req;
2836
2837 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2838 new_req.hot_x = new_req.hot_y = 0;
2839
2840 return drm_mode_cursor_common(dev, &new_req, file_priv);
2841}
2842
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002843/**
2844 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2845 * @dev: drm device for the ioctl
2846 * @data: data pointer for the ioctl
2847 * @file_priv: drm file for the ioctl call
2848 *
2849 * Set the cursor configuration based on user request. This implements the 2nd
2850 * version of the cursor ioctl, which allows userspace to additionally specify
2851 * the hotspot of the pointer.
2852 *
2853 * Called by the user via ioctl.
2854 *
2855 * Returns:
2856 * Zero on success, errno on failure.
2857 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002858int drm_mode_cursor2_ioctl(struct drm_device *dev,
2859 void *data, struct drm_file *file_priv)
2860{
2861 struct drm_mode_cursor2 *req = data;
2862 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002863}
2864
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002865/**
2866 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2867 * @bpp: bits per pixels
2868 * @depth: bit depth per pixel
2869 *
2870 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2871 * Useful in fbdev emulation code, since that deals in those values.
2872 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002873uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2874{
2875 uint32_t fmt;
2876
2877 switch (bpp) {
2878 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002879 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002880 break;
2881 case 16:
2882 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002883 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002884 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002885 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002886 break;
2887 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002888 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002889 break;
2890 case 32:
2891 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002892 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002893 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002894 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002895 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002896 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002897 break;
2898 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002899 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2900 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002901 break;
2902 }
2903
2904 return fmt;
2905}
2906EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2907
Dave Airlief453ba02008-11-07 14:05:41 -08002908/**
2909 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002910 * @dev: drm device for the ioctl
2911 * @data: data pointer for the ioctl
2912 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002913 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002914 * Add a new FB to the specified CRTC, given a user request. This is the
2915 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002916 *
2917 * Called by the user via ioctl.
2918 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002919 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002920 * Zero on success, errno on failure.
2921 */
2922int drm_mode_addfb(struct drm_device *dev,
2923 void *data, struct drm_file *file_priv)
2924{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002925 struct drm_mode_fb_cmd *or = data;
2926 struct drm_mode_fb_cmd2 r = {};
2927 struct drm_mode_config *config = &dev->mode_config;
2928 struct drm_framebuffer *fb;
2929 int ret = 0;
2930
2931 /* Use new struct with format internally */
2932 r.fb_id = or->fb_id;
2933 r.width = or->width;
2934 r.height = or->height;
2935 r.pitches[0] = or->pitch;
2936 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2937 r.handles[0] = or->handle;
2938
2939 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2940 return -EINVAL;
2941
Jesse Barnesacb4b992011-11-07 12:03:23 -08002942 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002943 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002944
2945 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002946 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002947
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002948 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2949 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002950 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002951 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002952 }
2953
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002954 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002955 or->fb_id = fb->base.id;
2956 list_add(&fb->filp_head, &file_priv->fbs);
2957 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002958 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002959
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002960 return ret;
2961}
2962
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002963static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002964{
2965 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2966
2967 switch (format) {
2968 case DRM_FORMAT_C8:
2969 case DRM_FORMAT_RGB332:
2970 case DRM_FORMAT_BGR233:
2971 case DRM_FORMAT_XRGB4444:
2972 case DRM_FORMAT_XBGR4444:
2973 case DRM_FORMAT_RGBX4444:
2974 case DRM_FORMAT_BGRX4444:
2975 case DRM_FORMAT_ARGB4444:
2976 case DRM_FORMAT_ABGR4444:
2977 case DRM_FORMAT_RGBA4444:
2978 case DRM_FORMAT_BGRA4444:
2979 case DRM_FORMAT_XRGB1555:
2980 case DRM_FORMAT_XBGR1555:
2981 case DRM_FORMAT_RGBX5551:
2982 case DRM_FORMAT_BGRX5551:
2983 case DRM_FORMAT_ARGB1555:
2984 case DRM_FORMAT_ABGR1555:
2985 case DRM_FORMAT_RGBA5551:
2986 case DRM_FORMAT_BGRA5551:
2987 case DRM_FORMAT_RGB565:
2988 case DRM_FORMAT_BGR565:
2989 case DRM_FORMAT_RGB888:
2990 case DRM_FORMAT_BGR888:
2991 case DRM_FORMAT_XRGB8888:
2992 case DRM_FORMAT_XBGR8888:
2993 case DRM_FORMAT_RGBX8888:
2994 case DRM_FORMAT_BGRX8888:
2995 case DRM_FORMAT_ARGB8888:
2996 case DRM_FORMAT_ABGR8888:
2997 case DRM_FORMAT_RGBA8888:
2998 case DRM_FORMAT_BGRA8888:
2999 case DRM_FORMAT_XRGB2101010:
3000 case DRM_FORMAT_XBGR2101010:
3001 case DRM_FORMAT_RGBX1010102:
3002 case DRM_FORMAT_BGRX1010102:
3003 case DRM_FORMAT_ARGB2101010:
3004 case DRM_FORMAT_ABGR2101010:
3005 case DRM_FORMAT_RGBA1010102:
3006 case DRM_FORMAT_BGRA1010102:
3007 case DRM_FORMAT_YUYV:
3008 case DRM_FORMAT_YVYU:
3009 case DRM_FORMAT_UYVY:
3010 case DRM_FORMAT_VYUY:
3011 case DRM_FORMAT_AYUV:
3012 case DRM_FORMAT_NV12:
3013 case DRM_FORMAT_NV21:
3014 case DRM_FORMAT_NV16:
3015 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003016 case DRM_FORMAT_NV24:
3017 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003018 case DRM_FORMAT_YUV410:
3019 case DRM_FORMAT_YVU410:
3020 case DRM_FORMAT_YUV411:
3021 case DRM_FORMAT_YVU411:
3022 case DRM_FORMAT_YUV420:
3023 case DRM_FORMAT_YVU420:
3024 case DRM_FORMAT_YUV422:
3025 case DRM_FORMAT_YVU422:
3026 case DRM_FORMAT_YUV444:
3027 case DRM_FORMAT_YVU444:
3028 return 0;
3029 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003030 DRM_DEBUG_KMS("invalid pixel format %s\n",
3031 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003032 return -EINVAL;
3033 }
3034}
3035
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003036static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003037{
3038 int ret, hsub, vsub, num_planes, i;
3039
3040 ret = format_check(r);
3041 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003042 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3043 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003044 return ret;
3045 }
3046
3047 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3048 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3049 num_planes = drm_format_num_planes(r->pixel_format);
3050
3051 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003052 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003053 return -EINVAL;
3054 }
3055
3056 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003057 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003058 return -EINVAL;
3059 }
3060
3061 for (i = 0; i < num_planes; i++) {
3062 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003063 unsigned int height = r->height / (i != 0 ? vsub : 1);
3064 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003065
3066 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003067 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003068 return -EINVAL;
3069 }
3070
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003071 if ((uint64_t) width * cpp > UINT_MAX)
3072 return -ERANGE;
3073
3074 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3075 return -ERANGE;
3076
3077 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003078 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003079 return -EINVAL;
3080 }
3081 }
3082
3083 return 0;
3084}
3085
Matt Roperc394c2b2014-06-10 08:28:08 -07003086static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3087 struct drm_mode_fb_cmd2 *r,
3088 struct drm_file *file_priv)
3089{
3090 struct drm_mode_config *config = &dev->mode_config;
3091 struct drm_framebuffer *fb;
3092 int ret;
3093
3094 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3095 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3096 return ERR_PTR(-EINVAL);
3097 }
3098
3099 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3100 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3101 r->width, config->min_width, config->max_width);
3102 return ERR_PTR(-EINVAL);
3103 }
3104 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3105 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3106 r->height, config->min_height, config->max_height);
3107 return ERR_PTR(-EINVAL);
3108 }
3109
3110 ret = framebuffer_check(r);
3111 if (ret)
3112 return ERR_PTR(ret);
3113
3114 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3115 if (IS_ERR(fb)) {
3116 DRM_DEBUG_KMS("could not create framebuffer\n");
3117 return fb;
3118 }
3119
3120 mutex_lock(&file_priv->fbs_lock);
3121 r->fb_id = fb->base.id;
3122 list_add(&fb->filp_head, &file_priv->fbs);
3123 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3124 mutex_unlock(&file_priv->fbs_lock);
3125
3126 return fb;
3127}
3128
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003129/**
3130 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003131 * @dev: drm device for the ioctl
3132 * @data: data pointer for the ioctl
3133 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003134 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003135 * Add a new FB to the specified CRTC, given a user request with format. This is
3136 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3137 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003138 *
3139 * Called by the user via ioctl.
3140 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003141 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003142 * Zero on success, errno on failure.
3143 */
3144int drm_mode_addfb2(struct drm_device *dev,
3145 void *data, struct drm_file *file_priv)
3146{
Dave Airlief453ba02008-11-07 14:05:41 -08003147 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003148
Dave Airliefb3b06c2011-02-08 13:55:21 +10003149 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3150 return -EINVAL;
3151
Matt Roperc394c2b2014-06-10 08:28:08 -07003152 fb = add_framebuffer_internal(dev, data, file_priv);
3153 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003154 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003155
Matt Roperc394c2b2014-06-10 08:28:08 -07003156 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003157}
3158
3159/**
3160 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003161 * @dev: drm device for the ioctl
3162 * @data: data pointer for the ioctl
3163 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003164 *
Dave Airlief453ba02008-11-07 14:05:41 -08003165 * Remove the FB specified by the user.
3166 *
3167 * Called by the user via ioctl.
3168 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003169 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003170 * Zero on success, errno on failure.
3171 */
3172int drm_mode_rmfb(struct drm_device *dev,
3173 void *data, struct drm_file *file_priv)
3174{
Dave Airlief453ba02008-11-07 14:05:41 -08003175 struct drm_framebuffer *fb = NULL;
3176 struct drm_framebuffer *fbl = NULL;
3177 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003178 int found = 0;
3179
Dave Airliefb3b06c2011-02-08 13:55:21 +10003180 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3181 return -EINVAL;
3182
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003183 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003184 mutex_lock(&dev->mode_config.fb_lock);
3185 fb = __drm_framebuffer_lookup(dev, *id);
3186 if (!fb)
3187 goto fail_lookup;
3188
Dave Airlief453ba02008-11-07 14:05:41 -08003189 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3190 if (fb == fbl)
3191 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003192 if (!found)
3193 goto fail_lookup;
3194
3195 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3196 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003197
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003198 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003199 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003200 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003201
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003202 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003203
Daniel Vetter2b677e82012-12-10 21:16:05 +01003204 return 0;
3205
3206fail_lookup:
3207 mutex_unlock(&dev->mode_config.fb_lock);
3208 mutex_unlock(&file_priv->fbs_lock);
3209
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003210 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003211}
3212
3213/**
3214 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003215 * @dev: drm device for the ioctl
3216 * @data: data pointer for the ioctl
3217 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003218 *
Dave Airlief453ba02008-11-07 14:05:41 -08003219 * Lookup the FB given its ID and return info about it.
3220 *
3221 * Called by the user via ioctl.
3222 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003223 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003224 * Zero on success, errno on failure.
3225 */
3226int drm_mode_getfb(struct drm_device *dev,
3227 void *data, struct drm_file *file_priv)
3228{
3229 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003230 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003231 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003232
Dave Airliefb3b06c2011-02-08 13:55:21 +10003233 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3234 return -EINVAL;
3235
Daniel Vetter786b99e2012-12-02 21:53:40 +01003236 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003237 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003238 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003239
3240 r->height = fb->height;
3241 r->width = fb->width;
3242 r->depth = fb->depth;
3243 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003244 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003245 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003246 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003247 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003248 ret = fb->funcs->create_handle(fb, file_priv,
3249 &r->handle);
3250 } else {
3251 /* GET_FB() is an unprivileged ioctl so we must not
3252 * return a buffer-handle to non-master processes! For
3253 * backwards-compatibility reasons, we cannot make
3254 * GET_FB() privileged, so just return an invalid handle
3255 * for non-masters. */
3256 r->handle = 0;
3257 ret = 0;
3258 }
3259 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003260 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003261 }
Dave Airlief453ba02008-11-07 14:05:41 -08003262
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003263 drm_framebuffer_unreference(fb);
3264
Dave Airlief453ba02008-11-07 14:05:41 -08003265 return ret;
3266}
3267
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003268/**
3269 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3270 * @dev: drm device for the ioctl
3271 * @data: data pointer for the ioctl
3272 * @file_priv: drm file for the ioctl call
3273 *
3274 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3275 * rectangle list. Generic userspace which does frontbuffer rendering must call
3276 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3277 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3278 *
3279 * Modesetting drivers which always update the frontbuffer do not need to
3280 * implement the corresponding ->dirty framebuffer callback.
3281 *
3282 * Called by the user via ioctl.
3283 *
3284 * Returns:
3285 * Zero on success, errno on failure.
3286 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003287int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3288 void *data, struct drm_file *file_priv)
3289{
3290 struct drm_clip_rect __user *clips_ptr;
3291 struct drm_clip_rect *clips = NULL;
3292 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003293 struct drm_framebuffer *fb;
3294 unsigned flags;
3295 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003296 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003297
Dave Airliefb3b06c2011-02-08 13:55:21 +10003298 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3299 return -EINVAL;
3300
Daniel Vetter786b99e2012-12-02 21:53:40 +01003301 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003302 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003303 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003304
3305 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003306 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003307
3308 if (!num_clips != !clips_ptr) {
3309 ret = -EINVAL;
3310 goto out_err1;
3311 }
3312
3313 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3314
3315 /* If userspace annotates copy, clips must come in pairs */
3316 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3317 ret = -EINVAL;
3318 goto out_err1;
3319 }
3320
3321 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003322 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3323 ret = -EINVAL;
3324 goto out_err1;
3325 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003326 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3327 if (!clips) {
3328 ret = -ENOMEM;
3329 goto out_err1;
3330 }
3331
3332 ret = copy_from_user(clips, clips_ptr,
3333 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003334 if (ret) {
3335 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003336 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003337 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003338 }
3339
3340 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003341 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3342 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003343 } else {
3344 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003345 }
3346
3347out_err2:
3348 kfree(clips);
3349out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003350 drm_framebuffer_unreference(fb);
3351
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003352 return ret;
3353}
3354
3355
Dave Airlief453ba02008-11-07 14:05:41 -08003356/**
3357 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003358 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003359 *
Dave Airlief453ba02008-11-07 14:05:41 -08003360 * Destroy all the FBs associated with @filp.
3361 *
3362 * Called by the user via ioctl.
3363 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003364 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003365 * Zero on success, errno on failure.
3366 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003367void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003368{
Dave Airlief453ba02008-11-07 14:05:41 -08003369 struct drm_device *dev = priv->minor->dev;
3370 struct drm_framebuffer *fb, *tfb;
3371
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003372 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003373 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003374
3375 mutex_lock(&dev->mode_config.fb_lock);
3376 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3377 __drm_framebuffer_unregister(dev, fb);
3378 mutex_unlock(&dev->mode_config.fb_lock);
3379
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003380 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003381
3382 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003383 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003384 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003385 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003386}
3387
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003388/**
3389 * drm_property_create - create a new property type
3390 * @dev: drm device
3391 * @flags: flags specifying the property type
3392 * @name: name of the property
3393 * @num_values: number of pre-defined values
3394 *
3395 * This creates a new generic drm property which can then be attached to a drm
3396 * object with drm_object_attach_property. The returned property object must be
3397 * freed with drm_property_destroy.
3398 *
3399 * Returns:
3400 * A pointer to the newly created property on success, NULL on failure.
3401 */
Dave Airlief453ba02008-11-07 14:05:41 -08003402struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3403 const char *name, int num_values)
3404{
3405 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003406 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003407
3408 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3409 if (!property)
3410 return NULL;
3411
Rob Clark98f75de2014-05-30 11:37:03 -04003412 property->dev = dev;
3413
Dave Airlief453ba02008-11-07 14:05:41 -08003414 if (num_values) {
3415 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3416 if (!property->values)
3417 goto fail;
3418 }
3419
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003420 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3421 if (ret)
3422 goto fail;
3423
Dave Airlief453ba02008-11-07 14:05:41 -08003424 property->flags = flags;
3425 property->num_values = num_values;
3426 INIT_LIST_HEAD(&property->enum_blob_list);
3427
Vinson Lee471dd2e2011-11-10 11:55:40 -08003428 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003429 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003430 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3431 }
Dave Airlief453ba02008-11-07 14:05:41 -08003432
3433 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003434
3435 WARN_ON(!drm_property_type_valid(property));
3436
Dave Airlief453ba02008-11-07 14:05:41 -08003437 return property;
3438fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003439 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003440 kfree(property);
3441 return NULL;
3442}
3443EXPORT_SYMBOL(drm_property_create);
3444
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003445/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003446 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003447 * @dev: drm device
3448 * @flags: flags specifying the property type
3449 * @name: name of the property
3450 * @props: enumeration lists with property values
3451 * @num_values: number of pre-defined values
3452 *
3453 * This creates a new generic drm property which can then be attached to a drm
3454 * object with drm_object_attach_property. The returned property object must be
3455 * freed with drm_property_destroy.
3456 *
3457 * Userspace is only allowed to set one of the predefined values for enumeration
3458 * properties.
3459 *
3460 * Returns:
3461 * A pointer to the newly created property on success, NULL on failure.
3462 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003463struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3464 const char *name,
3465 const struct drm_prop_enum_list *props,
3466 int num_values)
3467{
3468 struct drm_property *property;
3469 int i, ret;
3470
3471 flags |= DRM_MODE_PROP_ENUM;
3472
3473 property = drm_property_create(dev, flags, name, num_values);
3474 if (!property)
3475 return NULL;
3476
3477 for (i = 0; i < num_values; i++) {
3478 ret = drm_property_add_enum(property, i,
3479 props[i].type,
3480 props[i].name);
3481 if (ret) {
3482 drm_property_destroy(dev, property);
3483 return NULL;
3484 }
3485 }
3486
3487 return property;
3488}
3489EXPORT_SYMBOL(drm_property_create_enum);
3490
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003491/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003492 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003493 * @dev: drm device
3494 * @flags: flags specifying the property type
3495 * @name: name of the property
3496 * @props: enumeration lists with property bitflags
3497 * @num_values: number of pre-defined values
3498 *
3499 * This creates a new generic drm property which can then be attached to a drm
3500 * object with drm_object_attach_property. The returned property object must be
3501 * freed with drm_property_destroy.
3502 *
3503 * Compared to plain enumeration properties userspace is allowed to set any
3504 * or'ed together combination of the predefined property bitflag values
3505 *
3506 * Returns:
3507 * A pointer to the newly created property on success, NULL on failure.
3508 */
Rob Clark49e27542012-05-17 02:23:26 -06003509struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3510 int flags, const char *name,
3511 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303512 int num_props,
3513 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003514{
3515 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303516 int i, ret, index = 0;
3517 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003518
3519 flags |= DRM_MODE_PROP_BITMASK;
3520
3521 property = drm_property_create(dev, flags, name, num_values);
3522 if (!property)
3523 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303524 for (i = 0; i < num_props; i++) {
3525 if (!(supported_bits & (1ULL << props[i].type)))
3526 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003527
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303528 if (WARN_ON(index >= num_values)) {
3529 drm_property_destroy(dev, property);
3530 return NULL;
3531 }
3532
3533 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003534 props[i].type,
3535 props[i].name);
3536 if (ret) {
3537 drm_property_destroy(dev, property);
3538 return NULL;
3539 }
3540 }
3541
3542 return property;
3543}
3544EXPORT_SYMBOL(drm_property_create_bitmask);
3545
Rob Clarkebc44cf2012-09-12 22:22:31 -05003546static struct drm_property *property_create_range(struct drm_device *dev,
3547 int flags, const char *name,
3548 uint64_t min, uint64_t max)
3549{
3550 struct drm_property *property;
3551
3552 property = drm_property_create(dev, flags, name, 2);
3553 if (!property)
3554 return NULL;
3555
3556 property->values[0] = min;
3557 property->values[1] = max;
3558
3559 return property;
3560}
3561
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003562/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003563 * drm_property_create_range - create a new ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003564 * @dev: drm device
3565 * @flags: flags specifying the property type
3566 * @name: name of the property
3567 * @min: minimum value of the property
3568 * @max: maximum value of the property
3569 *
3570 * This creates a new generic drm property which can then be attached to a drm
3571 * object with drm_object_attach_property. The returned property object must be
3572 * freed with drm_property_destroy.
3573 *
3574 * Userspace is allowed to set any interger value in the (min, max) range
3575 * inclusive.
3576 *
3577 * Returns:
3578 * A pointer to the newly created property on success, NULL on failure.
3579 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003580struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3581 const char *name,
3582 uint64_t min, uint64_t max)
3583{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003584 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3585 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003586}
3587EXPORT_SYMBOL(drm_property_create_range);
3588
Rob Clarkebc44cf2012-09-12 22:22:31 -05003589struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3590 int flags, const char *name,
3591 int64_t min, int64_t max)
3592{
3593 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3594 name, I642U64(min), I642U64(max));
3595}
3596EXPORT_SYMBOL(drm_property_create_signed_range);
3597
Rob Clark98f75de2014-05-30 11:37:03 -04003598struct drm_property *drm_property_create_object(struct drm_device *dev,
3599 int flags, const char *name, uint32_t type)
3600{
3601 struct drm_property *property;
3602
3603 flags |= DRM_MODE_PROP_OBJECT;
3604
3605 property = drm_property_create(dev, flags, name, 1);
3606 if (!property)
3607 return NULL;
3608
3609 property->values[0] = type;
3610
3611 return property;
3612}
3613EXPORT_SYMBOL(drm_property_create_object);
3614
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003615/**
3616 * drm_property_add_enum - add a possible value to an enumeration property
3617 * @property: enumeration property to change
3618 * @index: index of the new enumeration
3619 * @value: value of the new enumeration
3620 * @name: symbolic name of the new enumeration
3621 *
3622 * This functions adds enumerations to a property.
3623 *
3624 * It's use is deprecated, drivers should use one of the more specific helpers
3625 * to directly create the property with all enumerations already attached.
3626 *
3627 * Returns:
3628 * Zero on success, error code on failure.
3629 */
Dave Airlief453ba02008-11-07 14:05:41 -08003630int drm_property_add_enum(struct drm_property *property, int index,
3631 uint64_t value, const char *name)
3632{
3633 struct drm_property_enum *prop_enum;
3634
Rob Clark5ea22f22014-05-30 11:34:01 -04003635 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3636 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003637 return -EINVAL;
3638
3639 /*
3640 * Bitmask enum properties have the additional constraint of values
3641 * from 0 to 63
3642 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003643 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3644 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003645 return -EINVAL;
3646
3647 if (!list_empty(&property->enum_blob_list)) {
3648 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3649 if (prop_enum->value == value) {
3650 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3651 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3652 return 0;
3653 }
3654 }
3655 }
3656
3657 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3658 if (!prop_enum)
3659 return -ENOMEM;
3660
3661 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3662 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3663 prop_enum->value = value;
3664
3665 property->values[index] = value;
3666 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3667 return 0;
3668}
3669EXPORT_SYMBOL(drm_property_add_enum);
3670
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003671/**
3672 * drm_property_destroy - destroy a drm property
3673 * @dev: drm device
3674 * @property: property to destry
3675 *
3676 * This function frees a property including any attached resources like
3677 * enumeration values.
3678 */
Dave Airlief453ba02008-11-07 14:05:41 -08003679void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3680{
3681 struct drm_property_enum *prop_enum, *pt;
3682
3683 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3684 list_del(&prop_enum->head);
3685 kfree(prop_enum);
3686 }
3687
3688 if (property->num_values)
3689 kfree(property->values);
3690 drm_mode_object_put(dev, &property->base);
3691 list_del(&property->head);
3692 kfree(property);
3693}
3694EXPORT_SYMBOL(drm_property_destroy);
3695
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003696/**
3697 * drm_object_attach_property - attach a property to a modeset object
3698 * @obj: drm modeset object
3699 * @property: property to attach
3700 * @init_val: initial value of the property
3701 *
3702 * This attaches the given property to the modeset object with the given initial
3703 * value. Currently this function cannot fail since the properties are stored in
3704 * a statically sized array.
3705 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003706void drm_object_attach_property(struct drm_mode_object *obj,
3707 struct drm_property *property,
3708 uint64_t init_val)
3709{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003710 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003711
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003712 if (count == DRM_OBJECT_MAX_PROPERTY) {
3713 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3714 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3715 "you see this message on the same object type.\n",
3716 obj->type);
3717 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003718 }
3719
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003720 obj->properties->ids[count] = property->base.id;
3721 obj->properties->values[count] = init_val;
3722 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003723}
3724EXPORT_SYMBOL(drm_object_attach_property);
3725
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003726/**
3727 * drm_object_property_set_value - set the value of a property
3728 * @obj: drm mode object to set property value for
3729 * @property: property to set
3730 * @val: value the property should be set to
3731 *
3732 * This functions sets a given property on a given object. This function only
3733 * changes the software state of the property, it does not call into the
3734 * driver's ->set_property callback.
3735 *
3736 * Returns:
3737 * Zero on success, error code on failure.
3738 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003739int drm_object_property_set_value(struct drm_mode_object *obj,
3740 struct drm_property *property, uint64_t val)
3741{
3742 int i;
3743
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003744 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003745 if (obj->properties->ids[i] == property->base.id) {
3746 obj->properties->values[i] = val;
3747 return 0;
3748 }
3749 }
3750
3751 return -EINVAL;
3752}
3753EXPORT_SYMBOL(drm_object_property_set_value);
3754
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003755/**
3756 * drm_object_property_get_value - retrieve the value of a property
3757 * @obj: drm mode object to get property value from
3758 * @property: property to retrieve
3759 * @val: storage for the property value
3760 *
3761 * This function retrieves the softare state of the given property for the given
3762 * property. Since there is no driver callback to retrieve the current property
3763 * value this might be out of sync with the hardware, depending upon the driver
3764 * and property.
3765 *
3766 * Returns:
3767 * Zero on success, error code on failure.
3768 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003769int drm_object_property_get_value(struct drm_mode_object *obj,
3770 struct drm_property *property, uint64_t *val)
3771{
3772 int i;
3773
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003774 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003775 if (obj->properties->ids[i] == property->base.id) {
3776 *val = obj->properties->values[i];
3777 return 0;
3778 }
3779 }
3780
3781 return -EINVAL;
3782}
3783EXPORT_SYMBOL(drm_object_property_get_value);
3784
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003785/**
3786 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3787 * @dev: DRM device
3788 * @data: ioctl data
3789 * @file_priv: DRM file info
3790 *
3791 * This function retrieves the current value for an connectors's property.
3792 *
3793 * Called by the user via ioctl.
3794 *
3795 * Returns:
3796 * Zero on success, errno on failure.
3797 */
Dave Airlief453ba02008-11-07 14:05:41 -08003798int drm_mode_getproperty_ioctl(struct drm_device *dev,
3799 void *data, struct drm_file *file_priv)
3800{
Dave Airlief453ba02008-11-07 14:05:41 -08003801 struct drm_mode_get_property *out_resp = data;
3802 struct drm_property *property;
3803 int enum_count = 0;
3804 int blob_count = 0;
3805 int value_count = 0;
3806 int ret = 0, i;
3807 int copied;
3808 struct drm_property_enum *prop_enum;
3809 struct drm_mode_property_enum __user *enum_ptr;
3810 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003811 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003812 uint64_t __user *values_ptr;
3813 uint32_t __user *blob_length_ptr;
3814
Dave Airliefb3b06c2011-02-08 13:55:21 +10003815 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3816 return -EINVAL;
3817
Daniel Vetter84849902012-12-02 00:28:11 +01003818 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003819 property = drm_property_find(dev, out_resp->prop_id);
3820 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003821 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003822 goto done;
3823 }
Dave Airlief453ba02008-11-07 14:05:41 -08003824
Rob Clark5ea22f22014-05-30 11:34:01 -04003825 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3826 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003827 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3828 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003829 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003830 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3831 blob_count++;
3832 }
3833
3834 value_count = property->num_values;
3835
3836 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3837 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3838 out_resp->flags = property->flags;
3839
3840 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003841 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003842 for (i = 0; i < value_count; i++) {
3843 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3844 ret = -EFAULT;
3845 goto done;
3846 }
3847 }
3848 }
3849 out_resp->count_values = value_count;
3850
Rob Clark5ea22f22014-05-30 11:34:01 -04003851 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3852 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003853 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3854 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003855 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003856 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3857
3858 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3859 ret = -EFAULT;
3860 goto done;
3861 }
3862
3863 if (copy_to_user(&enum_ptr[copied].name,
3864 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3865 ret = -EFAULT;
3866 goto done;
3867 }
3868 copied++;
3869 }
3870 }
3871 out_resp->count_enum_blobs = enum_count;
3872 }
3873
Rob Clark5ea22f22014-05-30 11:34:01 -04003874 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003875 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3876 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003877 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3878 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003879
3880 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3881 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3882 ret = -EFAULT;
3883 goto done;
3884 }
3885
3886 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3887 ret = -EFAULT;
3888 goto done;
3889 }
3890
3891 copied++;
3892 }
3893 }
3894 out_resp->count_enum_blobs = blob_count;
3895 }
3896done:
Daniel Vetter84849902012-12-02 00:28:11 +01003897 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003898 return ret;
3899}
3900
3901static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3902 void *data)
3903{
3904 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003905 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003906
3907 if (!length || !data)
3908 return NULL;
3909
3910 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3911 if (!blob)
3912 return NULL;
3913
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003914 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3915 if (ret) {
3916 kfree(blob);
3917 return NULL;
3918 }
3919
Dave Airlief453ba02008-11-07 14:05:41 -08003920 blob->length = length;
3921
3922 memcpy(blob->data, data, length);
3923
Dave Airlief453ba02008-11-07 14:05:41 -08003924 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3925 return blob;
3926}
3927
3928static void drm_property_destroy_blob(struct drm_device *dev,
3929 struct drm_property_blob *blob)
3930{
3931 drm_mode_object_put(dev, &blob->base);
3932 list_del(&blob->head);
3933 kfree(blob);
3934}
3935
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003936/**
3937 * drm_mode_getblob_ioctl - get the contents of a blob property value
3938 * @dev: DRM device
3939 * @data: ioctl data
3940 * @file_priv: DRM file info
3941 *
3942 * This function retrieves the contents of a blob property. The value stored in
3943 * an object's blob property is just a normal modeset object id.
3944 *
3945 * Called by the user via ioctl.
3946 *
3947 * Returns:
3948 * Zero on success, errno on failure.
3949 */
Dave Airlief453ba02008-11-07 14:05:41 -08003950int drm_mode_getblob_ioctl(struct drm_device *dev,
3951 void *data, struct drm_file *file_priv)
3952{
Dave Airlief453ba02008-11-07 14:05:41 -08003953 struct drm_mode_get_blob *out_resp = data;
3954 struct drm_property_blob *blob;
3955 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003956 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003957
Dave Airliefb3b06c2011-02-08 13:55:21 +10003958 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3959 return -EINVAL;
3960
Daniel Vetter84849902012-12-02 00:28:11 +01003961 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003962 blob = drm_property_blob_find(dev, out_resp->blob_id);
3963 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003964 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003965 goto done;
3966 }
Dave Airlief453ba02008-11-07 14:05:41 -08003967
3968 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003969 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003970 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3971 ret = -EFAULT;
3972 goto done;
3973 }
3974 }
3975 out_resp->length = blob->length;
3976
3977done:
Daniel Vetter84849902012-12-02 00:28:11 +01003978 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003979 return ret;
3980}
3981
Dave Airlie43aba7e2014-06-05 14:01:31 +10003982int drm_mode_connector_set_path_property(struct drm_connector *connector,
3983 char *path)
3984{
3985 struct drm_device *dev = connector->dev;
3986 int ret, size;
3987 size = strlen(path) + 1;
3988
3989 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
3990 size, path);
3991 if (!connector->path_blob_ptr)
3992 return -EINVAL;
3993
3994 ret = drm_object_property_set_value(&connector->base,
3995 dev->mode_config.path_property,
3996 connector->path_blob_ptr->base.id);
3997 return ret;
3998}
3999EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4000
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004001/**
4002 * drm_mode_connector_update_edid_property - update the edid property of a connector
4003 * @connector: drm connector
4004 * @edid: new value of the edid property
4005 *
4006 * This function creates a new blob modeset object and assigns its id to the
4007 * connector's edid property.
4008 *
4009 * Returns:
4010 * Zero on success, errno on failure.
4011 */
Dave Airlief453ba02008-11-07 14:05:41 -08004012int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4013 struct edid *edid)
4014{
4015 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02004016 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08004017
Thomas Wood4cf2b282014-06-18 17:52:33 +01004018 /* ignore requests to set edid when overridden */
4019 if (connector->override_edid)
4020 return 0;
4021
Dave Airlief453ba02008-11-07 14:05:41 -08004022 if (connector->edid_blob_ptr)
4023 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4024
4025 /* Delete edid, when there is none. */
4026 if (!edid) {
4027 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05004028 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08004029 return ret;
4030 }
4031
Adam Jackson7466f4c2010-03-29 21:43:23 +00004032 size = EDID_LENGTH * (1 + edid->extensions);
4033 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4034 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00004035 if (!connector->edid_blob_ptr)
4036 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08004037
Rob Clark58495562012-10-11 20:50:56 -05004038 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08004039 dev->mode_config.edid_property,
4040 connector->edid_blob_ptr->base.id);
4041
4042 return ret;
4043}
4044EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4045
Paulo Zanoni26a34812012-05-15 18:08:59 -03004046static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004047 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004048{
4049 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4050 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004051
4052 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004053 if (value < property->values[0] || value > property->values[1])
4054 return false;
4055 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004056 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4057 int64_t svalue = U642I64(value);
4058 if (svalue < U642I64(property->values[0]) ||
4059 svalue > U642I64(property->values[1]))
4060 return false;
4061 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004062 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06004063 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004064 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06004065 for (i = 0; i < property->num_values; i++)
4066 valid_mask |= (1ULL << property->values[i]);
4067 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004068 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004069 /* Only the driver knows */
4070 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004071 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4072 struct drm_mode_object *obj;
4073 /* a zero value for an object property translates to null: */
4074 if (value == 0)
4075 return true;
4076 /*
4077 * NOTE: use _object_find() directly to bypass restriction on
4078 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4079 * object this could race against object finalization, so it
4080 * simply tells us that the object *was* valid. Which is good
4081 * enough.
4082 */
4083 obj = _object_find(property->dev, value, property->values[0]);
4084 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004085 } else {
4086 int i;
4087 for (i = 0; i < property->num_values; i++)
4088 if (property->values[i] == value)
4089 return true;
4090 return false;
4091 }
4092}
4093
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004094/**
4095 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4096 * @dev: DRM device
4097 * @data: ioctl data
4098 * @file_priv: DRM file info
4099 *
4100 * This function sets the current value for a connectors's property. It also
4101 * calls into a driver's ->set_property callback to update the hardware state
4102 *
4103 * Called by the user via ioctl.
4104 *
4105 * Returns:
4106 * Zero on success, errno on failure.
4107 */
Dave Airlief453ba02008-11-07 14:05:41 -08004108int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4109 void *data, struct drm_file *file_priv)
4110{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004111 struct drm_mode_connector_set_property *conn_set_prop = data;
4112 struct drm_mode_obj_set_property obj_set_prop = {
4113 .value = conn_set_prop->value,
4114 .prop_id = conn_set_prop->prop_id,
4115 .obj_id = conn_set_prop->connector_id,
4116 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4117 };
Dave Airlief453ba02008-11-07 14:05:41 -08004118
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004119 /* It does all the locking and checking we need */
4120 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004121}
4122
Paulo Zanonic5431882012-05-15 18:09:02 -03004123static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4124 struct drm_property *property,
4125 uint64_t value)
4126{
4127 int ret = -EINVAL;
4128 struct drm_connector *connector = obj_to_connector(obj);
4129
4130 /* Do DPMS ourselves */
4131 if (property == connector->dev->mode_config.dpms_property) {
4132 if (connector->funcs->dpms)
4133 (*connector->funcs->dpms)(connector, (int)value);
4134 ret = 0;
4135 } else if (connector->funcs->set_property)
4136 ret = connector->funcs->set_property(connector, property, value);
4137
4138 /* store the property value if successful */
4139 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004140 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004141 return ret;
4142}
4143
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004144static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4145 struct drm_property *property,
4146 uint64_t value)
4147{
4148 int ret = -EINVAL;
4149 struct drm_crtc *crtc = obj_to_crtc(obj);
4150
4151 if (crtc->funcs->set_property)
4152 ret = crtc->funcs->set_property(crtc, property, value);
4153 if (!ret)
4154 drm_object_property_set_value(obj, property, value);
4155
4156 return ret;
4157}
4158
Rob Clark4d939142012-05-17 02:23:27 -06004159static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4160 struct drm_property *property,
4161 uint64_t value)
4162{
4163 int ret = -EINVAL;
4164 struct drm_plane *plane = obj_to_plane(obj);
4165
4166 if (plane->funcs->set_property)
4167 ret = plane->funcs->set_property(plane, property, value);
4168 if (!ret)
4169 drm_object_property_set_value(obj, property, value);
4170
4171 return ret;
4172}
4173
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004174/**
4175 * drm_mode_getproperty_ioctl - get the current value of a object's property
4176 * @dev: DRM device
4177 * @data: ioctl data
4178 * @file_priv: DRM file info
4179 *
4180 * This function retrieves the current value for an object's property. Compared
4181 * to the connector specific ioctl this one is extended to also work on crtc and
4182 * plane objects.
4183 *
4184 * Called by the user via ioctl.
4185 *
4186 * Returns:
4187 * Zero on success, errno on failure.
4188 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004189int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4190 struct drm_file *file_priv)
4191{
4192 struct drm_mode_obj_get_properties *arg = data;
4193 struct drm_mode_object *obj;
4194 int ret = 0;
4195 int i;
4196 int copied = 0;
4197 int props_count = 0;
4198 uint32_t __user *props_ptr;
4199 uint64_t __user *prop_values_ptr;
4200
4201 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4202 return -EINVAL;
4203
Daniel Vetter84849902012-12-02 00:28:11 +01004204 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004205
4206 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4207 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004208 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004209 goto out;
4210 }
4211 if (!obj->properties) {
4212 ret = -EINVAL;
4213 goto out;
4214 }
4215
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004216 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004217
4218 /* This ioctl is called twice, once to determine how much space is
4219 * needed, and the 2nd time to fill it. */
4220 if ((arg->count_props >= props_count) && props_count) {
4221 copied = 0;
4222 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4223 prop_values_ptr = (uint64_t __user *)(unsigned long)
4224 (arg->prop_values_ptr);
4225 for (i = 0; i < props_count; i++) {
4226 if (put_user(obj->properties->ids[i],
4227 props_ptr + copied)) {
4228 ret = -EFAULT;
4229 goto out;
4230 }
4231 if (put_user(obj->properties->values[i],
4232 prop_values_ptr + copied)) {
4233 ret = -EFAULT;
4234 goto out;
4235 }
4236 copied++;
4237 }
4238 }
4239 arg->count_props = props_count;
4240out:
Daniel Vetter84849902012-12-02 00:28:11 +01004241 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004242 return ret;
4243}
4244
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004245/**
4246 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4247 * @dev: DRM device
4248 * @data: ioctl data
4249 * @file_priv: DRM file info
4250 *
4251 * This function sets the current value for an object's property. It also calls
4252 * into a driver's ->set_property callback to update the hardware state.
4253 * Compared to the connector specific ioctl this one is extended to also work on
4254 * crtc and plane objects.
4255 *
4256 * Called by the user via ioctl.
4257 *
4258 * Returns:
4259 * Zero on success, errno on failure.
4260 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004261int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4262 struct drm_file *file_priv)
4263{
4264 struct drm_mode_obj_set_property *arg = data;
4265 struct drm_mode_object *arg_obj;
4266 struct drm_mode_object *prop_obj;
4267 struct drm_property *property;
4268 int ret = -EINVAL;
4269 int i;
4270
4271 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4272 return -EINVAL;
4273
Daniel Vetter84849902012-12-02 00:28:11 +01004274 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004275
4276 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004277 if (!arg_obj) {
4278 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004279 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004280 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004281 if (!arg_obj->properties)
4282 goto out;
4283
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004284 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004285 if (arg_obj->properties->ids[i] == arg->prop_id)
4286 break;
4287
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004288 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004289 goto out;
4290
4291 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4292 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004293 if (!prop_obj) {
4294 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004295 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004296 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004297 property = obj_to_property(prop_obj);
4298
4299 if (!drm_property_change_is_valid(property, arg->value))
4300 goto out;
4301
4302 switch (arg_obj->type) {
4303 case DRM_MODE_OBJECT_CONNECTOR:
4304 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4305 arg->value);
4306 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004307 case DRM_MODE_OBJECT_CRTC:
4308 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4309 break;
Rob Clark4d939142012-05-17 02:23:27 -06004310 case DRM_MODE_OBJECT_PLANE:
4311 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4312 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004313 }
4314
4315out:
Daniel Vetter84849902012-12-02 00:28:11 +01004316 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004317 return ret;
4318}
4319
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004320/**
4321 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4322 * @connector: connector to attach
4323 * @encoder: encoder to attach @connector to
4324 *
4325 * This function links up a connector to an encoder. Note that the routing
4326 * restrictions between encoders and crtcs are exposed to userspace through the
4327 * possible_clones and possible_crtcs bitmasks.
4328 *
4329 * Returns:
4330 * Zero on success, errno on failure.
4331 */
Dave Airlief453ba02008-11-07 14:05:41 -08004332int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4333 struct drm_encoder *encoder)
4334{
4335 int i;
4336
4337 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4338 if (connector->encoder_ids[i] == 0) {
4339 connector->encoder_ids[i] = encoder->base.id;
4340 return 0;
4341 }
4342 }
4343 return -ENOMEM;
4344}
4345EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4346
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004347/**
4348 * drm_mode_crtc_set_gamma_size - set the gamma table size
4349 * @crtc: CRTC to set the gamma table size for
4350 * @gamma_size: size of the gamma table
4351 *
4352 * Drivers which support gamma tables should set this to the supported gamma
4353 * table size when initializing the CRTC. Currently the drm core only supports a
4354 * fixed gamma table size.
4355 *
4356 * Returns:
4357 * Zero on success, errno on failure.
4358 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004359int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004360 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004361{
4362 crtc->gamma_size = gamma_size;
4363
4364 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4365 if (!crtc->gamma_store) {
4366 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004367 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004368 }
4369
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004370 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004371}
4372EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4373
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004374/**
4375 * drm_mode_gamma_set_ioctl - set the gamma table
4376 * @dev: DRM device
4377 * @data: ioctl data
4378 * @file_priv: DRM file info
4379 *
4380 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4381 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4382 *
4383 * Called by the user via ioctl.
4384 *
4385 * Returns:
4386 * Zero on success, errno on failure.
4387 */
Dave Airlief453ba02008-11-07 14:05:41 -08004388int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4389 void *data, struct drm_file *file_priv)
4390{
4391 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004392 struct drm_crtc *crtc;
4393 void *r_base, *g_base, *b_base;
4394 int size;
4395 int ret = 0;
4396
Dave Airliefb3b06c2011-02-08 13:55:21 +10004397 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4398 return -EINVAL;
4399
Daniel Vetter84849902012-12-02 00:28:11 +01004400 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004401 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4402 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004403 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004404 goto out;
4405 }
Dave Airlief453ba02008-11-07 14:05:41 -08004406
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004407 if (crtc->funcs->gamma_set == NULL) {
4408 ret = -ENOSYS;
4409 goto out;
4410 }
4411
Dave Airlief453ba02008-11-07 14:05:41 -08004412 /* memcpy into gamma store */
4413 if (crtc_lut->gamma_size != crtc->gamma_size) {
4414 ret = -EINVAL;
4415 goto out;
4416 }
4417
4418 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4419 r_base = crtc->gamma_store;
4420 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4421 ret = -EFAULT;
4422 goto out;
4423 }
4424
4425 g_base = r_base + size;
4426 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4427 ret = -EFAULT;
4428 goto out;
4429 }
4430
4431 b_base = g_base + size;
4432 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4433 ret = -EFAULT;
4434 goto out;
4435 }
4436
James Simmons72034252010-08-03 01:33:19 +01004437 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004438
4439out:
Daniel Vetter84849902012-12-02 00:28:11 +01004440 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004441 return ret;
4442
4443}
4444
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004445/**
4446 * drm_mode_gamma_get_ioctl - get the gamma table
4447 * @dev: DRM device
4448 * @data: ioctl data
4449 * @file_priv: DRM file info
4450 *
4451 * Copy the current gamma table into the storage provided. This also provides
4452 * the gamma table size the driver expects, which can be used to size the
4453 * allocated storage.
4454 *
4455 * Called by the user via ioctl.
4456 *
4457 * Returns:
4458 * Zero on success, errno on failure.
4459 */
Dave Airlief453ba02008-11-07 14:05:41 -08004460int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4461 void *data, struct drm_file *file_priv)
4462{
4463 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004464 struct drm_crtc *crtc;
4465 void *r_base, *g_base, *b_base;
4466 int size;
4467 int ret = 0;
4468
Dave Airliefb3b06c2011-02-08 13:55:21 +10004469 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4470 return -EINVAL;
4471
Daniel Vetter84849902012-12-02 00:28:11 +01004472 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004473 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4474 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004475 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004476 goto out;
4477 }
Dave Airlief453ba02008-11-07 14:05:41 -08004478
4479 /* memcpy into gamma store */
4480 if (crtc_lut->gamma_size != crtc->gamma_size) {
4481 ret = -EINVAL;
4482 goto out;
4483 }
4484
4485 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4486 r_base = crtc->gamma_store;
4487 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4488 ret = -EFAULT;
4489 goto out;
4490 }
4491
4492 g_base = r_base + size;
4493 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4494 ret = -EFAULT;
4495 goto out;
4496 }
4497
4498 b_base = g_base + size;
4499 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4500 ret = -EFAULT;
4501 goto out;
4502 }
4503out:
Daniel Vetter84849902012-12-02 00:28:11 +01004504 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004505 return ret;
4506}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004507
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004508/**
4509 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4510 * @dev: DRM device
4511 * @data: ioctl data
4512 * @file_priv: DRM file info
4513 *
4514 * This schedules an asynchronous update on a given CRTC, called page flip.
4515 * Optionally a drm event is generated to signal the completion of the event.
4516 * Generic drivers cannot assume that a pageflip with changed framebuffer
4517 * properties (including driver specific metadata like tiling layout) will work,
4518 * but some drivers support e.g. pixel format changes through the pageflip
4519 * ioctl.
4520 *
4521 * Called by the user via ioctl.
4522 *
4523 * Returns:
4524 * Zero on success, errno on failure.
4525 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004526int drm_mode_page_flip_ioctl(struct drm_device *dev,
4527 void *data, struct drm_file *file_priv)
4528{
4529 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004530 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004531 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004532 struct drm_pending_vblank_event *e = NULL;
4533 unsigned long flags;
4534 int ret = -EINVAL;
4535
4536 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4537 page_flip->reserved != 0)
4538 return -EINVAL;
4539
Keith Packard62f21042013-07-22 18:50:00 -07004540 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4541 return -EINVAL;
4542
Rob Clarka2b34e22013-10-05 16:36:52 -04004543 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4544 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004545 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004546
Rob Clark51fd3712013-11-19 12:10:12 -05004547 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004548 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004549 /* The framebuffer is currently unbound, presumably
4550 * due to a hotplug event, that userspace has not
4551 * yet discovered.
4552 */
4553 ret = -EBUSY;
4554 goto out;
4555 }
4556
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004557 if (crtc->funcs->page_flip == NULL)
4558 goto out;
4559
Daniel Vetter786b99e2012-12-02 21:53:40 +01004560 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004561 if (!fb) {
4562 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004563 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004564 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004565
Damien Lespiauc11e9282013-09-25 16:45:30 +01004566 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4567 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004568 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004569
Matt Roperf4510a22014-04-01 15:22:40 -07004570 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004571 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4572 ret = -EINVAL;
4573 goto out;
4574 }
4575
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004576 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4577 ret = -ENOMEM;
4578 spin_lock_irqsave(&dev->event_lock, flags);
4579 if (file_priv->event_space < sizeof e->event) {
4580 spin_unlock_irqrestore(&dev->event_lock, flags);
4581 goto out;
4582 }
4583 file_priv->event_space -= sizeof e->event;
4584 spin_unlock_irqrestore(&dev->event_lock, flags);
4585
4586 e = kzalloc(sizeof *e, GFP_KERNEL);
4587 if (e == NULL) {
4588 spin_lock_irqsave(&dev->event_lock, flags);
4589 file_priv->event_space += sizeof e->event;
4590 spin_unlock_irqrestore(&dev->event_lock, flags);
4591 goto out;
4592 }
4593
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004594 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004595 e->event.base.length = sizeof e->event;
4596 e->event.user_data = page_flip->user_data;
4597 e->base.event = &e->event.base;
4598 e->base.file_priv = file_priv;
4599 e->base.destroy =
4600 (void (*) (struct drm_pending_event *)) kfree;
4601 }
4602
Matt Roperf4510a22014-04-01 15:22:40 -07004603 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004604 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004605 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004606 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4607 spin_lock_irqsave(&dev->event_lock, flags);
4608 file_priv->event_space += sizeof e->event;
4609 spin_unlock_irqrestore(&dev->event_lock, flags);
4610 kfree(e);
4611 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004612 /* Keep the old fb, don't unref it. */
4613 old_fb = NULL;
4614 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004615 /*
4616 * Warn if the driver hasn't properly updated the crtc->fb
4617 * field to reflect that the new framebuffer is now used.
4618 * Failing to do so will screw with the reference counting
4619 * on framebuffers.
4620 */
Matt Roperf4510a22014-04-01 15:22:40 -07004621 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004622 /* Unref only the old framebuffer. */
4623 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004624 }
4625
4626out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004627 if (fb)
4628 drm_framebuffer_unreference(fb);
4629 if (old_fb)
4630 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004631 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004632
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004633 return ret;
4634}
Chris Wilsoneb033552011-01-24 15:11:08 +00004635
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004636/**
4637 * drm_mode_config_reset - call ->reset callbacks
4638 * @dev: drm device
4639 *
4640 * This functions calls all the crtc's, encoder's and connector's ->reset
4641 * callback. Drivers can use this in e.g. their driver load or resume code to
4642 * reset hardware and software state.
4643 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004644void drm_mode_config_reset(struct drm_device *dev)
4645{
4646 struct drm_crtc *crtc;
4647 struct drm_encoder *encoder;
4648 struct drm_connector *connector;
4649
4650 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4651 if (crtc->funcs->reset)
4652 crtc->funcs->reset(crtc);
4653
4654 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4655 if (encoder->funcs->reset)
4656 encoder->funcs->reset(encoder);
4657
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004658 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4659 connector->status = connector_status_unknown;
4660
Chris Wilsoneb033552011-01-24 15:11:08 +00004661 if (connector->funcs->reset)
4662 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004663 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004664}
4665EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004666
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004667/**
4668 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4669 * @dev: DRM device
4670 * @data: ioctl data
4671 * @file_priv: DRM file info
4672 *
4673 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4674 * TTM or something else entirely) and returns the resulting buffer handle. This
4675 * handle can then be wrapped up into a framebuffer modeset object.
4676 *
4677 * Note that userspace is not allowed to use such objects for render
4678 * acceleration - drivers must create their own private ioctls for such a use
4679 * case.
4680 *
4681 * Called by the user via ioctl.
4682 *
4683 * Returns:
4684 * Zero on success, errno on failure.
4685 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004686int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4687 void *data, struct drm_file *file_priv)
4688{
4689 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004690 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004691
4692 if (!dev->driver->dumb_create)
4693 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004694 if (!args->width || !args->height || !args->bpp)
4695 return -EINVAL;
4696
4697 /* overflow checks for 32bit size calculations */
4698 cpp = DIV_ROUND_UP(args->bpp, 8);
4699 if (cpp > 0xffffffffU / args->width)
4700 return -EINVAL;
4701 stride = cpp * args->width;
4702 if (args->height > 0xffffffffU / stride)
4703 return -EINVAL;
4704
4705 /* test for wrap-around */
4706 size = args->height * stride;
4707 if (PAGE_ALIGN(size) == 0)
4708 return -EINVAL;
4709
Dave Airlieff72145b2011-02-07 12:16:14 +10004710 return dev->driver->dumb_create(file_priv, dev, args);
4711}
4712
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004713/**
4714 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4715 * @dev: DRM device
4716 * @data: ioctl data
4717 * @file_priv: DRM file info
4718 *
4719 * Allocate an offset in the drm device node's address space to be able to
4720 * memory map a dumb buffer.
4721 *
4722 * Called by the user via ioctl.
4723 *
4724 * Returns:
4725 * Zero on success, errno on failure.
4726 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004727int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4728 void *data, struct drm_file *file_priv)
4729{
4730 struct drm_mode_map_dumb *args = data;
4731
4732 /* call driver ioctl to get mmap offset */
4733 if (!dev->driver->dumb_map_offset)
4734 return -ENOSYS;
4735
4736 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4737}
4738
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004739/**
4740 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4741 * @dev: DRM device
4742 * @data: ioctl data
4743 * @file_priv: DRM file info
4744 *
4745 * This destroys the userspace handle for the given dumb backing storage buffer.
4746 * Since buffer objects must be reference counted in the kernel a buffer object
4747 * won't be immediately freed if a framebuffer modeset object still uses it.
4748 *
4749 * Called by the user via ioctl.
4750 *
4751 * Returns:
4752 * Zero on success, errno on failure.
4753 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004754int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4755 void *data, struct drm_file *file_priv)
4756{
4757 struct drm_mode_destroy_dumb *args = data;
4758
4759 if (!dev->driver->dumb_destroy)
4760 return -ENOSYS;
4761
4762 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4763}
Dave Airlie248dbc22011-11-29 20:02:54 +00004764
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004765/**
4766 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4767 * @format: pixel format (DRM_FORMAT_*)
4768 * @depth: storage for the depth value
4769 * @bpp: storage for the bpp value
4770 *
4771 * This only supports RGB formats here for compat with code that doesn't use
4772 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004773 */
4774void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4775 int *bpp)
4776{
4777 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004778 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004779 case DRM_FORMAT_RGB332:
4780 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004781 *depth = 8;
4782 *bpp = 8;
4783 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004784 case DRM_FORMAT_XRGB1555:
4785 case DRM_FORMAT_XBGR1555:
4786 case DRM_FORMAT_RGBX5551:
4787 case DRM_FORMAT_BGRX5551:
4788 case DRM_FORMAT_ARGB1555:
4789 case DRM_FORMAT_ABGR1555:
4790 case DRM_FORMAT_RGBA5551:
4791 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004792 *depth = 15;
4793 *bpp = 16;
4794 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004795 case DRM_FORMAT_RGB565:
4796 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004797 *depth = 16;
4798 *bpp = 16;
4799 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004800 case DRM_FORMAT_RGB888:
4801 case DRM_FORMAT_BGR888:
4802 *depth = 24;
4803 *bpp = 24;
4804 break;
4805 case DRM_FORMAT_XRGB8888:
4806 case DRM_FORMAT_XBGR8888:
4807 case DRM_FORMAT_RGBX8888:
4808 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004809 *depth = 24;
4810 *bpp = 32;
4811 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004812 case DRM_FORMAT_XRGB2101010:
4813 case DRM_FORMAT_XBGR2101010:
4814 case DRM_FORMAT_RGBX1010102:
4815 case DRM_FORMAT_BGRX1010102:
4816 case DRM_FORMAT_ARGB2101010:
4817 case DRM_FORMAT_ABGR2101010:
4818 case DRM_FORMAT_RGBA1010102:
4819 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004820 *depth = 30;
4821 *bpp = 32;
4822 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004823 case DRM_FORMAT_ARGB8888:
4824 case DRM_FORMAT_ABGR8888:
4825 case DRM_FORMAT_RGBA8888:
4826 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004827 *depth = 32;
4828 *bpp = 32;
4829 break;
4830 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004831 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4832 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004833 *depth = 0;
4834 *bpp = 0;
4835 break;
4836 }
4837}
4838EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004839
4840/**
4841 * drm_format_num_planes - get the number of planes for format
4842 * @format: pixel format (DRM_FORMAT_*)
4843 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004844 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004845 * The number of planes used by the specified pixel format.
4846 */
4847int drm_format_num_planes(uint32_t format)
4848{
4849 switch (format) {
4850 case DRM_FORMAT_YUV410:
4851 case DRM_FORMAT_YVU410:
4852 case DRM_FORMAT_YUV411:
4853 case DRM_FORMAT_YVU411:
4854 case DRM_FORMAT_YUV420:
4855 case DRM_FORMAT_YVU420:
4856 case DRM_FORMAT_YUV422:
4857 case DRM_FORMAT_YVU422:
4858 case DRM_FORMAT_YUV444:
4859 case DRM_FORMAT_YVU444:
4860 return 3;
4861 case DRM_FORMAT_NV12:
4862 case DRM_FORMAT_NV21:
4863 case DRM_FORMAT_NV16:
4864 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004865 case DRM_FORMAT_NV24:
4866 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004867 return 2;
4868 default:
4869 return 1;
4870 }
4871}
4872EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004873
4874/**
4875 * drm_format_plane_cpp - determine the bytes per pixel value
4876 * @format: pixel format (DRM_FORMAT_*)
4877 * @plane: plane index
4878 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004879 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004880 * The bytes per pixel value for the specified plane.
4881 */
4882int drm_format_plane_cpp(uint32_t format, int plane)
4883{
4884 unsigned int depth;
4885 int bpp;
4886
4887 if (plane >= drm_format_num_planes(format))
4888 return 0;
4889
4890 switch (format) {
4891 case DRM_FORMAT_YUYV:
4892 case DRM_FORMAT_YVYU:
4893 case DRM_FORMAT_UYVY:
4894 case DRM_FORMAT_VYUY:
4895 return 2;
4896 case DRM_FORMAT_NV12:
4897 case DRM_FORMAT_NV21:
4898 case DRM_FORMAT_NV16:
4899 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004900 case DRM_FORMAT_NV24:
4901 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004902 return plane ? 2 : 1;
4903 case DRM_FORMAT_YUV410:
4904 case DRM_FORMAT_YVU410:
4905 case DRM_FORMAT_YUV411:
4906 case DRM_FORMAT_YVU411:
4907 case DRM_FORMAT_YUV420:
4908 case DRM_FORMAT_YVU420:
4909 case DRM_FORMAT_YUV422:
4910 case DRM_FORMAT_YVU422:
4911 case DRM_FORMAT_YUV444:
4912 case DRM_FORMAT_YVU444:
4913 return 1;
4914 default:
4915 drm_fb_get_bpp_depth(format, &depth, &bpp);
4916 return bpp >> 3;
4917 }
4918}
4919EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004920
4921/**
4922 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4923 * @format: pixel format (DRM_FORMAT_*)
4924 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004925 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004926 * The horizontal chroma subsampling factor for the
4927 * specified pixel format.
4928 */
4929int drm_format_horz_chroma_subsampling(uint32_t format)
4930{
4931 switch (format) {
4932 case DRM_FORMAT_YUV411:
4933 case DRM_FORMAT_YVU411:
4934 case DRM_FORMAT_YUV410:
4935 case DRM_FORMAT_YVU410:
4936 return 4;
4937 case DRM_FORMAT_YUYV:
4938 case DRM_FORMAT_YVYU:
4939 case DRM_FORMAT_UYVY:
4940 case DRM_FORMAT_VYUY:
4941 case DRM_FORMAT_NV12:
4942 case DRM_FORMAT_NV21:
4943 case DRM_FORMAT_NV16:
4944 case DRM_FORMAT_NV61:
4945 case DRM_FORMAT_YUV422:
4946 case DRM_FORMAT_YVU422:
4947 case DRM_FORMAT_YUV420:
4948 case DRM_FORMAT_YVU420:
4949 return 2;
4950 default:
4951 return 1;
4952 }
4953}
4954EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4955
4956/**
4957 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4958 * @format: pixel format (DRM_FORMAT_*)
4959 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004960 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004961 * The vertical chroma subsampling factor for the
4962 * specified pixel format.
4963 */
4964int drm_format_vert_chroma_subsampling(uint32_t format)
4965{
4966 switch (format) {
4967 case DRM_FORMAT_YUV410:
4968 case DRM_FORMAT_YVU410:
4969 return 4;
4970 case DRM_FORMAT_YUV420:
4971 case DRM_FORMAT_YVU420:
4972 case DRM_FORMAT_NV12:
4973 case DRM_FORMAT_NV21:
4974 return 2;
4975 default:
4976 return 1;
4977 }
4978}
4979EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004980
4981/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05304982 * drm_rotation_simplify() - Try to simplify the rotation
4983 * @rotation: Rotation to be simplified
4984 * @supported_rotations: Supported rotations
4985 *
4986 * Attempt to simplify the rotation to a form that is supported.
4987 * Eg. if the hardware supports everything except DRM_REFLECT_X
4988 * one could call this function like this:
4989 *
4990 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
4991 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
4992 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
4993 *
4994 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
4995 * transforms the hardware supports, this function may not
4996 * be able to produce a supported transform, so the caller should
4997 * check the result afterwards.
4998 */
4999unsigned int drm_rotation_simplify(unsigned int rotation,
5000 unsigned int supported_rotations)
5001{
5002 if (rotation & ~supported_rotations) {
5003 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5004 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5005 }
5006
5007 return rotation;
5008}
5009EXPORT_SYMBOL(drm_rotation_simplify);
5010
5011/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005012 * drm_mode_config_init - initialize DRM mode_configuration structure
5013 * @dev: DRM device
5014 *
5015 * Initialize @dev's mode_config structure, used for tracking the graphics
5016 * configuration of @dev.
5017 *
5018 * Since this initializes the modeset locks, no locking is possible. Which is no
5019 * problem, since this should happen single threaded at init time. It is the
5020 * driver's problem to ensure this guarantee.
5021 *
5022 */
5023void drm_mode_config_init(struct drm_device *dev)
5024{
5025 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005026 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005027 mutex_init(&dev->mode_config.idr_mutex);
5028 mutex_init(&dev->mode_config.fb_lock);
5029 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5030 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5031 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04005032 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005033 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5034 INIT_LIST_HEAD(&dev->mode_config.property_list);
5035 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5036 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5037 idr_init(&dev->mode_config.crtc_idr);
5038
5039 drm_modeset_lock_all(dev);
5040 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04005041 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005042 drm_modeset_unlock_all(dev);
5043
5044 /* Just to be sure */
5045 dev->mode_config.num_fb = 0;
5046 dev->mode_config.num_connector = 0;
5047 dev->mode_config.num_crtc = 0;
5048 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005049 dev->mode_config.num_overlay_plane = 0;
5050 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005051}
5052EXPORT_SYMBOL(drm_mode_config_init);
5053
5054/**
5055 * drm_mode_config_cleanup - free up DRM mode_config info
5056 * @dev: DRM device
5057 *
5058 * Free up all the connectors and CRTCs associated with this DRM device, then
5059 * free up the framebuffers and associated buffer objects.
5060 *
5061 * Note that since this /should/ happen single-threaded at driver/device
5062 * teardown time, no locking is required. It's the driver's job to ensure that
5063 * this guarantee actually holds true.
5064 *
5065 * FIXME: cleanup any dangling user buffer objects too
5066 */
5067void drm_mode_config_cleanup(struct drm_device *dev)
5068{
5069 struct drm_connector *connector, *ot;
5070 struct drm_crtc *crtc, *ct;
5071 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04005072 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005073 struct drm_framebuffer *fb, *fbt;
5074 struct drm_property *property, *pt;
5075 struct drm_property_blob *blob, *bt;
5076 struct drm_plane *plane, *plt;
5077
5078 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5079 head) {
5080 encoder->funcs->destroy(encoder);
5081 }
5082
Sean Paul3b336ec2013-08-14 16:47:37 -04005083 list_for_each_entry_safe(bridge, brt,
5084 &dev->mode_config.bridge_list, head) {
5085 bridge->funcs->destroy(bridge);
5086 }
5087
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005088 list_for_each_entry_safe(connector, ot,
5089 &dev->mode_config.connector_list, head) {
5090 connector->funcs->destroy(connector);
5091 }
5092
5093 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5094 head) {
5095 drm_property_destroy(dev, property);
5096 }
5097
5098 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5099 head) {
5100 drm_property_destroy_blob(dev, blob);
5101 }
5102
5103 /*
5104 * Single-threaded teardown context, so it's not required to grab the
5105 * fb_lock to protect against concurrent fb_list access. Contrary, it
5106 * would actually deadlock with the drm_framebuffer_cleanup function.
5107 *
5108 * Also, if there are any framebuffers left, that's a driver leak now,
5109 * so politely WARN about this.
5110 */
5111 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5112 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5113 drm_framebuffer_remove(fb);
5114 }
5115
5116 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5117 head) {
5118 plane->funcs->destroy(plane);
5119 }
5120
5121 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5122 crtc->funcs->destroy(crtc);
5123 }
5124
5125 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005126 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005127}
5128EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305129
5130struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5131 unsigned int supported_rotations)
5132{
5133 static const struct drm_prop_enum_list props[] = {
5134 { DRM_ROTATE_0, "rotate-0" },
5135 { DRM_ROTATE_90, "rotate-90" },
5136 { DRM_ROTATE_180, "rotate-180" },
5137 { DRM_ROTATE_270, "rotate-270" },
5138 { DRM_REFLECT_X, "reflect-x" },
5139 { DRM_REFLECT_Y, "reflect-y" },
5140 };
5141
5142 return drm_property_create_bitmask(dev, 0, "rotation",
5143 props, ARRAY_SIZE(props),
5144 supported_rotations);
5145}
5146EXPORT_SYMBOL(drm_mode_create_rotation_property);