blob: fa2be249999c70711e1b19cbe0df92fd5e081631 [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
Daniel Vetter2b677e82012-12-10 21:16:05 +0100518 /* Grab the idr reference. */
519 drm_framebuffer_reference(fb);
520
Dave Airlief453ba02008-11-07 14:05:41 -0800521 dev->mode_config.num_fb++;
522 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100523out:
524 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800525
526 return 0;
527}
528EXPORT_SYMBOL(drm_framebuffer_init);
529
Rob Clarkf7eff602012-09-05 21:48:38 +0000530static void drm_framebuffer_free(struct kref *kref)
531{
532 struct drm_framebuffer *fb =
533 container_of(kref, struct drm_framebuffer, refcount);
534 fb->funcs->destroy(fb);
535}
536
Daniel Vetter2b677e82012-12-10 21:16:05 +0100537static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
538 uint32_t id)
539{
540 struct drm_mode_object *obj = NULL;
541 struct drm_framebuffer *fb;
542
543 mutex_lock(&dev->mode_config.idr_mutex);
544 obj = idr_find(&dev->mode_config.crtc_idr, id);
545 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
546 fb = NULL;
547 else
548 fb = obj_to_fb(obj);
549 mutex_unlock(&dev->mode_config.idr_mutex);
550
551 return fb;
552}
553
Rob Clarkf7eff602012-09-05 21:48:38 +0000554/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100555 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
556 * @dev: drm device
557 * @id: id of the fb object
558 *
559 * If successful, this grabs an additional reference to the framebuffer -
560 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100561 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100562 */
563struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
564 uint32_t id)
565{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100566 struct drm_framebuffer *fb;
567
568 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100569 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100570 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000571 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100572 mutex_unlock(&dev->mode_config.fb_lock);
573
574 return fb;
575}
576EXPORT_SYMBOL(drm_framebuffer_lookup);
577
578/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000579 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100580 * @fb: framebuffer to unref
581 *
582 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000583 */
584void drm_framebuffer_unreference(struct drm_framebuffer *fb)
585{
Rob Clark82912722014-03-18 10:07:08 -0400586 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000587 kref_put(&fb->refcount, drm_framebuffer_free);
588}
589EXPORT_SYMBOL(drm_framebuffer_unreference);
590
591/**
592 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100593 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100594 *
595 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000596 */
597void drm_framebuffer_reference(struct drm_framebuffer *fb)
598{
Rob Clark82912722014-03-18 10:07:08 -0400599 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000600 kref_get(&fb->refcount);
601}
602EXPORT_SYMBOL(drm_framebuffer_reference);
603
Daniel Vetter2b677e82012-12-10 21:16:05 +0100604static void drm_framebuffer_free_bug(struct kref *kref)
605{
606 BUG();
607}
608
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100609static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
610{
Rob Clark82912722014-03-18 10:07:08 -0400611 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100612 kref_put(&fb->refcount, drm_framebuffer_free_bug);
613}
614
Daniel Vetter2b677e82012-12-10 21:16:05 +0100615/* dev->mode_config.fb_lock must be held! */
616static void __drm_framebuffer_unregister(struct drm_device *dev,
617 struct drm_framebuffer *fb)
618{
619 mutex_lock(&dev->mode_config.idr_mutex);
620 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
621 mutex_unlock(&dev->mode_config.idr_mutex);
622
623 fb->base.id = 0;
624
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100625 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100626}
627
Dave Airlief453ba02008-11-07 14:05:41 -0800628/**
Daniel Vetter36206362012-12-10 20:42:17 +0100629 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
630 * @fb: fb to unregister
631 *
632 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
633 * those used for fbdev. Note that the caller must hold a reference of it's own,
634 * i.e. the object may not be destroyed through this call (since it'll lead to a
635 * locking inversion).
636 */
637void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
638{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100639 struct drm_device *dev = fb->dev;
640
641 mutex_lock(&dev->mode_config.fb_lock);
642 /* Mark fb as reaped and drop idr ref. */
643 __drm_framebuffer_unregister(dev, fb);
644 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100645}
646EXPORT_SYMBOL(drm_framebuffer_unregister_private);
647
648/**
Dave Airlief453ba02008-11-07 14:05:41 -0800649 * drm_framebuffer_cleanup - remove a framebuffer object
650 * @fb: framebuffer to remove
651 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100652 * Cleanup framebuffer. This function is intended to be used from the drivers
653 * ->destroy callback. It can also be used to clean up driver private
654 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100655 *
656 * Note that this function does not remove the fb from active usuage - if it is
657 * still used anywhere, hilarity can ensue since userspace could call getfb on
658 * the id and get back -EINVAL. Obviously no concern at driver unload time.
659 *
660 * Also, the framebuffer will not be removed from the lookup idr - for
661 * user-created framebuffers this will happen in in the rmfb ioctl. For
662 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
663 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800664 */
665void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
666{
667 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100668
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100669 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000670 list_del(&fb->head);
671 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100672 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000673}
674EXPORT_SYMBOL(drm_framebuffer_cleanup);
675
676/**
677 * drm_framebuffer_remove - remove and unreference a framebuffer object
678 * @fb: framebuffer to remove
679 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000680 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100681 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100682 * passed-in framebuffer. Might take the modeset locks.
683 *
684 * Note that this function optimizes the cleanup away if the caller holds the
685 * last reference to the framebuffer. It is also guaranteed to not take the
686 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000687 */
688void drm_framebuffer_remove(struct drm_framebuffer *fb)
689{
690 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800691 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800692 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000693 struct drm_mode_set set;
694 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800695
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100696 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100697
Daniel Vetterb62584e2012-12-11 16:51:35 +0100698 /*
699 * drm ABI mandates that we remove any deleted framebuffers from active
700 * useage. But since most sane clients only remove framebuffers they no
701 * longer need, try to optimize this away.
702 *
703 * Since we're holding a reference ourselves, observing a refcount of 1
704 * means that we're the last holder and can skip it. Also, the refcount
705 * can never increase from 1 again, so we don't need any barriers or
706 * locks.
707 *
708 * Note that userspace could try to race with use and instate a new
709 * usage _after_ we've cleared all current ones. End result will be an
710 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
711 * in this manner.
712 */
713 if (atomic_read(&fb->refcount.refcount) > 1) {
714 drm_modeset_lock_all(dev);
715 /* remove from any CRTC */
716 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700717 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100718 /* should turn off the crtc */
719 memset(&set, 0, sizeof(struct drm_mode_set));
720 set.crtc = crtc;
721 set.fb = NULL;
722 ret = drm_mode_set_config_internal(&set);
723 if (ret)
724 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
725 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000726 }
Dave Airlief453ba02008-11-07 14:05:41 -0800727
Daniel Vetterb62584e2012-12-11 16:51:35 +0100728 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300729 if (plane->fb == fb)
730 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800731 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100732 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800733 }
734
Rob Clarkf7eff602012-09-05 21:48:38 +0000735 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800736}
Rob Clarkf7eff602012-09-05 21:48:38 +0000737EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800738
Rob Clark51fd3712013-11-19 12:10:12 -0500739DEFINE_WW_CLASS(crtc_ww_class);
740
Dave Airlief453ba02008-11-07 14:05:41 -0800741/**
Matt Ropere13161a2014-04-01 15:22:38 -0700742 * drm_crtc_init_with_planes - Initialise a new CRTC object with
743 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800744 * @dev: DRM device
745 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700746 * @primary: Primary plane for CRTC
747 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800748 * @funcs: callbacks for the new CRTC
749 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000750 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200751 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100752 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200753 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800754 */
Matt Ropere13161a2014-04-01 15:22:38 -0700755int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
756 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700757 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700758 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800759{
Rob Clark51fd3712013-11-19 12:10:12 -0500760 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200761 int ret;
762
Dave Airlief453ba02008-11-07 14:05:41 -0800763 crtc->dev = dev;
764 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000765 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800766
Daniel Vetter84849902012-12-02 00:28:11 +0100767 drm_modeset_lock_all(dev);
Rob Clark51fd3712013-11-19 12:10:12 -0500768 drm_modeset_lock_init(&crtc->mutex);
769 /* dropped by _unlock_all(): */
770 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200771
772 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
773 if (ret)
774 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800775
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300776 crtc->base.properties = &crtc->properties;
777
Rob Clark51fd3712013-11-19 12:10:12 -0500778 list_add_tail(&crtc->head, &config->crtc_list);
779 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200780
Matt Ropere13161a2014-04-01 15:22:38 -0700781 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700782 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700783 if (primary)
784 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700785 if (cursor)
786 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700787
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200788 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100789 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200790
791 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800792}
Matt Ropere13161a2014-04-01 15:22:38 -0700793EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800794
795/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000796 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800797 * @crtc: CRTC to cleanup
798 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000799 * This function cleans up @crtc and removes it from the DRM mode setting
800 * core. Note that the function does *not* free the crtc structure itself,
801 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800802 */
803void drm_crtc_cleanup(struct drm_crtc *crtc)
804{
805 struct drm_device *dev = crtc->dev;
806
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000807 kfree(crtc->gamma_store);
808 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800809
Rob Clark51fd3712013-11-19 12:10:12 -0500810 drm_modeset_lock_fini(&crtc->mutex);
811
Dave Airlief453ba02008-11-07 14:05:41 -0800812 drm_mode_object_put(dev, &crtc->base);
813 list_del(&crtc->head);
814 dev->mode_config.num_crtc--;
815}
816EXPORT_SYMBOL(drm_crtc_cleanup);
817
818/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000819 * drm_crtc_index - find the index of a registered CRTC
820 * @crtc: CRTC to find index for
821 *
822 * Given a registered CRTC, return the index of that CRTC within a DRM
823 * device's list of CRTCs.
824 */
825unsigned int drm_crtc_index(struct drm_crtc *crtc)
826{
827 unsigned int index = 0;
828 struct drm_crtc *tmp;
829
830 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
831 if (tmp == crtc)
832 return index;
833
834 index++;
835 }
836
837 BUG();
838}
839EXPORT_SYMBOL(drm_crtc_index);
840
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100841/*
Dave Airlief453ba02008-11-07 14:05:41 -0800842 * drm_mode_remove - remove and free a mode
843 * @connector: connector list to modify
844 * @mode: mode to remove
845 *
Dave Airlief453ba02008-11-07 14:05:41 -0800846 * Remove @mode from @connector's mode list, then free it.
847 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100848static void drm_mode_remove(struct drm_connector *connector,
849 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800850{
851 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100852 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800853}
Dave Airlief453ba02008-11-07 14:05:41 -0800854
855/**
856 * drm_connector_init - Init a preallocated connector
857 * @dev: DRM device
858 * @connector: the connector to init
859 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100860 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800861 *
Dave Airlief453ba02008-11-07 14:05:41 -0800862 * Initialises a preallocated connector. Connectors should be
863 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200864 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100865 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200866 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800867 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200868int drm_connector_init(struct drm_device *dev,
869 struct drm_connector *connector,
870 const struct drm_connector_funcs *funcs,
871 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800872{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200873 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400874 struct ida *connector_ida =
875 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200876
Daniel Vetter84849902012-12-02 00:28:11 +0100877 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800878
Dave Airlie2ee39452014-07-24 11:53:45 +1000879 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200880 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300881 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200882
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300883 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800884 connector->dev = dev;
885 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800886 connector->connector_type = connector_type;
887 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400888 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
889 if (connector->connector_type_id < 0) {
890 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300891 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400892 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300893 connector->name =
894 kasprintf(GFP_KERNEL, "%s-%d",
895 drm_connector_enum_list[connector_type].name,
896 connector->connector_type_id);
897 if (!connector->name) {
898 ret = -ENOMEM;
899 goto out_put;
900 }
901
Dave Airlief453ba02008-11-07 14:05:41 -0800902 INIT_LIST_HEAD(&connector->probed_modes);
903 INIT_LIST_HEAD(&connector->modes);
904 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000905 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800906
907 list_add_tail(&connector->head, &dev->mode_config.connector_list);
908 dev->mode_config.num_connector++;
909
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200910 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500911 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200912 dev->mode_config.edid_property,
913 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800914
Rob Clark58495562012-10-11 20:50:56 -0500915 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800916 dev->mode_config.dpms_property, 0);
917
Thomas Wood30f65702014-06-18 17:52:32 +0100918 connector->debugfs_entry = NULL;
919
Jani Nikula2abdd312014-05-14 16:58:19 +0300920out_put:
921 if (ret)
922 drm_mode_object_put(dev, &connector->base);
923
924out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100925 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200926
927 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800928}
929EXPORT_SYMBOL(drm_connector_init);
930
931/**
932 * drm_connector_cleanup - cleans up an initialised connector
933 * @connector: connector to cleanup
934 *
Dave Airlief453ba02008-11-07 14:05:41 -0800935 * Cleans up the connector but doesn't free the object.
936 */
937void drm_connector_cleanup(struct drm_connector *connector)
938{
939 struct drm_device *dev = connector->dev;
940 struct drm_display_mode *mode, *t;
941
942 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
943 drm_mode_remove(connector, mode);
944
945 list_for_each_entry_safe(mode, t, &connector->modes, head)
946 drm_mode_remove(connector, mode);
947
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400948 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
949 connector->connector_type_id);
950
Dave Airlief453ba02008-11-07 14:05:41 -0800951 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300952 kfree(connector->name);
953 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800954 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000955 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800956}
957EXPORT_SYMBOL(drm_connector_cleanup);
958
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100959/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100960 * drm_connector_register - register a connector
961 * @connector: the connector to register
962 *
963 * Register userspace interfaces for a connector
964 *
965 * Returns:
966 * Zero on success, error code on failure.
967 */
968int drm_connector_register(struct drm_connector *connector)
969{
Thomas Wood30f65702014-06-18 17:52:32 +0100970 int ret;
971
Dave Airlie2ee39452014-07-24 11:53:45 +1000972 drm_mode_object_register(connector->dev, &connector->base);
973
Thomas Wood30f65702014-06-18 17:52:32 +0100974 ret = drm_sysfs_connector_add(connector);
975 if (ret)
976 return ret;
977
978 ret = drm_debugfs_connector_add(connector);
979 if (ret) {
980 drm_sysfs_connector_remove(connector);
981 return ret;
982 }
983
984 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100985}
986EXPORT_SYMBOL(drm_connector_register);
987
988/**
989 * drm_connector_unregister - unregister a connector
990 * @connector: the connector to unregister
991 *
992 * Unregister userspace interfaces for a connector
993 */
994void drm_connector_unregister(struct drm_connector *connector)
995{
996 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +0100997 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100998}
999EXPORT_SYMBOL(drm_connector_unregister);
1000
1001
1002/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001003 * drm_connector_unplug_all - unregister connector userspace interfaces
1004 * @dev: drm device
1005 *
1006 * This function unregisters all connector userspace interfaces in sysfs. Should
1007 * be call when the device is disconnected, e.g. from an usb driver's
1008 * ->disconnect callback.
1009 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001010void drm_connector_unplug_all(struct drm_device *dev)
1011{
1012 struct drm_connector *connector;
1013
1014 /* taking the mode config mutex ends up in a clash with sysfs */
1015 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001016 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001017
1018}
1019EXPORT_SYMBOL(drm_connector_unplug_all);
1020
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001021/**
1022 * drm_bridge_init - initialize a drm transcoder/bridge
1023 * @dev: drm device
1024 * @bridge: transcoder/bridge to set up
1025 * @funcs: bridge function table
1026 *
1027 * Initialises a preallocated bridge. Bridges should be
1028 * subclassed as part of driver connector objects.
1029 *
1030 * Returns:
1031 * Zero on success, error code on failure.
1032 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001033int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1034 const struct drm_bridge_funcs *funcs)
1035{
1036 int ret;
1037
1038 drm_modeset_lock_all(dev);
1039
1040 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1041 if (ret)
1042 goto out;
1043
1044 bridge->dev = dev;
1045 bridge->funcs = funcs;
1046
1047 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1048 dev->mode_config.num_bridge++;
1049
1050 out:
1051 drm_modeset_unlock_all(dev);
1052 return ret;
1053}
1054EXPORT_SYMBOL(drm_bridge_init);
1055
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001056/**
1057 * drm_bridge_cleanup - cleans up an initialised bridge
1058 * @bridge: bridge to cleanup
1059 *
1060 * Cleans up the bridge but doesn't free the object.
1061 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001062void drm_bridge_cleanup(struct drm_bridge *bridge)
1063{
1064 struct drm_device *dev = bridge->dev;
1065
1066 drm_modeset_lock_all(dev);
1067 drm_mode_object_put(dev, &bridge->base);
1068 list_del(&bridge->head);
1069 dev->mode_config.num_bridge--;
1070 drm_modeset_unlock_all(dev);
1071}
1072EXPORT_SYMBOL(drm_bridge_cleanup);
1073
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001074/**
1075 * drm_encoder_init - Init a preallocated encoder
1076 * @dev: drm device
1077 * @encoder: the encoder to init
1078 * @funcs: callbacks for this encoder
1079 * @encoder_type: user visible type of the encoder
1080 *
1081 * Initialises a preallocated encoder. Encoder should be
1082 * subclassed as part of driver encoder objects.
1083 *
1084 * Returns:
1085 * Zero on success, error code on failure.
1086 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001087int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001088 struct drm_encoder *encoder,
1089 const struct drm_encoder_funcs *funcs,
1090 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001091{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001092 int ret;
1093
Daniel Vetter84849902012-12-02 00:28:11 +01001094 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001095
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001096 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1097 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001098 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001099
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001100 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001101 encoder->encoder_type = encoder_type;
1102 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001103 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1104 drm_encoder_enum_list[encoder_type].name,
1105 encoder->base.id);
1106 if (!encoder->name) {
1107 ret = -ENOMEM;
1108 goto out_put;
1109 }
Dave Airlief453ba02008-11-07 14:05:41 -08001110
1111 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1112 dev->mode_config.num_encoder++;
1113
Jani Nikulae5748942014-05-14 16:58:20 +03001114out_put:
1115 if (ret)
1116 drm_mode_object_put(dev, &encoder->base);
1117
1118out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001119 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001120
1121 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001122}
1123EXPORT_SYMBOL(drm_encoder_init);
1124
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001125/**
1126 * drm_encoder_cleanup - cleans up an initialised encoder
1127 * @encoder: encoder to cleanup
1128 *
1129 * Cleans up the encoder but doesn't free the object.
1130 */
Dave Airlief453ba02008-11-07 14:05:41 -08001131void drm_encoder_cleanup(struct drm_encoder *encoder)
1132{
1133 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001134 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001135 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001136 kfree(encoder->name);
1137 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001138 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001139 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001140 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001141}
1142EXPORT_SYMBOL(drm_encoder_cleanup);
1143
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001144/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001145 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001146 * @dev: DRM device
1147 * @plane: plane object to init
1148 * @possible_crtcs: bitmask of possible CRTCs
1149 * @funcs: callbacks for the new plane
1150 * @formats: array of supported formats (%DRM_FORMAT_*)
1151 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001152 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001153 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001154 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001155 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001156 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001157 * Zero on success, error code on failure.
1158 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001159int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1160 unsigned long possible_crtcs,
1161 const struct drm_plane_funcs *funcs,
1162 const uint32_t *formats, uint32_t format_count,
1163 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001164{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001165 int ret;
1166
Daniel Vetter84849902012-12-02 00:28:11 +01001167 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001168
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001169 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1170 if (ret)
1171 goto out;
1172
Rob Clark4d939142012-05-17 02:23:27 -06001173 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001174 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001175 plane->funcs = funcs;
1176 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1177 GFP_KERNEL);
1178 if (!plane->format_types) {
1179 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1180 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001181 ret = -ENOMEM;
1182 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001183 }
1184
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001185 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001186 plane->format_count = format_count;
1187 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001188 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001189
Matt Roperdc415ff2014-04-01 15:22:36 -07001190 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1191 dev->mode_config.num_total_plane++;
1192 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1193 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001194
Rob Clark9922ab52014-04-01 20:16:57 -04001195 drm_object_attach_property(&plane->base,
1196 dev->mode_config.plane_type_property,
1197 plane->type);
1198
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001199 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001200 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001201
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001202 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001203}
Matt Roperdc415ff2014-04-01 15:22:36 -07001204EXPORT_SYMBOL(drm_universal_plane_init);
1205
1206/**
1207 * drm_plane_init - Initialize a legacy plane
1208 * @dev: DRM device
1209 * @plane: plane object to init
1210 * @possible_crtcs: bitmask of possible CRTCs
1211 * @funcs: callbacks for the new plane
1212 * @formats: array of supported formats (%DRM_FORMAT_*)
1213 * @format_count: number of elements in @formats
1214 * @is_primary: plane type (primary vs overlay)
1215 *
1216 * Legacy API to initialize a DRM plane.
1217 *
1218 * New drivers should call drm_universal_plane_init() instead.
1219 *
1220 * Returns:
1221 * Zero on success, error code on failure.
1222 */
1223int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1224 unsigned long possible_crtcs,
1225 const struct drm_plane_funcs *funcs,
1226 const uint32_t *formats, uint32_t format_count,
1227 bool is_primary)
1228{
1229 enum drm_plane_type type;
1230
1231 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1232 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1233 formats, format_count, type);
1234}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001235EXPORT_SYMBOL(drm_plane_init);
1236
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001237/**
1238 * drm_plane_cleanup - Clean up the core plane usage
1239 * @plane: plane to cleanup
1240 *
1241 * This function cleans up @plane and removes it from the DRM mode setting
1242 * core. Note that the function does *not* free the plane structure itself,
1243 * this is the responsibility of the caller.
1244 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001245void drm_plane_cleanup(struct drm_plane *plane)
1246{
1247 struct drm_device *dev = plane->dev;
1248
Daniel Vetter84849902012-12-02 00:28:11 +01001249 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001250 kfree(plane->format_types);
1251 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001252
1253 BUG_ON(list_empty(&plane->head));
1254
1255 list_del(&plane->head);
1256 dev->mode_config.num_total_plane--;
1257 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1258 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001259 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001260}
1261EXPORT_SYMBOL(drm_plane_cleanup);
1262
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001263/**
1264 * drm_plane_force_disable - Forcibly disable a plane
1265 * @plane: plane to disable
1266 *
1267 * Forces the plane to be disabled.
1268 *
1269 * Used when the plane's current framebuffer is destroyed,
1270 * and when restoring fbdev mode.
1271 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001272void drm_plane_force_disable(struct drm_plane *plane)
1273{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001274 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001275 int ret;
1276
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001277 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001278 return;
1279
1280 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001281 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001282 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001283 return;
1284 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001285 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001286 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001287 plane->fb = NULL;
1288 plane->crtc = NULL;
1289}
1290EXPORT_SYMBOL(drm_plane_force_disable);
1291
Dave Airlief453ba02008-11-07 14:05:41 -08001292static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1293{
1294 struct drm_property *edid;
1295 struct drm_property *dpms;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001296 struct drm_property *dev_path;
Dave Airlief453ba02008-11-07 14:05:41 -08001297
1298 /*
1299 * Standard properties (apply to all connectors)
1300 */
1301 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1302 DRM_MODE_PROP_IMMUTABLE,
1303 "EDID", 0);
1304 dev->mode_config.edid_property = edid;
1305
Sascha Hauer4a67d392012-02-06 10:58:17 +01001306 dpms = drm_property_create_enum(dev, 0,
1307 "DPMS", drm_dpms_enum_list,
1308 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001309 dev->mode_config.dpms_property = dpms;
1310
Dave Airlie43aba7e2014-06-05 14:01:31 +10001311 dev_path = drm_property_create(dev,
1312 DRM_MODE_PROP_BLOB |
1313 DRM_MODE_PROP_IMMUTABLE,
1314 "PATH", 0);
1315 dev->mode_config.path_property = dev_path;
1316
Dave Airlief453ba02008-11-07 14:05:41 -08001317 return 0;
1318}
1319
Rob Clark9922ab52014-04-01 20:16:57 -04001320static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1321{
1322 struct drm_property *type;
1323
1324 /*
1325 * Standard properties (apply to all planes)
1326 */
1327 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1328 "type", drm_plane_type_enum_list,
1329 ARRAY_SIZE(drm_plane_type_enum_list));
1330 dev->mode_config.plane_type_property = type;
1331
1332 return 0;
1333}
1334
Dave Airlief453ba02008-11-07 14:05:41 -08001335/**
1336 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1337 * @dev: DRM device
1338 *
1339 * Called by a driver the first time a DVI-I connector is made.
1340 */
1341int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1342{
1343 struct drm_property *dvi_i_selector;
1344 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001345
1346 if (dev->mode_config.dvi_i_select_subconnector_property)
1347 return 0;
1348
1349 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001350 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001351 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001352 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001353 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001354 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1355
Sascha Hauer4a67d392012-02-06 10:58:17 +01001356 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001357 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001358 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001359 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001360 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1361
1362 return 0;
1363}
1364EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1365
1366/**
1367 * drm_create_tv_properties - create TV specific connector properties
1368 * @dev: DRM device
1369 * @num_modes: number of different TV formats (modes) supported
1370 * @modes: array of pointers to strings containing name of each format
1371 *
1372 * Called by a driver's TV initialization routine, this function creates
1373 * the TV specific connector properties for a given device. Caller is
1374 * responsible for allocating a list of format names and passing them to
1375 * this routine.
1376 */
1377int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1378 char *modes[])
1379{
1380 struct drm_property *tv_selector;
1381 struct drm_property *tv_subconnector;
1382 int i;
1383
1384 if (dev->mode_config.tv_select_subconnector_property)
1385 return 0;
1386
1387 /*
1388 * Basic connector properties
1389 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001390 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001391 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001392 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001393 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001394 dev->mode_config.tv_select_subconnector_property = tv_selector;
1395
1396 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001397 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1398 "subconnector",
1399 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001400 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001401 dev->mode_config.tv_subconnector_property = tv_subconnector;
1402
1403 /*
1404 * Other, TV specific properties: margins & TV modes.
1405 */
1406 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001407 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001408
1409 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001410 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001411
1412 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001413 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001414
1415 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001416 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001417
1418 dev->mode_config.tv_mode_property =
1419 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1420 "mode", num_modes);
1421 for (i = 0; i < num_modes; i++)
1422 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1423 i, modes[i]);
1424
Francisco Jerezb6b79022009-08-02 04:19:20 +02001425 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001426 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001427
1428 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001429 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001430
1431 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001432 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001433
Francisco Jereza75f0232009-08-12 02:30:10 +02001434 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001435 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001436
1437 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001438 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001439
1440 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001441 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001442
Dave Airlief453ba02008-11-07 14:05:41 -08001443 return 0;
1444}
1445EXPORT_SYMBOL(drm_mode_create_tv_properties);
1446
1447/**
1448 * drm_mode_create_scaling_mode_property - create scaling mode property
1449 * @dev: DRM device
1450 *
1451 * Called by a driver the first time it's needed, must be attached to desired
1452 * connectors.
1453 */
1454int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1455{
1456 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001457
1458 if (dev->mode_config.scaling_mode_property)
1459 return 0;
1460
1461 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001462 drm_property_create_enum(dev, 0, "scaling mode",
1463 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001464 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001465
1466 dev->mode_config.scaling_mode_property = scaling_mode;
1467
1468 return 0;
1469}
1470EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1471
1472/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301473 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1474 * @dev: DRM device
1475 *
1476 * Called by a driver the first time it's needed, must be attached to desired
1477 * connectors.
1478 *
1479 * Returns:
1480 * Zero on success, errno on failure.
1481 */
1482int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1483{
1484 if (dev->mode_config.aspect_ratio_property)
1485 return 0;
1486
1487 dev->mode_config.aspect_ratio_property =
1488 drm_property_create_enum(dev, 0, "aspect ratio",
1489 drm_aspect_ratio_enum_list,
1490 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1491
1492 if (dev->mode_config.aspect_ratio_property == NULL)
1493 return -ENOMEM;
1494
1495 return 0;
1496}
1497EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1498
1499/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001500 * drm_mode_create_dirty_property - create dirty property
1501 * @dev: DRM device
1502 *
1503 * Called by a driver the first time it's needed, must be attached to desired
1504 * connectors.
1505 */
1506int drm_mode_create_dirty_info_property(struct drm_device *dev)
1507{
1508 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001509
1510 if (dev->mode_config.dirty_info_property)
1511 return 0;
1512
1513 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001514 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001515 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001516 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001517 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001518 dev->mode_config.dirty_info_property = dirty_info;
1519
1520 return 0;
1521}
1522EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1523
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001524static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001525{
1526 uint32_t total_objects = 0;
1527
1528 total_objects += dev->mode_config.num_crtc;
1529 total_objects += dev->mode_config.num_connector;
1530 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001531 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001532
Dave Airlief453ba02008-11-07 14:05:41 -08001533 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1534 if (!group->id_list)
1535 return -ENOMEM;
1536
1537 group->num_crtcs = 0;
1538 group->num_connectors = 0;
1539 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001540 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001541 return 0;
1542}
1543
Dave Airliead222792014-05-02 13:22:19 +10001544void drm_mode_group_destroy(struct drm_mode_group *group)
1545{
1546 kfree(group->id_list);
1547 group->id_list = NULL;
1548}
1549
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001550/*
1551 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1552 * the drm core's responsibility to set up mode control groups.
1553 */
Dave Airlief453ba02008-11-07 14:05:41 -08001554int drm_mode_group_init_legacy_group(struct drm_device *dev,
1555 struct drm_mode_group *group)
1556{
1557 struct drm_crtc *crtc;
1558 struct drm_encoder *encoder;
1559 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001560 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001561 int ret;
1562
1563 if ((ret = drm_mode_group_init(dev, group)))
1564 return ret;
1565
1566 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1567 group->id_list[group->num_crtcs++] = crtc->base.id;
1568
1569 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1570 group->id_list[group->num_crtcs + group->num_encoders++] =
1571 encoder->base.id;
1572
1573 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1574 group->id_list[group->num_crtcs + group->num_encoders +
1575 group->num_connectors++] = connector->base.id;
1576
Sean Paul3b336ec2013-08-14 16:47:37 -04001577 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1578 group->id_list[group->num_crtcs + group->num_encoders +
1579 group->num_connectors + group->num_bridges++] =
1580 bridge->base.id;
1581
Dave Airlief453ba02008-11-07 14:05:41 -08001582 return 0;
1583}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001584EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001585
Dave Airlie2390cd12014-06-05 14:01:29 +10001586void drm_reinit_primary_mode_group(struct drm_device *dev)
1587{
1588 drm_modeset_lock_all(dev);
1589 drm_mode_group_destroy(&dev->primary->mode_group);
1590 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1591 drm_modeset_unlock_all(dev);
1592}
1593EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1594
Dave Airlief453ba02008-11-07 14:05:41 -08001595/**
Dave Airlief453ba02008-11-07 14:05:41 -08001596 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1597 * @out: drm_mode_modeinfo struct to return to the user
1598 * @in: drm_display_mode to use
1599 *
Dave Airlief453ba02008-11-07 14:05:41 -08001600 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1601 * the user.
1602 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001603static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1604 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001605{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001606 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1607 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1608 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1609 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1610 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1611 "timing values too large for mode info\n");
1612
Dave Airlief453ba02008-11-07 14:05:41 -08001613 out->clock = in->clock;
1614 out->hdisplay = in->hdisplay;
1615 out->hsync_start = in->hsync_start;
1616 out->hsync_end = in->hsync_end;
1617 out->htotal = in->htotal;
1618 out->hskew = in->hskew;
1619 out->vdisplay = in->vdisplay;
1620 out->vsync_start = in->vsync_start;
1621 out->vsync_end = in->vsync_end;
1622 out->vtotal = in->vtotal;
1623 out->vscan = in->vscan;
1624 out->vrefresh = in->vrefresh;
1625 out->flags = in->flags;
1626 out->type = in->type;
1627 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1628 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1629}
1630
1631/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001632 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001633 * @out: drm_display_mode to return to the user
1634 * @in: drm_mode_modeinfo to use
1635 *
Dave Airlief453ba02008-11-07 14:05:41 -08001636 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1637 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001638 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001639 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001640 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001641 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001642static int drm_crtc_convert_umode(struct drm_display_mode *out,
1643 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001644{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001645 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1646 return -ERANGE;
1647
Damien Lespiau5848ad42013-09-27 12:11:50 +01001648 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1649 return -EINVAL;
1650
Dave Airlief453ba02008-11-07 14:05:41 -08001651 out->clock = in->clock;
1652 out->hdisplay = in->hdisplay;
1653 out->hsync_start = in->hsync_start;
1654 out->hsync_end = in->hsync_end;
1655 out->htotal = in->htotal;
1656 out->hskew = in->hskew;
1657 out->vdisplay = in->vdisplay;
1658 out->vsync_start = in->vsync_start;
1659 out->vsync_end = in->vsync_end;
1660 out->vtotal = in->vtotal;
1661 out->vscan = in->vscan;
1662 out->vrefresh = in->vrefresh;
1663 out->flags = in->flags;
1664 out->type = in->type;
1665 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1666 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001667
1668 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001669}
1670
1671/**
1672 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001673 * @dev: drm device for the ioctl
1674 * @data: data pointer for the ioctl
1675 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001676 *
Dave Airlief453ba02008-11-07 14:05:41 -08001677 * Construct a set of configuration description structures and return
1678 * them to the user, including CRTC, connector and framebuffer configuration.
1679 *
1680 * Called by the user via ioctl.
1681 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001682 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001683 * Zero on success, errno on failure.
1684 */
1685int drm_mode_getresources(struct drm_device *dev, void *data,
1686 struct drm_file *file_priv)
1687{
1688 struct drm_mode_card_res *card_res = data;
1689 struct list_head *lh;
1690 struct drm_framebuffer *fb;
1691 struct drm_connector *connector;
1692 struct drm_crtc *crtc;
1693 struct drm_encoder *encoder;
1694 int ret = 0;
1695 int connector_count = 0;
1696 int crtc_count = 0;
1697 int fb_count = 0;
1698 int encoder_count = 0;
1699 int copied = 0, i;
1700 uint32_t __user *fb_id;
1701 uint32_t __user *crtc_id;
1702 uint32_t __user *connector_id;
1703 uint32_t __user *encoder_id;
1704 struct drm_mode_group *mode_group;
1705
Dave Airliefb3b06c2011-02-08 13:55:21 +10001706 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1707 return -EINVAL;
1708
Dave Airlief453ba02008-11-07 14:05:41 -08001709
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001710 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001711 /*
1712 * For the non-control nodes we need to limit the list of resources
1713 * by IDs in the group list for this node
1714 */
1715 list_for_each(lh, &file_priv->fbs)
1716 fb_count++;
1717
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001718 /* handle this in 4 parts */
1719 /* FBs */
1720 if (card_res->count_fbs >= fb_count) {
1721 copied = 0;
1722 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1723 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1724 if (put_user(fb->base.id, fb_id + copied)) {
1725 mutex_unlock(&file_priv->fbs_lock);
1726 return -EFAULT;
1727 }
1728 copied++;
1729 }
1730 }
1731 card_res->count_fbs = fb_count;
1732 mutex_unlock(&file_priv->fbs_lock);
1733
1734 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001735 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001736
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001737 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001738 list_for_each(lh, &dev->mode_config.crtc_list)
1739 crtc_count++;
1740
1741 list_for_each(lh, &dev->mode_config.connector_list)
1742 connector_count++;
1743
1744 list_for_each(lh, &dev->mode_config.encoder_list)
1745 encoder_count++;
1746 } else {
1747
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001748 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001749 crtc_count = mode_group->num_crtcs;
1750 connector_count = mode_group->num_connectors;
1751 encoder_count = mode_group->num_encoders;
1752 }
1753
1754 card_res->max_height = dev->mode_config.max_height;
1755 card_res->min_height = dev->mode_config.min_height;
1756 card_res->max_width = dev->mode_config.max_width;
1757 card_res->min_width = dev->mode_config.min_width;
1758
Dave Airlief453ba02008-11-07 14:05:41 -08001759 /* CRTCs */
1760 if (card_res->count_crtcs >= crtc_count) {
1761 copied = 0;
1762 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001763 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001764 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1765 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001766 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001767 if (put_user(crtc->base.id, crtc_id + copied)) {
1768 ret = -EFAULT;
1769 goto out;
1770 }
1771 copied++;
1772 }
1773 } else {
1774 for (i = 0; i < mode_group->num_crtcs; i++) {
1775 if (put_user(mode_group->id_list[i],
1776 crtc_id + copied)) {
1777 ret = -EFAULT;
1778 goto out;
1779 }
1780 copied++;
1781 }
1782 }
1783 }
1784 card_res->count_crtcs = crtc_count;
1785
1786 /* Encoders */
1787 if (card_res->count_encoders >= encoder_count) {
1788 copied = 0;
1789 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001790 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001791 list_for_each_entry(encoder,
1792 &dev->mode_config.encoder_list,
1793 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001794 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001795 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001796 if (put_user(encoder->base.id, encoder_id +
1797 copied)) {
1798 ret = -EFAULT;
1799 goto out;
1800 }
1801 copied++;
1802 }
1803 } else {
1804 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1805 if (put_user(mode_group->id_list[i],
1806 encoder_id + copied)) {
1807 ret = -EFAULT;
1808 goto out;
1809 }
1810 copied++;
1811 }
1812
1813 }
1814 }
1815 card_res->count_encoders = encoder_count;
1816
1817 /* Connectors */
1818 if (card_res->count_connectors >= connector_count) {
1819 copied = 0;
1820 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001821 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001822 list_for_each_entry(connector,
1823 &dev->mode_config.connector_list,
1824 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001825 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1826 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001827 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001828 if (put_user(connector->base.id,
1829 connector_id + copied)) {
1830 ret = -EFAULT;
1831 goto out;
1832 }
1833 copied++;
1834 }
1835 } else {
1836 int start = mode_group->num_crtcs +
1837 mode_group->num_encoders;
1838 for (i = start; i < start + mode_group->num_connectors; i++) {
1839 if (put_user(mode_group->id_list[i],
1840 connector_id + copied)) {
1841 ret = -EFAULT;
1842 goto out;
1843 }
1844 copied++;
1845 }
1846 }
1847 }
1848 card_res->count_connectors = connector_count;
1849
Jerome Glisse94401062010-07-15 15:43:25 -04001850 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001851 card_res->count_connectors, card_res->count_encoders);
1852
1853out:
Daniel Vetter84849902012-12-02 00:28:11 +01001854 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001855 return ret;
1856}
1857
1858/**
1859 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001860 * @dev: drm device for the ioctl
1861 * @data: data pointer for the ioctl
1862 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001863 *
Dave Airlief453ba02008-11-07 14:05:41 -08001864 * Construct a CRTC configuration structure to return to the user.
1865 *
1866 * Called by the user via ioctl.
1867 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001868 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001869 * Zero on success, errno on failure.
1870 */
1871int drm_mode_getcrtc(struct drm_device *dev,
1872 void *data, struct drm_file *file_priv)
1873{
1874 struct drm_mode_crtc *crtc_resp = data;
1875 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001876 int ret = 0;
1877
Dave Airliefb3b06c2011-02-08 13:55:21 +10001878 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1879 return -EINVAL;
1880
Daniel Vetter84849902012-12-02 00:28:11 +01001881 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001882
Rob Clarka2b34e22013-10-05 16:36:52 -04001883 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1884 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001885 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001886 goto out;
1887 }
Dave Airlief453ba02008-11-07 14:05:41 -08001888
1889 crtc_resp->x = crtc->x;
1890 crtc_resp->y = crtc->y;
1891 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001892 if (crtc->primary->fb)
1893 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001894 else
1895 crtc_resp->fb_id = 0;
1896
1897 if (crtc->enabled) {
1898
1899 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1900 crtc_resp->mode_valid = 1;
1901
1902 } else {
1903 crtc_resp->mode_valid = 0;
1904 }
1905
1906out:
Daniel Vetter84849902012-12-02 00:28:11 +01001907 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001908 return ret;
1909}
1910
Damien Lespiau61d8e322013-09-25 16:45:22 +01001911static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1912 const struct drm_file *file_priv)
1913{
1914 /*
1915 * If user-space hasn't configured the driver to expose the stereo 3D
1916 * modes, don't expose them.
1917 */
1918 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1919 return false;
1920
1921 return true;
1922}
1923
Dave Airlief453ba02008-11-07 14:05:41 -08001924/**
1925 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001926 * @dev: drm device for the ioctl
1927 * @data: data pointer for the ioctl
1928 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001929 *
Dave Airlief453ba02008-11-07 14:05:41 -08001930 * Construct a connector configuration structure to return to the user.
1931 *
1932 * Called by the user via ioctl.
1933 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001934 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001935 * Zero on success, errno on failure.
1936 */
1937int drm_mode_getconnector(struct drm_device *dev, void *data,
1938 struct drm_file *file_priv)
1939{
1940 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001941 struct drm_connector *connector;
1942 struct drm_display_mode *mode;
1943 int mode_count = 0;
1944 int props_count = 0;
1945 int encoders_count = 0;
1946 int ret = 0;
1947 int copied = 0;
1948 int i;
1949 struct drm_mode_modeinfo u_mode;
1950 struct drm_mode_modeinfo __user *mode_ptr;
1951 uint32_t __user *prop_ptr;
1952 uint64_t __user *prop_values;
1953 uint32_t __user *encoder_ptr;
1954
Dave Airliefb3b06c2011-02-08 13:55:21 +10001955 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1956 return -EINVAL;
1957
Dave Airlief453ba02008-11-07 14:05:41 -08001958 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1959
Jerome Glisse94401062010-07-15 15:43:25 -04001960 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001961
Daniel Vetter7b240562012-12-12 00:35:33 +01001962 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001963
Rob Clarka2b34e22013-10-05 16:36:52 -04001964 connector = drm_connector_find(dev, out_resp->connector_id);
1965 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001966 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001967 goto out;
1968 }
Dave Airlief453ba02008-11-07 14:05:41 -08001969
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001970 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001971
1972 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1973 if (connector->encoder_ids[i] != 0) {
1974 encoders_count++;
1975 }
1976 }
1977
1978 if (out_resp->count_modes == 0) {
1979 connector->funcs->fill_modes(connector,
1980 dev->mode_config.max_width,
1981 dev->mode_config.max_height);
1982 }
1983
1984 /* delayed so we get modes regardless of pre-fill_modes state */
1985 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001986 if (drm_mode_expose_to_userspace(mode, file_priv))
1987 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001988
1989 out_resp->connector_id = connector->base.id;
1990 out_resp->connector_type = connector->connector_type;
1991 out_resp->connector_type_id = connector->connector_type_id;
1992 out_resp->mm_width = connector->display_info.width_mm;
1993 out_resp->mm_height = connector->display_info.height_mm;
1994 out_resp->subpixel = connector->display_info.subpixel_order;
1995 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02001996 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08001997 if (connector->encoder)
1998 out_resp->encoder_id = connector->encoder->base.id;
1999 else
2000 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02002001 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002002
2003 /*
2004 * This ioctl is called twice, once to determine how much space is
2005 * needed, and the 2nd time to fill it.
2006 */
2007 if ((out_resp->count_modes >= mode_count) && mode_count) {
2008 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002009 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002010 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002011 if (!drm_mode_expose_to_userspace(mode, file_priv))
2012 continue;
2013
Dave Airlief453ba02008-11-07 14:05:41 -08002014 drm_crtc_convert_to_umode(&u_mode, mode);
2015 if (copy_to_user(mode_ptr + copied,
2016 &u_mode, sizeof(u_mode))) {
2017 ret = -EFAULT;
2018 goto out;
2019 }
2020 copied++;
2021 }
2022 }
2023 out_resp->count_modes = mode_count;
2024
2025 if ((out_resp->count_props >= props_count) && props_count) {
2026 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002027 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2028 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002029 for (i = 0; i < connector->properties.count; i++) {
2030 if (put_user(connector->properties.ids[i],
2031 prop_ptr + copied)) {
2032 ret = -EFAULT;
2033 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002034 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002035
2036 if (put_user(connector->properties.values[i],
2037 prop_values + copied)) {
2038 ret = -EFAULT;
2039 goto out;
2040 }
2041 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002042 }
2043 }
2044 out_resp->count_props = props_count;
2045
2046 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2047 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002048 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002049 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2050 if (connector->encoder_ids[i] != 0) {
2051 if (put_user(connector->encoder_ids[i],
2052 encoder_ptr + copied)) {
2053 ret = -EFAULT;
2054 goto out;
2055 }
2056 copied++;
2057 }
2058 }
2059 }
2060 out_resp->count_encoders = encoders_count;
2061
2062out:
Daniel Vetter7b240562012-12-12 00:35:33 +01002063 mutex_unlock(&dev->mode_config.mutex);
2064
Dave Airlief453ba02008-11-07 14:05:41 -08002065 return ret;
2066}
2067
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002068/**
2069 * drm_mode_getencoder - get encoder configuration
2070 * @dev: drm device for the ioctl
2071 * @data: data pointer for the ioctl
2072 * @file_priv: drm file for the ioctl call
2073 *
2074 * Construct a encoder configuration structure to return to the user.
2075 *
2076 * Called by the user via ioctl.
2077 *
2078 * Returns:
2079 * Zero on success, errno on failure.
2080 */
Dave Airlief453ba02008-11-07 14:05:41 -08002081int drm_mode_getencoder(struct drm_device *dev, void *data,
2082 struct drm_file *file_priv)
2083{
2084 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002085 struct drm_encoder *encoder;
2086 int ret = 0;
2087
Dave Airliefb3b06c2011-02-08 13:55:21 +10002088 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2089 return -EINVAL;
2090
Daniel Vetter84849902012-12-02 00:28:11 +01002091 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002092 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2093 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002094 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002095 goto out;
2096 }
Dave Airlief453ba02008-11-07 14:05:41 -08002097
2098 if (encoder->crtc)
2099 enc_resp->crtc_id = encoder->crtc->base.id;
2100 else
2101 enc_resp->crtc_id = 0;
2102 enc_resp->encoder_type = encoder->encoder_type;
2103 enc_resp->encoder_id = encoder->base.id;
2104 enc_resp->possible_crtcs = encoder->possible_crtcs;
2105 enc_resp->possible_clones = encoder->possible_clones;
2106
2107out:
Daniel Vetter84849902012-12-02 00:28:11 +01002108 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002109 return ret;
2110}
2111
2112/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002113 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002114 * @dev: DRM device
2115 * @data: ioctl data
2116 * @file_priv: DRM file info
2117 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002118 * Construct a list of plane ids to return to the user.
2119 *
2120 * Called by the user via ioctl.
2121 *
2122 * Returns:
2123 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002124 */
2125int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002126 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002127{
2128 struct drm_mode_get_plane_res *plane_resp = data;
2129 struct drm_mode_config *config;
2130 struct drm_plane *plane;
2131 uint32_t __user *plane_ptr;
2132 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002133 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002134
2135 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2136 return -EINVAL;
2137
Daniel Vetter84849902012-12-02 00:28:11 +01002138 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002139 config = &dev->mode_config;
2140
Matt Roper681e7ec2014-04-01 15:22:42 -07002141 if (file_priv->universal_planes)
2142 num_planes = config->num_total_plane;
2143 else
2144 num_planes = config->num_overlay_plane;
2145
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002146 /*
2147 * This ioctl is called twice, once to determine how much space is
2148 * needed, and the 2nd time to fill it.
2149 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002150 if (num_planes &&
2151 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002152 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002153
2154 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002155 /*
2156 * Unless userspace set the 'universal planes'
2157 * capability bit, only advertise overlays.
2158 */
2159 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2160 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002161 continue;
2162
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002163 if (put_user(plane->base.id, plane_ptr + copied)) {
2164 ret = -EFAULT;
2165 goto out;
2166 }
2167 copied++;
2168 }
2169 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002170 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002171
2172out:
Daniel Vetter84849902012-12-02 00:28:11 +01002173 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002174 return ret;
2175}
2176
2177/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002178 * drm_mode_getplane - get plane configuration
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 plane configuration structure 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(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 *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002194 struct drm_plane *plane;
2195 uint32_t __user *format_ptr;
2196 int ret = 0;
2197
2198 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2199 return -EINVAL;
2200
Daniel Vetter84849902012-12-02 00:28:11 +01002201 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002202 plane = drm_plane_find(dev, plane_resp->plane_id);
2203 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002204 ret = -ENOENT;
2205 goto out;
2206 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002207
2208 if (plane->crtc)
2209 plane_resp->crtc_id = plane->crtc->base.id;
2210 else
2211 plane_resp->crtc_id = 0;
2212
2213 if (plane->fb)
2214 plane_resp->fb_id = plane->fb->base.id;
2215 else
2216 plane_resp->fb_id = 0;
2217
2218 plane_resp->plane_id = plane->base.id;
2219 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002220 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002221
2222 /*
2223 * This ioctl is called twice, once to determine how much space is
2224 * needed, and the 2nd time to fill it.
2225 */
2226 if (plane->format_count &&
2227 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002228 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002229 if (copy_to_user(format_ptr,
2230 plane->format_types,
2231 sizeof(uint32_t) * plane->format_count)) {
2232 ret = -EFAULT;
2233 goto out;
2234 }
2235 }
2236 plane_resp->count_format_types = plane->format_count;
2237
2238out:
Daniel Vetter84849902012-12-02 00:28:11 +01002239 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002240 return ret;
2241}
2242
Matt Roperb36552b2014-06-10 08:28:09 -07002243/*
2244 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002245 *
Matt Roperb36552b2014-06-10 08:28:09 -07002246 * Note that we assume an extra reference has already been taken on fb. If the
2247 * update fails, this reference will be dropped before return; if it succeeds,
2248 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002249 *
Matt Roperb36552b2014-06-10 08:28:09 -07002250 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002251 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002252static int setplane_internal(struct drm_plane *plane,
2253 struct drm_crtc *crtc,
Matt Roperb36552b2014-06-10 08:28:09 -07002254 struct drm_framebuffer *fb,
2255 int32_t crtc_x, int32_t crtc_y,
2256 uint32_t crtc_w, uint32_t crtc_h,
2257 /* src_{x,y,w,h} values are 16.16 fixed point */
2258 uint32_t src_x, uint32_t src_y,
2259 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002260{
Chris Wilson17cfd912014-06-13 15:22:28 +01002261 struct drm_device *dev = plane->dev;
Matt Roperb36552b2014-06-10 08:28:09 -07002262 struct drm_framebuffer *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002263 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002264 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002265 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002266
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002267 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002268 if (!fb) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002269 drm_modeset_lock_all(dev);
2270 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002271 ret = plane->funcs->disable_plane(plane);
2272 if (!ret) {
2273 plane->crtc = NULL;
2274 plane->fb = NULL;
2275 } else {
2276 old_fb = NULL;
2277 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002278 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002279 goto out;
2280 }
2281
Matt Roper7f994f32014-05-29 08:06:51 -07002282 /* Check whether this plane is usable on this CRTC */
2283 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2284 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2285 ret = -EINVAL;
2286 goto out;
2287 }
2288
Ville Syrjälä62443be2011-12-20 00:06:47 +02002289 /* Check whether this plane supports the fb pixel format. */
2290 for (i = 0; i < plane->format_count; i++)
2291 if (fb->pixel_format == plane->format_types[i])
2292 break;
2293 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002294 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2295 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002296 ret = -EINVAL;
2297 goto out;
2298 }
2299
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002300 fb_width = fb->width << 16;
2301 fb_height = fb->height << 16;
2302
2303 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002304 if (src_w > fb_width ||
2305 src_x > fb_width - src_w ||
2306 src_h > fb_height ||
2307 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002308 DRM_DEBUG_KMS("Invalid source coordinates "
2309 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002310 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2311 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2312 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2313 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002314 ret = -ENOSPC;
2315 goto out;
2316 }
2317
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002318 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002319 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002320 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002321 crtc_x, crtc_y, crtc_w, crtc_h,
2322 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002323 if (!ret) {
2324 plane->crtc = crtc;
2325 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002326 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002327 } else {
2328 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002329 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002330 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002331
2332out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002333 if (fb)
2334 drm_framebuffer_unreference(fb);
2335 if (old_fb)
2336 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002337
2338 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002339
2340}
2341
2342/**
2343 * drm_mode_setplane - configure a plane's configuration
2344 * @dev: DRM device
2345 * @data: ioctl data*
2346 * @file_priv: DRM file info
2347 *
2348 * Set plane configuration, including placement, fb, scaling, and other factors.
2349 * Or pass a NULL fb to disable (planes may be disabled without providing a
2350 * valid crtc).
2351 *
2352 * Returns:
2353 * Zero on success, errno on failure.
2354 */
2355int drm_mode_setplane(struct drm_device *dev, void *data,
2356 struct drm_file *file_priv)
2357{
2358 struct drm_mode_set_plane *plane_req = data;
2359 struct drm_mode_object *obj;
2360 struct drm_plane *plane;
2361 struct drm_crtc *crtc = NULL;
2362 struct drm_framebuffer *fb = NULL;
2363
2364 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2365 return -EINVAL;
2366
2367 /* Give drivers some help against integer overflows */
2368 if (plane_req->crtc_w > INT_MAX ||
2369 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2370 plane_req->crtc_h > INT_MAX ||
2371 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2372 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2373 plane_req->crtc_w, plane_req->crtc_h,
2374 plane_req->crtc_x, plane_req->crtc_y);
2375 return -ERANGE;
2376 }
2377
2378 /*
2379 * First, find the plane, crtc, and fb objects. If not available,
2380 * we don't bother to call the driver.
2381 */
2382 obj = drm_mode_object_find(dev, plane_req->plane_id,
2383 DRM_MODE_OBJECT_PLANE);
2384 if (!obj) {
2385 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2386 plane_req->plane_id);
2387 return -ENOENT;
2388 }
2389 plane = obj_to_plane(obj);
2390
2391 if (plane_req->fb_id) {
2392 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2393 if (!fb) {
2394 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2395 plane_req->fb_id);
2396 return -ENOENT;
2397 }
2398
2399 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2400 DRM_MODE_OBJECT_CRTC);
2401 if (!obj) {
2402 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2403 plane_req->crtc_id);
2404 return -ENOENT;
2405 }
2406 crtc = obj_to_crtc(obj);
2407 }
2408
Matt Roper161d0dc2014-06-10 08:28:10 -07002409 /*
2410 * setplane_internal will take care of deref'ing either the old or new
2411 * framebuffer depending on success.
2412 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002413 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002414 plane_req->crtc_x, plane_req->crtc_y,
2415 plane_req->crtc_w, plane_req->crtc_h,
2416 plane_req->src_x, plane_req->src_y,
2417 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002418}
2419
2420/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002421 * drm_mode_set_config_internal - helper to call ->set_config
2422 * @set: modeset config to set
2423 *
2424 * This is a little helper to wrap internal calls to the ->set_config driver
2425 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002426 *
2427 * Returns:
2428 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002429 */
2430int drm_mode_set_config_internal(struct drm_mode_set *set)
2431{
2432 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002433 struct drm_framebuffer *fb;
2434 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002435 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002436
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002437 /*
2438 * NOTE: ->set_config can also disable other crtcs (if we steal all
2439 * connectors from it), hence we need to refcount the fbs across all
2440 * crtcs. Atomic modeset will have saner semantics ...
2441 */
2442 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002443 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002444
Daniel Vetterb0d12322012-12-11 01:07:12 +01002445 fb = set->fb;
2446
2447 ret = crtc->funcs->set_config(set);
2448 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002449 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002450 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002451 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002452
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002453 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002454 if (tmp->primary->fb)
2455 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002456 if (tmp->old_fb)
2457 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002458 }
2459
2460 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002461}
2462EXPORT_SYMBOL(drm_mode_set_config_internal);
2463
Matt Roperaf936292014-04-01 15:22:34 -07002464/**
2465 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2466 * CRTC viewport
2467 * @crtc: CRTC that framebuffer will be displayed on
2468 * @x: x panning
2469 * @y: y panning
2470 * @mode: mode that framebuffer will be displayed under
2471 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002472 */
Matt Roperaf936292014-04-01 15:22:34 -07002473int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2474 int x, int y,
2475 const struct drm_display_mode *mode,
2476 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002477
2478{
2479 int hdisplay, vdisplay;
2480
2481 hdisplay = mode->hdisplay;
2482 vdisplay = mode->vdisplay;
2483
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002484 if (drm_mode_is_stereo(mode)) {
2485 struct drm_display_mode adjusted = *mode;
2486
2487 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2488 hdisplay = adjusted.crtc_hdisplay;
2489 vdisplay = adjusted.crtc_vdisplay;
2490 }
2491
Damien Lespiauc11e9282013-09-25 16:45:30 +01002492 if (crtc->invert_dimensions)
2493 swap(hdisplay, vdisplay);
2494
2495 if (hdisplay > fb->width ||
2496 vdisplay > fb->height ||
2497 x > fb->width - hdisplay ||
2498 y > fb->height - vdisplay) {
2499 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2500 fb->width, fb->height, hdisplay, vdisplay, x, y,
2501 crtc->invert_dimensions ? " (inverted)" : "");
2502 return -ENOSPC;
2503 }
2504
2505 return 0;
2506}
Matt Roperaf936292014-04-01 15:22:34 -07002507EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002508
Daniel Vetter2d13b672012-12-11 13:47:23 +01002509/**
Dave Airlief453ba02008-11-07 14:05:41 -08002510 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002511 * @dev: drm device for the ioctl
2512 * @data: data pointer for the ioctl
2513 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002514 *
Dave Airlief453ba02008-11-07 14:05:41 -08002515 * Build a new CRTC configuration based on user request.
2516 *
2517 * Called by the user via ioctl.
2518 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002519 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002520 * Zero on success, errno on failure.
2521 */
2522int drm_mode_setcrtc(struct drm_device *dev, void *data,
2523 struct drm_file *file_priv)
2524{
2525 struct drm_mode_config *config = &dev->mode_config;
2526 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002527 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002528 struct drm_connector **connector_set = NULL, *connector;
2529 struct drm_framebuffer *fb = NULL;
2530 struct drm_display_mode *mode = NULL;
2531 struct drm_mode_set set;
2532 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002533 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002534 int i;
2535
Dave Airliefb3b06c2011-02-08 13:55:21 +10002536 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2537 return -EINVAL;
2538
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002539 /* For some reason crtc x/y offsets are signed internally. */
2540 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2541 return -ERANGE;
2542
Daniel Vetter84849902012-12-02 00:28:11 +01002543 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002544 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2545 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002546 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002547 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002548 goto out;
2549 }
Jerome Glisse94401062010-07-15 15:43:25 -04002550 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002551
2552 if (crtc_req->mode_valid) {
2553 /* If we have a mode we need a framebuffer. */
2554 /* If we pass -1, set the mode with the currently bound fb */
2555 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002556 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002557 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2558 ret = -EINVAL;
2559 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002560 }
Matt Roperf4510a22014-04-01 15:22:40 -07002561 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002562 /* Make refcounting symmetric with the lookup path. */
2563 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002564 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002565 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2566 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002567 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2568 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002569 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002570 goto out;
2571 }
Dave Airlief453ba02008-11-07 14:05:41 -08002572 }
2573
2574 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002575 if (!mode) {
2576 ret = -ENOMEM;
2577 goto out;
2578 }
2579
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002580 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2581 if (ret) {
2582 DRM_DEBUG_KMS("Invalid mode\n");
2583 goto out;
2584 }
2585
Dave Airlief453ba02008-11-07 14:05:41 -08002586 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002587
Damien Lespiauc11e9282013-09-25 16:45:30 +01002588 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2589 mode, fb);
2590 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002591 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002592
Dave Airlief453ba02008-11-07 14:05:41 -08002593 }
2594
2595 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002596 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002597 ret = -EINVAL;
2598 goto out;
2599 }
2600
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002601 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002602 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002603 crtc_req->count_connectors);
2604 ret = -EINVAL;
2605 goto out;
2606 }
2607
2608 if (crtc_req->count_connectors > 0) {
2609 u32 out_id;
2610
2611 /* Avoid unbounded kernel memory allocation */
2612 if (crtc_req->count_connectors > config->num_connector) {
2613 ret = -EINVAL;
2614 goto out;
2615 }
2616
2617 connector_set = kmalloc(crtc_req->count_connectors *
2618 sizeof(struct drm_connector *),
2619 GFP_KERNEL);
2620 if (!connector_set) {
2621 ret = -ENOMEM;
2622 goto out;
2623 }
2624
2625 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002626 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002627 if (get_user(out_id, &set_connectors_ptr[i])) {
2628 ret = -EFAULT;
2629 goto out;
2630 }
2631
Rob Clarka2b34e22013-10-05 16:36:52 -04002632 connector = drm_connector_find(dev, out_id);
2633 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002634 DRM_DEBUG_KMS("Connector id %d unknown\n",
2635 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002636 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002637 goto out;
2638 }
Jerome Glisse94401062010-07-15 15:43:25 -04002639 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2640 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002641 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002642
2643 connector_set[i] = connector;
2644 }
2645 }
2646
2647 set.crtc = crtc;
2648 set.x = crtc_req->x;
2649 set.y = crtc_req->y;
2650 set.mode = mode;
2651 set.connectors = connector_set;
2652 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002653 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002654 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002655
2656out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002657 if (fb)
2658 drm_framebuffer_unreference(fb);
2659
Dave Airlief453ba02008-11-07 14:05:41 -08002660 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002661 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002662 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002663 return ret;
2664}
2665
Matt Roper161d0dc2014-06-10 08:28:10 -07002666/**
2667 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2668 * universal plane handler call
2669 * @crtc: crtc to update cursor for
2670 * @req: data pointer for the ioctl
2671 * @file_priv: drm file for the ioctl call
2672 *
2673 * Legacy cursor ioctl's work directly with driver buffer handles. To
2674 * translate legacy ioctl calls into universal plane handler calls, we need to
2675 * wrap the native buffer handle in a drm_framebuffer.
2676 *
2677 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2678 * buffer with a pitch of 4*width; the universal plane interface should be used
2679 * directly in cases where the hardware can support other buffer settings and
2680 * userspace wants to make use of these capabilities.
2681 *
2682 * Returns:
2683 * Zero on success, errno on failure.
2684 */
2685static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2686 struct drm_mode_cursor2 *req,
2687 struct drm_file *file_priv)
2688{
2689 struct drm_device *dev = crtc->dev;
2690 struct drm_framebuffer *fb = NULL;
2691 struct drm_mode_fb_cmd2 fbreq = {
2692 .width = req->width,
2693 .height = req->height,
2694 .pixel_format = DRM_FORMAT_ARGB8888,
2695 .pitches = { req->width * 4 },
2696 .handles = { req->handle },
2697 };
2698 int32_t crtc_x, crtc_y;
2699 uint32_t crtc_w = 0, crtc_h = 0;
2700 uint32_t src_w = 0, src_h = 0;
2701 int ret = 0;
2702
2703 BUG_ON(!crtc->cursor);
2704
2705 /*
2706 * Obtain fb we'll be using (either new or existing) and take an extra
2707 * reference to it if fb != null. setplane will take care of dropping
2708 * the reference if the plane update fails.
2709 */
2710 if (req->flags & DRM_MODE_CURSOR_BO) {
2711 if (req->handle) {
2712 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2713 if (IS_ERR(fb)) {
2714 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2715 return PTR_ERR(fb);
2716 }
2717
2718 drm_framebuffer_reference(fb);
2719 } else {
2720 fb = NULL;
2721 }
2722 } else {
2723 mutex_lock(&dev->mode_config.mutex);
2724 fb = crtc->cursor->fb;
2725 if (fb)
2726 drm_framebuffer_reference(fb);
2727 mutex_unlock(&dev->mode_config.mutex);
2728 }
2729
2730 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2731 crtc_x = req->x;
2732 crtc_y = req->y;
2733 } else {
2734 crtc_x = crtc->cursor_x;
2735 crtc_y = crtc->cursor_y;
2736 }
2737
2738 if (fb) {
2739 crtc_w = fb->width;
2740 crtc_h = fb->height;
2741 src_w = fb->width << 16;
2742 src_h = fb->height << 16;
2743 }
2744
2745 /*
2746 * setplane_internal will take care of deref'ing either the old or new
2747 * framebuffer depending on success.
2748 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002749 ret = setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002750 crtc_x, crtc_y, crtc_w, crtc_h,
2751 0, 0, src_w, src_h);
2752
2753 /* Update successful; save new cursor position, if necessary */
2754 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2755 crtc->cursor_x = req->x;
2756 crtc->cursor_y = req->y;
2757 }
2758
2759 return ret;
2760}
2761
Dave Airlie4c813d42013-06-20 11:48:52 +10002762static int drm_mode_cursor_common(struct drm_device *dev,
2763 struct drm_mode_cursor2 *req,
2764 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002765{
Dave Airlief453ba02008-11-07 14:05:41 -08002766 struct drm_crtc *crtc;
2767 int ret = 0;
2768
Dave Airliefb3b06c2011-02-08 13:55:21 +10002769 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2770 return -EINVAL;
2771
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002772 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002773 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002774
Rob Clarka2b34e22013-10-05 16:36:52 -04002775 crtc = drm_crtc_find(dev, req->crtc_id);
2776 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002777 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002778 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002779 }
Dave Airlief453ba02008-11-07 14:05:41 -08002780
Matt Roper161d0dc2014-06-10 08:28:10 -07002781 /*
2782 * If this crtc has a universal cursor plane, call that plane's update
2783 * handler rather than using legacy cursor handlers.
2784 */
2785 if (crtc->cursor)
2786 return drm_mode_cursor_universal(crtc, req, file_priv);
2787
Rob Clark51fd3712013-11-19 12:10:12 -05002788 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002789 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002790 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002791 ret = -ENXIO;
2792 goto out;
2793 }
2794 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002795 if (crtc->funcs->cursor_set2)
2796 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2797 req->width, req->height, req->hot_x, req->hot_y);
2798 else
2799 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2800 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002801 }
2802
2803 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2804 if (crtc->funcs->cursor_move) {
2805 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2806 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002807 ret = -EFAULT;
2808 goto out;
2809 }
2810 }
2811out:
Rob Clark51fd3712013-11-19 12:10:12 -05002812 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002813
Dave Airlief453ba02008-11-07 14:05:41 -08002814 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002815
2816}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002817
2818
2819/**
2820 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2821 * @dev: drm device for the ioctl
2822 * @data: data pointer for the ioctl
2823 * @file_priv: drm file for the ioctl call
2824 *
2825 * Set the cursor configuration based on user request.
2826 *
2827 * Called by the user via ioctl.
2828 *
2829 * Returns:
2830 * Zero on success, errno on failure.
2831 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002832int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002833 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002834{
2835 struct drm_mode_cursor *req = data;
2836 struct drm_mode_cursor2 new_req;
2837
2838 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2839 new_req.hot_x = new_req.hot_y = 0;
2840
2841 return drm_mode_cursor_common(dev, &new_req, file_priv);
2842}
2843
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002844/**
2845 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2846 * @dev: drm device for the ioctl
2847 * @data: data pointer for the ioctl
2848 * @file_priv: drm file for the ioctl call
2849 *
2850 * Set the cursor configuration based on user request. This implements the 2nd
2851 * version of the cursor ioctl, which allows userspace to additionally specify
2852 * the hotspot of the pointer.
2853 *
2854 * Called by the user via ioctl.
2855 *
2856 * Returns:
2857 * Zero on success, errno on failure.
2858 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002859int drm_mode_cursor2_ioctl(struct drm_device *dev,
2860 void *data, struct drm_file *file_priv)
2861{
2862 struct drm_mode_cursor2 *req = data;
2863 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002864}
2865
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002866/**
2867 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2868 * @bpp: bits per pixels
2869 * @depth: bit depth per pixel
2870 *
2871 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2872 * Useful in fbdev emulation code, since that deals in those values.
2873 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002874uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2875{
2876 uint32_t fmt;
2877
2878 switch (bpp) {
2879 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002880 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002881 break;
2882 case 16:
2883 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002884 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002885 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002886 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002887 break;
2888 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002889 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002890 break;
2891 case 32:
2892 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002893 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002894 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002895 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002896 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002897 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002898 break;
2899 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002900 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2901 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002902 break;
2903 }
2904
2905 return fmt;
2906}
2907EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2908
Dave Airlief453ba02008-11-07 14:05:41 -08002909/**
2910 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002911 * @dev: drm device for the ioctl
2912 * @data: data pointer for the ioctl
2913 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002914 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002915 * Add a new FB to the specified CRTC, given a user request. This is the
2916 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002917 *
2918 * Called by the user via ioctl.
2919 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002920 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002921 * Zero on success, errno on failure.
2922 */
2923int drm_mode_addfb(struct drm_device *dev,
2924 void *data, struct drm_file *file_priv)
2925{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002926 struct drm_mode_fb_cmd *or = data;
2927 struct drm_mode_fb_cmd2 r = {};
2928 struct drm_mode_config *config = &dev->mode_config;
2929 struct drm_framebuffer *fb;
2930 int ret = 0;
2931
2932 /* Use new struct with format internally */
2933 r.fb_id = or->fb_id;
2934 r.width = or->width;
2935 r.height = or->height;
2936 r.pitches[0] = or->pitch;
2937 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2938 r.handles[0] = or->handle;
2939
2940 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2941 return -EINVAL;
2942
Jesse Barnesacb4b992011-11-07 12:03:23 -08002943 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002944 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002945
2946 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002947 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002948
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002949 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2950 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002951 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002952 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002953 }
2954
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002955 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002956 or->fb_id = fb->base.id;
2957 list_add(&fb->filp_head, &file_priv->fbs);
2958 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002959 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002960
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002961 return ret;
2962}
2963
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002964static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002965{
2966 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2967
2968 switch (format) {
2969 case DRM_FORMAT_C8:
2970 case DRM_FORMAT_RGB332:
2971 case DRM_FORMAT_BGR233:
2972 case DRM_FORMAT_XRGB4444:
2973 case DRM_FORMAT_XBGR4444:
2974 case DRM_FORMAT_RGBX4444:
2975 case DRM_FORMAT_BGRX4444:
2976 case DRM_FORMAT_ARGB4444:
2977 case DRM_FORMAT_ABGR4444:
2978 case DRM_FORMAT_RGBA4444:
2979 case DRM_FORMAT_BGRA4444:
2980 case DRM_FORMAT_XRGB1555:
2981 case DRM_FORMAT_XBGR1555:
2982 case DRM_FORMAT_RGBX5551:
2983 case DRM_FORMAT_BGRX5551:
2984 case DRM_FORMAT_ARGB1555:
2985 case DRM_FORMAT_ABGR1555:
2986 case DRM_FORMAT_RGBA5551:
2987 case DRM_FORMAT_BGRA5551:
2988 case DRM_FORMAT_RGB565:
2989 case DRM_FORMAT_BGR565:
2990 case DRM_FORMAT_RGB888:
2991 case DRM_FORMAT_BGR888:
2992 case DRM_FORMAT_XRGB8888:
2993 case DRM_FORMAT_XBGR8888:
2994 case DRM_FORMAT_RGBX8888:
2995 case DRM_FORMAT_BGRX8888:
2996 case DRM_FORMAT_ARGB8888:
2997 case DRM_FORMAT_ABGR8888:
2998 case DRM_FORMAT_RGBA8888:
2999 case DRM_FORMAT_BGRA8888:
3000 case DRM_FORMAT_XRGB2101010:
3001 case DRM_FORMAT_XBGR2101010:
3002 case DRM_FORMAT_RGBX1010102:
3003 case DRM_FORMAT_BGRX1010102:
3004 case DRM_FORMAT_ARGB2101010:
3005 case DRM_FORMAT_ABGR2101010:
3006 case DRM_FORMAT_RGBA1010102:
3007 case DRM_FORMAT_BGRA1010102:
3008 case DRM_FORMAT_YUYV:
3009 case DRM_FORMAT_YVYU:
3010 case DRM_FORMAT_UYVY:
3011 case DRM_FORMAT_VYUY:
3012 case DRM_FORMAT_AYUV:
3013 case DRM_FORMAT_NV12:
3014 case DRM_FORMAT_NV21:
3015 case DRM_FORMAT_NV16:
3016 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003017 case DRM_FORMAT_NV24:
3018 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003019 case DRM_FORMAT_YUV410:
3020 case DRM_FORMAT_YVU410:
3021 case DRM_FORMAT_YUV411:
3022 case DRM_FORMAT_YVU411:
3023 case DRM_FORMAT_YUV420:
3024 case DRM_FORMAT_YVU420:
3025 case DRM_FORMAT_YUV422:
3026 case DRM_FORMAT_YVU422:
3027 case DRM_FORMAT_YUV444:
3028 case DRM_FORMAT_YVU444:
3029 return 0;
3030 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003031 DRM_DEBUG_KMS("invalid pixel format %s\n",
3032 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003033 return -EINVAL;
3034 }
3035}
3036
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003037static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003038{
3039 int ret, hsub, vsub, num_planes, i;
3040
3041 ret = format_check(r);
3042 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003043 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3044 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003045 return ret;
3046 }
3047
3048 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3049 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3050 num_planes = drm_format_num_planes(r->pixel_format);
3051
3052 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003053 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003054 return -EINVAL;
3055 }
3056
3057 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003058 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003059 return -EINVAL;
3060 }
3061
3062 for (i = 0; i < num_planes; i++) {
3063 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003064 unsigned int height = r->height / (i != 0 ? vsub : 1);
3065 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003066
3067 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003068 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003069 return -EINVAL;
3070 }
3071
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003072 if ((uint64_t) width * cpp > UINT_MAX)
3073 return -ERANGE;
3074
3075 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3076 return -ERANGE;
3077
3078 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003079 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003080 return -EINVAL;
3081 }
3082 }
3083
3084 return 0;
3085}
3086
Matt Roperc394c2b2014-06-10 08:28:08 -07003087static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3088 struct drm_mode_fb_cmd2 *r,
3089 struct drm_file *file_priv)
3090{
3091 struct drm_mode_config *config = &dev->mode_config;
3092 struct drm_framebuffer *fb;
3093 int ret;
3094
3095 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3096 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3097 return ERR_PTR(-EINVAL);
3098 }
3099
3100 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3101 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3102 r->width, config->min_width, config->max_width);
3103 return ERR_PTR(-EINVAL);
3104 }
3105 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3106 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3107 r->height, config->min_height, config->max_height);
3108 return ERR_PTR(-EINVAL);
3109 }
3110
3111 ret = framebuffer_check(r);
3112 if (ret)
3113 return ERR_PTR(ret);
3114
3115 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3116 if (IS_ERR(fb)) {
3117 DRM_DEBUG_KMS("could not create framebuffer\n");
3118 return fb;
3119 }
3120
3121 mutex_lock(&file_priv->fbs_lock);
3122 r->fb_id = fb->base.id;
3123 list_add(&fb->filp_head, &file_priv->fbs);
3124 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3125 mutex_unlock(&file_priv->fbs_lock);
3126
3127 return fb;
3128}
3129
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003130/**
3131 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003132 * @dev: drm device for the ioctl
3133 * @data: data pointer for the ioctl
3134 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003135 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003136 * Add a new FB to the specified CRTC, given a user request with format. This is
3137 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3138 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003139 *
3140 * Called by the user via ioctl.
3141 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003142 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003143 * Zero on success, errno on failure.
3144 */
3145int drm_mode_addfb2(struct drm_device *dev,
3146 void *data, struct drm_file *file_priv)
3147{
Dave Airlief453ba02008-11-07 14:05:41 -08003148 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003149
Dave Airliefb3b06c2011-02-08 13:55:21 +10003150 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3151 return -EINVAL;
3152
Matt Roperc394c2b2014-06-10 08:28:08 -07003153 fb = add_framebuffer_internal(dev, data, file_priv);
3154 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003155 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003156
Matt Roperc394c2b2014-06-10 08:28:08 -07003157 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003158}
3159
3160/**
3161 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003162 * @dev: drm device for the ioctl
3163 * @data: data pointer for the ioctl
3164 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003165 *
Dave Airlief453ba02008-11-07 14:05:41 -08003166 * Remove the FB specified by the user.
3167 *
3168 * Called by the user via ioctl.
3169 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003170 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003171 * Zero on success, errno on failure.
3172 */
3173int drm_mode_rmfb(struct drm_device *dev,
3174 void *data, struct drm_file *file_priv)
3175{
Dave Airlief453ba02008-11-07 14:05:41 -08003176 struct drm_framebuffer *fb = NULL;
3177 struct drm_framebuffer *fbl = NULL;
3178 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003179 int found = 0;
3180
Dave Airliefb3b06c2011-02-08 13:55:21 +10003181 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3182 return -EINVAL;
3183
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003184 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003185 mutex_lock(&dev->mode_config.fb_lock);
3186 fb = __drm_framebuffer_lookup(dev, *id);
3187 if (!fb)
3188 goto fail_lookup;
3189
Dave Airlief453ba02008-11-07 14:05:41 -08003190 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3191 if (fb == fbl)
3192 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003193 if (!found)
3194 goto fail_lookup;
3195
3196 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3197 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003198
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003199 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003200 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003201 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003202
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003203 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003204
Daniel Vetter2b677e82012-12-10 21:16:05 +01003205 return 0;
3206
3207fail_lookup:
3208 mutex_unlock(&dev->mode_config.fb_lock);
3209 mutex_unlock(&file_priv->fbs_lock);
3210
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003211 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003212}
3213
3214/**
3215 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003216 * @dev: drm device for the ioctl
3217 * @data: data pointer for the ioctl
3218 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003219 *
Dave Airlief453ba02008-11-07 14:05:41 -08003220 * Lookup the FB given its ID and return info about it.
3221 *
3222 * Called by the user via ioctl.
3223 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003224 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003225 * Zero on success, errno on failure.
3226 */
3227int drm_mode_getfb(struct drm_device *dev,
3228 void *data, struct drm_file *file_priv)
3229{
3230 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003231 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003232 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003233
Dave Airliefb3b06c2011-02-08 13:55:21 +10003234 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3235 return -EINVAL;
3236
Daniel Vetter786b99e2012-12-02 21:53:40 +01003237 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003238 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003239 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003240
3241 r->height = fb->height;
3242 r->width = fb->width;
3243 r->depth = fb->depth;
3244 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003245 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003246 if (fb->funcs->create_handle) {
Dave Airlie7963e9d2014-08-08 07:30:53 +10003247 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003248 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003249 ret = fb->funcs->create_handle(fb, file_priv,
3250 &r->handle);
3251 } else {
3252 /* GET_FB() is an unprivileged ioctl so we must not
3253 * return a buffer-handle to non-master processes! For
3254 * backwards-compatibility reasons, we cannot make
3255 * GET_FB() privileged, so just return an invalid handle
3256 * for non-masters. */
3257 r->handle = 0;
3258 ret = 0;
3259 }
3260 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003261 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003262 }
Dave Airlief453ba02008-11-07 14:05:41 -08003263
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003264 drm_framebuffer_unreference(fb);
3265
Dave Airlief453ba02008-11-07 14:05:41 -08003266 return ret;
3267}
3268
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003269/**
3270 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3271 * @dev: drm device for the ioctl
3272 * @data: data pointer for the ioctl
3273 * @file_priv: drm file for the ioctl call
3274 *
3275 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3276 * rectangle list. Generic userspace which does frontbuffer rendering must call
3277 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3278 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3279 *
3280 * Modesetting drivers which always update the frontbuffer do not need to
3281 * implement the corresponding ->dirty framebuffer callback.
3282 *
3283 * Called by the user via ioctl.
3284 *
3285 * Returns:
3286 * Zero on success, errno on failure.
3287 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003288int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3289 void *data, struct drm_file *file_priv)
3290{
3291 struct drm_clip_rect __user *clips_ptr;
3292 struct drm_clip_rect *clips = NULL;
3293 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003294 struct drm_framebuffer *fb;
3295 unsigned flags;
3296 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003297 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003298
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 Vetter4ccf0972012-12-11 00:38:18 +01003303 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003304 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003305
3306 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003307 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003308
3309 if (!num_clips != !clips_ptr) {
3310 ret = -EINVAL;
3311 goto out_err1;
3312 }
3313
3314 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3315
3316 /* If userspace annotates copy, clips must come in pairs */
3317 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3318 ret = -EINVAL;
3319 goto out_err1;
3320 }
3321
3322 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003323 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3324 ret = -EINVAL;
3325 goto out_err1;
3326 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003327 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3328 if (!clips) {
3329 ret = -ENOMEM;
3330 goto out_err1;
3331 }
3332
3333 ret = copy_from_user(clips, clips_ptr,
3334 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003335 if (ret) {
3336 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003337 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003338 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003339 }
3340
3341 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003342 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3343 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003344 } else {
3345 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003346 }
3347
3348out_err2:
3349 kfree(clips);
3350out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003351 drm_framebuffer_unreference(fb);
3352
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003353 return ret;
3354}
3355
3356
Dave Airlief453ba02008-11-07 14:05:41 -08003357/**
3358 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003359 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003360 *
Dave Airlief453ba02008-11-07 14:05:41 -08003361 * Destroy all the FBs associated with @filp.
3362 *
3363 * Called by the user via ioctl.
3364 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003365 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003366 * Zero on success, errno on failure.
3367 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003368void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003369{
Dave Airlief453ba02008-11-07 14:05:41 -08003370 struct drm_device *dev = priv->minor->dev;
3371 struct drm_framebuffer *fb, *tfb;
3372
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003373 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003374 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003375
3376 mutex_lock(&dev->mode_config.fb_lock);
3377 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3378 __drm_framebuffer_unregister(dev, fb);
3379 mutex_unlock(&dev->mode_config.fb_lock);
3380
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003381 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003382
3383 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003384 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003385 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003386 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003387}
3388
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003389/**
3390 * drm_property_create - create a new property type
3391 * @dev: drm device
3392 * @flags: flags specifying the property type
3393 * @name: name of the property
3394 * @num_values: number of pre-defined values
3395 *
3396 * This creates a new generic drm property which can then be attached to a drm
3397 * object with drm_object_attach_property. The returned property object must be
3398 * freed with drm_property_destroy.
3399 *
3400 * Returns:
3401 * A pointer to the newly created property on success, NULL on failure.
3402 */
Dave Airlief453ba02008-11-07 14:05:41 -08003403struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3404 const char *name, int num_values)
3405{
3406 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003407 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003408
3409 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3410 if (!property)
3411 return NULL;
3412
Rob Clark98f75de2014-05-30 11:37:03 -04003413 property->dev = dev;
3414
Dave Airlief453ba02008-11-07 14:05:41 -08003415 if (num_values) {
3416 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3417 if (!property->values)
3418 goto fail;
3419 }
3420
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003421 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3422 if (ret)
3423 goto fail;
3424
Dave Airlief453ba02008-11-07 14:05:41 -08003425 property->flags = flags;
3426 property->num_values = num_values;
3427 INIT_LIST_HEAD(&property->enum_blob_list);
3428
Vinson Lee471dd2e2011-11-10 11:55:40 -08003429 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003430 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003431 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3432 }
Dave Airlief453ba02008-11-07 14:05:41 -08003433
3434 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003435
3436 WARN_ON(!drm_property_type_valid(property));
3437
Dave Airlief453ba02008-11-07 14:05:41 -08003438 return property;
3439fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003440 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003441 kfree(property);
3442 return NULL;
3443}
3444EXPORT_SYMBOL(drm_property_create);
3445
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003446/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003447 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003448 * @dev: drm device
3449 * @flags: flags specifying the property type
3450 * @name: name of the property
3451 * @props: enumeration lists with property values
3452 * @num_values: number of pre-defined values
3453 *
3454 * This creates a new generic drm property which can then be attached to a drm
3455 * object with drm_object_attach_property. The returned property object must be
3456 * freed with drm_property_destroy.
3457 *
3458 * Userspace is only allowed to set one of the predefined values for enumeration
3459 * properties.
3460 *
3461 * Returns:
3462 * A pointer to the newly created property on success, NULL on failure.
3463 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003464struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3465 const char *name,
3466 const struct drm_prop_enum_list *props,
3467 int num_values)
3468{
3469 struct drm_property *property;
3470 int i, ret;
3471
3472 flags |= DRM_MODE_PROP_ENUM;
3473
3474 property = drm_property_create(dev, flags, name, num_values);
3475 if (!property)
3476 return NULL;
3477
3478 for (i = 0; i < num_values; i++) {
3479 ret = drm_property_add_enum(property, i,
3480 props[i].type,
3481 props[i].name);
3482 if (ret) {
3483 drm_property_destroy(dev, property);
3484 return NULL;
3485 }
3486 }
3487
3488 return property;
3489}
3490EXPORT_SYMBOL(drm_property_create_enum);
3491
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003492/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003493 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003494 * @dev: drm device
3495 * @flags: flags specifying the property type
3496 * @name: name of the property
3497 * @props: enumeration lists with property bitflags
3498 * @num_values: number of pre-defined values
3499 *
3500 * This creates a new generic drm property which can then be attached to a drm
3501 * object with drm_object_attach_property. The returned property object must be
3502 * freed with drm_property_destroy.
3503 *
3504 * Compared to plain enumeration properties userspace is allowed to set any
3505 * or'ed together combination of the predefined property bitflag values
3506 *
3507 * Returns:
3508 * A pointer to the newly created property on success, NULL on failure.
3509 */
Rob Clark49e27542012-05-17 02:23:26 -06003510struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3511 int flags, const char *name,
3512 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303513 int num_props,
3514 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003515{
3516 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303517 int i, ret, index = 0;
3518 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003519
3520 flags |= DRM_MODE_PROP_BITMASK;
3521
3522 property = drm_property_create(dev, flags, name, num_values);
3523 if (!property)
3524 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303525 for (i = 0; i < num_props; i++) {
3526 if (!(supported_bits & (1ULL << props[i].type)))
3527 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003528
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303529 if (WARN_ON(index >= num_values)) {
3530 drm_property_destroy(dev, property);
3531 return NULL;
3532 }
3533
3534 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003535 props[i].type,
3536 props[i].name);
3537 if (ret) {
3538 drm_property_destroy(dev, property);
3539 return NULL;
3540 }
3541 }
3542
3543 return property;
3544}
3545EXPORT_SYMBOL(drm_property_create_bitmask);
3546
Rob Clarkebc44cf2012-09-12 22:22:31 -05003547static struct drm_property *property_create_range(struct drm_device *dev,
3548 int flags, const char *name,
3549 uint64_t min, uint64_t max)
3550{
3551 struct drm_property *property;
3552
3553 property = drm_property_create(dev, flags, name, 2);
3554 if (!property)
3555 return NULL;
3556
3557 property->values[0] = min;
3558 property->values[1] = max;
3559
3560 return property;
3561}
3562
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003563/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003564 * drm_property_create_range - create a new ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003565 * @dev: drm device
3566 * @flags: flags specifying the property type
3567 * @name: name of the property
3568 * @min: minimum value of the property
3569 * @max: maximum value of the property
3570 *
3571 * This creates a new generic drm property which can then be attached to a drm
3572 * object with drm_object_attach_property. The returned property object must be
3573 * freed with drm_property_destroy.
3574 *
3575 * Userspace is allowed to set any interger value in the (min, max) range
3576 * inclusive.
3577 *
3578 * Returns:
3579 * A pointer to the newly created property on success, NULL on failure.
3580 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003581struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3582 const char *name,
3583 uint64_t min, uint64_t max)
3584{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003585 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3586 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003587}
3588EXPORT_SYMBOL(drm_property_create_range);
3589
Rob Clarkebc44cf2012-09-12 22:22:31 -05003590struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3591 int flags, const char *name,
3592 int64_t min, int64_t max)
3593{
3594 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3595 name, I642U64(min), I642U64(max));
3596}
3597EXPORT_SYMBOL(drm_property_create_signed_range);
3598
Rob Clark98f75de2014-05-30 11:37:03 -04003599struct drm_property *drm_property_create_object(struct drm_device *dev,
3600 int flags, const char *name, uint32_t type)
3601{
3602 struct drm_property *property;
3603
3604 flags |= DRM_MODE_PROP_OBJECT;
3605
3606 property = drm_property_create(dev, flags, name, 1);
3607 if (!property)
3608 return NULL;
3609
3610 property->values[0] = type;
3611
3612 return property;
3613}
3614EXPORT_SYMBOL(drm_property_create_object);
3615
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003616/**
3617 * drm_property_add_enum - add a possible value to an enumeration property
3618 * @property: enumeration property to change
3619 * @index: index of the new enumeration
3620 * @value: value of the new enumeration
3621 * @name: symbolic name of the new enumeration
3622 *
3623 * This functions adds enumerations to a property.
3624 *
3625 * It's use is deprecated, drivers should use one of the more specific helpers
3626 * to directly create the property with all enumerations already attached.
3627 *
3628 * Returns:
3629 * Zero on success, error code on failure.
3630 */
Dave Airlief453ba02008-11-07 14:05:41 -08003631int drm_property_add_enum(struct drm_property *property, int index,
3632 uint64_t value, const char *name)
3633{
3634 struct drm_property_enum *prop_enum;
3635
Rob Clark5ea22f22014-05-30 11:34:01 -04003636 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3637 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003638 return -EINVAL;
3639
3640 /*
3641 * Bitmask enum properties have the additional constraint of values
3642 * from 0 to 63
3643 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003644 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3645 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003646 return -EINVAL;
3647
3648 if (!list_empty(&property->enum_blob_list)) {
3649 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3650 if (prop_enum->value == value) {
3651 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3652 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3653 return 0;
3654 }
3655 }
3656 }
3657
3658 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3659 if (!prop_enum)
3660 return -ENOMEM;
3661
3662 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3663 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3664 prop_enum->value = value;
3665
3666 property->values[index] = value;
3667 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3668 return 0;
3669}
3670EXPORT_SYMBOL(drm_property_add_enum);
3671
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003672/**
3673 * drm_property_destroy - destroy a drm property
3674 * @dev: drm device
3675 * @property: property to destry
3676 *
3677 * This function frees a property including any attached resources like
3678 * enumeration values.
3679 */
Dave Airlief453ba02008-11-07 14:05:41 -08003680void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3681{
3682 struct drm_property_enum *prop_enum, *pt;
3683
3684 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3685 list_del(&prop_enum->head);
3686 kfree(prop_enum);
3687 }
3688
3689 if (property->num_values)
3690 kfree(property->values);
3691 drm_mode_object_put(dev, &property->base);
3692 list_del(&property->head);
3693 kfree(property);
3694}
3695EXPORT_SYMBOL(drm_property_destroy);
3696
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003697/**
3698 * drm_object_attach_property - attach a property to a modeset object
3699 * @obj: drm modeset object
3700 * @property: property to attach
3701 * @init_val: initial value of the property
3702 *
3703 * This attaches the given property to the modeset object with the given initial
3704 * value. Currently this function cannot fail since the properties are stored in
3705 * a statically sized array.
3706 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003707void drm_object_attach_property(struct drm_mode_object *obj,
3708 struct drm_property *property,
3709 uint64_t init_val)
3710{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003711 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003712
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003713 if (count == DRM_OBJECT_MAX_PROPERTY) {
3714 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3715 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3716 "you see this message on the same object type.\n",
3717 obj->type);
3718 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003719 }
3720
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003721 obj->properties->ids[count] = property->base.id;
3722 obj->properties->values[count] = init_val;
3723 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003724}
3725EXPORT_SYMBOL(drm_object_attach_property);
3726
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003727/**
3728 * drm_object_property_set_value - set the value of a property
3729 * @obj: drm mode object to set property value for
3730 * @property: property to set
3731 * @val: value the property should be set to
3732 *
3733 * This functions sets a given property on a given object. This function only
3734 * changes the software state of the property, it does not call into the
3735 * driver's ->set_property callback.
3736 *
3737 * Returns:
3738 * Zero on success, error code on failure.
3739 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003740int drm_object_property_set_value(struct drm_mode_object *obj,
3741 struct drm_property *property, uint64_t val)
3742{
3743 int i;
3744
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003745 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003746 if (obj->properties->ids[i] == property->base.id) {
3747 obj->properties->values[i] = val;
3748 return 0;
3749 }
3750 }
3751
3752 return -EINVAL;
3753}
3754EXPORT_SYMBOL(drm_object_property_set_value);
3755
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003756/**
3757 * drm_object_property_get_value - retrieve the value of a property
3758 * @obj: drm mode object to get property value from
3759 * @property: property to retrieve
3760 * @val: storage for the property value
3761 *
3762 * This function retrieves the softare state of the given property for the given
3763 * property. Since there is no driver callback to retrieve the current property
3764 * value this might be out of sync with the hardware, depending upon the driver
3765 * and property.
3766 *
3767 * Returns:
3768 * Zero on success, error code on failure.
3769 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003770int drm_object_property_get_value(struct drm_mode_object *obj,
3771 struct drm_property *property, uint64_t *val)
3772{
3773 int i;
3774
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003775 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003776 if (obj->properties->ids[i] == property->base.id) {
3777 *val = obj->properties->values[i];
3778 return 0;
3779 }
3780 }
3781
3782 return -EINVAL;
3783}
3784EXPORT_SYMBOL(drm_object_property_get_value);
3785
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003786/**
3787 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3788 * @dev: DRM device
3789 * @data: ioctl data
3790 * @file_priv: DRM file info
3791 *
3792 * This function retrieves the current value for an connectors's property.
3793 *
3794 * Called by the user via ioctl.
3795 *
3796 * Returns:
3797 * Zero on success, errno on failure.
3798 */
Dave Airlief453ba02008-11-07 14:05:41 -08003799int drm_mode_getproperty_ioctl(struct drm_device *dev,
3800 void *data, struct drm_file *file_priv)
3801{
Dave Airlief453ba02008-11-07 14:05:41 -08003802 struct drm_mode_get_property *out_resp = data;
3803 struct drm_property *property;
3804 int enum_count = 0;
3805 int blob_count = 0;
3806 int value_count = 0;
3807 int ret = 0, i;
3808 int copied;
3809 struct drm_property_enum *prop_enum;
3810 struct drm_mode_property_enum __user *enum_ptr;
3811 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003812 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003813 uint64_t __user *values_ptr;
3814 uint32_t __user *blob_length_ptr;
3815
Dave Airliefb3b06c2011-02-08 13:55:21 +10003816 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3817 return -EINVAL;
3818
Daniel Vetter84849902012-12-02 00:28:11 +01003819 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003820 property = drm_property_find(dev, out_resp->prop_id);
3821 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003822 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003823 goto done;
3824 }
Dave Airlief453ba02008-11-07 14:05:41 -08003825
Rob Clark5ea22f22014-05-30 11:34:01 -04003826 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3827 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003828 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3829 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003830 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003831 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3832 blob_count++;
3833 }
3834
3835 value_count = property->num_values;
3836
3837 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3838 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3839 out_resp->flags = property->flags;
3840
3841 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003842 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003843 for (i = 0; i < value_count; i++) {
3844 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3845 ret = -EFAULT;
3846 goto done;
3847 }
3848 }
3849 }
3850 out_resp->count_values = value_count;
3851
Rob Clark5ea22f22014-05-30 11:34:01 -04003852 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3853 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003854 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3855 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003856 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003857 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3858
3859 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3860 ret = -EFAULT;
3861 goto done;
3862 }
3863
3864 if (copy_to_user(&enum_ptr[copied].name,
3865 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3866 ret = -EFAULT;
3867 goto done;
3868 }
3869 copied++;
3870 }
3871 }
3872 out_resp->count_enum_blobs = enum_count;
3873 }
3874
Rob Clark5ea22f22014-05-30 11:34:01 -04003875 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003876 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3877 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003878 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3879 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003880
3881 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3882 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3883 ret = -EFAULT;
3884 goto done;
3885 }
3886
3887 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3888 ret = -EFAULT;
3889 goto done;
3890 }
3891
3892 copied++;
3893 }
3894 }
3895 out_resp->count_enum_blobs = blob_count;
3896 }
3897done:
Daniel Vetter84849902012-12-02 00:28:11 +01003898 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003899 return ret;
3900}
3901
3902static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3903 void *data)
3904{
3905 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003906 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003907
3908 if (!length || !data)
3909 return NULL;
3910
3911 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3912 if (!blob)
3913 return NULL;
3914
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003915 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3916 if (ret) {
3917 kfree(blob);
3918 return NULL;
3919 }
3920
Dave Airlief453ba02008-11-07 14:05:41 -08003921 blob->length = length;
3922
3923 memcpy(blob->data, data, length);
3924
Dave Airlief453ba02008-11-07 14:05:41 -08003925 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3926 return blob;
3927}
3928
3929static void drm_property_destroy_blob(struct drm_device *dev,
3930 struct drm_property_blob *blob)
3931{
3932 drm_mode_object_put(dev, &blob->base);
3933 list_del(&blob->head);
3934 kfree(blob);
3935}
3936
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003937/**
3938 * drm_mode_getblob_ioctl - get the contents of a blob property value
3939 * @dev: DRM device
3940 * @data: ioctl data
3941 * @file_priv: DRM file info
3942 *
3943 * This function retrieves the contents of a blob property. The value stored in
3944 * an object's blob property is just a normal modeset object id.
3945 *
3946 * Called by the user via ioctl.
3947 *
3948 * Returns:
3949 * Zero on success, errno on failure.
3950 */
Dave Airlief453ba02008-11-07 14:05:41 -08003951int drm_mode_getblob_ioctl(struct drm_device *dev,
3952 void *data, struct drm_file *file_priv)
3953{
Dave Airlief453ba02008-11-07 14:05:41 -08003954 struct drm_mode_get_blob *out_resp = data;
3955 struct drm_property_blob *blob;
3956 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003957 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003958
Dave Airliefb3b06c2011-02-08 13:55:21 +10003959 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3960 return -EINVAL;
3961
Daniel Vetter84849902012-12-02 00:28:11 +01003962 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003963 blob = drm_property_blob_find(dev, out_resp->blob_id);
3964 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003965 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003966 goto done;
3967 }
Dave Airlief453ba02008-11-07 14:05:41 -08003968
3969 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003970 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003971 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3972 ret = -EFAULT;
3973 goto done;
3974 }
3975 }
3976 out_resp->length = blob->length;
3977
3978done:
Daniel Vetter84849902012-12-02 00:28:11 +01003979 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003980 return ret;
3981}
3982
Dave Airlie43aba7e2014-06-05 14:01:31 +10003983int drm_mode_connector_set_path_property(struct drm_connector *connector,
3984 char *path)
3985{
3986 struct drm_device *dev = connector->dev;
3987 int ret, size;
3988 size = strlen(path) + 1;
3989
3990 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
3991 size, path);
3992 if (!connector->path_blob_ptr)
3993 return -EINVAL;
3994
3995 ret = drm_object_property_set_value(&connector->base,
3996 dev->mode_config.path_property,
3997 connector->path_blob_ptr->base.id);
3998 return ret;
3999}
4000EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4001
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004002/**
4003 * drm_mode_connector_update_edid_property - update the edid property of a connector
4004 * @connector: drm connector
4005 * @edid: new value of the edid property
4006 *
4007 * This function creates a new blob modeset object and assigns its id to the
4008 * connector's edid property.
4009 *
4010 * Returns:
4011 * Zero on success, errno on failure.
4012 */
Dave Airlief453ba02008-11-07 14:05:41 -08004013int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4014 struct edid *edid)
4015{
4016 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02004017 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08004018
Thomas Wood4cf2b282014-06-18 17:52:33 +01004019 /* ignore requests to set edid when overridden */
4020 if (connector->override_edid)
4021 return 0;
4022
Dave Airlief453ba02008-11-07 14:05:41 -08004023 if (connector->edid_blob_ptr)
4024 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4025
4026 /* Delete edid, when there is none. */
4027 if (!edid) {
4028 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05004029 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08004030 return ret;
4031 }
4032
Adam Jackson7466f4c2010-03-29 21:43:23 +00004033 size = EDID_LENGTH * (1 + edid->extensions);
4034 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4035 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00004036 if (!connector->edid_blob_ptr)
4037 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08004038
Rob Clark58495562012-10-11 20:50:56 -05004039 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08004040 dev->mode_config.edid_property,
4041 connector->edid_blob_ptr->base.id);
4042
4043 return ret;
4044}
4045EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4046
Paulo Zanoni26a34812012-05-15 18:08:59 -03004047static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004048 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004049{
4050 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4051 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004052
4053 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004054 if (value < property->values[0] || value > property->values[1])
4055 return false;
4056 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004057 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4058 int64_t svalue = U642I64(value);
4059 if (svalue < U642I64(property->values[0]) ||
4060 svalue > U642I64(property->values[1]))
4061 return false;
4062 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004063 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06004064 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004065 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06004066 for (i = 0; i < property->num_values; i++)
4067 valid_mask |= (1ULL << property->values[i]);
4068 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004069 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004070 /* Only the driver knows */
4071 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004072 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4073 struct drm_mode_object *obj;
4074 /* a zero value for an object property translates to null: */
4075 if (value == 0)
4076 return true;
4077 /*
4078 * NOTE: use _object_find() directly to bypass restriction on
4079 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4080 * object this could race against object finalization, so it
4081 * simply tells us that the object *was* valid. Which is good
4082 * enough.
4083 */
4084 obj = _object_find(property->dev, value, property->values[0]);
4085 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004086 } else {
4087 int i;
4088 for (i = 0; i < property->num_values; i++)
4089 if (property->values[i] == value)
4090 return true;
4091 return false;
4092 }
4093}
4094
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004095/**
4096 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4097 * @dev: DRM device
4098 * @data: ioctl data
4099 * @file_priv: DRM file info
4100 *
4101 * This function sets the current value for a connectors's property. It also
4102 * calls into a driver's ->set_property callback to update the hardware state
4103 *
4104 * Called by the user via ioctl.
4105 *
4106 * Returns:
4107 * Zero on success, errno on failure.
4108 */
Dave Airlief453ba02008-11-07 14:05:41 -08004109int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4110 void *data, struct drm_file *file_priv)
4111{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004112 struct drm_mode_connector_set_property *conn_set_prop = data;
4113 struct drm_mode_obj_set_property obj_set_prop = {
4114 .value = conn_set_prop->value,
4115 .prop_id = conn_set_prop->prop_id,
4116 .obj_id = conn_set_prop->connector_id,
4117 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4118 };
Dave Airlief453ba02008-11-07 14:05:41 -08004119
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004120 /* It does all the locking and checking we need */
4121 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004122}
4123
Paulo Zanonic5431882012-05-15 18:09:02 -03004124static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4125 struct drm_property *property,
4126 uint64_t value)
4127{
4128 int ret = -EINVAL;
4129 struct drm_connector *connector = obj_to_connector(obj);
4130
4131 /* Do DPMS ourselves */
4132 if (property == connector->dev->mode_config.dpms_property) {
4133 if (connector->funcs->dpms)
4134 (*connector->funcs->dpms)(connector, (int)value);
4135 ret = 0;
4136 } else if (connector->funcs->set_property)
4137 ret = connector->funcs->set_property(connector, property, value);
4138
4139 /* store the property value if successful */
4140 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004141 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004142 return ret;
4143}
4144
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004145static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4146 struct drm_property *property,
4147 uint64_t value)
4148{
4149 int ret = -EINVAL;
4150 struct drm_crtc *crtc = obj_to_crtc(obj);
4151
4152 if (crtc->funcs->set_property)
4153 ret = crtc->funcs->set_property(crtc, property, value);
4154 if (!ret)
4155 drm_object_property_set_value(obj, property, value);
4156
4157 return ret;
4158}
4159
Rob Clark4d939142012-05-17 02:23:27 -06004160static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4161 struct drm_property *property,
4162 uint64_t value)
4163{
4164 int ret = -EINVAL;
4165 struct drm_plane *plane = obj_to_plane(obj);
4166
4167 if (plane->funcs->set_property)
4168 ret = plane->funcs->set_property(plane, property, value);
4169 if (!ret)
4170 drm_object_property_set_value(obj, property, value);
4171
4172 return ret;
4173}
4174
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004175/**
4176 * drm_mode_getproperty_ioctl - get the current value of a object's property
4177 * @dev: DRM device
4178 * @data: ioctl data
4179 * @file_priv: DRM file info
4180 *
4181 * This function retrieves the current value for an object's property. Compared
4182 * to the connector specific ioctl this one is extended to also work on crtc and
4183 * plane objects.
4184 *
4185 * Called by the user via ioctl.
4186 *
4187 * Returns:
4188 * Zero on success, errno on failure.
4189 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004190int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4191 struct drm_file *file_priv)
4192{
4193 struct drm_mode_obj_get_properties *arg = data;
4194 struct drm_mode_object *obj;
4195 int ret = 0;
4196 int i;
4197 int copied = 0;
4198 int props_count = 0;
4199 uint32_t __user *props_ptr;
4200 uint64_t __user *prop_values_ptr;
4201
4202 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4203 return -EINVAL;
4204
Daniel Vetter84849902012-12-02 00:28:11 +01004205 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004206
4207 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4208 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004209 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004210 goto out;
4211 }
4212 if (!obj->properties) {
4213 ret = -EINVAL;
4214 goto out;
4215 }
4216
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004217 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004218
4219 /* This ioctl is called twice, once to determine how much space is
4220 * needed, and the 2nd time to fill it. */
4221 if ((arg->count_props >= props_count) && props_count) {
4222 copied = 0;
4223 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4224 prop_values_ptr = (uint64_t __user *)(unsigned long)
4225 (arg->prop_values_ptr);
4226 for (i = 0; i < props_count; i++) {
4227 if (put_user(obj->properties->ids[i],
4228 props_ptr + copied)) {
4229 ret = -EFAULT;
4230 goto out;
4231 }
4232 if (put_user(obj->properties->values[i],
4233 prop_values_ptr + copied)) {
4234 ret = -EFAULT;
4235 goto out;
4236 }
4237 copied++;
4238 }
4239 }
4240 arg->count_props = props_count;
4241out:
Daniel Vetter84849902012-12-02 00:28:11 +01004242 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004243 return ret;
4244}
4245
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004246/**
4247 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4248 * @dev: DRM device
4249 * @data: ioctl data
4250 * @file_priv: DRM file info
4251 *
4252 * This function sets the current value for an object's property. It also calls
4253 * into a driver's ->set_property callback to update the hardware state.
4254 * Compared to the connector specific ioctl this one is extended to also work on
4255 * crtc and plane objects.
4256 *
4257 * Called by the user via ioctl.
4258 *
4259 * Returns:
4260 * Zero on success, errno on failure.
4261 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004262int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4263 struct drm_file *file_priv)
4264{
4265 struct drm_mode_obj_set_property *arg = data;
4266 struct drm_mode_object *arg_obj;
4267 struct drm_mode_object *prop_obj;
4268 struct drm_property *property;
4269 int ret = -EINVAL;
4270 int i;
4271
4272 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4273 return -EINVAL;
4274
Daniel Vetter84849902012-12-02 00:28:11 +01004275 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004276
4277 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004278 if (!arg_obj) {
4279 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004280 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004281 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004282 if (!arg_obj->properties)
4283 goto out;
4284
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004285 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004286 if (arg_obj->properties->ids[i] == arg->prop_id)
4287 break;
4288
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004289 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004290 goto out;
4291
4292 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4293 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004294 if (!prop_obj) {
4295 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004296 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004297 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004298 property = obj_to_property(prop_obj);
4299
4300 if (!drm_property_change_is_valid(property, arg->value))
4301 goto out;
4302
4303 switch (arg_obj->type) {
4304 case DRM_MODE_OBJECT_CONNECTOR:
4305 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4306 arg->value);
4307 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004308 case DRM_MODE_OBJECT_CRTC:
4309 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4310 break;
Rob Clark4d939142012-05-17 02:23:27 -06004311 case DRM_MODE_OBJECT_PLANE:
4312 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4313 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004314 }
4315
4316out:
Daniel Vetter84849902012-12-02 00:28:11 +01004317 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004318 return ret;
4319}
4320
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004321/**
4322 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4323 * @connector: connector to attach
4324 * @encoder: encoder to attach @connector to
4325 *
4326 * This function links up a connector to an encoder. Note that the routing
4327 * restrictions between encoders and crtcs are exposed to userspace through the
4328 * possible_clones and possible_crtcs bitmasks.
4329 *
4330 * Returns:
4331 * Zero on success, errno on failure.
4332 */
Dave Airlief453ba02008-11-07 14:05:41 -08004333int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4334 struct drm_encoder *encoder)
4335{
4336 int i;
4337
4338 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4339 if (connector->encoder_ids[i] == 0) {
4340 connector->encoder_ids[i] = encoder->base.id;
4341 return 0;
4342 }
4343 }
4344 return -ENOMEM;
4345}
4346EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4347
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004348/**
4349 * drm_mode_crtc_set_gamma_size - set the gamma table size
4350 * @crtc: CRTC to set the gamma table size for
4351 * @gamma_size: size of the gamma table
4352 *
4353 * Drivers which support gamma tables should set this to the supported gamma
4354 * table size when initializing the CRTC. Currently the drm core only supports a
4355 * fixed gamma table size.
4356 *
4357 * Returns:
4358 * Zero on success, errno on failure.
4359 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004360int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004361 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004362{
4363 crtc->gamma_size = gamma_size;
4364
4365 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4366 if (!crtc->gamma_store) {
4367 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004368 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004369 }
4370
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004371 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004372}
4373EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4374
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004375/**
4376 * drm_mode_gamma_set_ioctl - set the gamma table
4377 * @dev: DRM device
4378 * @data: ioctl data
4379 * @file_priv: DRM file info
4380 *
4381 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4382 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4383 *
4384 * Called by the user via ioctl.
4385 *
4386 * Returns:
4387 * Zero on success, errno on failure.
4388 */
Dave Airlief453ba02008-11-07 14:05:41 -08004389int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4390 void *data, struct drm_file *file_priv)
4391{
4392 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004393 struct drm_crtc *crtc;
4394 void *r_base, *g_base, *b_base;
4395 int size;
4396 int ret = 0;
4397
Dave Airliefb3b06c2011-02-08 13:55:21 +10004398 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4399 return -EINVAL;
4400
Daniel Vetter84849902012-12-02 00:28:11 +01004401 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004402 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4403 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004404 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004405 goto out;
4406 }
Dave Airlief453ba02008-11-07 14:05:41 -08004407
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004408 if (crtc->funcs->gamma_set == NULL) {
4409 ret = -ENOSYS;
4410 goto out;
4411 }
4412
Dave Airlief453ba02008-11-07 14:05:41 -08004413 /* memcpy into gamma store */
4414 if (crtc_lut->gamma_size != crtc->gamma_size) {
4415 ret = -EINVAL;
4416 goto out;
4417 }
4418
4419 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4420 r_base = crtc->gamma_store;
4421 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4422 ret = -EFAULT;
4423 goto out;
4424 }
4425
4426 g_base = r_base + size;
4427 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4428 ret = -EFAULT;
4429 goto out;
4430 }
4431
4432 b_base = g_base + size;
4433 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4434 ret = -EFAULT;
4435 goto out;
4436 }
4437
James Simmons72034252010-08-03 01:33:19 +01004438 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004439
4440out:
Daniel Vetter84849902012-12-02 00:28:11 +01004441 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004442 return ret;
4443
4444}
4445
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004446/**
4447 * drm_mode_gamma_get_ioctl - get the gamma table
4448 * @dev: DRM device
4449 * @data: ioctl data
4450 * @file_priv: DRM file info
4451 *
4452 * Copy the current gamma table into the storage provided. This also provides
4453 * the gamma table size the driver expects, which can be used to size the
4454 * allocated storage.
4455 *
4456 * Called by the user via ioctl.
4457 *
4458 * Returns:
4459 * Zero on success, errno on failure.
4460 */
Dave Airlief453ba02008-11-07 14:05:41 -08004461int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4462 void *data, struct drm_file *file_priv)
4463{
4464 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004465 struct drm_crtc *crtc;
4466 void *r_base, *g_base, *b_base;
4467 int size;
4468 int ret = 0;
4469
Dave Airliefb3b06c2011-02-08 13:55:21 +10004470 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4471 return -EINVAL;
4472
Daniel Vetter84849902012-12-02 00:28:11 +01004473 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004474 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4475 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004476 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004477 goto out;
4478 }
Dave Airlief453ba02008-11-07 14:05:41 -08004479
4480 /* memcpy into gamma store */
4481 if (crtc_lut->gamma_size != crtc->gamma_size) {
4482 ret = -EINVAL;
4483 goto out;
4484 }
4485
4486 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4487 r_base = crtc->gamma_store;
4488 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4489 ret = -EFAULT;
4490 goto out;
4491 }
4492
4493 g_base = r_base + size;
4494 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4495 ret = -EFAULT;
4496 goto out;
4497 }
4498
4499 b_base = g_base + size;
4500 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4501 ret = -EFAULT;
4502 goto out;
4503 }
4504out:
Daniel Vetter84849902012-12-02 00:28:11 +01004505 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004506 return ret;
4507}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004508
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004509/**
4510 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4511 * @dev: DRM device
4512 * @data: ioctl data
4513 * @file_priv: DRM file info
4514 *
4515 * This schedules an asynchronous update on a given CRTC, called page flip.
4516 * Optionally a drm event is generated to signal the completion of the event.
4517 * Generic drivers cannot assume that a pageflip with changed framebuffer
4518 * properties (including driver specific metadata like tiling layout) will work,
4519 * but some drivers support e.g. pixel format changes through the pageflip
4520 * ioctl.
4521 *
4522 * Called by the user via ioctl.
4523 *
4524 * Returns:
4525 * Zero on success, errno on failure.
4526 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004527int drm_mode_page_flip_ioctl(struct drm_device *dev,
4528 void *data, struct drm_file *file_priv)
4529{
4530 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004531 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004532 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004533 struct drm_pending_vblank_event *e = NULL;
4534 unsigned long flags;
4535 int ret = -EINVAL;
4536
4537 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4538 page_flip->reserved != 0)
4539 return -EINVAL;
4540
Keith Packard62f21042013-07-22 18:50:00 -07004541 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4542 return -EINVAL;
4543
Rob Clarka2b34e22013-10-05 16:36:52 -04004544 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4545 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004546 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004547
Rob Clark51fd3712013-11-19 12:10:12 -05004548 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004549 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004550 /* The framebuffer is currently unbound, presumably
4551 * due to a hotplug event, that userspace has not
4552 * yet discovered.
4553 */
4554 ret = -EBUSY;
4555 goto out;
4556 }
4557
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004558 if (crtc->funcs->page_flip == NULL)
4559 goto out;
4560
Daniel Vetter786b99e2012-12-02 21:53:40 +01004561 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004562 if (!fb) {
4563 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004564 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004565 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004566
Damien Lespiauc11e9282013-09-25 16:45:30 +01004567 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4568 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004569 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004570
Matt Roperf4510a22014-04-01 15:22:40 -07004571 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004572 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4573 ret = -EINVAL;
4574 goto out;
4575 }
4576
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004577 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4578 ret = -ENOMEM;
4579 spin_lock_irqsave(&dev->event_lock, flags);
4580 if (file_priv->event_space < sizeof e->event) {
4581 spin_unlock_irqrestore(&dev->event_lock, flags);
4582 goto out;
4583 }
4584 file_priv->event_space -= sizeof e->event;
4585 spin_unlock_irqrestore(&dev->event_lock, flags);
4586
4587 e = kzalloc(sizeof *e, GFP_KERNEL);
4588 if (e == NULL) {
4589 spin_lock_irqsave(&dev->event_lock, flags);
4590 file_priv->event_space += sizeof e->event;
4591 spin_unlock_irqrestore(&dev->event_lock, flags);
4592 goto out;
4593 }
4594
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004595 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004596 e->event.base.length = sizeof e->event;
4597 e->event.user_data = page_flip->user_data;
4598 e->base.event = &e->event.base;
4599 e->base.file_priv = file_priv;
4600 e->base.destroy =
4601 (void (*) (struct drm_pending_event *)) kfree;
4602 }
4603
Matt Roperf4510a22014-04-01 15:22:40 -07004604 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004605 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004606 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004607 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4608 spin_lock_irqsave(&dev->event_lock, flags);
4609 file_priv->event_space += sizeof e->event;
4610 spin_unlock_irqrestore(&dev->event_lock, flags);
4611 kfree(e);
4612 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004613 /* Keep the old fb, don't unref it. */
4614 old_fb = NULL;
4615 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004616 /*
4617 * Warn if the driver hasn't properly updated the crtc->fb
4618 * field to reflect that the new framebuffer is now used.
4619 * Failing to do so will screw with the reference counting
4620 * on framebuffers.
4621 */
Matt Roperf4510a22014-04-01 15:22:40 -07004622 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004623 /* Unref only the old framebuffer. */
4624 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004625 }
4626
4627out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004628 if (fb)
4629 drm_framebuffer_unreference(fb);
4630 if (old_fb)
4631 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004632 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004633
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004634 return ret;
4635}
Chris Wilsoneb033552011-01-24 15:11:08 +00004636
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004637/**
4638 * drm_mode_config_reset - call ->reset callbacks
4639 * @dev: drm device
4640 *
4641 * This functions calls all the crtc's, encoder's and connector's ->reset
4642 * callback. Drivers can use this in e.g. their driver load or resume code to
4643 * reset hardware and software state.
4644 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004645void drm_mode_config_reset(struct drm_device *dev)
4646{
4647 struct drm_crtc *crtc;
4648 struct drm_encoder *encoder;
4649 struct drm_connector *connector;
4650
4651 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4652 if (crtc->funcs->reset)
4653 crtc->funcs->reset(crtc);
4654
4655 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4656 if (encoder->funcs->reset)
4657 encoder->funcs->reset(encoder);
4658
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004659 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4660 connector->status = connector_status_unknown;
4661
Chris Wilsoneb033552011-01-24 15:11:08 +00004662 if (connector->funcs->reset)
4663 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004664 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004665}
4666EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004667
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004668/**
4669 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4670 * @dev: DRM device
4671 * @data: ioctl data
4672 * @file_priv: DRM file info
4673 *
4674 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4675 * TTM or something else entirely) and returns the resulting buffer handle. This
4676 * handle can then be wrapped up into a framebuffer modeset object.
4677 *
4678 * Note that userspace is not allowed to use such objects for render
4679 * acceleration - drivers must create their own private ioctls for such a use
4680 * case.
4681 *
4682 * Called by the user via ioctl.
4683 *
4684 * Returns:
4685 * Zero on success, errno on failure.
4686 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004687int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4688 void *data, struct drm_file *file_priv)
4689{
4690 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004691 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004692
4693 if (!dev->driver->dumb_create)
4694 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004695 if (!args->width || !args->height || !args->bpp)
4696 return -EINVAL;
4697
4698 /* overflow checks for 32bit size calculations */
4699 cpp = DIV_ROUND_UP(args->bpp, 8);
4700 if (cpp > 0xffffffffU / args->width)
4701 return -EINVAL;
4702 stride = cpp * args->width;
4703 if (args->height > 0xffffffffU / stride)
4704 return -EINVAL;
4705
4706 /* test for wrap-around */
4707 size = args->height * stride;
4708 if (PAGE_ALIGN(size) == 0)
4709 return -EINVAL;
4710
Dave Airlieff72145b2011-02-07 12:16:14 +10004711 return dev->driver->dumb_create(file_priv, dev, args);
4712}
4713
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004714/**
4715 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4716 * @dev: DRM device
4717 * @data: ioctl data
4718 * @file_priv: DRM file info
4719 *
4720 * Allocate an offset in the drm device node's address space to be able to
4721 * memory map a dumb buffer.
4722 *
4723 * Called by the user via ioctl.
4724 *
4725 * Returns:
4726 * Zero on success, errno on failure.
4727 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004728int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4729 void *data, struct drm_file *file_priv)
4730{
4731 struct drm_mode_map_dumb *args = data;
4732
4733 /* call driver ioctl to get mmap offset */
4734 if (!dev->driver->dumb_map_offset)
4735 return -ENOSYS;
4736
4737 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4738}
4739
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004740/**
4741 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4742 * @dev: DRM device
4743 * @data: ioctl data
4744 * @file_priv: DRM file info
4745 *
4746 * This destroys the userspace handle for the given dumb backing storage buffer.
4747 * Since buffer objects must be reference counted in the kernel a buffer object
4748 * won't be immediately freed if a framebuffer modeset object still uses it.
4749 *
4750 * Called by the user via ioctl.
4751 *
4752 * Returns:
4753 * Zero on success, errno on failure.
4754 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004755int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4756 void *data, struct drm_file *file_priv)
4757{
4758 struct drm_mode_destroy_dumb *args = data;
4759
4760 if (!dev->driver->dumb_destroy)
4761 return -ENOSYS;
4762
4763 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4764}
Dave Airlie248dbc22011-11-29 20:02:54 +00004765
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004766/**
4767 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4768 * @format: pixel format (DRM_FORMAT_*)
4769 * @depth: storage for the depth value
4770 * @bpp: storage for the bpp value
4771 *
4772 * This only supports RGB formats here for compat with code that doesn't use
4773 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004774 */
4775void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4776 int *bpp)
4777{
4778 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004779 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004780 case DRM_FORMAT_RGB332:
4781 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004782 *depth = 8;
4783 *bpp = 8;
4784 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004785 case DRM_FORMAT_XRGB1555:
4786 case DRM_FORMAT_XBGR1555:
4787 case DRM_FORMAT_RGBX5551:
4788 case DRM_FORMAT_BGRX5551:
4789 case DRM_FORMAT_ARGB1555:
4790 case DRM_FORMAT_ABGR1555:
4791 case DRM_FORMAT_RGBA5551:
4792 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004793 *depth = 15;
4794 *bpp = 16;
4795 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004796 case DRM_FORMAT_RGB565:
4797 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004798 *depth = 16;
4799 *bpp = 16;
4800 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004801 case DRM_FORMAT_RGB888:
4802 case DRM_FORMAT_BGR888:
4803 *depth = 24;
4804 *bpp = 24;
4805 break;
4806 case DRM_FORMAT_XRGB8888:
4807 case DRM_FORMAT_XBGR8888:
4808 case DRM_FORMAT_RGBX8888:
4809 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004810 *depth = 24;
4811 *bpp = 32;
4812 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004813 case DRM_FORMAT_XRGB2101010:
4814 case DRM_FORMAT_XBGR2101010:
4815 case DRM_FORMAT_RGBX1010102:
4816 case DRM_FORMAT_BGRX1010102:
4817 case DRM_FORMAT_ARGB2101010:
4818 case DRM_FORMAT_ABGR2101010:
4819 case DRM_FORMAT_RGBA1010102:
4820 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004821 *depth = 30;
4822 *bpp = 32;
4823 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004824 case DRM_FORMAT_ARGB8888:
4825 case DRM_FORMAT_ABGR8888:
4826 case DRM_FORMAT_RGBA8888:
4827 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004828 *depth = 32;
4829 *bpp = 32;
4830 break;
4831 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004832 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4833 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004834 *depth = 0;
4835 *bpp = 0;
4836 break;
4837 }
4838}
4839EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004840
4841/**
4842 * drm_format_num_planes - get the number of planes for format
4843 * @format: pixel format (DRM_FORMAT_*)
4844 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004845 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004846 * The number of planes used by the specified pixel format.
4847 */
4848int drm_format_num_planes(uint32_t format)
4849{
4850 switch (format) {
4851 case DRM_FORMAT_YUV410:
4852 case DRM_FORMAT_YVU410:
4853 case DRM_FORMAT_YUV411:
4854 case DRM_FORMAT_YVU411:
4855 case DRM_FORMAT_YUV420:
4856 case DRM_FORMAT_YVU420:
4857 case DRM_FORMAT_YUV422:
4858 case DRM_FORMAT_YVU422:
4859 case DRM_FORMAT_YUV444:
4860 case DRM_FORMAT_YVU444:
4861 return 3;
4862 case DRM_FORMAT_NV12:
4863 case DRM_FORMAT_NV21:
4864 case DRM_FORMAT_NV16:
4865 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004866 case DRM_FORMAT_NV24:
4867 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004868 return 2;
4869 default:
4870 return 1;
4871 }
4872}
4873EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004874
4875/**
4876 * drm_format_plane_cpp - determine the bytes per pixel value
4877 * @format: pixel format (DRM_FORMAT_*)
4878 * @plane: plane index
4879 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004880 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004881 * The bytes per pixel value for the specified plane.
4882 */
4883int drm_format_plane_cpp(uint32_t format, int plane)
4884{
4885 unsigned int depth;
4886 int bpp;
4887
4888 if (plane >= drm_format_num_planes(format))
4889 return 0;
4890
4891 switch (format) {
4892 case DRM_FORMAT_YUYV:
4893 case DRM_FORMAT_YVYU:
4894 case DRM_FORMAT_UYVY:
4895 case DRM_FORMAT_VYUY:
4896 return 2;
4897 case DRM_FORMAT_NV12:
4898 case DRM_FORMAT_NV21:
4899 case DRM_FORMAT_NV16:
4900 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004901 case DRM_FORMAT_NV24:
4902 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004903 return plane ? 2 : 1;
4904 case DRM_FORMAT_YUV410:
4905 case DRM_FORMAT_YVU410:
4906 case DRM_FORMAT_YUV411:
4907 case DRM_FORMAT_YVU411:
4908 case DRM_FORMAT_YUV420:
4909 case DRM_FORMAT_YVU420:
4910 case DRM_FORMAT_YUV422:
4911 case DRM_FORMAT_YVU422:
4912 case DRM_FORMAT_YUV444:
4913 case DRM_FORMAT_YVU444:
4914 return 1;
4915 default:
4916 drm_fb_get_bpp_depth(format, &depth, &bpp);
4917 return bpp >> 3;
4918 }
4919}
4920EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004921
4922/**
4923 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4924 * @format: pixel format (DRM_FORMAT_*)
4925 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004926 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004927 * The horizontal chroma subsampling factor for the
4928 * specified pixel format.
4929 */
4930int drm_format_horz_chroma_subsampling(uint32_t format)
4931{
4932 switch (format) {
4933 case DRM_FORMAT_YUV411:
4934 case DRM_FORMAT_YVU411:
4935 case DRM_FORMAT_YUV410:
4936 case DRM_FORMAT_YVU410:
4937 return 4;
4938 case DRM_FORMAT_YUYV:
4939 case DRM_FORMAT_YVYU:
4940 case DRM_FORMAT_UYVY:
4941 case DRM_FORMAT_VYUY:
4942 case DRM_FORMAT_NV12:
4943 case DRM_FORMAT_NV21:
4944 case DRM_FORMAT_NV16:
4945 case DRM_FORMAT_NV61:
4946 case DRM_FORMAT_YUV422:
4947 case DRM_FORMAT_YVU422:
4948 case DRM_FORMAT_YUV420:
4949 case DRM_FORMAT_YVU420:
4950 return 2;
4951 default:
4952 return 1;
4953 }
4954}
4955EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4956
4957/**
4958 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4959 * @format: pixel format (DRM_FORMAT_*)
4960 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004961 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004962 * The vertical chroma subsampling factor for the
4963 * specified pixel format.
4964 */
4965int drm_format_vert_chroma_subsampling(uint32_t format)
4966{
4967 switch (format) {
4968 case DRM_FORMAT_YUV410:
4969 case DRM_FORMAT_YVU410:
4970 return 4;
4971 case DRM_FORMAT_YUV420:
4972 case DRM_FORMAT_YVU420:
4973 case DRM_FORMAT_NV12:
4974 case DRM_FORMAT_NV21:
4975 return 2;
4976 default:
4977 return 1;
4978 }
4979}
4980EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004981
4982/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05304983 * drm_rotation_simplify() - Try to simplify the rotation
4984 * @rotation: Rotation to be simplified
4985 * @supported_rotations: Supported rotations
4986 *
4987 * Attempt to simplify the rotation to a form that is supported.
4988 * Eg. if the hardware supports everything except DRM_REFLECT_X
4989 * one could call this function like this:
4990 *
4991 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
4992 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
4993 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
4994 *
4995 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
4996 * transforms the hardware supports, this function may not
4997 * be able to produce a supported transform, so the caller should
4998 * check the result afterwards.
4999 */
5000unsigned int drm_rotation_simplify(unsigned int rotation,
5001 unsigned int supported_rotations)
5002{
5003 if (rotation & ~supported_rotations) {
5004 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5005 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5006 }
5007
5008 return rotation;
5009}
5010EXPORT_SYMBOL(drm_rotation_simplify);
5011
5012/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005013 * drm_mode_config_init - initialize DRM mode_configuration structure
5014 * @dev: DRM device
5015 *
5016 * Initialize @dev's mode_config structure, used for tracking the graphics
5017 * configuration of @dev.
5018 *
5019 * Since this initializes the modeset locks, no locking is possible. Which is no
5020 * problem, since this should happen single threaded at init time. It is the
5021 * driver's problem to ensure this guarantee.
5022 *
5023 */
5024void drm_mode_config_init(struct drm_device *dev)
5025{
5026 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005027 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005028 mutex_init(&dev->mode_config.idr_mutex);
5029 mutex_init(&dev->mode_config.fb_lock);
5030 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5031 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5032 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04005033 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005034 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5035 INIT_LIST_HEAD(&dev->mode_config.property_list);
5036 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5037 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5038 idr_init(&dev->mode_config.crtc_idr);
5039
5040 drm_modeset_lock_all(dev);
5041 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04005042 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005043 drm_modeset_unlock_all(dev);
5044
5045 /* Just to be sure */
5046 dev->mode_config.num_fb = 0;
5047 dev->mode_config.num_connector = 0;
5048 dev->mode_config.num_crtc = 0;
5049 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005050 dev->mode_config.num_overlay_plane = 0;
5051 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005052}
5053EXPORT_SYMBOL(drm_mode_config_init);
5054
5055/**
5056 * drm_mode_config_cleanup - free up DRM mode_config info
5057 * @dev: DRM device
5058 *
5059 * Free up all the connectors and CRTCs associated with this DRM device, then
5060 * free up the framebuffers and associated buffer objects.
5061 *
5062 * Note that since this /should/ happen single-threaded at driver/device
5063 * teardown time, no locking is required. It's the driver's job to ensure that
5064 * this guarantee actually holds true.
5065 *
5066 * FIXME: cleanup any dangling user buffer objects too
5067 */
5068void drm_mode_config_cleanup(struct drm_device *dev)
5069{
5070 struct drm_connector *connector, *ot;
5071 struct drm_crtc *crtc, *ct;
5072 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04005073 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005074 struct drm_framebuffer *fb, *fbt;
5075 struct drm_property *property, *pt;
5076 struct drm_property_blob *blob, *bt;
5077 struct drm_plane *plane, *plt;
5078
5079 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5080 head) {
5081 encoder->funcs->destroy(encoder);
5082 }
5083
Sean Paul3b336ec2013-08-14 16:47:37 -04005084 list_for_each_entry_safe(bridge, brt,
5085 &dev->mode_config.bridge_list, head) {
5086 bridge->funcs->destroy(bridge);
5087 }
5088
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005089 list_for_each_entry_safe(connector, ot,
5090 &dev->mode_config.connector_list, head) {
5091 connector->funcs->destroy(connector);
5092 }
5093
5094 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5095 head) {
5096 drm_property_destroy(dev, property);
5097 }
5098
5099 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5100 head) {
5101 drm_property_destroy_blob(dev, blob);
5102 }
5103
5104 /*
5105 * Single-threaded teardown context, so it's not required to grab the
5106 * fb_lock to protect against concurrent fb_list access. Contrary, it
5107 * would actually deadlock with the drm_framebuffer_cleanup function.
5108 *
5109 * Also, if there are any framebuffers left, that's a driver leak now,
5110 * so politely WARN about this.
5111 */
5112 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5113 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5114 drm_framebuffer_remove(fb);
5115 }
5116
5117 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5118 head) {
5119 plane->funcs->destroy(plane);
5120 }
5121
5122 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5123 crtc->funcs->destroy(crtc);
5124 }
5125
5126 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005127 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005128}
5129EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305130
5131struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5132 unsigned int supported_rotations)
5133{
5134 static const struct drm_prop_enum_list props[] = {
5135 { DRM_ROTATE_0, "rotate-0" },
5136 { DRM_ROTATE_90, "rotate-90" },
5137 { DRM_ROTATE_180, "rotate-180" },
5138 { DRM_ROTATE_270, "rotate-270" },
5139 { DRM_REFLECT_X, "reflect-x" },
5140 { DRM_REFLECT_Y, "reflect-y" },
5141 };
5142
5143 return drm_property_create_bitmask(dev, 0, "rotation",
5144 props, ARRAY_SIZE(props),
5145 supported_rotations);
5146}
5147EXPORT_SYMBOL(drm_mode_create_rotation_property);