blob: 66d3bfb8d26442e67366416bcea49b6739be4ab6 [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);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200449 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
450 obj = NULL;
451 if (obj && obj->id != id)
452 obj = NULL;
453 /* don't leak out unref'd fb's */
454 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Rob Clark98f75de2014-05-30 11:37:03 -0400455 obj = NULL;
456 mutex_unlock(&dev->mode_config.idr_mutex);
457
458 return obj;
459}
460
Daniel Vetter786b99e2012-12-02 21:53:40 +0100461/**
462 * drm_mode_object_find - look up a drm object with static lifetime
463 * @dev: drm device
464 * @id: id of the mode object
465 * @type: type of the mode object
466 *
467 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400468 * are reference counted, they need special treatment. Even with
469 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
470 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100471 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200472struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
473 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800474{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000475 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800476
Daniel Vetter786b99e2012-12-02 21:53:40 +0100477 /* Framebuffers are reference counted and need their own lookup
478 * function.*/
479 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400480 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800481 return obj;
482}
483EXPORT_SYMBOL(drm_mode_object_find);
484
485/**
Dave Airlief453ba02008-11-07 14:05:41 -0800486 * drm_framebuffer_init - initialize a framebuffer
487 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100488 * @fb: framebuffer to be initialized
489 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800490 *
Dave Airlief453ba02008-11-07 14:05:41 -0800491 * Allocates an ID for the framebuffer's parent mode object, sets its mode
492 * functions & device file and adds it to the master fd list.
493 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100494 * IMPORTANT:
495 * This functions publishes the fb and makes it available for concurrent access
496 * by other users. Which means by this point the fb _must_ be fully set up -
497 * since all the fb attributes are invariant over its lifetime, no further
498 * locking but only correct reference counting is required.
499 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100500 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200501 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800502 */
503int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
504 const struct drm_framebuffer_funcs *funcs)
505{
506 int ret;
507
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100508 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000509 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100510 INIT_LIST_HEAD(&fb->filp_head);
511 fb->dev = dev;
512 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000513
Dave Airlief453ba02008-11-07 14:05:41 -0800514 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200515 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100516 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800517
Dave Airlief453ba02008-11-07 14:05:41 -0800518 dev->mode_config.num_fb++;
519 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100520out:
521 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800522
523 return 0;
524}
525EXPORT_SYMBOL(drm_framebuffer_init);
526
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200527/* dev->mode_config.fb_lock must be held! */
528static void __drm_framebuffer_unregister(struct drm_device *dev,
529 struct drm_framebuffer *fb)
530{
531 mutex_lock(&dev->mode_config.idr_mutex);
532 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
533 mutex_unlock(&dev->mode_config.idr_mutex);
534
535 fb->base.id = 0;
536}
537
Rob Clarkf7eff602012-09-05 21:48:38 +0000538static void drm_framebuffer_free(struct kref *kref)
539{
540 struct drm_framebuffer *fb =
541 container_of(kref, struct drm_framebuffer, refcount);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200542 struct drm_device *dev = fb->dev;
543
544 /*
545 * The lookup idr holds a weak reference, which has not necessarily been
546 * removed at this point. Check for that.
547 */
548 mutex_lock(&dev->mode_config.fb_lock);
549 if (fb->base.id) {
550 /* Mark fb as reaped and drop idr ref. */
551 __drm_framebuffer_unregister(dev, fb);
552 }
553 mutex_unlock(&dev->mode_config.fb_lock);
554
Rob Clarkf7eff602012-09-05 21:48:38 +0000555 fb->funcs->destroy(fb);
556}
557
Daniel Vetter2b677e82012-12-10 21:16:05 +0100558static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
559 uint32_t id)
560{
561 struct drm_mode_object *obj = NULL;
562 struct drm_framebuffer *fb;
563
564 mutex_lock(&dev->mode_config.idr_mutex);
565 obj = idr_find(&dev->mode_config.crtc_idr, id);
566 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
567 fb = NULL;
568 else
569 fb = obj_to_fb(obj);
570 mutex_unlock(&dev->mode_config.idr_mutex);
571
572 return fb;
573}
574
Rob Clarkf7eff602012-09-05 21:48:38 +0000575/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100576 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
577 * @dev: drm device
578 * @id: id of the fb object
579 *
580 * If successful, this grabs an additional reference to the framebuffer -
581 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100582 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100583 */
584struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
585 uint32_t id)
586{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100587 struct drm_framebuffer *fb;
588
589 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100590 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200591 if (fb) {
592 if (!kref_get_unless_zero(&fb->refcount))
593 fb = NULL;
594 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100595 mutex_unlock(&dev->mode_config.fb_lock);
596
597 return fb;
598}
599EXPORT_SYMBOL(drm_framebuffer_lookup);
600
601/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000602 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100603 * @fb: framebuffer to unref
604 *
605 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000606 */
607void drm_framebuffer_unreference(struct drm_framebuffer *fb)
608{
Rob Clark82912722014-03-18 10:07:08 -0400609 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000610 kref_put(&fb->refcount, drm_framebuffer_free);
611}
612EXPORT_SYMBOL(drm_framebuffer_unreference);
613
614/**
615 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100616 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100617 *
618 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000619 */
620void drm_framebuffer_reference(struct drm_framebuffer *fb)
621{
Rob Clark82912722014-03-18 10:07:08 -0400622 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000623 kref_get(&fb->refcount);
624}
625EXPORT_SYMBOL(drm_framebuffer_reference);
626
Daniel Vetter2b677e82012-12-10 21:16:05 +0100627static void drm_framebuffer_free_bug(struct kref *kref)
628{
629 BUG();
630}
631
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100632static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
633{
Rob Clark82912722014-03-18 10:07:08 -0400634 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100635 kref_put(&fb->refcount, drm_framebuffer_free_bug);
636}
637
Dave Airlief453ba02008-11-07 14:05:41 -0800638/**
Daniel Vetter36206362012-12-10 20:42:17 +0100639 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
640 * @fb: fb to unregister
641 *
642 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
643 * those used for fbdev. Note that the caller must hold a reference of it's own,
644 * i.e. the object may not be destroyed through this call (since it'll lead to a
645 * locking inversion).
646 */
647void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
648{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100649 struct drm_device *dev = fb->dev;
650
651 mutex_lock(&dev->mode_config.fb_lock);
652 /* Mark fb as reaped and drop idr ref. */
653 __drm_framebuffer_unregister(dev, fb);
654 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100655}
656EXPORT_SYMBOL(drm_framebuffer_unregister_private);
657
658/**
Dave Airlief453ba02008-11-07 14:05:41 -0800659 * drm_framebuffer_cleanup - remove a framebuffer object
660 * @fb: framebuffer to remove
661 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100662 * Cleanup framebuffer. This function is intended to be used from the drivers
663 * ->destroy callback. It can also be used to clean up driver private
664 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100665 *
666 * Note that this function does not remove the fb from active usuage - if it is
667 * still used anywhere, hilarity can ensue since userspace could call getfb on
668 * the id and get back -EINVAL. Obviously no concern at driver unload time.
669 *
670 * Also, the framebuffer will not be removed from the lookup idr - for
671 * user-created framebuffers this will happen in in the rmfb ioctl. For
672 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
673 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800674 */
675void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
676{
677 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100678
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100679 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000680 list_del(&fb->head);
681 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100682 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000683}
684EXPORT_SYMBOL(drm_framebuffer_cleanup);
685
686/**
687 * drm_framebuffer_remove - remove and unreference a framebuffer object
688 * @fb: framebuffer to remove
689 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000690 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100691 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100692 * passed-in framebuffer. Might take the modeset locks.
693 *
694 * Note that this function optimizes the cleanup away if the caller holds the
695 * last reference to the framebuffer. It is also guaranteed to not take the
696 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000697 */
698void drm_framebuffer_remove(struct drm_framebuffer *fb)
699{
700 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800701 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800702 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000703 struct drm_mode_set set;
704 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800705
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100706 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100707
Daniel Vetterb62584e2012-12-11 16:51:35 +0100708 /*
709 * drm ABI mandates that we remove any deleted framebuffers from active
710 * useage. But since most sane clients only remove framebuffers they no
711 * longer need, try to optimize this away.
712 *
713 * Since we're holding a reference ourselves, observing a refcount of 1
714 * means that we're the last holder and can skip it. Also, the refcount
715 * can never increase from 1 again, so we don't need any barriers or
716 * locks.
717 *
718 * Note that userspace could try to race with use and instate a new
719 * usage _after_ we've cleared all current ones. End result will be an
720 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
721 * in this manner.
722 */
723 if (atomic_read(&fb->refcount.refcount) > 1) {
724 drm_modeset_lock_all(dev);
725 /* remove from any CRTC */
726 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700727 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100728 /* should turn off the crtc */
729 memset(&set, 0, sizeof(struct drm_mode_set));
730 set.crtc = crtc;
731 set.fb = NULL;
732 ret = drm_mode_set_config_internal(&set);
733 if (ret)
734 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
735 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000736 }
Dave Airlief453ba02008-11-07 14:05:41 -0800737
Daniel Vetterb62584e2012-12-11 16:51:35 +0100738 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300739 if (plane->fb == fb)
740 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800741 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100742 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800743 }
744
Rob Clarkf7eff602012-09-05 21:48:38 +0000745 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800746}
Rob Clarkf7eff602012-09-05 21:48:38 +0000747EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800748
Rob Clark51fd3712013-11-19 12:10:12 -0500749DEFINE_WW_CLASS(crtc_ww_class);
750
Dave Airlief453ba02008-11-07 14:05:41 -0800751/**
Matt Ropere13161a2014-04-01 15:22:38 -0700752 * drm_crtc_init_with_planes - Initialise a new CRTC object with
753 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800754 * @dev: DRM device
755 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700756 * @primary: Primary plane for CRTC
757 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800758 * @funcs: callbacks for the new CRTC
759 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000760 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200761 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100762 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200763 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800764 */
Matt Ropere13161a2014-04-01 15:22:38 -0700765int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
766 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700767 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700768 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800769{
Rob Clark51fd3712013-11-19 12:10:12 -0500770 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200771 int ret;
772
Dave Airlief453ba02008-11-07 14:05:41 -0800773 crtc->dev = dev;
774 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000775 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800776
Daniel Vetter84849902012-12-02 00:28:11 +0100777 drm_modeset_lock_all(dev);
Rob Clark51fd3712013-11-19 12:10:12 -0500778 drm_modeset_lock_init(&crtc->mutex);
779 /* dropped by _unlock_all(): */
780 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200781
782 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
783 if (ret)
784 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800785
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300786 crtc->base.properties = &crtc->properties;
787
Rob Clark51fd3712013-11-19 12:10:12 -0500788 list_add_tail(&crtc->head, &config->crtc_list);
789 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200790
Matt Ropere13161a2014-04-01 15:22:38 -0700791 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700792 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700793 if (primary)
794 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700795 if (cursor)
796 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700797
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200798 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100799 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200800
801 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800802}
Matt Ropere13161a2014-04-01 15:22:38 -0700803EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800804
805/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000806 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800807 * @crtc: CRTC to cleanup
808 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000809 * This function cleans up @crtc and removes it from the DRM mode setting
810 * core. Note that the function does *not* free the crtc structure itself,
811 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800812 */
813void drm_crtc_cleanup(struct drm_crtc *crtc)
814{
815 struct drm_device *dev = crtc->dev;
816
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000817 kfree(crtc->gamma_store);
818 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800819
Rob Clark51fd3712013-11-19 12:10:12 -0500820 drm_modeset_lock_fini(&crtc->mutex);
821
Dave Airlief453ba02008-11-07 14:05:41 -0800822 drm_mode_object_put(dev, &crtc->base);
823 list_del(&crtc->head);
824 dev->mode_config.num_crtc--;
825}
826EXPORT_SYMBOL(drm_crtc_cleanup);
827
828/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000829 * drm_crtc_index - find the index of a registered CRTC
830 * @crtc: CRTC to find index for
831 *
832 * Given a registered CRTC, return the index of that CRTC within a DRM
833 * device's list of CRTCs.
834 */
835unsigned int drm_crtc_index(struct drm_crtc *crtc)
836{
837 unsigned int index = 0;
838 struct drm_crtc *tmp;
839
840 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
841 if (tmp == crtc)
842 return index;
843
844 index++;
845 }
846
847 BUG();
848}
849EXPORT_SYMBOL(drm_crtc_index);
850
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100851/*
Dave Airlief453ba02008-11-07 14:05:41 -0800852 * drm_mode_remove - remove and free a mode
853 * @connector: connector list to modify
854 * @mode: mode to remove
855 *
Dave Airlief453ba02008-11-07 14:05:41 -0800856 * Remove @mode from @connector's mode list, then free it.
857 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100858static void drm_mode_remove(struct drm_connector *connector,
859 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800860{
861 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100862 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800863}
Dave Airlief453ba02008-11-07 14:05:41 -0800864
865/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200866 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
867 * @connector: connector to quwery
868 * @mode: returned mode
869 *
870 * The kernel supports per-connector configration of its consoles through
871 * use of the video= parameter. This function parses that option and
872 * extracts the user's specified mode (or enable/disable status) for a
873 * particular connector. This is typically only used during the early fbdev
874 * setup.
875 */
876static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
877{
878 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
879 char *option = NULL;
880
881 if (fb_get_options(connector->name, &option))
882 return;
883
884 if (!drm_mode_parse_command_line_for_connector(option,
885 connector,
886 mode))
887 return;
888
889 if (mode->force) {
890 const char *s;
891
892 switch (mode->force) {
893 case DRM_FORCE_OFF:
894 s = "OFF";
895 break;
896 case DRM_FORCE_ON_DIGITAL:
897 s = "ON - dig";
898 break;
899 default:
900 case DRM_FORCE_ON:
901 s = "ON";
902 break;
903 }
904
905 DRM_INFO("forcing %s connector %s\n", connector->name, s);
906 connector->force = mode->force;
907 }
908
909 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
910 connector->name,
911 mode->xres, mode->yres,
912 mode->refresh_specified ? mode->refresh : 60,
913 mode->rb ? " reduced blanking" : "",
914 mode->margins ? " with margins" : "",
915 mode->interlace ? " interlaced" : "");
916}
917
918/**
Dave Airlief453ba02008-11-07 14:05:41 -0800919 * drm_connector_init - Init a preallocated connector
920 * @dev: DRM device
921 * @connector: the connector to init
922 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100923 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800924 *
Dave Airlief453ba02008-11-07 14:05:41 -0800925 * Initialises a preallocated connector. Connectors should be
926 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200927 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100928 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200929 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800930 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200931int drm_connector_init(struct drm_device *dev,
932 struct drm_connector *connector,
933 const struct drm_connector_funcs *funcs,
934 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800935{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200936 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400937 struct ida *connector_ida =
938 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200939
Daniel Vetter84849902012-12-02 00:28:11 +0100940 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800941
Dave Airlie2ee39452014-07-24 11:53:45 +1000942 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200943 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300944 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200945
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300946 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800947 connector->dev = dev;
948 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800949 connector->connector_type = connector_type;
950 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400951 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
952 if (connector->connector_type_id < 0) {
953 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300954 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400955 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300956 connector->name =
957 kasprintf(GFP_KERNEL, "%s-%d",
958 drm_connector_enum_list[connector_type].name,
959 connector->connector_type_id);
960 if (!connector->name) {
961 ret = -ENOMEM;
962 goto out_put;
963 }
964
Dave Airlief453ba02008-11-07 14:05:41 -0800965 INIT_LIST_HEAD(&connector->probed_modes);
966 INIT_LIST_HEAD(&connector->modes);
967 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000968 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800969
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200970 drm_connector_get_cmdline_mode(connector);
971
Dave Airlief453ba02008-11-07 14:05:41 -0800972 list_add_tail(&connector->head, &dev->mode_config.connector_list);
973 dev->mode_config.num_connector++;
974
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200975 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500976 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200977 dev->mode_config.edid_property,
978 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800979
Rob Clark58495562012-10-11 20:50:56 -0500980 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800981 dev->mode_config.dpms_property, 0);
982
Thomas Wood30f65702014-06-18 17:52:32 +0100983 connector->debugfs_entry = NULL;
984
Jani Nikula2abdd312014-05-14 16:58:19 +0300985out_put:
986 if (ret)
987 drm_mode_object_put(dev, &connector->base);
988
989out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100990 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200991
992 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800993}
994EXPORT_SYMBOL(drm_connector_init);
995
996/**
997 * drm_connector_cleanup - cleans up an initialised connector
998 * @connector: connector to cleanup
999 *
Dave Airlief453ba02008-11-07 14:05:41 -08001000 * Cleans up the connector but doesn't free the object.
1001 */
1002void drm_connector_cleanup(struct drm_connector *connector)
1003{
1004 struct drm_device *dev = connector->dev;
1005 struct drm_display_mode *mode, *t;
1006
1007 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
1008 drm_mode_remove(connector, mode);
1009
1010 list_for_each_entry_safe(mode, t, &connector->modes, head)
1011 drm_mode_remove(connector, mode);
1012
Ilia Mirkinb21e3af2013-08-07 22:34:48 -04001013 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
1014 connector->connector_type_id);
1015
Dave Airlief453ba02008-11-07 14:05:41 -08001016 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +03001017 kfree(connector->name);
1018 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001019 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001020 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -08001021}
1022EXPORT_SYMBOL(drm_connector_cleanup);
1023
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001024/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001025 * drm_connector_register - register a connector
1026 * @connector: the connector to register
1027 *
1028 * Register userspace interfaces for a connector
1029 *
1030 * Returns:
1031 * Zero on success, error code on failure.
1032 */
1033int drm_connector_register(struct drm_connector *connector)
1034{
Thomas Wood30f65702014-06-18 17:52:32 +01001035 int ret;
1036
Dave Airlie2ee39452014-07-24 11:53:45 +10001037 drm_mode_object_register(connector->dev, &connector->base);
1038
Thomas Wood30f65702014-06-18 17:52:32 +01001039 ret = drm_sysfs_connector_add(connector);
1040 if (ret)
1041 return ret;
1042
1043 ret = drm_debugfs_connector_add(connector);
1044 if (ret) {
1045 drm_sysfs_connector_remove(connector);
1046 return ret;
1047 }
1048
1049 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001050}
1051EXPORT_SYMBOL(drm_connector_register);
1052
1053/**
1054 * drm_connector_unregister - unregister a connector
1055 * @connector: the connector to unregister
1056 *
1057 * Unregister userspace interfaces for a connector
1058 */
1059void drm_connector_unregister(struct drm_connector *connector)
1060{
1061 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001062 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001063}
1064EXPORT_SYMBOL(drm_connector_unregister);
1065
1066
1067/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001068 * drm_connector_unplug_all - unregister connector userspace interfaces
1069 * @dev: drm device
1070 *
1071 * This function unregisters all connector userspace interfaces in sysfs. Should
1072 * be call when the device is disconnected, e.g. from an usb driver's
1073 * ->disconnect callback.
1074 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001075void drm_connector_unplug_all(struct drm_device *dev)
1076{
1077 struct drm_connector *connector;
1078
1079 /* taking the mode config mutex ends up in a clash with sysfs */
1080 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001081 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001082
1083}
1084EXPORT_SYMBOL(drm_connector_unplug_all);
1085
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001086/**
1087 * drm_bridge_init - initialize a drm transcoder/bridge
1088 * @dev: drm device
1089 * @bridge: transcoder/bridge to set up
1090 * @funcs: bridge function table
1091 *
1092 * Initialises a preallocated bridge. Bridges should be
1093 * subclassed as part of driver connector objects.
1094 *
1095 * Returns:
1096 * Zero on success, error code on failure.
1097 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001098int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1099 const struct drm_bridge_funcs *funcs)
1100{
1101 int ret;
1102
1103 drm_modeset_lock_all(dev);
1104
1105 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1106 if (ret)
1107 goto out;
1108
1109 bridge->dev = dev;
1110 bridge->funcs = funcs;
1111
1112 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1113 dev->mode_config.num_bridge++;
1114
1115 out:
1116 drm_modeset_unlock_all(dev);
1117 return ret;
1118}
1119EXPORT_SYMBOL(drm_bridge_init);
1120
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001121/**
1122 * drm_bridge_cleanup - cleans up an initialised bridge
1123 * @bridge: bridge to cleanup
1124 *
1125 * Cleans up the bridge but doesn't free the object.
1126 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001127void drm_bridge_cleanup(struct drm_bridge *bridge)
1128{
1129 struct drm_device *dev = bridge->dev;
1130
1131 drm_modeset_lock_all(dev);
1132 drm_mode_object_put(dev, &bridge->base);
1133 list_del(&bridge->head);
1134 dev->mode_config.num_bridge--;
1135 drm_modeset_unlock_all(dev);
1136}
1137EXPORT_SYMBOL(drm_bridge_cleanup);
1138
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001139/**
1140 * drm_encoder_init - Init a preallocated encoder
1141 * @dev: drm device
1142 * @encoder: the encoder to init
1143 * @funcs: callbacks for this encoder
1144 * @encoder_type: user visible type of the encoder
1145 *
1146 * Initialises a preallocated encoder. Encoder should be
1147 * subclassed as part of driver encoder objects.
1148 *
1149 * Returns:
1150 * Zero on success, error code on failure.
1151 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001152int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001153 struct drm_encoder *encoder,
1154 const struct drm_encoder_funcs *funcs,
1155 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001156{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001157 int ret;
1158
Daniel Vetter84849902012-12-02 00:28:11 +01001159 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001160
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001161 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1162 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001163 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001164
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001165 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001166 encoder->encoder_type = encoder_type;
1167 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001168 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1169 drm_encoder_enum_list[encoder_type].name,
1170 encoder->base.id);
1171 if (!encoder->name) {
1172 ret = -ENOMEM;
1173 goto out_put;
1174 }
Dave Airlief453ba02008-11-07 14:05:41 -08001175
1176 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1177 dev->mode_config.num_encoder++;
1178
Jani Nikulae5748942014-05-14 16:58:20 +03001179out_put:
1180 if (ret)
1181 drm_mode_object_put(dev, &encoder->base);
1182
1183out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001184 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001185
1186 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001187}
1188EXPORT_SYMBOL(drm_encoder_init);
1189
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001190/**
1191 * drm_encoder_cleanup - cleans up an initialised encoder
1192 * @encoder: encoder to cleanup
1193 *
1194 * Cleans up the encoder but doesn't free the object.
1195 */
Dave Airlief453ba02008-11-07 14:05:41 -08001196void drm_encoder_cleanup(struct drm_encoder *encoder)
1197{
1198 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001199 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001200 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001201 kfree(encoder->name);
1202 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001203 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001204 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001205 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001206}
1207EXPORT_SYMBOL(drm_encoder_cleanup);
1208
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001209/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001210 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001211 * @dev: DRM device
1212 * @plane: plane object to init
1213 * @possible_crtcs: bitmask of possible CRTCs
1214 * @funcs: callbacks for the new plane
1215 * @formats: array of supported formats (%DRM_FORMAT_*)
1216 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001217 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001218 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001219 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001220 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001221 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001222 * Zero on success, error code on failure.
1223 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001224int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1225 unsigned long possible_crtcs,
1226 const struct drm_plane_funcs *funcs,
1227 const uint32_t *formats, uint32_t format_count,
1228 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001229{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001230 int ret;
1231
Daniel Vetter84849902012-12-02 00:28:11 +01001232 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001233
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001234 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1235 if (ret)
1236 goto out;
1237
Rob Clark4d939142012-05-17 02:23:27 -06001238 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001239 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001240 plane->funcs = funcs;
1241 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1242 GFP_KERNEL);
1243 if (!plane->format_types) {
1244 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1245 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001246 ret = -ENOMEM;
1247 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001248 }
1249
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001250 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001251 plane->format_count = format_count;
1252 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001253 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001254
Matt Roperdc415ff2014-04-01 15:22:36 -07001255 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1256 dev->mode_config.num_total_plane++;
1257 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1258 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001259
Rob Clark9922ab52014-04-01 20:16:57 -04001260 drm_object_attach_property(&plane->base,
1261 dev->mode_config.plane_type_property,
1262 plane->type);
1263
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001264 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001265 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001266
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001267 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001268}
Matt Roperdc415ff2014-04-01 15:22:36 -07001269EXPORT_SYMBOL(drm_universal_plane_init);
1270
1271/**
1272 * drm_plane_init - Initialize a legacy plane
1273 * @dev: DRM device
1274 * @plane: plane object to init
1275 * @possible_crtcs: bitmask of possible CRTCs
1276 * @funcs: callbacks for the new plane
1277 * @formats: array of supported formats (%DRM_FORMAT_*)
1278 * @format_count: number of elements in @formats
1279 * @is_primary: plane type (primary vs overlay)
1280 *
1281 * Legacy API to initialize a DRM plane.
1282 *
1283 * New drivers should call drm_universal_plane_init() instead.
1284 *
1285 * Returns:
1286 * Zero on success, error code on failure.
1287 */
1288int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1289 unsigned long possible_crtcs,
1290 const struct drm_plane_funcs *funcs,
1291 const uint32_t *formats, uint32_t format_count,
1292 bool is_primary)
1293{
1294 enum drm_plane_type type;
1295
1296 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1297 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1298 formats, format_count, type);
1299}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001300EXPORT_SYMBOL(drm_plane_init);
1301
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001302/**
1303 * drm_plane_cleanup - Clean up the core plane usage
1304 * @plane: plane to cleanup
1305 *
1306 * This function cleans up @plane and removes it from the DRM mode setting
1307 * core. Note that the function does *not* free the plane structure itself,
1308 * this is the responsibility of the caller.
1309 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001310void drm_plane_cleanup(struct drm_plane *plane)
1311{
1312 struct drm_device *dev = plane->dev;
1313
Daniel Vetter84849902012-12-02 00:28:11 +01001314 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001315 kfree(plane->format_types);
1316 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001317
1318 BUG_ON(list_empty(&plane->head));
1319
1320 list_del(&plane->head);
1321 dev->mode_config.num_total_plane--;
1322 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1323 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001324 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001325}
1326EXPORT_SYMBOL(drm_plane_cleanup);
1327
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001328/**
1329 * drm_plane_force_disable - Forcibly disable a plane
1330 * @plane: plane to disable
1331 *
1332 * Forces the plane to be disabled.
1333 *
1334 * Used when the plane's current framebuffer is destroyed,
1335 * and when restoring fbdev mode.
1336 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001337void drm_plane_force_disable(struct drm_plane *plane)
1338{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001339 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001340 int ret;
1341
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001342 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001343 return;
1344
1345 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001346 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001347 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001348 return;
1349 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001350 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001351 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001352 plane->fb = NULL;
1353 plane->crtc = NULL;
1354}
1355EXPORT_SYMBOL(drm_plane_force_disable);
1356
Dave Airlief453ba02008-11-07 14:05:41 -08001357static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1358{
1359 struct drm_property *edid;
1360 struct drm_property *dpms;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001361 struct drm_property *dev_path;
Dave Airlief453ba02008-11-07 14:05:41 -08001362
1363 /*
1364 * Standard properties (apply to all connectors)
1365 */
1366 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1367 DRM_MODE_PROP_IMMUTABLE,
1368 "EDID", 0);
1369 dev->mode_config.edid_property = edid;
1370
Sascha Hauer4a67d392012-02-06 10:58:17 +01001371 dpms = drm_property_create_enum(dev, 0,
1372 "DPMS", drm_dpms_enum_list,
1373 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001374 dev->mode_config.dpms_property = dpms;
1375
Dave Airlie43aba7e2014-06-05 14:01:31 +10001376 dev_path = drm_property_create(dev,
1377 DRM_MODE_PROP_BLOB |
1378 DRM_MODE_PROP_IMMUTABLE,
1379 "PATH", 0);
1380 dev->mode_config.path_property = dev_path;
1381
Dave Airlief453ba02008-11-07 14:05:41 -08001382 return 0;
1383}
1384
Rob Clark9922ab52014-04-01 20:16:57 -04001385static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1386{
1387 struct drm_property *type;
1388
1389 /*
1390 * Standard properties (apply to all planes)
1391 */
1392 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1393 "type", drm_plane_type_enum_list,
1394 ARRAY_SIZE(drm_plane_type_enum_list));
1395 dev->mode_config.plane_type_property = type;
1396
1397 return 0;
1398}
1399
Dave Airlief453ba02008-11-07 14:05:41 -08001400/**
1401 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1402 * @dev: DRM device
1403 *
1404 * Called by a driver the first time a DVI-I connector is made.
1405 */
1406int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1407{
1408 struct drm_property *dvi_i_selector;
1409 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001410
1411 if (dev->mode_config.dvi_i_select_subconnector_property)
1412 return 0;
1413
1414 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001415 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001416 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001417 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001418 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001419 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1420
Sascha Hauer4a67d392012-02-06 10:58:17 +01001421 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001422 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001423 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001424 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001425 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1426
1427 return 0;
1428}
1429EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1430
1431/**
1432 * drm_create_tv_properties - create TV specific connector properties
1433 * @dev: DRM device
1434 * @num_modes: number of different TV formats (modes) supported
1435 * @modes: array of pointers to strings containing name of each format
1436 *
1437 * Called by a driver's TV initialization routine, this function creates
1438 * the TV specific connector properties for a given device. Caller is
1439 * responsible for allocating a list of format names and passing them to
1440 * this routine.
1441 */
1442int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1443 char *modes[])
1444{
1445 struct drm_property *tv_selector;
1446 struct drm_property *tv_subconnector;
1447 int i;
1448
1449 if (dev->mode_config.tv_select_subconnector_property)
1450 return 0;
1451
1452 /*
1453 * Basic connector properties
1454 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001455 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001456 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001457 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001458 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001459 dev->mode_config.tv_select_subconnector_property = tv_selector;
1460
1461 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001462 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1463 "subconnector",
1464 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001465 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001466 dev->mode_config.tv_subconnector_property = tv_subconnector;
1467
1468 /*
1469 * Other, TV specific properties: margins & TV modes.
1470 */
1471 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001472 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001473
1474 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001475 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001476
1477 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001478 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001479
1480 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001481 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001482
1483 dev->mode_config.tv_mode_property =
1484 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1485 "mode", num_modes);
1486 for (i = 0; i < num_modes; i++)
1487 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1488 i, modes[i]);
1489
Francisco Jerezb6b79022009-08-02 04:19:20 +02001490 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001491 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001492
1493 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001494 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001495
1496 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001497 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001498
Francisco Jereza75f0232009-08-12 02:30:10 +02001499 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001500 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001501
1502 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001503 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001504
1505 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001506 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001507
Dave Airlief453ba02008-11-07 14:05:41 -08001508 return 0;
1509}
1510EXPORT_SYMBOL(drm_mode_create_tv_properties);
1511
1512/**
1513 * drm_mode_create_scaling_mode_property - create scaling mode property
1514 * @dev: DRM device
1515 *
1516 * Called by a driver the first time it's needed, must be attached to desired
1517 * connectors.
1518 */
1519int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1520{
1521 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001522
1523 if (dev->mode_config.scaling_mode_property)
1524 return 0;
1525
1526 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001527 drm_property_create_enum(dev, 0, "scaling mode",
1528 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001529 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001530
1531 dev->mode_config.scaling_mode_property = scaling_mode;
1532
1533 return 0;
1534}
1535EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1536
1537/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301538 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1539 * @dev: DRM device
1540 *
1541 * Called by a driver the first time it's needed, must be attached to desired
1542 * connectors.
1543 *
1544 * Returns:
1545 * Zero on success, errno on failure.
1546 */
1547int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1548{
1549 if (dev->mode_config.aspect_ratio_property)
1550 return 0;
1551
1552 dev->mode_config.aspect_ratio_property =
1553 drm_property_create_enum(dev, 0, "aspect ratio",
1554 drm_aspect_ratio_enum_list,
1555 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1556
1557 if (dev->mode_config.aspect_ratio_property == NULL)
1558 return -ENOMEM;
1559
1560 return 0;
1561}
1562EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1563
1564/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001565 * drm_mode_create_dirty_property - create dirty property
1566 * @dev: DRM device
1567 *
1568 * Called by a driver the first time it's needed, must be attached to desired
1569 * connectors.
1570 */
1571int drm_mode_create_dirty_info_property(struct drm_device *dev)
1572{
1573 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001574
1575 if (dev->mode_config.dirty_info_property)
1576 return 0;
1577
1578 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001579 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001580 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001581 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001582 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001583 dev->mode_config.dirty_info_property = dirty_info;
1584
1585 return 0;
1586}
1587EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1588
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001589static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001590{
1591 uint32_t total_objects = 0;
1592
1593 total_objects += dev->mode_config.num_crtc;
1594 total_objects += dev->mode_config.num_connector;
1595 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001596 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001597
Dave Airlief453ba02008-11-07 14:05:41 -08001598 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1599 if (!group->id_list)
1600 return -ENOMEM;
1601
1602 group->num_crtcs = 0;
1603 group->num_connectors = 0;
1604 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001605 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001606 return 0;
1607}
1608
Dave Airliead222792014-05-02 13:22:19 +10001609void drm_mode_group_destroy(struct drm_mode_group *group)
1610{
1611 kfree(group->id_list);
1612 group->id_list = NULL;
1613}
1614
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001615/*
1616 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1617 * the drm core's responsibility to set up mode control groups.
1618 */
Dave Airlief453ba02008-11-07 14:05:41 -08001619int drm_mode_group_init_legacy_group(struct drm_device *dev,
1620 struct drm_mode_group *group)
1621{
1622 struct drm_crtc *crtc;
1623 struct drm_encoder *encoder;
1624 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001625 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001626 int ret;
1627
1628 if ((ret = drm_mode_group_init(dev, group)))
1629 return ret;
1630
1631 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1632 group->id_list[group->num_crtcs++] = crtc->base.id;
1633
1634 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1635 group->id_list[group->num_crtcs + group->num_encoders++] =
1636 encoder->base.id;
1637
1638 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1639 group->id_list[group->num_crtcs + group->num_encoders +
1640 group->num_connectors++] = connector->base.id;
1641
Sean Paul3b336ec2013-08-14 16:47:37 -04001642 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1643 group->id_list[group->num_crtcs + group->num_encoders +
1644 group->num_connectors + group->num_bridges++] =
1645 bridge->base.id;
1646
Dave Airlief453ba02008-11-07 14:05:41 -08001647 return 0;
1648}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001649EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001650
Dave Airlie2390cd12014-06-05 14:01:29 +10001651void drm_reinit_primary_mode_group(struct drm_device *dev)
1652{
1653 drm_modeset_lock_all(dev);
1654 drm_mode_group_destroy(&dev->primary->mode_group);
1655 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1656 drm_modeset_unlock_all(dev);
1657}
1658EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1659
Dave Airlief453ba02008-11-07 14:05:41 -08001660/**
Dave Airlief453ba02008-11-07 14:05:41 -08001661 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1662 * @out: drm_mode_modeinfo struct to return to the user
1663 * @in: drm_display_mode to use
1664 *
Dave Airlief453ba02008-11-07 14:05:41 -08001665 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1666 * the user.
1667 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001668static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1669 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001670{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001671 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1672 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1673 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1674 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1675 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1676 "timing values too large for mode info\n");
1677
Dave Airlief453ba02008-11-07 14:05:41 -08001678 out->clock = in->clock;
1679 out->hdisplay = in->hdisplay;
1680 out->hsync_start = in->hsync_start;
1681 out->hsync_end = in->hsync_end;
1682 out->htotal = in->htotal;
1683 out->hskew = in->hskew;
1684 out->vdisplay = in->vdisplay;
1685 out->vsync_start = in->vsync_start;
1686 out->vsync_end = in->vsync_end;
1687 out->vtotal = in->vtotal;
1688 out->vscan = in->vscan;
1689 out->vrefresh = in->vrefresh;
1690 out->flags = in->flags;
1691 out->type = in->type;
1692 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1693 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1694}
1695
1696/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001697 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001698 * @out: drm_display_mode to return to the user
1699 * @in: drm_mode_modeinfo to use
1700 *
Dave Airlief453ba02008-11-07 14:05:41 -08001701 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1702 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001703 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001704 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001705 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001706 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001707static int drm_crtc_convert_umode(struct drm_display_mode *out,
1708 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001709{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001710 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1711 return -ERANGE;
1712
Damien Lespiau5848ad42013-09-27 12:11:50 +01001713 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1714 return -EINVAL;
1715
Dave Airlief453ba02008-11-07 14:05:41 -08001716 out->clock = in->clock;
1717 out->hdisplay = in->hdisplay;
1718 out->hsync_start = in->hsync_start;
1719 out->hsync_end = in->hsync_end;
1720 out->htotal = in->htotal;
1721 out->hskew = in->hskew;
1722 out->vdisplay = in->vdisplay;
1723 out->vsync_start = in->vsync_start;
1724 out->vsync_end = in->vsync_end;
1725 out->vtotal = in->vtotal;
1726 out->vscan = in->vscan;
1727 out->vrefresh = in->vrefresh;
1728 out->flags = in->flags;
1729 out->type = in->type;
1730 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1731 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001732
1733 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001734}
1735
1736/**
1737 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001738 * @dev: drm device for the ioctl
1739 * @data: data pointer for the ioctl
1740 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001741 *
Dave Airlief453ba02008-11-07 14:05:41 -08001742 * Construct a set of configuration description structures and return
1743 * them to the user, including CRTC, connector and framebuffer configuration.
1744 *
1745 * Called by the user via ioctl.
1746 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001747 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001748 * Zero on success, errno on failure.
1749 */
1750int drm_mode_getresources(struct drm_device *dev, void *data,
1751 struct drm_file *file_priv)
1752{
1753 struct drm_mode_card_res *card_res = data;
1754 struct list_head *lh;
1755 struct drm_framebuffer *fb;
1756 struct drm_connector *connector;
1757 struct drm_crtc *crtc;
1758 struct drm_encoder *encoder;
1759 int ret = 0;
1760 int connector_count = 0;
1761 int crtc_count = 0;
1762 int fb_count = 0;
1763 int encoder_count = 0;
1764 int copied = 0, i;
1765 uint32_t __user *fb_id;
1766 uint32_t __user *crtc_id;
1767 uint32_t __user *connector_id;
1768 uint32_t __user *encoder_id;
1769 struct drm_mode_group *mode_group;
1770
Dave Airliefb3b06c2011-02-08 13:55:21 +10001771 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1772 return -EINVAL;
1773
Dave Airlief453ba02008-11-07 14:05:41 -08001774
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001775 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001776 /*
1777 * For the non-control nodes we need to limit the list of resources
1778 * by IDs in the group list for this node
1779 */
1780 list_for_each(lh, &file_priv->fbs)
1781 fb_count++;
1782
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001783 /* handle this in 4 parts */
1784 /* FBs */
1785 if (card_res->count_fbs >= fb_count) {
1786 copied = 0;
1787 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1788 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1789 if (put_user(fb->base.id, fb_id + copied)) {
1790 mutex_unlock(&file_priv->fbs_lock);
1791 return -EFAULT;
1792 }
1793 copied++;
1794 }
1795 }
1796 card_res->count_fbs = fb_count;
1797 mutex_unlock(&file_priv->fbs_lock);
1798
1799 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001800 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001801
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001802 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001803 list_for_each(lh, &dev->mode_config.crtc_list)
1804 crtc_count++;
1805
1806 list_for_each(lh, &dev->mode_config.connector_list)
1807 connector_count++;
1808
1809 list_for_each(lh, &dev->mode_config.encoder_list)
1810 encoder_count++;
1811 } else {
1812
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001813 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001814 crtc_count = mode_group->num_crtcs;
1815 connector_count = mode_group->num_connectors;
1816 encoder_count = mode_group->num_encoders;
1817 }
1818
1819 card_res->max_height = dev->mode_config.max_height;
1820 card_res->min_height = dev->mode_config.min_height;
1821 card_res->max_width = dev->mode_config.max_width;
1822 card_res->min_width = dev->mode_config.min_width;
1823
Dave Airlief453ba02008-11-07 14:05:41 -08001824 /* CRTCs */
1825 if (card_res->count_crtcs >= crtc_count) {
1826 copied = 0;
1827 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001828 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001829 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1830 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001831 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001832 if (put_user(crtc->base.id, crtc_id + copied)) {
1833 ret = -EFAULT;
1834 goto out;
1835 }
1836 copied++;
1837 }
1838 } else {
1839 for (i = 0; i < mode_group->num_crtcs; i++) {
1840 if (put_user(mode_group->id_list[i],
1841 crtc_id + copied)) {
1842 ret = -EFAULT;
1843 goto out;
1844 }
1845 copied++;
1846 }
1847 }
1848 }
1849 card_res->count_crtcs = crtc_count;
1850
1851 /* Encoders */
1852 if (card_res->count_encoders >= encoder_count) {
1853 copied = 0;
1854 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001855 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001856 list_for_each_entry(encoder,
1857 &dev->mode_config.encoder_list,
1858 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001859 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001860 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001861 if (put_user(encoder->base.id, encoder_id +
1862 copied)) {
1863 ret = -EFAULT;
1864 goto out;
1865 }
1866 copied++;
1867 }
1868 } else {
1869 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1870 if (put_user(mode_group->id_list[i],
1871 encoder_id + copied)) {
1872 ret = -EFAULT;
1873 goto out;
1874 }
1875 copied++;
1876 }
1877
1878 }
1879 }
1880 card_res->count_encoders = encoder_count;
1881
1882 /* Connectors */
1883 if (card_res->count_connectors >= connector_count) {
1884 copied = 0;
1885 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001886 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001887 list_for_each_entry(connector,
1888 &dev->mode_config.connector_list,
1889 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001890 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1891 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001892 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001893 if (put_user(connector->base.id,
1894 connector_id + copied)) {
1895 ret = -EFAULT;
1896 goto out;
1897 }
1898 copied++;
1899 }
1900 } else {
1901 int start = mode_group->num_crtcs +
1902 mode_group->num_encoders;
1903 for (i = start; i < start + mode_group->num_connectors; i++) {
1904 if (put_user(mode_group->id_list[i],
1905 connector_id + copied)) {
1906 ret = -EFAULT;
1907 goto out;
1908 }
1909 copied++;
1910 }
1911 }
1912 }
1913 card_res->count_connectors = connector_count;
1914
Jerome Glisse94401062010-07-15 15:43:25 -04001915 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001916 card_res->count_connectors, card_res->count_encoders);
1917
1918out:
Daniel Vetter84849902012-12-02 00:28:11 +01001919 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001920 return ret;
1921}
1922
1923/**
1924 * drm_mode_getcrtc - get CRTC 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 CRTC 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_getcrtc(struct drm_device *dev,
1937 void *data, struct drm_file *file_priv)
1938{
1939 struct drm_mode_crtc *crtc_resp = data;
1940 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001941 int ret = 0;
1942
Dave Airliefb3b06c2011-02-08 13:55:21 +10001943 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1944 return -EINVAL;
1945
Daniel Vetter84849902012-12-02 00:28:11 +01001946 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001947
Rob Clarka2b34e22013-10-05 16:36:52 -04001948 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1949 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001950 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001951 goto out;
1952 }
Dave Airlief453ba02008-11-07 14:05:41 -08001953
1954 crtc_resp->x = crtc->x;
1955 crtc_resp->y = crtc->y;
1956 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001957 if (crtc->primary->fb)
1958 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001959 else
1960 crtc_resp->fb_id = 0;
1961
1962 if (crtc->enabled) {
1963
1964 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1965 crtc_resp->mode_valid = 1;
1966
1967 } else {
1968 crtc_resp->mode_valid = 0;
1969 }
1970
1971out:
Daniel Vetter84849902012-12-02 00:28:11 +01001972 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001973 return ret;
1974}
1975
Damien Lespiau61d8e322013-09-25 16:45:22 +01001976static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1977 const struct drm_file *file_priv)
1978{
1979 /*
1980 * If user-space hasn't configured the driver to expose the stereo 3D
1981 * modes, don't expose them.
1982 */
1983 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1984 return false;
1985
1986 return true;
1987}
1988
Dave Airlief453ba02008-11-07 14:05:41 -08001989/**
1990 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001991 * @dev: drm device for the ioctl
1992 * @data: data pointer for the ioctl
1993 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001994 *
Dave Airlief453ba02008-11-07 14:05:41 -08001995 * Construct a connector configuration structure to return to the user.
1996 *
1997 * Called by the user via ioctl.
1998 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001999 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002000 * Zero on success, errno on failure.
2001 */
2002int drm_mode_getconnector(struct drm_device *dev, void *data,
2003 struct drm_file *file_priv)
2004{
2005 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002006 struct drm_connector *connector;
2007 struct drm_display_mode *mode;
2008 int mode_count = 0;
2009 int props_count = 0;
2010 int encoders_count = 0;
2011 int ret = 0;
2012 int copied = 0;
2013 int i;
2014 struct drm_mode_modeinfo u_mode;
2015 struct drm_mode_modeinfo __user *mode_ptr;
2016 uint32_t __user *prop_ptr;
2017 uint64_t __user *prop_values;
2018 uint32_t __user *encoder_ptr;
2019
Dave Airliefb3b06c2011-02-08 13:55:21 +10002020 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2021 return -EINVAL;
2022
Dave Airlief453ba02008-11-07 14:05:41 -08002023 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2024
Jerome Glisse94401062010-07-15 15:43:25 -04002025 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002026
Daniel Vetter7b240562012-12-12 00:35:33 +01002027 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002028
Rob Clarka2b34e22013-10-05 16:36:52 -04002029 connector = drm_connector_find(dev, out_resp->connector_id);
2030 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002031 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002032 goto out;
2033 }
Dave Airlief453ba02008-11-07 14:05:41 -08002034
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002035 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08002036
2037 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2038 if (connector->encoder_ids[i] != 0) {
2039 encoders_count++;
2040 }
2041 }
2042
2043 if (out_resp->count_modes == 0) {
2044 connector->funcs->fill_modes(connector,
2045 dev->mode_config.max_width,
2046 dev->mode_config.max_height);
2047 }
2048
2049 /* delayed so we get modes regardless of pre-fill_modes state */
2050 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002051 if (drm_mode_expose_to_userspace(mode, file_priv))
2052 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002053
2054 out_resp->connector_id = connector->base.id;
2055 out_resp->connector_type = connector->connector_type;
2056 out_resp->connector_type_id = connector->connector_type_id;
2057 out_resp->mm_width = connector->display_info.width_mm;
2058 out_resp->mm_height = connector->display_info.height_mm;
2059 out_resp->subpixel = connector->display_info.subpixel_order;
2060 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02002061 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002062 if (connector->encoder)
2063 out_resp->encoder_id = connector->encoder->base.id;
2064 else
2065 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02002066 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002067
2068 /*
2069 * This ioctl is called twice, once to determine how much space is
2070 * needed, and the 2nd time to fill it.
2071 */
2072 if ((out_resp->count_modes >= mode_count) && mode_count) {
2073 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002074 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002075 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002076 if (!drm_mode_expose_to_userspace(mode, file_priv))
2077 continue;
2078
Dave Airlief453ba02008-11-07 14:05:41 -08002079 drm_crtc_convert_to_umode(&u_mode, mode);
2080 if (copy_to_user(mode_ptr + copied,
2081 &u_mode, sizeof(u_mode))) {
2082 ret = -EFAULT;
2083 goto out;
2084 }
2085 copied++;
2086 }
2087 }
2088 out_resp->count_modes = mode_count;
2089
2090 if ((out_resp->count_props >= props_count) && props_count) {
2091 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002092 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2093 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002094 for (i = 0; i < connector->properties.count; i++) {
2095 if (put_user(connector->properties.ids[i],
2096 prop_ptr + copied)) {
2097 ret = -EFAULT;
2098 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002099 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002100
2101 if (put_user(connector->properties.values[i],
2102 prop_values + copied)) {
2103 ret = -EFAULT;
2104 goto out;
2105 }
2106 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002107 }
2108 }
2109 out_resp->count_props = props_count;
2110
2111 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2112 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002113 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002114 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2115 if (connector->encoder_ids[i] != 0) {
2116 if (put_user(connector->encoder_ids[i],
2117 encoder_ptr + copied)) {
2118 ret = -EFAULT;
2119 goto out;
2120 }
2121 copied++;
2122 }
2123 }
2124 }
2125 out_resp->count_encoders = encoders_count;
2126
2127out:
Daniel Vetter7b240562012-12-12 00:35:33 +01002128 mutex_unlock(&dev->mode_config.mutex);
2129
Dave Airlief453ba02008-11-07 14:05:41 -08002130 return ret;
2131}
2132
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002133/**
2134 * drm_mode_getencoder - get encoder configuration
2135 * @dev: drm device for the ioctl
2136 * @data: data pointer for the ioctl
2137 * @file_priv: drm file for the ioctl call
2138 *
2139 * Construct a encoder configuration structure to return to the user.
2140 *
2141 * Called by the user via ioctl.
2142 *
2143 * Returns:
2144 * Zero on success, errno on failure.
2145 */
Dave Airlief453ba02008-11-07 14:05:41 -08002146int drm_mode_getencoder(struct drm_device *dev, void *data,
2147 struct drm_file *file_priv)
2148{
2149 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002150 struct drm_encoder *encoder;
2151 int ret = 0;
2152
Dave Airliefb3b06c2011-02-08 13:55:21 +10002153 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2154 return -EINVAL;
2155
Daniel Vetter84849902012-12-02 00:28:11 +01002156 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002157 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2158 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002159 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002160 goto out;
2161 }
Dave Airlief453ba02008-11-07 14:05:41 -08002162
2163 if (encoder->crtc)
2164 enc_resp->crtc_id = encoder->crtc->base.id;
2165 else
2166 enc_resp->crtc_id = 0;
2167 enc_resp->encoder_type = encoder->encoder_type;
2168 enc_resp->encoder_id = encoder->base.id;
2169 enc_resp->possible_crtcs = encoder->possible_crtcs;
2170 enc_resp->possible_clones = encoder->possible_clones;
2171
2172out:
Daniel Vetter84849902012-12-02 00:28:11 +01002173 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002174 return ret;
2175}
2176
2177/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002178 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002179 * @dev: DRM device
2180 * @data: ioctl data
2181 * @file_priv: DRM file info
2182 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002183 * Construct a list of plane ids to return to the user.
2184 *
2185 * Called by the user via ioctl.
2186 *
2187 * Returns:
2188 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002189 */
2190int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002191 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002192{
2193 struct drm_mode_get_plane_res *plane_resp = data;
2194 struct drm_mode_config *config;
2195 struct drm_plane *plane;
2196 uint32_t __user *plane_ptr;
2197 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002198 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002199
2200 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2201 return -EINVAL;
2202
Daniel Vetter84849902012-12-02 00:28:11 +01002203 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002204 config = &dev->mode_config;
2205
Matt Roper681e7ec2014-04-01 15:22:42 -07002206 if (file_priv->universal_planes)
2207 num_planes = config->num_total_plane;
2208 else
2209 num_planes = config->num_overlay_plane;
2210
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002211 /*
2212 * This ioctl is called twice, once to determine how much space is
2213 * needed, and the 2nd time to fill it.
2214 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002215 if (num_planes &&
2216 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002217 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002218
2219 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002220 /*
2221 * Unless userspace set the 'universal planes'
2222 * capability bit, only advertise overlays.
2223 */
2224 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2225 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002226 continue;
2227
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002228 if (put_user(plane->base.id, plane_ptr + copied)) {
2229 ret = -EFAULT;
2230 goto out;
2231 }
2232 copied++;
2233 }
2234 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002235 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002236
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
2242/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002243 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002244 * @dev: DRM device
2245 * @data: ioctl data
2246 * @file_priv: DRM file info
2247 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002248 * Construct a plane configuration structure to return to the user.
2249 *
2250 * Called by the user via ioctl.
2251 *
2252 * Returns:
2253 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002254 */
2255int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002256 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002257{
2258 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002259 struct drm_plane *plane;
2260 uint32_t __user *format_ptr;
2261 int ret = 0;
2262
2263 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2264 return -EINVAL;
2265
Daniel Vetter84849902012-12-02 00:28:11 +01002266 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002267 plane = drm_plane_find(dev, plane_resp->plane_id);
2268 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002269 ret = -ENOENT;
2270 goto out;
2271 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002272
2273 if (plane->crtc)
2274 plane_resp->crtc_id = plane->crtc->base.id;
2275 else
2276 plane_resp->crtc_id = 0;
2277
2278 if (plane->fb)
2279 plane_resp->fb_id = plane->fb->base.id;
2280 else
2281 plane_resp->fb_id = 0;
2282
2283 plane_resp->plane_id = plane->base.id;
2284 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002285 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002286
2287 /*
2288 * This ioctl is called twice, once to determine how much space is
2289 * needed, and the 2nd time to fill it.
2290 */
2291 if (plane->format_count &&
2292 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002293 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002294 if (copy_to_user(format_ptr,
2295 plane->format_types,
2296 sizeof(uint32_t) * plane->format_count)) {
2297 ret = -EFAULT;
2298 goto out;
2299 }
2300 }
2301 plane_resp->count_format_types = plane->format_count;
2302
2303out:
Daniel Vetter84849902012-12-02 00:28:11 +01002304 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002305 return ret;
2306}
2307
Matt Roperb36552b2014-06-10 08:28:09 -07002308/*
2309 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002310 *
Matt Roperb36552b2014-06-10 08:28:09 -07002311 * Note that we assume an extra reference has already been taken on fb. If the
2312 * update fails, this reference will be dropped before return; if it succeeds,
2313 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002314 *
Matt Roperb36552b2014-06-10 08:28:09 -07002315 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002316 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002317static int setplane_internal(struct drm_plane *plane,
2318 struct drm_crtc *crtc,
Matt Roperb36552b2014-06-10 08:28:09 -07002319 struct drm_framebuffer *fb,
2320 int32_t crtc_x, int32_t crtc_y,
2321 uint32_t crtc_w, uint32_t crtc_h,
2322 /* src_{x,y,w,h} values are 16.16 fixed point */
2323 uint32_t src_x, uint32_t src_y,
2324 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325{
Chris Wilson17cfd912014-06-13 15:22:28 +01002326 struct drm_device *dev = plane->dev;
Matt Roperb36552b2014-06-10 08:28:09 -07002327 struct drm_framebuffer *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002328 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002329 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002330 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002331
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002332 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002333 if (!fb) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002334 drm_modeset_lock_all(dev);
2335 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002336 ret = plane->funcs->disable_plane(plane);
2337 if (!ret) {
2338 plane->crtc = NULL;
2339 plane->fb = NULL;
2340 } else {
2341 old_fb = NULL;
2342 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002343 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002344 goto out;
2345 }
2346
Matt Roper7f994f32014-05-29 08:06:51 -07002347 /* Check whether this plane is usable on this CRTC */
2348 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2349 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2350 ret = -EINVAL;
2351 goto out;
2352 }
2353
Ville Syrjälä62443be2011-12-20 00:06:47 +02002354 /* Check whether this plane supports the fb pixel format. */
2355 for (i = 0; i < plane->format_count; i++)
2356 if (fb->pixel_format == plane->format_types[i])
2357 break;
2358 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002359 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2360 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002361 ret = -EINVAL;
2362 goto out;
2363 }
2364
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002365 fb_width = fb->width << 16;
2366 fb_height = fb->height << 16;
2367
2368 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002369 if (src_w > fb_width ||
2370 src_x > fb_width - src_w ||
2371 src_h > fb_height ||
2372 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002373 DRM_DEBUG_KMS("Invalid source coordinates "
2374 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002375 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2376 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2377 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2378 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002379 ret = -ENOSPC;
2380 goto out;
2381 }
2382
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002383 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002384 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002385 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002386 crtc_x, crtc_y, crtc_w, crtc_h,
2387 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002388 if (!ret) {
2389 plane->crtc = crtc;
2390 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002391 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002392 } else {
2393 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002394 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002395 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002396
2397out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002398 if (fb)
2399 drm_framebuffer_unreference(fb);
2400 if (old_fb)
2401 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002402
2403 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002404
2405}
2406
2407/**
2408 * drm_mode_setplane - configure a plane's configuration
2409 * @dev: DRM device
2410 * @data: ioctl data*
2411 * @file_priv: DRM file info
2412 *
2413 * Set plane configuration, including placement, fb, scaling, and other factors.
2414 * Or pass a NULL fb to disable (planes may be disabled without providing a
2415 * valid crtc).
2416 *
2417 * Returns:
2418 * Zero on success, errno on failure.
2419 */
2420int drm_mode_setplane(struct drm_device *dev, void *data,
2421 struct drm_file *file_priv)
2422{
2423 struct drm_mode_set_plane *plane_req = data;
2424 struct drm_mode_object *obj;
2425 struct drm_plane *plane;
2426 struct drm_crtc *crtc = NULL;
2427 struct drm_framebuffer *fb = NULL;
2428
2429 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2430 return -EINVAL;
2431
2432 /* Give drivers some help against integer overflows */
2433 if (plane_req->crtc_w > INT_MAX ||
2434 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2435 plane_req->crtc_h > INT_MAX ||
2436 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2437 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2438 plane_req->crtc_w, plane_req->crtc_h,
2439 plane_req->crtc_x, plane_req->crtc_y);
2440 return -ERANGE;
2441 }
2442
2443 /*
2444 * First, find the plane, crtc, and fb objects. If not available,
2445 * we don't bother to call the driver.
2446 */
2447 obj = drm_mode_object_find(dev, plane_req->plane_id,
2448 DRM_MODE_OBJECT_PLANE);
2449 if (!obj) {
2450 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2451 plane_req->plane_id);
2452 return -ENOENT;
2453 }
2454 plane = obj_to_plane(obj);
2455
2456 if (plane_req->fb_id) {
2457 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2458 if (!fb) {
2459 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2460 plane_req->fb_id);
2461 return -ENOENT;
2462 }
2463
2464 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2465 DRM_MODE_OBJECT_CRTC);
2466 if (!obj) {
2467 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2468 plane_req->crtc_id);
2469 return -ENOENT;
2470 }
2471 crtc = obj_to_crtc(obj);
2472 }
2473
Matt Roper161d0dc2014-06-10 08:28:10 -07002474 /*
2475 * setplane_internal will take care of deref'ing either the old or new
2476 * framebuffer depending on success.
2477 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002478 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002479 plane_req->crtc_x, plane_req->crtc_y,
2480 plane_req->crtc_w, plane_req->crtc_h,
2481 plane_req->src_x, plane_req->src_y,
2482 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002483}
2484
2485/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002486 * drm_mode_set_config_internal - helper to call ->set_config
2487 * @set: modeset config to set
2488 *
2489 * This is a little helper to wrap internal calls to the ->set_config driver
2490 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002491 *
2492 * Returns:
2493 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002494 */
2495int drm_mode_set_config_internal(struct drm_mode_set *set)
2496{
2497 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002498 struct drm_framebuffer *fb;
2499 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002500 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002501
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002502 /*
2503 * NOTE: ->set_config can also disable other crtcs (if we steal all
2504 * connectors from it), hence we need to refcount the fbs across all
2505 * crtcs. Atomic modeset will have saner semantics ...
2506 */
2507 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002508 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002509
Daniel Vetterb0d12322012-12-11 01:07:12 +01002510 fb = set->fb;
2511
2512 ret = crtc->funcs->set_config(set);
2513 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002514 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002515 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002516 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002517
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002518 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002519 if (tmp->primary->fb)
2520 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002521 if (tmp->old_fb)
2522 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002523 }
2524
2525 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002526}
2527EXPORT_SYMBOL(drm_mode_set_config_internal);
2528
Matt Roperaf936292014-04-01 15:22:34 -07002529/**
2530 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2531 * CRTC viewport
2532 * @crtc: CRTC that framebuffer will be displayed on
2533 * @x: x panning
2534 * @y: y panning
2535 * @mode: mode that framebuffer will be displayed under
2536 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002537 */
Matt Roperaf936292014-04-01 15:22:34 -07002538int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2539 int x, int y,
2540 const struct drm_display_mode *mode,
2541 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002542
2543{
2544 int hdisplay, vdisplay;
2545
2546 hdisplay = mode->hdisplay;
2547 vdisplay = mode->vdisplay;
2548
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002549 if (drm_mode_is_stereo(mode)) {
2550 struct drm_display_mode adjusted = *mode;
2551
2552 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2553 hdisplay = adjusted.crtc_hdisplay;
2554 vdisplay = adjusted.crtc_vdisplay;
2555 }
2556
Damien Lespiauc11e9282013-09-25 16:45:30 +01002557 if (crtc->invert_dimensions)
2558 swap(hdisplay, vdisplay);
2559
2560 if (hdisplay > fb->width ||
2561 vdisplay > fb->height ||
2562 x > fb->width - hdisplay ||
2563 y > fb->height - vdisplay) {
2564 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2565 fb->width, fb->height, hdisplay, vdisplay, x, y,
2566 crtc->invert_dimensions ? " (inverted)" : "");
2567 return -ENOSPC;
2568 }
2569
2570 return 0;
2571}
Matt Roperaf936292014-04-01 15:22:34 -07002572EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002573
Daniel Vetter2d13b672012-12-11 13:47:23 +01002574/**
Dave Airlief453ba02008-11-07 14:05:41 -08002575 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002576 * @dev: drm device for the ioctl
2577 * @data: data pointer for the ioctl
2578 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002579 *
Dave Airlief453ba02008-11-07 14:05:41 -08002580 * Build a new CRTC configuration based on user request.
2581 *
2582 * Called by the user via ioctl.
2583 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002584 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002585 * Zero on success, errno on failure.
2586 */
2587int drm_mode_setcrtc(struct drm_device *dev, void *data,
2588 struct drm_file *file_priv)
2589{
2590 struct drm_mode_config *config = &dev->mode_config;
2591 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002592 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002593 struct drm_connector **connector_set = NULL, *connector;
2594 struct drm_framebuffer *fb = NULL;
2595 struct drm_display_mode *mode = NULL;
2596 struct drm_mode_set set;
2597 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002598 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002599 int i;
2600
Dave Airliefb3b06c2011-02-08 13:55:21 +10002601 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2602 return -EINVAL;
2603
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002604 /* For some reason crtc x/y offsets are signed internally. */
2605 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2606 return -ERANGE;
2607
Daniel Vetter84849902012-12-02 00:28:11 +01002608 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002609 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2610 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002611 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002612 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002613 goto out;
2614 }
Jerome Glisse94401062010-07-15 15:43:25 -04002615 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002616
2617 if (crtc_req->mode_valid) {
2618 /* If we have a mode we need a framebuffer. */
2619 /* If we pass -1, set the mode with the currently bound fb */
2620 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002621 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002622 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2623 ret = -EINVAL;
2624 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002625 }
Matt Roperf4510a22014-04-01 15:22:40 -07002626 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002627 /* Make refcounting symmetric with the lookup path. */
2628 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002629 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002630 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2631 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002632 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2633 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002634 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002635 goto out;
2636 }
Dave Airlief453ba02008-11-07 14:05:41 -08002637 }
2638
2639 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002640 if (!mode) {
2641 ret = -ENOMEM;
2642 goto out;
2643 }
2644
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002645 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2646 if (ret) {
2647 DRM_DEBUG_KMS("Invalid mode\n");
2648 goto out;
2649 }
2650
Dave Airlief453ba02008-11-07 14:05:41 -08002651 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002652
Damien Lespiauc11e9282013-09-25 16:45:30 +01002653 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2654 mode, fb);
2655 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002656 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002657
Dave Airlief453ba02008-11-07 14:05:41 -08002658 }
2659
2660 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002661 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002662 ret = -EINVAL;
2663 goto out;
2664 }
2665
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002666 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002667 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002668 crtc_req->count_connectors);
2669 ret = -EINVAL;
2670 goto out;
2671 }
2672
2673 if (crtc_req->count_connectors > 0) {
2674 u32 out_id;
2675
2676 /* Avoid unbounded kernel memory allocation */
2677 if (crtc_req->count_connectors > config->num_connector) {
2678 ret = -EINVAL;
2679 goto out;
2680 }
2681
2682 connector_set = kmalloc(crtc_req->count_connectors *
2683 sizeof(struct drm_connector *),
2684 GFP_KERNEL);
2685 if (!connector_set) {
2686 ret = -ENOMEM;
2687 goto out;
2688 }
2689
2690 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002691 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002692 if (get_user(out_id, &set_connectors_ptr[i])) {
2693 ret = -EFAULT;
2694 goto out;
2695 }
2696
Rob Clarka2b34e22013-10-05 16:36:52 -04002697 connector = drm_connector_find(dev, out_id);
2698 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002699 DRM_DEBUG_KMS("Connector id %d unknown\n",
2700 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002701 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002702 goto out;
2703 }
Jerome Glisse94401062010-07-15 15:43:25 -04002704 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2705 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002706 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002707
2708 connector_set[i] = connector;
2709 }
2710 }
2711
2712 set.crtc = crtc;
2713 set.x = crtc_req->x;
2714 set.y = crtc_req->y;
2715 set.mode = mode;
2716 set.connectors = connector_set;
2717 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002718 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002719 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002720
2721out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002722 if (fb)
2723 drm_framebuffer_unreference(fb);
2724
Dave Airlief453ba02008-11-07 14:05:41 -08002725 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002726 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002727 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002728 return ret;
2729}
2730
Matt Roper161d0dc2014-06-10 08:28:10 -07002731/**
2732 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2733 * universal plane handler call
2734 * @crtc: crtc to update cursor for
2735 * @req: data pointer for the ioctl
2736 * @file_priv: drm file for the ioctl call
2737 *
2738 * Legacy cursor ioctl's work directly with driver buffer handles. To
2739 * translate legacy ioctl calls into universal plane handler calls, we need to
2740 * wrap the native buffer handle in a drm_framebuffer.
2741 *
2742 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2743 * buffer with a pitch of 4*width; the universal plane interface should be used
2744 * directly in cases where the hardware can support other buffer settings and
2745 * userspace wants to make use of these capabilities.
2746 *
2747 * Returns:
2748 * Zero on success, errno on failure.
2749 */
2750static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2751 struct drm_mode_cursor2 *req,
2752 struct drm_file *file_priv)
2753{
2754 struct drm_device *dev = crtc->dev;
2755 struct drm_framebuffer *fb = NULL;
2756 struct drm_mode_fb_cmd2 fbreq = {
2757 .width = req->width,
2758 .height = req->height,
2759 .pixel_format = DRM_FORMAT_ARGB8888,
2760 .pitches = { req->width * 4 },
2761 .handles = { req->handle },
2762 };
2763 int32_t crtc_x, crtc_y;
2764 uint32_t crtc_w = 0, crtc_h = 0;
2765 uint32_t src_w = 0, src_h = 0;
2766 int ret = 0;
2767
2768 BUG_ON(!crtc->cursor);
2769
2770 /*
2771 * Obtain fb we'll be using (either new or existing) and take an extra
2772 * reference to it if fb != null. setplane will take care of dropping
2773 * the reference if the plane update fails.
2774 */
2775 if (req->flags & DRM_MODE_CURSOR_BO) {
2776 if (req->handle) {
2777 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2778 if (IS_ERR(fb)) {
2779 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2780 return PTR_ERR(fb);
2781 }
2782
2783 drm_framebuffer_reference(fb);
2784 } else {
2785 fb = NULL;
2786 }
2787 } else {
2788 mutex_lock(&dev->mode_config.mutex);
2789 fb = crtc->cursor->fb;
2790 if (fb)
2791 drm_framebuffer_reference(fb);
2792 mutex_unlock(&dev->mode_config.mutex);
2793 }
2794
2795 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2796 crtc_x = req->x;
2797 crtc_y = req->y;
2798 } else {
2799 crtc_x = crtc->cursor_x;
2800 crtc_y = crtc->cursor_y;
2801 }
2802
2803 if (fb) {
2804 crtc_w = fb->width;
2805 crtc_h = fb->height;
2806 src_w = fb->width << 16;
2807 src_h = fb->height << 16;
2808 }
2809
2810 /*
2811 * setplane_internal will take care of deref'ing either the old or new
2812 * framebuffer depending on success.
2813 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002814 ret = setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002815 crtc_x, crtc_y, crtc_w, crtc_h,
2816 0, 0, src_w, src_h);
2817
2818 /* Update successful; save new cursor position, if necessary */
2819 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2820 crtc->cursor_x = req->x;
2821 crtc->cursor_y = req->y;
2822 }
2823
2824 return ret;
2825}
2826
Dave Airlie4c813d42013-06-20 11:48:52 +10002827static int drm_mode_cursor_common(struct drm_device *dev,
2828 struct drm_mode_cursor2 *req,
2829 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002830{
Dave Airlief453ba02008-11-07 14:05:41 -08002831 struct drm_crtc *crtc;
2832 int ret = 0;
2833
Dave Airliefb3b06c2011-02-08 13:55:21 +10002834 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2835 return -EINVAL;
2836
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002837 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002838 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002839
Rob Clarka2b34e22013-10-05 16:36:52 -04002840 crtc = drm_crtc_find(dev, req->crtc_id);
2841 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002842 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002843 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002844 }
Dave Airlief453ba02008-11-07 14:05:41 -08002845
Matt Roper161d0dc2014-06-10 08:28:10 -07002846 /*
2847 * If this crtc has a universal cursor plane, call that plane's update
2848 * handler rather than using legacy cursor handlers.
2849 */
2850 if (crtc->cursor)
2851 return drm_mode_cursor_universal(crtc, req, file_priv);
2852
Rob Clark51fd3712013-11-19 12:10:12 -05002853 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002854 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002855 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002856 ret = -ENXIO;
2857 goto out;
2858 }
2859 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002860 if (crtc->funcs->cursor_set2)
2861 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2862 req->width, req->height, req->hot_x, req->hot_y);
2863 else
2864 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2865 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002866 }
2867
2868 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2869 if (crtc->funcs->cursor_move) {
2870 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2871 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002872 ret = -EFAULT;
2873 goto out;
2874 }
2875 }
2876out:
Rob Clark51fd3712013-11-19 12:10:12 -05002877 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002878
Dave Airlief453ba02008-11-07 14:05:41 -08002879 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002880
2881}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002882
2883
2884/**
2885 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2886 * @dev: drm device for the ioctl
2887 * @data: data pointer for the ioctl
2888 * @file_priv: drm file for the ioctl call
2889 *
2890 * Set the cursor configuration based on user request.
2891 *
2892 * Called by the user via ioctl.
2893 *
2894 * Returns:
2895 * Zero on success, errno on failure.
2896 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002897int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002898 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002899{
2900 struct drm_mode_cursor *req = data;
2901 struct drm_mode_cursor2 new_req;
2902
2903 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2904 new_req.hot_x = new_req.hot_y = 0;
2905
2906 return drm_mode_cursor_common(dev, &new_req, file_priv);
2907}
2908
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002909/**
2910 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2911 * @dev: drm device for the ioctl
2912 * @data: data pointer for the ioctl
2913 * @file_priv: drm file for the ioctl call
2914 *
2915 * Set the cursor configuration based on user request. This implements the 2nd
2916 * version of the cursor ioctl, which allows userspace to additionally specify
2917 * the hotspot of the pointer.
2918 *
2919 * Called by the user via ioctl.
2920 *
2921 * Returns:
2922 * Zero on success, errno on failure.
2923 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002924int drm_mode_cursor2_ioctl(struct drm_device *dev,
2925 void *data, struct drm_file *file_priv)
2926{
2927 struct drm_mode_cursor2 *req = data;
2928 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002929}
2930
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002931/**
2932 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2933 * @bpp: bits per pixels
2934 * @depth: bit depth per pixel
2935 *
2936 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2937 * Useful in fbdev emulation code, since that deals in those values.
2938 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002939uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2940{
2941 uint32_t fmt;
2942
2943 switch (bpp) {
2944 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002945 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002946 break;
2947 case 16:
2948 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002949 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002950 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002951 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002952 break;
2953 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002954 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002955 break;
2956 case 32:
2957 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002958 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002959 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002960 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002961 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002962 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002963 break;
2964 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002965 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2966 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002967 break;
2968 }
2969
2970 return fmt;
2971}
2972EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2973
Dave Airlief453ba02008-11-07 14:05:41 -08002974/**
2975 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002976 * @dev: drm device for the ioctl
2977 * @data: data pointer for the ioctl
2978 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002979 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002980 * Add a new FB to the specified CRTC, given a user request. This is the
2981 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002982 *
2983 * Called by the user via ioctl.
2984 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002985 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002986 * Zero on success, errno on failure.
2987 */
2988int drm_mode_addfb(struct drm_device *dev,
2989 void *data, struct drm_file *file_priv)
2990{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002991 struct drm_mode_fb_cmd *or = data;
2992 struct drm_mode_fb_cmd2 r = {};
2993 struct drm_mode_config *config = &dev->mode_config;
2994 struct drm_framebuffer *fb;
2995 int ret = 0;
2996
2997 /* Use new struct with format internally */
2998 r.fb_id = or->fb_id;
2999 r.width = or->width;
3000 r.height = or->height;
3001 r.pitches[0] = or->pitch;
3002 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3003 r.handles[0] = or->handle;
3004
3005 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3006 return -EINVAL;
3007
Jesse Barnesacb4b992011-11-07 12:03:23 -08003008 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003009 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08003010
3011 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003012 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003013
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003014 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
3015 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003016 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003017 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003018 }
3019
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003020 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003021 or->fb_id = fb->base.id;
3022 list_add(&fb->filp_head, &file_priv->fbs);
3023 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003024 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003025
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003026 return ret;
3027}
3028
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003029static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003030{
3031 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3032
3033 switch (format) {
3034 case DRM_FORMAT_C8:
3035 case DRM_FORMAT_RGB332:
3036 case DRM_FORMAT_BGR233:
3037 case DRM_FORMAT_XRGB4444:
3038 case DRM_FORMAT_XBGR4444:
3039 case DRM_FORMAT_RGBX4444:
3040 case DRM_FORMAT_BGRX4444:
3041 case DRM_FORMAT_ARGB4444:
3042 case DRM_FORMAT_ABGR4444:
3043 case DRM_FORMAT_RGBA4444:
3044 case DRM_FORMAT_BGRA4444:
3045 case DRM_FORMAT_XRGB1555:
3046 case DRM_FORMAT_XBGR1555:
3047 case DRM_FORMAT_RGBX5551:
3048 case DRM_FORMAT_BGRX5551:
3049 case DRM_FORMAT_ARGB1555:
3050 case DRM_FORMAT_ABGR1555:
3051 case DRM_FORMAT_RGBA5551:
3052 case DRM_FORMAT_BGRA5551:
3053 case DRM_FORMAT_RGB565:
3054 case DRM_FORMAT_BGR565:
3055 case DRM_FORMAT_RGB888:
3056 case DRM_FORMAT_BGR888:
3057 case DRM_FORMAT_XRGB8888:
3058 case DRM_FORMAT_XBGR8888:
3059 case DRM_FORMAT_RGBX8888:
3060 case DRM_FORMAT_BGRX8888:
3061 case DRM_FORMAT_ARGB8888:
3062 case DRM_FORMAT_ABGR8888:
3063 case DRM_FORMAT_RGBA8888:
3064 case DRM_FORMAT_BGRA8888:
3065 case DRM_FORMAT_XRGB2101010:
3066 case DRM_FORMAT_XBGR2101010:
3067 case DRM_FORMAT_RGBX1010102:
3068 case DRM_FORMAT_BGRX1010102:
3069 case DRM_FORMAT_ARGB2101010:
3070 case DRM_FORMAT_ABGR2101010:
3071 case DRM_FORMAT_RGBA1010102:
3072 case DRM_FORMAT_BGRA1010102:
3073 case DRM_FORMAT_YUYV:
3074 case DRM_FORMAT_YVYU:
3075 case DRM_FORMAT_UYVY:
3076 case DRM_FORMAT_VYUY:
3077 case DRM_FORMAT_AYUV:
3078 case DRM_FORMAT_NV12:
3079 case DRM_FORMAT_NV21:
3080 case DRM_FORMAT_NV16:
3081 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003082 case DRM_FORMAT_NV24:
3083 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003084 case DRM_FORMAT_YUV410:
3085 case DRM_FORMAT_YVU410:
3086 case DRM_FORMAT_YUV411:
3087 case DRM_FORMAT_YVU411:
3088 case DRM_FORMAT_YUV420:
3089 case DRM_FORMAT_YVU420:
3090 case DRM_FORMAT_YUV422:
3091 case DRM_FORMAT_YVU422:
3092 case DRM_FORMAT_YUV444:
3093 case DRM_FORMAT_YVU444:
3094 return 0;
3095 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003096 DRM_DEBUG_KMS("invalid pixel format %s\n",
3097 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003098 return -EINVAL;
3099 }
3100}
3101
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003102static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003103{
3104 int ret, hsub, vsub, num_planes, i;
3105
3106 ret = format_check(r);
3107 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003108 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3109 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003110 return ret;
3111 }
3112
3113 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3114 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3115 num_planes = drm_format_num_planes(r->pixel_format);
3116
3117 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003118 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003119 return -EINVAL;
3120 }
3121
3122 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003123 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003124 return -EINVAL;
3125 }
3126
3127 for (i = 0; i < num_planes; i++) {
3128 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003129 unsigned int height = r->height / (i != 0 ? vsub : 1);
3130 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003131
3132 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003133 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003134 return -EINVAL;
3135 }
3136
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003137 if ((uint64_t) width * cpp > UINT_MAX)
3138 return -ERANGE;
3139
3140 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3141 return -ERANGE;
3142
3143 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003144 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003145 return -EINVAL;
3146 }
3147 }
3148
3149 return 0;
3150}
3151
Matt Roperc394c2b2014-06-10 08:28:08 -07003152static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3153 struct drm_mode_fb_cmd2 *r,
3154 struct drm_file *file_priv)
3155{
3156 struct drm_mode_config *config = &dev->mode_config;
3157 struct drm_framebuffer *fb;
3158 int ret;
3159
3160 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3161 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3162 return ERR_PTR(-EINVAL);
3163 }
3164
3165 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3166 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3167 r->width, config->min_width, config->max_width);
3168 return ERR_PTR(-EINVAL);
3169 }
3170 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3171 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3172 r->height, config->min_height, config->max_height);
3173 return ERR_PTR(-EINVAL);
3174 }
3175
3176 ret = framebuffer_check(r);
3177 if (ret)
3178 return ERR_PTR(ret);
3179
3180 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3181 if (IS_ERR(fb)) {
3182 DRM_DEBUG_KMS("could not create framebuffer\n");
3183 return fb;
3184 }
3185
3186 mutex_lock(&file_priv->fbs_lock);
3187 r->fb_id = fb->base.id;
3188 list_add(&fb->filp_head, &file_priv->fbs);
3189 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3190 mutex_unlock(&file_priv->fbs_lock);
3191
3192 return fb;
3193}
3194
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003195/**
3196 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003197 * @dev: drm device for the ioctl
3198 * @data: data pointer for the ioctl
3199 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003200 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003201 * Add a new FB to the specified CRTC, given a user request with format. This is
3202 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3203 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003204 *
3205 * Called by the user via ioctl.
3206 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003207 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003208 * Zero on success, errno on failure.
3209 */
3210int drm_mode_addfb2(struct drm_device *dev,
3211 void *data, struct drm_file *file_priv)
3212{
Dave Airlief453ba02008-11-07 14:05:41 -08003213 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003214
Dave Airliefb3b06c2011-02-08 13:55:21 +10003215 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3216 return -EINVAL;
3217
Matt Roperc394c2b2014-06-10 08:28:08 -07003218 fb = add_framebuffer_internal(dev, data, file_priv);
3219 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003220 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003221
Matt Roperc394c2b2014-06-10 08:28:08 -07003222 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003223}
3224
3225/**
3226 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003227 * @dev: drm device for the ioctl
3228 * @data: data pointer for the ioctl
3229 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003230 *
Dave Airlief453ba02008-11-07 14:05:41 -08003231 * Remove the FB specified by the user.
3232 *
3233 * Called by the user via ioctl.
3234 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003235 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003236 * Zero on success, errno on failure.
3237 */
3238int drm_mode_rmfb(struct drm_device *dev,
3239 void *data, struct drm_file *file_priv)
3240{
Dave Airlief453ba02008-11-07 14:05:41 -08003241 struct drm_framebuffer *fb = NULL;
3242 struct drm_framebuffer *fbl = NULL;
3243 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003244 int found = 0;
3245
Dave Airliefb3b06c2011-02-08 13:55:21 +10003246 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3247 return -EINVAL;
3248
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003249 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003250 mutex_lock(&dev->mode_config.fb_lock);
3251 fb = __drm_framebuffer_lookup(dev, *id);
3252 if (!fb)
3253 goto fail_lookup;
3254
Dave Airlief453ba02008-11-07 14:05:41 -08003255 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3256 if (fb == fbl)
3257 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003258 if (!found)
3259 goto fail_lookup;
3260
3261 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3262 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003263
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003264 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003265 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003266 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003267
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003268 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003269
Daniel Vetter2b677e82012-12-10 21:16:05 +01003270 return 0;
3271
3272fail_lookup:
3273 mutex_unlock(&dev->mode_config.fb_lock);
3274 mutex_unlock(&file_priv->fbs_lock);
3275
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003276 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003277}
3278
3279/**
3280 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003281 * @dev: drm device for the ioctl
3282 * @data: data pointer for the ioctl
3283 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003284 *
Dave Airlief453ba02008-11-07 14:05:41 -08003285 * Lookup the FB given its ID and return info about it.
3286 *
3287 * Called by the user via ioctl.
3288 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003289 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003290 * Zero on success, errno on failure.
3291 */
3292int drm_mode_getfb(struct drm_device *dev,
3293 void *data, struct drm_file *file_priv)
3294{
3295 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003296 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003297 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003298
Dave Airliefb3b06c2011-02-08 13:55:21 +10003299 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3300 return -EINVAL;
3301
Daniel Vetter786b99e2012-12-02 21:53:40 +01003302 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003303 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003304 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003305
3306 r->height = fb->height;
3307 r->width = fb->width;
3308 r->depth = fb->depth;
3309 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003310 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003311 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003312 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003313 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003314 ret = fb->funcs->create_handle(fb, file_priv,
3315 &r->handle);
3316 } else {
3317 /* GET_FB() is an unprivileged ioctl so we must not
3318 * return a buffer-handle to non-master processes! For
3319 * backwards-compatibility reasons, we cannot make
3320 * GET_FB() privileged, so just return an invalid handle
3321 * for non-masters. */
3322 r->handle = 0;
3323 ret = 0;
3324 }
3325 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003326 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003327 }
Dave Airlief453ba02008-11-07 14:05:41 -08003328
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003329 drm_framebuffer_unreference(fb);
3330
Dave Airlief453ba02008-11-07 14:05:41 -08003331 return ret;
3332}
3333
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003334/**
3335 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3336 * @dev: drm device for the ioctl
3337 * @data: data pointer for the ioctl
3338 * @file_priv: drm file for the ioctl call
3339 *
3340 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3341 * rectangle list. Generic userspace which does frontbuffer rendering must call
3342 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3343 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3344 *
3345 * Modesetting drivers which always update the frontbuffer do not need to
3346 * implement the corresponding ->dirty framebuffer callback.
3347 *
3348 * Called by the user via ioctl.
3349 *
3350 * Returns:
3351 * Zero on success, errno on failure.
3352 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003353int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3354 void *data, struct drm_file *file_priv)
3355{
3356 struct drm_clip_rect __user *clips_ptr;
3357 struct drm_clip_rect *clips = NULL;
3358 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003359 struct drm_framebuffer *fb;
3360 unsigned flags;
3361 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003362 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003363
Dave Airliefb3b06c2011-02-08 13:55:21 +10003364 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3365 return -EINVAL;
3366
Daniel Vetter786b99e2012-12-02 21:53:40 +01003367 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003368 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003369 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003370
3371 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003372 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003373
3374 if (!num_clips != !clips_ptr) {
3375 ret = -EINVAL;
3376 goto out_err1;
3377 }
3378
3379 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3380
3381 /* If userspace annotates copy, clips must come in pairs */
3382 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3383 ret = -EINVAL;
3384 goto out_err1;
3385 }
3386
3387 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003388 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3389 ret = -EINVAL;
3390 goto out_err1;
3391 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003392 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3393 if (!clips) {
3394 ret = -ENOMEM;
3395 goto out_err1;
3396 }
3397
3398 ret = copy_from_user(clips, clips_ptr,
3399 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003400 if (ret) {
3401 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003402 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003403 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003404 }
3405
3406 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003407 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3408 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003409 } else {
3410 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003411 }
3412
3413out_err2:
3414 kfree(clips);
3415out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003416 drm_framebuffer_unreference(fb);
3417
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003418 return ret;
3419}
3420
3421
Dave Airlief453ba02008-11-07 14:05:41 -08003422/**
3423 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003424 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003425 *
Dave Airlief453ba02008-11-07 14:05:41 -08003426 * Destroy all the FBs associated with @filp.
3427 *
3428 * Called by the user via ioctl.
3429 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003430 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003431 * Zero on success, errno on failure.
3432 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003433void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003434{
Dave Airlief453ba02008-11-07 14:05:41 -08003435 struct drm_device *dev = priv->minor->dev;
3436 struct drm_framebuffer *fb, *tfb;
3437
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003438 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003439 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003440
3441 mutex_lock(&dev->mode_config.fb_lock);
3442 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3443 __drm_framebuffer_unregister(dev, fb);
3444 mutex_unlock(&dev->mode_config.fb_lock);
3445
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003446 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003447
3448 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003449 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003450 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003451 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003452}
3453
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003454/**
3455 * drm_property_create - create a new property type
3456 * @dev: drm device
3457 * @flags: flags specifying the property type
3458 * @name: name of the property
3459 * @num_values: number of pre-defined values
3460 *
3461 * This creates a new generic drm property which can then be attached to a drm
3462 * object with drm_object_attach_property. The returned property object must be
3463 * freed with drm_property_destroy.
3464 *
3465 * Returns:
3466 * A pointer to the newly created property on success, NULL on failure.
3467 */
Dave Airlief453ba02008-11-07 14:05:41 -08003468struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3469 const char *name, int num_values)
3470{
3471 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003472 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003473
3474 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3475 if (!property)
3476 return NULL;
3477
Rob Clark98f75de2014-05-30 11:37:03 -04003478 property->dev = dev;
3479
Dave Airlief453ba02008-11-07 14:05:41 -08003480 if (num_values) {
3481 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3482 if (!property->values)
3483 goto fail;
3484 }
3485
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003486 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3487 if (ret)
3488 goto fail;
3489
Dave Airlief453ba02008-11-07 14:05:41 -08003490 property->flags = flags;
3491 property->num_values = num_values;
3492 INIT_LIST_HEAD(&property->enum_blob_list);
3493
Vinson Lee471dd2e2011-11-10 11:55:40 -08003494 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003495 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003496 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3497 }
Dave Airlief453ba02008-11-07 14:05:41 -08003498
3499 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003500
3501 WARN_ON(!drm_property_type_valid(property));
3502
Dave Airlief453ba02008-11-07 14:05:41 -08003503 return property;
3504fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003505 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003506 kfree(property);
3507 return NULL;
3508}
3509EXPORT_SYMBOL(drm_property_create);
3510
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003511/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003512 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003513 * @dev: drm device
3514 * @flags: flags specifying the property type
3515 * @name: name of the property
3516 * @props: enumeration lists with property values
3517 * @num_values: number of pre-defined values
3518 *
3519 * This creates a new generic drm property which can then be attached to a drm
3520 * object with drm_object_attach_property. The returned property object must be
3521 * freed with drm_property_destroy.
3522 *
3523 * Userspace is only allowed to set one of the predefined values for enumeration
3524 * properties.
3525 *
3526 * Returns:
3527 * A pointer to the newly created property on success, NULL on failure.
3528 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003529struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3530 const char *name,
3531 const struct drm_prop_enum_list *props,
3532 int num_values)
3533{
3534 struct drm_property *property;
3535 int i, ret;
3536
3537 flags |= DRM_MODE_PROP_ENUM;
3538
3539 property = drm_property_create(dev, flags, name, num_values);
3540 if (!property)
3541 return NULL;
3542
3543 for (i = 0; i < num_values; i++) {
3544 ret = drm_property_add_enum(property, i,
3545 props[i].type,
3546 props[i].name);
3547 if (ret) {
3548 drm_property_destroy(dev, property);
3549 return NULL;
3550 }
3551 }
3552
3553 return property;
3554}
3555EXPORT_SYMBOL(drm_property_create_enum);
3556
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003557/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003558 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003559 * @dev: drm device
3560 * @flags: flags specifying the property type
3561 * @name: name of the property
3562 * @props: enumeration lists with property bitflags
3563 * @num_values: number of pre-defined values
3564 *
3565 * This creates a new generic drm property which can then be attached to a drm
3566 * object with drm_object_attach_property. The returned property object must be
3567 * freed with drm_property_destroy.
3568 *
3569 * Compared to plain enumeration properties userspace is allowed to set any
3570 * or'ed together combination of the predefined property bitflag values
3571 *
3572 * Returns:
3573 * A pointer to the newly created property on success, NULL on failure.
3574 */
Rob Clark49e27542012-05-17 02:23:26 -06003575struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3576 int flags, const char *name,
3577 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303578 int num_props,
3579 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003580{
3581 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303582 int i, ret, index = 0;
3583 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003584
3585 flags |= DRM_MODE_PROP_BITMASK;
3586
3587 property = drm_property_create(dev, flags, name, num_values);
3588 if (!property)
3589 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303590 for (i = 0; i < num_props; i++) {
3591 if (!(supported_bits & (1ULL << props[i].type)))
3592 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003593
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303594 if (WARN_ON(index >= num_values)) {
3595 drm_property_destroy(dev, property);
3596 return NULL;
3597 }
3598
3599 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003600 props[i].type,
3601 props[i].name);
3602 if (ret) {
3603 drm_property_destroy(dev, property);
3604 return NULL;
3605 }
3606 }
3607
3608 return property;
3609}
3610EXPORT_SYMBOL(drm_property_create_bitmask);
3611
Rob Clarkebc44cf2012-09-12 22:22:31 -05003612static struct drm_property *property_create_range(struct drm_device *dev,
3613 int flags, const char *name,
3614 uint64_t min, uint64_t max)
3615{
3616 struct drm_property *property;
3617
3618 property = drm_property_create(dev, flags, name, 2);
3619 if (!property)
3620 return NULL;
3621
3622 property->values[0] = min;
3623 property->values[1] = max;
3624
3625 return property;
3626}
3627
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003628/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003629 * drm_property_create_range - create a new ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003630 * @dev: drm device
3631 * @flags: flags specifying the property type
3632 * @name: name of the property
3633 * @min: minimum value of the property
3634 * @max: maximum value of the property
3635 *
3636 * This creates a new generic drm property which can then be attached to a drm
3637 * object with drm_object_attach_property. The returned property object must be
3638 * freed with drm_property_destroy.
3639 *
3640 * Userspace is allowed to set any interger value in the (min, max) range
3641 * inclusive.
3642 *
3643 * Returns:
3644 * A pointer to the newly created property on success, NULL on failure.
3645 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003646struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3647 const char *name,
3648 uint64_t min, uint64_t max)
3649{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003650 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3651 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003652}
3653EXPORT_SYMBOL(drm_property_create_range);
3654
Rob Clarkebc44cf2012-09-12 22:22:31 -05003655struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3656 int flags, const char *name,
3657 int64_t min, int64_t max)
3658{
3659 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3660 name, I642U64(min), I642U64(max));
3661}
3662EXPORT_SYMBOL(drm_property_create_signed_range);
3663
Rob Clark98f75de2014-05-30 11:37:03 -04003664struct drm_property *drm_property_create_object(struct drm_device *dev,
3665 int flags, const char *name, uint32_t type)
3666{
3667 struct drm_property *property;
3668
3669 flags |= DRM_MODE_PROP_OBJECT;
3670
3671 property = drm_property_create(dev, flags, name, 1);
3672 if (!property)
3673 return NULL;
3674
3675 property->values[0] = type;
3676
3677 return property;
3678}
3679EXPORT_SYMBOL(drm_property_create_object);
3680
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003681/**
3682 * drm_property_add_enum - add a possible value to an enumeration property
3683 * @property: enumeration property to change
3684 * @index: index of the new enumeration
3685 * @value: value of the new enumeration
3686 * @name: symbolic name of the new enumeration
3687 *
3688 * This functions adds enumerations to a property.
3689 *
3690 * It's use is deprecated, drivers should use one of the more specific helpers
3691 * to directly create the property with all enumerations already attached.
3692 *
3693 * Returns:
3694 * Zero on success, error code on failure.
3695 */
Dave Airlief453ba02008-11-07 14:05:41 -08003696int drm_property_add_enum(struct drm_property *property, int index,
3697 uint64_t value, const char *name)
3698{
3699 struct drm_property_enum *prop_enum;
3700
Rob Clark5ea22f22014-05-30 11:34:01 -04003701 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3702 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003703 return -EINVAL;
3704
3705 /*
3706 * Bitmask enum properties have the additional constraint of values
3707 * from 0 to 63
3708 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003709 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3710 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003711 return -EINVAL;
3712
3713 if (!list_empty(&property->enum_blob_list)) {
3714 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3715 if (prop_enum->value == value) {
3716 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3717 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3718 return 0;
3719 }
3720 }
3721 }
3722
3723 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3724 if (!prop_enum)
3725 return -ENOMEM;
3726
3727 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3728 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3729 prop_enum->value = value;
3730
3731 property->values[index] = value;
3732 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3733 return 0;
3734}
3735EXPORT_SYMBOL(drm_property_add_enum);
3736
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003737/**
3738 * drm_property_destroy - destroy a drm property
3739 * @dev: drm device
3740 * @property: property to destry
3741 *
3742 * This function frees a property including any attached resources like
3743 * enumeration values.
3744 */
Dave Airlief453ba02008-11-07 14:05:41 -08003745void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3746{
3747 struct drm_property_enum *prop_enum, *pt;
3748
3749 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3750 list_del(&prop_enum->head);
3751 kfree(prop_enum);
3752 }
3753
3754 if (property->num_values)
3755 kfree(property->values);
3756 drm_mode_object_put(dev, &property->base);
3757 list_del(&property->head);
3758 kfree(property);
3759}
3760EXPORT_SYMBOL(drm_property_destroy);
3761
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003762/**
3763 * drm_object_attach_property - attach a property to a modeset object
3764 * @obj: drm modeset object
3765 * @property: property to attach
3766 * @init_val: initial value of the property
3767 *
3768 * This attaches the given property to the modeset object with the given initial
3769 * value. Currently this function cannot fail since the properties are stored in
3770 * a statically sized array.
3771 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003772void drm_object_attach_property(struct drm_mode_object *obj,
3773 struct drm_property *property,
3774 uint64_t init_val)
3775{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003776 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003777
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003778 if (count == DRM_OBJECT_MAX_PROPERTY) {
3779 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3780 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3781 "you see this message on the same object type.\n",
3782 obj->type);
3783 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003784 }
3785
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003786 obj->properties->ids[count] = property->base.id;
3787 obj->properties->values[count] = init_val;
3788 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003789}
3790EXPORT_SYMBOL(drm_object_attach_property);
3791
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003792/**
3793 * drm_object_property_set_value - set the value of a property
3794 * @obj: drm mode object to set property value for
3795 * @property: property to set
3796 * @val: value the property should be set to
3797 *
3798 * This functions sets a given property on a given object. This function only
3799 * changes the software state of the property, it does not call into the
3800 * driver's ->set_property callback.
3801 *
3802 * Returns:
3803 * Zero on success, error code on failure.
3804 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003805int drm_object_property_set_value(struct drm_mode_object *obj,
3806 struct drm_property *property, uint64_t val)
3807{
3808 int i;
3809
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003810 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003811 if (obj->properties->ids[i] == property->base.id) {
3812 obj->properties->values[i] = val;
3813 return 0;
3814 }
3815 }
3816
3817 return -EINVAL;
3818}
3819EXPORT_SYMBOL(drm_object_property_set_value);
3820
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003821/**
3822 * drm_object_property_get_value - retrieve the value of a property
3823 * @obj: drm mode object to get property value from
3824 * @property: property to retrieve
3825 * @val: storage for the property value
3826 *
3827 * This function retrieves the softare state of the given property for the given
3828 * property. Since there is no driver callback to retrieve the current property
3829 * value this might be out of sync with the hardware, depending upon the driver
3830 * and property.
3831 *
3832 * Returns:
3833 * Zero on success, error code on failure.
3834 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003835int drm_object_property_get_value(struct drm_mode_object *obj,
3836 struct drm_property *property, uint64_t *val)
3837{
3838 int i;
3839
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003840 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003841 if (obj->properties->ids[i] == property->base.id) {
3842 *val = obj->properties->values[i];
3843 return 0;
3844 }
3845 }
3846
3847 return -EINVAL;
3848}
3849EXPORT_SYMBOL(drm_object_property_get_value);
3850
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003851/**
3852 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3853 * @dev: DRM device
3854 * @data: ioctl data
3855 * @file_priv: DRM file info
3856 *
3857 * This function retrieves the current value for an connectors's property.
3858 *
3859 * Called by the user via ioctl.
3860 *
3861 * Returns:
3862 * Zero on success, errno on failure.
3863 */
Dave Airlief453ba02008-11-07 14:05:41 -08003864int drm_mode_getproperty_ioctl(struct drm_device *dev,
3865 void *data, struct drm_file *file_priv)
3866{
Dave Airlief453ba02008-11-07 14:05:41 -08003867 struct drm_mode_get_property *out_resp = data;
3868 struct drm_property *property;
3869 int enum_count = 0;
3870 int blob_count = 0;
3871 int value_count = 0;
3872 int ret = 0, i;
3873 int copied;
3874 struct drm_property_enum *prop_enum;
3875 struct drm_mode_property_enum __user *enum_ptr;
3876 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003877 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003878 uint64_t __user *values_ptr;
3879 uint32_t __user *blob_length_ptr;
3880
Dave Airliefb3b06c2011-02-08 13:55:21 +10003881 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3882 return -EINVAL;
3883
Daniel Vetter84849902012-12-02 00:28:11 +01003884 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003885 property = drm_property_find(dev, out_resp->prop_id);
3886 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003887 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003888 goto done;
3889 }
Dave Airlief453ba02008-11-07 14:05:41 -08003890
Rob Clark5ea22f22014-05-30 11:34:01 -04003891 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3892 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003893 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3894 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003895 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003896 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3897 blob_count++;
3898 }
3899
3900 value_count = property->num_values;
3901
3902 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3903 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3904 out_resp->flags = property->flags;
3905
3906 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003907 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003908 for (i = 0; i < value_count; i++) {
3909 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3910 ret = -EFAULT;
3911 goto done;
3912 }
3913 }
3914 }
3915 out_resp->count_values = value_count;
3916
Rob Clark5ea22f22014-05-30 11:34:01 -04003917 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3918 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003919 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3920 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003921 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003922 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3923
3924 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3925 ret = -EFAULT;
3926 goto done;
3927 }
3928
3929 if (copy_to_user(&enum_ptr[copied].name,
3930 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3931 ret = -EFAULT;
3932 goto done;
3933 }
3934 copied++;
3935 }
3936 }
3937 out_resp->count_enum_blobs = enum_count;
3938 }
3939
Rob Clark5ea22f22014-05-30 11:34:01 -04003940 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003941 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3942 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003943 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3944 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003945
3946 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3947 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3948 ret = -EFAULT;
3949 goto done;
3950 }
3951
3952 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3953 ret = -EFAULT;
3954 goto done;
3955 }
3956
3957 copied++;
3958 }
3959 }
3960 out_resp->count_enum_blobs = blob_count;
3961 }
3962done:
Daniel Vetter84849902012-12-02 00:28:11 +01003963 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003964 return ret;
3965}
3966
3967static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3968 void *data)
3969{
3970 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003971 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003972
3973 if (!length || !data)
3974 return NULL;
3975
3976 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3977 if (!blob)
3978 return NULL;
3979
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003980 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3981 if (ret) {
3982 kfree(blob);
3983 return NULL;
3984 }
3985
Dave Airlief453ba02008-11-07 14:05:41 -08003986 blob->length = length;
3987
3988 memcpy(blob->data, data, length);
3989
Dave Airlief453ba02008-11-07 14:05:41 -08003990 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3991 return blob;
3992}
3993
3994static void drm_property_destroy_blob(struct drm_device *dev,
3995 struct drm_property_blob *blob)
3996{
3997 drm_mode_object_put(dev, &blob->base);
3998 list_del(&blob->head);
3999 kfree(blob);
4000}
4001
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004002/**
4003 * drm_mode_getblob_ioctl - get the contents of a blob property value
4004 * @dev: DRM device
4005 * @data: ioctl data
4006 * @file_priv: DRM file info
4007 *
4008 * This function retrieves the contents of a blob property. The value stored in
4009 * an object's blob property is just a normal modeset object id.
4010 *
4011 * Called by the user via ioctl.
4012 *
4013 * Returns:
4014 * Zero on success, errno on failure.
4015 */
Dave Airlief453ba02008-11-07 14:05:41 -08004016int drm_mode_getblob_ioctl(struct drm_device *dev,
4017 void *data, struct drm_file *file_priv)
4018{
Dave Airlief453ba02008-11-07 14:05:41 -08004019 struct drm_mode_get_blob *out_resp = data;
4020 struct drm_property_blob *blob;
4021 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004022 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004023
Dave Airliefb3b06c2011-02-08 13:55:21 +10004024 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4025 return -EINVAL;
4026
Daniel Vetter84849902012-12-02 00:28:11 +01004027 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004028 blob = drm_property_blob_find(dev, out_resp->blob_id);
4029 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004030 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004031 goto done;
4032 }
Dave Airlief453ba02008-11-07 14:05:41 -08004033
4034 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004035 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08004036 if (copy_to_user(blob_ptr, blob->data, blob->length)){
4037 ret = -EFAULT;
4038 goto done;
4039 }
4040 }
4041 out_resp->length = blob->length;
4042
4043done:
Daniel Vetter84849902012-12-02 00:28:11 +01004044 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004045 return ret;
4046}
4047
Dave Airlie43aba7e2014-06-05 14:01:31 +10004048int drm_mode_connector_set_path_property(struct drm_connector *connector,
4049 char *path)
4050{
4051 struct drm_device *dev = connector->dev;
4052 int ret, size;
4053 size = strlen(path) + 1;
4054
4055 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4056 size, path);
4057 if (!connector->path_blob_ptr)
4058 return -EINVAL;
4059
4060 ret = drm_object_property_set_value(&connector->base,
4061 dev->mode_config.path_property,
4062 connector->path_blob_ptr->base.id);
4063 return ret;
4064}
4065EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4066
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004067/**
4068 * drm_mode_connector_update_edid_property - update the edid property of a connector
4069 * @connector: drm connector
4070 * @edid: new value of the edid property
4071 *
4072 * This function creates a new blob modeset object and assigns its id to the
4073 * connector's edid property.
4074 *
4075 * Returns:
4076 * Zero on success, errno on failure.
4077 */
Dave Airlief453ba02008-11-07 14:05:41 -08004078int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4079 struct edid *edid)
4080{
4081 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02004082 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08004083
Thomas Wood4cf2b282014-06-18 17:52:33 +01004084 /* ignore requests to set edid when overridden */
4085 if (connector->override_edid)
4086 return 0;
4087
Dave Airlief453ba02008-11-07 14:05:41 -08004088 if (connector->edid_blob_ptr)
4089 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4090
4091 /* Delete edid, when there is none. */
4092 if (!edid) {
4093 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05004094 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08004095 return ret;
4096 }
4097
Adam Jackson7466f4c2010-03-29 21:43:23 +00004098 size = EDID_LENGTH * (1 + edid->extensions);
4099 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4100 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00004101 if (!connector->edid_blob_ptr)
4102 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08004103
Rob Clark58495562012-10-11 20:50:56 -05004104 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08004105 dev->mode_config.edid_property,
4106 connector->edid_blob_ptr->base.id);
4107
4108 return ret;
4109}
4110EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4111
Paulo Zanoni26a34812012-05-15 18:08:59 -03004112static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004113 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004114{
4115 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4116 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004117
4118 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004119 if (value < property->values[0] || value > property->values[1])
4120 return false;
4121 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004122 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4123 int64_t svalue = U642I64(value);
4124 if (svalue < U642I64(property->values[0]) ||
4125 svalue > U642I64(property->values[1]))
4126 return false;
4127 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004128 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06004129 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004130 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06004131 for (i = 0; i < property->num_values; i++)
4132 valid_mask |= (1ULL << property->values[i]);
4133 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004134 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004135 /* Only the driver knows */
4136 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004137 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4138 struct drm_mode_object *obj;
4139 /* a zero value for an object property translates to null: */
4140 if (value == 0)
4141 return true;
4142 /*
4143 * NOTE: use _object_find() directly to bypass restriction on
4144 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4145 * object this could race against object finalization, so it
4146 * simply tells us that the object *was* valid. Which is good
4147 * enough.
4148 */
4149 obj = _object_find(property->dev, value, property->values[0]);
4150 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004151 } else {
4152 int i;
4153 for (i = 0; i < property->num_values; i++)
4154 if (property->values[i] == value)
4155 return true;
4156 return false;
4157 }
4158}
4159
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004160/**
4161 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4162 * @dev: DRM device
4163 * @data: ioctl data
4164 * @file_priv: DRM file info
4165 *
4166 * This function sets the current value for a connectors's property. It also
4167 * calls into a driver's ->set_property callback to update the hardware state
4168 *
4169 * Called by the user via ioctl.
4170 *
4171 * Returns:
4172 * Zero on success, errno on failure.
4173 */
Dave Airlief453ba02008-11-07 14:05:41 -08004174int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4175 void *data, struct drm_file *file_priv)
4176{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004177 struct drm_mode_connector_set_property *conn_set_prop = data;
4178 struct drm_mode_obj_set_property obj_set_prop = {
4179 .value = conn_set_prop->value,
4180 .prop_id = conn_set_prop->prop_id,
4181 .obj_id = conn_set_prop->connector_id,
4182 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4183 };
Dave Airlief453ba02008-11-07 14:05:41 -08004184
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004185 /* It does all the locking and checking we need */
4186 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004187}
4188
Paulo Zanonic5431882012-05-15 18:09:02 -03004189static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4190 struct drm_property *property,
4191 uint64_t value)
4192{
4193 int ret = -EINVAL;
4194 struct drm_connector *connector = obj_to_connector(obj);
4195
4196 /* Do DPMS ourselves */
4197 if (property == connector->dev->mode_config.dpms_property) {
4198 if (connector->funcs->dpms)
4199 (*connector->funcs->dpms)(connector, (int)value);
4200 ret = 0;
4201 } else if (connector->funcs->set_property)
4202 ret = connector->funcs->set_property(connector, property, value);
4203
4204 /* store the property value if successful */
4205 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004206 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004207 return ret;
4208}
4209
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004210static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4211 struct drm_property *property,
4212 uint64_t value)
4213{
4214 int ret = -EINVAL;
4215 struct drm_crtc *crtc = obj_to_crtc(obj);
4216
4217 if (crtc->funcs->set_property)
4218 ret = crtc->funcs->set_property(crtc, property, value);
4219 if (!ret)
4220 drm_object_property_set_value(obj, property, value);
4221
4222 return ret;
4223}
4224
Rob Clark4d939142012-05-17 02:23:27 -06004225static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4226 struct drm_property *property,
4227 uint64_t value)
4228{
4229 int ret = -EINVAL;
4230 struct drm_plane *plane = obj_to_plane(obj);
4231
4232 if (plane->funcs->set_property)
4233 ret = plane->funcs->set_property(plane, property, value);
4234 if (!ret)
4235 drm_object_property_set_value(obj, property, value);
4236
4237 return ret;
4238}
4239
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004240/**
4241 * drm_mode_getproperty_ioctl - get the current value of a object's property
4242 * @dev: DRM device
4243 * @data: ioctl data
4244 * @file_priv: DRM file info
4245 *
4246 * This function retrieves the current value for an object's property. Compared
4247 * to the connector specific ioctl this one is extended to also work on crtc and
4248 * plane objects.
4249 *
4250 * Called by the user via ioctl.
4251 *
4252 * Returns:
4253 * Zero on success, errno on failure.
4254 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004255int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4256 struct drm_file *file_priv)
4257{
4258 struct drm_mode_obj_get_properties *arg = data;
4259 struct drm_mode_object *obj;
4260 int ret = 0;
4261 int i;
4262 int copied = 0;
4263 int props_count = 0;
4264 uint32_t __user *props_ptr;
4265 uint64_t __user *prop_values_ptr;
4266
4267 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4268 return -EINVAL;
4269
Daniel Vetter84849902012-12-02 00:28:11 +01004270 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004271
4272 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4273 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004274 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004275 goto out;
4276 }
4277 if (!obj->properties) {
4278 ret = -EINVAL;
4279 goto out;
4280 }
4281
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004282 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004283
4284 /* This ioctl is called twice, once to determine how much space is
4285 * needed, and the 2nd time to fill it. */
4286 if ((arg->count_props >= props_count) && props_count) {
4287 copied = 0;
4288 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4289 prop_values_ptr = (uint64_t __user *)(unsigned long)
4290 (arg->prop_values_ptr);
4291 for (i = 0; i < props_count; i++) {
4292 if (put_user(obj->properties->ids[i],
4293 props_ptr + copied)) {
4294 ret = -EFAULT;
4295 goto out;
4296 }
4297 if (put_user(obj->properties->values[i],
4298 prop_values_ptr + copied)) {
4299 ret = -EFAULT;
4300 goto out;
4301 }
4302 copied++;
4303 }
4304 }
4305 arg->count_props = props_count;
4306out:
Daniel Vetter84849902012-12-02 00:28:11 +01004307 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004308 return ret;
4309}
4310
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004311/**
4312 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4313 * @dev: DRM device
4314 * @data: ioctl data
4315 * @file_priv: DRM file info
4316 *
4317 * This function sets the current value for an object's property. It also calls
4318 * into a driver's ->set_property callback to update the hardware state.
4319 * Compared to the connector specific ioctl this one is extended to also work on
4320 * crtc and plane objects.
4321 *
4322 * Called by the user via ioctl.
4323 *
4324 * Returns:
4325 * Zero on success, errno on failure.
4326 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004327int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4328 struct drm_file *file_priv)
4329{
4330 struct drm_mode_obj_set_property *arg = data;
4331 struct drm_mode_object *arg_obj;
4332 struct drm_mode_object *prop_obj;
4333 struct drm_property *property;
4334 int ret = -EINVAL;
4335 int i;
4336
4337 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4338 return -EINVAL;
4339
Daniel Vetter84849902012-12-02 00:28:11 +01004340 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004341
4342 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004343 if (!arg_obj) {
4344 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004345 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004346 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004347 if (!arg_obj->properties)
4348 goto out;
4349
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004350 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004351 if (arg_obj->properties->ids[i] == arg->prop_id)
4352 break;
4353
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004354 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004355 goto out;
4356
4357 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4358 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004359 if (!prop_obj) {
4360 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004361 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004362 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004363 property = obj_to_property(prop_obj);
4364
4365 if (!drm_property_change_is_valid(property, arg->value))
4366 goto out;
4367
4368 switch (arg_obj->type) {
4369 case DRM_MODE_OBJECT_CONNECTOR:
4370 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4371 arg->value);
4372 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004373 case DRM_MODE_OBJECT_CRTC:
4374 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4375 break;
Rob Clark4d939142012-05-17 02:23:27 -06004376 case DRM_MODE_OBJECT_PLANE:
4377 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4378 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004379 }
4380
4381out:
Daniel Vetter84849902012-12-02 00:28:11 +01004382 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004383 return ret;
4384}
4385
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004386/**
4387 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4388 * @connector: connector to attach
4389 * @encoder: encoder to attach @connector to
4390 *
4391 * This function links up a connector to an encoder. Note that the routing
4392 * restrictions between encoders and crtcs are exposed to userspace through the
4393 * possible_clones and possible_crtcs bitmasks.
4394 *
4395 * Returns:
4396 * Zero on success, errno on failure.
4397 */
Dave Airlief453ba02008-11-07 14:05:41 -08004398int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4399 struct drm_encoder *encoder)
4400{
4401 int i;
4402
4403 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4404 if (connector->encoder_ids[i] == 0) {
4405 connector->encoder_ids[i] = encoder->base.id;
4406 return 0;
4407 }
4408 }
4409 return -ENOMEM;
4410}
4411EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4412
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004413/**
4414 * drm_mode_crtc_set_gamma_size - set the gamma table size
4415 * @crtc: CRTC to set the gamma table size for
4416 * @gamma_size: size of the gamma table
4417 *
4418 * Drivers which support gamma tables should set this to the supported gamma
4419 * table size when initializing the CRTC. Currently the drm core only supports a
4420 * fixed gamma table size.
4421 *
4422 * Returns:
4423 * Zero on success, errno on failure.
4424 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004425int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004426 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004427{
4428 crtc->gamma_size = gamma_size;
4429
4430 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4431 if (!crtc->gamma_store) {
4432 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004433 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004434 }
4435
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004436 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004437}
4438EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4439
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004440/**
4441 * drm_mode_gamma_set_ioctl - set the gamma table
4442 * @dev: DRM device
4443 * @data: ioctl data
4444 * @file_priv: DRM file info
4445 *
4446 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4447 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4448 *
4449 * Called by the user via ioctl.
4450 *
4451 * Returns:
4452 * Zero on success, errno on failure.
4453 */
Dave Airlief453ba02008-11-07 14:05:41 -08004454int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4455 void *data, struct drm_file *file_priv)
4456{
4457 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004458 struct drm_crtc *crtc;
4459 void *r_base, *g_base, *b_base;
4460 int size;
4461 int ret = 0;
4462
Dave Airliefb3b06c2011-02-08 13:55:21 +10004463 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4464 return -EINVAL;
4465
Daniel Vetter84849902012-12-02 00:28:11 +01004466 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004467 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4468 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004469 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004470 goto out;
4471 }
Dave Airlief453ba02008-11-07 14:05:41 -08004472
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004473 if (crtc->funcs->gamma_set == NULL) {
4474 ret = -ENOSYS;
4475 goto out;
4476 }
4477
Dave Airlief453ba02008-11-07 14:05:41 -08004478 /* memcpy into gamma store */
4479 if (crtc_lut->gamma_size != crtc->gamma_size) {
4480 ret = -EINVAL;
4481 goto out;
4482 }
4483
4484 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4485 r_base = crtc->gamma_store;
4486 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4487 ret = -EFAULT;
4488 goto out;
4489 }
4490
4491 g_base = r_base + size;
4492 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4493 ret = -EFAULT;
4494 goto out;
4495 }
4496
4497 b_base = g_base + size;
4498 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4499 ret = -EFAULT;
4500 goto out;
4501 }
4502
James Simmons72034252010-08-03 01:33:19 +01004503 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004504
4505out:
Daniel Vetter84849902012-12-02 00:28:11 +01004506 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004507 return ret;
4508
4509}
4510
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004511/**
4512 * drm_mode_gamma_get_ioctl - get the gamma table
4513 * @dev: DRM device
4514 * @data: ioctl data
4515 * @file_priv: DRM file info
4516 *
4517 * Copy the current gamma table into the storage provided. This also provides
4518 * the gamma table size the driver expects, which can be used to size the
4519 * allocated storage.
4520 *
4521 * Called by the user via ioctl.
4522 *
4523 * Returns:
4524 * Zero on success, errno on failure.
4525 */
Dave Airlief453ba02008-11-07 14:05:41 -08004526int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4527 void *data, struct drm_file *file_priv)
4528{
4529 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004530 struct drm_crtc *crtc;
4531 void *r_base, *g_base, *b_base;
4532 int size;
4533 int ret = 0;
4534
Dave Airliefb3b06c2011-02-08 13:55:21 +10004535 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4536 return -EINVAL;
4537
Daniel Vetter84849902012-12-02 00:28:11 +01004538 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004539 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4540 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004541 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004542 goto out;
4543 }
Dave Airlief453ba02008-11-07 14:05:41 -08004544
4545 /* memcpy into gamma store */
4546 if (crtc_lut->gamma_size != crtc->gamma_size) {
4547 ret = -EINVAL;
4548 goto out;
4549 }
4550
4551 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4552 r_base = crtc->gamma_store;
4553 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4554 ret = -EFAULT;
4555 goto out;
4556 }
4557
4558 g_base = r_base + size;
4559 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4560 ret = -EFAULT;
4561 goto out;
4562 }
4563
4564 b_base = g_base + size;
4565 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4566 ret = -EFAULT;
4567 goto out;
4568 }
4569out:
Daniel Vetter84849902012-12-02 00:28:11 +01004570 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004571 return ret;
4572}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004573
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004574/**
4575 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4576 * @dev: DRM device
4577 * @data: ioctl data
4578 * @file_priv: DRM file info
4579 *
4580 * This schedules an asynchronous update on a given CRTC, called page flip.
4581 * Optionally a drm event is generated to signal the completion of the event.
4582 * Generic drivers cannot assume that a pageflip with changed framebuffer
4583 * properties (including driver specific metadata like tiling layout) will work,
4584 * but some drivers support e.g. pixel format changes through the pageflip
4585 * ioctl.
4586 *
4587 * Called by the user via ioctl.
4588 *
4589 * Returns:
4590 * Zero on success, errno on failure.
4591 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004592int drm_mode_page_flip_ioctl(struct drm_device *dev,
4593 void *data, struct drm_file *file_priv)
4594{
4595 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004596 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004597 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004598 struct drm_pending_vblank_event *e = NULL;
4599 unsigned long flags;
4600 int ret = -EINVAL;
4601
4602 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4603 page_flip->reserved != 0)
4604 return -EINVAL;
4605
Keith Packard62f21042013-07-22 18:50:00 -07004606 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4607 return -EINVAL;
4608
Rob Clarka2b34e22013-10-05 16:36:52 -04004609 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4610 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004611 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004612
Rob Clark51fd3712013-11-19 12:10:12 -05004613 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004614 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004615 /* The framebuffer is currently unbound, presumably
4616 * due to a hotplug event, that userspace has not
4617 * yet discovered.
4618 */
4619 ret = -EBUSY;
4620 goto out;
4621 }
4622
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004623 if (crtc->funcs->page_flip == NULL)
4624 goto out;
4625
Daniel Vetter786b99e2012-12-02 21:53:40 +01004626 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004627 if (!fb) {
4628 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004629 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004630 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004631
Damien Lespiauc11e9282013-09-25 16:45:30 +01004632 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4633 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004634 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004635
Matt Roperf4510a22014-04-01 15:22:40 -07004636 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004637 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4638 ret = -EINVAL;
4639 goto out;
4640 }
4641
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004642 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4643 ret = -ENOMEM;
4644 spin_lock_irqsave(&dev->event_lock, flags);
4645 if (file_priv->event_space < sizeof e->event) {
4646 spin_unlock_irqrestore(&dev->event_lock, flags);
4647 goto out;
4648 }
4649 file_priv->event_space -= sizeof e->event;
4650 spin_unlock_irqrestore(&dev->event_lock, flags);
4651
4652 e = kzalloc(sizeof *e, GFP_KERNEL);
4653 if (e == NULL) {
4654 spin_lock_irqsave(&dev->event_lock, flags);
4655 file_priv->event_space += sizeof e->event;
4656 spin_unlock_irqrestore(&dev->event_lock, flags);
4657 goto out;
4658 }
4659
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004660 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004661 e->event.base.length = sizeof e->event;
4662 e->event.user_data = page_flip->user_data;
4663 e->base.event = &e->event.base;
4664 e->base.file_priv = file_priv;
4665 e->base.destroy =
4666 (void (*) (struct drm_pending_event *)) kfree;
4667 }
4668
Matt Roperf4510a22014-04-01 15:22:40 -07004669 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004670 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004671 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004672 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4673 spin_lock_irqsave(&dev->event_lock, flags);
4674 file_priv->event_space += sizeof e->event;
4675 spin_unlock_irqrestore(&dev->event_lock, flags);
4676 kfree(e);
4677 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004678 /* Keep the old fb, don't unref it. */
4679 old_fb = NULL;
4680 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004681 /*
4682 * Warn if the driver hasn't properly updated the crtc->fb
4683 * field to reflect that the new framebuffer is now used.
4684 * Failing to do so will screw with the reference counting
4685 * on framebuffers.
4686 */
Matt Roperf4510a22014-04-01 15:22:40 -07004687 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004688 /* Unref only the old framebuffer. */
4689 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004690 }
4691
4692out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004693 if (fb)
4694 drm_framebuffer_unreference(fb);
4695 if (old_fb)
4696 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004697 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004698
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004699 return ret;
4700}
Chris Wilsoneb033552011-01-24 15:11:08 +00004701
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004702/**
4703 * drm_mode_config_reset - call ->reset callbacks
4704 * @dev: drm device
4705 *
4706 * This functions calls all the crtc's, encoder's and connector's ->reset
4707 * callback. Drivers can use this in e.g. their driver load or resume code to
4708 * reset hardware and software state.
4709 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004710void drm_mode_config_reset(struct drm_device *dev)
4711{
4712 struct drm_crtc *crtc;
4713 struct drm_encoder *encoder;
4714 struct drm_connector *connector;
4715
4716 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4717 if (crtc->funcs->reset)
4718 crtc->funcs->reset(crtc);
4719
4720 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4721 if (encoder->funcs->reset)
4722 encoder->funcs->reset(encoder);
4723
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004724 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4725 connector->status = connector_status_unknown;
4726
Chris Wilsoneb033552011-01-24 15:11:08 +00004727 if (connector->funcs->reset)
4728 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004729 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004730}
4731EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004732
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004733/**
4734 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4735 * @dev: DRM device
4736 * @data: ioctl data
4737 * @file_priv: DRM file info
4738 *
4739 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4740 * TTM or something else entirely) and returns the resulting buffer handle. This
4741 * handle can then be wrapped up into a framebuffer modeset object.
4742 *
4743 * Note that userspace is not allowed to use such objects for render
4744 * acceleration - drivers must create their own private ioctls for such a use
4745 * case.
4746 *
4747 * Called by the user via ioctl.
4748 *
4749 * Returns:
4750 * Zero on success, errno on failure.
4751 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004752int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4753 void *data, struct drm_file *file_priv)
4754{
4755 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004756 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004757
4758 if (!dev->driver->dumb_create)
4759 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004760 if (!args->width || !args->height || !args->bpp)
4761 return -EINVAL;
4762
4763 /* overflow checks for 32bit size calculations */
4764 cpp = DIV_ROUND_UP(args->bpp, 8);
4765 if (cpp > 0xffffffffU / args->width)
4766 return -EINVAL;
4767 stride = cpp * args->width;
4768 if (args->height > 0xffffffffU / stride)
4769 return -EINVAL;
4770
4771 /* test for wrap-around */
4772 size = args->height * stride;
4773 if (PAGE_ALIGN(size) == 0)
4774 return -EINVAL;
4775
Dave Airlieff72145b2011-02-07 12:16:14 +10004776 return dev->driver->dumb_create(file_priv, dev, args);
4777}
4778
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004779/**
4780 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4781 * @dev: DRM device
4782 * @data: ioctl data
4783 * @file_priv: DRM file info
4784 *
4785 * Allocate an offset in the drm device node's address space to be able to
4786 * memory map a dumb buffer.
4787 *
4788 * Called by the user via ioctl.
4789 *
4790 * Returns:
4791 * Zero on success, errno on failure.
4792 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004793int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4794 void *data, struct drm_file *file_priv)
4795{
4796 struct drm_mode_map_dumb *args = data;
4797
4798 /* call driver ioctl to get mmap offset */
4799 if (!dev->driver->dumb_map_offset)
4800 return -ENOSYS;
4801
4802 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4803}
4804
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004805/**
4806 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4807 * @dev: DRM device
4808 * @data: ioctl data
4809 * @file_priv: DRM file info
4810 *
4811 * This destroys the userspace handle for the given dumb backing storage buffer.
4812 * Since buffer objects must be reference counted in the kernel a buffer object
4813 * won't be immediately freed if a framebuffer modeset object still uses it.
4814 *
4815 * Called by the user via ioctl.
4816 *
4817 * Returns:
4818 * Zero on success, errno on failure.
4819 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004820int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4821 void *data, struct drm_file *file_priv)
4822{
4823 struct drm_mode_destroy_dumb *args = data;
4824
4825 if (!dev->driver->dumb_destroy)
4826 return -ENOSYS;
4827
4828 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4829}
Dave Airlie248dbc22011-11-29 20:02:54 +00004830
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004831/**
4832 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4833 * @format: pixel format (DRM_FORMAT_*)
4834 * @depth: storage for the depth value
4835 * @bpp: storage for the bpp value
4836 *
4837 * This only supports RGB formats here for compat with code that doesn't use
4838 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004839 */
4840void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4841 int *bpp)
4842{
4843 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004844 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004845 case DRM_FORMAT_RGB332:
4846 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004847 *depth = 8;
4848 *bpp = 8;
4849 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004850 case DRM_FORMAT_XRGB1555:
4851 case DRM_FORMAT_XBGR1555:
4852 case DRM_FORMAT_RGBX5551:
4853 case DRM_FORMAT_BGRX5551:
4854 case DRM_FORMAT_ARGB1555:
4855 case DRM_FORMAT_ABGR1555:
4856 case DRM_FORMAT_RGBA5551:
4857 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004858 *depth = 15;
4859 *bpp = 16;
4860 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004861 case DRM_FORMAT_RGB565:
4862 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004863 *depth = 16;
4864 *bpp = 16;
4865 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004866 case DRM_FORMAT_RGB888:
4867 case DRM_FORMAT_BGR888:
4868 *depth = 24;
4869 *bpp = 24;
4870 break;
4871 case DRM_FORMAT_XRGB8888:
4872 case DRM_FORMAT_XBGR8888:
4873 case DRM_FORMAT_RGBX8888:
4874 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004875 *depth = 24;
4876 *bpp = 32;
4877 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004878 case DRM_FORMAT_XRGB2101010:
4879 case DRM_FORMAT_XBGR2101010:
4880 case DRM_FORMAT_RGBX1010102:
4881 case DRM_FORMAT_BGRX1010102:
4882 case DRM_FORMAT_ARGB2101010:
4883 case DRM_FORMAT_ABGR2101010:
4884 case DRM_FORMAT_RGBA1010102:
4885 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004886 *depth = 30;
4887 *bpp = 32;
4888 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004889 case DRM_FORMAT_ARGB8888:
4890 case DRM_FORMAT_ABGR8888:
4891 case DRM_FORMAT_RGBA8888:
4892 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004893 *depth = 32;
4894 *bpp = 32;
4895 break;
4896 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004897 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4898 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004899 *depth = 0;
4900 *bpp = 0;
4901 break;
4902 }
4903}
4904EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004905
4906/**
4907 * drm_format_num_planes - get the number of planes for format
4908 * @format: pixel format (DRM_FORMAT_*)
4909 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004910 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004911 * The number of planes used by the specified pixel format.
4912 */
4913int drm_format_num_planes(uint32_t format)
4914{
4915 switch (format) {
4916 case DRM_FORMAT_YUV410:
4917 case DRM_FORMAT_YVU410:
4918 case DRM_FORMAT_YUV411:
4919 case DRM_FORMAT_YVU411:
4920 case DRM_FORMAT_YUV420:
4921 case DRM_FORMAT_YVU420:
4922 case DRM_FORMAT_YUV422:
4923 case DRM_FORMAT_YVU422:
4924 case DRM_FORMAT_YUV444:
4925 case DRM_FORMAT_YVU444:
4926 return 3;
4927 case DRM_FORMAT_NV12:
4928 case DRM_FORMAT_NV21:
4929 case DRM_FORMAT_NV16:
4930 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004931 case DRM_FORMAT_NV24:
4932 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004933 return 2;
4934 default:
4935 return 1;
4936 }
4937}
4938EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004939
4940/**
4941 * drm_format_plane_cpp - determine the bytes per pixel value
4942 * @format: pixel format (DRM_FORMAT_*)
4943 * @plane: plane index
4944 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004945 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004946 * The bytes per pixel value for the specified plane.
4947 */
4948int drm_format_plane_cpp(uint32_t format, int plane)
4949{
4950 unsigned int depth;
4951 int bpp;
4952
4953 if (plane >= drm_format_num_planes(format))
4954 return 0;
4955
4956 switch (format) {
4957 case DRM_FORMAT_YUYV:
4958 case DRM_FORMAT_YVYU:
4959 case DRM_FORMAT_UYVY:
4960 case DRM_FORMAT_VYUY:
4961 return 2;
4962 case DRM_FORMAT_NV12:
4963 case DRM_FORMAT_NV21:
4964 case DRM_FORMAT_NV16:
4965 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004966 case DRM_FORMAT_NV24:
4967 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004968 return plane ? 2 : 1;
4969 case DRM_FORMAT_YUV410:
4970 case DRM_FORMAT_YVU410:
4971 case DRM_FORMAT_YUV411:
4972 case DRM_FORMAT_YVU411:
4973 case DRM_FORMAT_YUV420:
4974 case DRM_FORMAT_YVU420:
4975 case DRM_FORMAT_YUV422:
4976 case DRM_FORMAT_YVU422:
4977 case DRM_FORMAT_YUV444:
4978 case DRM_FORMAT_YVU444:
4979 return 1;
4980 default:
4981 drm_fb_get_bpp_depth(format, &depth, &bpp);
4982 return bpp >> 3;
4983 }
4984}
4985EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004986
4987/**
4988 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4989 * @format: pixel format (DRM_FORMAT_*)
4990 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004991 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004992 * The horizontal chroma subsampling factor for the
4993 * specified pixel format.
4994 */
4995int drm_format_horz_chroma_subsampling(uint32_t format)
4996{
4997 switch (format) {
4998 case DRM_FORMAT_YUV411:
4999 case DRM_FORMAT_YVU411:
5000 case DRM_FORMAT_YUV410:
5001 case DRM_FORMAT_YVU410:
5002 return 4;
5003 case DRM_FORMAT_YUYV:
5004 case DRM_FORMAT_YVYU:
5005 case DRM_FORMAT_UYVY:
5006 case DRM_FORMAT_VYUY:
5007 case DRM_FORMAT_NV12:
5008 case DRM_FORMAT_NV21:
5009 case DRM_FORMAT_NV16:
5010 case DRM_FORMAT_NV61:
5011 case DRM_FORMAT_YUV422:
5012 case DRM_FORMAT_YVU422:
5013 case DRM_FORMAT_YUV420:
5014 case DRM_FORMAT_YVU420:
5015 return 2;
5016 default:
5017 return 1;
5018 }
5019}
5020EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5021
5022/**
5023 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5024 * @format: pixel format (DRM_FORMAT_*)
5025 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005026 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005027 * The vertical chroma subsampling factor for the
5028 * specified pixel format.
5029 */
5030int drm_format_vert_chroma_subsampling(uint32_t format)
5031{
5032 switch (format) {
5033 case DRM_FORMAT_YUV410:
5034 case DRM_FORMAT_YVU410:
5035 return 4;
5036 case DRM_FORMAT_YUV420:
5037 case DRM_FORMAT_YVU420:
5038 case DRM_FORMAT_NV12:
5039 case DRM_FORMAT_NV21:
5040 return 2;
5041 default:
5042 return 1;
5043 }
5044}
5045EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005046
5047/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305048 * drm_rotation_simplify() - Try to simplify the rotation
5049 * @rotation: Rotation to be simplified
5050 * @supported_rotations: Supported rotations
5051 *
5052 * Attempt to simplify the rotation to a form that is supported.
5053 * Eg. if the hardware supports everything except DRM_REFLECT_X
5054 * one could call this function like this:
5055 *
5056 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5057 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5058 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5059 *
5060 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5061 * transforms the hardware supports, this function may not
5062 * be able to produce a supported transform, so the caller should
5063 * check the result afterwards.
5064 */
5065unsigned int drm_rotation_simplify(unsigned int rotation,
5066 unsigned int supported_rotations)
5067{
5068 if (rotation & ~supported_rotations) {
5069 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5070 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5071 }
5072
5073 return rotation;
5074}
5075EXPORT_SYMBOL(drm_rotation_simplify);
5076
5077/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005078 * drm_mode_config_init - initialize DRM mode_configuration structure
5079 * @dev: DRM device
5080 *
5081 * Initialize @dev's mode_config structure, used for tracking the graphics
5082 * configuration of @dev.
5083 *
5084 * Since this initializes the modeset locks, no locking is possible. Which is no
5085 * problem, since this should happen single threaded at init time. It is the
5086 * driver's problem to ensure this guarantee.
5087 *
5088 */
5089void drm_mode_config_init(struct drm_device *dev)
5090{
5091 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005092 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005093 mutex_init(&dev->mode_config.idr_mutex);
5094 mutex_init(&dev->mode_config.fb_lock);
5095 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5096 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5097 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04005098 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005099 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5100 INIT_LIST_HEAD(&dev->mode_config.property_list);
5101 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5102 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5103 idr_init(&dev->mode_config.crtc_idr);
5104
5105 drm_modeset_lock_all(dev);
5106 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04005107 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005108 drm_modeset_unlock_all(dev);
5109
5110 /* Just to be sure */
5111 dev->mode_config.num_fb = 0;
5112 dev->mode_config.num_connector = 0;
5113 dev->mode_config.num_crtc = 0;
5114 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005115 dev->mode_config.num_overlay_plane = 0;
5116 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005117}
5118EXPORT_SYMBOL(drm_mode_config_init);
5119
5120/**
5121 * drm_mode_config_cleanup - free up DRM mode_config info
5122 * @dev: DRM device
5123 *
5124 * Free up all the connectors and CRTCs associated with this DRM device, then
5125 * free up the framebuffers and associated buffer objects.
5126 *
5127 * Note that since this /should/ happen single-threaded at driver/device
5128 * teardown time, no locking is required. It's the driver's job to ensure that
5129 * this guarantee actually holds true.
5130 *
5131 * FIXME: cleanup any dangling user buffer objects too
5132 */
5133void drm_mode_config_cleanup(struct drm_device *dev)
5134{
5135 struct drm_connector *connector, *ot;
5136 struct drm_crtc *crtc, *ct;
5137 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04005138 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005139 struct drm_framebuffer *fb, *fbt;
5140 struct drm_property *property, *pt;
5141 struct drm_property_blob *blob, *bt;
5142 struct drm_plane *plane, *plt;
5143
5144 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5145 head) {
5146 encoder->funcs->destroy(encoder);
5147 }
5148
Sean Paul3b336ec2013-08-14 16:47:37 -04005149 list_for_each_entry_safe(bridge, brt,
5150 &dev->mode_config.bridge_list, head) {
5151 bridge->funcs->destroy(bridge);
5152 }
5153
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005154 list_for_each_entry_safe(connector, ot,
5155 &dev->mode_config.connector_list, head) {
5156 connector->funcs->destroy(connector);
5157 }
5158
5159 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5160 head) {
5161 drm_property_destroy(dev, property);
5162 }
5163
5164 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5165 head) {
5166 drm_property_destroy_blob(dev, blob);
5167 }
5168
5169 /*
5170 * Single-threaded teardown context, so it's not required to grab the
5171 * fb_lock to protect against concurrent fb_list access. Contrary, it
5172 * would actually deadlock with the drm_framebuffer_cleanup function.
5173 *
5174 * Also, if there are any framebuffers left, that's a driver leak now,
5175 * so politely WARN about this.
5176 */
5177 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5178 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5179 drm_framebuffer_remove(fb);
5180 }
5181
5182 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5183 head) {
5184 plane->funcs->destroy(plane);
5185 }
5186
5187 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5188 crtc->funcs->destroy(crtc);
5189 }
5190
5191 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005192 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005193}
5194EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305195
5196struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5197 unsigned int supported_rotations)
5198{
5199 static const struct drm_prop_enum_list props[] = {
5200 { DRM_ROTATE_0, "rotate-0" },
5201 { DRM_ROTATE_90, "rotate-90" },
5202 { DRM_ROTATE_180, "rotate-180" },
5203 { DRM_ROTATE_270, "rotate-270" },
5204 { DRM_REFLECT_X, "reflect-x" },
5205 { DRM_REFLECT_Y, "reflect-y" },
5206 };
5207
5208 return drm_property_create_bitmask(dev, 0, "rotation",
5209 props, ARRAY_SIZE(props),
5210 supported_rotations);
5211}
5212EXPORT_SYMBOL(drm_mode_create_rotation_property);