blob: 2031ead208b1e15e608b34e6b517ce5f3b70e4ef [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"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020043#include "drm_internal.h"
Daniel Vetter8bd441b2014-01-23 15:35:24 +010044
Matt Roperc394c2b2014-06-10 08:28:08 -070045static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
46 struct drm_mode_fb_cmd2 *r,
47 struct drm_file *file_priv);
48
Dave Airlief453ba02008-11-07 14:05:41 -080049/* Avoid boilerplate. I'm tired of typing. */
50#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000051 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080052 { \
53 int i; \
54 for (i = 0; i < ARRAY_SIZE(list); i++) { \
55 if (list[i].type == val) \
56 return list[i].name; \
57 } \
58 return "(unknown)"; \
59 }
60
61/*
62 * Global properties
63 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010064static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
65 { DRM_MODE_DPMS_ON, "On" },
Dave Airlief453ba02008-11-07 14:05:41 -080066 { DRM_MODE_DPMS_STANDBY, "Standby" },
67 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
68 { DRM_MODE_DPMS_OFF, "Off" }
69};
70
71DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
72
Thierry Reding4dfd9092014-12-10 13:03:34 +010073static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
Rob Clark9922ab52014-04-01 20:16:57 -040074 { 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 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010082static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
Jesse Barnes53bd8382009-07-01 10:04:40 -070083 { DRM_MODE_SCALE_NONE, "None" },
84 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
85 { DRM_MODE_SCALE_CENTER, "Center" },
86 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080087};
88
Vandana Kannanff587e42014-06-11 10:46:48 +053089static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
90 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
91 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
92 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
93};
94
Dave Airlief453ba02008-11-07 14:05:41 -080095/*
96 * Non-global properties, but "required" for certain connectors.
97 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010098static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -080099 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
100 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
101 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
102};
103
104DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
105
Thierry Reding4dfd9092014-12-10 13:03:34 +0100106static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800107 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
109 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
110};
111
112DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
113 drm_dvi_i_subconnector_enum_list)
114
Thierry Reding4dfd9092014-12-10 13:03:34 +0100115static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800116 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
117 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
118 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200120 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800121};
122
123DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
124
Thierry Reding4dfd9092014-12-10 13:03:34 +0100125static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800126 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
127 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
128 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
129 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200130 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800131};
132
133DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
134 drm_tv_subconnector_enum_list)
135
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000136static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000137 { DRM_MODE_DIRTY_OFF, "Off" },
138 { DRM_MODE_DIRTY_ON, "On" },
139 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
140};
141
Dave Airlief453ba02008-11-07 14:05:41 -0800142struct drm_conn_prop_enum_list {
143 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000144 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400145 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800146};
147
148/*
149 * Connector and encoder types.
150 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100151static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
152 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400153 { DRM_MODE_CONNECTOR_VGA, "VGA" },
154 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
155 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
156 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
157 { DRM_MODE_CONNECTOR_Composite, "Composite" },
158 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
159 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
160 { DRM_MODE_CONNECTOR_Component, "Component" },
161 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
162 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
163 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
164 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
165 { DRM_MODE_CONNECTOR_TV, "TV" },
166 { DRM_MODE_CONNECTOR_eDP, "eDP" },
167 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300168 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800169};
170
Thierry Reding4dfd9092014-12-10 13:03:34 +0100171static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
172 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -0800173 { DRM_MODE_ENCODER_DAC, "DAC" },
174 { DRM_MODE_ENCODER_TMDS, "TMDS" },
175 { DRM_MODE_ENCODER_LVDS, "LVDS" },
176 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200177 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300178 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000179 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800180};
181
Thierry Reding4dfd9092014-12-10 13:03:34 +0100182static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
Jesse Barnesac1bb362014-02-10 15:32:44 -0800183 { SubPixelUnknown, "Unknown" },
184 { SubPixelHorizontalRGB, "Horizontal RGB" },
185 { SubPixelHorizontalBGR, "Horizontal BGR" },
186 { SubPixelVerticalRGB, "Vertical RGB" },
187 { SubPixelVerticalBGR, "Vertical BGR" },
188 { SubPixelNone, "None" },
189};
190
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400191void drm_connector_ida_init(void)
192{
193 int i;
194
195 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
196 ida_init(&drm_connector_enum_list[i].ida);
197}
198
199void drm_connector_ida_destroy(void)
200{
201 int i;
202
203 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
204 ida_destroy(&drm_connector_enum_list[i].ida);
205}
206
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100207/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100208 * drm_get_connector_status_name - return a string for connector status
209 * @status: connector status to compute name of
210 *
211 * In contrast to the other drm_get_*_name functions this one here returns a
212 * const pointer and hence is threadsafe.
213 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000214const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800215{
216 if (status == connector_status_connected)
217 return "connected";
218 else if (status == connector_status_disconnected)
219 return "disconnected";
220 else
221 return "unknown";
222}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000223EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800224
Jesse Barnesac1bb362014-02-10 15:32:44 -0800225/**
226 * drm_get_subpixel_order_name - return a string for a given subpixel enum
227 * @order: enum of subpixel_order
228 *
229 * Note you could abuse this and return something out of bounds, but that
230 * would be a caller error. No unscrubbed user data should make it here.
231 */
232const char *drm_get_subpixel_order_name(enum subpixel_order order)
233{
234 return drm_subpixel_enum_list[order].name;
235}
236EXPORT_SYMBOL(drm_get_subpixel_order_name);
237
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300238static char printable_char(int c)
239{
240 return isascii(c) && isprint(c) ? c : '?';
241}
242
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100243/**
244 * drm_get_format_name - return a string for drm fourcc format
245 * @format: format to compute name of
246 *
247 * Note that the buffer used by this function is globally shared and owned by
248 * the function itself.
249 *
250 * FIXME: This isn't really multithreading safe.
251 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000252const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300253{
254 static char buf[32];
255
256 snprintf(buf, sizeof(buf),
257 "%c%c%c%c %s-endian (0x%08x)",
258 printable_char(format & 0xff),
259 printable_char((format >> 8) & 0xff),
260 printable_char((format >> 16) & 0xff),
261 printable_char((format >> 24) & 0x7f),
262 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
263 format);
264
265 return buf;
266}
267EXPORT_SYMBOL(drm_get_format_name);
268
Dave Airlie2ee39452014-07-24 11:53:45 +1000269/*
270 * Internal function to assign a slot in the object idr and optionally
271 * register the object into the idr.
272 */
273static int drm_mode_object_get_reg(struct drm_device *dev,
274 struct drm_mode_object *obj,
275 uint32_t obj_type,
276 bool register_obj)
277{
278 int ret;
279
280 mutex_lock(&dev->mode_config.idr_mutex);
281 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
282 if (ret >= 0) {
283 /*
284 * Set up the object linking under the protection of the idr
285 * lock so that other users can't see inconsistent state.
286 */
287 obj->id = ret;
288 obj->type = obj_type;
289 }
290 mutex_unlock(&dev->mode_config.idr_mutex);
291
292 return ret < 0 ? ret : 0;
293}
294
Dave Airlief453ba02008-11-07 14:05:41 -0800295/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100296 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800297 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100298 * @obj: object pointer, used to generate unique ID
299 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800300 *
Dave Airlief453ba02008-11-07 14:05:41 -0800301 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100302 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
303 * modeset identifiers are _not_ reference counted. Hence don't use this for
304 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800305 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100306 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800307 * New unique (relative to other objects in @dev) integer identifier for the
308 * object.
309 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100310int drm_mode_object_get(struct drm_device *dev,
311 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800312{
Dave Airlie2ee39452014-07-24 11:53:45 +1000313 return drm_mode_object_get_reg(dev, obj, obj_type, true);
314}
Dave Airlief453ba02008-11-07 14:05:41 -0800315
Dave Airlie2ee39452014-07-24 11:53:45 +1000316static void drm_mode_object_register(struct drm_device *dev,
317 struct drm_mode_object *obj)
318{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000319 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000320 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000321 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800322}
323
324/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100325 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800326 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100327 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800328 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100329 * Free @id from @dev's unique identifier pool. Note that despite the _get
330 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
331 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800332 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100333void drm_mode_object_put(struct drm_device *dev,
334 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800335{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000336 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800337 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000338 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800339}
340
Rob Clark98f75de2014-05-30 11:37:03 -0400341static struct drm_mode_object *_object_find(struct drm_device *dev,
342 uint32_t id, uint32_t type)
343{
344 struct drm_mode_object *obj = NULL;
345
346 mutex_lock(&dev->mode_config.idr_mutex);
347 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200348 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
349 obj = NULL;
350 if (obj && obj->id != id)
351 obj = NULL;
352 /* don't leak out unref'd fb's */
353 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Rob Clark98f75de2014-05-30 11:37:03 -0400354 obj = NULL;
355 mutex_unlock(&dev->mode_config.idr_mutex);
356
357 return obj;
358}
359
Daniel Vetter786b99e2012-12-02 21:53:40 +0100360/**
361 * drm_mode_object_find - look up a drm object with static lifetime
362 * @dev: drm device
363 * @id: id of the mode object
364 * @type: type of the mode object
365 *
366 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400367 * are reference counted, they need special treatment. Even with
368 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
369 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100370 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200371struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
372 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800373{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000374 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800375
Daniel Vetter786b99e2012-12-02 21:53:40 +0100376 /* Framebuffers are reference counted and need their own lookup
377 * function.*/
378 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400379 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800380 return obj;
381}
382EXPORT_SYMBOL(drm_mode_object_find);
383
384/**
Dave Airlief453ba02008-11-07 14:05:41 -0800385 * drm_framebuffer_init - initialize a framebuffer
386 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100387 * @fb: framebuffer to be initialized
388 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800389 *
Dave Airlief453ba02008-11-07 14:05:41 -0800390 * Allocates an ID for the framebuffer's parent mode object, sets its mode
391 * functions & device file and adds it to the master fd list.
392 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100393 * IMPORTANT:
394 * This functions publishes the fb and makes it available for concurrent access
395 * by other users. Which means by this point the fb _must_ be fully set up -
396 * since all the fb attributes are invariant over its lifetime, no further
397 * locking but only correct reference counting is required.
398 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100399 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200400 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800401 */
402int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
403 const struct drm_framebuffer_funcs *funcs)
404{
405 int ret;
406
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100407 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000408 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100409 INIT_LIST_HEAD(&fb->filp_head);
410 fb->dev = dev;
411 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000412
Dave Airlief453ba02008-11-07 14:05:41 -0800413 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200414 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100415 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800416
Dave Airlief453ba02008-11-07 14:05:41 -0800417 dev->mode_config.num_fb++;
418 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100419out:
420 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800421
422 return 0;
423}
424EXPORT_SYMBOL(drm_framebuffer_init);
425
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200426/* dev->mode_config.fb_lock must be held! */
427static void __drm_framebuffer_unregister(struct drm_device *dev,
428 struct drm_framebuffer *fb)
429{
430 mutex_lock(&dev->mode_config.idr_mutex);
431 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
432 mutex_unlock(&dev->mode_config.idr_mutex);
433
434 fb->base.id = 0;
435}
436
Rob Clarkf7eff602012-09-05 21:48:38 +0000437static void drm_framebuffer_free(struct kref *kref)
438{
439 struct drm_framebuffer *fb =
440 container_of(kref, struct drm_framebuffer, refcount);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200441 struct drm_device *dev = fb->dev;
442
443 /*
444 * The lookup idr holds a weak reference, which has not necessarily been
445 * removed at this point. Check for that.
446 */
447 mutex_lock(&dev->mode_config.fb_lock);
448 if (fb->base.id) {
449 /* Mark fb as reaped and drop idr ref. */
450 __drm_framebuffer_unregister(dev, fb);
451 }
452 mutex_unlock(&dev->mode_config.fb_lock);
453
Rob Clarkf7eff602012-09-05 21:48:38 +0000454 fb->funcs->destroy(fb);
455}
456
Daniel Vetter2b677e82012-12-10 21:16:05 +0100457static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
458 uint32_t id)
459{
460 struct drm_mode_object *obj = NULL;
461 struct drm_framebuffer *fb;
462
463 mutex_lock(&dev->mode_config.idr_mutex);
464 obj = idr_find(&dev->mode_config.crtc_idr, id);
465 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
466 fb = NULL;
467 else
468 fb = obj_to_fb(obj);
469 mutex_unlock(&dev->mode_config.idr_mutex);
470
471 return fb;
472}
473
Rob Clarkf7eff602012-09-05 21:48:38 +0000474/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100475 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
476 * @dev: drm device
477 * @id: id of the fb object
478 *
479 * If successful, this grabs an additional reference to the framebuffer -
480 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100481 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100482 */
483struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
484 uint32_t id)
485{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100486 struct drm_framebuffer *fb;
487
488 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100489 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200490 if (fb) {
491 if (!kref_get_unless_zero(&fb->refcount))
492 fb = NULL;
493 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100494 mutex_unlock(&dev->mode_config.fb_lock);
495
496 return fb;
497}
498EXPORT_SYMBOL(drm_framebuffer_lookup);
499
500/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000501 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100502 * @fb: framebuffer to unref
503 *
504 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000505 */
506void drm_framebuffer_unreference(struct drm_framebuffer *fb)
507{
Rob Clark82912722014-03-18 10:07:08 -0400508 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000509 kref_put(&fb->refcount, drm_framebuffer_free);
510}
511EXPORT_SYMBOL(drm_framebuffer_unreference);
512
513/**
514 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100515 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100516 *
517 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000518 */
519void drm_framebuffer_reference(struct drm_framebuffer *fb)
520{
Rob Clark82912722014-03-18 10:07:08 -0400521 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000522 kref_get(&fb->refcount);
523}
524EXPORT_SYMBOL(drm_framebuffer_reference);
525
Daniel Vetter2b677e82012-12-10 21:16:05 +0100526static void drm_framebuffer_free_bug(struct kref *kref)
527{
528 BUG();
529}
530
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100531static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
532{
Rob Clark82912722014-03-18 10:07:08 -0400533 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100534 kref_put(&fb->refcount, drm_framebuffer_free_bug);
535}
536
Dave Airlief453ba02008-11-07 14:05:41 -0800537/**
Daniel Vetter36206362012-12-10 20:42:17 +0100538 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
539 * @fb: fb to unregister
540 *
541 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
542 * those used for fbdev. Note that the caller must hold a reference of it's own,
543 * i.e. the object may not be destroyed through this call (since it'll lead to a
544 * locking inversion).
545 */
546void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
547{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100548 struct drm_device *dev = fb->dev;
549
550 mutex_lock(&dev->mode_config.fb_lock);
551 /* Mark fb as reaped and drop idr ref. */
552 __drm_framebuffer_unregister(dev, fb);
553 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100554}
555EXPORT_SYMBOL(drm_framebuffer_unregister_private);
556
557/**
Dave Airlief453ba02008-11-07 14:05:41 -0800558 * drm_framebuffer_cleanup - remove a framebuffer object
559 * @fb: framebuffer to remove
560 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100561 * Cleanup framebuffer. This function is intended to be used from the drivers
562 * ->destroy callback. It can also be used to clean up driver private
563 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100564 *
565 * Note that this function does not remove the fb from active usuage - if it is
566 * still used anywhere, hilarity can ensue since userspace could call getfb on
567 * the id and get back -EINVAL. Obviously no concern at driver unload time.
568 *
569 * Also, the framebuffer will not be removed from the lookup idr - for
570 * user-created framebuffers this will happen in in the rmfb ioctl. For
571 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
572 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800573 */
574void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
575{
576 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100577
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100578 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000579 list_del(&fb->head);
580 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100581 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000582}
583EXPORT_SYMBOL(drm_framebuffer_cleanup);
584
585/**
586 * drm_framebuffer_remove - remove and unreference a framebuffer object
587 * @fb: framebuffer to remove
588 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000589 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100590 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100591 * passed-in framebuffer. Might take the modeset locks.
592 *
593 * Note that this function optimizes the cleanup away if the caller holds the
594 * last reference to the framebuffer. It is also guaranteed to not take the
595 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000596 */
597void drm_framebuffer_remove(struct drm_framebuffer *fb)
598{
599 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800600 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800601 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000602 struct drm_mode_set set;
603 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800604
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100605 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100606
Daniel Vetterb62584e2012-12-11 16:51:35 +0100607 /*
608 * drm ABI mandates that we remove any deleted framebuffers from active
609 * useage. But since most sane clients only remove framebuffers they no
610 * longer need, try to optimize this away.
611 *
612 * Since we're holding a reference ourselves, observing a refcount of 1
613 * means that we're the last holder and can skip it. Also, the refcount
614 * can never increase from 1 again, so we don't need any barriers or
615 * locks.
616 *
617 * Note that userspace could try to race with use and instate a new
618 * usage _after_ we've cleared all current ones. End result will be an
619 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
620 * in this manner.
621 */
622 if (atomic_read(&fb->refcount.refcount) > 1) {
623 drm_modeset_lock_all(dev);
624 /* remove from any CRTC */
625 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700626 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100627 /* should turn off the crtc */
628 memset(&set, 0, sizeof(struct drm_mode_set));
629 set.crtc = crtc;
630 set.fb = NULL;
631 ret = drm_mode_set_config_internal(&set);
632 if (ret)
633 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
634 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000635 }
Dave Airlief453ba02008-11-07 14:05:41 -0800636
Daniel Vetterb62584e2012-12-11 16:51:35 +0100637 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300638 if (plane->fb == fb)
639 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800640 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100641 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800642 }
643
Rob Clarkf7eff602012-09-05 21:48:38 +0000644 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800645}
Rob Clarkf7eff602012-09-05 21:48:38 +0000646EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800647
Rob Clark51fd3712013-11-19 12:10:12 -0500648DEFINE_WW_CLASS(crtc_ww_class);
649
Dave Airlief453ba02008-11-07 14:05:41 -0800650/**
Matt Ropere13161a2014-04-01 15:22:38 -0700651 * drm_crtc_init_with_planes - Initialise a new CRTC object with
652 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800653 * @dev: DRM device
654 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700655 * @primary: Primary plane for CRTC
656 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800657 * @funcs: callbacks for the new CRTC
658 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000659 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200660 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100661 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200662 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800663 */
Matt Ropere13161a2014-04-01 15:22:38 -0700664int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
665 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700666 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700667 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800668{
Rob Clark51fd3712013-11-19 12:10:12 -0500669 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200670 int ret;
671
Dave Airlief453ba02008-11-07 14:05:41 -0800672 crtc->dev = dev;
673 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000674 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800675
Rob Clark51fd3712013-11-19 12:10:12 -0500676 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200677 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
678 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100679 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800680
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300681 crtc->base.properties = &crtc->properties;
682
Rob Clark51fd3712013-11-19 12:10:12 -0500683 list_add_tail(&crtc->head, &config->crtc_list);
684 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200685
Matt Ropere13161a2014-04-01 15:22:38 -0700686 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700687 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700688 if (primary)
689 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700690 if (cursor)
691 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700692
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100693 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800694}
Matt Ropere13161a2014-04-01 15:22:38 -0700695EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800696
697/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000698 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800699 * @crtc: CRTC to cleanup
700 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000701 * This function cleans up @crtc and removes it from the DRM mode setting
702 * core. Note that the function does *not* free the crtc structure itself,
703 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800704 */
705void drm_crtc_cleanup(struct drm_crtc *crtc)
706{
707 struct drm_device *dev = crtc->dev;
708
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000709 kfree(crtc->gamma_store);
710 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800711
Rob Clark51fd3712013-11-19 12:10:12 -0500712 drm_modeset_lock_fini(&crtc->mutex);
713
Dave Airlief453ba02008-11-07 14:05:41 -0800714 drm_mode_object_put(dev, &crtc->base);
715 list_del(&crtc->head);
716 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100717
718 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
719 if (crtc->state && crtc->funcs->atomic_destroy_state)
720 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100721
722 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800723}
724EXPORT_SYMBOL(drm_crtc_cleanup);
725
726/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000727 * drm_crtc_index - find the index of a registered CRTC
728 * @crtc: CRTC to find index for
729 *
730 * Given a registered CRTC, return the index of that CRTC within a DRM
731 * device's list of CRTCs.
732 */
733unsigned int drm_crtc_index(struct drm_crtc *crtc)
734{
735 unsigned int index = 0;
736 struct drm_crtc *tmp;
737
738 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
739 if (tmp == crtc)
740 return index;
741
742 index++;
743 }
744
745 BUG();
746}
747EXPORT_SYMBOL(drm_crtc_index);
748
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100749/*
Dave Airlief453ba02008-11-07 14:05:41 -0800750 * drm_mode_remove - remove and free a mode
751 * @connector: connector list to modify
752 * @mode: mode to remove
753 *
Dave Airlief453ba02008-11-07 14:05:41 -0800754 * Remove @mode from @connector's mode list, then free it.
755 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100756static void drm_mode_remove(struct drm_connector *connector,
757 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800758{
759 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100760 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800761}
Dave Airlief453ba02008-11-07 14:05:41 -0800762
763/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200764 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
765 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200766 *
767 * The kernel supports per-connector configration of its consoles through
768 * use of the video= parameter. This function parses that option and
769 * extracts the user's specified mode (or enable/disable status) for a
770 * particular connector. This is typically only used during the early fbdev
771 * setup.
772 */
773static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
774{
775 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
776 char *option = NULL;
777
778 if (fb_get_options(connector->name, &option))
779 return;
780
781 if (!drm_mode_parse_command_line_for_connector(option,
782 connector,
783 mode))
784 return;
785
786 if (mode->force) {
787 const char *s;
788
789 switch (mode->force) {
790 case DRM_FORCE_OFF:
791 s = "OFF";
792 break;
793 case DRM_FORCE_ON_DIGITAL:
794 s = "ON - dig";
795 break;
796 default:
797 case DRM_FORCE_ON:
798 s = "ON";
799 break;
800 }
801
802 DRM_INFO("forcing %s connector %s\n", connector->name, s);
803 connector->force = mode->force;
804 }
805
806 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
807 connector->name,
808 mode->xres, mode->yres,
809 mode->refresh_specified ? mode->refresh : 60,
810 mode->rb ? " reduced blanking" : "",
811 mode->margins ? " with margins" : "",
812 mode->interlace ? " interlaced" : "");
813}
814
815/**
Dave Airlief453ba02008-11-07 14:05:41 -0800816 * drm_connector_init - Init a preallocated connector
817 * @dev: DRM device
818 * @connector: the connector to init
819 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100820 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800821 *
Dave Airlief453ba02008-11-07 14:05:41 -0800822 * Initialises a preallocated connector. Connectors should be
823 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200824 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100825 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200826 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800827 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200828int drm_connector_init(struct drm_device *dev,
829 struct drm_connector *connector,
830 const struct drm_connector_funcs *funcs,
831 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800832{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200833 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400834 struct ida *connector_ida =
835 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200836
Daniel Vetter84849902012-12-02 00:28:11 +0100837 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800838
Dave Airlie2ee39452014-07-24 11:53:45 +1000839 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200840 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300841 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200842
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300843 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800844 connector->dev = dev;
845 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800846 connector->connector_type = connector_type;
847 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400848 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
849 if (connector->connector_type_id < 0) {
850 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300851 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400852 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300853 connector->name =
854 kasprintf(GFP_KERNEL, "%s-%d",
855 drm_connector_enum_list[connector_type].name,
856 connector->connector_type_id);
857 if (!connector->name) {
858 ret = -ENOMEM;
859 goto out_put;
860 }
861
Dave Airlief453ba02008-11-07 14:05:41 -0800862 INIT_LIST_HEAD(&connector->probed_modes);
863 INIT_LIST_HEAD(&connector->modes);
864 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000865 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800866
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200867 drm_connector_get_cmdline_mode(connector);
868
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100869 /* We should add connectors at the end to avoid upsetting the connector
870 * index too much. */
Dave Airlief453ba02008-11-07 14:05:41 -0800871 list_add_tail(&connector->head, &dev->mode_config.connector_list);
872 dev->mode_config.num_connector++;
873
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200874 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500875 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200876 dev->mode_config.edid_property,
877 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800878
Rob Clark58495562012-10-11 20:50:56 -0500879 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800880 dev->mode_config.dpms_property, 0);
881
Thomas Wood30f65702014-06-18 17:52:32 +0100882 connector->debugfs_entry = NULL;
883
Jani Nikula2abdd312014-05-14 16:58:19 +0300884out_put:
885 if (ret)
886 drm_mode_object_put(dev, &connector->base);
887
888out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100889 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200890
891 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800892}
893EXPORT_SYMBOL(drm_connector_init);
894
895/**
896 * drm_connector_cleanup - cleans up an initialised connector
897 * @connector: connector to cleanup
898 *
Dave Airlief453ba02008-11-07 14:05:41 -0800899 * Cleans up the connector but doesn't free the object.
900 */
901void drm_connector_cleanup(struct drm_connector *connector)
902{
903 struct drm_device *dev = connector->dev;
904 struct drm_display_mode *mode, *t;
905
Dave Airlie40d9b042014-10-20 16:29:33 +1000906 if (connector->tile_group) {
907 drm_mode_put_tile_group(dev, connector->tile_group);
908 connector->tile_group = NULL;
909 }
910
Dave Airlief453ba02008-11-07 14:05:41 -0800911 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
912 drm_mode_remove(connector, mode);
913
914 list_for_each_entry_safe(mode, t, &connector->modes, head)
915 drm_mode_remove(connector, mode);
916
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400917 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
918 connector->connector_type_id);
919
Dave Airlief453ba02008-11-07 14:05:41 -0800920 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300921 kfree(connector->name);
922 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800923 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000924 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100925
926 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
927 if (connector->state && connector->funcs->atomic_destroy_state)
928 connector->funcs->atomic_destroy_state(connector,
929 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100930
931 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800932}
933EXPORT_SYMBOL(drm_connector_cleanup);
934
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100935/**
Daniel Vetter10f637b2014-07-29 13:47:11 +0200936 * drm_connector_index - find the index of a registered connector
937 * @connector: connector to find index for
938 *
939 * Given a registered connector, return the index of that connector within a DRM
940 * device's list of connectors.
941 */
942unsigned int drm_connector_index(struct drm_connector *connector)
943{
944 unsigned int index = 0;
945 struct drm_connector *tmp;
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100946 struct drm_mode_config *config = &connector->dev->mode_config;
947
948 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter10f637b2014-07-29 13:47:11 +0200949
950 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
951 if (tmp == connector)
952 return index;
953
954 index++;
955 }
956
957 BUG();
958}
959EXPORT_SYMBOL(drm_connector_index);
960
961/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100962 * drm_connector_register - register a connector
963 * @connector: the connector to register
964 *
965 * Register userspace interfaces for a connector
966 *
967 * Returns:
968 * Zero on success, error code on failure.
969 */
970int drm_connector_register(struct drm_connector *connector)
971{
Thomas Wood30f65702014-06-18 17:52:32 +0100972 int ret;
973
Dave Airlie2ee39452014-07-24 11:53:45 +1000974 drm_mode_object_register(connector->dev, &connector->base);
975
Thomas Wood30f65702014-06-18 17:52:32 +0100976 ret = drm_sysfs_connector_add(connector);
977 if (ret)
978 return ret;
979
980 ret = drm_debugfs_connector_add(connector);
981 if (ret) {
982 drm_sysfs_connector_remove(connector);
983 return ret;
984 }
985
986 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100987}
988EXPORT_SYMBOL(drm_connector_register);
989
990/**
991 * drm_connector_unregister - unregister a connector
992 * @connector: the connector to unregister
993 *
994 * Unregister userspace interfaces for a connector
995 */
996void drm_connector_unregister(struct drm_connector *connector)
997{
998 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +0100999 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001000}
1001EXPORT_SYMBOL(drm_connector_unregister);
1002
1003
1004/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001005 * drm_connector_unplug_all - unregister connector userspace interfaces
1006 * @dev: drm device
1007 *
1008 * This function unregisters all connector userspace interfaces in sysfs. Should
1009 * be call when the device is disconnected, e.g. from an usb driver's
1010 * ->disconnect callback.
1011 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001012void drm_connector_unplug_all(struct drm_device *dev)
1013{
1014 struct drm_connector *connector;
1015
1016 /* taking the mode config mutex ends up in a clash with sysfs */
1017 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001018 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001019
1020}
1021EXPORT_SYMBOL(drm_connector_unplug_all);
1022
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001023/**
1024 * drm_bridge_init - initialize a drm transcoder/bridge
1025 * @dev: drm device
1026 * @bridge: transcoder/bridge to set up
1027 * @funcs: bridge function table
1028 *
1029 * Initialises a preallocated bridge. Bridges should be
1030 * subclassed as part of driver connector objects.
1031 *
1032 * Returns:
1033 * Zero on success, error code on failure.
1034 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001035int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1036 const struct drm_bridge_funcs *funcs)
1037{
1038 int ret;
1039
1040 drm_modeset_lock_all(dev);
1041
1042 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1043 if (ret)
1044 goto out;
1045
1046 bridge->dev = dev;
1047 bridge->funcs = funcs;
1048
1049 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1050 dev->mode_config.num_bridge++;
1051
1052 out:
1053 drm_modeset_unlock_all(dev);
1054 return ret;
1055}
1056EXPORT_SYMBOL(drm_bridge_init);
1057
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001058/**
1059 * drm_bridge_cleanup - cleans up an initialised bridge
1060 * @bridge: bridge to cleanup
1061 *
1062 * Cleans up the bridge but doesn't free the object.
1063 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001064void drm_bridge_cleanup(struct drm_bridge *bridge)
1065{
1066 struct drm_device *dev = bridge->dev;
1067
1068 drm_modeset_lock_all(dev);
1069 drm_mode_object_put(dev, &bridge->base);
1070 list_del(&bridge->head);
1071 dev->mode_config.num_bridge--;
1072 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001073
1074 memset(bridge, 0, sizeof(*bridge));
Sean Paul3b336ec2013-08-14 16:47:37 -04001075}
1076EXPORT_SYMBOL(drm_bridge_cleanup);
1077
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001078/**
1079 * drm_encoder_init - Init a preallocated encoder
1080 * @dev: drm device
1081 * @encoder: the encoder to init
1082 * @funcs: callbacks for this encoder
1083 * @encoder_type: user visible type of the encoder
1084 *
1085 * Initialises a preallocated encoder. Encoder should be
1086 * subclassed as part of driver encoder objects.
1087 *
1088 * Returns:
1089 * Zero on success, error code on failure.
1090 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001091int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001092 struct drm_encoder *encoder,
1093 const struct drm_encoder_funcs *funcs,
1094 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001095{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001096 int ret;
1097
Daniel Vetter84849902012-12-02 00:28:11 +01001098 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001099
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001100 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1101 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001102 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001103
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001104 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001105 encoder->encoder_type = encoder_type;
1106 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001107 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1108 drm_encoder_enum_list[encoder_type].name,
1109 encoder->base.id);
1110 if (!encoder->name) {
1111 ret = -ENOMEM;
1112 goto out_put;
1113 }
Dave Airlief453ba02008-11-07 14:05:41 -08001114
1115 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1116 dev->mode_config.num_encoder++;
1117
Jani Nikulae5748942014-05-14 16:58:20 +03001118out_put:
1119 if (ret)
1120 drm_mode_object_put(dev, &encoder->base);
1121
1122out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001123 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001124
1125 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001126}
1127EXPORT_SYMBOL(drm_encoder_init);
1128
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001129/**
1130 * drm_encoder_cleanup - cleans up an initialised encoder
1131 * @encoder: encoder to cleanup
1132 *
1133 * Cleans up the encoder but doesn't free the object.
1134 */
Dave Airlief453ba02008-11-07 14:05:41 -08001135void drm_encoder_cleanup(struct drm_encoder *encoder)
1136{
1137 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001138
Daniel Vetter84849902012-12-02 00:28:11 +01001139 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001140 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001141 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001142 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001143 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001144 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001145
1146 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001147}
1148EXPORT_SYMBOL(drm_encoder_cleanup);
1149
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001150/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001151 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001152 * @dev: DRM device
1153 * @plane: plane object to init
1154 * @possible_crtcs: bitmask of possible CRTCs
1155 * @funcs: callbacks for the new plane
1156 * @formats: array of supported formats (%DRM_FORMAT_*)
1157 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001158 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001159 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001160 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001161 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001162 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001163 * Zero on success, error code on failure.
1164 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001165int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1166 unsigned long possible_crtcs,
1167 const struct drm_plane_funcs *funcs,
1168 const uint32_t *formats, uint32_t format_count,
1169 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001170{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001171 int ret;
1172
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001173 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1174 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001175 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001176
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001177 drm_modeset_lock_init(&plane->mutex);
1178
Rob Clark4d939142012-05-17 02:23:27 -06001179 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001180 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001181 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001182 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1183 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001184 if (!plane->format_types) {
1185 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1186 drm_mode_object_put(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001187 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001188 }
1189
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001190 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001191 plane->format_count = format_count;
1192 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001193 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001194
Matt Roperdc415ff2014-04-01 15:22:36 -07001195 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1196 dev->mode_config.num_total_plane++;
1197 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1198 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001199
Rob Clark9922ab52014-04-01 20:16:57 -04001200 drm_object_attach_property(&plane->base,
1201 dev->mode_config.plane_type_property,
1202 plane->type);
1203
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001204 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001205}
Matt Roperdc415ff2014-04-01 15:22:36 -07001206EXPORT_SYMBOL(drm_universal_plane_init);
1207
1208/**
1209 * drm_plane_init - Initialize a legacy plane
1210 * @dev: DRM device
1211 * @plane: plane object to init
1212 * @possible_crtcs: bitmask of possible CRTCs
1213 * @funcs: callbacks for the new plane
1214 * @formats: array of supported formats (%DRM_FORMAT_*)
1215 * @format_count: number of elements in @formats
1216 * @is_primary: plane type (primary vs overlay)
1217 *
1218 * Legacy API to initialize a DRM plane.
1219 *
1220 * New drivers should call drm_universal_plane_init() instead.
1221 *
1222 * Returns:
1223 * Zero on success, error code on failure.
1224 */
1225int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1226 unsigned long possible_crtcs,
1227 const struct drm_plane_funcs *funcs,
1228 const uint32_t *formats, uint32_t format_count,
1229 bool is_primary)
1230{
1231 enum drm_plane_type type;
1232
1233 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1234 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1235 formats, format_count, type);
1236}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001237EXPORT_SYMBOL(drm_plane_init);
1238
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001239/**
1240 * drm_plane_cleanup - Clean up the core plane usage
1241 * @plane: plane to cleanup
1242 *
1243 * This function cleans up @plane and removes it from the DRM mode setting
1244 * core. Note that the function does *not* free the plane structure itself,
1245 * this is the responsibility of the caller.
1246 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001247void drm_plane_cleanup(struct drm_plane *plane)
1248{
1249 struct drm_device *dev = plane->dev;
1250
Daniel Vetter84849902012-12-02 00:28:11 +01001251 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001252 kfree(plane->format_types);
1253 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001254
1255 BUG_ON(list_empty(&plane->head));
1256
1257 list_del(&plane->head);
1258 dev->mode_config.num_total_plane--;
1259 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1260 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001261 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001262
1263 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1264 if (plane->state && plane->funcs->atomic_destroy_state)
1265 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001266
1267 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001268}
1269EXPORT_SYMBOL(drm_plane_cleanup);
1270
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001271/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001272 * drm_plane_index - find the index of a registered plane
1273 * @plane: plane to find index for
1274 *
1275 * Given a registered plane, return the index of that CRTC within a DRM
1276 * device's list of planes.
1277 */
1278unsigned int drm_plane_index(struct drm_plane *plane)
1279{
1280 unsigned int index = 0;
1281 struct drm_plane *tmp;
1282
1283 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1284 if (tmp == plane)
1285 return index;
1286
1287 index++;
1288 }
1289
1290 BUG();
1291}
1292EXPORT_SYMBOL(drm_plane_index);
1293
1294/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001295 * drm_plane_force_disable - Forcibly disable a plane
1296 * @plane: plane to disable
1297 *
1298 * Forces the plane to be disabled.
1299 *
1300 * Used when the plane's current framebuffer is destroyed,
1301 * and when restoring fbdev mode.
1302 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001303void drm_plane_force_disable(struct drm_plane *plane)
1304{
1305 int ret;
1306
Daniel Vetter3d30a592014-07-27 13:42:42 +02001307 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001308 return;
1309
Daniel Vetter3d30a592014-07-27 13:42:42 +02001310 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001311 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001312 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001313 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001314 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001315 return;
1316 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001317 /* disconnect the plane from the fb and crtc: */
Daniel Vetter3d30a592014-07-27 13:42:42 +02001318 __drm_framebuffer_unreference(plane->old_fb);
1319 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001320 plane->fb = NULL;
1321 plane->crtc = NULL;
1322}
1323EXPORT_SYMBOL(drm_plane_force_disable);
1324
Dave Airlief453ba02008-11-07 14:05:41 -08001325static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1326{
1327 struct drm_property *edid;
1328 struct drm_property *dpms;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001329 struct drm_property *dev_path;
Dave Airlief453ba02008-11-07 14:05:41 -08001330
1331 /*
1332 * Standard properties (apply to all connectors)
1333 */
1334 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1335 DRM_MODE_PROP_IMMUTABLE,
1336 "EDID", 0);
1337 dev->mode_config.edid_property = edid;
1338
Sascha Hauer4a67d392012-02-06 10:58:17 +01001339 dpms = drm_property_create_enum(dev, 0,
1340 "DPMS", drm_dpms_enum_list,
1341 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001342 dev->mode_config.dpms_property = dpms;
1343
Dave Airlie43aba7e2014-06-05 14:01:31 +10001344 dev_path = drm_property_create(dev,
1345 DRM_MODE_PROP_BLOB |
1346 DRM_MODE_PROP_IMMUTABLE,
1347 "PATH", 0);
1348 dev->mode_config.path_property = dev_path;
1349
Dave Airlie6f134d72014-10-20 16:30:50 +10001350 dev->mode_config.tile_property = drm_property_create(dev,
1351 DRM_MODE_PROP_BLOB |
1352 DRM_MODE_PROP_IMMUTABLE,
1353 "TILE", 0);
1354
Dave Airlief453ba02008-11-07 14:05:41 -08001355 return 0;
1356}
1357
Rob Clark9922ab52014-04-01 20:16:57 -04001358static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1359{
1360 struct drm_property *type;
1361
1362 /*
1363 * Standard properties (apply to all planes)
1364 */
1365 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1366 "type", drm_plane_type_enum_list,
1367 ARRAY_SIZE(drm_plane_type_enum_list));
1368 dev->mode_config.plane_type_property = type;
1369
1370 return 0;
1371}
1372
Dave Airlief453ba02008-11-07 14:05:41 -08001373/**
1374 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1375 * @dev: DRM device
1376 *
1377 * Called by a driver the first time a DVI-I connector is made.
1378 */
1379int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1380{
1381 struct drm_property *dvi_i_selector;
1382 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001383
1384 if (dev->mode_config.dvi_i_select_subconnector_property)
1385 return 0;
1386
1387 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001388 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001389 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001390 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001391 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001392 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1393
Sascha Hauer4a67d392012-02-06 10:58:17 +01001394 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001395 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001396 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001397 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001398 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1399
1400 return 0;
1401}
1402EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1403
1404/**
1405 * drm_create_tv_properties - create TV specific connector properties
1406 * @dev: DRM device
1407 * @num_modes: number of different TV formats (modes) supported
1408 * @modes: array of pointers to strings containing name of each format
1409 *
1410 * Called by a driver's TV initialization routine, this function creates
1411 * the TV specific connector properties for a given device. Caller is
1412 * responsible for allocating a list of format names and passing them to
1413 * this routine.
1414 */
Thierry Reding2f763312014-10-13 12:45:57 +02001415int drm_mode_create_tv_properties(struct drm_device *dev,
1416 unsigned int num_modes,
Dave Airlief453ba02008-11-07 14:05:41 -08001417 char *modes[])
1418{
1419 struct drm_property *tv_selector;
1420 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001421 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001422
1423 if (dev->mode_config.tv_select_subconnector_property)
1424 return 0;
1425
1426 /*
1427 * Basic connector properties
1428 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001429 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001430 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001431 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001432 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001433 dev->mode_config.tv_select_subconnector_property = tv_selector;
1434
1435 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001436 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1437 "subconnector",
1438 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001439 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001440 dev->mode_config.tv_subconnector_property = tv_subconnector;
1441
1442 /*
1443 * Other, TV specific properties: margins & TV modes.
1444 */
1445 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001446 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001447
1448 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001449 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001450
1451 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001452 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001453
1454 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001455 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001456
1457 dev->mode_config.tv_mode_property =
1458 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1459 "mode", num_modes);
1460 for (i = 0; i < num_modes; i++)
1461 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1462 i, modes[i]);
1463
Francisco Jerezb6b79022009-08-02 04:19:20 +02001464 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001465 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001466
1467 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001468 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001469
1470 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001471 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001472
Francisco Jereza75f0232009-08-12 02:30:10 +02001473 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001474 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001475
1476 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001477 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001478
1479 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001480 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001481
Dave Airlief453ba02008-11-07 14:05:41 -08001482 return 0;
1483}
1484EXPORT_SYMBOL(drm_mode_create_tv_properties);
1485
1486/**
1487 * drm_mode_create_scaling_mode_property - create scaling mode property
1488 * @dev: DRM device
1489 *
1490 * Called by a driver the first time it's needed, must be attached to desired
1491 * connectors.
1492 */
1493int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1494{
1495 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001496
1497 if (dev->mode_config.scaling_mode_property)
1498 return 0;
1499
1500 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001501 drm_property_create_enum(dev, 0, "scaling mode",
1502 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001503 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001504
1505 dev->mode_config.scaling_mode_property = scaling_mode;
1506
1507 return 0;
1508}
1509EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1510
1511/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301512 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1513 * @dev: DRM device
1514 *
1515 * Called by a driver the first time it's needed, must be attached to desired
1516 * connectors.
1517 *
1518 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001519 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301520 */
1521int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1522{
1523 if (dev->mode_config.aspect_ratio_property)
1524 return 0;
1525
1526 dev->mode_config.aspect_ratio_property =
1527 drm_property_create_enum(dev, 0, "aspect ratio",
1528 drm_aspect_ratio_enum_list,
1529 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1530
1531 if (dev->mode_config.aspect_ratio_property == NULL)
1532 return -ENOMEM;
1533
1534 return 0;
1535}
1536EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1537
1538/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001539 * drm_mode_create_dirty_property - create dirty property
1540 * @dev: DRM device
1541 *
1542 * Called by a driver the first time it's needed, must be attached to desired
1543 * connectors.
1544 */
1545int drm_mode_create_dirty_info_property(struct drm_device *dev)
1546{
1547 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001548
1549 if (dev->mode_config.dirty_info_property)
1550 return 0;
1551
1552 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001553 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001554 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001555 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001556 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001557 dev->mode_config.dirty_info_property = dirty_info;
1558
1559 return 0;
1560}
1561EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1562
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001563/**
1564 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1565 * @dev: DRM device
1566 *
1567 * Create the the suggested x/y offset property for connectors.
1568 */
1569int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1570{
1571 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1572 return 0;
1573
1574 dev->mode_config.suggested_x_property =
1575 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1576
1577 dev->mode_config.suggested_y_property =
1578 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1579
1580 if (dev->mode_config.suggested_x_property == NULL ||
1581 dev->mode_config.suggested_y_property == NULL)
1582 return -ENOMEM;
1583 return 0;
1584}
1585EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1586
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001587static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001588{
1589 uint32_t total_objects = 0;
1590
1591 total_objects += dev->mode_config.num_crtc;
1592 total_objects += dev->mode_config.num_connector;
1593 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001594 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001595
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01001596 group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08001597 if (!group->id_list)
1598 return -ENOMEM;
1599
1600 group->num_crtcs = 0;
1601 group->num_connectors = 0;
1602 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001603 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001604 return 0;
1605}
1606
Dave Airliead222792014-05-02 13:22:19 +10001607void drm_mode_group_destroy(struct drm_mode_group *group)
1608{
1609 kfree(group->id_list);
1610 group->id_list = NULL;
1611}
1612
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001613/*
1614 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1615 * the drm core's responsibility to set up mode control groups.
1616 */
Dave Airlief453ba02008-11-07 14:05:41 -08001617int drm_mode_group_init_legacy_group(struct drm_device *dev,
1618 struct drm_mode_group *group)
1619{
1620 struct drm_crtc *crtc;
1621 struct drm_encoder *encoder;
1622 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001623 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001624 int ret;
1625
Thierry Reding0cc0b222014-12-10 13:03:37 +01001626 ret = drm_mode_group_init(dev, group);
1627 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08001628 return ret;
1629
1630 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1631 group->id_list[group->num_crtcs++] = crtc->base.id;
1632
1633 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1634 group->id_list[group->num_crtcs + group->num_encoders++] =
1635 encoder->base.id;
1636
1637 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1638 group->id_list[group->num_crtcs + group->num_encoders +
1639 group->num_connectors++] = connector->base.id;
1640
Sean Paul3b336ec2013-08-14 16:47:37 -04001641 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1642 group->id_list[group->num_crtcs + group->num_encoders +
1643 group->num_connectors + group->num_bridges++] =
1644 bridge->base.id;
1645
Dave Airlief453ba02008-11-07 14:05:41 -08001646 return 0;
1647}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001648EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001649
Dave Airlie2390cd12014-06-05 14:01:29 +10001650void drm_reinit_primary_mode_group(struct drm_device *dev)
1651{
1652 drm_modeset_lock_all(dev);
1653 drm_mode_group_destroy(&dev->primary->mode_group);
1654 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1655 drm_modeset_unlock_all(dev);
1656}
1657EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1658
Dave Airlief453ba02008-11-07 14:05:41 -08001659/**
Dave Airlief453ba02008-11-07 14:05:41 -08001660 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1661 * @out: drm_mode_modeinfo struct to return to the user
1662 * @in: drm_display_mode to use
1663 *
Dave Airlief453ba02008-11-07 14:05:41 -08001664 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1665 * the user.
1666 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001667static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1668 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001669{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001670 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1671 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1672 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1673 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1674 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1675 "timing values too large for mode info\n");
1676
Dave Airlief453ba02008-11-07 14:05:41 -08001677 out->clock = in->clock;
1678 out->hdisplay = in->hdisplay;
1679 out->hsync_start = in->hsync_start;
1680 out->hsync_end = in->hsync_end;
1681 out->htotal = in->htotal;
1682 out->hskew = in->hskew;
1683 out->vdisplay = in->vdisplay;
1684 out->vsync_start = in->vsync_start;
1685 out->vsync_end = in->vsync_end;
1686 out->vtotal = in->vtotal;
1687 out->vscan = in->vscan;
1688 out->vrefresh = in->vrefresh;
1689 out->flags = in->flags;
1690 out->type = in->type;
1691 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1692 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1693}
1694
1695/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001696 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001697 * @out: drm_display_mode to return to the user
1698 * @in: drm_mode_modeinfo to use
1699 *
Dave Airlief453ba02008-11-07 14:05:41 -08001700 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1701 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001702 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001703 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001704 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001705 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001706static int drm_crtc_convert_umode(struct drm_display_mode *out,
1707 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001708{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001709 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1710 return -ERANGE;
1711
Damien Lespiau5848ad42013-09-27 12:11:50 +01001712 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1713 return -EINVAL;
1714
Dave Airlief453ba02008-11-07 14:05:41 -08001715 out->clock = in->clock;
1716 out->hdisplay = in->hdisplay;
1717 out->hsync_start = in->hsync_start;
1718 out->hsync_end = in->hsync_end;
1719 out->htotal = in->htotal;
1720 out->hskew = in->hskew;
1721 out->vdisplay = in->vdisplay;
1722 out->vsync_start = in->vsync_start;
1723 out->vsync_end = in->vsync_end;
1724 out->vtotal = in->vtotal;
1725 out->vscan = in->vscan;
1726 out->vrefresh = in->vrefresh;
1727 out->flags = in->flags;
1728 out->type = in->type;
1729 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1730 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001731
1732 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001733}
1734
1735/**
1736 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001737 * @dev: drm device for the ioctl
1738 * @data: data pointer for the ioctl
1739 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001740 *
Dave Airlief453ba02008-11-07 14:05:41 -08001741 * Construct a set of configuration description structures and return
1742 * them to the user, including CRTC, connector and framebuffer configuration.
1743 *
1744 * Called by the user via ioctl.
1745 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001746 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001747 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001748 */
1749int drm_mode_getresources(struct drm_device *dev, void *data,
1750 struct drm_file *file_priv)
1751{
1752 struct drm_mode_card_res *card_res = data;
1753 struct list_head *lh;
1754 struct drm_framebuffer *fb;
1755 struct drm_connector *connector;
1756 struct drm_crtc *crtc;
1757 struct drm_encoder *encoder;
1758 int ret = 0;
1759 int connector_count = 0;
1760 int crtc_count = 0;
1761 int fb_count = 0;
1762 int encoder_count = 0;
1763 int copied = 0, i;
1764 uint32_t __user *fb_id;
1765 uint32_t __user *crtc_id;
1766 uint32_t __user *connector_id;
1767 uint32_t __user *encoder_id;
1768 struct drm_mode_group *mode_group;
1769
Dave Airliefb3b06c2011-02-08 13:55:21 +10001770 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1771 return -EINVAL;
1772
Dave Airlief453ba02008-11-07 14:05:41 -08001773
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001774 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001775 /*
1776 * For the non-control nodes we need to limit the list of resources
1777 * by IDs in the group list for this node
1778 */
1779 list_for_each(lh, &file_priv->fbs)
1780 fb_count++;
1781
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001782 /* handle this in 4 parts */
1783 /* FBs */
1784 if (card_res->count_fbs >= fb_count) {
1785 copied = 0;
1786 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1787 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1788 if (put_user(fb->base.id, fb_id + copied)) {
1789 mutex_unlock(&file_priv->fbs_lock);
1790 return -EFAULT;
1791 }
1792 copied++;
1793 }
1794 }
1795 card_res->count_fbs = fb_count;
1796 mutex_unlock(&file_priv->fbs_lock);
1797
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001798 /* mode_config.mutex protects the connector list against e.g. DP MST
1799 * connector hot-adding. CRTC/Plane lists are invariant. */
1800 mutex_lock(&dev->mode_config.mutex);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001801 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001802
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001803 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001804 list_for_each(lh, &dev->mode_config.crtc_list)
1805 crtc_count++;
1806
1807 list_for_each(lh, &dev->mode_config.connector_list)
1808 connector_count++;
1809
1810 list_for_each(lh, &dev->mode_config.encoder_list)
1811 encoder_count++;
1812 } else {
1813
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001814 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001815 crtc_count = mode_group->num_crtcs;
1816 connector_count = mode_group->num_connectors;
1817 encoder_count = mode_group->num_encoders;
1818 }
1819
1820 card_res->max_height = dev->mode_config.max_height;
1821 card_res->min_height = dev->mode_config.min_height;
1822 card_res->max_width = dev->mode_config.max_width;
1823 card_res->min_width = dev->mode_config.min_width;
1824
Dave Airlief453ba02008-11-07 14:05:41 -08001825 /* CRTCs */
1826 if (card_res->count_crtcs >= crtc_count) {
1827 copied = 0;
1828 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001829 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001830 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1831 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001832 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001833 if (put_user(crtc->base.id, crtc_id + copied)) {
1834 ret = -EFAULT;
1835 goto out;
1836 }
1837 copied++;
1838 }
1839 } else {
1840 for (i = 0; i < mode_group->num_crtcs; i++) {
1841 if (put_user(mode_group->id_list[i],
1842 crtc_id + copied)) {
1843 ret = -EFAULT;
1844 goto out;
1845 }
1846 copied++;
1847 }
1848 }
1849 }
1850 card_res->count_crtcs = crtc_count;
1851
1852 /* Encoders */
1853 if (card_res->count_encoders >= encoder_count) {
1854 copied = 0;
1855 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001856 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001857 list_for_each_entry(encoder,
1858 &dev->mode_config.encoder_list,
1859 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001860 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001861 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001862 if (put_user(encoder->base.id, encoder_id +
1863 copied)) {
1864 ret = -EFAULT;
1865 goto out;
1866 }
1867 copied++;
1868 }
1869 } else {
1870 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1871 if (put_user(mode_group->id_list[i],
1872 encoder_id + copied)) {
1873 ret = -EFAULT;
1874 goto out;
1875 }
1876 copied++;
1877 }
1878
1879 }
1880 }
1881 card_res->count_encoders = encoder_count;
1882
1883 /* Connectors */
1884 if (card_res->count_connectors >= connector_count) {
1885 copied = 0;
1886 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001887 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001888 list_for_each_entry(connector,
1889 &dev->mode_config.connector_list,
1890 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001891 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1892 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001893 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001894 if (put_user(connector->base.id,
1895 connector_id + copied)) {
1896 ret = -EFAULT;
1897 goto out;
1898 }
1899 copied++;
1900 }
1901 } else {
1902 int start = mode_group->num_crtcs +
1903 mode_group->num_encoders;
1904 for (i = start; i < start + mode_group->num_connectors; i++) {
1905 if (put_user(mode_group->id_list[i],
1906 connector_id + copied)) {
1907 ret = -EFAULT;
1908 goto out;
1909 }
1910 copied++;
1911 }
1912 }
1913 }
1914 card_res->count_connectors = connector_count;
1915
Jerome Glisse94401062010-07-15 15:43:25 -04001916 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001917 card_res->count_connectors, card_res->count_encoders);
1918
1919out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001920 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001921 return ret;
1922}
1923
1924/**
1925 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001926 * @dev: drm device for the ioctl
1927 * @data: data pointer for the ioctl
1928 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001929 *
Dave Airlief453ba02008-11-07 14:05:41 -08001930 * Construct a CRTC configuration structure to return to the user.
1931 *
1932 * Called by the user via ioctl.
1933 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001934 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001935 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001936 */
1937int drm_mode_getcrtc(struct drm_device *dev,
1938 void *data, struct drm_file *file_priv)
1939{
1940 struct drm_mode_crtc *crtc_resp = data;
1941 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001942
Dave Airliefb3b06c2011-02-08 13:55:21 +10001943 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1944 return -EINVAL;
1945
Rob Clarka2b34e22013-10-05 16:36:52 -04001946 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001947 if (!crtc)
1948 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001949
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001950 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08001951 crtc_resp->x = crtc->x;
1952 crtc_resp->y = crtc->y;
1953 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001954 if (crtc->primary->fb)
1955 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001956 else
1957 crtc_resp->fb_id = 0;
1958
1959 if (crtc->enabled) {
1960
1961 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1962 crtc_resp->mode_valid = 1;
1963
1964 } else {
1965 crtc_resp->mode_valid = 0;
1966 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001967 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08001968
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001969 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001970}
1971
Damien Lespiau61d8e322013-09-25 16:45:22 +01001972static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1973 const struct drm_file *file_priv)
1974{
1975 /*
1976 * If user-space hasn't configured the driver to expose the stereo 3D
1977 * modes, don't expose them.
1978 */
1979 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1980 return false;
1981
1982 return true;
1983}
1984
Daniel Vetterabd69c52014-11-25 23:50:05 +01001985static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1986{
1987 /* For atomic drivers only state objects are synchronously updated and
1988 * protected by modeset locks, so check those first. */
1989 if (connector->state)
1990 return connector->state->best_encoder;
1991 return connector->encoder;
1992}
1993
Dave Airlief453ba02008-11-07 14:05:41 -08001994/**
1995 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001996 * @dev: drm device for the ioctl
1997 * @data: data pointer for the ioctl
1998 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001999 *
Dave Airlief453ba02008-11-07 14:05:41 -08002000 * Construct a connector configuration structure to return to the user.
2001 *
2002 * Called by the user via ioctl.
2003 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002004 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002005 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002006 */
2007int drm_mode_getconnector(struct drm_device *dev, void *data,
2008 struct drm_file *file_priv)
2009{
2010 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002011 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002012 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002013 struct drm_display_mode *mode;
2014 int mode_count = 0;
2015 int props_count = 0;
2016 int encoders_count = 0;
2017 int ret = 0;
2018 int copied = 0;
2019 int i;
2020 struct drm_mode_modeinfo u_mode;
2021 struct drm_mode_modeinfo __user *mode_ptr;
2022 uint32_t __user *prop_ptr;
2023 uint64_t __user *prop_values;
2024 uint32_t __user *encoder_ptr;
2025
Dave Airliefb3b06c2011-02-08 13:55:21 +10002026 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2027 return -EINVAL;
2028
Dave Airlief453ba02008-11-07 14:05:41 -08002029 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2030
Jerome Glisse94401062010-07-15 15:43:25 -04002031 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002032
Daniel Vetter7b240562012-12-12 00:35:33 +01002033 mutex_lock(&dev->mode_config.mutex);
Rob Clarkccfc0862014-12-18 16:01:48 -05002034 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002035
Rob Clarka2b34e22013-10-05 16:36:52 -04002036 connector = drm_connector_find(dev, out_resp->connector_id);
2037 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002038 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002039 goto out;
2040 }
Dave Airlief453ba02008-11-07 14:05:41 -08002041
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002042 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08002043
Thierry Reding01073b02014-12-10 13:03:38 +01002044 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2045 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002046 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002047
2048 if (out_resp->count_modes == 0) {
2049 connector->funcs->fill_modes(connector,
2050 dev->mode_config.max_width,
2051 dev->mode_config.max_height);
2052 }
2053
2054 /* delayed so we get modes regardless of pre-fill_modes state */
2055 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002056 if (drm_mode_expose_to_userspace(mode, file_priv))
2057 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002058
2059 out_resp->connector_id = connector->base.id;
2060 out_resp->connector_type = connector->connector_type;
2061 out_resp->connector_type_id = connector->connector_type_id;
2062 out_resp->mm_width = connector->display_info.width_mm;
2063 out_resp->mm_height = connector->display_info.height_mm;
2064 out_resp->subpixel = connector->display_info.subpixel_order;
2065 out_resp->connection = connector->status;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002066 encoder = drm_connector_get_encoder(connector);
2067 if (encoder)
2068 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002069 else
2070 out_resp->encoder_id = 0;
2071
2072 /*
2073 * This ioctl is called twice, once to determine how much space is
2074 * needed, and the 2nd time to fill it.
2075 */
2076 if ((out_resp->count_modes >= mode_count) && mode_count) {
2077 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002078 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002079 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002080 if (!drm_mode_expose_to_userspace(mode, file_priv))
2081 continue;
2082
Dave Airlief453ba02008-11-07 14:05:41 -08002083 drm_crtc_convert_to_umode(&u_mode, mode);
2084 if (copy_to_user(mode_ptr + copied,
2085 &u_mode, sizeof(u_mode))) {
2086 ret = -EFAULT;
2087 goto out;
2088 }
2089 copied++;
2090 }
2091 }
2092 out_resp->count_modes = mode_count;
2093
2094 if ((out_resp->count_props >= props_count) && props_count) {
2095 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002096 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2097 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002098 for (i = 0; i < connector->properties.count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05002099 struct drm_property *prop = connector->properties.properties[i];
Rob Clark22b8b132014-12-16 18:05:31 -05002100 uint64_t val;
2101
2102 ret = drm_object_property_get_value(&connector->base, prop, &val);
2103 if (ret)
2104 goto out;
2105
Rob Clarkb17cd752014-12-16 18:05:30 -05002106 if (put_user(prop->base.id, prop_ptr + copied)) {
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002107 ret = -EFAULT;
2108 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002109 }
Rob Clark22b8b132014-12-16 18:05:31 -05002110 if (put_user(val, prop_values + copied)) {
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002111 ret = -EFAULT;
2112 goto out;
2113 }
2114 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002115 }
2116 }
2117 out_resp->count_props = props_count;
2118
2119 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2120 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002121 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002122 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2123 if (connector->encoder_ids[i] != 0) {
2124 if (put_user(connector->encoder_ids[i],
2125 encoder_ptr + copied)) {
2126 ret = -EFAULT;
2127 goto out;
2128 }
2129 copied++;
2130 }
2131 }
2132 }
2133 out_resp->count_encoders = encoders_count;
2134
2135out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002136 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Daniel Vetter7b240562012-12-12 00:35:33 +01002137 mutex_unlock(&dev->mode_config.mutex);
2138
Dave Airlief453ba02008-11-07 14:05:41 -08002139 return ret;
2140}
2141
Daniel Vetterabd69c52014-11-25 23:50:05 +01002142static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2143{
2144 struct drm_connector *connector;
2145 struct drm_device *dev = encoder->dev;
2146 bool uses_atomic = false;
2147
2148 /* For atomic drivers only state objects are synchronously updated and
2149 * protected by modeset locks, so check those first. */
2150 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2151 if (!connector->state)
2152 continue;
2153
2154 uses_atomic = true;
2155
2156 if (connector->state->best_encoder != encoder)
2157 continue;
2158
2159 return connector->state->crtc;
2160 }
2161
2162 /* Don't return stale data (e.g. pending async disable). */
2163 if (uses_atomic)
2164 return NULL;
2165
2166 return encoder->crtc;
2167}
2168
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002169/**
2170 * drm_mode_getencoder - get encoder configuration
2171 * @dev: drm device for the ioctl
2172 * @data: data pointer for the ioctl
2173 * @file_priv: drm file for the ioctl call
2174 *
2175 * Construct a encoder configuration structure to return to the user.
2176 *
2177 * Called by the user via ioctl.
2178 *
2179 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002180 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002181 */
Dave Airlief453ba02008-11-07 14:05:41 -08002182int drm_mode_getencoder(struct drm_device *dev, void *data,
2183 struct drm_file *file_priv)
2184{
2185 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002186 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002187 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002188
Dave Airliefb3b06c2011-02-08 13:55:21 +10002189 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2190 return -EINVAL;
2191
Rob Clarka2b34e22013-10-05 16:36:52 -04002192 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002193 if (!encoder)
2194 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002195
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002196 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002197 crtc = drm_encoder_get_crtc(encoder);
2198 if (crtc)
2199 enc_resp->crtc_id = crtc->base.id;
2200 else if (encoder->crtc)
Dave Airlief453ba02008-11-07 14:05:41 -08002201 enc_resp->crtc_id = encoder->crtc->base.id;
2202 else
2203 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002204 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2205
Dave Airlief453ba02008-11-07 14:05:41 -08002206 enc_resp->encoder_type = encoder->encoder_type;
2207 enc_resp->encoder_id = encoder->base.id;
2208 enc_resp->possible_crtcs = encoder->possible_crtcs;
2209 enc_resp->possible_clones = encoder->possible_clones;
2210
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002211 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002212}
2213
2214/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002215 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002216 * @dev: DRM device
2217 * @data: ioctl data
2218 * @file_priv: DRM file info
2219 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002220 * Construct a list of plane ids to return to the user.
2221 *
2222 * Called by the user via ioctl.
2223 *
2224 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002225 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002226 */
2227int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002228 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002229{
2230 struct drm_mode_get_plane_res *plane_resp = data;
2231 struct drm_mode_config *config;
2232 struct drm_plane *plane;
2233 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002234 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002235 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002236
2237 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2238 return -EINVAL;
2239
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002240 config = &dev->mode_config;
2241
Matt Roper681e7ec2014-04-01 15:22:42 -07002242 if (file_priv->universal_planes)
2243 num_planes = config->num_total_plane;
2244 else
2245 num_planes = config->num_overlay_plane;
2246
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002247 /*
2248 * This ioctl is called twice, once to determine how much space is
2249 * needed, and the 2nd time to fill it.
2250 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002251 if (num_planes &&
2252 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002253 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002254
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002255 /* Plane lists are invariant, no locking needed. */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002256 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002257 /*
2258 * Unless userspace set the 'universal planes'
2259 * capability bit, only advertise overlays.
2260 */
2261 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2262 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002263 continue;
2264
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002265 if (put_user(plane->base.id, plane_ptr + copied))
2266 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002267 copied++;
2268 }
2269 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002270 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002271
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002272 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002273}
2274
2275/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002276 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002277 * @dev: DRM device
2278 * @data: ioctl data
2279 * @file_priv: DRM file info
2280 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002281 * Construct a plane configuration structure to return to the user.
2282 *
2283 * Called by the user via ioctl.
2284 *
2285 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002286 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002287 */
2288int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002289 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002290{
2291 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002292 struct drm_plane *plane;
2293 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002294
2295 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2296 return -EINVAL;
2297
Rob Clarka2b34e22013-10-05 16:36:52 -04002298 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002299 if (!plane)
2300 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002301
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002302 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002303 if (plane->crtc)
2304 plane_resp->crtc_id = plane->crtc->base.id;
2305 else
2306 plane_resp->crtc_id = 0;
2307
2308 if (plane->fb)
2309 plane_resp->fb_id = plane->fb->base.id;
2310 else
2311 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002312 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002313
2314 plane_resp->plane_id = plane->base.id;
2315 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002316 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002317
2318 /*
2319 * This ioctl is called twice, once to determine how much space is
2320 * needed, and the 2nd time to fill it.
2321 */
2322 if (plane->format_count &&
2323 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002324 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325 if (copy_to_user(format_ptr,
2326 plane->format_types,
2327 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002328 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002329 }
2330 }
2331 plane_resp->count_format_types = plane->format_count;
2332
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002333 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002334}
2335
Matt Roperb36552b2014-06-10 08:28:09 -07002336/*
2337 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002338 *
Matt Roperb36552b2014-06-10 08:28:09 -07002339 * Note that we assume an extra reference has already been taken on fb. If the
2340 * update fails, this reference will be dropped before return; if it succeeds,
2341 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002342 *
Matt Roperb36552b2014-06-10 08:28:09 -07002343 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002344 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002345static int __setplane_internal(struct drm_plane *plane,
2346 struct drm_crtc *crtc,
2347 struct drm_framebuffer *fb,
2348 int32_t crtc_x, int32_t crtc_y,
2349 uint32_t crtc_w, uint32_t crtc_h,
2350 /* src_{x,y,w,h} values are 16.16 fixed point */
2351 uint32_t src_x, uint32_t src_y,
2352 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002353{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002354 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002355 unsigned int fb_width, fb_height;
Thierry Reding2f763312014-10-13 12:45:57 +02002356 unsigned int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002357
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002358 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002359 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002360 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002361 ret = plane->funcs->disable_plane(plane);
2362 if (!ret) {
2363 plane->crtc = NULL;
2364 plane->fb = NULL;
2365 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002366 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002367 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002368 goto out;
2369 }
2370
Matt Roper7f994f32014-05-29 08:06:51 -07002371 /* Check whether this plane is usable on this CRTC */
2372 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2373 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2374 ret = -EINVAL;
2375 goto out;
2376 }
2377
Ville Syrjälä62443be2011-12-20 00:06:47 +02002378 /* Check whether this plane supports the fb pixel format. */
2379 for (i = 0; i < plane->format_count; i++)
2380 if (fb->pixel_format == plane->format_types[i])
2381 break;
2382 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002383 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2384 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002385 ret = -EINVAL;
2386 goto out;
2387 }
2388
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002389 fb_width = fb->width << 16;
2390 fb_height = fb->height << 16;
2391
2392 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002393 if (src_w > fb_width ||
2394 src_x > fb_width - src_w ||
2395 src_h > fb_height ||
2396 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002397 DRM_DEBUG_KMS("Invalid source coordinates "
2398 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002399 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2400 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2401 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2402 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002403 ret = -ENOSPC;
2404 goto out;
2405 }
2406
Daniel Vetter3d30a592014-07-27 13:42:42 +02002407 plane->old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002408 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002409 crtc_x, crtc_y, crtc_w, crtc_h,
2410 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002411 if (!ret) {
2412 plane->crtc = crtc;
2413 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002414 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002415 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002416 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002417 }
2418
2419out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002420 if (fb)
2421 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002422 if (plane->old_fb)
2423 drm_framebuffer_unreference(plane->old_fb);
2424 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002425
2426 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002427}
Matt Roperb36552b2014-06-10 08:28:09 -07002428
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002429static int setplane_internal(struct drm_plane *plane,
2430 struct drm_crtc *crtc,
2431 struct drm_framebuffer *fb,
2432 int32_t crtc_x, int32_t crtc_y,
2433 uint32_t crtc_w, uint32_t crtc_h,
2434 /* src_{x,y,w,h} values are 16.16 fixed point */
2435 uint32_t src_x, uint32_t src_y,
2436 uint32_t src_w, uint32_t src_h)
2437{
2438 int ret;
2439
2440 drm_modeset_lock_all(plane->dev);
2441 ret = __setplane_internal(plane, crtc, fb,
2442 crtc_x, crtc_y, crtc_w, crtc_h,
2443 src_x, src_y, src_w, src_h);
2444 drm_modeset_unlock_all(plane->dev);
2445
2446 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002447}
2448
2449/**
2450 * drm_mode_setplane - configure a plane's configuration
2451 * @dev: DRM device
2452 * @data: ioctl data*
2453 * @file_priv: DRM file info
2454 *
2455 * Set plane configuration, including placement, fb, scaling, and other factors.
2456 * Or pass a NULL fb to disable (planes may be disabled without providing a
2457 * valid crtc).
2458 *
2459 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002460 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002461 */
2462int drm_mode_setplane(struct drm_device *dev, void *data,
2463 struct drm_file *file_priv)
2464{
2465 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002466 struct drm_plane *plane;
2467 struct drm_crtc *crtc = NULL;
2468 struct drm_framebuffer *fb = NULL;
2469
2470 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2471 return -EINVAL;
2472
2473 /* Give drivers some help against integer overflows */
2474 if (plane_req->crtc_w > INT_MAX ||
2475 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2476 plane_req->crtc_h > INT_MAX ||
2477 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2478 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2479 plane_req->crtc_w, plane_req->crtc_h,
2480 plane_req->crtc_x, plane_req->crtc_y);
2481 return -ERANGE;
2482 }
2483
2484 /*
2485 * First, find the plane, crtc, and fb objects. If not available,
2486 * we don't bother to call the driver.
2487 */
Rob Clark933f6222014-11-25 20:33:11 -05002488 plane = drm_plane_find(dev, plane_req->plane_id);
2489 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002490 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2491 plane_req->plane_id);
2492 return -ENOENT;
2493 }
Matt Roperb36552b2014-06-10 08:28:09 -07002494
2495 if (plane_req->fb_id) {
2496 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2497 if (!fb) {
2498 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2499 plane_req->fb_id);
2500 return -ENOENT;
2501 }
2502
Rob Clark933f6222014-11-25 20:33:11 -05002503 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2504 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002505 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2506 plane_req->crtc_id);
2507 return -ENOENT;
2508 }
Matt Roperb36552b2014-06-10 08:28:09 -07002509 }
2510
Matt Roper161d0dc2014-06-10 08:28:10 -07002511 /*
2512 * setplane_internal will take care of deref'ing either the old or new
2513 * framebuffer depending on success.
2514 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002515 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002516 plane_req->crtc_x, plane_req->crtc_y,
2517 plane_req->crtc_w, plane_req->crtc_h,
2518 plane_req->src_x, plane_req->src_y,
2519 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002520}
2521
2522/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002523 * drm_mode_set_config_internal - helper to call ->set_config
2524 * @set: modeset config to set
2525 *
2526 * This is a little helper to wrap internal calls to the ->set_config driver
2527 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002528 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002529 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002530 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002531 */
2532int drm_mode_set_config_internal(struct drm_mode_set *set)
2533{
2534 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002535 struct drm_framebuffer *fb;
2536 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002537 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002538
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002539 /*
2540 * NOTE: ->set_config can also disable other crtcs (if we steal all
2541 * connectors from it), hence we need to refcount the fbs across all
2542 * crtcs. Atomic modeset will have saner semantics ...
2543 */
2544 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002545 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002546
Daniel Vetterb0d12322012-12-11 01:07:12 +01002547 fb = set->fb;
2548
2549 ret = crtc->funcs->set_config(set);
2550 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002551 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002552 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002553 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002554
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002555 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002556 if (tmp->primary->fb)
2557 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002558 if (tmp->primary->old_fb)
2559 drm_framebuffer_unreference(tmp->primary->old_fb);
2560 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002561 }
2562
2563 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002564}
2565EXPORT_SYMBOL(drm_mode_set_config_internal);
2566
Matt Roperaf936292014-04-01 15:22:34 -07002567/**
2568 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2569 * CRTC viewport
2570 * @crtc: CRTC that framebuffer will be displayed on
2571 * @x: x panning
2572 * @y: y panning
2573 * @mode: mode that framebuffer will be displayed under
2574 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002575 */
Matt Roperaf936292014-04-01 15:22:34 -07002576int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2577 int x, int y,
2578 const struct drm_display_mode *mode,
2579 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002580
2581{
2582 int hdisplay, vdisplay;
2583
2584 hdisplay = mode->hdisplay;
2585 vdisplay = mode->vdisplay;
2586
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002587 if (drm_mode_is_stereo(mode)) {
2588 struct drm_display_mode adjusted = *mode;
2589
2590 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2591 hdisplay = adjusted.crtc_hdisplay;
2592 vdisplay = adjusted.crtc_vdisplay;
2593 }
2594
Damien Lespiauc11e9282013-09-25 16:45:30 +01002595 if (crtc->invert_dimensions)
2596 swap(hdisplay, vdisplay);
2597
2598 if (hdisplay > fb->width ||
2599 vdisplay > fb->height ||
2600 x > fb->width - hdisplay ||
2601 y > fb->height - vdisplay) {
2602 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2603 fb->width, fb->height, hdisplay, vdisplay, x, y,
2604 crtc->invert_dimensions ? " (inverted)" : "");
2605 return -ENOSPC;
2606 }
2607
2608 return 0;
2609}
Matt Roperaf936292014-04-01 15:22:34 -07002610EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002611
Daniel Vetter2d13b672012-12-11 13:47:23 +01002612/**
Dave Airlief453ba02008-11-07 14:05:41 -08002613 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002614 * @dev: drm device for the ioctl
2615 * @data: data pointer for the ioctl
2616 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002617 *
Dave Airlief453ba02008-11-07 14:05:41 -08002618 * Build a new CRTC configuration based on user request.
2619 *
2620 * Called by the user via ioctl.
2621 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002622 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002623 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002624 */
2625int drm_mode_setcrtc(struct drm_device *dev, void *data,
2626 struct drm_file *file_priv)
2627{
2628 struct drm_mode_config *config = &dev->mode_config;
2629 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002630 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002631 struct drm_connector **connector_set = NULL, *connector;
2632 struct drm_framebuffer *fb = NULL;
2633 struct drm_display_mode *mode = NULL;
2634 struct drm_mode_set set;
2635 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002636 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002637 int i;
2638
Dave Airliefb3b06c2011-02-08 13:55:21 +10002639 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2640 return -EINVAL;
2641
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002642 /* For some reason crtc x/y offsets are signed internally. */
2643 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2644 return -ERANGE;
2645
Daniel Vetter84849902012-12-02 00:28:11 +01002646 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002647 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2648 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002649 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002650 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002651 goto out;
2652 }
Jerome Glisse94401062010-07-15 15:43:25 -04002653 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002654
2655 if (crtc_req->mode_valid) {
2656 /* If we have a mode we need a framebuffer. */
2657 /* If we pass -1, set the mode with the currently bound fb */
2658 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002659 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002660 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2661 ret = -EINVAL;
2662 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002663 }
Matt Roperf4510a22014-04-01 15:22:40 -07002664 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002665 /* Make refcounting symmetric with the lookup path. */
2666 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002667 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002668 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2669 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002670 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2671 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002672 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002673 goto out;
2674 }
Dave Airlief453ba02008-11-07 14:05:41 -08002675 }
2676
2677 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002678 if (!mode) {
2679 ret = -ENOMEM;
2680 goto out;
2681 }
2682
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002683 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2684 if (ret) {
2685 DRM_DEBUG_KMS("Invalid mode\n");
2686 goto out;
2687 }
2688
Dave Airlief453ba02008-11-07 14:05:41 -08002689 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002690
Damien Lespiauc11e9282013-09-25 16:45:30 +01002691 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2692 mode, fb);
2693 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002694 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002695
Dave Airlief453ba02008-11-07 14:05:41 -08002696 }
2697
2698 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002699 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002700 ret = -EINVAL;
2701 goto out;
2702 }
2703
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002704 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002705 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002706 crtc_req->count_connectors);
2707 ret = -EINVAL;
2708 goto out;
2709 }
2710
2711 if (crtc_req->count_connectors > 0) {
2712 u32 out_id;
2713
2714 /* Avoid unbounded kernel memory allocation */
2715 if (crtc_req->count_connectors > config->num_connector) {
2716 ret = -EINVAL;
2717 goto out;
2718 }
2719
Thierry Reding2f6c5382014-12-10 13:03:36 +01002720 connector_set = kmalloc_array(crtc_req->count_connectors,
2721 sizeof(struct drm_connector *),
2722 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002723 if (!connector_set) {
2724 ret = -ENOMEM;
2725 goto out;
2726 }
2727
2728 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002729 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002730 if (get_user(out_id, &set_connectors_ptr[i])) {
2731 ret = -EFAULT;
2732 goto out;
2733 }
2734
Rob Clarka2b34e22013-10-05 16:36:52 -04002735 connector = drm_connector_find(dev, out_id);
2736 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002737 DRM_DEBUG_KMS("Connector id %d unknown\n",
2738 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002739 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002740 goto out;
2741 }
Jerome Glisse94401062010-07-15 15:43:25 -04002742 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2743 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002744 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002745
2746 connector_set[i] = connector;
2747 }
2748 }
2749
2750 set.crtc = crtc;
2751 set.x = crtc_req->x;
2752 set.y = crtc_req->y;
2753 set.mode = mode;
2754 set.connectors = connector_set;
2755 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002756 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002757 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002758
2759out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002760 if (fb)
2761 drm_framebuffer_unreference(fb);
2762
Dave Airlief453ba02008-11-07 14:05:41 -08002763 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002764 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002765 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002766 return ret;
2767}
2768
Matt Roper161d0dc2014-06-10 08:28:10 -07002769/**
2770 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2771 * universal plane handler call
2772 * @crtc: crtc to update cursor for
2773 * @req: data pointer for the ioctl
2774 * @file_priv: drm file for the ioctl call
2775 *
2776 * Legacy cursor ioctl's work directly with driver buffer handles. To
2777 * translate legacy ioctl calls into universal plane handler calls, we need to
2778 * wrap the native buffer handle in a drm_framebuffer.
2779 *
2780 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2781 * buffer with a pitch of 4*width; the universal plane interface should be used
2782 * directly in cases where the hardware can support other buffer settings and
2783 * userspace wants to make use of these capabilities.
2784 *
2785 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002786 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002787 */
2788static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2789 struct drm_mode_cursor2 *req,
2790 struct drm_file *file_priv)
2791{
2792 struct drm_device *dev = crtc->dev;
2793 struct drm_framebuffer *fb = NULL;
2794 struct drm_mode_fb_cmd2 fbreq = {
2795 .width = req->width,
2796 .height = req->height,
2797 .pixel_format = DRM_FORMAT_ARGB8888,
2798 .pitches = { req->width * 4 },
2799 .handles = { req->handle },
2800 };
2801 int32_t crtc_x, crtc_y;
2802 uint32_t crtc_w = 0, crtc_h = 0;
2803 uint32_t src_w = 0, src_h = 0;
2804 int ret = 0;
2805
2806 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002807 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002808
2809 /*
2810 * Obtain fb we'll be using (either new or existing) and take an extra
2811 * reference to it if fb != null. setplane will take care of dropping
2812 * the reference if the plane update fails.
2813 */
2814 if (req->flags & DRM_MODE_CURSOR_BO) {
2815 if (req->handle) {
2816 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2817 if (IS_ERR(fb)) {
2818 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2819 return PTR_ERR(fb);
2820 }
2821
2822 drm_framebuffer_reference(fb);
2823 } else {
2824 fb = NULL;
2825 }
2826 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002827 fb = crtc->cursor->fb;
2828 if (fb)
2829 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002830 }
2831
2832 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2833 crtc_x = req->x;
2834 crtc_y = req->y;
2835 } else {
2836 crtc_x = crtc->cursor_x;
2837 crtc_y = crtc->cursor_y;
2838 }
2839
2840 if (fb) {
2841 crtc_w = fb->width;
2842 crtc_h = fb->height;
2843 src_w = fb->width << 16;
2844 src_h = fb->height << 16;
2845 }
2846
2847 /*
2848 * setplane_internal will take care of deref'ing either the old or new
2849 * framebuffer depending on success.
2850 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002851 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002852 crtc_x, crtc_y, crtc_w, crtc_h,
2853 0, 0, src_w, src_h);
2854
2855 /* Update successful; save new cursor position, if necessary */
2856 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2857 crtc->cursor_x = req->x;
2858 crtc->cursor_y = req->y;
2859 }
2860
2861 return ret;
2862}
2863
Dave Airlie4c813d42013-06-20 11:48:52 +10002864static int drm_mode_cursor_common(struct drm_device *dev,
2865 struct drm_mode_cursor2 *req,
2866 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002867{
Dave Airlief453ba02008-11-07 14:05:41 -08002868 struct drm_crtc *crtc;
2869 int ret = 0;
2870
Dave Airliefb3b06c2011-02-08 13:55:21 +10002871 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2872 return -EINVAL;
2873
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002874 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002875 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002876
Rob Clarka2b34e22013-10-05 16:36:52 -04002877 crtc = drm_crtc_find(dev, req->crtc_id);
2878 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002879 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002880 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002881 }
Dave Airlief453ba02008-11-07 14:05:41 -08002882
Matt Roper161d0dc2014-06-10 08:28:10 -07002883 /*
2884 * If this crtc has a universal cursor plane, call that plane's update
2885 * handler rather than using legacy cursor handlers.
2886 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002887 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002888 if (crtc->cursor) {
2889 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2890 goto out;
2891 }
2892
Dave Airlief453ba02008-11-07 14:05:41 -08002893 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002894 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002895 ret = -ENXIO;
2896 goto out;
2897 }
2898 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002899 if (crtc->funcs->cursor_set2)
2900 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2901 req->width, req->height, req->hot_x, req->hot_y);
2902 else
2903 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2904 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002905 }
2906
2907 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2908 if (crtc->funcs->cursor_move) {
2909 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2910 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002911 ret = -EFAULT;
2912 goto out;
2913 }
2914 }
2915out:
Daniel Vetterd059f652014-07-25 18:07:40 +02002916 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01002917
Dave Airlief453ba02008-11-07 14:05:41 -08002918 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002919
2920}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002921
2922
2923/**
2924 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2925 * @dev: drm device for the ioctl
2926 * @data: data pointer for the ioctl
2927 * @file_priv: drm file for the ioctl call
2928 *
2929 * Set the cursor configuration based on user request.
2930 *
2931 * Called by the user via ioctl.
2932 *
2933 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002934 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002935 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002936int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002937 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002938{
2939 struct drm_mode_cursor *req = data;
2940 struct drm_mode_cursor2 new_req;
2941
2942 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2943 new_req.hot_x = new_req.hot_y = 0;
2944
2945 return drm_mode_cursor_common(dev, &new_req, file_priv);
2946}
2947
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002948/**
2949 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2950 * @dev: drm device for the ioctl
2951 * @data: data pointer for the ioctl
2952 * @file_priv: drm file for the ioctl call
2953 *
2954 * Set the cursor configuration based on user request. This implements the 2nd
2955 * version of the cursor ioctl, which allows userspace to additionally specify
2956 * the hotspot of the pointer.
2957 *
2958 * Called by the user via ioctl.
2959 *
2960 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002961 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002962 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002963int drm_mode_cursor2_ioctl(struct drm_device *dev,
2964 void *data, struct drm_file *file_priv)
2965{
2966 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01002967
Dave Airlie4c813d42013-06-20 11:48:52 +10002968 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002969}
2970
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002971/**
2972 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2973 * @bpp: bits per pixels
2974 * @depth: bit depth per pixel
2975 *
2976 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2977 * Useful in fbdev emulation code, since that deals in those values.
2978 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002979uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2980{
2981 uint32_t fmt;
2982
2983 switch (bpp) {
2984 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002985 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002986 break;
2987 case 16:
2988 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002989 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002990 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002991 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002992 break;
2993 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002994 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002995 break;
2996 case 32:
2997 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002998 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002999 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003000 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003001 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003002 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003003 break;
3004 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003005 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3006 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003007 break;
3008 }
3009
3010 return fmt;
3011}
3012EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3013
Dave Airlief453ba02008-11-07 14:05:41 -08003014/**
3015 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003016 * @dev: drm device for the ioctl
3017 * @data: data pointer for the ioctl
3018 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003019 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003020 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003021 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003022 *
3023 * Called by the user via ioctl.
3024 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003025 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003026 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003027 */
3028int drm_mode_addfb(struct drm_device *dev,
3029 void *data, struct drm_file *file_priv)
3030{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003031 struct drm_mode_fb_cmd *or = data;
3032 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003033 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003034
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003035 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003036 r.fb_id = or->fb_id;
3037 r.width = or->width;
3038 r.height = or->height;
3039 r.pitches[0] = or->pitch;
3040 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3041 r.handles[0] = or->handle;
3042
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003043 ret = drm_mode_addfb2(dev, &r, file_priv);
3044 if (ret)
3045 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003046
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003047 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003048
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003049 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003050}
3051
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003052static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003053{
3054 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3055
3056 switch (format) {
3057 case DRM_FORMAT_C8:
3058 case DRM_FORMAT_RGB332:
3059 case DRM_FORMAT_BGR233:
3060 case DRM_FORMAT_XRGB4444:
3061 case DRM_FORMAT_XBGR4444:
3062 case DRM_FORMAT_RGBX4444:
3063 case DRM_FORMAT_BGRX4444:
3064 case DRM_FORMAT_ARGB4444:
3065 case DRM_FORMAT_ABGR4444:
3066 case DRM_FORMAT_RGBA4444:
3067 case DRM_FORMAT_BGRA4444:
3068 case DRM_FORMAT_XRGB1555:
3069 case DRM_FORMAT_XBGR1555:
3070 case DRM_FORMAT_RGBX5551:
3071 case DRM_FORMAT_BGRX5551:
3072 case DRM_FORMAT_ARGB1555:
3073 case DRM_FORMAT_ABGR1555:
3074 case DRM_FORMAT_RGBA5551:
3075 case DRM_FORMAT_BGRA5551:
3076 case DRM_FORMAT_RGB565:
3077 case DRM_FORMAT_BGR565:
3078 case DRM_FORMAT_RGB888:
3079 case DRM_FORMAT_BGR888:
3080 case DRM_FORMAT_XRGB8888:
3081 case DRM_FORMAT_XBGR8888:
3082 case DRM_FORMAT_RGBX8888:
3083 case DRM_FORMAT_BGRX8888:
3084 case DRM_FORMAT_ARGB8888:
3085 case DRM_FORMAT_ABGR8888:
3086 case DRM_FORMAT_RGBA8888:
3087 case DRM_FORMAT_BGRA8888:
3088 case DRM_FORMAT_XRGB2101010:
3089 case DRM_FORMAT_XBGR2101010:
3090 case DRM_FORMAT_RGBX1010102:
3091 case DRM_FORMAT_BGRX1010102:
3092 case DRM_FORMAT_ARGB2101010:
3093 case DRM_FORMAT_ABGR2101010:
3094 case DRM_FORMAT_RGBA1010102:
3095 case DRM_FORMAT_BGRA1010102:
3096 case DRM_FORMAT_YUYV:
3097 case DRM_FORMAT_YVYU:
3098 case DRM_FORMAT_UYVY:
3099 case DRM_FORMAT_VYUY:
3100 case DRM_FORMAT_AYUV:
3101 case DRM_FORMAT_NV12:
3102 case DRM_FORMAT_NV21:
3103 case DRM_FORMAT_NV16:
3104 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003105 case DRM_FORMAT_NV24:
3106 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003107 case DRM_FORMAT_YUV410:
3108 case DRM_FORMAT_YVU410:
3109 case DRM_FORMAT_YUV411:
3110 case DRM_FORMAT_YVU411:
3111 case DRM_FORMAT_YUV420:
3112 case DRM_FORMAT_YVU420:
3113 case DRM_FORMAT_YUV422:
3114 case DRM_FORMAT_YVU422:
3115 case DRM_FORMAT_YUV444:
3116 case DRM_FORMAT_YVU444:
3117 return 0;
3118 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003119 DRM_DEBUG_KMS("invalid pixel format %s\n",
3120 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003121 return -EINVAL;
3122 }
3123}
3124
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003125static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003126{
3127 int ret, hsub, vsub, num_planes, i;
3128
3129 ret = format_check(r);
3130 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003131 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3132 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003133 return ret;
3134 }
3135
3136 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3137 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3138 num_planes = drm_format_num_planes(r->pixel_format);
3139
3140 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003141 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003142 return -EINVAL;
3143 }
3144
3145 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003146 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003147 return -EINVAL;
3148 }
3149
3150 for (i = 0; i < num_planes; i++) {
3151 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003152 unsigned int height = r->height / (i != 0 ? vsub : 1);
3153 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003154
3155 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003156 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003157 return -EINVAL;
3158 }
3159
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003160 if ((uint64_t) width * cpp > UINT_MAX)
3161 return -ERANGE;
3162
3163 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3164 return -ERANGE;
3165
3166 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003167 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003168 return -EINVAL;
3169 }
3170 }
3171
3172 return 0;
3173}
3174
Matt Roperc394c2b2014-06-10 08:28:08 -07003175static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3176 struct drm_mode_fb_cmd2 *r,
3177 struct drm_file *file_priv)
3178{
3179 struct drm_mode_config *config = &dev->mode_config;
3180 struct drm_framebuffer *fb;
3181 int ret;
3182
3183 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3184 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3185 return ERR_PTR(-EINVAL);
3186 }
3187
3188 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3189 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3190 r->width, config->min_width, config->max_width);
3191 return ERR_PTR(-EINVAL);
3192 }
3193 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3194 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3195 r->height, config->min_height, config->max_height);
3196 return ERR_PTR(-EINVAL);
3197 }
3198
3199 ret = framebuffer_check(r);
3200 if (ret)
3201 return ERR_PTR(ret);
3202
3203 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3204 if (IS_ERR(fb)) {
3205 DRM_DEBUG_KMS("could not create framebuffer\n");
3206 return fb;
3207 }
3208
3209 mutex_lock(&file_priv->fbs_lock);
3210 r->fb_id = fb->base.id;
3211 list_add(&fb->filp_head, &file_priv->fbs);
3212 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3213 mutex_unlock(&file_priv->fbs_lock);
3214
3215 return fb;
3216}
3217
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003218/**
3219 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003220 * @dev: drm device for the ioctl
3221 * @data: data pointer for the ioctl
3222 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003223 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003224 * Add a new FB to the specified CRTC, given a user request with format. This is
3225 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3226 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003227 *
3228 * Called by the user via ioctl.
3229 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003230 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003231 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003232 */
3233int drm_mode_addfb2(struct drm_device *dev,
3234 void *data, struct drm_file *file_priv)
3235{
Dave Airlief453ba02008-11-07 14:05:41 -08003236 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003237
Dave Airliefb3b06c2011-02-08 13:55:21 +10003238 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3239 return -EINVAL;
3240
Matt Roperc394c2b2014-06-10 08:28:08 -07003241 fb = add_framebuffer_internal(dev, data, file_priv);
3242 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003243 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003244
Matt Roperc394c2b2014-06-10 08:28:08 -07003245 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003246}
3247
3248/**
3249 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003250 * @dev: drm device for the ioctl
3251 * @data: data pointer for the ioctl
3252 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003253 *
Dave Airlief453ba02008-11-07 14:05:41 -08003254 * Remove the FB specified by the user.
3255 *
3256 * Called by the user via ioctl.
3257 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003258 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003259 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003260 */
3261int drm_mode_rmfb(struct drm_device *dev,
3262 void *data, struct drm_file *file_priv)
3263{
Dave Airlief453ba02008-11-07 14:05:41 -08003264 struct drm_framebuffer *fb = NULL;
3265 struct drm_framebuffer *fbl = NULL;
3266 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003267 int found = 0;
3268
Dave Airliefb3b06c2011-02-08 13:55:21 +10003269 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3270 return -EINVAL;
3271
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003272 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003273 mutex_lock(&dev->mode_config.fb_lock);
3274 fb = __drm_framebuffer_lookup(dev, *id);
3275 if (!fb)
3276 goto fail_lookup;
3277
Dave Airlief453ba02008-11-07 14:05:41 -08003278 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3279 if (fb == fbl)
3280 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003281 if (!found)
3282 goto fail_lookup;
3283
3284 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3285 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003286
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003287 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003288 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003289 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003290
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003291 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003292
Daniel Vetter2b677e82012-12-10 21:16:05 +01003293 return 0;
3294
3295fail_lookup:
3296 mutex_unlock(&dev->mode_config.fb_lock);
3297 mutex_unlock(&file_priv->fbs_lock);
3298
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003299 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003300}
3301
3302/**
3303 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003304 * @dev: drm device for the ioctl
3305 * @data: data pointer for the ioctl
3306 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003307 *
Dave Airlief453ba02008-11-07 14:05:41 -08003308 * Lookup the FB given its ID and return info about it.
3309 *
3310 * Called by the user via ioctl.
3311 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003312 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003313 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003314 */
3315int drm_mode_getfb(struct drm_device *dev,
3316 void *data, struct drm_file *file_priv)
3317{
3318 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003319 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003320 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003321
Dave Airliefb3b06c2011-02-08 13:55:21 +10003322 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3323 return -EINVAL;
3324
Daniel Vetter786b99e2012-12-02 21:53:40 +01003325 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003326 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003327 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003328
3329 r->height = fb->height;
3330 r->width = fb->width;
3331 r->depth = fb->depth;
3332 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003333 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003334 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003335 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003336 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003337 ret = fb->funcs->create_handle(fb, file_priv,
3338 &r->handle);
3339 } else {
3340 /* GET_FB() is an unprivileged ioctl so we must not
3341 * return a buffer-handle to non-master processes! For
3342 * backwards-compatibility reasons, we cannot make
3343 * GET_FB() privileged, so just return an invalid handle
3344 * for non-masters. */
3345 r->handle = 0;
3346 ret = 0;
3347 }
3348 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003349 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003350 }
Dave Airlief453ba02008-11-07 14:05:41 -08003351
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003352 drm_framebuffer_unreference(fb);
3353
Dave Airlief453ba02008-11-07 14:05:41 -08003354 return ret;
3355}
3356
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003357/**
3358 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3359 * @dev: drm device for the ioctl
3360 * @data: data pointer for the ioctl
3361 * @file_priv: drm file for the ioctl call
3362 *
3363 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3364 * rectangle list. Generic userspace which does frontbuffer rendering must call
3365 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3366 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3367 *
3368 * Modesetting drivers which always update the frontbuffer do not need to
3369 * implement the corresponding ->dirty framebuffer callback.
3370 *
3371 * Called by the user via ioctl.
3372 *
3373 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003374 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003375 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003376int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3377 void *data, struct drm_file *file_priv)
3378{
3379 struct drm_clip_rect __user *clips_ptr;
3380 struct drm_clip_rect *clips = NULL;
3381 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003382 struct drm_framebuffer *fb;
3383 unsigned flags;
3384 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003385 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003386
Dave Airliefb3b06c2011-02-08 13:55:21 +10003387 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3388 return -EINVAL;
3389
Daniel Vetter786b99e2012-12-02 21:53:40 +01003390 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003391 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003392 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003393
3394 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003395 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003396
3397 if (!num_clips != !clips_ptr) {
3398 ret = -EINVAL;
3399 goto out_err1;
3400 }
3401
3402 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3403
3404 /* If userspace annotates copy, clips must come in pairs */
3405 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3406 ret = -EINVAL;
3407 goto out_err1;
3408 }
3409
3410 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003411 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3412 ret = -EINVAL;
3413 goto out_err1;
3414 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003415 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003416 if (!clips) {
3417 ret = -ENOMEM;
3418 goto out_err1;
3419 }
3420
3421 ret = copy_from_user(clips, clips_ptr,
3422 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003423 if (ret) {
3424 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003425 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003426 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003427 }
3428
3429 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003430 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3431 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003432 } else {
3433 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003434 }
3435
3436out_err2:
3437 kfree(clips);
3438out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003439 drm_framebuffer_unreference(fb);
3440
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003441 return ret;
3442}
3443
3444
Dave Airlief453ba02008-11-07 14:05:41 -08003445/**
3446 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003447 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003448 *
Dave Airlief453ba02008-11-07 14:05:41 -08003449 * Destroy all the FBs associated with @filp.
3450 *
3451 * Called by the user via ioctl.
3452 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003453 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003454 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003455 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003456void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003457{
Dave Airlief453ba02008-11-07 14:05:41 -08003458 struct drm_device *dev = priv->minor->dev;
3459 struct drm_framebuffer *fb, *tfb;
3460
Daniel Vetter1b116292014-09-24 21:51:06 +02003461 /*
3462 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003463 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003464 * avoid upsetting lockdep since the universal cursor code adds a
3465 * framebuffer while holding mutex locks.
3466 *
3467 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3468 * locks is impossible here since no one else but this function can get
3469 * at it any more.
3470 */
Dave Airlief453ba02008-11-07 14:05:41 -08003471 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003472
3473 mutex_lock(&dev->mode_config.fb_lock);
3474 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3475 __drm_framebuffer_unregister(dev, fb);
3476 mutex_unlock(&dev->mode_config.fb_lock);
3477
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003478 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003479
3480 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003481 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003482 }
Dave Airlief453ba02008-11-07 14:05:41 -08003483}
3484
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003485/**
3486 * drm_property_create - create a new property type
3487 * @dev: drm device
3488 * @flags: flags specifying the property type
3489 * @name: name of the property
3490 * @num_values: number of pre-defined values
3491 *
3492 * This creates a new generic drm property which can then be attached to a drm
3493 * object with drm_object_attach_property. The returned property object must be
3494 * freed with drm_property_destroy.
3495 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003496 * Note that the DRM core keeps a per-device list of properties and that, if
3497 * drm_mode_config_cleanup() is called, it will destroy all properties created
3498 * by the driver.
3499 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003500 * Returns:
3501 * A pointer to the newly created property on success, NULL on failure.
3502 */
Dave Airlief453ba02008-11-07 14:05:41 -08003503struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3504 const char *name, int num_values)
3505{
3506 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003507 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003508
3509 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3510 if (!property)
3511 return NULL;
3512
Rob Clark98f75de2014-05-30 11:37:03 -04003513 property->dev = dev;
3514
Dave Airlief453ba02008-11-07 14:05:41 -08003515 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003516 property->values = kcalloc(num_values, sizeof(uint64_t),
3517 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003518 if (!property->values)
3519 goto fail;
3520 }
3521
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003522 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3523 if (ret)
3524 goto fail;
3525
Dave Airlief453ba02008-11-07 14:05:41 -08003526 property->flags = flags;
3527 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003528 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003529
Vinson Lee471dd2e2011-11-10 11:55:40 -08003530 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003531 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003532 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3533 }
Dave Airlief453ba02008-11-07 14:05:41 -08003534
3535 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003536
3537 WARN_ON(!drm_property_type_valid(property));
3538
Dave Airlief453ba02008-11-07 14:05:41 -08003539 return property;
3540fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003541 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003542 kfree(property);
3543 return NULL;
3544}
3545EXPORT_SYMBOL(drm_property_create);
3546
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003547/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003548 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003549 * @dev: drm device
3550 * @flags: flags specifying the property type
3551 * @name: name of the property
3552 * @props: enumeration lists with property values
3553 * @num_values: number of pre-defined values
3554 *
3555 * This creates a new generic drm property which can then be attached to a drm
3556 * object with drm_object_attach_property. The returned property object must be
3557 * freed with drm_property_destroy.
3558 *
3559 * Userspace is only allowed to set one of the predefined values for enumeration
3560 * properties.
3561 *
3562 * Returns:
3563 * A pointer to the newly created property on success, NULL on failure.
3564 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003565struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3566 const char *name,
3567 const struct drm_prop_enum_list *props,
3568 int num_values)
3569{
3570 struct drm_property *property;
3571 int i, ret;
3572
3573 flags |= DRM_MODE_PROP_ENUM;
3574
3575 property = drm_property_create(dev, flags, name, num_values);
3576 if (!property)
3577 return NULL;
3578
3579 for (i = 0; i < num_values; i++) {
3580 ret = drm_property_add_enum(property, i,
3581 props[i].type,
3582 props[i].name);
3583 if (ret) {
3584 drm_property_destroy(dev, property);
3585 return NULL;
3586 }
3587 }
3588
3589 return property;
3590}
3591EXPORT_SYMBOL(drm_property_create_enum);
3592
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003593/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003594 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003595 * @dev: drm device
3596 * @flags: flags specifying the property type
3597 * @name: name of the property
3598 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003599 * @num_props: size of the @props array
3600 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003601 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003602 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003603 * object with drm_object_attach_property. The returned property object must be
3604 * freed with drm_property_destroy.
3605 *
3606 * Compared to plain enumeration properties userspace is allowed to set any
3607 * or'ed together combination of the predefined property bitflag values
3608 *
3609 * Returns:
3610 * A pointer to the newly created property on success, NULL on failure.
3611 */
Rob Clark49e27542012-05-17 02:23:26 -06003612struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3613 int flags, const char *name,
3614 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303615 int num_props,
3616 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003617{
3618 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303619 int i, ret, index = 0;
3620 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003621
3622 flags |= DRM_MODE_PROP_BITMASK;
3623
3624 property = drm_property_create(dev, flags, name, num_values);
3625 if (!property)
3626 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303627 for (i = 0; i < num_props; i++) {
3628 if (!(supported_bits & (1ULL << props[i].type)))
3629 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003630
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303631 if (WARN_ON(index >= num_values)) {
3632 drm_property_destroy(dev, property);
3633 return NULL;
3634 }
3635
3636 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003637 props[i].type,
3638 props[i].name);
3639 if (ret) {
3640 drm_property_destroy(dev, property);
3641 return NULL;
3642 }
3643 }
3644
3645 return property;
3646}
3647EXPORT_SYMBOL(drm_property_create_bitmask);
3648
Rob Clarkebc44cf2012-09-12 22:22:31 -05003649static struct drm_property *property_create_range(struct drm_device *dev,
3650 int flags, const char *name,
3651 uint64_t min, uint64_t max)
3652{
3653 struct drm_property *property;
3654
3655 property = drm_property_create(dev, flags, name, 2);
3656 if (!property)
3657 return NULL;
3658
3659 property->values[0] = min;
3660 property->values[1] = max;
3661
3662 return property;
3663}
3664
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003665/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003666 * drm_property_create_range - create a new ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003667 * @dev: drm device
3668 * @flags: flags specifying the property type
3669 * @name: name of the property
3670 * @min: minimum value of the property
3671 * @max: maximum value of the property
3672 *
3673 * This creates a new generic drm property which can then be attached to a drm
3674 * object with drm_object_attach_property. The returned property object must be
3675 * freed with drm_property_destroy.
3676 *
Masanari Iida32197aa2014-10-20 23:53:13 +09003677 * Userspace is allowed to set any integer value in the (min, max) range
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003678 * inclusive.
3679 *
3680 * Returns:
3681 * A pointer to the newly created property on success, NULL on failure.
3682 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003683struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3684 const char *name,
3685 uint64_t min, uint64_t max)
3686{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003687 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3688 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003689}
3690EXPORT_SYMBOL(drm_property_create_range);
3691
Rob Clarkebc44cf2012-09-12 22:22:31 -05003692struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3693 int flags, const char *name,
3694 int64_t min, int64_t max)
3695{
3696 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3697 name, I642U64(min), I642U64(max));
3698}
3699EXPORT_SYMBOL(drm_property_create_signed_range);
3700
Rob Clark98f75de2014-05-30 11:37:03 -04003701struct drm_property *drm_property_create_object(struct drm_device *dev,
3702 int flags, const char *name, uint32_t type)
3703{
3704 struct drm_property *property;
3705
3706 flags |= DRM_MODE_PROP_OBJECT;
3707
3708 property = drm_property_create(dev, flags, name, 1);
3709 if (!property)
3710 return NULL;
3711
3712 property->values[0] = type;
3713
3714 return property;
3715}
3716EXPORT_SYMBOL(drm_property_create_object);
3717
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003718/**
3719 * drm_property_add_enum - add a possible value to an enumeration property
3720 * @property: enumeration property to change
3721 * @index: index of the new enumeration
3722 * @value: value of the new enumeration
3723 * @name: symbolic name of the new enumeration
3724 *
3725 * This functions adds enumerations to a property.
3726 *
3727 * It's use is deprecated, drivers should use one of the more specific helpers
3728 * to directly create the property with all enumerations already attached.
3729 *
3730 * Returns:
3731 * Zero on success, error code on failure.
3732 */
Dave Airlief453ba02008-11-07 14:05:41 -08003733int drm_property_add_enum(struct drm_property *property, int index,
3734 uint64_t value, const char *name)
3735{
3736 struct drm_property_enum *prop_enum;
3737
Rob Clark5ea22f22014-05-30 11:34:01 -04003738 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3739 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003740 return -EINVAL;
3741
3742 /*
3743 * Bitmask enum properties have the additional constraint of values
3744 * from 0 to 63
3745 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003746 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3747 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003748 return -EINVAL;
3749
Daniel Vetter3758b342014-11-19 18:38:10 +01003750 if (!list_empty(&property->enum_list)) {
3751 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003752 if (prop_enum->value == value) {
3753 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3754 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3755 return 0;
3756 }
3757 }
3758 }
3759
3760 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3761 if (!prop_enum)
3762 return -ENOMEM;
3763
3764 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3765 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3766 prop_enum->value = value;
3767
3768 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01003769 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003770 return 0;
3771}
3772EXPORT_SYMBOL(drm_property_add_enum);
3773
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003774/**
3775 * drm_property_destroy - destroy a drm property
3776 * @dev: drm device
3777 * @property: property to destry
3778 *
3779 * This function frees a property including any attached resources like
3780 * enumeration values.
3781 */
Dave Airlief453ba02008-11-07 14:05:41 -08003782void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3783{
3784 struct drm_property_enum *prop_enum, *pt;
3785
Daniel Vetter3758b342014-11-19 18:38:10 +01003786 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003787 list_del(&prop_enum->head);
3788 kfree(prop_enum);
3789 }
3790
3791 if (property->num_values)
3792 kfree(property->values);
3793 drm_mode_object_put(dev, &property->base);
3794 list_del(&property->head);
3795 kfree(property);
3796}
3797EXPORT_SYMBOL(drm_property_destroy);
3798
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003799/**
3800 * drm_object_attach_property - attach a property to a modeset object
3801 * @obj: drm modeset object
3802 * @property: property to attach
3803 * @init_val: initial value of the property
3804 *
3805 * This attaches the given property to the modeset object with the given initial
3806 * value. Currently this function cannot fail since the properties are stored in
3807 * a statically sized array.
3808 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003809void drm_object_attach_property(struct drm_mode_object *obj,
3810 struct drm_property *property,
3811 uint64_t init_val)
3812{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003813 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003814
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003815 if (count == DRM_OBJECT_MAX_PROPERTY) {
3816 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3817 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3818 "you see this message on the same object type.\n",
3819 obj->type);
3820 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003821 }
3822
Rob Clarkb17cd752014-12-16 18:05:30 -05003823 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003824 obj->properties->values[count] = init_val;
3825 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003826}
3827EXPORT_SYMBOL(drm_object_attach_property);
3828
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003829/**
3830 * drm_object_property_set_value - set the value of a property
3831 * @obj: drm mode object to set property value for
3832 * @property: property to set
3833 * @val: value the property should be set to
3834 *
3835 * This functions sets a given property on a given object. This function only
3836 * changes the software state of the property, it does not call into the
3837 * driver's ->set_property callback.
3838 *
3839 * Returns:
3840 * Zero on success, error code on failure.
3841 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003842int drm_object_property_set_value(struct drm_mode_object *obj,
3843 struct drm_property *property, uint64_t val)
3844{
3845 int i;
3846
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003847 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05003848 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003849 obj->properties->values[i] = val;
3850 return 0;
3851 }
3852 }
3853
3854 return -EINVAL;
3855}
3856EXPORT_SYMBOL(drm_object_property_set_value);
3857
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003858/**
3859 * drm_object_property_get_value - retrieve the value of a property
3860 * @obj: drm mode object to get property value from
3861 * @property: property to retrieve
3862 * @val: storage for the property value
3863 *
3864 * This function retrieves the softare state of the given property for the given
3865 * property. Since there is no driver callback to retrieve the current property
3866 * value this might be out of sync with the hardware, depending upon the driver
3867 * and property.
3868 *
3869 * Returns:
3870 * Zero on success, error code on failure.
3871 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003872int drm_object_property_get_value(struct drm_mode_object *obj,
3873 struct drm_property *property, uint64_t *val)
3874{
3875 int i;
3876
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003877 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05003878 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003879 *val = obj->properties->values[i];
3880 return 0;
3881 }
3882 }
3883
3884 return -EINVAL;
3885}
3886EXPORT_SYMBOL(drm_object_property_get_value);
3887
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003888/**
Daniel Vetter1a498632014-11-19 18:38:09 +01003889 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003890 * @dev: DRM device
3891 * @data: ioctl data
3892 * @file_priv: DRM file info
3893 *
Daniel Vetter1a498632014-11-19 18:38:09 +01003894 * This function retrieves the metadata for a given property, like the different
3895 * possible values for an enum property or the limits for a range property.
3896 *
3897 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003898 *
3899 * Called by the user via ioctl.
3900 *
3901 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003902 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003903 */
Dave Airlief453ba02008-11-07 14:05:41 -08003904int drm_mode_getproperty_ioctl(struct drm_device *dev,
3905 void *data, struct drm_file *file_priv)
3906{
Dave Airlief453ba02008-11-07 14:05:41 -08003907 struct drm_mode_get_property *out_resp = data;
3908 struct drm_property *property;
3909 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003910 int value_count = 0;
3911 int ret = 0, i;
3912 int copied;
3913 struct drm_property_enum *prop_enum;
3914 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003915 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003916
Dave Airliefb3b06c2011-02-08 13:55:21 +10003917 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3918 return -EINVAL;
3919
Daniel Vetter84849902012-12-02 00:28:11 +01003920 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003921 property = drm_property_find(dev, out_resp->prop_id);
3922 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003923 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003924 goto done;
3925 }
Dave Airlief453ba02008-11-07 14:05:41 -08003926
Rob Clark5ea22f22014-05-30 11:34:01 -04003927 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3928 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01003929 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08003930 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08003931 }
3932
3933 value_count = property->num_values;
3934
3935 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3936 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3937 out_resp->flags = property->flags;
3938
3939 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003940 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003941 for (i = 0; i < value_count; i++) {
3942 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3943 ret = -EFAULT;
3944 goto done;
3945 }
3946 }
3947 }
3948 out_resp->count_values = value_count;
3949
Rob Clark5ea22f22014-05-30 11:34:01 -04003950 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3951 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003952 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3953 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003954 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01003955 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003956
3957 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3958 ret = -EFAULT;
3959 goto done;
3960 }
3961
3962 if (copy_to_user(&enum_ptr[copied].name,
3963 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3964 ret = -EFAULT;
3965 goto done;
3966 }
3967 copied++;
3968 }
3969 }
3970 out_resp->count_enum_blobs = enum_count;
3971 }
3972
Daniel Vetter3758b342014-11-19 18:38:10 +01003973 /*
3974 * NOTE: The idea seems to have been to use this to read all the blob
3975 * property values. But nothing ever added them to the corresponding
3976 * list, userspace always used the special-purpose get_blob ioctl to
3977 * read the value for a blob property. It also doesn't make a lot of
3978 * sense to return values here when everything else is just metadata for
3979 * the property itself.
3980 */
3981 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
3982 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003983done:
Daniel Vetter84849902012-12-02 00:28:11 +01003984 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003985 return ret;
3986}
3987
Thierry Redingecbbe592014-05-13 11:36:13 +02003988static struct drm_property_blob *
3989drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02003990 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08003991{
3992 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003993 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003994
3995 if (!length || !data)
3996 return NULL;
3997
3998 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3999 if (!blob)
4000 return NULL;
4001
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004002 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4003 if (ret) {
4004 kfree(blob);
4005 return NULL;
4006 }
4007
Dave Airlief453ba02008-11-07 14:05:41 -08004008 blob->length = length;
4009
4010 memcpy(blob->data, data, length);
4011
Dave Airlief453ba02008-11-07 14:05:41 -08004012 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
4013 return blob;
4014}
4015
4016static void drm_property_destroy_blob(struct drm_device *dev,
4017 struct drm_property_blob *blob)
4018{
4019 drm_mode_object_put(dev, &blob->base);
4020 list_del(&blob->head);
4021 kfree(blob);
4022}
4023
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004024/**
4025 * drm_mode_getblob_ioctl - get the contents of a blob property value
4026 * @dev: DRM device
4027 * @data: ioctl data
4028 * @file_priv: DRM file info
4029 *
4030 * This function retrieves the contents of a blob property. The value stored in
4031 * an object's blob property is just a normal modeset object id.
4032 *
4033 * Called by the user via ioctl.
4034 *
4035 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004036 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004037 */
Dave Airlief453ba02008-11-07 14:05:41 -08004038int drm_mode_getblob_ioctl(struct drm_device *dev,
4039 void *data, struct drm_file *file_priv)
4040{
Dave Airlief453ba02008-11-07 14:05:41 -08004041 struct drm_mode_get_blob *out_resp = data;
4042 struct drm_property_blob *blob;
4043 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004044 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004045
Dave Airliefb3b06c2011-02-08 13:55:21 +10004046 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4047 return -EINVAL;
4048
Daniel Vetter84849902012-12-02 00:28:11 +01004049 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004050 blob = drm_property_blob_find(dev, out_resp->blob_id);
4051 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004052 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004053 goto done;
4054 }
Dave Airlief453ba02008-11-07 14:05:41 -08004055
4056 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004057 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004058 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004059 ret = -EFAULT;
4060 goto done;
4061 }
4062 }
4063 out_resp->length = blob->length;
4064
4065done:
Daniel Vetter84849902012-12-02 00:28:11 +01004066 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004067 return ret;
4068}
4069
Dave Airliecc7096f2014-10-22 12:03:04 +10004070/**
4071 * drm_mode_connector_set_path_property - set tile property on connector
4072 * @connector: connector to set property on.
4073 * @path: path to use for property.
4074 *
4075 * This creates a property to expose to userspace to specify a
4076 * connector path. This is mainly used for DisplayPort MST where
4077 * connectors have a topology and we want to allow userspace to give
4078 * them more meaningful names.
4079 *
4080 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004081 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004082 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004083int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004084 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004085{
4086 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004087 size_t size = strlen(path) + 1;
4088 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004089
4090 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4091 size, path);
4092 if (!connector->path_blob_ptr)
4093 return -EINVAL;
4094
4095 ret = drm_object_property_set_value(&connector->base,
4096 dev->mode_config.path_property,
4097 connector->path_blob_ptr->base.id);
4098 return ret;
4099}
4100EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4101
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004102/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004103 * drm_mode_connector_set_tile_property - set tile property on connector
4104 * @connector: connector to set property on.
4105 *
4106 * This looks up the tile information for a connector, and creates a
4107 * property for userspace to parse if it exists. The property is of
4108 * the form of 8 integers using ':' as a separator.
4109 *
4110 * Returns:
4111 * Zero on success, errno on failure.
4112 */
4113int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4114{
4115 struct drm_device *dev = connector->dev;
4116 int ret, size;
4117 char tile[256];
4118
4119 if (connector->tile_blob_ptr)
4120 drm_property_destroy_blob(dev, connector->tile_blob_ptr);
4121
4122 if (!connector->has_tile) {
4123 connector->tile_blob_ptr = NULL;
4124 ret = drm_object_property_set_value(&connector->base,
4125 dev->mode_config.tile_property, 0);
4126 return ret;
4127 }
4128
4129 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4130 connector->tile_group->id, connector->tile_is_single_monitor,
4131 connector->num_h_tile, connector->num_v_tile,
4132 connector->tile_h_loc, connector->tile_v_loc,
4133 connector->tile_h_size, connector->tile_v_size);
4134 size = strlen(tile) + 1;
4135
4136 connector->tile_blob_ptr = drm_property_create_blob(connector->dev,
4137 size, tile);
4138 if (!connector->tile_blob_ptr)
4139 return -EINVAL;
4140
4141 ret = drm_object_property_set_value(&connector->base,
4142 dev->mode_config.tile_property,
4143 connector->tile_blob_ptr->base.id);
4144 return ret;
4145}
4146EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4147
4148/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004149 * drm_mode_connector_update_edid_property - update the edid property of a connector
4150 * @connector: drm connector
4151 * @edid: new value of the edid property
4152 *
4153 * This function creates a new blob modeset object and assigns its id to the
4154 * connector's edid property.
4155 *
4156 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004157 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004158 */
Dave Airlief453ba02008-11-07 14:05:41 -08004159int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004160 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004161{
4162 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004163 size_t size;
4164 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004165
Thomas Wood4cf2b282014-06-18 17:52:33 +01004166 /* ignore requests to set edid when overridden */
4167 if (connector->override_edid)
4168 return 0;
4169
Dave Airlief453ba02008-11-07 14:05:41 -08004170 if (connector->edid_blob_ptr)
4171 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4172
4173 /* Delete edid, when there is none. */
4174 if (!edid) {
4175 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05004176 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08004177 return ret;
4178 }
4179
Adam Jackson7466f4c2010-03-29 21:43:23 +00004180 size = EDID_LENGTH * (1 + edid->extensions);
4181 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4182 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00004183 if (!connector->edid_blob_ptr)
4184 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08004185
Rob Clark58495562012-10-11 20:50:56 -05004186 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08004187 dev->mode_config.edid_property,
4188 connector->edid_blob_ptr->base.id);
4189
4190 return ret;
4191}
4192EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4193
Rob Clark3843e712014-12-16 18:05:29 -05004194/* Some properties could refer to dynamic refcnt'd objects, or things that
4195 * need special locking to handle lifetime issues (ie. to ensure the prop
4196 * value doesn't become invalid part way through the property update due to
4197 * race). The value returned by reference via 'obj' should be passed back
4198 * to drm_property_change_valid_put() after the property is set (and the
4199 * object to which the property is attached has a chance to take it's own
4200 * reference).
4201 */
4202static bool drm_property_change_valid_get(struct drm_property *property,
4203 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004204{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004205 int i;
4206
Paulo Zanoni26a34812012-05-15 18:08:59 -03004207 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4208 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004209
Rob Clark3843e712014-12-16 18:05:29 -05004210 *ref = NULL;
4211
Rob Clark5ea22f22014-05-30 11:34:01 -04004212 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004213 if (value < property->values[0] || value > property->values[1])
4214 return false;
4215 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004216 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4217 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004218
Rob Clarkebc44cf2012-09-12 22:22:31 -05004219 if (svalue < U642I64(property->values[0]) ||
4220 svalue > U642I64(property->values[1]))
4221 return false;
4222 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004223 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004224 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004225
Rob Clark49e27542012-05-17 02:23:26 -06004226 for (i = 0; i < property->num_values; i++)
4227 valid_mask |= (1ULL << property->values[i]);
4228 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004229 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004230 /* Only the driver knows */
4231 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004232 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004233 /* a zero value for an object property translates to null: */
4234 if (value == 0)
4235 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004236
4237 /* handle refcnt'd objects specially: */
4238 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4239 struct drm_framebuffer *fb;
4240 fb = drm_framebuffer_lookup(property->dev, value);
4241 if (fb) {
4242 *ref = &fb->base;
4243 return true;
4244 } else {
4245 return false;
4246 }
4247 } else {
4248 return _object_find(property->dev, value, property->values[0]) != NULL;
4249 }
Paulo Zanoni26a34812012-05-15 18:08:59 -03004250 } else {
4251 int i;
4252 for (i = 0; i < property->num_values; i++)
4253 if (property->values[i] == value)
4254 return true;
4255 return false;
4256 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004257
4258 for (i = 0; i < property->num_values; i++)
4259 if (property->values[i] == value)
4260 return true;
4261 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004262}
4263
Rob Clark3843e712014-12-16 18:05:29 -05004264static void drm_property_change_valid_put(struct drm_property *property,
4265 struct drm_mode_object *ref)
4266{
4267 if (!ref)
4268 return;
4269
4270 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4271 if (property->values[0] == DRM_MODE_OBJECT_FB)
4272 drm_framebuffer_unreference(obj_to_fb(ref));
4273 }
4274}
4275
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004276/**
4277 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4278 * @dev: DRM device
4279 * @data: ioctl data
4280 * @file_priv: DRM file info
4281 *
4282 * This function sets the current value for a connectors's property. It also
4283 * calls into a driver's ->set_property callback to update the hardware state
4284 *
4285 * Called by the user via ioctl.
4286 *
4287 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004288 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004289 */
Dave Airlief453ba02008-11-07 14:05:41 -08004290int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4291 void *data, struct drm_file *file_priv)
4292{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004293 struct drm_mode_connector_set_property *conn_set_prop = data;
4294 struct drm_mode_obj_set_property obj_set_prop = {
4295 .value = conn_set_prop->value,
4296 .prop_id = conn_set_prop->prop_id,
4297 .obj_id = conn_set_prop->connector_id,
4298 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4299 };
Dave Airlief453ba02008-11-07 14:05:41 -08004300
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004301 /* It does all the locking and checking we need */
4302 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004303}
4304
Paulo Zanonic5431882012-05-15 18:09:02 -03004305static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4306 struct drm_property *property,
4307 uint64_t value)
4308{
4309 int ret = -EINVAL;
4310 struct drm_connector *connector = obj_to_connector(obj);
4311
4312 /* Do DPMS ourselves */
4313 if (property == connector->dev->mode_config.dpms_property) {
4314 if (connector->funcs->dpms)
4315 (*connector->funcs->dpms)(connector, (int)value);
4316 ret = 0;
4317 } else if (connector->funcs->set_property)
4318 ret = connector->funcs->set_property(connector, property, value);
4319
4320 /* store the property value if successful */
4321 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004322 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004323 return ret;
4324}
4325
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004326static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4327 struct drm_property *property,
4328 uint64_t value)
4329{
4330 int ret = -EINVAL;
4331 struct drm_crtc *crtc = obj_to_crtc(obj);
4332
4333 if (crtc->funcs->set_property)
4334 ret = crtc->funcs->set_property(crtc, property, value);
4335 if (!ret)
4336 drm_object_property_set_value(obj, property, value);
4337
4338 return ret;
4339}
4340
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004341/**
4342 * drm_mode_plane_set_obj_prop - set the value of a property
4343 * @plane: drm plane object to set property value for
4344 * @property: property to set
4345 * @value: value the property should be set to
4346 *
4347 * This functions sets a given property on a given plane object. This function
4348 * calls the driver's ->set_property callback and changes the software state of
4349 * the property if the callback succeeds.
4350 *
4351 * Returns:
4352 * Zero on success, error code on failure.
4353 */
4354int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4355 struct drm_property *property,
4356 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004357{
4358 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004359 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004360
4361 if (plane->funcs->set_property)
4362 ret = plane->funcs->set_property(plane, property, value);
4363 if (!ret)
4364 drm_object_property_set_value(obj, property, value);
4365
4366 return ret;
4367}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004368EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004369
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004370/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004371 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004372 * @dev: DRM device
4373 * @data: ioctl data
4374 * @file_priv: DRM file info
4375 *
4376 * This function retrieves the current value for an object's property. Compared
4377 * to the connector specific ioctl this one is extended to also work on crtc and
4378 * plane objects.
4379 *
4380 * Called by the user via ioctl.
4381 *
4382 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004383 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004384 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004385int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4386 struct drm_file *file_priv)
4387{
4388 struct drm_mode_obj_get_properties *arg = data;
4389 struct drm_mode_object *obj;
4390 int ret = 0;
4391 int i;
4392 int copied = 0;
4393 int props_count = 0;
4394 uint32_t __user *props_ptr;
4395 uint64_t __user *prop_values_ptr;
4396
4397 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4398 return -EINVAL;
4399
Daniel Vetter84849902012-12-02 00:28:11 +01004400 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004401
4402 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4403 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004404 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004405 goto out;
4406 }
4407 if (!obj->properties) {
4408 ret = -EINVAL;
4409 goto out;
4410 }
4411
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004412 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004413
4414 /* This ioctl is called twice, once to determine how much space is
4415 * needed, and the 2nd time to fill it. */
4416 if ((arg->count_props >= props_count) && props_count) {
4417 copied = 0;
4418 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4419 prop_values_ptr = (uint64_t __user *)(unsigned long)
4420 (arg->prop_values_ptr);
4421 for (i = 0; i < props_count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004422 struct drm_property *prop = obj->properties->properties[i];
Rob Clark22b8b132014-12-16 18:05:31 -05004423 uint64_t val;
4424
4425 ret = drm_object_property_get_value(obj, prop, &val);
4426 if (ret)
4427 goto out;
4428
Rob Clarkb17cd752014-12-16 18:05:30 -05004429 if (put_user(prop->base.id, props_ptr + copied)) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004430 ret = -EFAULT;
4431 goto out;
4432 }
Rob Clark22b8b132014-12-16 18:05:31 -05004433
4434 if (put_user(val, prop_values_ptr + copied)) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004435 ret = -EFAULT;
4436 goto out;
4437 }
4438 copied++;
4439 }
4440 }
4441 arg->count_props = props_count;
4442out:
Daniel Vetter84849902012-12-02 00:28:11 +01004443 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004444 return ret;
4445}
4446
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004447/**
4448 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4449 * @dev: DRM device
4450 * @data: ioctl data
4451 * @file_priv: DRM file info
4452 *
4453 * This function sets the current value for an object's property. It also calls
4454 * into a driver's ->set_property callback to update the hardware state.
4455 * Compared to the connector specific ioctl this one is extended to also work on
4456 * crtc and plane objects.
4457 *
4458 * Called by the user via ioctl.
4459 *
4460 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004461 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004462 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004463int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4464 struct drm_file *file_priv)
4465{
4466 struct drm_mode_obj_set_property *arg = data;
4467 struct drm_mode_object *arg_obj;
4468 struct drm_mode_object *prop_obj;
4469 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05004470 int i, ret = -EINVAL;
4471 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004472
4473 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4474 return -EINVAL;
4475
Daniel Vetter84849902012-12-02 00:28:11 +01004476 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004477
4478 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004479 if (!arg_obj) {
4480 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004481 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004482 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004483 if (!arg_obj->properties)
4484 goto out;
4485
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004486 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05004487 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03004488 break;
4489
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004490 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004491 goto out;
4492
4493 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4494 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004495 if (!prop_obj) {
4496 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004497 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004498 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004499 property = obj_to_property(prop_obj);
4500
Rob Clark3843e712014-12-16 18:05:29 -05004501 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03004502 goto out;
4503
4504 switch (arg_obj->type) {
4505 case DRM_MODE_OBJECT_CONNECTOR:
4506 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4507 arg->value);
4508 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004509 case DRM_MODE_OBJECT_CRTC:
4510 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4511 break;
Rob Clark4d939142012-05-17 02:23:27 -06004512 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004513 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4514 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06004515 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004516 }
4517
Rob Clark3843e712014-12-16 18:05:29 -05004518 drm_property_change_valid_put(property, ref);
4519
Paulo Zanonic5431882012-05-15 18:09:02 -03004520out:
Daniel Vetter84849902012-12-02 00:28:11 +01004521 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004522 return ret;
4523}
4524
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004525/**
4526 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4527 * @connector: connector to attach
4528 * @encoder: encoder to attach @connector to
4529 *
4530 * This function links up a connector to an encoder. Note that the routing
4531 * restrictions between encoders and crtcs are exposed to userspace through the
4532 * possible_clones and possible_crtcs bitmasks.
4533 *
4534 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004535 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004536 */
Dave Airlief453ba02008-11-07 14:05:41 -08004537int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4538 struct drm_encoder *encoder)
4539{
4540 int i;
4541
4542 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4543 if (connector->encoder_ids[i] == 0) {
4544 connector->encoder_ids[i] = encoder->base.id;
4545 return 0;
4546 }
4547 }
4548 return -ENOMEM;
4549}
4550EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4551
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004552/**
4553 * drm_mode_crtc_set_gamma_size - set the gamma table size
4554 * @crtc: CRTC to set the gamma table size for
4555 * @gamma_size: size of the gamma table
4556 *
4557 * Drivers which support gamma tables should set this to the supported gamma
4558 * table size when initializing the CRTC. Currently the drm core only supports a
4559 * fixed gamma table size.
4560 *
4561 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004562 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004563 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004564int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004565 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004566{
4567 crtc->gamma_size = gamma_size;
4568
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01004569 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4570 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08004571 if (!crtc->gamma_store) {
4572 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004573 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004574 }
4575
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004576 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004577}
4578EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4579
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004580/**
4581 * drm_mode_gamma_set_ioctl - set the gamma table
4582 * @dev: DRM device
4583 * @data: ioctl data
4584 * @file_priv: DRM file info
4585 *
4586 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4587 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4588 *
4589 * Called by the user via ioctl.
4590 *
4591 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004592 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004593 */
Dave Airlief453ba02008-11-07 14:05:41 -08004594int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4595 void *data, struct drm_file *file_priv)
4596{
4597 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004598 struct drm_crtc *crtc;
4599 void *r_base, *g_base, *b_base;
4600 int size;
4601 int ret = 0;
4602
Dave Airliefb3b06c2011-02-08 13:55:21 +10004603 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4604 return -EINVAL;
4605
Daniel Vetter84849902012-12-02 00:28:11 +01004606 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004607 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4608 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004609 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004610 goto out;
4611 }
Dave Airlief453ba02008-11-07 14:05:41 -08004612
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004613 if (crtc->funcs->gamma_set == NULL) {
4614 ret = -ENOSYS;
4615 goto out;
4616 }
4617
Dave Airlief453ba02008-11-07 14:05:41 -08004618 /* memcpy into gamma store */
4619 if (crtc_lut->gamma_size != crtc->gamma_size) {
4620 ret = -EINVAL;
4621 goto out;
4622 }
4623
4624 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4625 r_base = crtc->gamma_store;
4626 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4627 ret = -EFAULT;
4628 goto out;
4629 }
4630
4631 g_base = r_base + size;
4632 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4633 ret = -EFAULT;
4634 goto out;
4635 }
4636
4637 b_base = g_base + size;
4638 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4639 ret = -EFAULT;
4640 goto out;
4641 }
4642
James Simmons72034252010-08-03 01:33:19 +01004643 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004644
4645out:
Daniel Vetter84849902012-12-02 00:28:11 +01004646 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004647 return ret;
4648
4649}
4650
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004651/**
4652 * drm_mode_gamma_get_ioctl - get the gamma table
4653 * @dev: DRM device
4654 * @data: ioctl data
4655 * @file_priv: DRM file info
4656 *
4657 * Copy the current gamma table into the storage provided. This also provides
4658 * the gamma table size the driver expects, which can be used to size the
4659 * allocated storage.
4660 *
4661 * Called by the user via ioctl.
4662 *
4663 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004664 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004665 */
Dave Airlief453ba02008-11-07 14:05:41 -08004666int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4667 void *data, struct drm_file *file_priv)
4668{
4669 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004670 struct drm_crtc *crtc;
4671 void *r_base, *g_base, *b_base;
4672 int size;
4673 int ret = 0;
4674
Dave Airliefb3b06c2011-02-08 13:55:21 +10004675 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4676 return -EINVAL;
4677
Daniel Vetter84849902012-12-02 00:28:11 +01004678 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004679 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4680 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004681 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004682 goto out;
4683 }
Dave Airlief453ba02008-11-07 14:05:41 -08004684
4685 /* memcpy into gamma store */
4686 if (crtc_lut->gamma_size != crtc->gamma_size) {
4687 ret = -EINVAL;
4688 goto out;
4689 }
4690
4691 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4692 r_base = crtc->gamma_store;
4693 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4694 ret = -EFAULT;
4695 goto out;
4696 }
4697
4698 g_base = r_base + size;
4699 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4700 ret = -EFAULT;
4701 goto out;
4702 }
4703
4704 b_base = g_base + size;
4705 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4706 ret = -EFAULT;
4707 goto out;
4708 }
4709out:
Daniel Vetter84849902012-12-02 00:28:11 +01004710 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004711 return ret;
4712}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004713
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004714/**
4715 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4716 * @dev: DRM device
4717 * @data: ioctl data
4718 * @file_priv: DRM file info
4719 *
4720 * This schedules an asynchronous update on a given CRTC, called page flip.
4721 * Optionally a drm event is generated to signal the completion of the event.
4722 * Generic drivers cannot assume that a pageflip with changed framebuffer
4723 * properties (including driver specific metadata like tiling layout) will work,
4724 * but some drivers support e.g. pixel format changes through the pageflip
4725 * ioctl.
4726 *
4727 * Called by the user via ioctl.
4728 *
4729 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004730 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004731 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004732int drm_mode_page_flip_ioctl(struct drm_device *dev,
4733 void *data, struct drm_file *file_priv)
4734{
4735 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004736 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02004737 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004738 struct drm_pending_vblank_event *e = NULL;
4739 unsigned long flags;
4740 int ret = -EINVAL;
4741
4742 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4743 page_flip->reserved != 0)
4744 return -EINVAL;
4745
Keith Packard62f21042013-07-22 18:50:00 -07004746 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4747 return -EINVAL;
4748
Rob Clarka2b34e22013-10-05 16:36:52 -04004749 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4750 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004751 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004752
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01004753 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07004754 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004755 /* The framebuffer is currently unbound, presumably
4756 * due to a hotplug event, that userspace has not
4757 * yet discovered.
4758 */
4759 ret = -EBUSY;
4760 goto out;
4761 }
4762
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004763 if (crtc->funcs->page_flip == NULL)
4764 goto out;
4765
Daniel Vetter786b99e2012-12-02 21:53:40 +01004766 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004767 if (!fb) {
4768 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004769 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004770 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004771
Damien Lespiauc11e9282013-09-25 16:45:30 +01004772 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4773 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004774 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004775
Matt Roperf4510a22014-04-01 15:22:40 -07004776 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004777 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4778 ret = -EINVAL;
4779 goto out;
4780 }
4781
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004782 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4783 ret = -ENOMEM;
4784 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004785 if (file_priv->event_space < sizeof(e->event)) {
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004786 spin_unlock_irqrestore(&dev->event_lock, flags);
4787 goto out;
4788 }
Thierry Redingf76511b2014-12-10 13:03:40 +01004789 file_priv->event_space -= sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004790 spin_unlock_irqrestore(&dev->event_lock, flags);
4791
Thierry Redingf76511b2014-12-10 13:03:40 +01004792 e = kzalloc(sizeof(*e), GFP_KERNEL);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004793 if (e == NULL) {
4794 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004795 file_priv->event_space += sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004796 spin_unlock_irqrestore(&dev->event_lock, flags);
4797 goto out;
4798 }
4799
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004800 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01004801 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004802 e->event.user_data = page_flip->user_data;
4803 e->base.event = &e->event.base;
4804 e->base.file_priv = file_priv;
4805 e->base.destroy =
4806 (void (*) (struct drm_pending_event *)) kfree;
4807 }
4808
Daniel Vetter3d30a592014-07-27 13:42:42 +02004809 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004810 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004811 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004812 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4813 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004814 file_priv->event_space += sizeof(e->event);
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004815 spin_unlock_irqrestore(&dev->event_lock, flags);
4816 kfree(e);
4817 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004818 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02004819 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004820 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004821 /*
4822 * Warn if the driver hasn't properly updated the crtc->fb
4823 * field to reflect that the new framebuffer is now used.
4824 * Failing to do so will screw with the reference counting
4825 * on framebuffers.
4826 */
Matt Roperf4510a22014-04-01 15:22:40 -07004827 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004828 /* Unref only the old framebuffer. */
4829 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004830 }
4831
4832out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004833 if (fb)
4834 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02004835 if (crtc->primary->old_fb)
4836 drm_framebuffer_unreference(crtc->primary->old_fb);
4837 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02004838 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004839
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004840 return ret;
4841}
Chris Wilsoneb033552011-01-24 15:11:08 +00004842
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004843/**
4844 * drm_mode_config_reset - call ->reset callbacks
4845 * @dev: drm device
4846 *
4847 * This functions calls all the crtc's, encoder's and connector's ->reset
4848 * callback. Drivers can use this in e.g. their driver load or resume code to
4849 * reset hardware and software state.
4850 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004851void drm_mode_config_reset(struct drm_device *dev)
4852{
4853 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004854 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00004855 struct drm_encoder *encoder;
4856 struct drm_connector *connector;
4857
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004858 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4859 if (plane->funcs->reset)
4860 plane->funcs->reset(plane);
4861
Chris Wilsoneb033552011-01-24 15:11:08 +00004862 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4863 if (crtc->funcs->reset)
4864 crtc->funcs->reset(crtc);
4865
4866 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4867 if (encoder->funcs->reset)
4868 encoder->funcs->reset(encoder);
4869
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004870 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4871 connector->status = connector_status_unknown;
4872
Chris Wilsoneb033552011-01-24 15:11:08 +00004873 if (connector->funcs->reset)
4874 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004875 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004876}
4877EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004878
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004879/**
4880 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4881 * @dev: DRM device
4882 * @data: ioctl data
4883 * @file_priv: DRM file info
4884 *
4885 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4886 * TTM or something else entirely) and returns the resulting buffer handle. This
4887 * handle can then be wrapped up into a framebuffer modeset object.
4888 *
4889 * Note that userspace is not allowed to use such objects for render
4890 * acceleration - drivers must create their own private ioctls for such a use
4891 * case.
4892 *
4893 * Called by the user via ioctl.
4894 *
4895 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004896 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004897 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004898int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4899 void *data, struct drm_file *file_priv)
4900{
4901 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004902 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004903
4904 if (!dev->driver->dumb_create)
4905 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004906 if (!args->width || !args->height || !args->bpp)
4907 return -EINVAL;
4908
4909 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02004910 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01004911 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02004912 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01004913 return -EINVAL;
4914 stride = cpp * args->width;
4915 if (args->height > 0xffffffffU / stride)
4916 return -EINVAL;
4917
4918 /* test for wrap-around */
4919 size = args->height * stride;
4920 if (PAGE_ALIGN(size) == 0)
4921 return -EINVAL;
4922
Thierry Redingf6085952014-11-03 11:14:14 +01004923 /*
4924 * handle, pitch and size are output parameters. Zero them out to
4925 * prevent drivers from accidentally using uninitialized data. Since
4926 * not all existing userspace is clearing these fields properly we
4927 * cannot reject IOCTL with garbage in them.
4928 */
4929 args->handle = 0;
4930 args->pitch = 0;
4931 args->size = 0;
4932
Dave Airlieff72145b2011-02-07 12:16:14 +10004933 return dev->driver->dumb_create(file_priv, dev, args);
4934}
4935
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004936/**
4937 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4938 * @dev: DRM device
4939 * @data: ioctl data
4940 * @file_priv: DRM file info
4941 *
4942 * Allocate an offset in the drm device node's address space to be able to
4943 * memory map a dumb buffer.
4944 *
4945 * Called by the user via ioctl.
4946 *
4947 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004948 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004949 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004950int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4951 void *data, struct drm_file *file_priv)
4952{
4953 struct drm_mode_map_dumb *args = data;
4954
4955 /* call driver ioctl to get mmap offset */
4956 if (!dev->driver->dumb_map_offset)
4957 return -ENOSYS;
4958
4959 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4960}
4961
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004962/**
4963 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4964 * @dev: DRM device
4965 * @data: ioctl data
4966 * @file_priv: DRM file info
4967 *
4968 * This destroys the userspace handle for the given dumb backing storage buffer.
4969 * Since buffer objects must be reference counted in the kernel a buffer object
4970 * won't be immediately freed if a framebuffer modeset object still uses it.
4971 *
4972 * Called by the user via ioctl.
4973 *
4974 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004975 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004976 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004977int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4978 void *data, struct drm_file *file_priv)
4979{
4980 struct drm_mode_destroy_dumb *args = data;
4981
4982 if (!dev->driver->dumb_destroy)
4983 return -ENOSYS;
4984
4985 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4986}
Dave Airlie248dbc22011-11-29 20:02:54 +00004987
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004988/**
4989 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4990 * @format: pixel format (DRM_FORMAT_*)
4991 * @depth: storage for the depth value
4992 * @bpp: storage for the bpp value
4993 *
4994 * This only supports RGB formats here for compat with code that doesn't use
4995 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004996 */
4997void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4998 int *bpp)
4999{
5000 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005001 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005002 case DRM_FORMAT_RGB332:
5003 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005004 *depth = 8;
5005 *bpp = 8;
5006 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005007 case DRM_FORMAT_XRGB1555:
5008 case DRM_FORMAT_XBGR1555:
5009 case DRM_FORMAT_RGBX5551:
5010 case DRM_FORMAT_BGRX5551:
5011 case DRM_FORMAT_ARGB1555:
5012 case DRM_FORMAT_ABGR1555:
5013 case DRM_FORMAT_RGBA5551:
5014 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005015 *depth = 15;
5016 *bpp = 16;
5017 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005018 case DRM_FORMAT_RGB565:
5019 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005020 *depth = 16;
5021 *bpp = 16;
5022 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005023 case DRM_FORMAT_RGB888:
5024 case DRM_FORMAT_BGR888:
5025 *depth = 24;
5026 *bpp = 24;
5027 break;
5028 case DRM_FORMAT_XRGB8888:
5029 case DRM_FORMAT_XBGR8888:
5030 case DRM_FORMAT_RGBX8888:
5031 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005032 *depth = 24;
5033 *bpp = 32;
5034 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005035 case DRM_FORMAT_XRGB2101010:
5036 case DRM_FORMAT_XBGR2101010:
5037 case DRM_FORMAT_RGBX1010102:
5038 case DRM_FORMAT_BGRX1010102:
5039 case DRM_FORMAT_ARGB2101010:
5040 case DRM_FORMAT_ABGR2101010:
5041 case DRM_FORMAT_RGBA1010102:
5042 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005043 *depth = 30;
5044 *bpp = 32;
5045 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005046 case DRM_FORMAT_ARGB8888:
5047 case DRM_FORMAT_ABGR8888:
5048 case DRM_FORMAT_RGBA8888:
5049 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005050 *depth = 32;
5051 *bpp = 32;
5052 break;
5053 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005054 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5055 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005056 *depth = 0;
5057 *bpp = 0;
5058 break;
5059 }
5060}
5061EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005062
5063/**
5064 * drm_format_num_planes - get the number of planes for format
5065 * @format: pixel format (DRM_FORMAT_*)
5066 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005067 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005068 * The number of planes used by the specified pixel format.
5069 */
5070int drm_format_num_planes(uint32_t format)
5071{
5072 switch (format) {
5073 case DRM_FORMAT_YUV410:
5074 case DRM_FORMAT_YVU410:
5075 case DRM_FORMAT_YUV411:
5076 case DRM_FORMAT_YVU411:
5077 case DRM_FORMAT_YUV420:
5078 case DRM_FORMAT_YVU420:
5079 case DRM_FORMAT_YUV422:
5080 case DRM_FORMAT_YVU422:
5081 case DRM_FORMAT_YUV444:
5082 case DRM_FORMAT_YVU444:
5083 return 3;
5084 case DRM_FORMAT_NV12:
5085 case DRM_FORMAT_NV21:
5086 case DRM_FORMAT_NV16:
5087 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005088 case DRM_FORMAT_NV24:
5089 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005090 return 2;
5091 default:
5092 return 1;
5093 }
5094}
5095EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005096
5097/**
5098 * drm_format_plane_cpp - determine the bytes per pixel value
5099 * @format: pixel format (DRM_FORMAT_*)
5100 * @plane: plane index
5101 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005102 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005103 * The bytes per pixel value for the specified plane.
5104 */
5105int drm_format_plane_cpp(uint32_t format, int plane)
5106{
5107 unsigned int depth;
5108 int bpp;
5109
5110 if (plane >= drm_format_num_planes(format))
5111 return 0;
5112
5113 switch (format) {
5114 case DRM_FORMAT_YUYV:
5115 case DRM_FORMAT_YVYU:
5116 case DRM_FORMAT_UYVY:
5117 case DRM_FORMAT_VYUY:
5118 return 2;
5119 case DRM_FORMAT_NV12:
5120 case DRM_FORMAT_NV21:
5121 case DRM_FORMAT_NV16:
5122 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005123 case DRM_FORMAT_NV24:
5124 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005125 return plane ? 2 : 1;
5126 case DRM_FORMAT_YUV410:
5127 case DRM_FORMAT_YVU410:
5128 case DRM_FORMAT_YUV411:
5129 case DRM_FORMAT_YVU411:
5130 case DRM_FORMAT_YUV420:
5131 case DRM_FORMAT_YVU420:
5132 case DRM_FORMAT_YUV422:
5133 case DRM_FORMAT_YVU422:
5134 case DRM_FORMAT_YUV444:
5135 case DRM_FORMAT_YVU444:
5136 return 1;
5137 default:
5138 drm_fb_get_bpp_depth(format, &depth, &bpp);
5139 return bpp >> 3;
5140 }
5141}
5142EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005143
5144/**
5145 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5146 * @format: pixel format (DRM_FORMAT_*)
5147 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005148 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005149 * The horizontal chroma subsampling factor for the
5150 * specified pixel format.
5151 */
5152int drm_format_horz_chroma_subsampling(uint32_t format)
5153{
5154 switch (format) {
5155 case DRM_FORMAT_YUV411:
5156 case DRM_FORMAT_YVU411:
5157 case DRM_FORMAT_YUV410:
5158 case DRM_FORMAT_YVU410:
5159 return 4;
5160 case DRM_FORMAT_YUYV:
5161 case DRM_FORMAT_YVYU:
5162 case DRM_FORMAT_UYVY:
5163 case DRM_FORMAT_VYUY:
5164 case DRM_FORMAT_NV12:
5165 case DRM_FORMAT_NV21:
5166 case DRM_FORMAT_NV16:
5167 case DRM_FORMAT_NV61:
5168 case DRM_FORMAT_YUV422:
5169 case DRM_FORMAT_YVU422:
5170 case DRM_FORMAT_YUV420:
5171 case DRM_FORMAT_YVU420:
5172 return 2;
5173 default:
5174 return 1;
5175 }
5176}
5177EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5178
5179/**
5180 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5181 * @format: pixel format (DRM_FORMAT_*)
5182 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005183 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005184 * The vertical chroma subsampling factor for the
5185 * specified pixel format.
5186 */
5187int drm_format_vert_chroma_subsampling(uint32_t format)
5188{
5189 switch (format) {
5190 case DRM_FORMAT_YUV410:
5191 case DRM_FORMAT_YVU410:
5192 return 4;
5193 case DRM_FORMAT_YUV420:
5194 case DRM_FORMAT_YVU420:
5195 case DRM_FORMAT_NV12:
5196 case DRM_FORMAT_NV21:
5197 return 2;
5198 default:
5199 return 1;
5200 }
5201}
5202EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005203
5204/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305205 * drm_rotation_simplify() - Try to simplify the rotation
5206 * @rotation: Rotation to be simplified
5207 * @supported_rotations: Supported rotations
5208 *
5209 * Attempt to simplify the rotation to a form that is supported.
5210 * Eg. if the hardware supports everything except DRM_REFLECT_X
5211 * one could call this function like this:
5212 *
5213 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5214 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5215 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5216 *
5217 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5218 * transforms the hardware supports, this function may not
5219 * be able to produce a supported transform, so the caller should
5220 * check the result afterwards.
5221 */
5222unsigned int drm_rotation_simplify(unsigned int rotation,
5223 unsigned int supported_rotations)
5224{
5225 if (rotation & ~supported_rotations) {
5226 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5227 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5228 }
5229
5230 return rotation;
5231}
5232EXPORT_SYMBOL(drm_rotation_simplify);
5233
5234/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005235 * drm_mode_config_init - initialize DRM mode_configuration structure
5236 * @dev: DRM device
5237 *
5238 * Initialize @dev's mode_config structure, used for tracking the graphics
5239 * configuration of @dev.
5240 *
5241 * Since this initializes the modeset locks, no locking is possible. Which is no
5242 * problem, since this should happen single threaded at init time. It is the
5243 * driver's problem to ensure this guarantee.
5244 *
5245 */
5246void drm_mode_config_init(struct drm_device *dev)
5247{
5248 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005249 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005250 mutex_init(&dev->mode_config.idr_mutex);
5251 mutex_init(&dev->mode_config.fb_lock);
5252 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5253 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5254 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04005255 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005256 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5257 INIT_LIST_HEAD(&dev->mode_config.property_list);
5258 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5259 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5260 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005261 idr_init(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005262
5263 drm_modeset_lock_all(dev);
5264 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04005265 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005266 drm_modeset_unlock_all(dev);
5267
5268 /* Just to be sure */
5269 dev->mode_config.num_fb = 0;
5270 dev->mode_config.num_connector = 0;
5271 dev->mode_config.num_crtc = 0;
5272 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005273 dev->mode_config.num_overlay_plane = 0;
5274 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005275}
5276EXPORT_SYMBOL(drm_mode_config_init);
5277
5278/**
5279 * drm_mode_config_cleanup - free up DRM mode_config info
5280 * @dev: DRM device
5281 *
5282 * Free up all the connectors and CRTCs associated with this DRM device, then
5283 * free up the framebuffers and associated buffer objects.
5284 *
5285 * Note that since this /should/ happen single-threaded at driver/device
5286 * teardown time, no locking is required. It's the driver's job to ensure that
5287 * this guarantee actually holds true.
5288 *
5289 * FIXME: cleanup any dangling user buffer objects too
5290 */
5291void drm_mode_config_cleanup(struct drm_device *dev)
5292{
5293 struct drm_connector *connector, *ot;
5294 struct drm_crtc *crtc, *ct;
5295 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04005296 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005297 struct drm_framebuffer *fb, *fbt;
5298 struct drm_property *property, *pt;
5299 struct drm_property_blob *blob, *bt;
5300 struct drm_plane *plane, *plt;
5301
5302 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5303 head) {
5304 encoder->funcs->destroy(encoder);
5305 }
5306
Sean Paul3b336ec2013-08-14 16:47:37 -04005307 list_for_each_entry_safe(bridge, brt,
5308 &dev->mode_config.bridge_list, head) {
5309 bridge->funcs->destroy(bridge);
5310 }
5311
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005312 list_for_each_entry_safe(connector, ot,
5313 &dev->mode_config.connector_list, head) {
5314 connector->funcs->destroy(connector);
5315 }
5316
5317 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5318 head) {
5319 drm_property_destroy(dev, property);
5320 }
5321
5322 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5323 head) {
5324 drm_property_destroy_blob(dev, blob);
5325 }
5326
5327 /*
5328 * Single-threaded teardown context, so it's not required to grab the
5329 * fb_lock to protect against concurrent fb_list access. Contrary, it
5330 * would actually deadlock with the drm_framebuffer_cleanup function.
5331 *
5332 * Also, if there are any framebuffers left, that's a driver leak now,
5333 * so politely WARN about this.
5334 */
5335 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5336 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5337 drm_framebuffer_remove(fb);
5338 }
5339
5340 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5341 head) {
5342 plane->funcs->destroy(plane);
5343 }
5344
5345 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5346 crtc->funcs->destroy(crtc);
5347 }
5348
Dave Airlie138f9eb2014-10-20 16:17:17 +10005349 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005350 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005351 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005352}
5353EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305354
5355struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5356 unsigned int supported_rotations)
5357{
5358 static const struct drm_prop_enum_list props[] = {
5359 { DRM_ROTATE_0, "rotate-0" },
5360 { DRM_ROTATE_90, "rotate-90" },
5361 { DRM_ROTATE_180, "rotate-180" },
5362 { DRM_ROTATE_270, "rotate-270" },
5363 { DRM_REFLECT_X, "reflect-x" },
5364 { DRM_REFLECT_Y, "reflect-y" },
5365 };
5366
5367 return drm_property_create_bitmask(dev, 0, "rotation",
5368 props, ARRAY_SIZE(props),
5369 supported_rotations);
5370}
5371EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005372
5373/**
5374 * DOC: Tile group
5375 *
5376 * Tile groups are used to represent tiled monitors with a unique
5377 * integer identifier. Tiled monitors using DisplayID v1.3 have
5378 * a unique 8-byte handle, we store this in a tile group, so we
5379 * have a common identifier for all tiles in a monitor group.
5380 */
5381static void drm_tile_group_free(struct kref *kref)
5382{
5383 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5384 struct drm_device *dev = tg->dev;
5385 mutex_lock(&dev->mode_config.idr_mutex);
5386 idr_remove(&dev->mode_config.tile_idr, tg->id);
5387 mutex_unlock(&dev->mode_config.idr_mutex);
5388 kfree(tg);
5389}
5390
5391/**
5392 * drm_mode_put_tile_group - drop a reference to a tile group.
5393 * @dev: DRM device
5394 * @tg: tile group to drop reference to.
5395 *
5396 * drop reference to tile group and free if 0.
5397 */
5398void drm_mode_put_tile_group(struct drm_device *dev,
5399 struct drm_tile_group *tg)
5400{
5401 kref_put(&tg->refcount, drm_tile_group_free);
5402}
5403
5404/**
5405 * drm_mode_get_tile_group - get a reference to an existing tile group
5406 * @dev: DRM device
5407 * @topology: 8-bytes unique per monitor.
5408 *
5409 * Use the unique bytes to get a reference to an existing tile group.
5410 *
5411 * RETURNS:
5412 * tile group or NULL if not found.
5413 */
5414struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5415 char topology[8])
5416{
5417 struct drm_tile_group *tg;
5418 int id;
5419 mutex_lock(&dev->mode_config.idr_mutex);
5420 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5421 if (!memcmp(tg->group_data, topology, 8)) {
5422 if (!kref_get_unless_zero(&tg->refcount))
5423 tg = NULL;
5424 mutex_unlock(&dev->mode_config.idr_mutex);
5425 return tg;
5426 }
5427 }
5428 mutex_unlock(&dev->mode_config.idr_mutex);
5429 return NULL;
5430}
5431
5432/**
5433 * drm_mode_create_tile_group - create a tile group from a displayid description
5434 * @dev: DRM device
5435 * @topology: 8-bytes unique per monitor.
5436 *
5437 * Create a tile group for the unique monitor, and get a unique
5438 * identifier for the tile group.
5439 *
5440 * RETURNS:
5441 * new tile group or error.
5442 */
5443struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5444 char topology[8])
5445{
5446 struct drm_tile_group *tg;
5447 int ret;
5448
5449 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5450 if (!tg)
5451 return ERR_PTR(-ENOMEM);
5452
5453 kref_init(&tg->refcount);
5454 memcpy(tg->group_data, topology, 8);
5455 tg->dev = dev;
5456
5457 mutex_lock(&dev->mode_config.idr_mutex);
5458 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5459 if (ret >= 0) {
5460 tg->id = ret;
5461 } else {
5462 kfree(tg);
5463 tg = ERR_PTR(ret);
5464 }
5465
5466 mutex_unlock(&dev->mode_config.idr_mutex);
5467 return tg;
5468}