blob: 7d7c1fd15443f233cc8c75f4dd729abe32c4ff8b [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
Dave Airlief453ba02008-11-07 14:05:41 -080048/* Avoid boilerplate. I'm tired of typing. */
49#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000050 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080051 { \
52 int i; \
53 for (i = 0; i < ARRAY_SIZE(list); i++) { \
54 if (list[i].type == val) \
55 return list[i].name; \
56 } \
57 return "(unknown)"; \
58 }
59
60/*
61 * Global properties
62 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +000063static const struct drm_prop_enum_list drm_dpms_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -080064{ { DRM_MODE_DPMS_ON, "On" },
65 { DRM_MODE_DPMS_STANDBY, "Standby" },
66 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
67 { DRM_MODE_DPMS_OFF, "Off" }
68};
69
70DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
71
Rob Clark9922ab52014-04-01 20:16:57 -040072static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
73{
74 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
75 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
76 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
77};
78
Dave Airlief453ba02008-11-07 14:05:41 -080079/*
80 * Optional properties
81 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +000082static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -080083{
Jesse Barnes53bd8382009-07-01 10:04:40 -070084 { DRM_MODE_SCALE_NONE, "None" },
85 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
86 { DRM_MODE_SCALE_CENTER, "Center" },
87 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080088};
89
Vandana Kannanff587e42014-06-11 10:46:48 +053090static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
91 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
92 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
93 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
94};
95
Dave Airlief453ba02008-11-07 14:05:41 -080096/*
97 * Non-global properties, but "required" for certain connectors.
98 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +000099static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800100{
101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
104};
105
106DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
107
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000108static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800109{
110 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
111 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
112 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
113};
114
115DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
116 drm_dvi_i_subconnector_enum_list)
117
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000118static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800119{
120 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
121 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
123 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200124 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800125};
126
127DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
128
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000129static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800130{
131 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
132 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
133 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
134 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200135 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800136};
137
138DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
139 drm_tv_subconnector_enum_list)
140
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000141static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000142 { DRM_MODE_DIRTY_OFF, "Off" },
143 { DRM_MODE_DIRTY_ON, "On" },
144 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
145};
146
Dave Airlief453ba02008-11-07 14:05:41 -0800147struct drm_conn_prop_enum_list {
148 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000149 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400150 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800151};
152
153/*
154 * Connector and encoder types.
155 */
156static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400157{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
158 { DRM_MODE_CONNECTOR_VGA, "VGA" },
159 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
160 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
161 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
162 { DRM_MODE_CONNECTOR_Composite, "Composite" },
163 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
164 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
165 { DRM_MODE_CONNECTOR_Component, "Component" },
166 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
167 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
168 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
169 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
170 { DRM_MODE_CONNECTOR_TV, "TV" },
171 { DRM_MODE_CONNECTOR_eDP, "eDP" },
172 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300173 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800174};
175
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000176static const struct drm_prop_enum_list drm_encoder_enum_list[] =
Dave Airlief453ba02008-11-07 14:05:41 -0800177{ { DRM_MODE_ENCODER_NONE, "None" },
178 { DRM_MODE_ENCODER_DAC, "DAC" },
179 { DRM_MODE_ENCODER_TMDS, "TMDS" },
180 { DRM_MODE_ENCODER_LVDS, "LVDS" },
181 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200182 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300183 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000184 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800185};
186
Jesse Barnesac1bb362014-02-10 15:32:44 -0800187static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
188{
189 { SubPixelUnknown, "Unknown" },
190 { SubPixelHorizontalRGB, "Horizontal RGB" },
191 { SubPixelHorizontalBGR, "Horizontal BGR" },
192 { SubPixelVerticalRGB, "Vertical RGB" },
193 { SubPixelVerticalBGR, "Vertical BGR" },
194 { SubPixelNone, "None" },
195};
196
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400197void drm_connector_ida_init(void)
198{
199 int i;
200
201 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
202 ida_init(&drm_connector_enum_list[i].ida);
203}
204
205void drm_connector_ida_destroy(void)
206{
207 int i;
208
209 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
210 ida_destroy(&drm_connector_enum_list[i].ida);
211}
212
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100213/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100214 * drm_get_connector_status_name - return a string for connector status
215 * @status: connector status to compute name of
216 *
217 * In contrast to the other drm_get_*_name functions this one here returns a
218 * const pointer and hence is threadsafe.
219 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000220const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800221{
222 if (status == connector_status_connected)
223 return "connected";
224 else if (status == connector_status_disconnected)
225 return "disconnected";
226 else
227 return "unknown";
228}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000229EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800230
Jesse Barnesac1bb362014-02-10 15:32:44 -0800231/**
232 * drm_get_subpixel_order_name - return a string for a given subpixel enum
233 * @order: enum of subpixel_order
234 *
235 * Note you could abuse this and return something out of bounds, but that
236 * would be a caller error. No unscrubbed user data should make it here.
237 */
238const char *drm_get_subpixel_order_name(enum subpixel_order order)
239{
240 return drm_subpixel_enum_list[order].name;
241}
242EXPORT_SYMBOL(drm_get_subpixel_order_name);
243
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300244static char printable_char(int c)
245{
246 return isascii(c) && isprint(c) ? c : '?';
247}
248
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100249/**
250 * drm_get_format_name - return a string for drm fourcc format
251 * @format: format to compute name of
252 *
253 * Note that the buffer used by this function is globally shared and owned by
254 * the function itself.
255 *
256 * FIXME: This isn't really multithreading safe.
257 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000258const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300259{
260 static char buf[32];
261
262 snprintf(buf, sizeof(buf),
263 "%c%c%c%c %s-endian (0x%08x)",
264 printable_char(format & 0xff),
265 printable_char((format >> 8) & 0xff),
266 printable_char((format >> 16) & 0xff),
267 printable_char((format >> 24) & 0x7f),
268 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
269 format);
270
271 return buf;
272}
273EXPORT_SYMBOL(drm_get_format_name);
274
Dave Airlie2ee39452014-07-24 11:53:45 +1000275/*
276 * Internal function to assign a slot in the object idr and optionally
277 * register the object into the idr.
278 */
279static int drm_mode_object_get_reg(struct drm_device *dev,
280 struct drm_mode_object *obj,
281 uint32_t obj_type,
282 bool register_obj)
283{
284 int ret;
285
286 mutex_lock(&dev->mode_config.idr_mutex);
287 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
288 if (ret >= 0) {
289 /*
290 * Set up the object linking under the protection of the idr
291 * lock so that other users can't see inconsistent state.
292 */
293 obj->id = ret;
294 obj->type = obj_type;
295 }
296 mutex_unlock(&dev->mode_config.idr_mutex);
297
298 return ret < 0 ? ret : 0;
299}
300
Dave Airlief453ba02008-11-07 14:05:41 -0800301/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100302 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800303 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100304 * @obj: object pointer, used to generate unique ID
305 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800306 *
Dave Airlief453ba02008-11-07 14:05:41 -0800307 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100308 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
309 * modeset identifiers are _not_ reference counted. Hence don't use this for
310 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800311 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100312 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800313 * New unique (relative to other objects in @dev) integer identifier for the
314 * object.
315 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100316int drm_mode_object_get(struct drm_device *dev,
317 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800318{
Dave Airlie2ee39452014-07-24 11:53:45 +1000319 return drm_mode_object_get_reg(dev, obj, obj_type, true);
320}
Dave Airlief453ba02008-11-07 14:05:41 -0800321
Dave Airlie2ee39452014-07-24 11:53:45 +1000322static void drm_mode_object_register(struct drm_device *dev,
323 struct drm_mode_object *obj)
324{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000325 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000326 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000327 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800328}
329
330/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100331 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800332 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100333 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800334 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100335 * Free @id from @dev's unique identifier pool. Note that despite the _get
336 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
337 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800338 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100339void drm_mode_object_put(struct drm_device *dev,
340 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800341{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000342 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800343 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000344 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800345}
346
Rob Clark98f75de2014-05-30 11:37:03 -0400347static struct drm_mode_object *_object_find(struct drm_device *dev,
348 uint32_t id, uint32_t type)
349{
350 struct drm_mode_object *obj = NULL;
351
352 mutex_lock(&dev->mode_config.idr_mutex);
353 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200354 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
355 obj = NULL;
356 if (obj && obj->id != id)
357 obj = NULL;
358 /* don't leak out unref'd fb's */
359 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Rob Clark98f75de2014-05-30 11:37:03 -0400360 obj = NULL;
361 mutex_unlock(&dev->mode_config.idr_mutex);
362
363 return obj;
364}
365
Daniel Vetter786b99e2012-12-02 21:53:40 +0100366/**
367 * drm_mode_object_find - look up a drm object with static lifetime
368 * @dev: drm device
369 * @id: id of the mode object
370 * @type: type of the mode object
371 *
372 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400373 * are reference counted, they need special treatment. Even with
374 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
375 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100376 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200377struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
378 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800379{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000380 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800381
Daniel Vetter786b99e2012-12-02 21:53:40 +0100382 /* Framebuffers are reference counted and need their own lookup
383 * function.*/
384 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400385 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800386 return obj;
387}
388EXPORT_SYMBOL(drm_mode_object_find);
389
390/**
Dave Airlief453ba02008-11-07 14:05:41 -0800391 * drm_framebuffer_init - initialize a framebuffer
392 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100393 * @fb: framebuffer to be initialized
394 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800395 *
Dave Airlief453ba02008-11-07 14:05:41 -0800396 * Allocates an ID for the framebuffer's parent mode object, sets its mode
397 * functions & device file and adds it to the master fd list.
398 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100399 * IMPORTANT:
400 * This functions publishes the fb and makes it available for concurrent access
401 * by other users. Which means by this point the fb _must_ be fully set up -
402 * since all the fb attributes are invariant over its lifetime, no further
403 * locking but only correct reference counting is required.
404 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100405 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200406 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800407 */
408int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
409 const struct drm_framebuffer_funcs *funcs)
410{
411 int ret;
412
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100413 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000414 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100415 INIT_LIST_HEAD(&fb->filp_head);
416 fb->dev = dev;
417 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000418
Dave Airlief453ba02008-11-07 14:05:41 -0800419 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200420 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100421 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800422
Dave Airlief453ba02008-11-07 14:05:41 -0800423 dev->mode_config.num_fb++;
424 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100425out:
426 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800427
428 return 0;
429}
430EXPORT_SYMBOL(drm_framebuffer_init);
431
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200432/* dev->mode_config.fb_lock must be held! */
433static void __drm_framebuffer_unregister(struct drm_device *dev,
434 struct drm_framebuffer *fb)
435{
436 mutex_lock(&dev->mode_config.idr_mutex);
437 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
438 mutex_unlock(&dev->mode_config.idr_mutex);
439
440 fb->base.id = 0;
441}
442
Rob Clarkf7eff602012-09-05 21:48:38 +0000443static void drm_framebuffer_free(struct kref *kref)
444{
445 struct drm_framebuffer *fb =
446 container_of(kref, struct drm_framebuffer, refcount);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200447 struct drm_device *dev = fb->dev;
448
449 /*
450 * The lookup idr holds a weak reference, which has not necessarily been
451 * removed at this point. Check for that.
452 */
453 mutex_lock(&dev->mode_config.fb_lock);
454 if (fb->base.id) {
455 /* Mark fb as reaped and drop idr ref. */
456 __drm_framebuffer_unregister(dev, fb);
457 }
458 mutex_unlock(&dev->mode_config.fb_lock);
459
Rob Clarkf7eff602012-09-05 21:48:38 +0000460 fb->funcs->destroy(fb);
461}
462
Daniel Vetter2b677e82012-12-10 21:16:05 +0100463static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
464 uint32_t id)
465{
466 struct drm_mode_object *obj = NULL;
467 struct drm_framebuffer *fb;
468
469 mutex_lock(&dev->mode_config.idr_mutex);
470 obj = idr_find(&dev->mode_config.crtc_idr, id);
471 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
472 fb = NULL;
473 else
474 fb = obj_to_fb(obj);
475 mutex_unlock(&dev->mode_config.idr_mutex);
476
477 return fb;
478}
479
Rob Clarkf7eff602012-09-05 21:48:38 +0000480/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100481 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
482 * @dev: drm device
483 * @id: id of the fb object
484 *
485 * If successful, this grabs an additional reference to the framebuffer -
486 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100487 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100488 */
489struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
490 uint32_t id)
491{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100492 struct drm_framebuffer *fb;
493
494 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100495 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200496 if (fb) {
497 if (!kref_get_unless_zero(&fb->refcount))
498 fb = NULL;
499 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100500 mutex_unlock(&dev->mode_config.fb_lock);
501
502 return fb;
503}
504EXPORT_SYMBOL(drm_framebuffer_lookup);
505
506/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000507 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100508 * @fb: framebuffer to unref
509 *
510 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000511 */
512void drm_framebuffer_unreference(struct drm_framebuffer *fb)
513{
Rob Clark82912722014-03-18 10:07:08 -0400514 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000515 kref_put(&fb->refcount, drm_framebuffer_free);
516}
517EXPORT_SYMBOL(drm_framebuffer_unreference);
518
519/**
520 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100521 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100522 *
523 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000524 */
525void drm_framebuffer_reference(struct drm_framebuffer *fb)
526{
Rob Clark82912722014-03-18 10:07:08 -0400527 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000528 kref_get(&fb->refcount);
529}
530EXPORT_SYMBOL(drm_framebuffer_reference);
531
Daniel Vetter2b677e82012-12-10 21:16:05 +0100532static void drm_framebuffer_free_bug(struct kref *kref)
533{
534 BUG();
535}
536
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100537static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
538{
Rob Clark82912722014-03-18 10:07:08 -0400539 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100540 kref_put(&fb->refcount, drm_framebuffer_free_bug);
541}
542
Dave Airlief453ba02008-11-07 14:05:41 -0800543/**
Daniel Vetter36206362012-12-10 20:42:17 +0100544 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
545 * @fb: fb to unregister
546 *
547 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
548 * those used for fbdev. Note that the caller must hold a reference of it's own,
549 * i.e. the object may not be destroyed through this call (since it'll lead to a
550 * locking inversion).
551 */
552void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
553{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100554 struct drm_device *dev = fb->dev;
555
556 mutex_lock(&dev->mode_config.fb_lock);
557 /* Mark fb as reaped and drop idr ref. */
558 __drm_framebuffer_unregister(dev, fb);
559 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100560}
561EXPORT_SYMBOL(drm_framebuffer_unregister_private);
562
563/**
Dave Airlief453ba02008-11-07 14:05:41 -0800564 * drm_framebuffer_cleanup - remove a framebuffer object
565 * @fb: framebuffer to remove
566 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100567 * Cleanup framebuffer. This function is intended to be used from the drivers
568 * ->destroy callback. It can also be used to clean up driver private
569 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100570 *
571 * Note that this function does not remove the fb from active usuage - if it is
572 * still used anywhere, hilarity can ensue since userspace could call getfb on
573 * the id and get back -EINVAL. Obviously no concern at driver unload time.
574 *
575 * Also, the framebuffer will not be removed from the lookup idr - for
576 * user-created framebuffers this will happen in in the rmfb ioctl. For
577 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
578 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800579 */
580void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
581{
582 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100583
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100584 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000585 list_del(&fb->head);
586 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100587 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000588}
589EXPORT_SYMBOL(drm_framebuffer_cleanup);
590
591/**
592 * drm_framebuffer_remove - remove and unreference a framebuffer object
593 * @fb: framebuffer to remove
594 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000595 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100596 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100597 * passed-in framebuffer. Might take the modeset locks.
598 *
599 * Note that this function optimizes the cleanup away if the caller holds the
600 * last reference to the framebuffer. It is also guaranteed to not take the
601 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000602 */
603void drm_framebuffer_remove(struct drm_framebuffer *fb)
604{
605 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800606 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800607 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000608 struct drm_mode_set set;
609 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800610
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100611 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100612
Daniel Vetterb62584e2012-12-11 16:51:35 +0100613 /*
614 * drm ABI mandates that we remove any deleted framebuffers from active
615 * useage. But since most sane clients only remove framebuffers they no
616 * longer need, try to optimize this away.
617 *
618 * Since we're holding a reference ourselves, observing a refcount of 1
619 * means that we're the last holder and can skip it. Also, the refcount
620 * can never increase from 1 again, so we don't need any barriers or
621 * locks.
622 *
623 * Note that userspace could try to race with use and instate a new
624 * usage _after_ we've cleared all current ones. End result will be an
625 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
626 * in this manner.
627 */
628 if (atomic_read(&fb->refcount.refcount) > 1) {
629 drm_modeset_lock_all(dev);
630 /* remove from any CRTC */
631 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700632 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100633 /* should turn off the crtc */
634 memset(&set, 0, sizeof(struct drm_mode_set));
635 set.crtc = crtc;
636 set.fb = NULL;
637 ret = drm_mode_set_config_internal(&set);
638 if (ret)
639 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
640 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000641 }
Dave Airlief453ba02008-11-07 14:05:41 -0800642
Daniel Vetterb62584e2012-12-11 16:51:35 +0100643 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300644 if (plane->fb == fb)
645 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800646 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100647 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800648 }
649
Rob Clarkf7eff602012-09-05 21:48:38 +0000650 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800651}
Rob Clarkf7eff602012-09-05 21:48:38 +0000652EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800653
Rob Clark51fd3712013-11-19 12:10:12 -0500654DEFINE_WW_CLASS(crtc_ww_class);
655
Dave Airlief453ba02008-11-07 14:05:41 -0800656/**
Matt Ropere13161a2014-04-01 15:22:38 -0700657 * drm_crtc_init_with_planes - Initialise a new CRTC object with
658 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800659 * @dev: DRM device
660 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700661 * @primary: Primary plane for CRTC
662 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800663 * @funcs: callbacks for the new CRTC
664 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000665 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200666 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100667 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200668 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800669 */
Matt Ropere13161a2014-04-01 15:22:38 -0700670int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
671 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700672 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700673 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800674{
Rob Clark51fd3712013-11-19 12:10:12 -0500675 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200676 int ret;
677
Dave Airlief453ba02008-11-07 14:05:41 -0800678 crtc->dev = dev;
679 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000680 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800681
Daniel Vetter84849902012-12-02 00:28:11 +0100682 drm_modeset_lock_all(dev);
Rob Clark51fd3712013-11-19 12:10:12 -0500683 drm_modeset_lock_init(&crtc->mutex);
684 /* dropped by _unlock_all(): */
685 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200686
687 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
688 if (ret)
689 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800690
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300691 crtc->base.properties = &crtc->properties;
692
Rob Clark51fd3712013-11-19 12:10:12 -0500693 list_add_tail(&crtc->head, &config->crtc_list);
694 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200695
Matt Ropere13161a2014-04-01 15:22:38 -0700696 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700697 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700698 if (primary)
699 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700700 if (cursor)
701 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700702
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200703 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100704 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200705
706 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800707}
Matt Ropere13161a2014-04-01 15:22:38 -0700708EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800709
710/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000711 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800712 * @crtc: CRTC to cleanup
713 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000714 * This function cleans up @crtc and removes it from the DRM mode setting
715 * core. Note that the function does *not* free the crtc structure itself,
716 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800717 */
718void drm_crtc_cleanup(struct drm_crtc *crtc)
719{
720 struct drm_device *dev = crtc->dev;
721
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000722 kfree(crtc->gamma_store);
723 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800724
Rob Clark51fd3712013-11-19 12:10:12 -0500725 drm_modeset_lock_fini(&crtc->mutex);
726
Dave Airlief453ba02008-11-07 14:05:41 -0800727 drm_mode_object_put(dev, &crtc->base);
728 list_del(&crtc->head);
729 dev->mode_config.num_crtc--;
730}
731EXPORT_SYMBOL(drm_crtc_cleanup);
732
733/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000734 * drm_crtc_index - find the index of a registered CRTC
735 * @crtc: CRTC to find index for
736 *
737 * Given a registered CRTC, return the index of that CRTC within a DRM
738 * device's list of CRTCs.
739 */
740unsigned int drm_crtc_index(struct drm_crtc *crtc)
741{
742 unsigned int index = 0;
743 struct drm_crtc *tmp;
744
745 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
746 if (tmp == crtc)
747 return index;
748
749 index++;
750 }
751
752 BUG();
753}
754EXPORT_SYMBOL(drm_crtc_index);
755
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100756/*
Dave Airlief453ba02008-11-07 14:05:41 -0800757 * drm_mode_remove - remove and free a mode
758 * @connector: connector list to modify
759 * @mode: mode to remove
760 *
Dave Airlief453ba02008-11-07 14:05:41 -0800761 * Remove @mode from @connector's mode list, then free it.
762 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100763static void drm_mode_remove(struct drm_connector *connector,
764 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800765{
766 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100767 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800768}
Dave Airlief453ba02008-11-07 14:05:41 -0800769
770/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200771 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
772 * @connector: connector to quwery
773 * @mode: returned mode
774 *
775 * The kernel supports per-connector configration of its consoles through
776 * use of the video= parameter. This function parses that option and
777 * extracts the user's specified mode (or enable/disable status) for a
778 * particular connector. This is typically only used during the early fbdev
779 * setup.
780 */
781static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
782{
783 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
784 char *option = NULL;
785
786 if (fb_get_options(connector->name, &option))
787 return;
788
789 if (!drm_mode_parse_command_line_for_connector(option,
790 connector,
791 mode))
792 return;
793
794 if (mode->force) {
795 const char *s;
796
797 switch (mode->force) {
798 case DRM_FORCE_OFF:
799 s = "OFF";
800 break;
801 case DRM_FORCE_ON_DIGITAL:
802 s = "ON - dig";
803 break;
804 default:
805 case DRM_FORCE_ON:
806 s = "ON";
807 break;
808 }
809
810 DRM_INFO("forcing %s connector %s\n", connector->name, s);
811 connector->force = mode->force;
812 }
813
814 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
815 connector->name,
816 mode->xres, mode->yres,
817 mode->refresh_specified ? mode->refresh : 60,
818 mode->rb ? " reduced blanking" : "",
819 mode->margins ? " with margins" : "",
820 mode->interlace ? " interlaced" : "");
821}
822
823/**
Dave Airlief453ba02008-11-07 14:05:41 -0800824 * drm_connector_init - Init a preallocated connector
825 * @dev: DRM device
826 * @connector: the connector to init
827 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100828 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800829 *
Dave Airlief453ba02008-11-07 14:05:41 -0800830 * Initialises a preallocated connector. Connectors should be
831 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200832 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100833 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200834 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800835 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200836int drm_connector_init(struct drm_device *dev,
837 struct drm_connector *connector,
838 const struct drm_connector_funcs *funcs,
839 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800840{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200841 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400842 struct ida *connector_ida =
843 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200844
Daniel Vetter84849902012-12-02 00:28:11 +0100845 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800846
Dave Airlie2ee39452014-07-24 11:53:45 +1000847 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200848 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300849 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200850
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300851 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800852 connector->dev = dev;
853 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800854 connector->connector_type = connector_type;
855 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400856 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
857 if (connector->connector_type_id < 0) {
858 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300859 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400860 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300861 connector->name =
862 kasprintf(GFP_KERNEL, "%s-%d",
863 drm_connector_enum_list[connector_type].name,
864 connector->connector_type_id);
865 if (!connector->name) {
866 ret = -ENOMEM;
867 goto out_put;
868 }
869
Dave Airlief453ba02008-11-07 14:05:41 -0800870 INIT_LIST_HEAD(&connector->probed_modes);
871 INIT_LIST_HEAD(&connector->modes);
872 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000873 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800874
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200875 drm_connector_get_cmdline_mode(connector);
876
Dave Airlief453ba02008-11-07 14:05:41 -0800877 list_add_tail(&connector->head, &dev->mode_config.connector_list);
878 dev->mode_config.num_connector++;
879
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200880 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500881 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200882 dev->mode_config.edid_property,
883 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800884
Rob Clark58495562012-10-11 20:50:56 -0500885 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800886 dev->mode_config.dpms_property, 0);
887
Thomas Wood30f65702014-06-18 17:52:32 +0100888 connector->debugfs_entry = NULL;
889
Jani Nikula2abdd312014-05-14 16:58:19 +0300890out_put:
891 if (ret)
892 drm_mode_object_put(dev, &connector->base);
893
894out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100895 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200896
897 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800898}
899EXPORT_SYMBOL(drm_connector_init);
900
901/**
902 * drm_connector_cleanup - cleans up an initialised connector
903 * @connector: connector to cleanup
904 *
Dave Airlief453ba02008-11-07 14:05:41 -0800905 * Cleans up the connector but doesn't free the object.
906 */
907void drm_connector_cleanup(struct drm_connector *connector)
908{
909 struct drm_device *dev = connector->dev;
910 struct drm_display_mode *mode, *t;
911
912 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
913 drm_mode_remove(connector, mode);
914
915 list_for_each_entry_safe(mode, t, &connector->modes, head)
916 drm_mode_remove(connector, mode);
917
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400918 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
919 connector->connector_type_id);
920
Dave Airlief453ba02008-11-07 14:05:41 -0800921 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300922 kfree(connector->name);
923 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800924 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000925 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800926}
927EXPORT_SYMBOL(drm_connector_cleanup);
928
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100929/**
Daniel Vetter10f637b2014-07-29 13:47:11 +0200930 * drm_connector_index - find the index of a registered connector
931 * @connector: connector to find index for
932 *
933 * Given a registered connector, return the index of that connector within a DRM
934 * device's list of connectors.
935 */
936unsigned int drm_connector_index(struct drm_connector *connector)
937{
938 unsigned int index = 0;
939 struct drm_connector *tmp;
940
941 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
942 if (tmp == connector)
943 return index;
944
945 index++;
946 }
947
948 BUG();
949}
950EXPORT_SYMBOL(drm_connector_index);
951
952/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100953 * drm_connector_register - register a connector
954 * @connector: the connector to register
955 *
956 * Register userspace interfaces for a connector
957 *
958 * Returns:
959 * Zero on success, error code on failure.
960 */
961int drm_connector_register(struct drm_connector *connector)
962{
Thomas Wood30f65702014-06-18 17:52:32 +0100963 int ret;
964
Dave Airlie2ee39452014-07-24 11:53:45 +1000965 drm_mode_object_register(connector->dev, &connector->base);
966
Thomas Wood30f65702014-06-18 17:52:32 +0100967 ret = drm_sysfs_connector_add(connector);
968 if (ret)
969 return ret;
970
971 ret = drm_debugfs_connector_add(connector);
972 if (ret) {
973 drm_sysfs_connector_remove(connector);
974 return ret;
975 }
976
977 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100978}
979EXPORT_SYMBOL(drm_connector_register);
980
981/**
982 * drm_connector_unregister - unregister a connector
983 * @connector: the connector to unregister
984 *
985 * Unregister userspace interfaces for a connector
986 */
987void drm_connector_unregister(struct drm_connector *connector)
988{
989 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +0100990 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100991}
992EXPORT_SYMBOL(drm_connector_unregister);
993
994
995/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100996 * drm_connector_unplug_all - unregister connector userspace interfaces
997 * @dev: drm device
998 *
999 * This function unregisters all connector userspace interfaces in sysfs. Should
1000 * be call when the device is disconnected, e.g. from an usb driver's
1001 * ->disconnect callback.
1002 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001003void drm_connector_unplug_all(struct drm_device *dev)
1004{
1005 struct drm_connector *connector;
1006
1007 /* taking the mode config mutex ends up in a clash with sysfs */
1008 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001009 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001010
1011}
1012EXPORT_SYMBOL(drm_connector_unplug_all);
1013
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001014/**
1015 * drm_bridge_init - initialize a drm transcoder/bridge
1016 * @dev: drm device
1017 * @bridge: transcoder/bridge to set up
1018 * @funcs: bridge function table
1019 *
1020 * Initialises a preallocated bridge. Bridges should be
1021 * subclassed as part of driver connector objects.
1022 *
1023 * Returns:
1024 * Zero on success, error code on failure.
1025 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001026int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1027 const struct drm_bridge_funcs *funcs)
1028{
1029 int ret;
1030
1031 drm_modeset_lock_all(dev);
1032
1033 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1034 if (ret)
1035 goto out;
1036
1037 bridge->dev = dev;
1038 bridge->funcs = funcs;
1039
1040 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1041 dev->mode_config.num_bridge++;
1042
1043 out:
1044 drm_modeset_unlock_all(dev);
1045 return ret;
1046}
1047EXPORT_SYMBOL(drm_bridge_init);
1048
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001049/**
1050 * drm_bridge_cleanup - cleans up an initialised bridge
1051 * @bridge: bridge to cleanup
1052 *
1053 * Cleans up the bridge but doesn't free the object.
1054 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001055void drm_bridge_cleanup(struct drm_bridge *bridge)
1056{
1057 struct drm_device *dev = bridge->dev;
1058
1059 drm_modeset_lock_all(dev);
1060 drm_mode_object_put(dev, &bridge->base);
1061 list_del(&bridge->head);
1062 dev->mode_config.num_bridge--;
1063 drm_modeset_unlock_all(dev);
1064}
1065EXPORT_SYMBOL(drm_bridge_cleanup);
1066
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001067/**
1068 * drm_encoder_init - Init a preallocated encoder
1069 * @dev: drm device
1070 * @encoder: the encoder to init
1071 * @funcs: callbacks for this encoder
1072 * @encoder_type: user visible type of the encoder
1073 *
1074 * Initialises a preallocated encoder. Encoder should be
1075 * subclassed as part of driver encoder objects.
1076 *
1077 * Returns:
1078 * Zero on success, error code on failure.
1079 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001080int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001081 struct drm_encoder *encoder,
1082 const struct drm_encoder_funcs *funcs,
1083 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001084{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001085 int ret;
1086
Daniel Vetter84849902012-12-02 00:28:11 +01001087 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001088
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001089 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1090 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001091 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001092
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001093 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001094 encoder->encoder_type = encoder_type;
1095 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001096 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1097 drm_encoder_enum_list[encoder_type].name,
1098 encoder->base.id);
1099 if (!encoder->name) {
1100 ret = -ENOMEM;
1101 goto out_put;
1102 }
Dave Airlief453ba02008-11-07 14:05:41 -08001103
1104 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1105 dev->mode_config.num_encoder++;
1106
Jani Nikulae5748942014-05-14 16:58:20 +03001107out_put:
1108 if (ret)
1109 drm_mode_object_put(dev, &encoder->base);
1110
1111out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001112 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001113
1114 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001115}
1116EXPORT_SYMBOL(drm_encoder_init);
1117
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001118/**
1119 * drm_encoder_cleanup - cleans up an initialised encoder
1120 * @encoder: encoder to cleanup
1121 *
1122 * Cleans up the encoder but doesn't free the object.
1123 */
Dave Airlief453ba02008-11-07 14:05:41 -08001124void drm_encoder_cleanup(struct drm_encoder *encoder)
1125{
1126 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +01001127 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001128 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001129 kfree(encoder->name);
1130 encoder->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001131 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001132 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001133 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001134}
1135EXPORT_SYMBOL(drm_encoder_cleanup);
1136
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001137/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001138 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001139 * @dev: DRM device
1140 * @plane: plane object to init
1141 * @possible_crtcs: bitmask of possible CRTCs
1142 * @funcs: callbacks for the new plane
1143 * @formats: array of supported formats (%DRM_FORMAT_*)
1144 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001145 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001146 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001147 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001148 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001149 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001150 * Zero on success, error code on failure.
1151 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001152int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1153 unsigned long possible_crtcs,
1154 const struct drm_plane_funcs *funcs,
1155 const uint32_t *formats, uint32_t format_count,
1156 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001157{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001158 int ret;
1159
Daniel Vetter84849902012-12-02 00:28:11 +01001160 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001161
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001162 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1163 if (ret)
1164 goto out;
1165
Rob Clark4d939142012-05-17 02:23:27 -06001166 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001167 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001168 plane->funcs = funcs;
1169 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1170 GFP_KERNEL);
1171 if (!plane->format_types) {
1172 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1173 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001174 ret = -ENOMEM;
1175 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001176 }
1177
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001178 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001179 plane->format_count = format_count;
1180 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001181 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001182
Matt Roperdc415ff2014-04-01 15:22:36 -07001183 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1184 dev->mode_config.num_total_plane++;
1185 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1186 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001187
Rob Clark9922ab52014-04-01 20:16:57 -04001188 drm_object_attach_property(&plane->base,
1189 dev->mode_config.plane_type_property,
1190 plane->type);
1191
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001192 out:
Daniel Vetter84849902012-12-02 00:28:11 +01001193 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001194
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001195 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001196}
Matt Roperdc415ff2014-04-01 15:22:36 -07001197EXPORT_SYMBOL(drm_universal_plane_init);
1198
1199/**
1200 * drm_plane_init - Initialize a legacy plane
1201 * @dev: DRM device
1202 * @plane: plane object to init
1203 * @possible_crtcs: bitmask of possible CRTCs
1204 * @funcs: callbacks for the new plane
1205 * @formats: array of supported formats (%DRM_FORMAT_*)
1206 * @format_count: number of elements in @formats
1207 * @is_primary: plane type (primary vs overlay)
1208 *
1209 * Legacy API to initialize a DRM plane.
1210 *
1211 * New drivers should call drm_universal_plane_init() instead.
1212 *
1213 * Returns:
1214 * Zero on success, error code on failure.
1215 */
1216int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1217 unsigned long possible_crtcs,
1218 const struct drm_plane_funcs *funcs,
1219 const uint32_t *formats, uint32_t format_count,
1220 bool is_primary)
1221{
1222 enum drm_plane_type type;
1223
1224 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1225 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1226 formats, format_count, type);
1227}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001228EXPORT_SYMBOL(drm_plane_init);
1229
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001230/**
1231 * drm_plane_cleanup - Clean up the core plane usage
1232 * @plane: plane to cleanup
1233 *
1234 * This function cleans up @plane and removes it from the DRM mode setting
1235 * core. Note that the function does *not* free the plane structure itself,
1236 * this is the responsibility of the caller.
1237 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001238void drm_plane_cleanup(struct drm_plane *plane)
1239{
1240 struct drm_device *dev = plane->dev;
1241
Daniel Vetter84849902012-12-02 00:28:11 +01001242 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001243 kfree(plane->format_types);
1244 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001245
1246 BUG_ON(list_empty(&plane->head));
1247
1248 list_del(&plane->head);
1249 dev->mode_config.num_total_plane--;
1250 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1251 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001252 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001253}
1254EXPORT_SYMBOL(drm_plane_cleanup);
1255
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001256/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001257 * drm_plane_index - find the index of a registered plane
1258 * @plane: plane to find index for
1259 *
1260 * Given a registered plane, return the index of that CRTC within a DRM
1261 * device's list of planes.
1262 */
1263unsigned int drm_plane_index(struct drm_plane *plane)
1264{
1265 unsigned int index = 0;
1266 struct drm_plane *tmp;
1267
1268 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1269 if (tmp == plane)
1270 return index;
1271
1272 index++;
1273 }
1274
1275 BUG();
1276}
1277EXPORT_SYMBOL(drm_plane_index);
1278
1279/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001280 * drm_plane_force_disable - Forcibly disable a plane
1281 * @plane: plane to disable
1282 *
1283 * Forces the plane to be disabled.
1284 *
1285 * Used when the plane's current framebuffer is destroyed,
1286 * and when restoring fbdev mode.
1287 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001288void drm_plane_force_disable(struct drm_plane *plane)
1289{
1290 int ret;
1291
Daniel Vetter3d30a592014-07-27 13:42:42 +02001292 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001293 return;
1294
Daniel Vetter3d30a592014-07-27 13:42:42 +02001295 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001296 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001297 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001298 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001299 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001300 return;
1301 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001302 /* disconnect the plane from the fb and crtc: */
Daniel Vetter3d30a592014-07-27 13:42:42 +02001303 __drm_framebuffer_unreference(plane->old_fb);
1304 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001305 plane->fb = NULL;
1306 plane->crtc = NULL;
1307}
1308EXPORT_SYMBOL(drm_plane_force_disable);
1309
Dave Airlief453ba02008-11-07 14:05:41 -08001310static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1311{
1312 struct drm_property *edid;
1313 struct drm_property *dpms;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001314 struct drm_property *dev_path;
Dave Airlief453ba02008-11-07 14:05:41 -08001315
1316 /*
1317 * Standard properties (apply to all connectors)
1318 */
1319 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1320 DRM_MODE_PROP_IMMUTABLE,
1321 "EDID", 0);
1322 dev->mode_config.edid_property = edid;
1323
Sascha Hauer4a67d392012-02-06 10:58:17 +01001324 dpms = drm_property_create_enum(dev, 0,
1325 "DPMS", drm_dpms_enum_list,
1326 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001327 dev->mode_config.dpms_property = dpms;
1328
Dave Airlie43aba7e2014-06-05 14:01:31 +10001329 dev_path = drm_property_create(dev,
1330 DRM_MODE_PROP_BLOB |
1331 DRM_MODE_PROP_IMMUTABLE,
1332 "PATH", 0);
1333 dev->mode_config.path_property = dev_path;
1334
Dave Airlief453ba02008-11-07 14:05:41 -08001335 return 0;
1336}
1337
Rob Clark9922ab52014-04-01 20:16:57 -04001338static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1339{
1340 struct drm_property *type;
1341
1342 /*
1343 * Standard properties (apply to all planes)
1344 */
1345 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1346 "type", drm_plane_type_enum_list,
1347 ARRAY_SIZE(drm_plane_type_enum_list));
1348 dev->mode_config.plane_type_property = type;
1349
1350 return 0;
1351}
1352
Dave Airlief453ba02008-11-07 14:05:41 -08001353/**
1354 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1355 * @dev: DRM device
1356 *
1357 * Called by a driver the first time a DVI-I connector is made.
1358 */
1359int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1360{
1361 struct drm_property *dvi_i_selector;
1362 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001363
1364 if (dev->mode_config.dvi_i_select_subconnector_property)
1365 return 0;
1366
1367 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001368 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001369 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001370 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001371 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001372 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1373
Sascha Hauer4a67d392012-02-06 10:58:17 +01001374 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001375 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001376 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001377 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001378 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1379
1380 return 0;
1381}
1382EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1383
1384/**
1385 * drm_create_tv_properties - create TV specific connector properties
1386 * @dev: DRM device
1387 * @num_modes: number of different TV formats (modes) supported
1388 * @modes: array of pointers to strings containing name of each format
1389 *
1390 * Called by a driver's TV initialization routine, this function creates
1391 * the TV specific connector properties for a given device. Caller is
1392 * responsible for allocating a list of format names and passing them to
1393 * this routine.
1394 */
1395int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1396 char *modes[])
1397{
1398 struct drm_property *tv_selector;
1399 struct drm_property *tv_subconnector;
1400 int i;
1401
1402 if (dev->mode_config.tv_select_subconnector_property)
1403 return 0;
1404
1405 /*
1406 * Basic connector properties
1407 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001408 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001409 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001410 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001411 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001412 dev->mode_config.tv_select_subconnector_property = tv_selector;
1413
1414 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001415 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1416 "subconnector",
1417 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001418 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001419 dev->mode_config.tv_subconnector_property = tv_subconnector;
1420
1421 /*
1422 * Other, TV specific properties: margins & TV modes.
1423 */
1424 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001425 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001426
1427 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001428 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001429
1430 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001431 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001432
1433 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001434 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001435
1436 dev->mode_config.tv_mode_property =
1437 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1438 "mode", num_modes);
1439 for (i = 0; i < num_modes; i++)
1440 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1441 i, modes[i]);
1442
Francisco Jerezb6b79022009-08-02 04:19:20 +02001443 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001444 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001445
1446 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001447 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001448
1449 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001450 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001451
Francisco Jereza75f0232009-08-12 02:30:10 +02001452 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001453 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001454
1455 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001456 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001457
1458 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001459 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001460
Dave Airlief453ba02008-11-07 14:05:41 -08001461 return 0;
1462}
1463EXPORT_SYMBOL(drm_mode_create_tv_properties);
1464
1465/**
1466 * drm_mode_create_scaling_mode_property - create scaling mode property
1467 * @dev: DRM device
1468 *
1469 * Called by a driver the first time it's needed, must be attached to desired
1470 * connectors.
1471 */
1472int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1473{
1474 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001475
1476 if (dev->mode_config.scaling_mode_property)
1477 return 0;
1478
1479 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001480 drm_property_create_enum(dev, 0, "scaling mode",
1481 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001482 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001483
1484 dev->mode_config.scaling_mode_property = scaling_mode;
1485
1486 return 0;
1487}
1488EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1489
1490/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301491 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1492 * @dev: DRM device
1493 *
1494 * Called by a driver the first time it's needed, must be attached to desired
1495 * connectors.
1496 *
1497 * Returns:
1498 * Zero on success, errno on failure.
1499 */
1500int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1501{
1502 if (dev->mode_config.aspect_ratio_property)
1503 return 0;
1504
1505 dev->mode_config.aspect_ratio_property =
1506 drm_property_create_enum(dev, 0, "aspect ratio",
1507 drm_aspect_ratio_enum_list,
1508 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1509
1510 if (dev->mode_config.aspect_ratio_property == NULL)
1511 return -ENOMEM;
1512
1513 return 0;
1514}
1515EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1516
1517/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001518 * drm_mode_create_dirty_property - create dirty property
1519 * @dev: DRM device
1520 *
1521 * Called by a driver the first time it's needed, must be attached to desired
1522 * connectors.
1523 */
1524int drm_mode_create_dirty_info_property(struct drm_device *dev)
1525{
1526 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001527
1528 if (dev->mode_config.dirty_info_property)
1529 return 0;
1530
1531 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001532 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001533 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001534 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001535 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001536 dev->mode_config.dirty_info_property = dirty_info;
1537
1538 return 0;
1539}
1540EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1541
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001542static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001543{
1544 uint32_t total_objects = 0;
1545
1546 total_objects += dev->mode_config.num_crtc;
1547 total_objects += dev->mode_config.num_connector;
1548 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001549 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001550
Dave Airlief453ba02008-11-07 14:05:41 -08001551 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1552 if (!group->id_list)
1553 return -ENOMEM;
1554
1555 group->num_crtcs = 0;
1556 group->num_connectors = 0;
1557 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001558 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001559 return 0;
1560}
1561
Dave Airliead222792014-05-02 13:22:19 +10001562void drm_mode_group_destroy(struct drm_mode_group *group)
1563{
1564 kfree(group->id_list);
1565 group->id_list = NULL;
1566}
1567
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001568/*
1569 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1570 * the drm core's responsibility to set up mode control groups.
1571 */
Dave Airlief453ba02008-11-07 14:05:41 -08001572int drm_mode_group_init_legacy_group(struct drm_device *dev,
1573 struct drm_mode_group *group)
1574{
1575 struct drm_crtc *crtc;
1576 struct drm_encoder *encoder;
1577 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001578 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001579 int ret;
1580
1581 if ((ret = drm_mode_group_init(dev, group)))
1582 return ret;
1583
1584 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1585 group->id_list[group->num_crtcs++] = crtc->base.id;
1586
1587 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1588 group->id_list[group->num_crtcs + group->num_encoders++] =
1589 encoder->base.id;
1590
1591 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1592 group->id_list[group->num_crtcs + group->num_encoders +
1593 group->num_connectors++] = connector->base.id;
1594
Sean Paul3b336ec2013-08-14 16:47:37 -04001595 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1596 group->id_list[group->num_crtcs + group->num_encoders +
1597 group->num_connectors + group->num_bridges++] =
1598 bridge->base.id;
1599
Dave Airlief453ba02008-11-07 14:05:41 -08001600 return 0;
1601}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001602EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001603
Dave Airlie2390cd12014-06-05 14:01:29 +10001604void drm_reinit_primary_mode_group(struct drm_device *dev)
1605{
1606 drm_modeset_lock_all(dev);
1607 drm_mode_group_destroy(&dev->primary->mode_group);
1608 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1609 drm_modeset_unlock_all(dev);
1610}
1611EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1612
Dave Airlief453ba02008-11-07 14:05:41 -08001613/**
Dave Airlief453ba02008-11-07 14:05:41 -08001614 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1615 * @out: drm_mode_modeinfo struct to return to the user
1616 * @in: drm_display_mode to use
1617 *
Dave Airlief453ba02008-11-07 14:05:41 -08001618 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1619 * the user.
1620 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001621static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1622 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001623{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001624 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1625 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1626 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1627 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1628 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1629 "timing values too large for mode info\n");
1630
Dave Airlief453ba02008-11-07 14:05:41 -08001631 out->clock = in->clock;
1632 out->hdisplay = in->hdisplay;
1633 out->hsync_start = in->hsync_start;
1634 out->hsync_end = in->hsync_end;
1635 out->htotal = in->htotal;
1636 out->hskew = in->hskew;
1637 out->vdisplay = in->vdisplay;
1638 out->vsync_start = in->vsync_start;
1639 out->vsync_end = in->vsync_end;
1640 out->vtotal = in->vtotal;
1641 out->vscan = in->vscan;
1642 out->vrefresh = in->vrefresh;
1643 out->flags = in->flags;
1644 out->type = in->type;
1645 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1646 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1647}
1648
1649/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001650 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001651 * @out: drm_display_mode to return to the user
1652 * @in: drm_mode_modeinfo to use
1653 *
Dave Airlief453ba02008-11-07 14:05:41 -08001654 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1655 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001656 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001657 * Returns:
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001658 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001659 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001660static int drm_crtc_convert_umode(struct drm_display_mode *out,
1661 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001662{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001663 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1664 return -ERANGE;
1665
Damien Lespiau5848ad42013-09-27 12:11:50 +01001666 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1667 return -EINVAL;
1668
Dave Airlief453ba02008-11-07 14:05:41 -08001669 out->clock = in->clock;
1670 out->hdisplay = in->hdisplay;
1671 out->hsync_start = in->hsync_start;
1672 out->hsync_end = in->hsync_end;
1673 out->htotal = in->htotal;
1674 out->hskew = in->hskew;
1675 out->vdisplay = in->vdisplay;
1676 out->vsync_start = in->vsync_start;
1677 out->vsync_end = in->vsync_end;
1678 out->vtotal = in->vtotal;
1679 out->vscan = in->vscan;
1680 out->vrefresh = in->vrefresh;
1681 out->flags = in->flags;
1682 out->type = in->type;
1683 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1684 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001685
1686 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001687}
1688
1689/**
1690 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001691 * @dev: drm device for the ioctl
1692 * @data: data pointer for the ioctl
1693 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001694 *
Dave Airlief453ba02008-11-07 14:05:41 -08001695 * Construct a set of configuration description structures and return
1696 * them to the user, including CRTC, connector and framebuffer configuration.
1697 *
1698 * Called by the user via ioctl.
1699 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001700 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001701 * Zero on success, errno on failure.
1702 */
1703int drm_mode_getresources(struct drm_device *dev, void *data,
1704 struct drm_file *file_priv)
1705{
1706 struct drm_mode_card_res *card_res = data;
1707 struct list_head *lh;
1708 struct drm_framebuffer *fb;
1709 struct drm_connector *connector;
1710 struct drm_crtc *crtc;
1711 struct drm_encoder *encoder;
1712 int ret = 0;
1713 int connector_count = 0;
1714 int crtc_count = 0;
1715 int fb_count = 0;
1716 int encoder_count = 0;
1717 int copied = 0, i;
1718 uint32_t __user *fb_id;
1719 uint32_t __user *crtc_id;
1720 uint32_t __user *connector_id;
1721 uint32_t __user *encoder_id;
1722 struct drm_mode_group *mode_group;
1723
Dave Airliefb3b06c2011-02-08 13:55:21 +10001724 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1725 return -EINVAL;
1726
Dave Airlief453ba02008-11-07 14:05:41 -08001727
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001728 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001729 /*
1730 * For the non-control nodes we need to limit the list of resources
1731 * by IDs in the group list for this node
1732 */
1733 list_for_each(lh, &file_priv->fbs)
1734 fb_count++;
1735
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001736 /* handle this in 4 parts */
1737 /* FBs */
1738 if (card_res->count_fbs >= fb_count) {
1739 copied = 0;
1740 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1741 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1742 if (put_user(fb->base.id, fb_id + copied)) {
1743 mutex_unlock(&file_priv->fbs_lock);
1744 return -EFAULT;
1745 }
1746 copied++;
1747 }
1748 }
1749 card_res->count_fbs = fb_count;
1750 mutex_unlock(&file_priv->fbs_lock);
1751
1752 drm_modeset_lock_all(dev);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001753 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001754
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001755 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001756 list_for_each(lh, &dev->mode_config.crtc_list)
1757 crtc_count++;
1758
1759 list_for_each(lh, &dev->mode_config.connector_list)
1760 connector_count++;
1761
1762 list_for_each(lh, &dev->mode_config.encoder_list)
1763 encoder_count++;
1764 } else {
1765
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001766 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001767 crtc_count = mode_group->num_crtcs;
1768 connector_count = mode_group->num_connectors;
1769 encoder_count = mode_group->num_encoders;
1770 }
1771
1772 card_res->max_height = dev->mode_config.max_height;
1773 card_res->min_height = dev->mode_config.min_height;
1774 card_res->max_width = dev->mode_config.max_width;
1775 card_res->min_width = dev->mode_config.min_width;
1776
Dave Airlief453ba02008-11-07 14:05:41 -08001777 /* CRTCs */
1778 if (card_res->count_crtcs >= crtc_count) {
1779 copied = 0;
1780 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001781 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001782 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1783 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001784 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001785 if (put_user(crtc->base.id, crtc_id + copied)) {
1786 ret = -EFAULT;
1787 goto out;
1788 }
1789 copied++;
1790 }
1791 } else {
1792 for (i = 0; i < mode_group->num_crtcs; i++) {
1793 if (put_user(mode_group->id_list[i],
1794 crtc_id + copied)) {
1795 ret = -EFAULT;
1796 goto out;
1797 }
1798 copied++;
1799 }
1800 }
1801 }
1802 card_res->count_crtcs = crtc_count;
1803
1804 /* Encoders */
1805 if (card_res->count_encoders >= encoder_count) {
1806 copied = 0;
1807 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001808 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001809 list_for_each_entry(encoder,
1810 &dev->mode_config.encoder_list,
1811 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001812 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001813 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001814 if (put_user(encoder->base.id, encoder_id +
1815 copied)) {
1816 ret = -EFAULT;
1817 goto out;
1818 }
1819 copied++;
1820 }
1821 } else {
1822 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1823 if (put_user(mode_group->id_list[i],
1824 encoder_id + copied)) {
1825 ret = -EFAULT;
1826 goto out;
1827 }
1828 copied++;
1829 }
1830
1831 }
1832 }
1833 card_res->count_encoders = encoder_count;
1834
1835 /* Connectors */
1836 if (card_res->count_connectors >= connector_count) {
1837 copied = 0;
1838 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001839 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001840 list_for_each_entry(connector,
1841 &dev->mode_config.connector_list,
1842 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001843 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1844 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001845 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001846 if (put_user(connector->base.id,
1847 connector_id + copied)) {
1848 ret = -EFAULT;
1849 goto out;
1850 }
1851 copied++;
1852 }
1853 } else {
1854 int start = mode_group->num_crtcs +
1855 mode_group->num_encoders;
1856 for (i = start; i < start + mode_group->num_connectors; i++) {
1857 if (put_user(mode_group->id_list[i],
1858 connector_id + copied)) {
1859 ret = -EFAULT;
1860 goto out;
1861 }
1862 copied++;
1863 }
1864 }
1865 }
1866 card_res->count_connectors = connector_count;
1867
Jerome Glisse94401062010-07-15 15:43:25 -04001868 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001869 card_res->count_connectors, card_res->count_encoders);
1870
1871out:
Daniel Vetter84849902012-12-02 00:28:11 +01001872 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001873 return ret;
1874}
1875
1876/**
1877 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001878 * @dev: drm device for the ioctl
1879 * @data: data pointer for the ioctl
1880 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001881 *
Dave Airlief453ba02008-11-07 14:05:41 -08001882 * Construct a CRTC configuration structure to return to the user.
1883 *
1884 * Called by the user via ioctl.
1885 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001886 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001887 * Zero on success, errno on failure.
1888 */
1889int drm_mode_getcrtc(struct drm_device *dev,
1890 void *data, struct drm_file *file_priv)
1891{
1892 struct drm_mode_crtc *crtc_resp = data;
1893 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001894 int ret = 0;
1895
Dave Airliefb3b06c2011-02-08 13:55:21 +10001896 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1897 return -EINVAL;
1898
Daniel Vetter84849902012-12-02 00:28:11 +01001899 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001900
Rob Clarka2b34e22013-10-05 16:36:52 -04001901 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1902 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001903 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001904 goto out;
1905 }
Dave Airlief453ba02008-11-07 14:05:41 -08001906
1907 crtc_resp->x = crtc->x;
1908 crtc_resp->y = crtc->y;
1909 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001910 if (crtc->primary->fb)
1911 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001912 else
1913 crtc_resp->fb_id = 0;
1914
1915 if (crtc->enabled) {
1916
1917 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1918 crtc_resp->mode_valid = 1;
1919
1920 } else {
1921 crtc_resp->mode_valid = 0;
1922 }
1923
1924out:
Daniel Vetter84849902012-12-02 00:28:11 +01001925 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001926 return ret;
1927}
1928
Damien Lespiau61d8e322013-09-25 16:45:22 +01001929static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1930 const struct drm_file *file_priv)
1931{
1932 /*
1933 * If user-space hasn't configured the driver to expose the stereo 3D
1934 * modes, don't expose them.
1935 */
1936 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1937 return false;
1938
1939 return true;
1940}
1941
Dave Airlief453ba02008-11-07 14:05:41 -08001942/**
1943 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001944 * @dev: drm device for the ioctl
1945 * @data: data pointer for the ioctl
1946 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001947 *
Dave Airlief453ba02008-11-07 14:05:41 -08001948 * Construct a connector configuration structure to return to the user.
1949 *
1950 * Called by the user via ioctl.
1951 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001952 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001953 * Zero on success, errno on failure.
1954 */
1955int drm_mode_getconnector(struct drm_device *dev, void *data,
1956 struct drm_file *file_priv)
1957{
1958 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001959 struct drm_connector *connector;
1960 struct drm_display_mode *mode;
1961 int mode_count = 0;
1962 int props_count = 0;
1963 int encoders_count = 0;
1964 int ret = 0;
1965 int copied = 0;
1966 int i;
1967 struct drm_mode_modeinfo u_mode;
1968 struct drm_mode_modeinfo __user *mode_ptr;
1969 uint32_t __user *prop_ptr;
1970 uint64_t __user *prop_values;
1971 uint32_t __user *encoder_ptr;
1972
Dave Airliefb3b06c2011-02-08 13:55:21 +10001973 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1974 return -EINVAL;
1975
Dave Airlief453ba02008-11-07 14:05:41 -08001976 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1977
Jerome Glisse94401062010-07-15 15:43:25 -04001978 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001979
Daniel Vetter7b240562012-12-12 00:35:33 +01001980 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001981
Rob Clarka2b34e22013-10-05 16:36:52 -04001982 connector = drm_connector_find(dev, out_resp->connector_id);
1983 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001984 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001985 goto out;
1986 }
Dave Airlief453ba02008-11-07 14:05:41 -08001987
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001988 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001989
1990 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1991 if (connector->encoder_ids[i] != 0) {
1992 encoders_count++;
1993 }
1994 }
1995
1996 if (out_resp->count_modes == 0) {
1997 connector->funcs->fill_modes(connector,
1998 dev->mode_config.max_width,
1999 dev->mode_config.max_height);
2000 }
2001
2002 /* delayed so we get modes regardless of pre-fill_modes state */
2003 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002004 if (drm_mode_expose_to_userspace(mode, file_priv))
2005 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002006
2007 out_resp->connector_id = connector->base.id;
2008 out_resp->connector_type = connector->connector_type;
2009 out_resp->connector_type_id = connector->connector_type_id;
2010 out_resp->mm_width = connector->display_info.width_mm;
2011 out_resp->mm_height = connector->display_info.height_mm;
2012 out_resp->subpixel = connector->display_info.subpixel_order;
2013 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02002014 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002015 if (connector->encoder)
2016 out_resp->encoder_id = connector->encoder->base.id;
2017 else
2018 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02002019 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002020
2021 /*
2022 * This ioctl is called twice, once to determine how much space is
2023 * needed, and the 2nd time to fill it.
2024 */
2025 if ((out_resp->count_modes >= mode_count) && mode_count) {
2026 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002027 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002028 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002029 if (!drm_mode_expose_to_userspace(mode, file_priv))
2030 continue;
2031
Dave Airlief453ba02008-11-07 14:05:41 -08002032 drm_crtc_convert_to_umode(&u_mode, mode);
2033 if (copy_to_user(mode_ptr + copied,
2034 &u_mode, sizeof(u_mode))) {
2035 ret = -EFAULT;
2036 goto out;
2037 }
2038 copied++;
2039 }
2040 }
2041 out_resp->count_modes = mode_count;
2042
2043 if ((out_resp->count_props >= props_count) && props_count) {
2044 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002045 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2046 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002047 for (i = 0; i < connector->properties.count; i++) {
2048 if (put_user(connector->properties.ids[i],
2049 prop_ptr + copied)) {
2050 ret = -EFAULT;
2051 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002052 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002053
2054 if (put_user(connector->properties.values[i],
2055 prop_values + copied)) {
2056 ret = -EFAULT;
2057 goto out;
2058 }
2059 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002060 }
2061 }
2062 out_resp->count_props = props_count;
2063
2064 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2065 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002066 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002067 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2068 if (connector->encoder_ids[i] != 0) {
2069 if (put_user(connector->encoder_ids[i],
2070 encoder_ptr + copied)) {
2071 ret = -EFAULT;
2072 goto out;
2073 }
2074 copied++;
2075 }
2076 }
2077 }
2078 out_resp->count_encoders = encoders_count;
2079
2080out:
Daniel Vetter7b240562012-12-12 00:35:33 +01002081 mutex_unlock(&dev->mode_config.mutex);
2082
Dave Airlief453ba02008-11-07 14:05:41 -08002083 return ret;
2084}
2085
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002086/**
2087 * drm_mode_getencoder - get encoder configuration
2088 * @dev: drm device for the ioctl
2089 * @data: data pointer for the ioctl
2090 * @file_priv: drm file for the ioctl call
2091 *
2092 * Construct a encoder configuration structure to return to the user.
2093 *
2094 * Called by the user via ioctl.
2095 *
2096 * Returns:
2097 * Zero on success, errno on failure.
2098 */
Dave Airlief453ba02008-11-07 14:05:41 -08002099int drm_mode_getencoder(struct drm_device *dev, void *data,
2100 struct drm_file *file_priv)
2101{
2102 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002103 struct drm_encoder *encoder;
2104 int ret = 0;
2105
Dave Airliefb3b06c2011-02-08 13:55:21 +10002106 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2107 return -EINVAL;
2108
Daniel Vetter84849902012-12-02 00:28:11 +01002109 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002110 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2111 if (!encoder) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002112 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002113 goto out;
2114 }
Dave Airlief453ba02008-11-07 14:05:41 -08002115
2116 if (encoder->crtc)
2117 enc_resp->crtc_id = encoder->crtc->base.id;
2118 else
2119 enc_resp->crtc_id = 0;
2120 enc_resp->encoder_type = encoder->encoder_type;
2121 enc_resp->encoder_id = encoder->base.id;
2122 enc_resp->possible_crtcs = encoder->possible_crtcs;
2123 enc_resp->possible_clones = encoder->possible_clones;
2124
2125out:
Daniel Vetter84849902012-12-02 00:28:11 +01002126 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002127 return ret;
2128}
2129
2130/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002131 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002132 * @dev: DRM device
2133 * @data: ioctl data
2134 * @file_priv: DRM file info
2135 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002136 * Construct a list of plane ids to return to the user.
2137 *
2138 * Called by the user via ioctl.
2139 *
2140 * Returns:
2141 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002142 */
2143int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002144 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002145{
2146 struct drm_mode_get_plane_res *plane_resp = data;
2147 struct drm_mode_config *config;
2148 struct drm_plane *plane;
2149 uint32_t __user *plane_ptr;
2150 int copied = 0, ret = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002151 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002152
2153 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2154 return -EINVAL;
2155
Daniel Vetter84849902012-12-02 00:28:11 +01002156 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002157 config = &dev->mode_config;
2158
Matt Roper681e7ec2014-04-01 15:22:42 -07002159 if (file_priv->universal_planes)
2160 num_planes = config->num_total_plane;
2161 else
2162 num_planes = config->num_overlay_plane;
2163
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002164 /*
2165 * This ioctl is called twice, once to determine how much space is
2166 * needed, and the 2nd time to fill it.
2167 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002168 if (num_planes &&
2169 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002170 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002171
2172 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002173 /*
2174 * Unless userspace set the 'universal planes'
2175 * capability bit, only advertise overlays.
2176 */
2177 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2178 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002179 continue;
2180
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002181 if (put_user(plane->base.id, plane_ptr + copied)) {
2182 ret = -EFAULT;
2183 goto out;
2184 }
2185 copied++;
2186 }
2187 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002188 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002189
2190out:
Daniel Vetter84849902012-12-02 00:28:11 +01002191 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002192 return ret;
2193}
2194
2195/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002196 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002197 * @dev: DRM device
2198 * @data: ioctl data
2199 * @file_priv: DRM file info
2200 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002201 * Construct a plane configuration structure to return to the user.
2202 *
2203 * Called by the user via ioctl.
2204 *
2205 * Returns:
2206 * Zero on success, errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002207 */
2208int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002209 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002210{
2211 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002212 struct drm_plane *plane;
2213 uint32_t __user *format_ptr;
2214 int ret = 0;
2215
2216 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2217 return -EINVAL;
2218
Daniel Vetter84849902012-12-02 00:28:11 +01002219 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002220 plane = drm_plane_find(dev, plane_resp->plane_id);
2221 if (!plane) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002222 ret = -ENOENT;
2223 goto out;
2224 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002225
2226 if (plane->crtc)
2227 plane_resp->crtc_id = plane->crtc->base.id;
2228 else
2229 plane_resp->crtc_id = 0;
2230
2231 if (plane->fb)
2232 plane_resp->fb_id = plane->fb->base.id;
2233 else
2234 plane_resp->fb_id = 0;
2235
2236 plane_resp->plane_id = plane->base.id;
2237 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002238 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002239
2240 /*
2241 * This ioctl is called twice, once to determine how much space is
2242 * needed, and the 2nd time to fill it.
2243 */
2244 if (plane->format_count &&
2245 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002246 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002247 if (copy_to_user(format_ptr,
2248 plane->format_types,
2249 sizeof(uint32_t) * plane->format_count)) {
2250 ret = -EFAULT;
2251 goto out;
2252 }
2253 }
2254 plane_resp->count_format_types = plane->format_count;
2255
2256out:
Daniel Vetter84849902012-12-02 00:28:11 +01002257 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002258 return ret;
2259}
2260
Matt Roperb36552b2014-06-10 08:28:09 -07002261/*
2262 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002263 *
Matt Roperb36552b2014-06-10 08:28:09 -07002264 * Note that we assume an extra reference has already been taken on fb. If the
2265 * update fails, this reference will be dropped before return; if it succeeds,
2266 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002267 *
Matt Roperb36552b2014-06-10 08:28:09 -07002268 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002269 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002270static int setplane_internal(struct drm_plane *plane,
2271 struct drm_crtc *crtc,
Matt Roperb36552b2014-06-10 08:28:09 -07002272 struct drm_framebuffer *fb,
2273 int32_t crtc_x, int32_t crtc_y,
2274 uint32_t crtc_w, uint32_t crtc_h,
2275 /* src_{x,y,w,h} values are 16.16 fixed point */
2276 uint32_t src_x, uint32_t src_y,
2277 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002278{
Chris Wilson17cfd912014-06-13 15:22:28 +01002279 struct drm_device *dev = plane->dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002280 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002281 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02002282 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002283
Daniel Vetter3d30a592014-07-27 13:42:42 +02002284 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002285 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002286 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002287 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002288 ret = plane->funcs->disable_plane(plane);
2289 if (!ret) {
2290 plane->crtc = NULL;
2291 plane->fb = NULL;
2292 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002293 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002294 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002295 goto out;
2296 }
2297
Matt Roper7f994f32014-05-29 08:06:51 -07002298 /* Check whether this plane is usable on this CRTC */
2299 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2300 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2301 ret = -EINVAL;
2302 goto out;
2303 }
2304
Ville Syrjälä62443be2011-12-20 00:06:47 +02002305 /* Check whether this plane supports the fb pixel format. */
2306 for (i = 0; i < plane->format_count; i++)
2307 if (fb->pixel_format == plane->format_types[i])
2308 break;
2309 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002310 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2311 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002312 ret = -EINVAL;
2313 goto out;
2314 }
2315
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002316 fb_width = fb->width << 16;
2317 fb_height = fb->height << 16;
2318
2319 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002320 if (src_w > fb_width ||
2321 src_x > fb_width - src_w ||
2322 src_h > fb_height ||
2323 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002324 DRM_DEBUG_KMS("Invalid source coordinates "
2325 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002326 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2327 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2328 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2329 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002330 ret = -ENOSPC;
2331 goto out;
2332 }
2333
Daniel Vetter3d30a592014-07-27 13:42:42 +02002334 plane->old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002335 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002336 crtc_x, crtc_y, crtc_w, crtc_h,
2337 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002338 if (!ret) {
2339 plane->crtc = crtc;
2340 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002341 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002342 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002343 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002344 }
2345
2346out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002347 if (fb)
2348 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002349 if (plane->old_fb)
2350 drm_framebuffer_unreference(plane->old_fb);
2351 plane->old_fb = NULL;
2352 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002353
2354 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002355
2356}
2357
2358/**
2359 * drm_mode_setplane - configure a plane's configuration
2360 * @dev: DRM device
2361 * @data: ioctl data*
2362 * @file_priv: DRM file info
2363 *
2364 * Set plane configuration, including placement, fb, scaling, and other factors.
2365 * Or pass a NULL fb to disable (planes may be disabled without providing a
2366 * valid crtc).
2367 *
2368 * Returns:
2369 * Zero on success, errno on failure.
2370 */
2371int drm_mode_setplane(struct drm_device *dev, void *data,
2372 struct drm_file *file_priv)
2373{
2374 struct drm_mode_set_plane *plane_req = data;
2375 struct drm_mode_object *obj;
2376 struct drm_plane *plane;
2377 struct drm_crtc *crtc = NULL;
2378 struct drm_framebuffer *fb = NULL;
2379
2380 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2381 return -EINVAL;
2382
2383 /* Give drivers some help against integer overflows */
2384 if (plane_req->crtc_w > INT_MAX ||
2385 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2386 plane_req->crtc_h > INT_MAX ||
2387 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2388 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2389 plane_req->crtc_w, plane_req->crtc_h,
2390 plane_req->crtc_x, plane_req->crtc_y);
2391 return -ERANGE;
2392 }
2393
2394 /*
2395 * First, find the plane, crtc, and fb objects. If not available,
2396 * we don't bother to call the driver.
2397 */
2398 obj = drm_mode_object_find(dev, plane_req->plane_id,
2399 DRM_MODE_OBJECT_PLANE);
2400 if (!obj) {
2401 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2402 plane_req->plane_id);
2403 return -ENOENT;
2404 }
2405 plane = obj_to_plane(obj);
2406
2407 if (plane_req->fb_id) {
2408 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2409 if (!fb) {
2410 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2411 plane_req->fb_id);
2412 return -ENOENT;
2413 }
2414
2415 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2416 DRM_MODE_OBJECT_CRTC);
2417 if (!obj) {
2418 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2419 plane_req->crtc_id);
2420 return -ENOENT;
2421 }
2422 crtc = obj_to_crtc(obj);
2423 }
2424
Matt Roper161d0dc2014-06-10 08:28:10 -07002425 /*
2426 * setplane_internal will take care of deref'ing either the old or new
2427 * framebuffer depending on success.
2428 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002429 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002430 plane_req->crtc_x, plane_req->crtc_y,
2431 plane_req->crtc_w, plane_req->crtc_h,
2432 plane_req->src_x, plane_req->src_y,
2433 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002434}
2435
2436/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002437 * drm_mode_set_config_internal - helper to call ->set_config
2438 * @set: modeset config to set
2439 *
2440 * This is a little helper to wrap internal calls to the ->set_config driver
2441 * interface. The only thing it adds is correct refcounting dance.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002442 *
2443 * Returns:
2444 * Zero on success, errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002445 */
2446int drm_mode_set_config_internal(struct drm_mode_set *set)
2447{
2448 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002449 struct drm_framebuffer *fb;
2450 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002451 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002452
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002453 /*
2454 * NOTE: ->set_config can also disable other crtcs (if we steal all
2455 * connectors from it), hence we need to refcount the fbs across all
2456 * crtcs. Atomic modeset will have saner semantics ...
2457 */
2458 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002459 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002460
Daniel Vetterb0d12322012-12-11 01:07:12 +01002461 fb = set->fb;
2462
2463 ret = crtc->funcs->set_config(set);
2464 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002465 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002466 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002467 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002468
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002469 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002470 if (tmp->primary->fb)
2471 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002472 if (tmp->primary->old_fb)
2473 drm_framebuffer_unreference(tmp->primary->old_fb);
2474 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002475 }
2476
2477 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002478}
2479EXPORT_SYMBOL(drm_mode_set_config_internal);
2480
Matt Roperaf936292014-04-01 15:22:34 -07002481/**
2482 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2483 * CRTC viewport
2484 * @crtc: CRTC that framebuffer will be displayed on
2485 * @x: x panning
2486 * @y: y panning
2487 * @mode: mode that framebuffer will be displayed under
2488 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002489 */
Matt Roperaf936292014-04-01 15:22:34 -07002490int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2491 int x, int y,
2492 const struct drm_display_mode *mode,
2493 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002494
2495{
2496 int hdisplay, vdisplay;
2497
2498 hdisplay = mode->hdisplay;
2499 vdisplay = mode->vdisplay;
2500
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002501 if (drm_mode_is_stereo(mode)) {
2502 struct drm_display_mode adjusted = *mode;
2503
2504 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2505 hdisplay = adjusted.crtc_hdisplay;
2506 vdisplay = adjusted.crtc_vdisplay;
2507 }
2508
Damien Lespiauc11e9282013-09-25 16:45:30 +01002509 if (crtc->invert_dimensions)
2510 swap(hdisplay, vdisplay);
2511
2512 if (hdisplay > fb->width ||
2513 vdisplay > fb->height ||
2514 x > fb->width - hdisplay ||
2515 y > fb->height - vdisplay) {
2516 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2517 fb->width, fb->height, hdisplay, vdisplay, x, y,
2518 crtc->invert_dimensions ? " (inverted)" : "");
2519 return -ENOSPC;
2520 }
2521
2522 return 0;
2523}
Matt Roperaf936292014-04-01 15:22:34 -07002524EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002525
Daniel Vetter2d13b672012-12-11 13:47:23 +01002526/**
Dave Airlief453ba02008-11-07 14:05:41 -08002527 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002528 * @dev: drm device for the ioctl
2529 * @data: data pointer for the ioctl
2530 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002531 *
Dave Airlief453ba02008-11-07 14:05:41 -08002532 * Build a new CRTC configuration based on user request.
2533 *
2534 * Called by the user via ioctl.
2535 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002536 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002537 * Zero on success, errno on failure.
2538 */
2539int drm_mode_setcrtc(struct drm_device *dev, void *data,
2540 struct drm_file *file_priv)
2541{
2542 struct drm_mode_config *config = &dev->mode_config;
2543 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002544 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002545 struct drm_connector **connector_set = NULL, *connector;
2546 struct drm_framebuffer *fb = NULL;
2547 struct drm_display_mode *mode = NULL;
2548 struct drm_mode_set set;
2549 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002550 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002551 int i;
2552
Dave Airliefb3b06c2011-02-08 13:55:21 +10002553 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2554 return -EINVAL;
2555
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002556 /* For some reason crtc x/y offsets are signed internally. */
2557 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2558 return -ERANGE;
2559
Daniel Vetter84849902012-12-02 00:28:11 +01002560 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002561 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2562 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002563 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002564 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002565 goto out;
2566 }
Jerome Glisse94401062010-07-15 15:43:25 -04002567 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002568
2569 if (crtc_req->mode_valid) {
2570 /* If we have a mode we need a framebuffer. */
2571 /* If we pass -1, set the mode with the currently bound fb */
2572 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002573 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002574 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2575 ret = -EINVAL;
2576 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002577 }
Matt Roperf4510a22014-04-01 15:22:40 -07002578 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002579 /* Make refcounting symmetric with the lookup path. */
2580 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002581 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002582 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2583 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002584 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2585 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002586 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002587 goto out;
2588 }
Dave Airlief453ba02008-11-07 14:05:41 -08002589 }
2590
2591 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002592 if (!mode) {
2593 ret = -ENOMEM;
2594 goto out;
2595 }
2596
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002597 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2598 if (ret) {
2599 DRM_DEBUG_KMS("Invalid mode\n");
2600 goto out;
2601 }
2602
Dave Airlief453ba02008-11-07 14:05:41 -08002603 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002604
Damien Lespiauc11e9282013-09-25 16:45:30 +01002605 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2606 mode, fb);
2607 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002608 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002609
Dave Airlief453ba02008-11-07 14:05:41 -08002610 }
2611
2612 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002613 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002614 ret = -EINVAL;
2615 goto out;
2616 }
2617
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002618 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002619 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002620 crtc_req->count_connectors);
2621 ret = -EINVAL;
2622 goto out;
2623 }
2624
2625 if (crtc_req->count_connectors > 0) {
2626 u32 out_id;
2627
2628 /* Avoid unbounded kernel memory allocation */
2629 if (crtc_req->count_connectors > config->num_connector) {
2630 ret = -EINVAL;
2631 goto out;
2632 }
2633
2634 connector_set = kmalloc(crtc_req->count_connectors *
2635 sizeof(struct drm_connector *),
2636 GFP_KERNEL);
2637 if (!connector_set) {
2638 ret = -ENOMEM;
2639 goto out;
2640 }
2641
2642 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002643 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002644 if (get_user(out_id, &set_connectors_ptr[i])) {
2645 ret = -EFAULT;
2646 goto out;
2647 }
2648
Rob Clarka2b34e22013-10-05 16:36:52 -04002649 connector = drm_connector_find(dev, out_id);
2650 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002651 DRM_DEBUG_KMS("Connector id %d unknown\n",
2652 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002653 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002654 goto out;
2655 }
Jerome Glisse94401062010-07-15 15:43:25 -04002656 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2657 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002658 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002659
2660 connector_set[i] = connector;
2661 }
2662 }
2663
2664 set.crtc = crtc;
2665 set.x = crtc_req->x;
2666 set.y = crtc_req->y;
2667 set.mode = mode;
2668 set.connectors = connector_set;
2669 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002670 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002671 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002672
2673out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002674 if (fb)
2675 drm_framebuffer_unreference(fb);
2676
Dave Airlief453ba02008-11-07 14:05:41 -08002677 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002678 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002679 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002680 return ret;
2681}
2682
Matt Roper161d0dc2014-06-10 08:28:10 -07002683/**
2684 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2685 * universal plane handler call
2686 * @crtc: crtc to update cursor for
2687 * @req: data pointer for the ioctl
2688 * @file_priv: drm file for the ioctl call
2689 *
2690 * Legacy cursor ioctl's work directly with driver buffer handles. To
2691 * translate legacy ioctl calls into universal plane handler calls, we need to
2692 * wrap the native buffer handle in a drm_framebuffer.
2693 *
2694 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2695 * buffer with a pitch of 4*width; the universal plane interface should be used
2696 * directly in cases where the hardware can support other buffer settings and
2697 * userspace wants to make use of these capabilities.
2698 *
2699 * Returns:
2700 * Zero on success, errno on failure.
2701 */
2702static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2703 struct drm_mode_cursor2 *req,
2704 struct drm_file *file_priv)
2705{
2706 struct drm_device *dev = crtc->dev;
2707 struct drm_framebuffer *fb = NULL;
2708 struct drm_mode_fb_cmd2 fbreq = {
2709 .width = req->width,
2710 .height = req->height,
2711 .pixel_format = DRM_FORMAT_ARGB8888,
2712 .pitches = { req->width * 4 },
2713 .handles = { req->handle },
2714 };
2715 int32_t crtc_x, crtc_y;
2716 uint32_t crtc_w = 0, crtc_h = 0;
2717 uint32_t src_w = 0, src_h = 0;
2718 int ret = 0;
2719
2720 BUG_ON(!crtc->cursor);
2721
2722 /*
2723 * Obtain fb we'll be using (either new or existing) and take an extra
2724 * reference to it if fb != null. setplane will take care of dropping
2725 * the reference if the plane update fails.
2726 */
2727 if (req->flags & DRM_MODE_CURSOR_BO) {
2728 if (req->handle) {
2729 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2730 if (IS_ERR(fb)) {
2731 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2732 return PTR_ERR(fb);
2733 }
2734
2735 drm_framebuffer_reference(fb);
2736 } else {
2737 fb = NULL;
2738 }
2739 } else {
2740 mutex_lock(&dev->mode_config.mutex);
2741 fb = crtc->cursor->fb;
2742 if (fb)
2743 drm_framebuffer_reference(fb);
2744 mutex_unlock(&dev->mode_config.mutex);
2745 }
2746
2747 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2748 crtc_x = req->x;
2749 crtc_y = req->y;
2750 } else {
2751 crtc_x = crtc->cursor_x;
2752 crtc_y = crtc->cursor_y;
2753 }
2754
2755 if (fb) {
2756 crtc_w = fb->width;
2757 crtc_h = fb->height;
2758 src_w = fb->width << 16;
2759 src_h = fb->height << 16;
2760 }
2761
2762 /*
2763 * setplane_internal will take care of deref'ing either the old or new
2764 * framebuffer depending on success.
2765 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002766 ret = setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002767 crtc_x, crtc_y, crtc_w, crtc_h,
2768 0, 0, src_w, src_h);
2769
2770 /* Update successful; save new cursor position, if necessary */
2771 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2772 crtc->cursor_x = req->x;
2773 crtc->cursor_y = req->y;
2774 }
2775
2776 return ret;
2777}
2778
Dave Airlie4c813d42013-06-20 11:48:52 +10002779static int drm_mode_cursor_common(struct drm_device *dev,
2780 struct drm_mode_cursor2 *req,
2781 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002782{
Dave Airlief453ba02008-11-07 14:05:41 -08002783 struct drm_crtc *crtc;
2784 int ret = 0;
2785
Dave Airliefb3b06c2011-02-08 13:55:21 +10002786 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2787 return -EINVAL;
2788
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002789 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002790 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002791
Rob Clarka2b34e22013-10-05 16:36:52 -04002792 crtc = drm_crtc_find(dev, req->crtc_id);
2793 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002794 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002795 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002796 }
Dave Airlief453ba02008-11-07 14:05:41 -08002797
Matt Roper161d0dc2014-06-10 08:28:10 -07002798 /*
2799 * If this crtc has a universal cursor plane, call that plane's update
2800 * handler rather than using legacy cursor handlers.
2801 */
2802 if (crtc->cursor)
2803 return drm_mode_cursor_universal(crtc, req, file_priv);
2804
Daniel Vetterd059f652014-07-25 18:07:40 +02002805 drm_modeset_lock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002806 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002807 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002808 ret = -ENXIO;
2809 goto out;
2810 }
2811 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002812 if (crtc->funcs->cursor_set2)
2813 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2814 req->width, req->height, req->hot_x, req->hot_y);
2815 else
2816 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2817 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002818 }
2819
2820 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2821 if (crtc->funcs->cursor_move) {
2822 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2823 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002824 ret = -EFAULT;
2825 goto out;
2826 }
2827 }
2828out:
Daniel Vetterd059f652014-07-25 18:07:40 +02002829 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01002830
Dave Airlief453ba02008-11-07 14:05:41 -08002831 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002832
2833}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002834
2835
2836/**
2837 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2838 * @dev: drm device for the ioctl
2839 * @data: data pointer for the ioctl
2840 * @file_priv: drm file for the ioctl call
2841 *
2842 * Set the cursor configuration based on user request.
2843 *
2844 * Called by the user via ioctl.
2845 *
2846 * Returns:
2847 * Zero on success, errno on failure.
2848 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002849int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002850 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002851{
2852 struct drm_mode_cursor *req = data;
2853 struct drm_mode_cursor2 new_req;
2854
2855 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2856 new_req.hot_x = new_req.hot_y = 0;
2857
2858 return drm_mode_cursor_common(dev, &new_req, file_priv);
2859}
2860
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002861/**
2862 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2863 * @dev: drm device for the ioctl
2864 * @data: data pointer for the ioctl
2865 * @file_priv: drm file for the ioctl call
2866 *
2867 * Set the cursor configuration based on user request. This implements the 2nd
2868 * version of the cursor ioctl, which allows userspace to additionally specify
2869 * the hotspot of the pointer.
2870 *
2871 * Called by the user via ioctl.
2872 *
2873 * Returns:
2874 * Zero on success, errno on failure.
2875 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002876int drm_mode_cursor2_ioctl(struct drm_device *dev,
2877 void *data, struct drm_file *file_priv)
2878{
2879 struct drm_mode_cursor2 *req = data;
2880 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002881}
2882
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002883/**
2884 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2885 * @bpp: bits per pixels
2886 * @depth: bit depth per pixel
2887 *
2888 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2889 * Useful in fbdev emulation code, since that deals in those values.
2890 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002891uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2892{
2893 uint32_t fmt;
2894
2895 switch (bpp) {
2896 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002897 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002898 break;
2899 case 16:
2900 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002901 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002902 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002903 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002904 break;
2905 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002906 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002907 break;
2908 case 32:
2909 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002910 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002911 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002912 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002913 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002914 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002915 break;
2916 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002917 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2918 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002919 break;
2920 }
2921
2922 return fmt;
2923}
2924EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2925
Dave Airlief453ba02008-11-07 14:05:41 -08002926/**
2927 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002928 * @dev: drm device for the ioctl
2929 * @data: data pointer for the ioctl
2930 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002931 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002932 * Add a new FB to the specified CRTC, given a user request. This is the
2933 * original addfb ioclt which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08002934 *
2935 * Called by the user via ioctl.
2936 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002937 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08002938 * Zero on success, errno on failure.
2939 */
2940int drm_mode_addfb(struct drm_device *dev,
2941 void *data, struct drm_file *file_priv)
2942{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002943 struct drm_mode_fb_cmd *or = data;
2944 struct drm_mode_fb_cmd2 r = {};
2945 struct drm_mode_config *config = &dev->mode_config;
2946 struct drm_framebuffer *fb;
2947 int ret = 0;
2948
2949 /* Use new struct with format internally */
2950 r.fb_id = or->fb_id;
2951 r.width = or->width;
2952 r.height = or->height;
2953 r.pitches[0] = or->pitch;
2954 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2955 r.handles[0] = or->handle;
2956
2957 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2958 return -EINVAL;
2959
Jesse Barnesacb4b992011-11-07 12:03:23 -08002960 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002961 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002962
2963 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002964 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002965
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002966 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2967 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002968 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002969 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002970 }
2971
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002972 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002973 or->fb_id = fb->base.id;
2974 list_add(&fb->filp_head, &file_priv->fbs);
2975 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002976 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002977
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002978 return ret;
2979}
2980
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002981static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002982{
2983 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2984
2985 switch (format) {
2986 case DRM_FORMAT_C8:
2987 case DRM_FORMAT_RGB332:
2988 case DRM_FORMAT_BGR233:
2989 case DRM_FORMAT_XRGB4444:
2990 case DRM_FORMAT_XBGR4444:
2991 case DRM_FORMAT_RGBX4444:
2992 case DRM_FORMAT_BGRX4444:
2993 case DRM_FORMAT_ARGB4444:
2994 case DRM_FORMAT_ABGR4444:
2995 case DRM_FORMAT_RGBA4444:
2996 case DRM_FORMAT_BGRA4444:
2997 case DRM_FORMAT_XRGB1555:
2998 case DRM_FORMAT_XBGR1555:
2999 case DRM_FORMAT_RGBX5551:
3000 case DRM_FORMAT_BGRX5551:
3001 case DRM_FORMAT_ARGB1555:
3002 case DRM_FORMAT_ABGR1555:
3003 case DRM_FORMAT_RGBA5551:
3004 case DRM_FORMAT_BGRA5551:
3005 case DRM_FORMAT_RGB565:
3006 case DRM_FORMAT_BGR565:
3007 case DRM_FORMAT_RGB888:
3008 case DRM_FORMAT_BGR888:
3009 case DRM_FORMAT_XRGB8888:
3010 case DRM_FORMAT_XBGR8888:
3011 case DRM_FORMAT_RGBX8888:
3012 case DRM_FORMAT_BGRX8888:
3013 case DRM_FORMAT_ARGB8888:
3014 case DRM_FORMAT_ABGR8888:
3015 case DRM_FORMAT_RGBA8888:
3016 case DRM_FORMAT_BGRA8888:
3017 case DRM_FORMAT_XRGB2101010:
3018 case DRM_FORMAT_XBGR2101010:
3019 case DRM_FORMAT_RGBX1010102:
3020 case DRM_FORMAT_BGRX1010102:
3021 case DRM_FORMAT_ARGB2101010:
3022 case DRM_FORMAT_ABGR2101010:
3023 case DRM_FORMAT_RGBA1010102:
3024 case DRM_FORMAT_BGRA1010102:
3025 case DRM_FORMAT_YUYV:
3026 case DRM_FORMAT_YVYU:
3027 case DRM_FORMAT_UYVY:
3028 case DRM_FORMAT_VYUY:
3029 case DRM_FORMAT_AYUV:
3030 case DRM_FORMAT_NV12:
3031 case DRM_FORMAT_NV21:
3032 case DRM_FORMAT_NV16:
3033 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003034 case DRM_FORMAT_NV24:
3035 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003036 case DRM_FORMAT_YUV410:
3037 case DRM_FORMAT_YVU410:
3038 case DRM_FORMAT_YUV411:
3039 case DRM_FORMAT_YVU411:
3040 case DRM_FORMAT_YUV420:
3041 case DRM_FORMAT_YVU420:
3042 case DRM_FORMAT_YUV422:
3043 case DRM_FORMAT_YVU422:
3044 case DRM_FORMAT_YUV444:
3045 case DRM_FORMAT_YVU444:
3046 return 0;
3047 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003048 DRM_DEBUG_KMS("invalid pixel format %s\n",
3049 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003050 return -EINVAL;
3051 }
3052}
3053
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003054static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003055{
3056 int ret, hsub, vsub, num_planes, i;
3057
3058 ret = format_check(r);
3059 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003060 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3061 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003062 return ret;
3063 }
3064
3065 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3066 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3067 num_planes = drm_format_num_planes(r->pixel_format);
3068
3069 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003070 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003071 return -EINVAL;
3072 }
3073
3074 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003075 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003076 return -EINVAL;
3077 }
3078
3079 for (i = 0; i < num_planes; i++) {
3080 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003081 unsigned int height = r->height / (i != 0 ? vsub : 1);
3082 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003083
3084 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003085 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003086 return -EINVAL;
3087 }
3088
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003089 if ((uint64_t) width * cpp > UINT_MAX)
3090 return -ERANGE;
3091
3092 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3093 return -ERANGE;
3094
3095 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003096 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003097 return -EINVAL;
3098 }
3099 }
3100
3101 return 0;
3102}
3103
Matt Roperc394c2b2014-06-10 08:28:08 -07003104static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3105 struct drm_mode_fb_cmd2 *r,
3106 struct drm_file *file_priv)
3107{
3108 struct drm_mode_config *config = &dev->mode_config;
3109 struct drm_framebuffer *fb;
3110 int ret;
3111
3112 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3113 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3114 return ERR_PTR(-EINVAL);
3115 }
3116
3117 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3118 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3119 r->width, config->min_width, config->max_width);
3120 return ERR_PTR(-EINVAL);
3121 }
3122 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3123 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3124 r->height, config->min_height, config->max_height);
3125 return ERR_PTR(-EINVAL);
3126 }
3127
3128 ret = framebuffer_check(r);
3129 if (ret)
3130 return ERR_PTR(ret);
3131
3132 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3133 if (IS_ERR(fb)) {
3134 DRM_DEBUG_KMS("could not create framebuffer\n");
3135 return fb;
3136 }
3137
3138 mutex_lock(&file_priv->fbs_lock);
3139 r->fb_id = fb->base.id;
3140 list_add(&fb->filp_head, &file_priv->fbs);
3141 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3142 mutex_unlock(&file_priv->fbs_lock);
3143
3144 return fb;
3145}
3146
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003147/**
3148 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003149 * @dev: drm device for the ioctl
3150 * @data: data pointer for the ioctl
3151 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003152 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003153 * Add a new FB to the specified CRTC, given a user request with format. This is
3154 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3155 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003156 *
3157 * Called by the user via ioctl.
3158 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003159 * Returns:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003160 * Zero on success, errno on failure.
3161 */
3162int drm_mode_addfb2(struct drm_device *dev,
3163 void *data, struct drm_file *file_priv)
3164{
Dave Airlief453ba02008-11-07 14:05:41 -08003165 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003166
Dave Airliefb3b06c2011-02-08 13:55:21 +10003167 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3168 return -EINVAL;
3169
Matt Roperc394c2b2014-06-10 08:28:08 -07003170 fb = add_framebuffer_internal(dev, data, file_priv);
3171 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003172 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003173
Matt Roperc394c2b2014-06-10 08:28:08 -07003174 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003175}
3176
3177/**
3178 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003179 * @dev: drm device for the ioctl
3180 * @data: data pointer for the ioctl
3181 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003182 *
Dave Airlief453ba02008-11-07 14:05:41 -08003183 * Remove the FB specified by the user.
3184 *
3185 * Called by the user via ioctl.
3186 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003187 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003188 * Zero on success, errno on failure.
3189 */
3190int drm_mode_rmfb(struct drm_device *dev,
3191 void *data, struct drm_file *file_priv)
3192{
Dave Airlief453ba02008-11-07 14:05:41 -08003193 struct drm_framebuffer *fb = NULL;
3194 struct drm_framebuffer *fbl = NULL;
3195 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003196 int found = 0;
3197
Dave Airliefb3b06c2011-02-08 13:55:21 +10003198 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3199 return -EINVAL;
3200
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003201 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003202 mutex_lock(&dev->mode_config.fb_lock);
3203 fb = __drm_framebuffer_lookup(dev, *id);
3204 if (!fb)
3205 goto fail_lookup;
3206
Dave Airlief453ba02008-11-07 14:05:41 -08003207 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3208 if (fb == fbl)
3209 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003210 if (!found)
3211 goto fail_lookup;
3212
3213 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3214 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003215
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003216 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003217 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003218 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003219
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003220 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003221
Daniel Vetter2b677e82012-12-10 21:16:05 +01003222 return 0;
3223
3224fail_lookup:
3225 mutex_unlock(&dev->mode_config.fb_lock);
3226 mutex_unlock(&file_priv->fbs_lock);
3227
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003228 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003229}
3230
3231/**
3232 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003233 * @dev: drm device for the ioctl
3234 * @data: data pointer for the ioctl
3235 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003236 *
Dave Airlief453ba02008-11-07 14:05:41 -08003237 * Lookup the FB given its ID and return info about it.
3238 *
3239 * Called by the user via ioctl.
3240 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003241 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003242 * Zero on success, errno on failure.
3243 */
3244int drm_mode_getfb(struct drm_device *dev,
3245 void *data, struct drm_file *file_priv)
3246{
3247 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003248 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003249 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003250
Dave Airliefb3b06c2011-02-08 13:55:21 +10003251 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3252 return -EINVAL;
3253
Daniel Vetter786b99e2012-12-02 21:53:40 +01003254 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003255 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003256 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003257
3258 r->height = fb->height;
3259 r->width = fb->width;
3260 r->depth = fb->depth;
3261 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003262 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003263 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003264 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003265 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003266 ret = fb->funcs->create_handle(fb, file_priv,
3267 &r->handle);
3268 } else {
3269 /* GET_FB() is an unprivileged ioctl so we must not
3270 * return a buffer-handle to non-master processes! For
3271 * backwards-compatibility reasons, we cannot make
3272 * GET_FB() privileged, so just return an invalid handle
3273 * for non-masters. */
3274 r->handle = 0;
3275 ret = 0;
3276 }
3277 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003278 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003279 }
Dave Airlief453ba02008-11-07 14:05:41 -08003280
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003281 drm_framebuffer_unreference(fb);
3282
Dave Airlief453ba02008-11-07 14:05:41 -08003283 return ret;
3284}
3285
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003286/**
3287 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3288 * @dev: drm device for the ioctl
3289 * @data: data pointer for the ioctl
3290 * @file_priv: drm file for the ioctl call
3291 *
3292 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3293 * rectangle list. Generic userspace which does frontbuffer rendering must call
3294 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3295 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3296 *
3297 * Modesetting drivers which always update the frontbuffer do not need to
3298 * implement the corresponding ->dirty framebuffer callback.
3299 *
3300 * Called by the user via ioctl.
3301 *
3302 * Returns:
3303 * Zero on success, errno on failure.
3304 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003305int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3306 void *data, struct drm_file *file_priv)
3307{
3308 struct drm_clip_rect __user *clips_ptr;
3309 struct drm_clip_rect *clips = NULL;
3310 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003311 struct drm_framebuffer *fb;
3312 unsigned flags;
3313 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003314 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003315
Dave Airliefb3b06c2011-02-08 13:55:21 +10003316 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3317 return -EINVAL;
3318
Daniel Vetter786b99e2012-12-02 21:53:40 +01003319 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003320 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003321 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003322
3323 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003324 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003325
3326 if (!num_clips != !clips_ptr) {
3327 ret = -EINVAL;
3328 goto out_err1;
3329 }
3330
3331 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3332
3333 /* If userspace annotates copy, clips must come in pairs */
3334 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3335 ret = -EINVAL;
3336 goto out_err1;
3337 }
3338
3339 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003340 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3341 ret = -EINVAL;
3342 goto out_err1;
3343 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003344 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3345 if (!clips) {
3346 ret = -ENOMEM;
3347 goto out_err1;
3348 }
3349
3350 ret = copy_from_user(clips, clips_ptr,
3351 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003352 if (ret) {
3353 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003354 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003355 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003356 }
3357
3358 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003359 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3360 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003361 } else {
3362 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003363 }
3364
3365out_err2:
3366 kfree(clips);
3367out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003368 drm_framebuffer_unreference(fb);
3369
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003370 return ret;
3371}
3372
3373
Dave Airlief453ba02008-11-07 14:05:41 -08003374/**
3375 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003376 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003377 *
Dave Airlief453ba02008-11-07 14:05:41 -08003378 * Destroy all the FBs associated with @filp.
3379 *
3380 * Called by the user via ioctl.
3381 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003382 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08003383 * Zero on success, errno on failure.
3384 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003385void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003386{
Dave Airlief453ba02008-11-07 14:05:41 -08003387 struct drm_device *dev = priv->minor->dev;
3388 struct drm_framebuffer *fb, *tfb;
3389
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003390 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003391 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003392
3393 mutex_lock(&dev->mode_config.fb_lock);
3394 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3395 __drm_framebuffer_unregister(dev, fb);
3396 mutex_unlock(&dev->mode_config.fb_lock);
3397
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003398 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003399
3400 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003401 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003402 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003403 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003404}
3405
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003406/**
3407 * drm_property_create - create a new property type
3408 * @dev: drm device
3409 * @flags: flags specifying the property type
3410 * @name: name of the property
3411 * @num_values: number of pre-defined values
3412 *
3413 * This creates a new generic drm property which can then be attached to a drm
3414 * object with drm_object_attach_property. The returned property object must be
3415 * freed with drm_property_destroy.
3416 *
3417 * Returns:
3418 * A pointer to the newly created property on success, NULL on failure.
3419 */
Dave Airlief453ba02008-11-07 14:05:41 -08003420struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3421 const char *name, int num_values)
3422{
3423 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003424 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003425
3426 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3427 if (!property)
3428 return NULL;
3429
Rob Clark98f75de2014-05-30 11:37:03 -04003430 property->dev = dev;
3431
Dave Airlief453ba02008-11-07 14:05:41 -08003432 if (num_values) {
3433 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3434 if (!property->values)
3435 goto fail;
3436 }
3437
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003438 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3439 if (ret)
3440 goto fail;
3441
Dave Airlief453ba02008-11-07 14:05:41 -08003442 property->flags = flags;
3443 property->num_values = num_values;
3444 INIT_LIST_HEAD(&property->enum_blob_list);
3445
Vinson Lee471dd2e2011-11-10 11:55:40 -08003446 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003447 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003448 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3449 }
Dave Airlief453ba02008-11-07 14:05:41 -08003450
3451 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003452
3453 WARN_ON(!drm_property_type_valid(property));
3454
Dave Airlief453ba02008-11-07 14:05:41 -08003455 return property;
3456fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003457 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003458 kfree(property);
3459 return NULL;
3460}
3461EXPORT_SYMBOL(drm_property_create);
3462
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003463/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003464 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003465 * @dev: drm device
3466 * @flags: flags specifying the property type
3467 * @name: name of the property
3468 * @props: enumeration lists with property values
3469 * @num_values: number of pre-defined values
3470 *
3471 * This creates a new generic drm property which can then be attached to a drm
3472 * object with drm_object_attach_property. The returned property object must be
3473 * freed with drm_property_destroy.
3474 *
3475 * Userspace is only allowed to set one of the predefined values for enumeration
3476 * properties.
3477 *
3478 * Returns:
3479 * A pointer to the newly created property on success, NULL on failure.
3480 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003481struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3482 const char *name,
3483 const struct drm_prop_enum_list *props,
3484 int num_values)
3485{
3486 struct drm_property *property;
3487 int i, ret;
3488
3489 flags |= DRM_MODE_PROP_ENUM;
3490
3491 property = drm_property_create(dev, flags, name, num_values);
3492 if (!property)
3493 return NULL;
3494
3495 for (i = 0; i < num_values; i++) {
3496 ret = drm_property_add_enum(property, i,
3497 props[i].type,
3498 props[i].name);
3499 if (ret) {
3500 drm_property_destroy(dev, property);
3501 return NULL;
3502 }
3503 }
3504
3505 return property;
3506}
3507EXPORT_SYMBOL(drm_property_create_enum);
3508
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003509/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003510 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003511 * @dev: drm device
3512 * @flags: flags specifying the property type
3513 * @name: name of the property
3514 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003515 * @num_props: size of the @props array
3516 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003517 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003518 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003519 * object with drm_object_attach_property. The returned property object must be
3520 * freed with drm_property_destroy.
3521 *
3522 * Compared to plain enumeration properties userspace is allowed to set any
3523 * or'ed together combination of the predefined property bitflag values
3524 *
3525 * Returns:
3526 * A pointer to the newly created property on success, NULL on failure.
3527 */
Rob Clark49e27542012-05-17 02:23:26 -06003528struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3529 int flags, const char *name,
3530 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303531 int num_props,
3532 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003533{
3534 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303535 int i, ret, index = 0;
3536 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003537
3538 flags |= DRM_MODE_PROP_BITMASK;
3539
3540 property = drm_property_create(dev, flags, name, num_values);
3541 if (!property)
3542 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303543 for (i = 0; i < num_props; i++) {
3544 if (!(supported_bits & (1ULL << props[i].type)))
3545 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003546
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303547 if (WARN_ON(index >= num_values)) {
3548 drm_property_destroy(dev, property);
3549 return NULL;
3550 }
3551
3552 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003553 props[i].type,
3554 props[i].name);
3555 if (ret) {
3556 drm_property_destroy(dev, property);
3557 return NULL;
3558 }
3559 }
3560
3561 return property;
3562}
3563EXPORT_SYMBOL(drm_property_create_bitmask);
3564
Rob Clarkebc44cf2012-09-12 22:22:31 -05003565static struct drm_property *property_create_range(struct drm_device *dev,
3566 int flags, const char *name,
3567 uint64_t min, uint64_t max)
3568{
3569 struct drm_property *property;
3570
3571 property = drm_property_create(dev, flags, name, 2);
3572 if (!property)
3573 return NULL;
3574
3575 property->values[0] = min;
3576 property->values[1] = max;
3577
3578 return property;
3579}
3580
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003581/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003582 * drm_property_create_range - create a new ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003583 * @dev: drm device
3584 * @flags: flags specifying the property type
3585 * @name: name of the property
3586 * @min: minimum value of the property
3587 * @max: maximum value of the property
3588 *
3589 * This creates a new generic drm property which can then be attached to a drm
3590 * object with drm_object_attach_property. The returned property object must be
3591 * freed with drm_property_destroy.
3592 *
3593 * Userspace is allowed to set any interger value in the (min, max) range
3594 * inclusive.
3595 *
3596 * Returns:
3597 * A pointer to the newly created property on success, NULL on failure.
3598 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003599struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3600 const char *name,
3601 uint64_t min, uint64_t max)
3602{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003603 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3604 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003605}
3606EXPORT_SYMBOL(drm_property_create_range);
3607
Rob Clarkebc44cf2012-09-12 22:22:31 -05003608struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3609 int flags, const char *name,
3610 int64_t min, int64_t max)
3611{
3612 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3613 name, I642U64(min), I642U64(max));
3614}
3615EXPORT_SYMBOL(drm_property_create_signed_range);
3616
Rob Clark98f75de2014-05-30 11:37:03 -04003617struct drm_property *drm_property_create_object(struct drm_device *dev,
3618 int flags, const char *name, uint32_t type)
3619{
3620 struct drm_property *property;
3621
3622 flags |= DRM_MODE_PROP_OBJECT;
3623
3624 property = drm_property_create(dev, flags, name, 1);
3625 if (!property)
3626 return NULL;
3627
3628 property->values[0] = type;
3629
3630 return property;
3631}
3632EXPORT_SYMBOL(drm_property_create_object);
3633
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003634/**
3635 * drm_property_add_enum - add a possible value to an enumeration property
3636 * @property: enumeration property to change
3637 * @index: index of the new enumeration
3638 * @value: value of the new enumeration
3639 * @name: symbolic name of the new enumeration
3640 *
3641 * This functions adds enumerations to a property.
3642 *
3643 * It's use is deprecated, drivers should use one of the more specific helpers
3644 * to directly create the property with all enumerations already attached.
3645 *
3646 * Returns:
3647 * Zero on success, error code on failure.
3648 */
Dave Airlief453ba02008-11-07 14:05:41 -08003649int drm_property_add_enum(struct drm_property *property, int index,
3650 uint64_t value, const char *name)
3651{
3652 struct drm_property_enum *prop_enum;
3653
Rob Clark5ea22f22014-05-30 11:34:01 -04003654 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3655 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003656 return -EINVAL;
3657
3658 /*
3659 * Bitmask enum properties have the additional constraint of values
3660 * from 0 to 63
3661 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003662 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3663 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003664 return -EINVAL;
3665
3666 if (!list_empty(&property->enum_blob_list)) {
3667 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3668 if (prop_enum->value == value) {
3669 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3670 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3671 return 0;
3672 }
3673 }
3674 }
3675
3676 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3677 if (!prop_enum)
3678 return -ENOMEM;
3679
3680 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3681 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3682 prop_enum->value = value;
3683
3684 property->values[index] = value;
3685 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3686 return 0;
3687}
3688EXPORT_SYMBOL(drm_property_add_enum);
3689
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003690/**
3691 * drm_property_destroy - destroy a drm property
3692 * @dev: drm device
3693 * @property: property to destry
3694 *
3695 * This function frees a property including any attached resources like
3696 * enumeration values.
3697 */
Dave Airlief453ba02008-11-07 14:05:41 -08003698void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3699{
3700 struct drm_property_enum *prop_enum, *pt;
3701
3702 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3703 list_del(&prop_enum->head);
3704 kfree(prop_enum);
3705 }
3706
3707 if (property->num_values)
3708 kfree(property->values);
3709 drm_mode_object_put(dev, &property->base);
3710 list_del(&property->head);
3711 kfree(property);
3712}
3713EXPORT_SYMBOL(drm_property_destroy);
3714
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003715/**
3716 * drm_object_attach_property - attach a property to a modeset object
3717 * @obj: drm modeset object
3718 * @property: property to attach
3719 * @init_val: initial value of the property
3720 *
3721 * This attaches the given property to the modeset object with the given initial
3722 * value. Currently this function cannot fail since the properties are stored in
3723 * a statically sized array.
3724 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003725void drm_object_attach_property(struct drm_mode_object *obj,
3726 struct drm_property *property,
3727 uint64_t init_val)
3728{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003729 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003730
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003731 if (count == DRM_OBJECT_MAX_PROPERTY) {
3732 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3733 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3734 "you see this message on the same object type.\n",
3735 obj->type);
3736 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003737 }
3738
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003739 obj->properties->ids[count] = property->base.id;
3740 obj->properties->values[count] = init_val;
3741 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003742}
3743EXPORT_SYMBOL(drm_object_attach_property);
3744
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003745/**
3746 * drm_object_property_set_value - set the value of a property
3747 * @obj: drm mode object to set property value for
3748 * @property: property to set
3749 * @val: value the property should be set to
3750 *
3751 * This functions sets a given property on a given object. This function only
3752 * changes the software state of the property, it does not call into the
3753 * driver's ->set_property callback.
3754 *
3755 * Returns:
3756 * Zero on success, error code on failure.
3757 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003758int drm_object_property_set_value(struct drm_mode_object *obj,
3759 struct drm_property *property, uint64_t val)
3760{
3761 int i;
3762
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003763 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003764 if (obj->properties->ids[i] == property->base.id) {
3765 obj->properties->values[i] = val;
3766 return 0;
3767 }
3768 }
3769
3770 return -EINVAL;
3771}
3772EXPORT_SYMBOL(drm_object_property_set_value);
3773
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003774/**
3775 * drm_object_property_get_value - retrieve the value of a property
3776 * @obj: drm mode object to get property value from
3777 * @property: property to retrieve
3778 * @val: storage for the property value
3779 *
3780 * This function retrieves the softare state of the given property for the given
3781 * property. Since there is no driver callback to retrieve the current property
3782 * value this might be out of sync with the hardware, depending upon the driver
3783 * and property.
3784 *
3785 * Returns:
3786 * Zero on success, error code on failure.
3787 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003788int drm_object_property_get_value(struct drm_mode_object *obj,
3789 struct drm_property *property, uint64_t *val)
3790{
3791 int i;
3792
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003793 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003794 if (obj->properties->ids[i] == property->base.id) {
3795 *val = obj->properties->values[i];
3796 return 0;
3797 }
3798 }
3799
3800 return -EINVAL;
3801}
3802EXPORT_SYMBOL(drm_object_property_get_value);
3803
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003804/**
3805 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3806 * @dev: DRM device
3807 * @data: ioctl data
3808 * @file_priv: DRM file info
3809 *
3810 * This function retrieves the current value for an connectors's property.
3811 *
3812 * Called by the user via ioctl.
3813 *
3814 * Returns:
3815 * Zero on success, errno on failure.
3816 */
Dave Airlief453ba02008-11-07 14:05:41 -08003817int drm_mode_getproperty_ioctl(struct drm_device *dev,
3818 void *data, struct drm_file *file_priv)
3819{
Dave Airlief453ba02008-11-07 14:05:41 -08003820 struct drm_mode_get_property *out_resp = data;
3821 struct drm_property *property;
3822 int enum_count = 0;
3823 int blob_count = 0;
3824 int value_count = 0;
3825 int ret = 0, i;
3826 int copied;
3827 struct drm_property_enum *prop_enum;
3828 struct drm_mode_property_enum __user *enum_ptr;
3829 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003830 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003831 uint64_t __user *values_ptr;
3832 uint32_t __user *blob_length_ptr;
3833
Dave Airliefb3b06c2011-02-08 13:55:21 +10003834 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3835 return -EINVAL;
3836
Daniel Vetter84849902012-12-02 00:28:11 +01003837 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003838 property = drm_property_find(dev, out_resp->prop_id);
3839 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003840 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003841 goto done;
3842 }
Dave Airlief453ba02008-11-07 14:05:41 -08003843
Rob Clark5ea22f22014-05-30 11:34:01 -04003844 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3845 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003846 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3847 enum_count++;
Rob Clark5ea22f22014-05-30 11:34:01 -04003848 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003849 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3850 blob_count++;
3851 }
3852
3853 value_count = property->num_values;
3854
3855 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3856 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3857 out_resp->flags = property->flags;
3858
3859 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003860 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003861 for (i = 0; i < value_count; i++) {
3862 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3863 ret = -EFAULT;
3864 goto done;
3865 }
3866 }
3867 }
3868 out_resp->count_values = value_count;
3869
Rob Clark5ea22f22014-05-30 11:34:01 -04003870 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3871 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003872 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3873 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003874 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003875 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3876
3877 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3878 ret = -EFAULT;
3879 goto done;
3880 }
3881
3882 if (copy_to_user(&enum_ptr[copied].name,
3883 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3884 ret = -EFAULT;
3885 goto done;
3886 }
3887 copied++;
3888 }
3889 }
3890 out_resp->count_enum_blobs = enum_count;
3891 }
3892
Rob Clark5ea22f22014-05-30 11:34:01 -04003893 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003894 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3895 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003896 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3897 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003898
3899 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3900 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3901 ret = -EFAULT;
3902 goto done;
3903 }
3904
3905 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3906 ret = -EFAULT;
3907 goto done;
3908 }
3909
3910 copied++;
3911 }
3912 }
3913 out_resp->count_enum_blobs = blob_count;
3914 }
3915done:
Daniel Vetter84849902012-12-02 00:28:11 +01003916 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003917 return ret;
3918}
3919
3920static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3921 void *data)
3922{
3923 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003924 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003925
3926 if (!length || !data)
3927 return NULL;
3928
3929 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3930 if (!blob)
3931 return NULL;
3932
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003933 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3934 if (ret) {
3935 kfree(blob);
3936 return NULL;
3937 }
3938
Dave Airlief453ba02008-11-07 14:05:41 -08003939 blob->length = length;
3940
3941 memcpy(blob->data, data, length);
3942
Dave Airlief453ba02008-11-07 14:05:41 -08003943 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3944 return blob;
3945}
3946
3947static void drm_property_destroy_blob(struct drm_device *dev,
3948 struct drm_property_blob *blob)
3949{
3950 drm_mode_object_put(dev, &blob->base);
3951 list_del(&blob->head);
3952 kfree(blob);
3953}
3954
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003955/**
3956 * drm_mode_getblob_ioctl - get the contents of a blob property value
3957 * @dev: DRM device
3958 * @data: ioctl data
3959 * @file_priv: DRM file info
3960 *
3961 * This function retrieves the contents of a blob property. The value stored in
3962 * an object's blob property is just a normal modeset object id.
3963 *
3964 * Called by the user via ioctl.
3965 *
3966 * Returns:
3967 * Zero on success, errno on failure.
3968 */
Dave Airlief453ba02008-11-07 14:05:41 -08003969int drm_mode_getblob_ioctl(struct drm_device *dev,
3970 void *data, struct drm_file *file_priv)
3971{
Dave Airlief453ba02008-11-07 14:05:41 -08003972 struct drm_mode_get_blob *out_resp = data;
3973 struct drm_property_blob *blob;
3974 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003975 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003976
Dave Airliefb3b06c2011-02-08 13:55:21 +10003977 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3978 return -EINVAL;
3979
Daniel Vetter84849902012-12-02 00:28:11 +01003980 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003981 blob = drm_property_blob_find(dev, out_resp->blob_id);
3982 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003983 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003984 goto done;
3985 }
Dave Airlief453ba02008-11-07 14:05:41 -08003986
3987 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003988 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003989 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3990 ret = -EFAULT;
3991 goto done;
3992 }
3993 }
3994 out_resp->length = blob->length;
3995
3996done:
Daniel Vetter84849902012-12-02 00:28:11 +01003997 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003998 return ret;
3999}
4000
Dave Airlie43aba7e2014-06-05 14:01:31 +10004001int drm_mode_connector_set_path_property(struct drm_connector *connector,
4002 char *path)
4003{
4004 struct drm_device *dev = connector->dev;
4005 int ret, size;
4006 size = strlen(path) + 1;
4007
4008 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4009 size, path);
4010 if (!connector->path_blob_ptr)
4011 return -EINVAL;
4012
4013 ret = drm_object_property_set_value(&connector->base,
4014 dev->mode_config.path_property,
4015 connector->path_blob_ptr->base.id);
4016 return ret;
4017}
4018EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4019
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004020/**
4021 * drm_mode_connector_update_edid_property - update the edid property of a connector
4022 * @connector: drm connector
4023 * @edid: new value of the edid property
4024 *
4025 * This function creates a new blob modeset object and assigns its id to the
4026 * connector's edid property.
4027 *
4028 * Returns:
4029 * Zero on success, errno on failure.
4030 */
Dave Airlief453ba02008-11-07 14:05:41 -08004031int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4032 struct edid *edid)
4033{
4034 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02004035 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08004036
Thomas Wood4cf2b282014-06-18 17:52:33 +01004037 /* ignore requests to set edid when overridden */
4038 if (connector->override_edid)
4039 return 0;
4040
Dave Airlief453ba02008-11-07 14:05:41 -08004041 if (connector->edid_blob_ptr)
4042 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4043
4044 /* Delete edid, when there is none. */
4045 if (!edid) {
4046 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05004047 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08004048 return ret;
4049 }
4050
Adam Jackson7466f4c2010-03-29 21:43:23 +00004051 size = EDID_LENGTH * (1 + edid->extensions);
4052 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4053 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00004054 if (!connector->edid_blob_ptr)
4055 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08004056
Rob Clark58495562012-10-11 20:50:56 -05004057 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08004058 dev->mode_config.edid_property,
4059 connector->edid_blob_ptr->base.id);
4060
4061 return ret;
4062}
4063EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4064
Paulo Zanoni26a34812012-05-15 18:08:59 -03004065static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004066 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004067{
4068 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4069 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004070
4071 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004072 if (value < property->values[0] || value > property->values[1])
4073 return false;
4074 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004075 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4076 int64_t svalue = U642I64(value);
4077 if (svalue < U642I64(property->values[0]) ||
4078 svalue > U642I64(property->values[1]))
4079 return false;
4080 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004081 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Rob Clark49e27542012-05-17 02:23:26 -06004082 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004083 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06004084 for (i = 0; i < property->num_values; i++)
4085 valid_mask |= (1ULL << property->values[i]);
4086 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004087 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004088 /* Only the driver knows */
4089 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004090 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4091 struct drm_mode_object *obj;
4092 /* a zero value for an object property translates to null: */
4093 if (value == 0)
4094 return true;
4095 /*
4096 * NOTE: use _object_find() directly to bypass restriction on
4097 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4098 * object this could race against object finalization, so it
4099 * simply tells us that the object *was* valid. Which is good
4100 * enough.
4101 */
4102 obj = _object_find(property->dev, value, property->values[0]);
4103 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004104 } else {
4105 int i;
4106 for (i = 0; i < property->num_values; i++)
4107 if (property->values[i] == value)
4108 return true;
4109 return false;
4110 }
4111}
4112
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004113/**
4114 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4115 * @dev: DRM device
4116 * @data: ioctl data
4117 * @file_priv: DRM file info
4118 *
4119 * This function sets the current value for a connectors's property. It also
4120 * calls into a driver's ->set_property callback to update the hardware state
4121 *
4122 * Called by the user via ioctl.
4123 *
4124 * Returns:
4125 * Zero on success, errno on failure.
4126 */
Dave Airlief453ba02008-11-07 14:05:41 -08004127int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4128 void *data, struct drm_file *file_priv)
4129{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004130 struct drm_mode_connector_set_property *conn_set_prop = data;
4131 struct drm_mode_obj_set_property obj_set_prop = {
4132 .value = conn_set_prop->value,
4133 .prop_id = conn_set_prop->prop_id,
4134 .obj_id = conn_set_prop->connector_id,
4135 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4136 };
Dave Airlief453ba02008-11-07 14:05:41 -08004137
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004138 /* It does all the locking and checking we need */
4139 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004140}
4141
Paulo Zanonic5431882012-05-15 18:09:02 -03004142static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4143 struct drm_property *property,
4144 uint64_t value)
4145{
4146 int ret = -EINVAL;
4147 struct drm_connector *connector = obj_to_connector(obj);
4148
4149 /* Do DPMS ourselves */
4150 if (property == connector->dev->mode_config.dpms_property) {
4151 if (connector->funcs->dpms)
4152 (*connector->funcs->dpms)(connector, (int)value);
4153 ret = 0;
4154 } else if (connector->funcs->set_property)
4155 ret = connector->funcs->set_property(connector, property, value);
4156
4157 /* store the property value if successful */
4158 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004159 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004160 return ret;
4161}
4162
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004163static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4164 struct drm_property *property,
4165 uint64_t value)
4166{
4167 int ret = -EINVAL;
4168 struct drm_crtc *crtc = obj_to_crtc(obj);
4169
4170 if (crtc->funcs->set_property)
4171 ret = crtc->funcs->set_property(crtc, property, value);
4172 if (!ret)
4173 drm_object_property_set_value(obj, property, value);
4174
4175 return ret;
4176}
4177
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004178/**
4179 * drm_mode_plane_set_obj_prop - set the value of a property
4180 * @plane: drm plane object to set property value for
4181 * @property: property to set
4182 * @value: value the property should be set to
4183 *
4184 * This functions sets a given property on a given plane object. This function
4185 * calls the driver's ->set_property callback and changes the software state of
4186 * the property if the callback succeeds.
4187 *
4188 * Returns:
4189 * Zero on success, error code on failure.
4190 */
4191int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4192 struct drm_property *property,
4193 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004194{
4195 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004196 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004197
4198 if (plane->funcs->set_property)
4199 ret = plane->funcs->set_property(plane, property, value);
4200 if (!ret)
4201 drm_object_property_set_value(obj, property, value);
4202
4203 return ret;
4204}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004205EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004206
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004207/**
4208 * drm_mode_getproperty_ioctl - get the current value of a object's property
4209 * @dev: DRM device
4210 * @data: ioctl data
4211 * @file_priv: DRM file info
4212 *
4213 * This function retrieves the current value for an object's property. Compared
4214 * to the connector specific ioctl this one is extended to also work on crtc and
4215 * plane objects.
4216 *
4217 * Called by the user via ioctl.
4218 *
4219 * Returns:
4220 * Zero on success, errno on failure.
4221 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004222int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4223 struct drm_file *file_priv)
4224{
4225 struct drm_mode_obj_get_properties *arg = data;
4226 struct drm_mode_object *obj;
4227 int ret = 0;
4228 int i;
4229 int copied = 0;
4230 int props_count = 0;
4231 uint32_t __user *props_ptr;
4232 uint64_t __user *prop_values_ptr;
4233
4234 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4235 return -EINVAL;
4236
Daniel Vetter84849902012-12-02 00:28:11 +01004237 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004238
4239 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4240 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004241 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004242 goto out;
4243 }
4244 if (!obj->properties) {
4245 ret = -EINVAL;
4246 goto out;
4247 }
4248
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004249 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004250
4251 /* This ioctl is called twice, once to determine how much space is
4252 * needed, and the 2nd time to fill it. */
4253 if ((arg->count_props >= props_count) && props_count) {
4254 copied = 0;
4255 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4256 prop_values_ptr = (uint64_t __user *)(unsigned long)
4257 (arg->prop_values_ptr);
4258 for (i = 0; i < props_count; i++) {
4259 if (put_user(obj->properties->ids[i],
4260 props_ptr + copied)) {
4261 ret = -EFAULT;
4262 goto out;
4263 }
4264 if (put_user(obj->properties->values[i],
4265 prop_values_ptr + copied)) {
4266 ret = -EFAULT;
4267 goto out;
4268 }
4269 copied++;
4270 }
4271 }
4272 arg->count_props = props_count;
4273out:
Daniel Vetter84849902012-12-02 00:28:11 +01004274 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004275 return ret;
4276}
4277
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004278/**
4279 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4280 * @dev: DRM device
4281 * @data: ioctl data
4282 * @file_priv: DRM file info
4283 *
4284 * This function sets the current value for an object's property. It also calls
4285 * into a driver's ->set_property callback to update the hardware state.
4286 * Compared to the connector specific ioctl this one is extended to also work on
4287 * crtc and plane objects.
4288 *
4289 * Called by the user via ioctl.
4290 *
4291 * Returns:
4292 * Zero on success, errno on failure.
4293 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004294int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4295 struct drm_file *file_priv)
4296{
4297 struct drm_mode_obj_set_property *arg = data;
4298 struct drm_mode_object *arg_obj;
4299 struct drm_mode_object *prop_obj;
4300 struct drm_property *property;
4301 int ret = -EINVAL;
4302 int i;
4303
4304 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4305 return -EINVAL;
4306
Daniel Vetter84849902012-12-02 00:28:11 +01004307 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004308
4309 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004310 if (!arg_obj) {
4311 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004312 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004313 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004314 if (!arg_obj->properties)
4315 goto out;
4316
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004317 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004318 if (arg_obj->properties->ids[i] == arg->prop_id)
4319 break;
4320
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004321 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004322 goto out;
4323
4324 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4325 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004326 if (!prop_obj) {
4327 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004328 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004329 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004330 property = obj_to_property(prop_obj);
4331
4332 if (!drm_property_change_is_valid(property, arg->value))
4333 goto out;
4334
4335 switch (arg_obj->type) {
4336 case DRM_MODE_OBJECT_CONNECTOR:
4337 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4338 arg->value);
4339 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004340 case DRM_MODE_OBJECT_CRTC:
4341 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4342 break;
Rob Clark4d939142012-05-17 02:23:27 -06004343 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004344 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4345 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06004346 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004347 }
4348
4349out:
Daniel Vetter84849902012-12-02 00:28:11 +01004350 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004351 return ret;
4352}
4353
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004354/**
4355 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4356 * @connector: connector to attach
4357 * @encoder: encoder to attach @connector to
4358 *
4359 * This function links up a connector to an encoder. Note that the routing
4360 * restrictions between encoders and crtcs are exposed to userspace through the
4361 * possible_clones and possible_crtcs bitmasks.
4362 *
4363 * Returns:
4364 * Zero on success, errno on failure.
4365 */
Dave Airlief453ba02008-11-07 14:05:41 -08004366int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4367 struct drm_encoder *encoder)
4368{
4369 int i;
4370
4371 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4372 if (connector->encoder_ids[i] == 0) {
4373 connector->encoder_ids[i] = encoder->base.id;
4374 return 0;
4375 }
4376 }
4377 return -ENOMEM;
4378}
4379EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4380
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004381/**
4382 * drm_mode_crtc_set_gamma_size - set the gamma table size
4383 * @crtc: CRTC to set the gamma table size for
4384 * @gamma_size: size of the gamma table
4385 *
4386 * Drivers which support gamma tables should set this to the supported gamma
4387 * table size when initializing the CRTC. Currently the drm core only supports a
4388 * fixed gamma table size.
4389 *
4390 * Returns:
4391 * Zero on success, errno on failure.
4392 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004393int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004394 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004395{
4396 crtc->gamma_size = gamma_size;
4397
4398 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4399 if (!crtc->gamma_store) {
4400 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004401 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004402 }
4403
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004404 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004405}
4406EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4407
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004408/**
4409 * drm_mode_gamma_set_ioctl - set the gamma table
4410 * @dev: DRM device
4411 * @data: ioctl data
4412 * @file_priv: DRM file info
4413 *
4414 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4415 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4416 *
4417 * Called by the user via ioctl.
4418 *
4419 * Returns:
4420 * Zero on success, errno on failure.
4421 */
Dave Airlief453ba02008-11-07 14:05:41 -08004422int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4423 void *data, struct drm_file *file_priv)
4424{
4425 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004426 struct drm_crtc *crtc;
4427 void *r_base, *g_base, *b_base;
4428 int size;
4429 int ret = 0;
4430
Dave Airliefb3b06c2011-02-08 13:55:21 +10004431 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4432 return -EINVAL;
4433
Daniel Vetter84849902012-12-02 00:28:11 +01004434 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004435 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4436 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004437 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004438 goto out;
4439 }
Dave Airlief453ba02008-11-07 14:05:41 -08004440
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004441 if (crtc->funcs->gamma_set == NULL) {
4442 ret = -ENOSYS;
4443 goto out;
4444 }
4445
Dave Airlief453ba02008-11-07 14:05:41 -08004446 /* memcpy into gamma store */
4447 if (crtc_lut->gamma_size != crtc->gamma_size) {
4448 ret = -EINVAL;
4449 goto out;
4450 }
4451
4452 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4453 r_base = crtc->gamma_store;
4454 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4455 ret = -EFAULT;
4456 goto out;
4457 }
4458
4459 g_base = r_base + size;
4460 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4461 ret = -EFAULT;
4462 goto out;
4463 }
4464
4465 b_base = g_base + size;
4466 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4467 ret = -EFAULT;
4468 goto out;
4469 }
4470
James Simmons72034252010-08-03 01:33:19 +01004471 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004472
4473out:
Daniel Vetter84849902012-12-02 00:28:11 +01004474 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004475 return ret;
4476
4477}
4478
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004479/**
4480 * drm_mode_gamma_get_ioctl - get the gamma table
4481 * @dev: DRM device
4482 * @data: ioctl data
4483 * @file_priv: DRM file info
4484 *
4485 * Copy the current gamma table into the storage provided. This also provides
4486 * the gamma table size the driver expects, which can be used to size the
4487 * allocated storage.
4488 *
4489 * Called by the user via ioctl.
4490 *
4491 * Returns:
4492 * Zero on success, errno on failure.
4493 */
Dave Airlief453ba02008-11-07 14:05:41 -08004494int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4495 void *data, struct drm_file *file_priv)
4496{
4497 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004498 struct drm_crtc *crtc;
4499 void *r_base, *g_base, *b_base;
4500 int size;
4501 int ret = 0;
4502
Dave Airliefb3b06c2011-02-08 13:55:21 +10004503 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4504 return -EINVAL;
4505
Daniel Vetter84849902012-12-02 00:28:11 +01004506 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004507 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4508 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004509 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004510 goto out;
4511 }
Dave Airlief453ba02008-11-07 14:05:41 -08004512
4513 /* memcpy into gamma store */
4514 if (crtc_lut->gamma_size != crtc->gamma_size) {
4515 ret = -EINVAL;
4516 goto out;
4517 }
4518
4519 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4520 r_base = crtc->gamma_store;
4521 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4522 ret = -EFAULT;
4523 goto out;
4524 }
4525
4526 g_base = r_base + size;
4527 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4528 ret = -EFAULT;
4529 goto out;
4530 }
4531
4532 b_base = g_base + size;
4533 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4534 ret = -EFAULT;
4535 goto out;
4536 }
4537out:
Daniel Vetter84849902012-12-02 00:28:11 +01004538 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004539 return ret;
4540}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004541
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004542/**
4543 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4544 * @dev: DRM device
4545 * @data: ioctl data
4546 * @file_priv: DRM file info
4547 *
4548 * This schedules an asynchronous update on a given CRTC, called page flip.
4549 * Optionally a drm event is generated to signal the completion of the event.
4550 * Generic drivers cannot assume that a pageflip with changed framebuffer
4551 * properties (including driver specific metadata like tiling layout) will work,
4552 * but some drivers support e.g. pixel format changes through the pageflip
4553 * ioctl.
4554 *
4555 * Called by the user via ioctl.
4556 *
4557 * Returns:
4558 * Zero on success, errno on failure.
4559 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004560int drm_mode_page_flip_ioctl(struct drm_device *dev,
4561 void *data, struct drm_file *file_priv)
4562{
4563 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004564 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02004565 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004566 struct drm_pending_vblank_event *e = NULL;
4567 unsigned long flags;
4568 int ret = -EINVAL;
4569
4570 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4571 page_flip->reserved != 0)
4572 return -EINVAL;
4573
Keith Packard62f21042013-07-22 18:50:00 -07004574 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4575 return -EINVAL;
4576
Rob Clarka2b34e22013-10-05 16:36:52 -04004577 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4578 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004579 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004580
Daniel Vetterd059f652014-07-25 18:07:40 +02004581 drm_modeset_lock_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07004582 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004583 /* The framebuffer is currently unbound, presumably
4584 * due to a hotplug event, that userspace has not
4585 * yet discovered.
4586 */
4587 ret = -EBUSY;
4588 goto out;
4589 }
4590
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004591 if (crtc->funcs->page_flip == NULL)
4592 goto out;
4593
Daniel Vetter786b99e2012-12-02 21:53:40 +01004594 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004595 if (!fb) {
4596 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004597 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004598 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004599
Damien Lespiauc11e9282013-09-25 16:45:30 +01004600 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4601 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004602 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004603
Matt Roperf4510a22014-04-01 15:22:40 -07004604 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004605 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4606 ret = -EINVAL;
4607 goto out;
4608 }
4609
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004610 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4611 ret = -ENOMEM;
4612 spin_lock_irqsave(&dev->event_lock, flags);
4613 if (file_priv->event_space < sizeof e->event) {
4614 spin_unlock_irqrestore(&dev->event_lock, flags);
4615 goto out;
4616 }
4617 file_priv->event_space -= sizeof e->event;
4618 spin_unlock_irqrestore(&dev->event_lock, flags);
4619
4620 e = kzalloc(sizeof *e, GFP_KERNEL);
4621 if (e == NULL) {
4622 spin_lock_irqsave(&dev->event_lock, flags);
4623 file_priv->event_space += sizeof e->event;
4624 spin_unlock_irqrestore(&dev->event_lock, flags);
4625 goto out;
4626 }
4627
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004628 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004629 e->event.base.length = sizeof e->event;
4630 e->event.user_data = page_flip->user_data;
4631 e->base.event = &e->event.base;
4632 e->base.file_priv = file_priv;
4633 e->base.destroy =
4634 (void (*) (struct drm_pending_event *)) kfree;
4635 }
4636
Daniel Vetter3d30a592014-07-27 13:42:42 +02004637 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004638 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004639 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004640 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4641 spin_lock_irqsave(&dev->event_lock, flags);
4642 file_priv->event_space += sizeof e->event;
4643 spin_unlock_irqrestore(&dev->event_lock, flags);
4644 kfree(e);
4645 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004646 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02004647 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004648 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004649 /*
4650 * Warn if the driver hasn't properly updated the crtc->fb
4651 * field to reflect that the new framebuffer is now used.
4652 * Failing to do so will screw with the reference counting
4653 * on framebuffers.
4654 */
Matt Roperf4510a22014-04-01 15:22:40 -07004655 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004656 /* Unref only the old framebuffer. */
4657 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004658 }
4659
4660out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004661 if (fb)
4662 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02004663 if (crtc->primary->old_fb)
4664 drm_framebuffer_unreference(crtc->primary->old_fb);
4665 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02004666 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004667
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004668 return ret;
4669}
Chris Wilsoneb033552011-01-24 15:11:08 +00004670
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004671/**
4672 * drm_mode_config_reset - call ->reset callbacks
4673 * @dev: drm device
4674 *
4675 * This functions calls all the crtc's, encoder's and connector's ->reset
4676 * callback. Drivers can use this in e.g. their driver load or resume code to
4677 * reset hardware and software state.
4678 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004679void drm_mode_config_reset(struct drm_device *dev)
4680{
4681 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004682 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00004683 struct drm_encoder *encoder;
4684 struct drm_connector *connector;
4685
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004686 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4687 if (plane->funcs->reset)
4688 plane->funcs->reset(plane);
4689
Chris Wilsoneb033552011-01-24 15:11:08 +00004690 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4691 if (crtc->funcs->reset)
4692 crtc->funcs->reset(crtc);
4693
4694 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4695 if (encoder->funcs->reset)
4696 encoder->funcs->reset(encoder);
4697
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004698 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4699 connector->status = connector_status_unknown;
4700
Chris Wilsoneb033552011-01-24 15:11:08 +00004701 if (connector->funcs->reset)
4702 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004703 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004704}
4705EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004706
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004707/**
4708 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4709 * @dev: DRM device
4710 * @data: ioctl data
4711 * @file_priv: DRM file info
4712 *
4713 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4714 * TTM or something else entirely) and returns the resulting buffer handle. This
4715 * handle can then be wrapped up into a framebuffer modeset object.
4716 *
4717 * Note that userspace is not allowed to use such objects for render
4718 * acceleration - drivers must create their own private ioctls for such a use
4719 * case.
4720 *
4721 * Called by the user via ioctl.
4722 *
4723 * Returns:
4724 * Zero on success, errno on failure.
4725 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004726int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4727 void *data, struct drm_file *file_priv)
4728{
4729 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004730 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004731
4732 if (!dev->driver->dumb_create)
4733 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004734 if (!args->width || !args->height || !args->bpp)
4735 return -EINVAL;
4736
4737 /* overflow checks for 32bit size calculations */
4738 cpp = DIV_ROUND_UP(args->bpp, 8);
4739 if (cpp > 0xffffffffU / args->width)
4740 return -EINVAL;
4741 stride = cpp * args->width;
4742 if (args->height > 0xffffffffU / stride)
4743 return -EINVAL;
4744
4745 /* test for wrap-around */
4746 size = args->height * stride;
4747 if (PAGE_ALIGN(size) == 0)
4748 return -EINVAL;
4749
Dave Airlieff72145b2011-02-07 12:16:14 +10004750 return dev->driver->dumb_create(file_priv, dev, args);
4751}
4752
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004753/**
4754 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4755 * @dev: DRM device
4756 * @data: ioctl data
4757 * @file_priv: DRM file info
4758 *
4759 * Allocate an offset in the drm device node's address space to be able to
4760 * memory map a dumb buffer.
4761 *
4762 * Called by the user via ioctl.
4763 *
4764 * Returns:
4765 * Zero on success, errno on failure.
4766 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004767int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4768 void *data, struct drm_file *file_priv)
4769{
4770 struct drm_mode_map_dumb *args = data;
4771
4772 /* call driver ioctl to get mmap offset */
4773 if (!dev->driver->dumb_map_offset)
4774 return -ENOSYS;
4775
4776 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4777}
4778
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004779/**
4780 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4781 * @dev: DRM device
4782 * @data: ioctl data
4783 * @file_priv: DRM file info
4784 *
4785 * This destroys the userspace handle for the given dumb backing storage buffer.
4786 * Since buffer objects must be reference counted in the kernel a buffer object
4787 * won't be immediately freed if a framebuffer modeset object still uses it.
4788 *
4789 * Called by the user via ioctl.
4790 *
4791 * Returns:
4792 * Zero on success, errno on failure.
4793 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004794int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4795 void *data, struct drm_file *file_priv)
4796{
4797 struct drm_mode_destroy_dumb *args = data;
4798
4799 if (!dev->driver->dumb_destroy)
4800 return -ENOSYS;
4801
4802 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4803}
Dave Airlie248dbc22011-11-29 20:02:54 +00004804
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004805/**
4806 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4807 * @format: pixel format (DRM_FORMAT_*)
4808 * @depth: storage for the depth value
4809 * @bpp: storage for the bpp value
4810 *
4811 * This only supports RGB formats here for compat with code that doesn't use
4812 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004813 */
4814void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4815 int *bpp)
4816{
4817 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004818 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004819 case DRM_FORMAT_RGB332:
4820 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004821 *depth = 8;
4822 *bpp = 8;
4823 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004824 case DRM_FORMAT_XRGB1555:
4825 case DRM_FORMAT_XBGR1555:
4826 case DRM_FORMAT_RGBX5551:
4827 case DRM_FORMAT_BGRX5551:
4828 case DRM_FORMAT_ARGB1555:
4829 case DRM_FORMAT_ABGR1555:
4830 case DRM_FORMAT_RGBA5551:
4831 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004832 *depth = 15;
4833 *bpp = 16;
4834 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004835 case DRM_FORMAT_RGB565:
4836 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004837 *depth = 16;
4838 *bpp = 16;
4839 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004840 case DRM_FORMAT_RGB888:
4841 case DRM_FORMAT_BGR888:
4842 *depth = 24;
4843 *bpp = 24;
4844 break;
4845 case DRM_FORMAT_XRGB8888:
4846 case DRM_FORMAT_XBGR8888:
4847 case DRM_FORMAT_RGBX8888:
4848 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004849 *depth = 24;
4850 *bpp = 32;
4851 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004852 case DRM_FORMAT_XRGB2101010:
4853 case DRM_FORMAT_XBGR2101010:
4854 case DRM_FORMAT_RGBX1010102:
4855 case DRM_FORMAT_BGRX1010102:
4856 case DRM_FORMAT_ARGB2101010:
4857 case DRM_FORMAT_ABGR2101010:
4858 case DRM_FORMAT_RGBA1010102:
4859 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004860 *depth = 30;
4861 *bpp = 32;
4862 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004863 case DRM_FORMAT_ARGB8888:
4864 case DRM_FORMAT_ABGR8888:
4865 case DRM_FORMAT_RGBA8888:
4866 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004867 *depth = 32;
4868 *bpp = 32;
4869 break;
4870 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004871 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4872 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004873 *depth = 0;
4874 *bpp = 0;
4875 break;
4876 }
4877}
4878EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004879
4880/**
4881 * drm_format_num_planes - get the number of planes for format
4882 * @format: pixel format (DRM_FORMAT_*)
4883 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004884 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004885 * The number of planes used by the specified pixel format.
4886 */
4887int drm_format_num_planes(uint32_t format)
4888{
4889 switch (format) {
4890 case DRM_FORMAT_YUV410:
4891 case DRM_FORMAT_YVU410:
4892 case DRM_FORMAT_YUV411:
4893 case DRM_FORMAT_YVU411:
4894 case DRM_FORMAT_YUV420:
4895 case DRM_FORMAT_YVU420:
4896 case DRM_FORMAT_YUV422:
4897 case DRM_FORMAT_YVU422:
4898 case DRM_FORMAT_YUV444:
4899 case DRM_FORMAT_YVU444:
4900 return 3;
4901 case DRM_FORMAT_NV12:
4902 case DRM_FORMAT_NV21:
4903 case DRM_FORMAT_NV16:
4904 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004905 case DRM_FORMAT_NV24:
4906 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004907 return 2;
4908 default:
4909 return 1;
4910 }
4911}
4912EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004913
4914/**
4915 * drm_format_plane_cpp - determine the bytes per pixel value
4916 * @format: pixel format (DRM_FORMAT_*)
4917 * @plane: plane index
4918 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004919 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004920 * The bytes per pixel value for the specified plane.
4921 */
4922int drm_format_plane_cpp(uint32_t format, int plane)
4923{
4924 unsigned int depth;
4925 int bpp;
4926
4927 if (plane >= drm_format_num_planes(format))
4928 return 0;
4929
4930 switch (format) {
4931 case DRM_FORMAT_YUYV:
4932 case DRM_FORMAT_YVYU:
4933 case DRM_FORMAT_UYVY:
4934 case DRM_FORMAT_VYUY:
4935 return 2;
4936 case DRM_FORMAT_NV12:
4937 case DRM_FORMAT_NV21:
4938 case DRM_FORMAT_NV16:
4939 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004940 case DRM_FORMAT_NV24:
4941 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03004942 return plane ? 2 : 1;
4943 case DRM_FORMAT_YUV410:
4944 case DRM_FORMAT_YVU410:
4945 case DRM_FORMAT_YUV411:
4946 case DRM_FORMAT_YVU411:
4947 case DRM_FORMAT_YUV420:
4948 case DRM_FORMAT_YVU420:
4949 case DRM_FORMAT_YUV422:
4950 case DRM_FORMAT_YVU422:
4951 case DRM_FORMAT_YUV444:
4952 case DRM_FORMAT_YVU444:
4953 return 1;
4954 default:
4955 drm_fb_get_bpp_depth(format, &depth, &bpp);
4956 return bpp >> 3;
4957 }
4958}
4959EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004960
4961/**
4962 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4963 * @format: pixel format (DRM_FORMAT_*)
4964 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004965 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004966 * The horizontal chroma subsampling factor for the
4967 * specified pixel format.
4968 */
4969int drm_format_horz_chroma_subsampling(uint32_t format)
4970{
4971 switch (format) {
4972 case DRM_FORMAT_YUV411:
4973 case DRM_FORMAT_YVU411:
4974 case DRM_FORMAT_YUV410:
4975 case DRM_FORMAT_YVU410:
4976 return 4;
4977 case DRM_FORMAT_YUYV:
4978 case DRM_FORMAT_YVYU:
4979 case DRM_FORMAT_UYVY:
4980 case DRM_FORMAT_VYUY:
4981 case DRM_FORMAT_NV12:
4982 case DRM_FORMAT_NV21:
4983 case DRM_FORMAT_NV16:
4984 case DRM_FORMAT_NV61:
4985 case DRM_FORMAT_YUV422:
4986 case DRM_FORMAT_YVU422:
4987 case DRM_FORMAT_YUV420:
4988 case DRM_FORMAT_YVU420:
4989 return 2;
4990 default:
4991 return 1;
4992 }
4993}
4994EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4995
4996/**
4997 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4998 * @format: pixel format (DRM_FORMAT_*)
4999 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005000 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005001 * The vertical chroma subsampling factor for the
5002 * specified pixel format.
5003 */
5004int drm_format_vert_chroma_subsampling(uint32_t format)
5005{
5006 switch (format) {
5007 case DRM_FORMAT_YUV410:
5008 case DRM_FORMAT_YVU410:
5009 return 4;
5010 case DRM_FORMAT_YUV420:
5011 case DRM_FORMAT_YVU420:
5012 case DRM_FORMAT_NV12:
5013 case DRM_FORMAT_NV21:
5014 return 2;
5015 default:
5016 return 1;
5017 }
5018}
5019EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005020
5021/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305022 * drm_rotation_simplify() - Try to simplify the rotation
5023 * @rotation: Rotation to be simplified
5024 * @supported_rotations: Supported rotations
5025 *
5026 * Attempt to simplify the rotation to a form that is supported.
5027 * Eg. if the hardware supports everything except DRM_REFLECT_X
5028 * one could call this function like this:
5029 *
5030 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5031 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5032 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5033 *
5034 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5035 * transforms the hardware supports, this function may not
5036 * be able to produce a supported transform, so the caller should
5037 * check the result afterwards.
5038 */
5039unsigned int drm_rotation_simplify(unsigned int rotation,
5040 unsigned int supported_rotations)
5041{
5042 if (rotation & ~supported_rotations) {
5043 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5044 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5045 }
5046
5047 return rotation;
5048}
5049EXPORT_SYMBOL(drm_rotation_simplify);
5050
5051/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005052 * drm_mode_config_init - initialize DRM mode_configuration structure
5053 * @dev: DRM device
5054 *
5055 * Initialize @dev's mode_config structure, used for tracking the graphics
5056 * configuration of @dev.
5057 *
5058 * Since this initializes the modeset locks, no locking is possible. Which is no
5059 * problem, since this should happen single threaded at init time. It is the
5060 * driver's problem to ensure this guarantee.
5061 *
5062 */
5063void drm_mode_config_init(struct drm_device *dev)
5064{
5065 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005066 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005067 mutex_init(&dev->mode_config.idr_mutex);
5068 mutex_init(&dev->mode_config.fb_lock);
5069 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5070 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5071 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04005072 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005073 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5074 INIT_LIST_HEAD(&dev->mode_config.property_list);
5075 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5076 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5077 idr_init(&dev->mode_config.crtc_idr);
5078
5079 drm_modeset_lock_all(dev);
5080 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04005081 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005082 drm_modeset_unlock_all(dev);
5083
5084 /* Just to be sure */
5085 dev->mode_config.num_fb = 0;
5086 dev->mode_config.num_connector = 0;
5087 dev->mode_config.num_crtc = 0;
5088 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005089 dev->mode_config.num_overlay_plane = 0;
5090 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005091}
5092EXPORT_SYMBOL(drm_mode_config_init);
5093
5094/**
5095 * drm_mode_config_cleanup - free up DRM mode_config info
5096 * @dev: DRM device
5097 *
5098 * Free up all the connectors and CRTCs associated with this DRM device, then
5099 * free up the framebuffers and associated buffer objects.
5100 *
5101 * Note that since this /should/ happen single-threaded at driver/device
5102 * teardown time, no locking is required. It's the driver's job to ensure that
5103 * this guarantee actually holds true.
5104 *
5105 * FIXME: cleanup any dangling user buffer objects too
5106 */
5107void drm_mode_config_cleanup(struct drm_device *dev)
5108{
5109 struct drm_connector *connector, *ot;
5110 struct drm_crtc *crtc, *ct;
5111 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04005112 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005113 struct drm_framebuffer *fb, *fbt;
5114 struct drm_property *property, *pt;
5115 struct drm_property_blob *blob, *bt;
5116 struct drm_plane *plane, *plt;
5117
5118 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5119 head) {
5120 encoder->funcs->destroy(encoder);
5121 }
5122
Sean Paul3b336ec2013-08-14 16:47:37 -04005123 list_for_each_entry_safe(bridge, brt,
5124 &dev->mode_config.bridge_list, head) {
5125 bridge->funcs->destroy(bridge);
5126 }
5127
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005128 list_for_each_entry_safe(connector, ot,
5129 &dev->mode_config.connector_list, head) {
5130 connector->funcs->destroy(connector);
5131 }
5132
5133 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5134 head) {
5135 drm_property_destroy(dev, property);
5136 }
5137
5138 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5139 head) {
5140 drm_property_destroy_blob(dev, blob);
5141 }
5142
5143 /*
5144 * Single-threaded teardown context, so it's not required to grab the
5145 * fb_lock to protect against concurrent fb_list access. Contrary, it
5146 * would actually deadlock with the drm_framebuffer_cleanup function.
5147 *
5148 * Also, if there are any framebuffers left, that's a driver leak now,
5149 * so politely WARN about this.
5150 */
5151 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5152 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5153 drm_framebuffer_remove(fb);
5154 }
5155
5156 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5157 head) {
5158 plane->funcs->destroy(plane);
5159 }
5160
5161 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5162 crtc->funcs->destroy(crtc);
5163 }
5164
5165 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005166 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005167}
5168EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305169
5170struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5171 unsigned int supported_rotations)
5172{
5173 static const struct drm_prop_enum_list props[] = {
5174 { DRM_ROTATE_0, "rotate-0" },
5175 { DRM_ROTATE_90, "rotate-90" },
5176 { DRM_ROTATE_180, "rotate-180" },
5177 { DRM_ROTATE_270, "rotate-270" },
5178 { DRM_REFLECT_X, "reflect-x" },
5179 { DRM_REFLECT_Y, "reflect-y" },
5180 };
5181
5182 return drm_property_create_bitmask(dev, 0, "rotation",
5183 props, ARRAY_SIZE(props),
5184 supported_rotations);
5185}
5186EXPORT_SYMBOL(drm_mode_create_rotation_property);