blob: f0a777747907eb2af0005d0f676147828082fb0b [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 Airlief453ba02008-11-07 14:05:41 -0800370/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100371 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800372 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100373 * @obj: object pointer, used to generate unique ID
374 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800375 *
Dave Airlief453ba02008-11-07 14:05:41 -0800376 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100377 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
378 * modeset identifiers are _not_ reference counted. Hence don't use this for
379 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800380 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100381 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800382 * New unique (relative to other objects in @dev) integer identifier for the
383 * object.
384 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100385int drm_mode_object_get(struct drm_device *dev,
386 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800387{
Dave Airlief453ba02008-11-07 14:05:41 -0800388 int ret;
389
Jesse Barnesad2563c2009-01-19 17:21:45 +1000390 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800391 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
392 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100393 /*
394 * Set up the object linking under the protection of the idr
395 * lock so that other users can't see inconsistent state.
396 */
Tejun Heo2e928812013-02-27 17:04:08 -0800397 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100398 obj->type = obj_type;
399 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000400 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100401
Tejun Heo2e928812013-02-27 17:04:08 -0800402 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800403}
404
405/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100406 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800407 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100408 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800409 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100410 * Free @id from @dev's unique identifier pool. Note that despite the _get
411 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
412 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800413 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100414void drm_mode_object_put(struct drm_device *dev,
415 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800416{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000417 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800418 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000419 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800420}
421
Rob Clark98f75de2014-05-30 11:37:03 -0400422static struct drm_mode_object *_object_find(struct drm_device *dev,
423 uint32_t id, uint32_t type)
424{
425 struct drm_mode_object *obj = NULL;
426
427 mutex_lock(&dev->mode_config.idr_mutex);
428 obj = idr_find(&dev->mode_config.crtc_idr, id);
429 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
430 (obj->id != id))
431 obj = NULL;
432 mutex_unlock(&dev->mode_config.idr_mutex);
433
434 return obj;
435}
436
Daniel Vetter786b99e2012-12-02 21:53:40 +0100437/**
438 * drm_mode_object_find - look up a drm object with static lifetime
439 * @dev: drm device
440 * @id: id of the mode object
441 * @type: type of the mode object
442 *
443 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400444 * are reference counted, they need special treatment. Even with
445 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
446 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100447 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200448struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
449 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800450{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000451 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800452
Daniel Vetter786b99e2012-12-02 21:53:40 +0100453 /* Framebuffers are reference counted and need their own lookup
454 * function.*/
455 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400456 obj = _object_find(dev, id, type);
457 /* don't leak out unref'd fb's */
458 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000459 obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800460 return obj;
461}
462EXPORT_SYMBOL(drm_mode_object_find);
463
464/**
Dave Airlief453ba02008-11-07 14:05:41 -0800465 * drm_framebuffer_init - initialize a framebuffer
466 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100467 * @fb: framebuffer to be initialized
468 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800469 *
Dave Airlief453ba02008-11-07 14:05:41 -0800470 * Allocates an ID for the framebuffer's parent mode object, sets its mode
471 * functions & device file and adds it to the master fd list.
472 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100473 * IMPORTANT:
474 * This functions publishes the fb and makes it available for concurrent access
475 * by other users. Which means by this point the fb _must_ be fully set up -
476 * since all the fb attributes are invariant over its lifetime, no further
477 * locking but only correct reference counting is required.
478 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100479 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200480 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800481 */
482int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
483 const struct drm_framebuffer_funcs *funcs)
484{
485 int ret;
486
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100487 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000488 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100489 INIT_LIST_HEAD(&fb->filp_head);
490 fb->dev = dev;
491 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000492
Dave Airlief453ba02008-11-07 14:05:41 -0800493 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200494 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100495 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800496
Daniel Vetter2b677e82012-12-10 21:16:05 +0100497 /* Grab the idr reference. */
498 drm_framebuffer_reference(fb);
499
Dave Airlief453ba02008-11-07 14:05:41 -0800500 dev->mode_config.num_fb++;
501 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100502out:
503 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800504
505 return 0;
506}
507EXPORT_SYMBOL(drm_framebuffer_init);
508
Rob Clarkf7eff602012-09-05 21:48:38 +0000509static void drm_framebuffer_free(struct kref *kref)
510{
511 struct drm_framebuffer *fb =
512 container_of(kref, struct drm_framebuffer, refcount);
513 fb->funcs->destroy(fb);
514}
515
Daniel Vetter2b677e82012-12-10 21:16:05 +0100516static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
517 uint32_t id)
518{
519 struct drm_mode_object *obj = NULL;
520 struct drm_framebuffer *fb;
521
522 mutex_lock(&dev->mode_config.idr_mutex);
523 obj = idr_find(&dev->mode_config.crtc_idr, id);
524 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
525 fb = NULL;
526 else
527 fb = obj_to_fb(obj);
528 mutex_unlock(&dev->mode_config.idr_mutex);
529
530 return fb;
531}
532
Rob Clarkf7eff602012-09-05 21:48:38 +0000533/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100534 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
535 * @dev: drm device
536 * @id: id of the fb object
537 *
538 * If successful, this grabs an additional reference to the framebuffer -
539 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100540 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100541 */
542struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
543 uint32_t id)
544{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100545 struct drm_framebuffer *fb;
546
547 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100548 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100549 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000550 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100551 mutex_unlock(&dev->mode_config.fb_lock);
552
553 return fb;
554}
555EXPORT_SYMBOL(drm_framebuffer_lookup);
556
557/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000558 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100559 * @fb: framebuffer to unref
560 *
561 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000562 */
563void drm_framebuffer_unreference(struct drm_framebuffer *fb)
564{
Rob Clark82912722014-03-18 10:07:08 -0400565 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000566 kref_put(&fb->refcount, drm_framebuffer_free);
567}
568EXPORT_SYMBOL(drm_framebuffer_unreference);
569
570/**
571 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100572 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100573 *
574 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000575 */
576void drm_framebuffer_reference(struct drm_framebuffer *fb)
577{
Rob Clark82912722014-03-18 10:07:08 -0400578 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000579 kref_get(&fb->refcount);
580}
581EXPORT_SYMBOL(drm_framebuffer_reference);
582
Daniel Vetter2b677e82012-12-10 21:16:05 +0100583static void drm_framebuffer_free_bug(struct kref *kref)
584{
585 BUG();
586}
587
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100588static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
589{
Rob Clark82912722014-03-18 10:07:08 -0400590 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100591 kref_put(&fb->refcount, drm_framebuffer_free_bug);
592}
593
Daniel Vetter2b677e82012-12-10 21:16:05 +0100594/* dev->mode_config.fb_lock must be held! */
595static void __drm_framebuffer_unregister(struct drm_device *dev,
596 struct drm_framebuffer *fb)
597{
598 mutex_lock(&dev->mode_config.idr_mutex);
599 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
600 mutex_unlock(&dev->mode_config.idr_mutex);
601
602 fb->base.id = 0;
603
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100604 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100605}
606
Dave Airlief453ba02008-11-07 14:05:41 -0800607/**
Daniel Vetter36206362012-12-10 20:42:17 +0100608 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
609 * @fb: fb to unregister
610 *
611 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
612 * those used for fbdev. Note that the caller must hold a reference of it's own,
613 * i.e. the object may not be destroyed through this call (since it'll lead to a
614 * locking inversion).
615 */
616void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
617{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100618 struct drm_device *dev = fb->dev;
619
620 mutex_lock(&dev->mode_config.fb_lock);
621 /* Mark fb as reaped and drop idr ref. */
622 __drm_framebuffer_unregister(dev, fb);
623 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100624}
625EXPORT_SYMBOL(drm_framebuffer_unregister_private);
626
627/**
Dave Airlief453ba02008-11-07 14:05:41 -0800628 * drm_framebuffer_cleanup - remove a framebuffer object
629 * @fb: framebuffer to remove
630 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100631 * Cleanup framebuffer. This function is intended to be used from the drivers
632 * ->destroy callback. It can also be used to clean up driver private
633 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100634 *
635 * Note that this function does not remove the fb from active usuage - if it is
636 * still used anywhere, hilarity can ensue since userspace could call getfb on
637 * the id and get back -EINVAL. Obviously no concern at driver unload time.
638 *
639 * Also, the framebuffer will not be removed from the lookup idr - for
640 * user-created framebuffers this will happen in in the rmfb ioctl. For
641 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
642 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800643 */
644void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
645{
646 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100647
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100648 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000649 list_del(&fb->head);
650 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100651 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000652}
653EXPORT_SYMBOL(drm_framebuffer_cleanup);
654
655/**
656 * drm_framebuffer_remove - remove and unreference a framebuffer object
657 * @fb: framebuffer to remove
658 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000659 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100660 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100661 * passed-in framebuffer. Might take the modeset locks.
662 *
663 * Note that this function optimizes the cleanup away if the caller holds the
664 * last reference to the framebuffer. It is also guaranteed to not take the
665 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000666 */
667void drm_framebuffer_remove(struct drm_framebuffer *fb)
668{
669 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800670 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800671 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000672 struct drm_mode_set set;
673 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800674
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100675 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100676
Daniel Vetterb62584e2012-12-11 16:51:35 +0100677 /*
678 * drm ABI mandates that we remove any deleted framebuffers from active
679 * useage. But since most sane clients only remove framebuffers they no
680 * longer need, try to optimize this away.
681 *
682 * Since we're holding a reference ourselves, observing a refcount of 1
683 * means that we're the last holder and can skip it. Also, the refcount
684 * can never increase from 1 again, so we don't need any barriers or
685 * locks.
686 *
687 * Note that userspace could try to race with use and instate a new
688 * usage _after_ we've cleared all current ones. End result will be an
689 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
690 * in this manner.
691 */
692 if (atomic_read(&fb->refcount.refcount) > 1) {
693 drm_modeset_lock_all(dev);
694 /* remove from any CRTC */
695 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700696 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100697 /* should turn off the crtc */
698 memset(&set, 0, sizeof(struct drm_mode_set));
699 set.crtc = crtc;
700 set.fb = NULL;
701 ret = drm_mode_set_config_internal(&set);
702 if (ret)
703 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
704 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000705 }
Dave Airlief453ba02008-11-07 14:05:41 -0800706
Daniel Vetterb62584e2012-12-11 16:51:35 +0100707 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300708 if (plane->fb == fb)
709 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800710 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100711 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800712 }
713
Rob Clarkf7eff602012-09-05 21:48:38 +0000714 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800715}
Rob Clarkf7eff602012-09-05 21:48:38 +0000716EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800717
Rob Clark51fd3712013-11-19 12:10:12 -0500718DEFINE_WW_CLASS(crtc_ww_class);
719
Dave Airlief453ba02008-11-07 14:05:41 -0800720/**
Matt Ropere13161a2014-04-01 15:22:38 -0700721 * drm_crtc_init_with_planes - Initialise a new CRTC object with
722 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800723 * @dev: DRM device
724 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700725 * @primary: Primary plane for CRTC
726 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800727 * @funcs: callbacks for the new CRTC
728 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000729 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200730 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100731 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200732 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800733 */
Matt Ropere13161a2014-04-01 15:22:38 -0700734int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
735 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700736 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700737 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800738{
Rob Clark51fd3712013-11-19 12:10:12 -0500739 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200740 int ret;
741
Dave Airlief453ba02008-11-07 14:05:41 -0800742 crtc->dev = dev;
743 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000744 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800745
Daniel Vetter84849902012-12-02 00:28:11 +0100746 drm_modeset_lock_all(dev);
Rob Clark51fd3712013-11-19 12:10:12 -0500747 drm_modeset_lock_init(&crtc->mutex);
748 /* dropped by _unlock_all(): */
749 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200750
751 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
752 if (ret)
753 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800754
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300755 crtc->base.properties = &crtc->properties;
756
Rob Clark51fd3712013-11-19 12:10:12 -0500757 list_add_tail(&crtc->head, &config->crtc_list);
758 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200759
Matt Ropere13161a2014-04-01 15:22:38 -0700760 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700761 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700762 if (primary)
763 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700764 if (cursor)
765 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700766
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200767 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100768 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200769
770 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800771}
Matt Ropere13161a2014-04-01 15:22:38 -0700772EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800773
774/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000775 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800776 * @crtc: CRTC to cleanup
777 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000778 * This function cleans up @crtc and removes it from the DRM mode setting
779 * core. Note that the function does *not* free the crtc structure itself,
780 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800781 */
782void drm_crtc_cleanup(struct drm_crtc *crtc)
783{
784 struct drm_device *dev = crtc->dev;
785
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000786 kfree(crtc->gamma_store);
787 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800788
Rob Clark51fd3712013-11-19 12:10:12 -0500789 drm_modeset_lock_fini(&crtc->mutex);
790
Dave Airlief453ba02008-11-07 14:05:41 -0800791 drm_mode_object_put(dev, &crtc->base);
792 list_del(&crtc->head);
793 dev->mode_config.num_crtc--;
794}
795EXPORT_SYMBOL(drm_crtc_cleanup);
796
797/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000798 * drm_crtc_index - find the index of a registered CRTC
799 * @crtc: CRTC to find index for
800 *
801 * Given a registered CRTC, return the index of that CRTC within a DRM
802 * device's list of CRTCs.
803 */
804unsigned int drm_crtc_index(struct drm_crtc *crtc)
805{
806 unsigned int index = 0;
807 struct drm_crtc *tmp;
808
809 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
810 if (tmp == crtc)
811 return index;
812
813 index++;
814 }
815
816 BUG();
817}
818EXPORT_SYMBOL(drm_crtc_index);
819
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100820/*
Dave Airlief453ba02008-11-07 14:05:41 -0800821 * drm_mode_remove - remove and free a mode
822 * @connector: connector list to modify
823 * @mode: mode to remove
824 *
Dave Airlief453ba02008-11-07 14:05:41 -0800825 * Remove @mode from @connector's mode list, then free it.
826 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100827static void drm_mode_remove(struct drm_connector *connector,
828 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800829{
830 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100831 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800832}
Dave Airlief453ba02008-11-07 14:05:41 -0800833
834/**
835 * drm_connector_init - Init a preallocated connector
836 * @dev: DRM device
837 * @connector: the connector to init
838 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100839 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800840 *
Dave Airlief453ba02008-11-07 14:05:41 -0800841 * Initialises a preallocated connector. Connectors should be
842 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200843 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100844 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200845 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800846 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200847int drm_connector_init(struct drm_device *dev,
848 struct drm_connector *connector,
849 const struct drm_connector_funcs *funcs,
850 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800851{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200852 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400853 struct ida *connector_ida =
854 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200855
Daniel Vetter84849902012-12-02 00:28:11 +0100856 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800857
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200858 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
859 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300860 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200861
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300862 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800863 connector->dev = dev;
864 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800865 connector->connector_type = connector_type;
866 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400867 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
868 if (connector->connector_type_id < 0) {
869 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300870 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400871 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300872 connector->name =
873 kasprintf(GFP_KERNEL, "%s-%d",
874 drm_connector_enum_list[connector_type].name,
875 connector->connector_type_id);
876 if (!connector->name) {
877 ret = -ENOMEM;
878 goto out_put;
879 }
880
Dave Airlief453ba02008-11-07 14:05:41 -0800881 INIT_LIST_HEAD(&connector->probed_modes);
882 INIT_LIST_HEAD(&connector->modes);
883 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000884 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800885
886 list_add_tail(&connector->head, &dev->mode_config.connector_list);
887 dev->mode_config.num_connector++;
888
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200889 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500890 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200891 dev->mode_config.edid_property,
892 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800893
Rob Clark58495562012-10-11 20:50:56 -0500894 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800895 dev->mode_config.dpms_property, 0);
896
Jani Nikula2abdd312014-05-14 16:58:19 +0300897out_put:
898 if (ret)
899 drm_mode_object_put(dev, &connector->base);
900
901out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100902 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200903
904 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800905}
906EXPORT_SYMBOL(drm_connector_init);
907
908/**
909 * drm_connector_cleanup - cleans up an initialised connector
910 * @connector: connector to cleanup
911 *
Dave Airlief453ba02008-11-07 14:05:41 -0800912 * Cleans up the connector but doesn't free the object.
913 */
914void drm_connector_cleanup(struct drm_connector *connector)
915{
916 struct drm_device *dev = connector->dev;
917 struct drm_display_mode *mode, *t;
918
919 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
920 drm_mode_remove(connector, mode);
921
922 list_for_each_entry_safe(mode, t, &connector->modes, head)
923 drm_mode_remove(connector, mode);
924
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400925 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
926 connector->connector_type_id);
927
Dave Airlief453ba02008-11-07 14:05:41 -0800928 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300929 kfree(connector->name);
930 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800931 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000932 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800933}
934EXPORT_SYMBOL(drm_connector_cleanup);
935
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100936/**
937 * drm_connector_unplug_all - unregister connector userspace interfaces
938 * @dev: drm device
939 *
940 * This function unregisters all connector userspace interfaces in sysfs. Should
941 * be call when the device is disconnected, e.g. from an usb driver's
942 * ->disconnect callback.
943 */
Dave Airliecbc7e222012-02-20 14:16:40 +0000944void drm_connector_unplug_all(struct drm_device *dev)
945{
946 struct drm_connector *connector;
947
948 /* taking the mode config mutex ends up in a clash with sysfs */
949 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
950 drm_sysfs_connector_remove(connector);
951
952}
953EXPORT_SYMBOL(drm_connector_unplug_all);
954
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100955/**
956 * drm_bridge_init - initialize a drm transcoder/bridge
957 * @dev: drm device
958 * @bridge: transcoder/bridge to set up
959 * @funcs: bridge function table
960 *
961 * Initialises a preallocated bridge. Bridges should be
962 * subclassed as part of driver connector objects.
963 *
964 * Returns:
965 * Zero on success, error code on failure.
966 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400967int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
968 const struct drm_bridge_funcs *funcs)
969{
970 int ret;
971
972 drm_modeset_lock_all(dev);
973
974 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
975 if (ret)
976 goto out;
977
978 bridge->dev = dev;
979 bridge->funcs = funcs;
980
981 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
982 dev->mode_config.num_bridge++;
983
984 out:
985 drm_modeset_unlock_all(dev);
986 return ret;
987}
988EXPORT_SYMBOL(drm_bridge_init);
989
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100990/**
991 * drm_bridge_cleanup - cleans up an initialised bridge
992 * @bridge: bridge to cleanup
993 *
994 * Cleans up the bridge but doesn't free the object.
995 */
Sean Paul3b336ec2013-08-14 16:47:37 -0400996void drm_bridge_cleanup(struct drm_bridge *bridge)
997{
998 struct drm_device *dev = bridge->dev;
999
1000 drm_modeset_lock_all(dev);
1001 drm_mode_object_put(dev, &bridge->base);
1002 list_del(&bridge->head);
1003 dev->mode_config.num_bridge--;
1004 drm_modeset_unlock_all(dev);
1005}
1006EXPORT_SYMBOL(drm_bridge_cleanup);
1007
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001008/**
1009 * drm_encoder_init - Init a preallocated encoder
1010 * @dev: drm device
1011 * @encoder: the encoder to init
1012 * @funcs: callbacks for this encoder
1013 * @encoder_type: user visible type of the encoder
1014 *
1015 * Initialises a preallocated encoder. Encoder should be
1016 * subclassed as part of driver encoder objects.
1017 *
1018 * Returns:
1019 * Zero on success, error code on failure.
1020 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001021int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001022 struct drm_encoder *encoder,
1023 const struct drm_encoder_funcs *funcs,
1024 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001025{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001026 int ret;
1027
Daniel Vetter84849902012-12-02 00:28:11 +01001028 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001029
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001030 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1031 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001032 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001033
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001034 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001035 encoder->encoder_type = encoder_type;
1036 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001037 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1038 drm_encoder_enum_list[encoder_type].name,
1039 encoder->base.id);
1040 if (!encoder->name) {
1041 ret = -ENOMEM;
1042 goto out_put;
1043 }
Dave Airlief453ba02008-11-07 14:05:41 -08001044
1045 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1046 dev->mode_config.num_encoder++;
1047
Jani Nikulae5748942014-05-14 16:58:20 +03001048out_put:
1049 if (ret)
1050 drm_mode_object_put(dev, &encoder->base);
1051
1052out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001053 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001054
1055 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001056}
1057EXPORT_SYMBOL(drm_encoder_init);
1058
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001059/**
1060 * drm_encoder_cleanup - cleans up an initialised encoder
1061 * @encoder: encoder to cleanup
1062 *
1063 * Cleans up the encoder but doesn't free the object.
1064 */
Dave Airlief453ba02008-11-07 14:05:41 -08001065void drm_encoder_cleanup(struct drm_encoder *encoder)
1066{
1067 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001068 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001069 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001070 kfree(encoder->name);
1071 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001072 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001073 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001074 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001075}
1076EXPORT_SYMBOL(drm_encoder_cleanup);
1077
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001078/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001079 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001080 * @dev: DRM device
1081 * @plane: plane object to init
1082 * @possible_crtcs: bitmask of possible CRTCs
1083 * @funcs: callbacks for the new plane
1084 * @formats: array of supported formats (%DRM_FORMAT_*)
1085 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001086 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001087 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001088 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001089 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001090 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001091 * Zero on success, error code on failure.
1092 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001093int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1094 unsigned long possible_crtcs,
1095 const struct drm_plane_funcs *funcs,
1096 const uint32_t *formats, uint32_t format_count,
1097 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001098{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001099 int ret;
1100
Daniel Vetter84849902012-12-02 00:28:11 +01001101 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001102
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001103 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1104 if (ret)
1105 goto out;
1106
Rob Clark4d939142012-05-17 02:23:27 -06001107 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001108 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001109 plane->funcs = funcs;
1110 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1111 GFP_KERNEL);
1112 if (!plane->format_types) {
1113 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1114 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001115 ret = -ENOMEM;
1116 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001117 }
1118
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001119 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001120 plane->format_count = format_count;
1121 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001122 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001123
Matt Roperdc415ff2014-04-01 15:22:36 -07001124 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1125 dev->mode_config.num_total_plane++;
1126 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1127 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001128
Rob Clark9922ab52014-04-01 20:16:57 -04001129 drm_object_attach_property(&plane->base,
1130 dev->mode_config.plane_type_property,
1131 plane->type);
1132
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001133 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001134 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001135
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001136 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001137}
Matt Roperdc415ff2014-04-01 15:22:36 -07001138EXPORT_SYMBOL(drm_universal_plane_init);
1139
1140/**
1141 * drm_plane_init - Initialize a legacy plane
1142 * @dev: DRM device
1143 * @plane: plane object to init
1144 * @possible_crtcs: bitmask of possible CRTCs
1145 * @funcs: callbacks for the new plane
1146 * @formats: array of supported formats (%DRM_FORMAT_*)
1147 * @format_count: number of elements in @formats
1148 * @is_primary: plane type (primary vs overlay)
1149 *
1150 * Legacy API to initialize a DRM plane.
1151 *
1152 * New drivers should call drm_universal_plane_init() instead.
1153 *
1154 * Returns:
1155 * Zero on success, error code on failure.
1156 */
1157int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1158 unsigned long possible_crtcs,
1159 const struct drm_plane_funcs *funcs,
1160 const uint32_t *formats, uint32_t format_count,
1161 bool is_primary)
1162{
1163 enum drm_plane_type type;
1164
1165 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1166 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1167 formats, format_count, type);
1168}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001169EXPORT_SYMBOL(drm_plane_init);
1170
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001171/**
1172 * drm_plane_cleanup - Clean up the core plane usage
1173 * @plane: plane to cleanup
1174 *
1175 * This function cleans up @plane and removes it from the DRM mode setting
1176 * core. Note that the function does *not* free the plane structure itself,
1177 * this is the responsibility of the caller.
1178 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001179void drm_plane_cleanup(struct drm_plane *plane)
1180{
1181 struct drm_device *dev = plane->dev;
1182
Daniel Vetter84849902012-12-02 00:28:11 +01001183 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001184 kfree(plane->format_types);
1185 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001186
1187 BUG_ON(list_empty(&plane->head));
1188
1189 list_del(&plane->head);
1190 dev->mode_config.num_total_plane--;
1191 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1192 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001193 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001194}
1195EXPORT_SYMBOL(drm_plane_cleanup);
1196
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001197/**
1198 * drm_plane_force_disable - Forcibly disable a plane
1199 * @plane: plane to disable
1200 *
1201 * Forces the plane to be disabled.
1202 *
1203 * Used when the plane's current framebuffer is destroyed,
1204 * and when restoring fbdev mode.
1205 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001206void drm_plane_force_disable(struct drm_plane *plane)
1207{
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001208 struct drm_framebuffer *old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001209 int ret;
1210
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001211 if (!old_fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001212 return;
1213
1214 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001215 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001216 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter731cce42014-04-23 10:24:11 +02001217 return;
1218 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001219 /* disconnect the plane from the fb and crtc: */
Daniel Vetter0fe27f02014-04-23 17:34:06 +02001220 __drm_framebuffer_unreference(old_fb);
Ville Syrjälä9125e612013-06-03 16:10:40 +03001221 plane->fb = NULL;
1222 plane->crtc = NULL;
1223}
1224EXPORT_SYMBOL(drm_plane_force_disable);
1225
Dave Airlief453ba02008-11-07 14:05:41 -08001226static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1227{
1228 struct drm_property *edid;
1229 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -08001230
1231 /*
1232 * Standard properties (apply to all connectors)
1233 */
1234 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1235 DRM_MODE_PROP_IMMUTABLE,
1236 "EDID", 0);
1237 dev->mode_config.edid_property = edid;
1238
Sascha Hauer4a67d392012-02-06 10:58:17 +01001239 dpms = drm_property_create_enum(dev, 0,
1240 "DPMS", drm_dpms_enum_list,
1241 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001242 dev->mode_config.dpms_property = dpms;
1243
1244 return 0;
1245}
1246
Rob Clark9922ab52014-04-01 20:16:57 -04001247static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1248{
1249 struct drm_property *type;
1250
1251 /*
1252 * Standard properties (apply to all planes)
1253 */
1254 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1255 "type", drm_plane_type_enum_list,
1256 ARRAY_SIZE(drm_plane_type_enum_list));
1257 dev->mode_config.plane_type_property = type;
1258
1259 return 0;
1260}
1261
Dave Airlief453ba02008-11-07 14:05:41 -08001262/**
1263 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1264 * @dev: DRM device
1265 *
1266 * Called by a driver the first time a DVI-I connector is made.
1267 */
1268int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1269{
1270 struct drm_property *dvi_i_selector;
1271 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001272
1273 if (dev->mode_config.dvi_i_select_subconnector_property)
1274 return 0;
1275
1276 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001277 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001278 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001279 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001280 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001281 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1282
Sascha Hauer4a67d392012-02-06 10:58:17 +01001283 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001284 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001285 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001286 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001287 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1288
1289 return 0;
1290}
1291EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1292
1293/**
1294 * drm_create_tv_properties - create TV specific connector properties
1295 * @dev: DRM device
1296 * @num_modes: number of different TV formats (modes) supported
1297 * @modes: array of pointers to strings containing name of each format
1298 *
1299 * Called by a driver's TV initialization routine, this function creates
1300 * the TV specific connector properties for a given device. Caller is
1301 * responsible for allocating a list of format names and passing them to
1302 * this routine.
1303 */
1304int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1305 char *modes[])
1306{
1307 struct drm_property *tv_selector;
1308 struct drm_property *tv_subconnector;
1309 int i;
1310
1311 if (dev->mode_config.tv_select_subconnector_property)
1312 return 0;
1313
1314 /*
1315 * Basic connector properties
1316 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001317 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001318 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001319 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001320 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001321 dev->mode_config.tv_select_subconnector_property = tv_selector;
1322
1323 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001324 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1325 "subconnector",
1326 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001327 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001328 dev->mode_config.tv_subconnector_property = tv_subconnector;
1329
1330 /*
1331 * Other, TV specific properties: margins & TV modes.
1332 */
1333 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001334 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001335
1336 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001337 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001338
1339 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001340 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001341
1342 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001343 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001344
1345 dev->mode_config.tv_mode_property =
1346 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1347 "mode", num_modes);
1348 for (i = 0; i < num_modes; i++)
1349 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1350 i, modes[i]);
1351
Francisco Jerezb6b79022009-08-02 04:19:20 +02001352 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001353 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001354
1355 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001356 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001357
1358 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001359 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001360
Francisco Jereza75f0232009-08-12 02:30:10 +02001361 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001362 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001363
1364 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001365 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001366
1367 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001368 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001369
Dave Airlief453ba02008-11-07 14:05:41 -08001370 return 0;
1371}
1372EXPORT_SYMBOL(drm_mode_create_tv_properties);
1373
1374/**
1375 * drm_mode_create_scaling_mode_property - create scaling mode property
1376 * @dev: DRM device
1377 *
1378 * Called by a driver the first time it's needed, must be attached to desired
1379 * connectors.
1380 */
1381int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1382{
1383 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001384
1385 if (dev->mode_config.scaling_mode_property)
1386 return 0;
1387
1388 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001389 drm_property_create_enum(dev, 0, "scaling mode",
1390 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001391 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001392
1393 dev->mode_config.scaling_mode_property = scaling_mode;
1394
1395 return 0;
1396}
1397EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1398
1399/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301400 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1401 * @dev: DRM device
1402 *
1403 * Called by a driver the first time it's needed, must be attached to desired
1404 * connectors.
1405 *
1406 * Returns:
1407 * Zero on success, errno on failure.
1408 */
1409int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1410{
1411 if (dev->mode_config.aspect_ratio_property)
1412 return 0;
1413
1414 dev->mode_config.aspect_ratio_property =
1415 drm_property_create_enum(dev, 0, "aspect ratio",
1416 drm_aspect_ratio_enum_list,
1417 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1418
1419 if (dev->mode_config.aspect_ratio_property == NULL)
1420 return -ENOMEM;
1421
1422 return 0;
1423}
1424EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1425
1426/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001427 * drm_mode_create_dirty_property - create dirty property
1428 * @dev: DRM device
1429 *
1430 * Called by a driver the first time it's needed, must be attached to desired
1431 * connectors.
1432 */
1433int drm_mode_create_dirty_info_property(struct drm_device *dev)
1434{
1435 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001436
1437 if (dev->mode_config.dirty_info_property)
1438 return 0;
1439
1440 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001441 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001442 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001443 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001444 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001445 dev->mode_config.dirty_info_property = dirty_info;
1446
1447 return 0;
1448}
1449EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1450
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001451static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001452{
1453 uint32_t total_objects = 0;
1454
1455 total_objects += dev->mode_config.num_crtc;
1456 total_objects += dev->mode_config.num_connector;
1457 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001458 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001459
Dave Airlief453ba02008-11-07 14:05:41 -08001460 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1461 if (!group->id_list)
1462 return -ENOMEM;
1463
1464 group->num_crtcs = 0;
1465 group->num_connectors = 0;
1466 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001467 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001468 return 0;
1469}
1470
Dave Airliead222792014-05-02 13:22:19 +10001471void drm_mode_group_destroy(struct drm_mode_group *group)
1472{
1473 kfree(group->id_list);
1474 group->id_list = NULL;
1475}
1476
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001477/*
1478 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1479 * the drm core's responsibility to set up mode control groups.
1480 */
Dave Airlief453ba02008-11-07 14:05:41 -08001481int drm_mode_group_init_legacy_group(struct drm_device *dev,
1482 struct drm_mode_group *group)
1483{
1484 struct drm_crtc *crtc;
1485 struct drm_encoder *encoder;
1486 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001487 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001488 int ret;
1489
1490 if ((ret = drm_mode_group_init(dev, group)))
1491 return ret;
1492
1493 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1494 group->id_list[group->num_crtcs++] = crtc->base.id;
1495
1496 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1497 group->id_list[group->num_crtcs + group->num_encoders++] =
1498 encoder->base.id;
1499
1500 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1501 group->id_list[group->num_crtcs + group->num_encoders +
1502 group->num_connectors++] = connector->base.id;
1503
Sean Paul3b336ec2013-08-14 16:47:37 -04001504 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1505 group->id_list[group->num_crtcs + group->num_encoders +
1506 group->num_connectors + group->num_bridges++] =
1507 bridge->base.id;
1508
Dave Airlief453ba02008-11-07 14:05:41 -08001509 return 0;
1510}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001511EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001512
1513/**
Dave Airlief453ba02008-11-07 14:05:41 -08001514 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1515 * @out: drm_mode_modeinfo struct to return to the user
1516 * @in: drm_display_mode to use
1517 *
Dave Airlief453ba02008-11-07 14:05:41 -08001518 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1519 * the user.
1520 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001521static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1522 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001523{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001524 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1525 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1526 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1527 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1528 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1529 "timing values too large for mode info\n");
1530
Dave Airlief453ba02008-11-07 14:05:41 -08001531 out->clock = in->clock;
1532 out->hdisplay = in->hdisplay;
1533 out->hsync_start = in->hsync_start;
1534 out->hsync_end = in->hsync_end;
1535 out->htotal = in->htotal;
1536 out->hskew = in->hskew;
1537 out->vdisplay = in->vdisplay;
1538 out->vsync_start = in->vsync_start;
1539 out->vsync_end = in->vsync_end;
1540 out->vtotal = in->vtotal;
1541 out->vscan = in->vscan;
1542 out->vrefresh = in->vrefresh;
1543 out->flags = in->flags;
1544 out->type = in->type;
1545 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1546 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1547}
1548
1549/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001550 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001551 * @out: drm_display_mode to return to the user
1552 * @in: drm_mode_modeinfo to use
1553 *
Dave Airlief453ba02008-11-07 14:05:41 -08001554 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1555 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001556 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001557 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001558 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001559 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001560static int drm_crtc_convert_umode(struct drm_display_mode *out,
1561 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001562{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001563 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1564 return -ERANGE;
1565
Damien Lespiau5848ad42013-09-27 12:11:50 +01001566 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1567 return -EINVAL;
1568
Dave Airlief453ba02008-11-07 14:05:41 -08001569 out->clock = in->clock;
1570 out->hdisplay = in->hdisplay;
1571 out->hsync_start = in->hsync_start;
1572 out->hsync_end = in->hsync_end;
1573 out->htotal = in->htotal;
1574 out->hskew = in->hskew;
1575 out->vdisplay = in->vdisplay;
1576 out->vsync_start = in->vsync_start;
1577 out->vsync_end = in->vsync_end;
1578 out->vtotal = in->vtotal;
1579 out->vscan = in->vscan;
1580 out->vrefresh = in->vrefresh;
1581 out->flags = in->flags;
1582 out->type = in->type;
1583 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1584 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001585
1586 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001587}
1588
1589/**
1590 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001591 * @dev: drm device for the ioctl
1592 * @data: data pointer for the ioctl
1593 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001594 *
Dave Airlief453ba02008-11-07 14:05:41 -08001595 * Construct a set of configuration description structures and return
1596 * them to the user, including CRTC, connector and framebuffer configuration.
1597 *
1598 * Called by the user via ioctl.
1599 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001600 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001601 * Zero on success, errno on failure.
1602 */
1603int drm_mode_getresources(struct drm_device *dev, void *data,
1604 struct drm_file *file_priv)
1605{
1606 struct drm_mode_card_res *card_res = data;
1607 struct list_head *lh;
1608 struct drm_framebuffer *fb;
1609 struct drm_connector *connector;
1610 struct drm_crtc *crtc;
1611 struct drm_encoder *encoder;
1612 int ret = 0;
1613 int connector_count = 0;
1614 int crtc_count = 0;
1615 int fb_count = 0;
1616 int encoder_count = 0;
1617 int copied = 0, i;
1618 uint32_t __user *fb_id;
1619 uint32_t __user *crtc_id;
1620 uint32_t __user *connector_id;
1621 uint32_t __user *encoder_id;
1622 struct drm_mode_group *mode_group;
1623
Dave Airliefb3b06c2011-02-08 13:55:21 +10001624 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1625 return -EINVAL;
1626
Dave Airlief453ba02008-11-07 14:05:41 -08001627
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001628 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001629 /*
1630 * For the non-control nodes we need to limit the list of resources
1631 * by IDs in the group list for this node
1632 */
1633 list_for_each(lh, &file_priv->fbs)
1634 fb_count++;
1635
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001636 /* handle this in 4 parts */
1637 /* FBs */
1638 if (card_res->count_fbs >= fb_count) {
1639 copied = 0;
1640 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1641 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1642 if (put_user(fb->base.id, fb_id + copied)) {
1643 mutex_unlock(&file_priv->fbs_lock);
1644 return -EFAULT;
1645 }
1646 copied++;
1647 }
1648 }
1649 card_res->count_fbs = fb_count;
1650 mutex_unlock(&file_priv->fbs_lock);
1651
1652 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001653 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001654
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001655 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001656 list_for_each(lh, &dev->mode_config.crtc_list)
1657 crtc_count++;
1658
1659 list_for_each(lh, &dev->mode_config.connector_list)
1660 connector_count++;
1661
1662 list_for_each(lh, &dev->mode_config.encoder_list)
1663 encoder_count++;
1664 } else {
1665
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001666 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001667 crtc_count = mode_group->num_crtcs;
1668 connector_count = mode_group->num_connectors;
1669 encoder_count = mode_group->num_encoders;
1670 }
1671
1672 card_res->max_height = dev->mode_config.max_height;
1673 card_res->min_height = dev->mode_config.min_height;
1674 card_res->max_width = dev->mode_config.max_width;
1675 card_res->min_width = dev->mode_config.min_width;
1676
Dave Airlief453ba02008-11-07 14:05:41 -08001677 /* CRTCs */
1678 if (card_res->count_crtcs >= crtc_count) {
1679 copied = 0;
1680 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001681 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001682 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1683 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001684 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001685 if (put_user(crtc->base.id, crtc_id + copied)) {
1686 ret = -EFAULT;
1687 goto out;
1688 }
1689 copied++;
1690 }
1691 } else {
1692 for (i = 0; i < mode_group->num_crtcs; i++) {
1693 if (put_user(mode_group->id_list[i],
1694 crtc_id + copied)) {
1695 ret = -EFAULT;
1696 goto out;
1697 }
1698 copied++;
1699 }
1700 }
1701 }
1702 card_res->count_crtcs = crtc_count;
1703
1704 /* Encoders */
1705 if (card_res->count_encoders >= encoder_count) {
1706 copied = 0;
1707 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001708 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001709 list_for_each_entry(encoder,
1710 &dev->mode_config.encoder_list,
1711 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001712 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001713 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001714 if (put_user(encoder->base.id, encoder_id +
1715 copied)) {
1716 ret = -EFAULT;
1717 goto out;
1718 }
1719 copied++;
1720 }
1721 } else {
1722 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1723 if (put_user(mode_group->id_list[i],
1724 encoder_id + copied)) {
1725 ret = -EFAULT;
1726 goto out;
1727 }
1728 copied++;
1729 }
1730
1731 }
1732 }
1733 card_res->count_encoders = encoder_count;
1734
1735 /* Connectors */
1736 if (card_res->count_connectors >= connector_count) {
1737 copied = 0;
1738 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001739 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001740 list_for_each_entry(connector,
1741 &dev->mode_config.connector_list,
1742 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001743 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1744 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001745 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001746 if (put_user(connector->base.id,
1747 connector_id + copied)) {
1748 ret = -EFAULT;
1749 goto out;
1750 }
1751 copied++;
1752 }
1753 } else {
1754 int start = mode_group->num_crtcs +
1755 mode_group->num_encoders;
1756 for (i = start; i < start + mode_group->num_connectors; i++) {
1757 if (put_user(mode_group->id_list[i],
1758 connector_id + copied)) {
1759 ret = -EFAULT;
1760 goto out;
1761 }
1762 copied++;
1763 }
1764 }
1765 }
1766 card_res->count_connectors = connector_count;
1767
Jerome Glisse94401062010-07-15 15:43:25 -04001768 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001769 card_res->count_connectors, card_res->count_encoders);
1770
1771out:
Daniel Vetter84849902012-12-02 00:28:11 +01001772 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001773 return ret;
1774}
1775
1776/**
1777 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001778 * @dev: drm device for the ioctl
1779 * @data: data pointer for the ioctl
1780 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001781 *
Dave Airlief453ba02008-11-07 14:05:41 -08001782 * Construct a CRTC configuration structure to return to the user.
1783 *
1784 * Called by the user via ioctl.
1785 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001786 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001787 * Zero on success, errno on failure.
1788 */
1789int drm_mode_getcrtc(struct drm_device *dev,
1790 void *data, struct drm_file *file_priv)
1791{
1792 struct drm_mode_crtc *crtc_resp = data;
1793 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001794 int ret = 0;
1795
Dave Airliefb3b06c2011-02-08 13:55:21 +10001796 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1797 return -EINVAL;
1798
Daniel Vetter84849902012-12-02 00:28:11 +01001799 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001800
Rob Clarka2b34e22013-10-05 16:36:52 -04001801 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1802 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001803 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001804 goto out;
1805 }
Dave Airlief453ba02008-11-07 14:05:41 -08001806
1807 crtc_resp->x = crtc->x;
1808 crtc_resp->y = crtc->y;
1809 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001810 if (crtc->primary->fb)
1811 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001812 else
1813 crtc_resp->fb_id = 0;
1814
1815 if (crtc->enabled) {
1816
1817 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1818 crtc_resp->mode_valid = 1;
1819
1820 } else {
1821 crtc_resp->mode_valid = 0;
1822 }
1823
1824out:
Daniel Vetter84849902012-12-02 00:28:11 +01001825 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001826 return ret;
1827}
1828
Damien Lespiau61d8e322013-09-25 16:45:22 +01001829static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1830 const struct drm_file *file_priv)
1831{
1832 /*
1833 * If user-space hasn't configured the driver to expose the stereo 3D
1834 * modes, don't expose them.
1835 */
1836 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1837 return false;
1838
1839 return true;
1840}
1841
Dave Airlief453ba02008-11-07 14:05:41 -08001842/**
1843 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001844 * @dev: drm device for the ioctl
1845 * @data: data pointer for the ioctl
1846 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001847 *
Dave Airlief453ba02008-11-07 14:05:41 -08001848 * Construct a connector configuration structure to return to the user.
1849 *
1850 * Called by the user via ioctl.
1851 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001852 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001853 * Zero on success, errno on failure.
1854 */
1855int drm_mode_getconnector(struct drm_device *dev, void *data,
1856 struct drm_file *file_priv)
1857{
1858 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001859 struct drm_connector *connector;
1860 struct drm_display_mode *mode;
1861 int mode_count = 0;
1862 int props_count = 0;
1863 int encoders_count = 0;
1864 int ret = 0;
1865 int copied = 0;
1866 int i;
1867 struct drm_mode_modeinfo u_mode;
1868 struct drm_mode_modeinfo __user *mode_ptr;
1869 uint32_t __user *prop_ptr;
1870 uint64_t __user *prop_values;
1871 uint32_t __user *encoder_ptr;
1872
Dave Airliefb3b06c2011-02-08 13:55:21 +10001873 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1874 return -EINVAL;
1875
Dave Airlief453ba02008-11-07 14:05:41 -08001876 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1877
Jerome Glisse94401062010-07-15 15:43:25 -04001878 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001879
Daniel Vetter7b240562012-12-12 00:35:33 +01001880 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001881
Rob Clarka2b34e22013-10-05 16:36:52 -04001882 connector = drm_connector_find(dev, out_resp->connector_id);
1883 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001884 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001885 goto out;
1886 }
Dave Airlief453ba02008-11-07 14:05:41 -08001887
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001888 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001889
1890 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1891 if (connector->encoder_ids[i] != 0) {
1892 encoders_count++;
1893 }
1894 }
1895
1896 if (out_resp->count_modes == 0) {
1897 connector->funcs->fill_modes(connector,
1898 dev->mode_config.max_width,
1899 dev->mode_config.max_height);
1900 }
1901
1902 /* delayed so we get modes regardless of pre-fill_modes state */
1903 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01001904 if (drm_mode_expose_to_userspace(mode, file_priv))
1905 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001906
1907 out_resp->connector_id = connector->base.id;
1908 out_resp->connector_type = connector->connector_type;
1909 out_resp->connector_type_id = connector->connector_type_id;
1910 out_resp->mm_width = connector->display_info.width_mm;
1911 out_resp->mm_height = connector->display_info.height_mm;
1912 out_resp->subpixel = connector->display_info.subpixel_order;
1913 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02001914 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08001915 if (connector->encoder)
1916 out_resp->encoder_id = connector->encoder->base.id;
1917 else
1918 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02001919 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001920
1921 /*
1922 * This ioctl is called twice, once to determine how much space is
1923 * needed, and the 2nd time to fill it.
1924 */
1925 if ((out_resp->count_modes >= mode_count) && mode_count) {
1926 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001927 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001928 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01001929 if (!drm_mode_expose_to_userspace(mode, file_priv))
1930 continue;
1931
Dave Airlief453ba02008-11-07 14:05:41 -08001932 drm_crtc_convert_to_umode(&u_mode, mode);
1933 if (copy_to_user(mode_ptr + copied,
1934 &u_mode, sizeof(u_mode))) {
1935 ret = -EFAULT;
1936 goto out;
1937 }
1938 copied++;
1939 }
1940 }
1941 out_resp->count_modes = mode_count;
1942
1943 if ((out_resp->count_props >= props_count) && props_count) {
1944 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001945 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1946 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001947 for (i = 0; i < connector->properties.count; i++) {
1948 if (put_user(connector->properties.ids[i],
1949 prop_ptr + copied)) {
1950 ret = -EFAULT;
1951 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001952 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001953
1954 if (put_user(connector->properties.values[i],
1955 prop_values + copied)) {
1956 ret = -EFAULT;
1957 goto out;
1958 }
1959 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001960 }
1961 }
1962 out_resp->count_props = props_count;
1963
1964 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1965 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001966 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001967 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1968 if (connector->encoder_ids[i] != 0) {
1969 if (put_user(connector->encoder_ids[i],
1970 encoder_ptr + copied)) {
1971 ret = -EFAULT;
1972 goto out;
1973 }
1974 copied++;
1975 }
1976 }
1977 }
1978 out_resp->count_encoders = encoders_count;
1979
1980out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001981 mutex_unlock(&dev->mode_config.mutex);
1982
Dave Airlief453ba02008-11-07 14:05:41 -08001983 return ret;
1984}
1985
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001986/**
1987 * drm_mode_getencoder - get encoder configuration
1988 * @dev: drm device for the ioctl
1989 * @data: data pointer for the ioctl
1990 * @file_priv: drm file for the ioctl call
1991 *
1992 * Construct a encoder configuration structure to return to the user.
1993 *
1994 * Called by the user via ioctl.
1995 *
1996 * Returns:
1997 * Zero on success, errno on failure.
1998 */
Dave Airlief453ba02008-11-07 14:05:41 -08001999int drm_mode_getencoder(struct drm_device *dev, void *data,
2000 struct drm_file *file_priv)
2001{
2002 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002003 struct drm_encoder *encoder;
2004 int ret = 0;
2005
Dave Airliefb3b06c2011-02-08 13:55:21 +10002006 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2007 return -EINVAL;
2008
Daniel Vetter84849902012-12-02 00:28:11 +01002009 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002010 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2011 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002012 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002013 goto out;
2014 }
Dave Airlief453ba02008-11-07 14:05:41 -08002015
2016 if (encoder->crtc)
2017 enc_resp->crtc_id = encoder->crtc->base.id;
2018 else
2019 enc_resp->crtc_id = 0;
2020 enc_resp->encoder_type = encoder->encoder_type;
2021 enc_resp->encoder_id = encoder->base.id;
2022 enc_resp->possible_crtcs = encoder->possible_crtcs;
2023 enc_resp->possible_clones = encoder->possible_clones;
2024
2025out:
Daniel Vetter84849902012-12-02 00:28:11 +01002026 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002027 return ret;
2028}
2029
2030/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002031 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002032 * @dev: DRM device
2033 * @data: ioctl data
2034 * @file_priv: DRM file info
2035 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002036 * Construct a list of plane ids to return to the user.
2037 *
2038 * Called by the user via ioctl.
2039 *
2040 * Returns:
2041 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002042 */
2043int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002044 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002045{
2046 struct drm_mode_get_plane_res *plane_resp = data;
2047 struct drm_mode_config *config;
2048 struct drm_plane *plane;
2049 uint32_t __user *plane_ptr;
2050 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002051 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002052
2053 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2054 return -EINVAL;
2055
Daniel Vetter84849902012-12-02 00:28:11 +01002056 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002057 config = &dev->mode_config;
2058
Matt Roper681e7ec2014-04-01 15:22:42 -07002059 if (file_priv->universal_planes)
2060 num_planes = config->num_total_plane;
2061 else
2062 num_planes = config->num_overlay_plane;
2063
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002064 /*
2065 * This ioctl is called twice, once to determine how much space is
2066 * needed, and the 2nd time to fill it.
2067 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002068 if (num_planes &&
2069 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002070 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002071
2072 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002073 /*
2074 * Unless userspace set the 'universal planes'
2075 * capability bit, only advertise overlays.
2076 */
2077 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2078 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002079 continue;
2080
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002081 if (put_user(plane->base.id, plane_ptr + copied)) {
2082 ret = -EFAULT;
2083 goto out;
2084 }
2085 copied++;
2086 }
2087 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002088 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002089
2090out:
Daniel Vetter84849902012-12-02 00:28:11 +01002091 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002092 return ret;
2093}
2094
2095/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002096 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002097 * @dev: DRM device
2098 * @data: ioctl data
2099 * @file_priv: DRM file info
2100 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002101 * Construct a plane configuration structure to return to the user.
2102 *
2103 * Called by the user via ioctl.
2104 *
2105 * Returns:
2106 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002107 */
2108int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002109 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002110{
2111 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002112 struct drm_plane *plane;
2113 uint32_t __user *format_ptr;
2114 int ret = 0;
2115
2116 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2117 return -EINVAL;
2118
Daniel Vetter84849902012-12-02 00:28:11 +01002119 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002120 plane = drm_plane_find(dev, plane_resp->plane_id);
2121 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002122 ret = -ENOENT;
2123 goto out;
2124 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002125
2126 if (plane->crtc)
2127 plane_resp->crtc_id = plane->crtc->base.id;
2128 else
2129 plane_resp->crtc_id = 0;
2130
2131 if (plane->fb)
2132 plane_resp->fb_id = plane->fb->base.id;
2133 else
2134 plane_resp->fb_id = 0;
2135
2136 plane_resp->plane_id = plane->base.id;
2137 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002138 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002139
2140 /*
2141 * This ioctl is called twice, once to determine how much space is
2142 * needed, and the 2nd time to fill it.
2143 */
2144 if (plane->format_count &&
2145 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002146 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002147 if (copy_to_user(format_ptr,
2148 plane->format_types,
2149 sizeof(uint32_t) * plane->format_count)) {
2150 ret = -EFAULT;
2151 goto out;
2152 }
2153 }
2154 plane_resp->count_format_types = plane->format_count;
2155
2156out:
Daniel Vetter84849902012-12-02 00:28:11 +01002157 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002158 return ret;
2159}
2160
Matt Roperb36552b2014-06-10 08:28:09 -07002161/*
2162 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002163 *
Matt Roperb36552b2014-06-10 08:28:09 -07002164 * Note that we assume an extra reference has already been taken on fb. If the
2165 * update fails, this reference will be dropped before return; if it succeeds,
2166 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002167 *
Matt Roperb36552b2014-06-10 08:28:09 -07002168 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002169 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002170static int setplane_internal(struct drm_plane *plane,
2171 struct drm_crtc *crtc,
Matt Roperb36552b2014-06-10 08:28:09 -07002172 struct drm_framebuffer *fb,
2173 int32_t crtc_x, int32_t crtc_y,
2174 uint32_t crtc_w, uint32_t crtc_h,
2175 /* src_{x,y,w,h} values are 16.16 fixed point */
2176 uint32_t src_x, uint32_t src_y,
2177 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002178{
Chris Wilson17cfd912014-06-13 15:22:28 +01002179 struct drm_device *dev = plane->dev;
Matt Roperb36552b2014-06-10 08:28:09 -07002180 struct drm_framebuffer *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002181 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002182 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002183 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002184
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002185 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002186 if (!fb) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002187 drm_modeset_lock_all(dev);
2188 old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002189 ret = plane->funcs->disable_plane(plane);
2190 if (!ret) {
2191 plane->crtc = NULL;
2192 plane->fb = NULL;
2193 } else {
2194 old_fb = NULL;
2195 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002196 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002197 goto out;
2198 }
2199
Matt Roper7f994f32014-05-29 08:06:51 -07002200 /* Check whether this plane is usable on this CRTC */
2201 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2202 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2203 ret = -EINVAL;
2204 goto out;
2205 }
2206
Ville Syrjälä62443be2011-12-20 00:06:47 +02002207 /* Check whether this plane supports the fb pixel format. */
2208 for (i = 0; i < plane->format_count; i++)
2209 if (fb->pixel_format == plane->format_types[i])
2210 break;
2211 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002212 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2213 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002214 ret = -EINVAL;
2215 goto out;
2216 }
2217
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002218 fb_width = fb->width << 16;
2219 fb_height = fb->height << 16;
2220
2221 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002222 if (src_w > fb_width ||
2223 src_x > fb_width - src_w ||
2224 src_h > fb_height ||
2225 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002226 DRM_DEBUG_KMS("Invalid source coordinates "
2227 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002228 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2229 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2230 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2231 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002232 ret = -ENOSPC;
2233 goto out;
2234 }
2235
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002236 drm_modeset_lock_all(dev);
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002237 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002238 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002239 crtc_x, crtc_y, crtc_w, crtc_h,
2240 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002241 if (!ret) {
2242 plane->crtc = crtc;
2243 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002244 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002245 } else {
2246 old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002247 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002248 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002249
2250out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002251 if (fb)
2252 drm_framebuffer_unreference(fb);
2253 if (old_fb)
2254 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002255
2256 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002257
2258}
2259
2260/**
2261 * drm_mode_setplane - configure a plane's configuration
2262 * @dev: DRM device
2263 * @data: ioctl data*
2264 * @file_priv: DRM file info
2265 *
2266 * Set plane configuration, including placement, fb, scaling, and other factors.
2267 * Or pass a NULL fb to disable (planes may be disabled without providing a
2268 * valid crtc).
2269 *
2270 * Returns:
2271 * Zero on success, errno on failure.
2272 */
2273int drm_mode_setplane(struct drm_device *dev, void *data,
2274 struct drm_file *file_priv)
2275{
2276 struct drm_mode_set_plane *plane_req = data;
2277 struct drm_mode_object *obj;
2278 struct drm_plane *plane;
2279 struct drm_crtc *crtc = NULL;
2280 struct drm_framebuffer *fb = NULL;
2281
2282 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2283 return -EINVAL;
2284
2285 /* Give drivers some help against integer overflows */
2286 if (plane_req->crtc_w > INT_MAX ||
2287 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2288 plane_req->crtc_h > INT_MAX ||
2289 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2290 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2291 plane_req->crtc_w, plane_req->crtc_h,
2292 plane_req->crtc_x, plane_req->crtc_y);
2293 return -ERANGE;
2294 }
2295
2296 /*
2297 * First, find the plane, crtc, and fb objects. If not available,
2298 * we don't bother to call the driver.
2299 */
2300 obj = drm_mode_object_find(dev, plane_req->plane_id,
2301 DRM_MODE_OBJECT_PLANE);
2302 if (!obj) {
2303 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2304 plane_req->plane_id);
2305 return -ENOENT;
2306 }
2307 plane = obj_to_plane(obj);
2308
2309 if (plane_req->fb_id) {
2310 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2311 if (!fb) {
2312 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2313 plane_req->fb_id);
2314 return -ENOENT;
2315 }
2316
2317 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2318 DRM_MODE_OBJECT_CRTC);
2319 if (!obj) {
2320 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2321 plane_req->crtc_id);
2322 return -ENOENT;
2323 }
2324 crtc = obj_to_crtc(obj);
2325 }
2326
Matt Roper161d0dc2014-06-10 08:28:10 -07002327 /*
2328 * setplane_internal will take care of deref'ing either the old or new
2329 * framebuffer depending on success.
2330 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002331 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002332 plane_req->crtc_x, plane_req->crtc_y,
2333 plane_req->crtc_w, plane_req->crtc_h,
2334 plane_req->src_x, plane_req->src_y,
2335 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002336}
2337
2338/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002339 * drm_mode_set_config_internal - helper to call ->set_config
2340 * @set: modeset config to set
2341 *
2342 * This is a little helper to wrap internal calls to the ->set_config driver
2343 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002344 *
2345 * Returns:
2346 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002347 */
2348int drm_mode_set_config_internal(struct drm_mode_set *set)
2349{
2350 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002351 struct drm_framebuffer *fb;
2352 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002353 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002354
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002355 /*
2356 * NOTE: ->set_config can also disable other crtcs (if we steal all
2357 * connectors from it), hence we need to refcount the fbs across all
2358 * crtcs. Atomic modeset will have saner semantics ...
2359 */
2360 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Matt Roperf4510a22014-04-01 15:22:40 -07002361 tmp->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002362
Daniel Vetterb0d12322012-12-11 01:07:12 +01002363 fb = set->fb;
2364
2365 ret = crtc->funcs->set_config(set);
2366 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002367 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002368 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002369 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002370
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002371 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002372 if (tmp->primary->fb)
2373 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002374 if (tmp->old_fb)
2375 drm_framebuffer_unreference(tmp->old_fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01002376 }
2377
2378 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002379}
2380EXPORT_SYMBOL(drm_mode_set_config_internal);
2381
Matt Roperaf936292014-04-01 15:22:34 -07002382/**
2383 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2384 * CRTC viewport
2385 * @crtc: CRTC that framebuffer will be displayed on
2386 * @x: x panning
2387 * @y: y panning
2388 * @mode: mode that framebuffer will be displayed under
2389 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002390 */
Matt Roperaf936292014-04-01 15:22:34 -07002391int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2392 int x, int y,
2393 const struct drm_display_mode *mode,
2394 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002395
2396{
2397 int hdisplay, vdisplay;
2398
2399 hdisplay = mode->hdisplay;
2400 vdisplay = mode->vdisplay;
2401
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002402 if (drm_mode_is_stereo(mode)) {
2403 struct drm_display_mode adjusted = *mode;
2404
2405 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2406 hdisplay = adjusted.crtc_hdisplay;
2407 vdisplay = adjusted.crtc_vdisplay;
2408 }
2409
Damien Lespiauc11e9282013-09-25 16:45:30 +01002410 if (crtc->invert_dimensions)
2411 swap(hdisplay, vdisplay);
2412
2413 if (hdisplay > fb->width ||
2414 vdisplay > fb->height ||
2415 x > fb->width - hdisplay ||
2416 y > fb->height - vdisplay) {
2417 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2418 fb->width, fb->height, hdisplay, vdisplay, x, y,
2419 crtc->invert_dimensions ? " (inverted)" : "");
2420 return -ENOSPC;
2421 }
2422
2423 return 0;
2424}
Matt Roperaf936292014-04-01 15:22:34 -07002425EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002426
Daniel Vetter2d13b672012-12-11 13:47:23 +01002427/**
Dave Airlief453ba02008-11-07 14:05:41 -08002428 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002429 * @dev: drm device for the ioctl
2430 * @data: data pointer for the ioctl
2431 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002432 *
Dave Airlief453ba02008-11-07 14:05:41 -08002433 * Build a new CRTC configuration based on user request.
2434 *
2435 * Called by the user via ioctl.
2436 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002437 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002438 * Zero on success, errno on failure.
2439 */
2440int drm_mode_setcrtc(struct drm_device *dev, void *data,
2441 struct drm_file *file_priv)
2442{
2443 struct drm_mode_config *config = &dev->mode_config;
2444 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002445 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002446 struct drm_connector **connector_set = NULL, *connector;
2447 struct drm_framebuffer *fb = NULL;
2448 struct drm_display_mode *mode = NULL;
2449 struct drm_mode_set set;
2450 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002451 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002452 int i;
2453
Dave Airliefb3b06c2011-02-08 13:55:21 +10002454 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2455 return -EINVAL;
2456
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002457 /* For some reason crtc x/y offsets are signed internally. */
2458 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2459 return -ERANGE;
2460
Daniel Vetter84849902012-12-02 00:28:11 +01002461 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002462 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2463 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002464 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002465 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002466 goto out;
2467 }
Jerome Glisse94401062010-07-15 15:43:25 -04002468 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002469
2470 if (crtc_req->mode_valid) {
2471 /* If we have a mode we need a framebuffer. */
2472 /* If we pass -1, set the mode with the currently bound fb */
2473 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002474 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002475 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2476 ret = -EINVAL;
2477 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002478 }
Matt Roperf4510a22014-04-01 15:22:40 -07002479 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002480 /* Make refcounting symmetric with the lookup path. */
2481 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002482 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002483 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2484 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002485 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2486 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002487 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002488 goto out;
2489 }
Dave Airlief453ba02008-11-07 14:05:41 -08002490 }
2491
2492 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002493 if (!mode) {
2494 ret = -ENOMEM;
2495 goto out;
2496 }
2497
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002498 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2499 if (ret) {
2500 DRM_DEBUG_KMS("Invalid mode\n");
2501 goto out;
2502 }
2503
Dave Airlief453ba02008-11-07 14:05:41 -08002504 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002505
Damien Lespiauc11e9282013-09-25 16:45:30 +01002506 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2507 mode, fb);
2508 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002509 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002510
Dave Airlief453ba02008-11-07 14:05:41 -08002511 }
2512
2513 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002514 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002515 ret = -EINVAL;
2516 goto out;
2517 }
2518
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002519 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002520 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002521 crtc_req->count_connectors);
2522 ret = -EINVAL;
2523 goto out;
2524 }
2525
2526 if (crtc_req->count_connectors > 0) {
2527 u32 out_id;
2528
2529 /* Avoid unbounded kernel memory allocation */
2530 if (crtc_req->count_connectors > config->num_connector) {
2531 ret = -EINVAL;
2532 goto out;
2533 }
2534
2535 connector_set = kmalloc(crtc_req->count_connectors *
2536 sizeof(struct drm_connector *),
2537 GFP_KERNEL);
2538 if (!connector_set) {
2539 ret = -ENOMEM;
2540 goto out;
2541 }
2542
2543 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002544 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002545 if (get_user(out_id, &set_connectors_ptr[i])) {
2546 ret = -EFAULT;
2547 goto out;
2548 }
2549
Rob Clarka2b34e22013-10-05 16:36:52 -04002550 connector = drm_connector_find(dev, out_id);
2551 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002552 DRM_DEBUG_KMS("Connector id %d unknown\n",
2553 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002554 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002555 goto out;
2556 }
Jerome Glisse94401062010-07-15 15:43:25 -04002557 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2558 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002559 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002560
2561 connector_set[i] = connector;
2562 }
2563 }
2564
2565 set.crtc = crtc;
2566 set.x = crtc_req->x;
2567 set.y = crtc_req->y;
2568 set.mode = mode;
2569 set.connectors = connector_set;
2570 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002571 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002572 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002573
2574out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002575 if (fb)
2576 drm_framebuffer_unreference(fb);
2577
Dave Airlief453ba02008-11-07 14:05:41 -08002578 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002579 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002580 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002581 return ret;
2582}
2583
Matt Roper161d0dc2014-06-10 08:28:10 -07002584/**
2585 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2586 * universal plane handler call
2587 * @crtc: crtc to update cursor for
2588 * @req: data pointer for the ioctl
2589 * @file_priv: drm file for the ioctl call
2590 *
2591 * Legacy cursor ioctl's work directly with driver buffer handles. To
2592 * translate legacy ioctl calls into universal plane handler calls, we need to
2593 * wrap the native buffer handle in a drm_framebuffer.
2594 *
2595 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2596 * buffer with a pitch of 4*width; the universal plane interface should be used
2597 * directly in cases where the hardware can support other buffer settings and
2598 * userspace wants to make use of these capabilities.
2599 *
2600 * Returns:
2601 * Zero on success, errno on failure.
2602 */
2603static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2604 struct drm_mode_cursor2 *req,
2605 struct drm_file *file_priv)
2606{
2607 struct drm_device *dev = crtc->dev;
2608 struct drm_framebuffer *fb = NULL;
2609 struct drm_mode_fb_cmd2 fbreq = {
2610 .width = req->width,
2611 .height = req->height,
2612 .pixel_format = DRM_FORMAT_ARGB8888,
2613 .pitches = { req->width * 4 },
2614 .handles = { req->handle },
2615 };
2616 int32_t crtc_x, crtc_y;
2617 uint32_t crtc_w = 0, crtc_h = 0;
2618 uint32_t src_w = 0, src_h = 0;
2619 int ret = 0;
2620
2621 BUG_ON(!crtc->cursor);
2622
2623 /*
2624 * Obtain fb we'll be using (either new or existing) and take an extra
2625 * reference to it if fb != null. setplane will take care of dropping
2626 * the reference if the plane update fails.
2627 */
2628 if (req->flags & DRM_MODE_CURSOR_BO) {
2629 if (req->handle) {
2630 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2631 if (IS_ERR(fb)) {
2632 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2633 return PTR_ERR(fb);
2634 }
2635
2636 drm_framebuffer_reference(fb);
2637 } else {
2638 fb = NULL;
2639 }
2640 } else {
2641 mutex_lock(&dev->mode_config.mutex);
2642 fb = crtc->cursor->fb;
2643 if (fb)
2644 drm_framebuffer_reference(fb);
2645 mutex_unlock(&dev->mode_config.mutex);
2646 }
2647
2648 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2649 crtc_x = req->x;
2650 crtc_y = req->y;
2651 } else {
2652 crtc_x = crtc->cursor_x;
2653 crtc_y = crtc->cursor_y;
2654 }
2655
2656 if (fb) {
2657 crtc_w = fb->width;
2658 crtc_h = fb->height;
2659 src_w = fb->width << 16;
2660 src_h = fb->height << 16;
2661 }
2662
2663 /*
2664 * setplane_internal will take care of deref'ing either the old or new
2665 * framebuffer depending on success.
2666 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002667 ret = setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002668 crtc_x, crtc_y, crtc_w, crtc_h,
2669 0, 0, src_w, src_h);
2670
2671 /* Update successful; save new cursor position, if necessary */
2672 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2673 crtc->cursor_x = req->x;
2674 crtc->cursor_y = req->y;
2675 }
2676
2677 return ret;
2678}
2679
Dave Airlie4c813d42013-06-20 11:48:52 +10002680static int drm_mode_cursor_common(struct drm_device *dev,
2681 struct drm_mode_cursor2 *req,
2682 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002683{
Dave Airlief453ba02008-11-07 14:05:41 -08002684 struct drm_crtc *crtc;
2685 int ret = 0;
2686
Dave Airliefb3b06c2011-02-08 13:55:21 +10002687 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2688 return -EINVAL;
2689
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002690 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002691 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002692
Rob Clarka2b34e22013-10-05 16:36:52 -04002693 crtc = drm_crtc_find(dev, req->crtc_id);
2694 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002695 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002696 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002697 }
Dave Airlief453ba02008-11-07 14:05:41 -08002698
Matt Roper161d0dc2014-06-10 08:28:10 -07002699 /*
2700 * If this crtc has a universal cursor plane, call that plane's update
2701 * handler rather than using legacy cursor handlers.
2702 */
2703 if (crtc->cursor)
2704 return drm_mode_cursor_universal(crtc, req, file_priv);
2705
Rob Clark51fd3712013-11-19 12:10:12 -05002706 drm_modeset_lock(&crtc->mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002707 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002708 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002709 ret = -ENXIO;
2710 goto out;
2711 }
2712 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002713 if (crtc->funcs->cursor_set2)
2714 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2715 req->width, req->height, req->hot_x, req->hot_y);
2716 else
2717 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2718 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002719 }
2720
2721 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2722 if (crtc->funcs->cursor_move) {
2723 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2724 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002725 ret = -EFAULT;
2726 goto out;
2727 }
2728 }
2729out:
Rob Clark51fd3712013-11-19 12:10:12 -05002730 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterdac35662012-12-02 15:24:10 +01002731
Dave Airlief453ba02008-11-07 14:05:41 -08002732 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002733
2734}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002735
2736
2737/**
2738 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2739 * @dev: drm device for the ioctl
2740 * @data: data pointer for the ioctl
2741 * @file_priv: drm file for the ioctl call
2742 *
2743 * Set the cursor configuration based on user request.
2744 *
2745 * Called by the user via ioctl.
2746 *
2747 * Returns:
2748 * Zero on success, errno on failure.
2749 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002750int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002751 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002752{
2753 struct drm_mode_cursor *req = data;
2754 struct drm_mode_cursor2 new_req;
2755
2756 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2757 new_req.hot_x = new_req.hot_y = 0;
2758
2759 return drm_mode_cursor_common(dev, &new_req, file_priv);
2760}
2761
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002762/**
2763 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2764 * @dev: drm device for the ioctl
2765 * @data: data pointer for the ioctl
2766 * @file_priv: drm file for the ioctl call
2767 *
2768 * Set the cursor configuration based on user request. This implements the 2nd
2769 * version of the cursor ioctl, which allows userspace to additionally specify
2770 * the hotspot of the pointer.
2771 *
2772 * Called by the user via ioctl.
2773 *
2774 * Returns:
2775 * Zero on success, errno on failure.
2776 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002777int drm_mode_cursor2_ioctl(struct drm_device *dev,
2778 void *data, struct drm_file *file_priv)
2779{
2780 struct drm_mode_cursor2 *req = data;
2781 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002782}
2783
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002784/**
2785 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2786 * @bpp: bits per pixels
2787 * @depth: bit depth per pixel
2788 *
2789 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2790 * Useful in fbdev emulation code, since that deals in those values.
2791 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002792uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2793{
2794 uint32_t fmt;
2795
2796 switch (bpp) {
2797 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002798 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002799 break;
2800 case 16:
2801 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002802 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002803 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002804 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002805 break;
2806 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002807 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002808 break;
2809 case 32:
2810 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002811 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002812 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002813 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002814 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002815 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002816 break;
2817 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002818 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2819 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002820 break;
2821 }
2822
2823 return fmt;
2824}
2825EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2826
Dave Airlief453ba02008-11-07 14:05:41 -08002827/**
2828 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002829 * @dev: drm device for the ioctl
2830 * @data: data pointer for the ioctl
2831 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002832 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002833 * Add a new FB to the specified CRTC, given a user request. This is the
2834 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002835 *
2836 * Called by the user via ioctl.
2837 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002838 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002839 * Zero on success, errno on failure.
2840 */
2841int drm_mode_addfb(struct drm_device *dev,
2842 void *data, struct drm_file *file_priv)
2843{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002844 struct drm_mode_fb_cmd *or = data;
2845 struct drm_mode_fb_cmd2 r = {};
2846 struct drm_mode_config *config = &dev->mode_config;
2847 struct drm_framebuffer *fb;
2848 int ret = 0;
2849
2850 /* Use new struct with format internally */
2851 r.fb_id = or->fb_id;
2852 r.width = or->width;
2853 r.height = or->height;
2854 r.pitches[0] = or->pitch;
2855 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2856 r.handles[0] = or->handle;
2857
2858 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2859 return -EINVAL;
2860
Jesse Barnesacb4b992011-11-07 12:03:23 -08002861 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002862 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002863
2864 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002865 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002866
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002867 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2868 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002869 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002870 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002871 }
2872
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002873 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002874 or->fb_id = fb->base.id;
2875 list_add(&fb->filp_head, &file_priv->fbs);
2876 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002877 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002878
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002879 return ret;
2880}
2881
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002882static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002883{
2884 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2885
2886 switch (format) {
2887 case DRM_FORMAT_C8:
2888 case DRM_FORMAT_RGB332:
2889 case DRM_FORMAT_BGR233:
2890 case DRM_FORMAT_XRGB4444:
2891 case DRM_FORMAT_XBGR4444:
2892 case DRM_FORMAT_RGBX4444:
2893 case DRM_FORMAT_BGRX4444:
2894 case DRM_FORMAT_ARGB4444:
2895 case DRM_FORMAT_ABGR4444:
2896 case DRM_FORMAT_RGBA4444:
2897 case DRM_FORMAT_BGRA4444:
2898 case DRM_FORMAT_XRGB1555:
2899 case DRM_FORMAT_XBGR1555:
2900 case DRM_FORMAT_RGBX5551:
2901 case DRM_FORMAT_BGRX5551:
2902 case DRM_FORMAT_ARGB1555:
2903 case DRM_FORMAT_ABGR1555:
2904 case DRM_FORMAT_RGBA5551:
2905 case DRM_FORMAT_BGRA5551:
2906 case DRM_FORMAT_RGB565:
2907 case DRM_FORMAT_BGR565:
2908 case DRM_FORMAT_RGB888:
2909 case DRM_FORMAT_BGR888:
2910 case DRM_FORMAT_XRGB8888:
2911 case DRM_FORMAT_XBGR8888:
2912 case DRM_FORMAT_RGBX8888:
2913 case DRM_FORMAT_BGRX8888:
2914 case DRM_FORMAT_ARGB8888:
2915 case DRM_FORMAT_ABGR8888:
2916 case DRM_FORMAT_RGBA8888:
2917 case DRM_FORMAT_BGRA8888:
2918 case DRM_FORMAT_XRGB2101010:
2919 case DRM_FORMAT_XBGR2101010:
2920 case DRM_FORMAT_RGBX1010102:
2921 case DRM_FORMAT_BGRX1010102:
2922 case DRM_FORMAT_ARGB2101010:
2923 case DRM_FORMAT_ABGR2101010:
2924 case DRM_FORMAT_RGBA1010102:
2925 case DRM_FORMAT_BGRA1010102:
2926 case DRM_FORMAT_YUYV:
2927 case DRM_FORMAT_YVYU:
2928 case DRM_FORMAT_UYVY:
2929 case DRM_FORMAT_VYUY:
2930 case DRM_FORMAT_AYUV:
2931 case DRM_FORMAT_NV12:
2932 case DRM_FORMAT_NV21:
2933 case DRM_FORMAT_NV16:
2934 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002935 case DRM_FORMAT_NV24:
2936 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002937 case DRM_FORMAT_YUV410:
2938 case DRM_FORMAT_YVU410:
2939 case DRM_FORMAT_YUV411:
2940 case DRM_FORMAT_YVU411:
2941 case DRM_FORMAT_YUV420:
2942 case DRM_FORMAT_YVU420:
2943 case DRM_FORMAT_YUV422:
2944 case DRM_FORMAT_YVU422:
2945 case DRM_FORMAT_YUV444:
2946 case DRM_FORMAT_YVU444:
2947 return 0;
2948 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03002949 DRM_DEBUG_KMS("invalid pixel format %s\n",
2950 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02002951 return -EINVAL;
2952 }
2953}
2954
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002955static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002956{
2957 int ret, hsub, vsub, num_planes, i;
2958
2959 ret = format_check(r);
2960 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002961 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2962 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002963 return ret;
2964 }
2965
2966 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2967 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2968 num_planes = drm_format_num_planes(r->pixel_format);
2969
2970 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002971 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002972 return -EINVAL;
2973 }
2974
2975 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002976 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002977 return -EINVAL;
2978 }
2979
2980 for (i = 0; i < num_planes; i++) {
2981 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002982 unsigned int height = r->height / (i != 0 ? vsub : 1);
2983 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002984
2985 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002986 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002987 return -EINVAL;
2988 }
2989
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002990 if ((uint64_t) width * cpp > UINT_MAX)
2991 return -ERANGE;
2992
2993 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2994 return -ERANGE;
2995
2996 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002997 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002998 return -EINVAL;
2999 }
3000 }
3001
3002 return 0;
3003}
3004
Matt Roperc394c2b2014-06-10 08:28:08 -07003005static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3006 struct drm_mode_fb_cmd2 *r,
3007 struct drm_file *file_priv)
3008{
3009 struct drm_mode_config *config = &dev->mode_config;
3010 struct drm_framebuffer *fb;
3011 int ret;
3012
3013 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3014 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3015 return ERR_PTR(-EINVAL);
3016 }
3017
3018 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3019 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3020 r->width, config->min_width, config->max_width);
3021 return ERR_PTR(-EINVAL);
3022 }
3023 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3024 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3025 r->height, config->min_height, config->max_height);
3026 return ERR_PTR(-EINVAL);
3027 }
3028
3029 ret = framebuffer_check(r);
3030 if (ret)
3031 return ERR_PTR(ret);
3032
3033 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3034 if (IS_ERR(fb)) {
3035 DRM_DEBUG_KMS("could not create framebuffer\n");
3036 return fb;
3037 }
3038
3039 mutex_lock(&file_priv->fbs_lock);
3040 r->fb_id = fb->base.id;
3041 list_add(&fb->filp_head, &file_priv->fbs);
3042 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3043 mutex_unlock(&file_priv->fbs_lock);
3044
3045 return fb;
3046}
3047
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003048/**
3049 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003050 * @dev: drm device for the ioctl
3051 * @data: data pointer for the ioctl
3052 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003053 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003054 * Add a new FB to the specified CRTC, given a user request with format. This is
3055 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3056 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003057 *
3058 * Called by the user via ioctl.
3059 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003060 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003061 * Zero on success, errno on failure.
3062 */
3063int drm_mode_addfb2(struct drm_device *dev,
3064 void *data, struct drm_file *file_priv)
3065{
Dave Airlief453ba02008-11-07 14:05:41 -08003066 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003067
Dave Airliefb3b06c2011-02-08 13:55:21 +10003068 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3069 return -EINVAL;
3070
Matt Roperc394c2b2014-06-10 08:28:08 -07003071 fb = add_framebuffer_internal(dev, data, file_priv);
3072 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003073 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003074
Matt Roperc394c2b2014-06-10 08:28:08 -07003075 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003076}
3077
3078/**
3079 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003080 * @dev: drm device for the ioctl
3081 * @data: data pointer for the ioctl
3082 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003083 *
Dave Airlief453ba02008-11-07 14:05:41 -08003084 * Remove the FB specified by the user.
3085 *
3086 * Called by the user via ioctl.
3087 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003088 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003089 * Zero on success, errno on failure.
3090 */
3091int drm_mode_rmfb(struct drm_device *dev,
3092 void *data, struct drm_file *file_priv)
3093{
Dave Airlief453ba02008-11-07 14:05:41 -08003094 struct drm_framebuffer *fb = NULL;
3095 struct drm_framebuffer *fbl = NULL;
3096 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003097 int found = 0;
3098
Dave Airliefb3b06c2011-02-08 13:55:21 +10003099 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3100 return -EINVAL;
3101
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003102 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003103 mutex_lock(&dev->mode_config.fb_lock);
3104 fb = __drm_framebuffer_lookup(dev, *id);
3105 if (!fb)
3106 goto fail_lookup;
3107
Dave Airlief453ba02008-11-07 14:05:41 -08003108 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3109 if (fb == fbl)
3110 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003111 if (!found)
3112 goto fail_lookup;
3113
3114 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3115 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003116
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003117 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003118 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003119 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003120
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003121 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003122
Daniel Vetter2b677e82012-12-10 21:16:05 +01003123 return 0;
3124
3125fail_lookup:
3126 mutex_unlock(&dev->mode_config.fb_lock);
3127 mutex_unlock(&file_priv->fbs_lock);
3128
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003129 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003130}
3131
3132/**
3133 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003134 * @dev: drm device for the ioctl
3135 * @data: data pointer for the ioctl
3136 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003137 *
Dave Airlief453ba02008-11-07 14:05:41 -08003138 * Lookup the FB given its ID and return info about it.
3139 *
3140 * Called by the user via ioctl.
3141 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003142 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003143 * Zero on success, errno on failure.
3144 */
3145int drm_mode_getfb(struct drm_device *dev,
3146 void *data, struct drm_file *file_priv)
3147{
3148 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003149 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003150 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003151
Dave Airliefb3b06c2011-02-08 13:55:21 +10003152 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3153 return -EINVAL;
3154
Daniel Vetter786b99e2012-12-02 21:53:40 +01003155 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003156 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003157 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003158
3159 r->height = fb->height;
3160 r->width = fb->width;
3161 r->depth = fb->depth;
3162 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003163 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003164 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003165 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003166 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003167 ret = fb->funcs->create_handle(fb, file_priv,
3168 &r->handle);
3169 } else {
3170 /* GET_FB() is an unprivileged ioctl so we must not
3171 * return a buffer-handle to non-master processes! For
3172 * backwards-compatibility reasons, we cannot make
3173 * GET_FB() privileged, so just return an invalid handle
3174 * for non-masters. */
3175 r->handle = 0;
3176 ret = 0;
3177 }
3178 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003179 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003180 }
Dave Airlief453ba02008-11-07 14:05:41 -08003181
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003182 drm_framebuffer_unreference(fb);
3183
Dave Airlief453ba02008-11-07 14:05:41 -08003184 return ret;
3185}
3186
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003187/**
3188 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3189 * @dev: drm device for the ioctl
3190 * @data: data pointer for the ioctl
3191 * @file_priv: drm file for the ioctl call
3192 *
3193 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3194 * rectangle list. Generic userspace which does frontbuffer rendering must call
3195 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3196 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3197 *
3198 * Modesetting drivers which always update the frontbuffer do not need to
3199 * implement the corresponding ->dirty framebuffer callback.
3200 *
3201 * Called by the user via ioctl.
3202 *
3203 * Returns:
3204 * Zero on success, errno on failure.
3205 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003206int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3207 void *data, struct drm_file *file_priv)
3208{
3209 struct drm_clip_rect __user *clips_ptr;
3210 struct drm_clip_rect *clips = NULL;
3211 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003212 struct drm_framebuffer *fb;
3213 unsigned flags;
3214 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003215 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003216
Dave Airliefb3b06c2011-02-08 13:55:21 +10003217 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3218 return -EINVAL;
3219
Daniel Vetter786b99e2012-12-02 21:53:40 +01003220 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003221 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003222 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003223
3224 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003225 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003226
3227 if (!num_clips != !clips_ptr) {
3228 ret = -EINVAL;
3229 goto out_err1;
3230 }
3231
3232 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3233
3234 /* If userspace annotates copy, clips must come in pairs */
3235 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3236 ret = -EINVAL;
3237 goto out_err1;
3238 }
3239
3240 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003241 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3242 ret = -EINVAL;
3243 goto out_err1;
3244 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003245 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3246 if (!clips) {
3247 ret = -ENOMEM;
3248 goto out_err1;
3249 }
3250
3251 ret = copy_from_user(clips, clips_ptr,
3252 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003253 if (ret) {
3254 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003255 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003256 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003257 }
3258
3259 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003260 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3261 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003262 } else {
3263 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003264 }
3265
3266out_err2:
3267 kfree(clips);
3268out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003269 drm_framebuffer_unreference(fb);
3270
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003271 return ret;
3272}
3273
3274
Dave Airlief453ba02008-11-07 14:05:41 -08003275/**
3276 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003277 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003278 *
Dave Airlief453ba02008-11-07 14:05:41 -08003279 * Destroy all the FBs associated with @filp.
3280 *
3281 * Called by the user via ioctl.
3282 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003283 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003284 * Zero on success, errno on failure.
3285 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003286void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003287{
Dave Airlief453ba02008-11-07 14:05:41 -08003288 struct drm_device *dev = priv->minor->dev;
3289 struct drm_framebuffer *fb, *tfb;
3290
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003291 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003292 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003293
3294 mutex_lock(&dev->mode_config.fb_lock);
3295 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3296 __drm_framebuffer_unregister(dev, fb);
3297 mutex_unlock(&dev->mode_config.fb_lock);
3298
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003299 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003300
3301 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003302 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003303 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003304 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003305}
3306
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003307/**
3308 * drm_property_create - create a new property type
3309 * @dev: drm device
3310 * @flags: flags specifying the property type
3311 * @name: name of the property
3312 * @num_values: number of pre-defined values
3313 *
3314 * This creates a new generic drm property which can then be attached to a drm
3315 * object with drm_object_attach_property. The returned property object must be
3316 * freed with drm_property_destroy.
3317 *
3318 * Returns:
3319 * A pointer to the newly created property on success, NULL on failure.
3320 */
Dave Airlief453ba02008-11-07 14:05:41 -08003321struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3322 const char *name, int num_values)
3323{
3324 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003325 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003326
3327 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3328 if (!property)
3329 return NULL;
3330
Rob Clark98f75de2014-05-30 11:37:03 -04003331 property->dev = dev;
3332
Dave Airlief453ba02008-11-07 14:05:41 -08003333 if (num_values) {
3334 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3335 if (!property->values)
3336 goto fail;
3337 }
3338
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003339 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3340 if (ret)
3341 goto fail;
3342
Dave Airlief453ba02008-11-07 14:05:41 -08003343 property->flags = flags;
3344 property->num_values = num_values;
3345 INIT_LIST_HEAD(&property->enum_blob_list);
3346
Vinson Lee471dd2e2011-11-10 11:55:40 -08003347 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003348 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003349 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3350 }
Dave Airlief453ba02008-11-07 14:05:41 -08003351
3352 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003353
3354 WARN_ON(!drm_property_type_valid(property));
3355
Dave Airlief453ba02008-11-07 14:05:41 -08003356 return property;
3357fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003358 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003359 kfree(property);
3360 return NULL;
3361}
3362EXPORT_SYMBOL(drm_property_create);
3363
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003364/**
3365 * drm_property_create - create a new enumeration property type
3366 * @dev: drm device
3367 * @flags: flags specifying the property type
3368 * @name: name of the property
3369 * @props: enumeration lists with property values
3370 * @num_values: number of pre-defined values
3371 *
3372 * This creates a new generic drm property which can then be attached to a drm
3373 * object with drm_object_attach_property. The returned property object must be
3374 * freed with drm_property_destroy.
3375 *
3376 * Userspace is only allowed to set one of the predefined values for enumeration
3377 * properties.
3378 *
3379 * Returns:
3380 * A pointer to the newly created property on success, NULL on failure.
3381 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003382struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3383 const char *name,
3384 const struct drm_prop_enum_list *props,
3385 int num_values)
3386{
3387 struct drm_property *property;
3388 int i, ret;
3389
3390 flags |= DRM_MODE_PROP_ENUM;
3391
3392 property = drm_property_create(dev, flags, name, num_values);
3393 if (!property)
3394 return NULL;
3395
3396 for (i = 0; i < num_values; i++) {
3397 ret = drm_property_add_enum(property, i,
3398 props[i].type,
3399 props[i].name);
3400 if (ret) {
3401 drm_property_destroy(dev, property);
3402 return NULL;
3403 }
3404 }
3405
3406 return property;
3407}
3408EXPORT_SYMBOL(drm_property_create_enum);
3409
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003410/**
3411 * drm_property_create - create a new bitmask property type
3412 * @dev: drm device
3413 * @flags: flags specifying the property type
3414 * @name: name of the property
3415 * @props: enumeration lists with property bitflags
3416 * @num_values: number of pre-defined values
3417 *
3418 * This creates a new generic drm property which can then be attached to a drm
3419 * object with drm_object_attach_property. The returned property object must be
3420 * freed with drm_property_destroy.
3421 *
3422 * Compared to plain enumeration properties userspace is allowed to set any
3423 * or'ed together combination of the predefined property bitflag values
3424 *
3425 * Returns:
3426 * A pointer to the newly created property on success, NULL on failure.
3427 */
Rob Clark49e27542012-05-17 02:23:26 -06003428struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3429 int flags, const char *name,
3430 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303431 int num_props,
3432 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003433{
3434 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303435 int i, ret, index = 0;
3436 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003437
3438 flags |= DRM_MODE_PROP_BITMASK;
3439
3440 property = drm_property_create(dev, flags, name, num_values);
3441 if (!property)
3442 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303443 for (i = 0; i < num_props; i++) {
3444 if (!(supported_bits & (1ULL << props[i].type)))
3445 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003446
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303447 if (WARN_ON(index >= num_values)) {
3448 drm_property_destroy(dev, property);
3449 return NULL;
3450 }
3451
3452 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003453 props[i].type,
3454 props[i].name);
3455 if (ret) {
3456 drm_property_destroy(dev, property);
3457 return NULL;
3458 }
3459 }
3460
3461 return property;
3462}
3463EXPORT_SYMBOL(drm_property_create_bitmask);
3464
Rob Clarkebc44cf2012-09-12 22:22:31 -05003465static struct drm_property *property_create_range(struct drm_device *dev,
3466 int flags, const char *name,
3467 uint64_t min, uint64_t max)
3468{
3469 struct drm_property *property;
3470
3471 property = drm_property_create(dev, flags, name, 2);
3472 if (!property)
3473 return NULL;
3474
3475 property->values[0] = min;
3476 property->values[1] = max;
3477
3478 return property;
3479}
3480
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003481/**
3482 * drm_property_create - create a new ranged property type
3483 * @dev: drm device
3484 * @flags: flags specifying the property type
3485 * @name: name of the property
3486 * @min: minimum value of the property
3487 * @max: maximum value of the property
3488 *
3489 * This creates a new generic drm property which can then be attached to a drm
3490 * object with drm_object_attach_property. The returned property object must be
3491 * freed with drm_property_destroy.
3492 *
3493 * Userspace is allowed to set any interger value in the (min, max) range
3494 * inclusive.
3495 *
3496 * Returns:
3497 * A pointer to the newly created property on success, NULL on failure.
3498 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003499struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3500 const char *name,
3501 uint64_t min, uint64_t max)
3502{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003503 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3504 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003505}
3506EXPORT_SYMBOL(drm_property_create_range);
3507
Rob Clarkebc44cf2012-09-12 22:22:31 -05003508struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3509 int flags, const char *name,
3510 int64_t min, int64_t max)
3511{
3512 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3513 name, I642U64(min), I642U64(max));
3514}
3515EXPORT_SYMBOL(drm_property_create_signed_range);
3516
Rob Clark98f75de2014-05-30 11:37:03 -04003517struct drm_property *drm_property_create_object(struct drm_device *dev,
3518 int flags, const char *name, uint32_t type)
3519{
3520 struct drm_property *property;
3521
3522 flags |= DRM_MODE_PROP_OBJECT;
3523
3524 property = drm_property_create(dev, flags, name, 1);
3525 if (!property)
3526 return NULL;
3527
3528 property->values[0] = type;
3529
3530 return property;
3531}
3532EXPORT_SYMBOL(drm_property_create_object);
3533
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003534/**
3535 * drm_property_add_enum - add a possible value to an enumeration property
3536 * @property: enumeration property to change
3537 * @index: index of the new enumeration
3538 * @value: value of the new enumeration
3539 * @name: symbolic name of the new enumeration
3540 *
3541 * This functions adds enumerations to a property.
3542 *
3543 * It's use is deprecated, drivers should use one of the more specific helpers
3544 * to directly create the property with all enumerations already attached.
3545 *
3546 * Returns:
3547 * Zero on success, error code on failure.
3548 */
Dave Airlief453ba02008-11-07 14:05:41 -08003549int drm_property_add_enum(struct drm_property *property, int index,
3550 uint64_t value, const char *name)
3551{
3552 struct drm_property_enum *prop_enum;
3553
Rob Clark5ea22f22014-05-30 11:34:01 -04003554 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3555 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003556 return -EINVAL;
3557
3558 /*
3559 * Bitmask enum properties have the additional constraint of values
3560 * from 0 to 63
3561 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003562 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3563 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003564 return -EINVAL;
3565
3566 if (!list_empty(&property->enum_blob_list)) {
3567 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3568 if (prop_enum->value == value) {
3569 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3570 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3571 return 0;
3572 }
3573 }
3574 }
3575
3576 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3577 if (!prop_enum)
3578 return -ENOMEM;
3579
3580 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3581 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3582 prop_enum->value = value;
3583
3584 property->values[index] = value;
3585 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3586 return 0;
3587}
3588EXPORT_SYMBOL(drm_property_add_enum);
3589
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003590/**
3591 * drm_property_destroy - destroy a drm property
3592 * @dev: drm device
3593 * @property: property to destry
3594 *
3595 * This function frees a property including any attached resources like
3596 * enumeration values.
3597 */
Dave Airlief453ba02008-11-07 14:05:41 -08003598void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3599{
3600 struct drm_property_enum *prop_enum, *pt;
3601
3602 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3603 list_del(&prop_enum->head);
3604 kfree(prop_enum);
3605 }
3606
3607 if (property->num_values)
3608 kfree(property->values);
3609 drm_mode_object_put(dev, &property->base);
3610 list_del(&property->head);
3611 kfree(property);
3612}
3613EXPORT_SYMBOL(drm_property_destroy);
3614
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003615/**
3616 * drm_object_attach_property - attach a property to a modeset object
3617 * @obj: drm modeset object
3618 * @property: property to attach
3619 * @init_val: initial value of the property
3620 *
3621 * This attaches the given property to the modeset object with the given initial
3622 * value. Currently this function cannot fail since the properties are stored in
3623 * a statically sized array.
3624 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003625void drm_object_attach_property(struct drm_mode_object *obj,
3626 struct drm_property *property,
3627 uint64_t init_val)
3628{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003629 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003630
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003631 if (count == DRM_OBJECT_MAX_PROPERTY) {
3632 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3633 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3634 "you see this message on the same object type.\n",
3635 obj->type);
3636 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003637 }
3638
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003639 obj->properties->ids[count] = property->base.id;
3640 obj->properties->values[count] = init_val;
3641 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003642}
3643EXPORT_SYMBOL(drm_object_attach_property);
3644
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003645/**
3646 * drm_object_property_set_value - set the value of a property
3647 * @obj: drm mode object to set property value for
3648 * @property: property to set
3649 * @val: value the property should be set to
3650 *
3651 * This functions sets a given property on a given object. This function only
3652 * changes the software state of the property, it does not call into the
3653 * driver's ->set_property callback.
3654 *
3655 * Returns:
3656 * Zero on success, error code on failure.
3657 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003658int drm_object_property_set_value(struct drm_mode_object *obj,
3659 struct drm_property *property, uint64_t val)
3660{
3661 int i;
3662
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003663 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003664 if (obj->properties->ids[i] == property->base.id) {
3665 obj->properties->values[i] = val;
3666 return 0;
3667 }
3668 }
3669
3670 return -EINVAL;
3671}
3672EXPORT_SYMBOL(drm_object_property_set_value);
3673
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003674/**
3675 * drm_object_property_get_value - retrieve the value of a property
3676 * @obj: drm mode object to get property value from
3677 * @property: property to retrieve
3678 * @val: storage for the property value
3679 *
3680 * This function retrieves the softare state of the given property for the given
3681 * property. Since there is no driver callback to retrieve the current property
3682 * value this might be out of sync with the hardware, depending upon the driver
3683 * and property.
3684 *
3685 * Returns:
3686 * Zero on success, error code on failure.
3687 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003688int drm_object_property_get_value(struct drm_mode_object *obj,
3689 struct drm_property *property, uint64_t *val)
3690{
3691 int i;
3692
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003693 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003694 if (obj->properties->ids[i] == property->base.id) {
3695 *val = obj->properties->values[i];
3696 return 0;
3697 }
3698 }
3699
3700 return -EINVAL;
3701}
3702EXPORT_SYMBOL(drm_object_property_get_value);
3703
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003704/**
3705 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3706 * @dev: DRM device
3707 * @data: ioctl data
3708 * @file_priv: DRM file info
3709 *
3710 * This function retrieves the current value for an connectors's property.
3711 *
3712 * Called by the user via ioctl.
3713 *
3714 * Returns:
3715 * Zero on success, errno on failure.
3716 */
Dave Airlief453ba02008-11-07 14:05:41 -08003717int drm_mode_getproperty_ioctl(struct drm_device *dev,
3718 void *data, struct drm_file *file_priv)
3719{
Dave Airlief453ba02008-11-07 14:05:41 -08003720 struct drm_mode_get_property *out_resp = data;
3721 struct drm_property *property;
3722 int enum_count = 0;
3723 int blob_count = 0;
3724 int value_count = 0;
3725 int ret = 0, i;
3726 int copied;
3727 struct drm_property_enum *prop_enum;
3728 struct drm_mode_property_enum __user *enum_ptr;
3729 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003730 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003731 uint64_t __user *values_ptr;
3732 uint32_t __user *blob_length_ptr;
3733
Dave Airliefb3b06c2011-02-08 13:55:21 +10003734 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3735 return -EINVAL;
3736
Daniel Vetter84849902012-12-02 00:28:11 +01003737 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003738 property = drm_property_find(dev, out_resp->prop_id);
3739 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003740 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003741 goto done;
3742 }
Dave Airlief453ba02008-11-07 14:05:41 -08003743
Rob Clark5ea22f22014-05-30 11:34:01 -04003744 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3745 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003746 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3747 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003748 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003749 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3750 blob_count++;
3751 }
3752
3753 value_count = property->num_values;
3754
3755 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3756 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3757 out_resp->flags = property->flags;
3758
3759 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003760 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003761 for (i = 0; i < value_count; i++) {
3762 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3763 ret = -EFAULT;
3764 goto done;
3765 }
3766 }
3767 }
3768 out_resp->count_values = value_count;
3769
Rob Clark5ea22f22014-05-30 11:34:01 -04003770 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3771 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003772 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3773 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003774 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003775 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3776
3777 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3778 ret = -EFAULT;
3779 goto done;
3780 }
3781
3782 if (copy_to_user(&enum_ptr[copied].name,
3783 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3784 ret = -EFAULT;
3785 goto done;
3786 }
3787 copied++;
3788 }
3789 }
3790 out_resp->count_enum_blobs = enum_count;
3791 }
3792
Rob Clark5ea22f22014-05-30 11:34:01 -04003793 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003794 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3795 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003796 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3797 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003798
3799 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3800 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3801 ret = -EFAULT;
3802 goto done;
3803 }
3804
3805 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3806 ret = -EFAULT;
3807 goto done;
3808 }
3809
3810 copied++;
3811 }
3812 }
3813 out_resp->count_enum_blobs = blob_count;
3814 }
3815done:
Daniel Vetter84849902012-12-02 00:28:11 +01003816 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003817 return ret;
3818}
3819
3820static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3821 void *data)
3822{
3823 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003824 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003825
3826 if (!length || !data)
3827 return NULL;
3828
3829 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3830 if (!blob)
3831 return NULL;
3832
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003833 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3834 if (ret) {
3835 kfree(blob);
3836 return NULL;
3837 }
3838
Dave Airlief453ba02008-11-07 14:05:41 -08003839 blob->length = length;
3840
3841 memcpy(blob->data, data, length);
3842
Dave Airlief453ba02008-11-07 14:05:41 -08003843 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3844 return blob;
3845}
3846
3847static void drm_property_destroy_blob(struct drm_device *dev,
3848 struct drm_property_blob *blob)
3849{
3850 drm_mode_object_put(dev, &blob->base);
3851 list_del(&blob->head);
3852 kfree(blob);
3853}
3854
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003855/**
3856 * drm_mode_getblob_ioctl - get the contents of a blob property value
3857 * @dev: DRM device
3858 * @data: ioctl data
3859 * @file_priv: DRM file info
3860 *
3861 * This function retrieves the contents of a blob property. The value stored in
3862 * an object's blob property is just a normal modeset object id.
3863 *
3864 * Called by the user via ioctl.
3865 *
3866 * Returns:
3867 * Zero on success, errno on failure.
3868 */
Dave Airlief453ba02008-11-07 14:05:41 -08003869int drm_mode_getblob_ioctl(struct drm_device *dev,
3870 void *data, struct drm_file *file_priv)
3871{
Dave Airlief453ba02008-11-07 14:05:41 -08003872 struct drm_mode_get_blob *out_resp = data;
3873 struct drm_property_blob *blob;
3874 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003875 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003876
Dave Airliefb3b06c2011-02-08 13:55:21 +10003877 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3878 return -EINVAL;
3879
Daniel Vetter84849902012-12-02 00:28:11 +01003880 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003881 blob = drm_property_blob_find(dev, out_resp->blob_id);
3882 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003883 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003884 goto done;
3885 }
Dave Airlief453ba02008-11-07 14:05:41 -08003886
3887 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003888 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003889 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3890 ret = -EFAULT;
3891 goto done;
3892 }
3893 }
3894 out_resp->length = blob->length;
3895
3896done:
Daniel Vetter84849902012-12-02 00:28:11 +01003897 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003898 return ret;
3899}
3900
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003901/**
3902 * drm_mode_connector_update_edid_property - update the edid property of a connector
3903 * @connector: drm connector
3904 * @edid: new value of the edid property
3905 *
3906 * This function creates a new blob modeset object and assigns its id to the
3907 * connector's edid property.
3908 *
3909 * Returns:
3910 * Zero on success, errno on failure.
3911 */
Dave Airlief453ba02008-11-07 14:05:41 -08003912int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3913 struct edid *edid)
3914{
3915 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003916 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003917
3918 if (connector->edid_blob_ptr)
3919 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3920
3921 /* Delete edid, when there is none. */
3922 if (!edid) {
3923 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003924 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003925 return ret;
3926 }
3927
Adam Jackson7466f4c2010-03-29 21:43:23 +00003928 size = EDID_LENGTH * (1 + edid->extensions);
3929 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3930 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003931 if (!connector->edid_blob_ptr)
3932 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003933
Rob Clark58495562012-10-11 20:50:56 -05003934 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003935 dev->mode_config.edid_property,
3936 connector->edid_blob_ptr->base.id);
3937
3938 return ret;
3939}
3940EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3941
Paulo Zanoni26a34812012-05-15 18:08:59 -03003942static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003943 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003944{
3945 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3946 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04003947
3948 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03003949 if (value < property->values[0] || value > property->values[1])
3950 return false;
3951 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05003952 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3953 int64_t svalue = U642I64(value);
3954 if (svalue < U642I64(property->values[0]) ||
3955 svalue > U642I64(property->values[1]))
3956 return false;
3957 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04003958 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06003959 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003960 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003961 for (i = 0; i < property->num_values; i++)
3962 valid_mask |= (1ULL << property->values[i]);
3963 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04003964 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003965 /* Only the driver knows */
3966 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04003967 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3968 struct drm_mode_object *obj;
3969 /* a zero value for an object property translates to null: */
3970 if (value == 0)
3971 return true;
3972 /*
3973 * NOTE: use _object_find() directly to bypass restriction on
3974 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3975 * object this could race against object finalization, so it
3976 * simply tells us that the object *was* valid. Which is good
3977 * enough.
3978 */
3979 obj = _object_find(property->dev, value, property->values[0]);
3980 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003981 } else {
3982 int i;
3983 for (i = 0; i < property->num_values; i++)
3984 if (property->values[i] == value)
3985 return true;
3986 return false;
3987 }
3988}
3989
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003990/**
3991 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3992 * @dev: DRM device
3993 * @data: ioctl data
3994 * @file_priv: DRM file info
3995 *
3996 * This function sets the current value for a connectors's property. It also
3997 * calls into a driver's ->set_property callback to update the hardware state
3998 *
3999 * Called by the user via ioctl.
4000 *
4001 * Returns:
4002 * Zero on success, errno on failure.
4003 */
Dave Airlief453ba02008-11-07 14:05:41 -08004004int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4005 void *data, struct drm_file *file_priv)
4006{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004007 struct drm_mode_connector_set_property *conn_set_prop = data;
4008 struct drm_mode_obj_set_property obj_set_prop = {
4009 .value = conn_set_prop->value,
4010 .prop_id = conn_set_prop->prop_id,
4011 .obj_id = conn_set_prop->connector_id,
4012 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4013 };
Dave Airlief453ba02008-11-07 14:05:41 -08004014
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004015 /* It does all the locking and checking we need */
4016 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004017}
4018
Paulo Zanonic5431882012-05-15 18:09:02 -03004019static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4020 struct drm_property *property,
4021 uint64_t value)
4022{
4023 int ret = -EINVAL;
4024 struct drm_connector *connector = obj_to_connector(obj);
4025
4026 /* Do DPMS ourselves */
4027 if (property == connector->dev->mode_config.dpms_property) {
4028 if (connector->funcs->dpms)
4029 (*connector->funcs->dpms)(connector, (int)value);
4030 ret = 0;
4031 } else if (connector->funcs->set_property)
4032 ret = connector->funcs->set_property(connector, property, value);
4033
4034 /* store the property value if successful */
4035 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004036 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004037 return ret;
4038}
4039
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004040static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4041 struct drm_property *property,
4042 uint64_t value)
4043{
4044 int ret = -EINVAL;
4045 struct drm_crtc *crtc = obj_to_crtc(obj);
4046
4047 if (crtc->funcs->set_property)
4048 ret = crtc->funcs->set_property(crtc, property, value);
4049 if (!ret)
4050 drm_object_property_set_value(obj, property, value);
4051
4052 return ret;
4053}
4054
Rob Clark4d939142012-05-17 02:23:27 -06004055static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4056 struct drm_property *property,
4057 uint64_t value)
4058{
4059 int ret = -EINVAL;
4060 struct drm_plane *plane = obj_to_plane(obj);
4061
4062 if (plane->funcs->set_property)
4063 ret = plane->funcs->set_property(plane, property, value);
4064 if (!ret)
4065 drm_object_property_set_value(obj, property, value);
4066
4067 return ret;
4068}
4069
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004070/**
4071 * drm_mode_getproperty_ioctl - get the current value of a object's property
4072 * @dev: DRM device
4073 * @data: ioctl data
4074 * @file_priv: DRM file info
4075 *
4076 * This function retrieves the current value for an object's property. Compared
4077 * to the connector specific ioctl this one is extended to also work on crtc and
4078 * plane objects.
4079 *
4080 * Called by the user via ioctl.
4081 *
4082 * Returns:
4083 * Zero on success, errno on failure.
4084 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004085int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4086 struct drm_file *file_priv)
4087{
4088 struct drm_mode_obj_get_properties *arg = data;
4089 struct drm_mode_object *obj;
4090 int ret = 0;
4091 int i;
4092 int copied = 0;
4093 int props_count = 0;
4094 uint32_t __user *props_ptr;
4095 uint64_t __user *prop_values_ptr;
4096
4097 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4098 return -EINVAL;
4099
Daniel Vetter84849902012-12-02 00:28:11 +01004100 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004101
4102 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4103 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004104 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004105 goto out;
4106 }
4107 if (!obj->properties) {
4108 ret = -EINVAL;
4109 goto out;
4110 }
4111
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004112 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004113
4114 /* This ioctl is called twice, once to determine how much space is
4115 * needed, and the 2nd time to fill it. */
4116 if ((arg->count_props >= props_count) && props_count) {
4117 copied = 0;
4118 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4119 prop_values_ptr = (uint64_t __user *)(unsigned long)
4120 (arg->prop_values_ptr);
4121 for (i = 0; i < props_count; i++) {
4122 if (put_user(obj->properties->ids[i],
4123 props_ptr + copied)) {
4124 ret = -EFAULT;
4125 goto out;
4126 }
4127 if (put_user(obj->properties->values[i],
4128 prop_values_ptr + copied)) {
4129 ret = -EFAULT;
4130 goto out;
4131 }
4132 copied++;
4133 }
4134 }
4135 arg->count_props = props_count;
4136out:
Daniel Vetter84849902012-12-02 00:28:11 +01004137 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004138 return ret;
4139}
4140
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004141/**
4142 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4143 * @dev: DRM device
4144 * @data: ioctl data
4145 * @file_priv: DRM file info
4146 *
4147 * This function sets the current value for an object's property. It also calls
4148 * into a driver's ->set_property callback to update the hardware state.
4149 * Compared to the connector specific ioctl this one is extended to also work on
4150 * crtc and plane objects.
4151 *
4152 * Called by the user via ioctl.
4153 *
4154 * Returns:
4155 * Zero on success, errno on failure.
4156 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004157int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4158 struct drm_file *file_priv)
4159{
4160 struct drm_mode_obj_set_property *arg = data;
4161 struct drm_mode_object *arg_obj;
4162 struct drm_mode_object *prop_obj;
4163 struct drm_property *property;
4164 int ret = -EINVAL;
4165 int i;
4166
4167 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4168 return -EINVAL;
4169
Daniel Vetter84849902012-12-02 00:28:11 +01004170 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004171
4172 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004173 if (!arg_obj) {
4174 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004175 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004176 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004177 if (!arg_obj->properties)
4178 goto out;
4179
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004180 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004181 if (arg_obj->properties->ids[i] == arg->prop_id)
4182 break;
4183
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004184 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004185 goto out;
4186
4187 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4188 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004189 if (!prop_obj) {
4190 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004191 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004192 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004193 property = obj_to_property(prop_obj);
4194
4195 if (!drm_property_change_is_valid(property, arg->value))
4196 goto out;
4197
4198 switch (arg_obj->type) {
4199 case DRM_MODE_OBJECT_CONNECTOR:
4200 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4201 arg->value);
4202 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004203 case DRM_MODE_OBJECT_CRTC:
4204 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4205 break;
Rob Clark4d939142012-05-17 02:23:27 -06004206 case DRM_MODE_OBJECT_PLANE:
4207 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4208 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004209 }
4210
4211out:
Daniel Vetter84849902012-12-02 00:28:11 +01004212 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004213 return ret;
4214}
4215
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004216/**
4217 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4218 * @connector: connector to attach
4219 * @encoder: encoder to attach @connector to
4220 *
4221 * This function links up a connector to an encoder. Note that the routing
4222 * restrictions between encoders and crtcs are exposed to userspace through the
4223 * possible_clones and possible_crtcs bitmasks.
4224 *
4225 * Returns:
4226 * Zero on success, errno on failure.
4227 */
Dave Airlief453ba02008-11-07 14:05:41 -08004228int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4229 struct drm_encoder *encoder)
4230{
4231 int i;
4232
4233 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4234 if (connector->encoder_ids[i] == 0) {
4235 connector->encoder_ids[i] = encoder->base.id;
4236 return 0;
4237 }
4238 }
4239 return -ENOMEM;
4240}
4241EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4242
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004243/**
4244 * drm_mode_crtc_set_gamma_size - set the gamma table size
4245 * @crtc: CRTC to set the gamma table size for
4246 * @gamma_size: size of the gamma table
4247 *
4248 * Drivers which support gamma tables should set this to the supported gamma
4249 * table size when initializing the CRTC. Currently the drm core only supports a
4250 * fixed gamma table size.
4251 *
4252 * Returns:
4253 * Zero on success, errno on failure.
4254 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004255int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004256 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004257{
4258 crtc->gamma_size = gamma_size;
4259
4260 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4261 if (!crtc->gamma_store) {
4262 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004263 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004264 }
4265
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004266 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004267}
4268EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4269
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004270/**
4271 * drm_mode_gamma_set_ioctl - set the gamma table
4272 * @dev: DRM device
4273 * @data: ioctl data
4274 * @file_priv: DRM file info
4275 *
4276 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4277 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4278 *
4279 * Called by the user via ioctl.
4280 *
4281 * Returns:
4282 * Zero on success, errno on failure.
4283 */
Dave Airlief453ba02008-11-07 14:05:41 -08004284int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4285 void *data, struct drm_file *file_priv)
4286{
4287 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004288 struct drm_crtc *crtc;
4289 void *r_base, *g_base, *b_base;
4290 int size;
4291 int ret = 0;
4292
Dave Airliefb3b06c2011-02-08 13:55:21 +10004293 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4294 return -EINVAL;
4295
Daniel Vetter84849902012-12-02 00:28:11 +01004296 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004297 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4298 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004299 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004300 goto out;
4301 }
Dave Airlief453ba02008-11-07 14:05:41 -08004302
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004303 if (crtc->funcs->gamma_set == NULL) {
4304 ret = -ENOSYS;
4305 goto out;
4306 }
4307
Dave Airlief453ba02008-11-07 14:05:41 -08004308 /* memcpy into gamma store */
4309 if (crtc_lut->gamma_size != crtc->gamma_size) {
4310 ret = -EINVAL;
4311 goto out;
4312 }
4313
4314 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4315 r_base = crtc->gamma_store;
4316 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4317 ret = -EFAULT;
4318 goto out;
4319 }
4320
4321 g_base = r_base + size;
4322 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4323 ret = -EFAULT;
4324 goto out;
4325 }
4326
4327 b_base = g_base + size;
4328 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4329 ret = -EFAULT;
4330 goto out;
4331 }
4332
James Simmons72034252010-08-03 01:33:19 +01004333 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004334
4335out:
Daniel Vetter84849902012-12-02 00:28:11 +01004336 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004337 return ret;
4338
4339}
4340
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004341/**
4342 * drm_mode_gamma_get_ioctl - get the gamma table
4343 * @dev: DRM device
4344 * @data: ioctl data
4345 * @file_priv: DRM file info
4346 *
4347 * Copy the current gamma table into the storage provided. This also provides
4348 * the gamma table size the driver expects, which can be used to size the
4349 * allocated storage.
4350 *
4351 * Called by the user via ioctl.
4352 *
4353 * Returns:
4354 * Zero on success, errno on failure.
4355 */
Dave Airlief453ba02008-11-07 14:05:41 -08004356int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4357 void *data, struct drm_file *file_priv)
4358{
4359 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004360 struct drm_crtc *crtc;
4361 void *r_base, *g_base, *b_base;
4362 int size;
4363 int ret = 0;
4364
Dave Airliefb3b06c2011-02-08 13:55:21 +10004365 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4366 return -EINVAL;
4367
Daniel Vetter84849902012-12-02 00:28:11 +01004368 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004369 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4370 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004371 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004372 goto out;
4373 }
Dave Airlief453ba02008-11-07 14:05:41 -08004374
4375 /* memcpy into gamma store */
4376 if (crtc_lut->gamma_size != crtc->gamma_size) {
4377 ret = -EINVAL;
4378 goto out;
4379 }
4380
4381 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4382 r_base = crtc->gamma_store;
4383 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4384 ret = -EFAULT;
4385 goto out;
4386 }
4387
4388 g_base = r_base + size;
4389 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4390 ret = -EFAULT;
4391 goto out;
4392 }
4393
4394 b_base = g_base + size;
4395 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4396 ret = -EFAULT;
4397 goto out;
4398 }
4399out:
Daniel Vetter84849902012-12-02 00:28:11 +01004400 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004401 return ret;
4402}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004403
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004404/**
4405 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4406 * @dev: DRM device
4407 * @data: ioctl data
4408 * @file_priv: DRM file info
4409 *
4410 * This schedules an asynchronous update on a given CRTC, called page flip.
4411 * Optionally a drm event is generated to signal the completion of the event.
4412 * Generic drivers cannot assume that a pageflip with changed framebuffer
4413 * properties (including driver specific metadata like tiling layout) will work,
4414 * but some drivers support e.g. pixel format changes through the pageflip
4415 * ioctl.
4416 *
4417 * Called by the user via ioctl.
4418 *
4419 * Returns:
4420 * Zero on success, errno on failure.
4421 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004422int drm_mode_page_flip_ioctl(struct drm_device *dev,
4423 void *data, struct drm_file *file_priv)
4424{
4425 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004426 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004427 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004428 struct drm_pending_vblank_event *e = NULL;
4429 unsigned long flags;
4430 int ret = -EINVAL;
4431
4432 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4433 page_flip->reserved != 0)
4434 return -EINVAL;
4435
Keith Packard62f21042013-07-22 18:50:00 -07004436 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4437 return -EINVAL;
4438
Rob Clarka2b34e22013-10-05 16:36:52 -04004439 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4440 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004441 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004442
Rob Clark51fd3712013-11-19 12:10:12 -05004443 drm_modeset_lock(&crtc->mutex, NULL);
Matt Roperf4510a22014-04-01 15:22:40 -07004444 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004445 /* The framebuffer is currently unbound, presumably
4446 * due to a hotplug event, that userspace has not
4447 * yet discovered.
4448 */
4449 ret = -EBUSY;
4450 goto out;
4451 }
4452
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004453 if (crtc->funcs->page_flip == NULL)
4454 goto out;
4455
Daniel Vetter786b99e2012-12-02 21:53:40 +01004456 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004457 if (!fb) {
4458 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004459 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004460 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004461
Damien Lespiauc11e9282013-09-25 16:45:30 +01004462 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4463 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004464 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004465
Matt Roperf4510a22014-04-01 15:22:40 -07004466 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004467 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4468 ret = -EINVAL;
4469 goto out;
4470 }
4471
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004472 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4473 ret = -ENOMEM;
4474 spin_lock_irqsave(&dev->event_lock, flags);
4475 if (file_priv->event_space < sizeof e->event) {
4476 spin_unlock_irqrestore(&dev->event_lock, flags);
4477 goto out;
4478 }
4479 file_priv->event_space -= sizeof e->event;
4480 spin_unlock_irqrestore(&dev->event_lock, flags);
4481
4482 e = kzalloc(sizeof *e, GFP_KERNEL);
4483 if (e == NULL) {
4484 spin_lock_irqsave(&dev->event_lock, flags);
4485 file_priv->event_space += sizeof e->event;
4486 spin_unlock_irqrestore(&dev->event_lock, flags);
4487 goto out;
4488 }
4489
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004490 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004491 e->event.base.length = sizeof e->event;
4492 e->event.user_data = page_flip->user_data;
4493 e->base.event = &e->event.base;
4494 e->base.file_priv = file_priv;
4495 e->base.destroy =
4496 (void (*) (struct drm_pending_event *)) kfree;
4497 }
4498
Matt Roperf4510a22014-04-01 15:22:40 -07004499 old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004500 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004501 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004502 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4503 spin_lock_irqsave(&dev->event_lock, flags);
4504 file_priv->event_space += sizeof e->event;
4505 spin_unlock_irqrestore(&dev->event_lock, flags);
4506 kfree(e);
4507 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004508 /* Keep the old fb, don't unref it. */
4509 old_fb = NULL;
4510 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004511 /*
4512 * Warn if the driver hasn't properly updated the crtc->fb
4513 * field to reflect that the new framebuffer is now used.
4514 * Failing to do so will screw with the reference counting
4515 * on framebuffers.
4516 */
Matt Roperf4510a22014-04-01 15:22:40 -07004517 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004518 /* Unref only the old framebuffer. */
4519 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004520 }
4521
4522out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004523 if (fb)
4524 drm_framebuffer_unreference(fb);
4525 if (old_fb)
4526 drm_framebuffer_unreference(old_fb);
Rob Clark51fd3712013-11-19 12:10:12 -05004527 drm_modeset_unlock(&crtc->mutex);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004528
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004529 return ret;
4530}
Chris Wilsoneb033552011-01-24 15:11:08 +00004531
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004532/**
4533 * drm_mode_config_reset - call ->reset callbacks
4534 * @dev: drm device
4535 *
4536 * This functions calls all the crtc's, encoder's and connector's ->reset
4537 * callback. Drivers can use this in e.g. their driver load or resume code to
4538 * reset hardware and software state.
4539 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004540void drm_mode_config_reset(struct drm_device *dev)
4541{
4542 struct drm_crtc *crtc;
4543 struct drm_encoder *encoder;
4544 struct drm_connector *connector;
4545
4546 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4547 if (crtc->funcs->reset)
4548 crtc->funcs->reset(crtc);
4549
4550 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4551 if (encoder->funcs->reset)
4552 encoder->funcs->reset(encoder);
4553
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004554 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4555 connector->status = connector_status_unknown;
4556
Chris Wilsoneb033552011-01-24 15:11:08 +00004557 if (connector->funcs->reset)
4558 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004559 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004560}
4561EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004562
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004563/**
4564 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4565 * @dev: DRM device
4566 * @data: ioctl data
4567 * @file_priv: DRM file info
4568 *
4569 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4570 * TTM or something else entirely) and returns the resulting buffer handle. This
4571 * handle can then be wrapped up into a framebuffer modeset object.
4572 *
4573 * Note that userspace is not allowed to use such objects for render
4574 * acceleration - drivers must create their own private ioctls for such a use
4575 * case.
4576 *
4577 * Called by the user via ioctl.
4578 *
4579 * Returns:
4580 * Zero on success, errno on failure.
4581 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004582int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4583 void *data, struct drm_file *file_priv)
4584{
4585 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004586 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004587
4588 if (!dev->driver->dumb_create)
4589 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004590 if (!args->width || !args->height || !args->bpp)
4591 return -EINVAL;
4592
4593 /* overflow checks for 32bit size calculations */
4594 cpp = DIV_ROUND_UP(args->bpp, 8);
4595 if (cpp > 0xffffffffU / args->width)
4596 return -EINVAL;
4597 stride = cpp * args->width;
4598 if (args->height > 0xffffffffU / stride)
4599 return -EINVAL;
4600
4601 /* test for wrap-around */
4602 size = args->height * stride;
4603 if (PAGE_ALIGN(size) == 0)
4604 return -EINVAL;
4605
Dave Airlieff72145b2011-02-07 12:16:14 +10004606 return dev->driver->dumb_create(file_priv, dev, args);
4607}
4608
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004609/**
4610 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4611 * @dev: DRM device
4612 * @data: ioctl data
4613 * @file_priv: DRM file info
4614 *
4615 * Allocate an offset in the drm device node's address space to be able to
4616 * memory map a dumb buffer.
4617 *
4618 * Called by the user via ioctl.
4619 *
4620 * Returns:
4621 * Zero on success, errno on failure.
4622 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004623int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4624 void *data, struct drm_file *file_priv)
4625{
4626 struct drm_mode_map_dumb *args = data;
4627
4628 /* call driver ioctl to get mmap offset */
4629 if (!dev->driver->dumb_map_offset)
4630 return -ENOSYS;
4631
4632 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4633}
4634
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004635/**
4636 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4637 * @dev: DRM device
4638 * @data: ioctl data
4639 * @file_priv: DRM file info
4640 *
4641 * This destroys the userspace handle for the given dumb backing storage buffer.
4642 * Since buffer objects must be reference counted in the kernel a buffer object
4643 * won't be immediately freed if a framebuffer modeset object still uses it.
4644 *
4645 * Called by the user via ioctl.
4646 *
4647 * Returns:
4648 * Zero on success, errno on failure.
4649 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004650int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4651 void *data, struct drm_file *file_priv)
4652{
4653 struct drm_mode_destroy_dumb *args = data;
4654
4655 if (!dev->driver->dumb_destroy)
4656 return -ENOSYS;
4657
4658 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4659}
Dave Airlie248dbc22011-11-29 20:02:54 +00004660
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004661/**
4662 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4663 * @format: pixel format (DRM_FORMAT_*)
4664 * @depth: storage for the depth value
4665 * @bpp: storage for the bpp value
4666 *
4667 * This only supports RGB formats here for compat with code that doesn't use
4668 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004669 */
4670void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4671 int *bpp)
4672{
4673 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004674 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004675 case DRM_FORMAT_RGB332:
4676 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004677 *depth = 8;
4678 *bpp = 8;
4679 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004680 case DRM_FORMAT_XRGB1555:
4681 case DRM_FORMAT_XBGR1555:
4682 case DRM_FORMAT_RGBX5551:
4683 case DRM_FORMAT_BGRX5551:
4684 case DRM_FORMAT_ARGB1555:
4685 case DRM_FORMAT_ABGR1555:
4686 case DRM_FORMAT_RGBA5551:
4687 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004688 *depth = 15;
4689 *bpp = 16;
4690 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004691 case DRM_FORMAT_RGB565:
4692 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004693 *depth = 16;
4694 *bpp = 16;
4695 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004696 case DRM_FORMAT_RGB888:
4697 case DRM_FORMAT_BGR888:
4698 *depth = 24;
4699 *bpp = 24;
4700 break;
4701 case DRM_FORMAT_XRGB8888:
4702 case DRM_FORMAT_XBGR8888:
4703 case DRM_FORMAT_RGBX8888:
4704 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004705 *depth = 24;
4706 *bpp = 32;
4707 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004708 case DRM_FORMAT_XRGB2101010:
4709 case DRM_FORMAT_XBGR2101010:
4710 case DRM_FORMAT_RGBX1010102:
4711 case DRM_FORMAT_BGRX1010102:
4712 case DRM_FORMAT_ARGB2101010:
4713 case DRM_FORMAT_ABGR2101010:
4714 case DRM_FORMAT_RGBA1010102:
4715 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004716 *depth = 30;
4717 *bpp = 32;
4718 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004719 case DRM_FORMAT_ARGB8888:
4720 case DRM_FORMAT_ABGR8888:
4721 case DRM_FORMAT_RGBA8888:
4722 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004723 *depth = 32;
4724 *bpp = 32;
4725 break;
4726 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004727 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4728 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004729 *depth = 0;
4730 *bpp = 0;
4731 break;
4732 }
4733}
4734EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004735
4736/**
4737 * drm_format_num_planes - get the number of planes for format
4738 * @format: pixel format (DRM_FORMAT_*)
4739 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004740 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004741 * The number of planes used by the specified pixel format.
4742 */
4743int drm_format_num_planes(uint32_t format)
4744{
4745 switch (format) {
4746 case DRM_FORMAT_YUV410:
4747 case DRM_FORMAT_YVU410:
4748 case DRM_FORMAT_YUV411:
4749 case DRM_FORMAT_YVU411:
4750 case DRM_FORMAT_YUV420:
4751 case DRM_FORMAT_YVU420:
4752 case DRM_FORMAT_YUV422:
4753 case DRM_FORMAT_YVU422:
4754 case DRM_FORMAT_YUV444:
4755 case DRM_FORMAT_YVU444:
4756 return 3;
4757 case DRM_FORMAT_NV12:
4758 case DRM_FORMAT_NV21:
4759 case DRM_FORMAT_NV16:
4760 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004761 case DRM_FORMAT_NV24:
4762 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004763 return 2;
4764 default:
4765 return 1;
4766 }
4767}
4768EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004769
4770/**
4771 * drm_format_plane_cpp - determine the bytes per pixel value
4772 * @format: pixel format (DRM_FORMAT_*)
4773 * @plane: plane index
4774 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004775 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004776 * The bytes per pixel value for the specified plane.
4777 */
4778int drm_format_plane_cpp(uint32_t format, int plane)
4779{
4780 unsigned int depth;
4781 int bpp;
4782
4783 if (plane >= drm_format_num_planes(format))
4784 return 0;
4785
4786 switch (format) {
4787 case DRM_FORMAT_YUYV:
4788 case DRM_FORMAT_YVYU:
4789 case DRM_FORMAT_UYVY:
4790 case DRM_FORMAT_VYUY:
4791 return 2;
4792 case DRM_FORMAT_NV12:
4793 case DRM_FORMAT_NV21:
4794 case DRM_FORMAT_NV16:
4795 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004796 case DRM_FORMAT_NV24:
4797 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004798 return plane ? 2 : 1;
4799 case DRM_FORMAT_YUV410:
4800 case DRM_FORMAT_YVU410:
4801 case DRM_FORMAT_YUV411:
4802 case DRM_FORMAT_YVU411:
4803 case DRM_FORMAT_YUV420:
4804 case DRM_FORMAT_YVU420:
4805 case DRM_FORMAT_YUV422:
4806 case DRM_FORMAT_YVU422:
4807 case DRM_FORMAT_YUV444:
4808 case DRM_FORMAT_YVU444:
4809 return 1;
4810 default:
4811 drm_fb_get_bpp_depth(format, &depth, &bpp);
4812 return bpp >> 3;
4813 }
4814}
4815EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004816
4817/**
4818 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4819 * @format: pixel format (DRM_FORMAT_*)
4820 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004821 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004822 * The horizontal chroma subsampling factor for the
4823 * specified pixel format.
4824 */
4825int drm_format_horz_chroma_subsampling(uint32_t format)
4826{
4827 switch (format) {
4828 case DRM_FORMAT_YUV411:
4829 case DRM_FORMAT_YVU411:
4830 case DRM_FORMAT_YUV410:
4831 case DRM_FORMAT_YVU410:
4832 return 4;
4833 case DRM_FORMAT_YUYV:
4834 case DRM_FORMAT_YVYU:
4835 case DRM_FORMAT_UYVY:
4836 case DRM_FORMAT_VYUY:
4837 case DRM_FORMAT_NV12:
4838 case DRM_FORMAT_NV21:
4839 case DRM_FORMAT_NV16:
4840 case DRM_FORMAT_NV61:
4841 case DRM_FORMAT_YUV422:
4842 case DRM_FORMAT_YVU422:
4843 case DRM_FORMAT_YUV420:
4844 case DRM_FORMAT_YVU420:
4845 return 2;
4846 default:
4847 return 1;
4848 }
4849}
4850EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4851
4852/**
4853 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4854 * @format: pixel format (DRM_FORMAT_*)
4855 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004856 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004857 * The vertical chroma subsampling factor for the
4858 * specified pixel format.
4859 */
4860int drm_format_vert_chroma_subsampling(uint32_t format)
4861{
4862 switch (format) {
4863 case DRM_FORMAT_YUV410:
4864 case DRM_FORMAT_YVU410:
4865 return 4;
4866 case DRM_FORMAT_YUV420:
4867 case DRM_FORMAT_YVU420:
4868 case DRM_FORMAT_NV12:
4869 case DRM_FORMAT_NV21:
4870 return 2;
4871 default:
4872 return 1;
4873 }
4874}
4875EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004876
4877/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05304878 * drm_rotation_simplify() - Try to simplify the rotation
4879 * @rotation: Rotation to be simplified
4880 * @supported_rotations: Supported rotations
4881 *
4882 * Attempt to simplify the rotation to a form that is supported.
4883 * Eg. if the hardware supports everything except DRM_REFLECT_X
4884 * one could call this function like this:
4885 *
4886 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
4887 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
4888 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
4889 *
4890 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
4891 * transforms the hardware supports, this function may not
4892 * be able to produce a supported transform, so the caller should
4893 * check the result afterwards.
4894 */
4895unsigned int drm_rotation_simplify(unsigned int rotation,
4896 unsigned int supported_rotations)
4897{
4898 if (rotation & ~supported_rotations) {
4899 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
4900 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
4901 }
4902
4903 return rotation;
4904}
4905EXPORT_SYMBOL(drm_rotation_simplify);
4906
4907/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004908 * drm_mode_config_init - initialize DRM mode_configuration structure
4909 * @dev: DRM device
4910 *
4911 * Initialize @dev's mode_config structure, used for tracking the graphics
4912 * configuration of @dev.
4913 *
4914 * Since this initializes the modeset locks, no locking is possible. Which is no
4915 * problem, since this should happen single threaded at init time. It is the
4916 * driver's problem to ensure this guarantee.
4917 *
4918 */
4919void drm_mode_config_init(struct drm_device *dev)
4920{
4921 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004922 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004923 mutex_init(&dev->mode_config.idr_mutex);
4924 mutex_init(&dev->mode_config.fb_lock);
4925 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4926 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4927 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04004928 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004929 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4930 INIT_LIST_HEAD(&dev->mode_config.property_list);
4931 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4932 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4933 idr_init(&dev->mode_config.crtc_idr);
4934
4935 drm_modeset_lock_all(dev);
4936 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04004937 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004938 drm_modeset_unlock_all(dev);
4939
4940 /* Just to be sure */
4941 dev->mode_config.num_fb = 0;
4942 dev->mode_config.num_connector = 0;
4943 dev->mode_config.num_crtc = 0;
4944 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004945 dev->mode_config.num_overlay_plane = 0;
4946 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004947}
4948EXPORT_SYMBOL(drm_mode_config_init);
4949
4950/**
4951 * drm_mode_config_cleanup - free up DRM mode_config info
4952 * @dev: DRM device
4953 *
4954 * Free up all the connectors and CRTCs associated with this DRM device, then
4955 * free up the framebuffers and associated buffer objects.
4956 *
4957 * Note that since this /should/ happen single-threaded at driver/device
4958 * teardown time, no locking is required. It's the driver's job to ensure that
4959 * this guarantee actually holds true.
4960 *
4961 * FIXME: cleanup any dangling user buffer objects too
4962 */
4963void drm_mode_config_cleanup(struct drm_device *dev)
4964{
4965 struct drm_connector *connector, *ot;
4966 struct drm_crtc *crtc, *ct;
4967 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04004968 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004969 struct drm_framebuffer *fb, *fbt;
4970 struct drm_property *property, *pt;
4971 struct drm_property_blob *blob, *bt;
4972 struct drm_plane *plane, *plt;
4973
4974 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4975 head) {
4976 encoder->funcs->destroy(encoder);
4977 }
4978
Sean Paul3b336ec2013-08-14 16:47:37 -04004979 list_for_each_entry_safe(bridge, brt,
4980 &dev->mode_config.bridge_list, head) {
4981 bridge->funcs->destroy(bridge);
4982 }
4983
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004984 list_for_each_entry_safe(connector, ot,
4985 &dev->mode_config.connector_list, head) {
4986 connector->funcs->destroy(connector);
4987 }
4988
4989 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4990 head) {
4991 drm_property_destroy(dev, property);
4992 }
4993
4994 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4995 head) {
4996 drm_property_destroy_blob(dev, blob);
4997 }
4998
4999 /*
5000 * Single-threaded teardown context, so it's not required to grab the
5001 * fb_lock to protect against concurrent fb_list access. Contrary, it
5002 * would actually deadlock with the drm_framebuffer_cleanup function.
5003 *
5004 * Also, if there are any framebuffers left, that's a driver leak now,
5005 * so politely WARN about this.
5006 */
5007 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5008 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5009 drm_framebuffer_remove(fb);
5010 }
5011
5012 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5013 head) {
5014 plane->funcs->destroy(plane);
5015 }
5016
5017 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5018 crtc->funcs->destroy(crtc);
5019 }
5020
5021 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005022 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005023}
5024EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305025
5026struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5027 unsigned int supported_rotations)
5028{
5029 static const struct drm_prop_enum_list props[] = {
5030 { DRM_ROTATE_0, "rotate-0" },
5031 { DRM_ROTATE_90, "rotate-90" },
5032 { DRM_ROTATE_180, "rotate-180" },
5033 { DRM_ROTATE_270, "rotate-270" },
5034 { DRM_REFLECT_X, "reflect-x" },
5035 { DRM_REFLECT_Y, "reflect-y" },
5036 };
5037
5038 return drm_property_create_bitmask(dev, 0, "rotation",
5039 props, ARRAY_SIZE(props),
5040 supported_rotations);
5041}
5042EXPORT_SYMBOL(drm_mode_create_rotation_property);