blob: e3140c19055632986c1b0f03557982eba766708e [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
906 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
907 drm_mode_remove(connector, mode);
908
909 list_for_each_entry_safe(mode, t, &connector->modes, head)
910 drm_mode_remove(connector, mode);
911
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400912 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
913 connector->connector_type_id);
914
Dave Airlief453ba02008-11-07 14:05:41 -0800915 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300916 kfree(connector->name);
917 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800918 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000919 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100920
921 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
922 if (connector->state && connector->funcs->atomic_destroy_state)
923 connector->funcs->atomic_destroy_state(connector,
924 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100925
926 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800927}
928EXPORT_SYMBOL(drm_connector_cleanup);
929
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100930/**
Daniel Vetter10f637b2014-07-29 13:47:11 +0200931 * drm_connector_index - find the index of a registered connector
932 * @connector: connector to find index for
933 *
934 * Given a registered connector, return the index of that connector within a DRM
935 * device's list of connectors.
936 */
937unsigned int drm_connector_index(struct drm_connector *connector)
938{
939 unsigned int index = 0;
940 struct drm_connector *tmp;
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100941 struct drm_mode_config *config = &connector->dev->mode_config;
942
943 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter10f637b2014-07-29 13:47:11 +0200944
945 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
946 if (tmp == connector)
947 return index;
948
949 index++;
950 }
951
952 BUG();
953}
954EXPORT_SYMBOL(drm_connector_index);
955
956/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100957 * drm_connector_register - register a connector
958 * @connector: the connector to register
959 *
960 * Register userspace interfaces for a connector
961 *
962 * Returns:
963 * Zero on success, error code on failure.
964 */
965int drm_connector_register(struct drm_connector *connector)
966{
Thomas Wood30f65702014-06-18 17:52:32 +0100967 int ret;
968
Dave Airlie2ee39452014-07-24 11:53:45 +1000969 drm_mode_object_register(connector->dev, &connector->base);
970
Thomas Wood30f65702014-06-18 17:52:32 +0100971 ret = drm_sysfs_connector_add(connector);
972 if (ret)
973 return ret;
974
975 ret = drm_debugfs_connector_add(connector);
976 if (ret) {
977 drm_sysfs_connector_remove(connector);
978 return ret;
979 }
980
981 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100982}
983EXPORT_SYMBOL(drm_connector_register);
984
985/**
986 * drm_connector_unregister - unregister a connector
987 * @connector: the connector to unregister
988 *
989 * Unregister userspace interfaces for a connector
990 */
991void drm_connector_unregister(struct drm_connector *connector)
992{
993 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +0100994 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100995}
996EXPORT_SYMBOL(drm_connector_unregister);
997
998
999/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001000 * drm_connector_unplug_all - unregister connector userspace interfaces
1001 * @dev: drm device
1002 *
1003 * This function unregisters all connector userspace interfaces in sysfs. Should
1004 * be call when the device is disconnected, e.g. from an usb driver's
1005 * ->disconnect callback.
1006 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001007void drm_connector_unplug_all(struct drm_device *dev)
1008{
1009 struct drm_connector *connector;
1010
1011 /* taking the mode config mutex ends up in a clash with sysfs */
1012 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001013 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001014
1015}
1016EXPORT_SYMBOL(drm_connector_unplug_all);
1017
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001018/**
1019 * drm_bridge_init - initialize a drm transcoder/bridge
1020 * @dev: drm device
1021 * @bridge: transcoder/bridge to set up
1022 * @funcs: bridge function table
1023 *
1024 * Initialises a preallocated bridge. Bridges should be
1025 * subclassed as part of driver connector objects.
1026 *
1027 * Returns:
1028 * Zero on success, error code on failure.
1029 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001030int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1031 const struct drm_bridge_funcs *funcs)
1032{
1033 int ret;
1034
1035 drm_modeset_lock_all(dev);
1036
1037 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1038 if (ret)
1039 goto out;
1040
1041 bridge->dev = dev;
1042 bridge->funcs = funcs;
1043
1044 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1045 dev->mode_config.num_bridge++;
1046
1047 out:
1048 drm_modeset_unlock_all(dev);
1049 return ret;
1050}
1051EXPORT_SYMBOL(drm_bridge_init);
1052
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001053/**
1054 * drm_bridge_cleanup - cleans up an initialised bridge
1055 * @bridge: bridge to cleanup
1056 *
1057 * Cleans up the bridge but doesn't free the object.
1058 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001059void drm_bridge_cleanup(struct drm_bridge *bridge)
1060{
1061 struct drm_device *dev = bridge->dev;
1062
1063 drm_modeset_lock_all(dev);
1064 drm_mode_object_put(dev, &bridge->base);
1065 list_del(&bridge->head);
1066 dev->mode_config.num_bridge--;
1067 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001068
1069 memset(bridge, 0, sizeof(*bridge));
Sean Paul3b336ec2013-08-14 16:47:37 -04001070}
1071EXPORT_SYMBOL(drm_bridge_cleanup);
1072
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001073/**
1074 * drm_encoder_init - Init a preallocated encoder
1075 * @dev: drm device
1076 * @encoder: the encoder to init
1077 * @funcs: callbacks for this encoder
1078 * @encoder_type: user visible type of the encoder
1079 *
1080 * Initialises a preallocated encoder. Encoder should be
1081 * subclassed as part of driver encoder objects.
1082 *
1083 * Returns:
1084 * Zero on success, error code on failure.
1085 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001086int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001087 struct drm_encoder *encoder,
1088 const struct drm_encoder_funcs *funcs,
1089 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001090{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001091 int ret;
1092
Daniel Vetter84849902012-12-02 00:28:11 +01001093 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001094
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001095 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1096 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001097 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001098
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001099 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001100 encoder->encoder_type = encoder_type;
1101 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001102 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1103 drm_encoder_enum_list[encoder_type].name,
1104 encoder->base.id);
1105 if (!encoder->name) {
1106 ret = -ENOMEM;
1107 goto out_put;
1108 }
Dave Airlief453ba02008-11-07 14:05:41 -08001109
1110 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1111 dev->mode_config.num_encoder++;
1112
Jani Nikulae5748942014-05-14 16:58:20 +03001113out_put:
1114 if (ret)
1115 drm_mode_object_put(dev, &encoder->base);
1116
1117out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001118 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001119
1120 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001121}
1122EXPORT_SYMBOL(drm_encoder_init);
1123
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001124/**
1125 * drm_encoder_cleanup - cleans up an initialised encoder
1126 * @encoder: encoder to cleanup
1127 *
1128 * Cleans up the encoder but doesn't free the object.
1129 */
Dave Airlief453ba02008-11-07 14:05:41 -08001130void drm_encoder_cleanup(struct drm_encoder *encoder)
1131{
1132 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001133
Daniel Vetter84849902012-12-02 00:28:11 +01001134 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001135 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001136 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001137 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001138 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001139 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001140
1141 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001142}
1143EXPORT_SYMBOL(drm_encoder_cleanup);
1144
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001145/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001146 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001147 * @dev: DRM device
1148 * @plane: plane object to init
1149 * @possible_crtcs: bitmask of possible CRTCs
1150 * @funcs: callbacks for the new plane
1151 * @formats: array of supported formats (%DRM_FORMAT_*)
1152 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001153 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001154 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001155 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001156 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001157 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001158 * Zero on success, error code on failure.
1159 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001160int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1161 unsigned long possible_crtcs,
1162 const struct drm_plane_funcs *funcs,
1163 const uint32_t *formats, uint32_t format_count,
1164 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001165{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001166 int ret;
1167
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001168 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1169 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001170 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001171
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001172 drm_modeset_lock_init(&plane->mutex);
1173
Rob Clark4d939142012-05-17 02:23:27 -06001174 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001175 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001176 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001177 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1178 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001179 if (!plane->format_types) {
1180 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1181 drm_mode_object_put(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001182 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001183 }
1184
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001185 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001186 plane->format_count = format_count;
1187 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001188 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001189
Matt Roperdc415ff2014-04-01 15:22:36 -07001190 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1191 dev->mode_config.num_total_plane++;
1192 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1193 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001194
Rob Clark9922ab52014-04-01 20:16:57 -04001195 drm_object_attach_property(&plane->base,
1196 dev->mode_config.plane_type_property,
1197 plane->type);
1198
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001199 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001200}
Matt Roperdc415ff2014-04-01 15:22:36 -07001201EXPORT_SYMBOL(drm_universal_plane_init);
1202
1203/**
1204 * drm_plane_init - Initialize a legacy plane
1205 * @dev: DRM device
1206 * @plane: plane object to init
1207 * @possible_crtcs: bitmask of possible CRTCs
1208 * @funcs: callbacks for the new plane
1209 * @formats: array of supported formats (%DRM_FORMAT_*)
1210 * @format_count: number of elements in @formats
1211 * @is_primary: plane type (primary vs overlay)
1212 *
1213 * Legacy API to initialize a DRM plane.
1214 *
1215 * New drivers should call drm_universal_plane_init() instead.
1216 *
1217 * Returns:
1218 * Zero on success, error code on failure.
1219 */
1220int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1221 unsigned long possible_crtcs,
1222 const struct drm_plane_funcs *funcs,
1223 const uint32_t *formats, uint32_t format_count,
1224 bool is_primary)
1225{
1226 enum drm_plane_type type;
1227
1228 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1229 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1230 formats, format_count, type);
1231}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001232EXPORT_SYMBOL(drm_plane_init);
1233
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001234/**
1235 * drm_plane_cleanup - Clean up the core plane usage
1236 * @plane: plane to cleanup
1237 *
1238 * This function cleans up @plane and removes it from the DRM mode setting
1239 * core. Note that the function does *not* free the plane structure itself,
1240 * this is the responsibility of the caller.
1241 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001242void drm_plane_cleanup(struct drm_plane *plane)
1243{
1244 struct drm_device *dev = plane->dev;
1245
Daniel Vetter84849902012-12-02 00:28:11 +01001246 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001247 kfree(plane->format_types);
1248 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001249
1250 BUG_ON(list_empty(&plane->head));
1251
1252 list_del(&plane->head);
1253 dev->mode_config.num_total_plane--;
1254 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1255 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001256 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001257
1258 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1259 if (plane->state && plane->funcs->atomic_destroy_state)
1260 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001261
1262 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001263}
1264EXPORT_SYMBOL(drm_plane_cleanup);
1265
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001266/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001267 * drm_plane_index - find the index of a registered plane
1268 * @plane: plane to find index for
1269 *
1270 * Given a registered plane, return the index of that CRTC within a DRM
1271 * device's list of planes.
1272 */
1273unsigned int drm_plane_index(struct drm_plane *plane)
1274{
1275 unsigned int index = 0;
1276 struct drm_plane *tmp;
1277
1278 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1279 if (tmp == plane)
1280 return index;
1281
1282 index++;
1283 }
1284
1285 BUG();
1286}
1287EXPORT_SYMBOL(drm_plane_index);
1288
1289/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001290 * drm_plane_force_disable - Forcibly disable a plane
1291 * @plane: plane to disable
1292 *
1293 * Forces the plane to be disabled.
1294 *
1295 * Used when the plane's current framebuffer is destroyed,
1296 * and when restoring fbdev mode.
1297 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001298void drm_plane_force_disable(struct drm_plane *plane)
1299{
1300 int ret;
1301
Daniel Vetter3d30a592014-07-27 13:42:42 +02001302 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001303 return;
1304
Daniel Vetter3d30a592014-07-27 13:42:42 +02001305 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001306 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001307 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001308 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001309 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001310 return;
1311 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001312 /* disconnect the plane from the fb and crtc: */
Daniel Vetter3d30a592014-07-27 13:42:42 +02001313 __drm_framebuffer_unreference(plane->old_fb);
1314 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001315 plane->fb = NULL;
1316 plane->crtc = NULL;
1317}
1318EXPORT_SYMBOL(drm_plane_force_disable);
1319
Dave Airlief453ba02008-11-07 14:05:41 -08001320static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1321{
1322 struct drm_property *edid;
1323 struct drm_property *dpms;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001324 struct drm_property *dev_path;
Dave Airlief453ba02008-11-07 14:05:41 -08001325
1326 /*
1327 * Standard properties (apply to all connectors)
1328 */
1329 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1330 DRM_MODE_PROP_IMMUTABLE,
1331 "EDID", 0);
1332 dev->mode_config.edid_property = edid;
1333
Sascha Hauer4a67d392012-02-06 10:58:17 +01001334 dpms = drm_property_create_enum(dev, 0,
1335 "DPMS", drm_dpms_enum_list,
1336 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001337 dev->mode_config.dpms_property = dpms;
1338
Dave Airlie43aba7e2014-06-05 14:01:31 +10001339 dev_path = drm_property_create(dev,
1340 DRM_MODE_PROP_BLOB |
1341 DRM_MODE_PROP_IMMUTABLE,
1342 "PATH", 0);
1343 dev->mode_config.path_property = dev_path;
1344
Dave Airlief453ba02008-11-07 14:05:41 -08001345 return 0;
1346}
1347
Rob Clark9922ab52014-04-01 20:16:57 -04001348static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1349{
1350 struct drm_property *type;
1351
1352 /*
1353 * Standard properties (apply to all planes)
1354 */
1355 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1356 "type", drm_plane_type_enum_list,
1357 ARRAY_SIZE(drm_plane_type_enum_list));
1358 dev->mode_config.plane_type_property = type;
1359
1360 return 0;
1361}
1362
Dave Airlief453ba02008-11-07 14:05:41 -08001363/**
1364 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1365 * @dev: DRM device
1366 *
1367 * Called by a driver the first time a DVI-I connector is made.
1368 */
1369int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1370{
1371 struct drm_property *dvi_i_selector;
1372 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001373
1374 if (dev->mode_config.dvi_i_select_subconnector_property)
1375 return 0;
1376
1377 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001378 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001379 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001380 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001381 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001382 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1383
Sascha Hauer4a67d392012-02-06 10:58:17 +01001384 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001385 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001386 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001387 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001388 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1389
1390 return 0;
1391}
1392EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1393
1394/**
1395 * drm_create_tv_properties - create TV specific connector properties
1396 * @dev: DRM device
1397 * @num_modes: number of different TV formats (modes) supported
1398 * @modes: array of pointers to strings containing name of each format
1399 *
1400 * Called by a driver's TV initialization routine, this function creates
1401 * the TV specific connector properties for a given device. Caller is
1402 * responsible for allocating a list of format names and passing them to
1403 * this routine.
1404 */
Thierry Reding2f763312014-10-13 12:45:57 +02001405int drm_mode_create_tv_properties(struct drm_device *dev,
1406 unsigned int num_modes,
Dave Airlief453ba02008-11-07 14:05:41 -08001407 char *modes[])
1408{
1409 struct drm_property *tv_selector;
1410 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001411 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001412
1413 if (dev->mode_config.tv_select_subconnector_property)
1414 return 0;
1415
1416 /*
1417 * Basic connector properties
1418 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001419 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001420 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001421 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001422 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001423 dev->mode_config.tv_select_subconnector_property = tv_selector;
1424
1425 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001426 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1427 "subconnector",
1428 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001429 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001430 dev->mode_config.tv_subconnector_property = tv_subconnector;
1431
1432 /*
1433 * Other, TV specific properties: margins & TV modes.
1434 */
1435 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001436 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001437
1438 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001439 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001440
1441 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001442 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001443
1444 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001445 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001446
1447 dev->mode_config.tv_mode_property =
1448 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1449 "mode", num_modes);
1450 for (i = 0; i < num_modes; i++)
1451 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1452 i, modes[i]);
1453
Francisco Jerezb6b79022009-08-02 04:19:20 +02001454 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001455 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001456
1457 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001458 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001459
1460 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001461 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001462
Francisco Jereza75f0232009-08-12 02:30:10 +02001463 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001464 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001465
1466 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001467 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001468
1469 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001470 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001471
Dave Airlief453ba02008-11-07 14:05:41 -08001472 return 0;
1473}
1474EXPORT_SYMBOL(drm_mode_create_tv_properties);
1475
1476/**
1477 * drm_mode_create_scaling_mode_property - create scaling mode property
1478 * @dev: DRM device
1479 *
1480 * Called by a driver the first time it's needed, must be attached to desired
1481 * connectors.
1482 */
1483int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1484{
1485 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001486
1487 if (dev->mode_config.scaling_mode_property)
1488 return 0;
1489
1490 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001491 drm_property_create_enum(dev, 0, "scaling mode",
1492 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001493 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001494
1495 dev->mode_config.scaling_mode_property = scaling_mode;
1496
1497 return 0;
1498}
1499EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1500
1501/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301502 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1503 * @dev: DRM device
1504 *
1505 * Called by a driver the first time it's needed, must be attached to desired
1506 * connectors.
1507 *
1508 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001509 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301510 */
1511int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1512{
1513 if (dev->mode_config.aspect_ratio_property)
1514 return 0;
1515
1516 dev->mode_config.aspect_ratio_property =
1517 drm_property_create_enum(dev, 0, "aspect ratio",
1518 drm_aspect_ratio_enum_list,
1519 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1520
1521 if (dev->mode_config.aspect_ratio_property == NULL)
1522 return -ENOMEM;
1523
1524 return 0;
1525}
1526EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1527
1528/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001529 * drm_mode_create_dirty_property - create dirty property
1530 * @dev: DRM device
1531 *
1532 * Called by a driver the first time it's needed, must be attached to desired
1533 * connectors.
1534 */
1535int drm_mode_create_dirty_info_property(struct drm_device *dev)
1536{
1537 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001538
1539 if (dev->mode_config.dirty_info_property)
1540 return 0;
1541
1542 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001543 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001544 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001545 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001546 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001547 dev->mode_config.dirty_info_property = dirty_info;
1548
1549 return 0;
1550}
1551EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1552
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001553/**
1554 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1555 * @dev: DRM device
1556 *
1557 * Create the the suggested x/y offset property for connectors.
1558 */
1559int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1560{
1561 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1562 return 0;
1563
1564 dev->mode_config.suggested_x_property =
1565 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1566
1567 dev->mode_config.suggested_y_property =
1568 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1569
1570 if (dev->mode_config.suggested_x_property == NULL ||
1571 dev->mode_config.suggested_y_property == NULL)
1572 return -ENOMEM;
1573 return 0;
1574}
1575EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1576
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001577static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001578{
1579 uint32_t total_objects = 0;
1580
1581 total_objects += dev->mode_config.num_crtc;
1582 total_objects += dev->mode_config.num_connector;
1583 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001584 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001585
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01001586 group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08001587 if (!group->id_list)
1588 return -ENOMEM;
1589
1590 group->num_crtcs = 0;
1591 group->num_connectors = 0;
1592 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001593 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001594 return 0;
1595}
1596
Dave Airliead222792014-05-02 13:22:19 +10001597void drm_mode_group_destroy(struct drm_mode_group *group)
1598{
1599 kfree(group->id_list);
1600 group->id_list = NULL;
1601}
1602
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001603/*
1604 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1605 * the drm core's responsibility to set up mode control groups.
1606 */
Dave Airlief453ba02008-11-07 14:05:41 -08001607int drm_mode_group_init_legacy_group(struct drm_device *dev,
1608 struct drm_mode_group *group)
1609{
1610 struct drm_crtc *crtc;
1611 struct drm_encoder *encoder;
1612 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001613 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001614 int ret;
1615
Thierry Reding0cc0b222014-12-10 13:03:37 +01001616 ret = drm_mode_group_init(dev, group);
1617 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08001618 return ret;
1619
1620 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1621 group->id_list[group->num_crtcs++] = crtc->base.id;
1622
1623 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1624 group->id_list[group->num_crtcs + group->num_encoders++] =
1625 encoder->base.id;
1626
1627 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1628 group->id_list[group->num_crtcs + group->num_encoders +
1629 group->num_connectors++] = connector->base.id;
1630
Sean Paul3b336ec2013-08-14 16:47:37 -04001631 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1632 group->id_list[group->num_crtcs + group->num_encoders +
1633 group->num_connectors + group->num_bridges++] =
1634 bridge->base.id;
1635
Dave Airlief453ba02008-11-07 14:05:41 -08001636 return 0;
1637}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001638EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001639
Dave Airlie2390cd12014-06-05 14:01:29 +10001640void drm_reinit_primary_mode_group(struct drm_device *dev)
1641{
1642 drm_modeset_lock_all(dev);
1643 drm_mode_group_destroy(&dev->primary->mode_group);
1644 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1645 drm_modeset_unlock_all(dev);
1646}
1647EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1648
Dave Airlief453ba02008-11-07 14:05:41 -08001649/**
Dave Airlief453ba02008-11-07 14:05:41 -08001650 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1651 * @out: drm_mode_modeinfo struct to return to the user
1652 * @in: drm_display_mode to use
1653 *
Dave Airlief453ba02008-11-07 14:05:41 -08001654 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1655 * the user.
1656 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001657static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1658 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001659{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001660 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1661 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1662 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1663 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1664 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1665 "timing values too large for mode info\n");
1666
Dave Airlief453ba02008-11-07 14:05:41 -08001667 out->clock = in->clock;
1668 out->hdisplay = in->hdisplay;
1669 out->hsync_start = in->hsync_start;
1670 out->hsync_end = in->hsync_end;
1671 out->htotal = in->htotal;
1672 out->hskew = in->hskew;
1673 out->vdisplay = in->vdisplay;
1674 out->vsync_start = in->vsync_start;
1675 out->vsync_end = in->vsync_end;
1676 out->vtotal = in->vtotal;
1677 out->vscan = in->vscan;
1678 out->vrefresh = in->vrefresh;
1679 out->flags = in->flags;
1680 out->type = in->type;
1681 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1682 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1683}
1684
1685/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001686 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001687 * @out: drm_display_mode to return to the user
1688 * @in: drm_mode_modeinfo to use
1689 *
Dave Airlief453ba02008-11-07 14:05:41 -08001690 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1691 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001692 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001693 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001694 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001695 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001696static int drm_crtc_convert_umode(struct drm_display_mode *out,
1697 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001698{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001699 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1700 return -ERANGE;
1701
Damien Lespiau5848ad42013-09-27 12:11:50 +01001702 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1703 return -EINVAL;
1704
Dave Airlief453ba02008-11-07 14:05:41 -08001705 out->clock = in->clock;
1706 out->hdisplay = in->hdisplay;
1707 out->hsync_start = in->hsync_start;
1708 out->hsync_end = in->hsync_end;
1709 out->htotal = in->htotal;
1710 out->hskew = in->hskew;
1711 out->vdisplay = in->vdisplay;
1712 out->vsync_start = in->vsync_start;
1713 out->vsync_end = in->vsync_end;
1714 out->vtotal = in->vtotal;
1715 out->vscan = in->vscan;
1716 out->vrefresh = in->vrefresh;
1717 out->flags = in->flags;
1718 out->type = in->type;
1719 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1720 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001721
1722 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001723}
1724
1725/**
1726 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001727 * @dev: drm device for the ioctl
1728 * @data: data pointer for the ioctl
1729 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001730 *
Dave Airlief453ba02008-11-07 14:05:41 -08001731 * Construct a set of configuration description structures and return
1732 * them to the user, including CRTC, connector and framebuffer configuration.
1733 *
1734 * Called by the user via ioctl.
1735 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001736 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001737 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001738 */
1739int drm_mode_getresources(struct drm_device *dev, void *data,
1740 struct drm_file *file_priv)
1741{
1742 struct drm_mode_card_res *card_res = data;
1743 struct list_head *lh;
1744 struct drm_framebuffer *fb;
1745 struct drm_connector *connector;
1746 struct drm_crtc *crtc;
1747 struct drm_encoder *encoder;
1748 int ret = 0;
1749 int connector_count = 0;
1750 int crtc_count = 0;
1751 int fb_count = 0;
1752 int encoder_count = 0;
1753 int copied = 0, i;
1754 uint32_t __user *fb_id;
1755 uint32_t __user *crtc_id;
1756 uint32_t __user *connector_id;
1757 uint32_t __user *encoder_id;
1758 struct drm_mode_group *mode_group;
1759
Dave Airliefb3b06c2011-02-08 13:55:21 +10001760 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1761 return -EINVAL;
1762
Dave Airlief453ba02008-11-07 14:05:41 -08001763
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001764 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001765 /*
1766 * For the non-control nodes we need to limit the list of resources
1767 * by IDs in the group list for this node
1768 */
1769 list_for_each(lh, &file_priv->fbs)
1770 fb_count++;
1771
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001772 /* handle this in 4 parts */
1773 /* FBs */
1774 if (card_res->count_fbs >= fb_count) {
1775 copied = 0;
1776 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1777 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1778 if (put_user(fb->base.id, fb_id + copied)) {
1779 mutex_unlock(&file_priv->fbs_lock);
1780 return -EFAULT;
1781 }
1782 copied++;
1783 }
1784 }
1785 card_res->count_fbs = fb_count;
1786 mutex_unlock(&file_priv->fbs_lock);
1787
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001788 /* mode_config.mutex protects the connector list against e.g. DP MST
1789 * connector hot-adding. CRTC/Plane lists are invariant. */
1790 mutex_lock(&dev->mode_config.mutex);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001791 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001792
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001793 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001794 list_for_each(lh, &dev->mode_config.crtc_list)
1795 crtc_count++;
1796
1797 list_for_each(lh, &dev->mode_config.connector_list)
1798 connector_count++;
1799
1800 list_for_each(lh, &dev->mode_config.encoder_list)
1801 encoder_count++;
1802 } else {
1803
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001804 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001805 crtc_count = mode_group->num_crtcs;
1806 connector_count = mode_group->num_connectors;
1807 encoder_count = mode_group->num_encoders;
1808 }
1809
1810 card_res->max_height = dev->mode_config.max_height;
1811 card_res->min_height = dev->mode_config.min_height;
1812 card_res->max_width = dev->mode_config.max_width;
1813 card_res->min_width = dev->mode_config.min_width;
1814
Dave Airlief453ba02008-11-07 14:05:41 -08001815 /* CRTCs */
1816 if (card_res->count_crtcs >= crtc_count) {
1817 copied = 0;
1818 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001819 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001820 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1821 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001822 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001823 if (put_user(crtc->base.id, crtc_id + copied)) {
1824 ret = -EFAULT;
1825 goto out;
1826 }
1827 copied++;
1828 }
1829 } else {
1830 for (i = 0; i < mode_group->num_crtcs; i++) {
1831 if (put_user(mode_group->id_list[i],
1832 crtc_id + copied)) {
1833 ret = -EFAULT;
1834 goto out;
1835 }
1836 copied++;
1837 }
1838 }
1839 }
1840 card_res->count_crtcs = crtc_count;
1841
1842 /* Encoders */
1843 if (card_res->count_encoders >= encoder_count) {
1844 copied = 0;
1845 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001846 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001847 list_for_each_entry(encoder,
1848 &dev->mode_config.encoder_list,
1849 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001850 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001851 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001852 if (put_user(encoder->base.id, encoder_id +
1853 copied)) {
1854 ret = -EFAULT;
1855 goto out;
1856 }
1857 copied++;
1858 }
1859 } else {
1860 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1861 if (put_user(mode_group->id_list[i],
1862 encoder_id + copied)) {
1863 ret = -EFAULT;
1864 goto out;
1865 }
1866 copied++;
1867 }
1868
1869 }
1870 }
1871 card_res->count_encoders = encoder_count;
1872
1873 /* Connectors */
1874 if (card_res->count_connectors >= connector_count) {
1875 copied = 0;
1876 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001877 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001878 list_for_each_entry(connector,
1879 &dev->mode_config.connector_list,
1880 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001881 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1882 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001883 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001884 if (put_user(connector->base.id,
1885 connector_id + copied)) {
1886 ret = -EFAULT;
1887 goto out;
1888 }
1889 copied++;
1890 }
1891 } else {
1892 int start = mode_group->num_crtcs +
1893 mode_group->num_encoders;
1894 for (i = start; i < start + mode_group->num_connectors; i++) {
1895 if (put_user(mode_group->id_list[i],
1896 connector_id + copied)) {
1897 ret = -EFAULT;
1898 goto out;
1899 }
1900 copied++;
1901 }
1902 }
1903 }
1904 card_res->count_connectors = connector_count;
1905
Jerome Glisse94401062010-07-15 15:43:25 -04001906 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001907 card_res->count_connectors, card_res->count_encoders);
1908
1909out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001910 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001911 return ret;
1912}
1913
1914/**
1915 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001916 * @dev: drm device for the ioctl
1917 * @data: data pointer for the ioctl
1918 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001919 *
Dave Airlief453ba02008-11-07 14:05:41 -08001920 * Construct a CRTC configuration structure to return to the user.
1921 *
1922 * Called by the user via ioctl.
1923 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001924 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001925 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001926 */
1927int drm_mode_getcrtc(struct drm_device *dev,
1928 void *data, struct drm_file *file_priv)
1929{
1930 struct drm_mode_crtc *crtc_resp = data;
1931 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001932
Dave Airliefb3b06c2011-02-08 13:55:21 +10001933 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1934 return -EINVAL;
1935
Rob Clarka2b34e22013-10-05 16:36:52 -04001936 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001937 if (!crtc)
1938 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001939
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001940 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08001941 crtc_resp->x = crtc->x;
1942 crtc_resp->y = crtc->y;
1943 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001944 if (crtc->primary->fb)
1945 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001946 else
1947 crtc_resp->fb_id = 0;
1948
1949 if (crtc->enabled) {
1950
1951 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1952 crtc_resp->mode_valid = 1;
1953
1954 } else {
1955 crtc_resp->mode_valid = 0;
1956 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001957 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08001958
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001959 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001960}
1961
Damien Lespiau61d8e322013-09-25 16:45:22 +01001962static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1963 const struct drm_file *file_priv)
1964{
1965 /*
1966 * If user-space hasn't configured the driver to expose the stereo 3D
1967 * modes, don't expose them.
1968 */
1969 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1970 return false;
1971
1972 return true;
1973}
1974
Daniel Vetterabd69c52014-11-25 23:50:05 +01001975static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1976{
1977 /* For atomic drivers only state objects are synchronously updated and
1978 * protected by modeset locks, so check those first. */
1979 if (connector->state)
1980 return connector->state->best_encoder;
1981 return connector->encoder;
1982}
1983
Dave Airlief453ba02008-11-07 14:05:41 -08001984/**
1985 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001986 * @dev: drm device for the ioctl
1987 * @data: data pointer for the ioctl
1988 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001989 *
Dave Airlief453ba02008-11-07 14:05:41 -08001990 * Construct a connector configuration structure to return to the user.
1991 *
1992 * Called by the user via ioctl.
1993 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001994 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001995 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001996 */
1997int drm_mode_getconnector(struct drm_device *dev, void *data,
1998 struct drm_file *file_priv)
1999{
2000 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002001 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002002 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002003 struct drm_display_mode *mode;
2004 int mode_count = 0;
2005 int props_count = 0;
2006 int encoders_count = 0;
2007 int ret = 0;
2008 int copied = 0;
2009 int i;
2010 struct drm_mode_modeinfo u_mode;
2011 struct drm_mode_modeinfo __user *mode_ptr;
2012 uint32_t __user *prop_ptr;
2013 uint64_t __user *prop_values;
2014 uint32_t __user *encoder_ptr;
2015
Dave Airliefb3b06c2011-02-08 13:55:21 +10002016 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2017 return -EINVAL;
2018
Dave Airlief453ba02008-11-07 14:05:41 -08002019 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2020
Jerome Glisse94401062010-07-15 15:43:25 -04002021 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002022
Daniel Vetter7b240562012-12-12 00:35:33 +01002023 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002024
Rob Clarka2b34e22013-10-05 16:36:52 -04002025 connector = drm_connector_find(dev, out_resp->connector_id);
2026 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002027 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002028 goto out;
2029 }
Dave Airlief453ba02008-11-07 14:05:41 -08002030
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002031 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08002032
Thierry Reding01073b02014-12-10 13:03:38 +01002033 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2034 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002035 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002036
2037 if (out_resp->count_modes == 0) {
2038 connector->funcs->fill_modes(connector,
2039 dev->mode_config.max_width,
2040 dev->mode_config.max_height);
2041 }
2042
2043 /* delayed so we get modes regardless of pre-fill_modes state */
2044 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002045 if (drm_mode_expose_to_userspace(mode, file_priv))
2046 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002047
2048 out_resp->connector_id = connector->base.id;
2049 out_resp->connector_type = connector->connector_type;
2050 out_resp->connector_type_id = connector->connector_type_id;
2051 out_resp->mm_width = connector->display_info.width_mm;
2052 out_resp->mm_height = connector->display_info.height_mm;
2053 out_resp->subpixel = connector->display_info.subpixel_order;
2054 out_resp->connection = connector->status;
Daniel Vetter832fd392014-06-05 11:07:20 +02002055 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002056
2057 encoder = drm_connector_get_encoder(connector);
2058 if (encoder)
2059 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002060 else
2061 out_resp->encoder_id = 0;
Daniel Vetter832fd392014-06-05 11:07:20 +02002062 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002063
2064 /*
2065 * This ioctl is called twice, once to determine how much space is
2066 * needed, and the 2nd time to fill it.
2067 */
2068 if ((out_resp->count_modes >= mode_count) && mode_count) {
2069 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002070 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002071 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002072 if (!drm_mode_expose_to_userspace(mode, file_priv))
2073 continue;
2074
Dave Airlief453ba02008-11-07 14:05:41 -08002075 drm_crtc_convert_to_umode(&u_mode, mode);
2076 if (copy_to_user(mode_ptr + copied,
2077 &u_mode, sizeof(u_mode))) {
2078 ret = -EFAULT;
2079 goto out;
2080 }
2081 copied++;
2082 }
2083 }
2084 out_resp->count_modes = mode_count;
2085
2086 if ((out_resp->count_props >= props_count) && props_count) {
2087 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002088 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2089 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002090 for (i = 0; i < connector->properties.count; i++) {
2091 if (put_user(connector->properties.ids[i],
2092 prop_ptr + copied)) {
2093 ret = -EFAULT;
2094 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002095 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002096
2097 if (put_user(connector->properties.values[i],
2098 prop_values + copied)) {
2099 ret = -EFAULT;
2100 goto out;
2101 }
2102 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08002103 }
2104 }
2105 out_resp->count_props = props_count;
2106
2107 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2108 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002109 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002110 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2111 if (connector->encoder_ids[i] != 0) {
2112 if (put_user(connector->encoder_ids[i],
2113 encoder_ptr + copied)) {
2114 ret = -EFAULT;
2115 goto out;
2116 }
2117 copied++;
2118 }
2119 }
2120 }
2121 out_resp->count_encoders = encoders_count;
2122
2123out:
Daniel Vetter7b240562012-12-12 00:35:33 +01002124 mutex_unlock(&dev->mode_config.mutex);
2125
Dave Airlief453ba02008-11-07 14:05:41 -08002126 return ret;
2127}
2128
Daniel Vetterabd69c52014-11-25 23:50:05 +01002129static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2130{
2131 struct drm_connector *connector;
2132 struct drm_device *dev = encoder->dev;
2133 bool uses_atomic = false;
2134
2135 /* For atomic drivers only state objects are synchronously updated and
2136 * protected by modeset locks, so check those first. */
2137 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2138 if (!connector->state)
2139 continue;
2140
2141 uses_atomic = true;
2142
2143 if (connector->state->best_encoder != encoder)
2144 continue;
2145
2146 return connector->state->crtc;
2147 }
2148
2149 /* Don't return stale data (e.g. pending async disable). */
2150 if (uses_atomic)
2151 return NULL;
2152
2153 return encoder->crtc;
2154}
2155
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002156/**
2157 * drm_mode_getencoder - get encoder configuration
2158 * @dev: drm device for the ioctl
2159 * @data: data pointer for the ioctl
2160 * @file_priv: drm file for the ioctl call
2161 *
2162 * Construct a encoder configuration structure to return to the user.
2163 *
2164 * Called by the user via ioctl.
2165 *
2166 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002167 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002168 */
Dave Airlief453ba02008-11-07 14:05:41 -08002169int drm_mode_getencoder(struct drm_device *dev, void *data,
2170 struct drm_file *file_priv)
2171{
2172 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002173 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002174 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002175
Dave Airliefb3b06c2011-02-08 13:55:21 +10002176 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2177 return -EINVAL;
2178
Rob Clarka2b34e22013-10-05 16:36:52 -04002179 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002180 if (!encoder)
2181 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002182
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002183 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002184 crtc = drm_encoder_get_crtc(encoder);
2185 if (crtc)
2186 enc_resp->crtc_id = crtc->base.id;
2187 else if (encoder->crtc)
Dave Airlief453ba02008-11-07 14:05:41 -08002188 enc_resp->crtc_id = encoder->crtc->base.id;
2189 else
2190 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002191 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2192
Dave Airlief453ba02008-11-07 14:05:41 -08002193 enc_resp->encoder_type = encoder->encoder_type;
2194 enc_resp->encoder_id = encoder->base.id;
2195 enc_resp->possible_crtcs = encoder->possible_crtcs;
2196 enc_resp->possible_clones = encoder->possible_clones;
2197
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002198 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002199}
2200
2201/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002202 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002203 * @dev: DRM device
2204 * @data: ioctl data
2205 * @file_priv: DRM file info
2206 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002207 * Construct a list of plane ids to return to the user.
2208 *
2209 * Called by the user via ioctl.
2210 *
2211 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002212 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002213 */
2214int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002215 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002216{
2217 struct drm_mode_get_plane_res *plane_resp = data;
2218 struct drm_mode_config *config;
2219 struct drm_plane *plane;
2220 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002221 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002222 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002223
2224 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2225 return -EINVAL;
2226
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002227 config = &dev->mode_config;
2228
Matt Roper681e7ec2014-04-01 15:22:42 -07002229 if (file_priv->universal_planes)
2230 num_planes = config->num_total_plane;
2231 else
2232 num_planes = config->num_overlay_plane;
2233
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002234 /*
2235 * This ioctl is called twice, once to determine how much space is
2236 * needed, and the 2nd time to fill it.
2237 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002238 if (num_planes &&
2239 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002240 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002241
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002242 /* Plane lists are invariant, no locking needed. */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002243 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002244 /*
2245 * Unless userspace set the 'universal planes'
2246 * capability bit, only advertise overlays.
2247 */
2248 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2249 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002250 continue;
2251
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002252 if (put_user(plane->base.id, plane_ptr + copied))
2253 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002254 copied++;
2255 }
2256 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002257 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002258
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002259 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002260}
2261
2262/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002263 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002264 * @dev: DRM device
2265 * @data: ioctl data
2266 * @file_priv: DRM file info
2267 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002268 * Construct a plane configuration structure to return to the user.
2269 *
2270 * Called by the user via ioctl.
2271 *
2272 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002273 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002274 */
2275int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002276 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002277{
2278 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002279 struct drm_plane *plane;
2280 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002281
2282 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2283 return -EINVAL;
2284
Rob Clarka2b34e22013-10-05 16:36:52 -04002285 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002286 if (!plane)
2287 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002288
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002289 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002290 if (plane->crtc)
2291 plane_resp->crtc_id = plane->crtc->base.id;
2292 else
2293 plane_resp->crtc_id = 0;
2294
2295 if (plane->fb)
2296 plane_resp->fb_id = plane->fb->base.id;
2297 else
2298 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002299 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002300
2301 plane_resp->plane_id = plane->base.id;
2302 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002303 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002304
2305 /*
2306 * This ioctl is called twice, once to determine how much space is
2307 * needed, and the 2nd time to fill it.
2308 */
2309 if (plane->format_count &&
2310 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002311 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002312 if (copy_to_user(format_ptr,
2313 plane->format_types,
2314 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002315 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002316 }
2317 }
2318 plane_resp->count_format_types = plane->format_count;
2319
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002320 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002321}
2322
Matt Roperb36552b2014-06-10 08:28:09 -07002323/*
2324 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325 *
Matt Roperb36552b2014-06-10 08:28:09 -07002326 * Note that we assume an extra reference has already been taken on fb. If the
2327 * update fails, this reference will be dropped before return; if it succeeds,
2328 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002329 *
Matt Roperb36552b2014-06-10 08:28:09 -07002330 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002331 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002332static int __setplane_internal(struct drm_plane *plane,
2333 struct drm_crtc *crtc,
2334 struct drm_framebuffer *fb,
2335 int32_t crtc_x, int32_t crtc_y,
2336 uint32_t crtc_w, uint32_t crtc_h,
2337 /* src_{x,y,w,h} values are 16.16 fixed point */
2338 uint32_t src_x, uint32_t src_y,
2339 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002340{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002341 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002342 unsigned int fb_width, fb_height;
Thierry Reding2f763312014-10-13 12:45:57 +02002343 unsigned int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002344
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002345 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002346 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002347 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002348 ret = plane->funcs->disable_plane(plane);
2349 if (!ret) {
2350 plane->crtc = NULL;
2351 plane->fb = NULL;
2352 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002353 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002354 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002355 goto out;
2356 }
2357
Matt Roper7f994f32014-05-29 08:06:51 -07002358 /* Check whether this plane is usable on this CRTC */
2359 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2360 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2361 ret = -EINVAL;
2362 goto out;
2363 }
2364
Ville Syrjälä62443be2011-12-20 00:06:47 +02002365 /* Check whether this plane supports the fb pixel format. */
2366 for (i = 0; i < plane->format_count; i++)
2367 if (fb->pixel_format == plane->format_types[i])
2368 break;
2369 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002370 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2371 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002372 ret = -EINVAL;
2373 goto out;
2374 }
2375
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002376 fb_width = fb->width << 16;
2377 fb_height = fb->height << 16;
2378
2379 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002380 if (src_w > fb_width ||
2381 src_x > fb_width - src_w ||
2382 src_h > fb_height ||
2383 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002384 DRM_DEBUG_KMS("Invalid source coordinates "
2385 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002386 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2387 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2388 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2389 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002390 ret = -ENOSPC;
2391 goto out;
2392 }
2393
Daniel Vetter3d30a592014-07-27 13:42:42 +02002394 plane->old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002395 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002396 crtc_x, crtc_y, crtc_w, crtc_h,
2397 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002398 if (!ret) {
2399 plane->crtc = crtc;
2400 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002401 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002402 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002403 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002404 }
2405
2406out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002407 if (fb)
2408 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002409 if (plane->old_fb)
2410 drm_framebuffer_unreference(plane->old_fb);
2411 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002412
2413 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002414}
Matt Roperb36552b2014-06-10 08:28:09 -07002415
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002416static int setplane_internal(struct drm_plane *plane,
2417 struct drm_crtc *crtc,
2418 struct drm_framebuffer *fb,
2419 int32_t crtc_x, int32_t crtc_y,
2420 uint32_t crtc_w, uint32_t crtc_h,
2421 /* src_{x,y,w,h} values are 16.16 fixed point */
2422 uint32_t src_x, uint32_t src_y,
2423 uint32_t src_w, uint32_t src_h)
2424{
2425 int ret;
2426
2427 drm_modeset_lock_all(plane->dev);
2428 ret = __setplane_internal(plane, crtc, fb,
2429 crtc_x, crtc_y, crtc_w, crtc_h,
2430 src_x, src_y, src_w, src_h);
2431 drm_modeset_unlock_all(plane->dev);
2432
2433 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002434}
2435
2436/**
2437 * drm_mode_setplane - configure a plane's configuration
2438 * @dev: DRM device
2439 * @data: ioctl data*
2440 * @file_priv: DRM file info
2441 *
2442 * Set plane configuration, including placement, fb, scaling, and other factors.
2443 * Or pass a NULL fb to disable (planes may be disabled without providing a
2444 * valid crtc).
2445 *
2446 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002447 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002448 */
2449int drm_mode_setplane(struct drm_device *dev, void *data,
2450 struct drm_file *file_priv)
2451{
2452 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002453 struct drm_plane *plane;
2454 struct drm_crtc *crtc = NULL;
2455 struct drm_framebuffer *fb = NULL;
2456
2457 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2458 return -EINVAL;
2459
2460 /* Give drivers some help against integer overflows */
2461 if (plane_req->crtc_w > INT_MAX ||
2462 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2463 plane_req->crtc_h > INT_MAX ||
2464 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2465 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2466 plane_req->crtc_w, plane_req->crtc_h,
2467 plane_req->crtc_x, plane_req->crtc_y);
2468 return -ERANGE;
2469 }
2470
2471 /*
2472 * First, find the plane, crtc, and fb objects. If not available,
2473 * we don't bother to call the driver.
2474 */
Rob Clark933f6222014-11-25 20:33:11 -05002475 plane = drm_plane_find(dev, plane_req->plane_id);
2476 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002477 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2478 plane_req->plane_id);
2479 return -ENOENT;
2480 }
Matt Roperb36552b2014-06-10 08:28:09 -07002481
2482 if (plane_req->fb_id) {
2483 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2484 if (!fb) {
2485 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2486 plane_req->fb_id);
2487 return -ENOENT;
2488 }
2489
Rob Clark933f6222014-11-25 20:33:11 -05002490 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2491 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002492 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2493 plane_req->crtc_id);
2494 return -ENOENT;
2495 }
Matt Roperb36552b2014-06-10 08:28:09 -07002496 }
2497
Matt Roper161d0dc2014-06-10 08:28:10 -07002498 /*
2499 * setplane_internal will take care of deref'ing either the old or new
2500 * framebuffer depending on success.
2501 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002502 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002503 plane_req->crtc_x, plane_req->crtc_y,
2504 plane_req->crtc_w, plane_req->crtc_h,
2505 plane_req->src_x, plane_req->src_y,
2506 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002507}
2508
2509/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002510 * drm_mode_set_config_internal - helper to call ->set_config
2511 * @set: modeset config to set
2512 *
2513 * This is a little helper to wrap internal calls to the ->set_config driver
2514 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002515 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002516 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002517 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002518 */
2519int drm_mode_set_config_internal(struct drm_mode_set *set)
2520{
2521 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002522 struct drm_framebuffer *fb;
2523 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002524 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002525
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002526 /*
2527 * NOTE: ->set_config can also disable other crtcs (if we steal all
2528 * connectors from it), hence we need to refcount the fbs across all
2529 * crtcs. Atomic modeset will have saner semantics ...
2530 */
2531 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002532 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002533
Daniel Vetterb0d12322012-12-11 01:07:12 +01002534 fb = set->fb;
2535
2536 ret = crtc->funcs->set_config(set);
2537 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002538 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002539 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002540 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002541
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002542 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002543 if (tmp->primary->fb)
2544 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002545 if (tmp->primary->old_fb)
2546 drm_framebuffer_unreference(tmp->primary->old_fb);
2547 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002548 }
2549
2550 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002551}
2552EXPORT_SYMBOL(drm_mode_set_config_internal);
2553
Matt Roperaf936292014-04-01 15:22:34 -07002554/**
2555 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2556 * CRTC viewport
2557 * @crtc: CRTC that framebuffer will be displayed on
2558 * @x: x panning
2559 * @y: y panning
2560 * @mode: mode that framebuffer will be displayed under
2561 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002562 */
Matt Roperaf936292014-04-01 15:22:34 -07002563int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2564 int x, int y,
2565 const struct drm_display_mode *mode,
2566 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002567
2568{
2569 int hdisplay, vdisplay;
2570
2571 hdisplay = mode->hdisplay;
2572 vdisplay = mode->vdisplay;
2573
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002574 if (drm_mode_is_stereo(mode)) {
2575 struct drm_display_mode adjusted = *mode;
2576
2577 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2578 hdisplay = adjusted.crtc_hdisplay;
2579 vdisplay = adjusted.crtc_vdisplay;
2580 }
2581
Damien Lespiauc11e9282013-09-25 16:45:30 +01002582 if (crtc->invert_dimensions)
2583 swap(hdisplay, vdisplay);
2584
2585 if (hdisplay > fb->width ||
2586 vdisplay > fb->height ||
2587 x > fb->width - hdisplay ||
2588 y > fb->height - vdisplay) {
2589 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2590 fb->width, fb->height, hdisplay, vdisplay, x, y,
2591 crtc->invert_dimensions ? " (inverted)" : "");
2592 return -ENOSPC;
2593 }
2594
2595 return 0;
2596}
Matt Roperaf936292014-04-01 15:22:34 -07002597EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002598
Daniel Vetter2d13b672012-12-11 13:47:23 +01002599/**
Dave Airlief453ba02008-11-07 14:05:41 -08002600 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002601 * @dev: drm device for the ioctl
2602 * @data: data pointer for the ioctl
2603 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002604 *
Dave Airlief453ba02008-11-07 14:05:41 -08002605 * Build a new CRTC configuration based on user request.
2606 *
2607 * Called by the user via ioctl.
2608 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002609 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002610 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002611 */
2612int drm_mode_setcrtc(struct drm_device *dev, void *data,
2613 struct drm_file *file_priv)
2614{
2615 struct drm_mode_config *config = &dev->mode_config;
2616 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002617 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002618 struct drm_connector **connector_set = NULL, *connector;
2619 struct drm_framebuffer *fb = NULL;
2620 struct drm_display_mode *mode = NULL;
2621 struct drm_mode_set set;
2622 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002623 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002624 int i;
2625
Dave Airliefb3b06c2011-02-08 13:55:21 +10002626 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2627 return -EINVAL;
2628
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002629 /* For some reason crtc x/y offsets are signed internally. */
2630 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2631 return -ERANGE;
2632
Daniel Vetter84849902012-12-02 00:28:11 +01002633 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002634 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2635 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002636 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002637 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002638 goto out;
2639 }
Jerome Glisse94401062010-07-15 15:43:25 -04002640 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002641
2642 if (crtc_req->mode_valid) {
2643 /* If we have a mode we need a framebuffer. */
2644 /* If we pass -1, set the mode with the currently bound fb */
2645 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002646 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002647 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2648 ret = -EINVAL;
2649 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002650 }
Matt Roperf4510a22014-04-01 15:22:40 -07002651 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002652 /* Make refcounting symmetric with the lookup path. */
2653 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002654 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002655 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2656 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002657 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2658 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002659 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002660 goto out;
2661 }
Dave Airlief453ba02008-11-07 14:05:41 -08002662 }
2663
2664 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002665 if (!mode) {
2666 ret = -ENOMEM;
2667 goto out;
2668 }
2669
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002670 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2671 if (ret) {
2672 DRM_DEBUG_KMS("Invalid mode\n");
2673 goto out;
2674 }
2675
Ville Syrjälä23e1ce82014-12-17 13:56:24 +02002676 mode->status = drm_mode_validate_basic(mode);
2677 if (mode->status != MODE_OK) {
2678 ret = -EINVAL;
2679 goto out;
2680 }
2681
Dave Airlief453ba02008-11-07 14:05:41 -08002682 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002683
Damien Lespiauc11e9282013-09-25 16:45:30 +01002684 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2685 mode, fb);
2686 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002687 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002688
Dave Airlief453ba02008-11-07 14:05:41 -08002689 }
2690
2691 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002692 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002693 ret = -EINVAL;
2694 goto out;
2695 }
2696
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002697 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002698 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002699 crtc_req->count_connectors);
2700 ret = -EINVAL;
2701 goto out;
2702 }
2703
2704 if (crtc_req->count_connectors > 0) {
2705 u32 out_id;
2706
2707 /* Avoid unbounded kernel memory allocation */
2708 if (crtc_req->count_connectors > config->num_connector) {
2709 ret = -EINVAL;
2710 goto out;
2711 }
2712
Thierry Reding2f6c5382014-12-10 13:03:36 +01002713 connector_set = kmalloc_array(crtc_req->count_connectors,
2714 sizeof(struct drm_connector *),
2715 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002716 if (!connector_set) {
2717 ret = -ENOMEM;
2718 goto out;
2719 }
2720
2721 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002722 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002723 if (get_user(out_id, &set_connectors_ptr[i])) {
2724 ret = -EFAULT;
2725 goto out;
2726 }
2727
Rob Clarka2b34e22013-10-05 16:36:52 -04002728 connector = drm_connector_find(dev, out_id);
2729 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002730 DRM_DEBUG_KMS("Connector id %d unknown\n",
2731 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002732 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002733 goto out;
2734 }
Jerome Glisse94401062010-07-15 15:43:25 -04002735 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2736 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002737 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002738
2739 connector_set[i] = connector;
2740 }
2741 }
2742
2743 set.crtc = crtc;
2744 set.x = crtc_req->x;
2745 set.y = crtc_req->y;
2746 set.mode = mode;
2747 set.connectors = connector_set;
2748 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002749 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002750 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002751
2752out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002753 if (fb)
2754 drm_framebuffer_unreference(fb);
2755
Dave Airlief453ba02008-11-07 14:05:41 -08002756 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002757 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002758 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002759 return ret;
2760}
2761
Matt Roper161d0dc2014-06-10 08:28:10 -07002762/**
2763 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2764 * universal plane handler call
2765 * @crtc: crtc to update cursor for
2766 * @req: data pointer for the ioctl
2767 * @file_priv: drm file for the ioctl call
2768 *
2769 * Legacy cursor ioctl's work directly with driver buffer handles. To
2770 * translate legacy ioctl calls into universal plane handler calls, we need to
2771 * wrap the native buffer handle in a drm_framebuffer.
2772 *
2773 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2774 * buffer with a pitch of 4*width; the universal plane interface should be used
2775 * directly in cases where the hardware can support other buffer settings and
2776 * userspace wants to make use of these capabilities.
2777 *
2778 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002779 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002780 */
2781static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2782 struct drm_mode_cursor2 *req,
2783 struct drm_file *file_priv)
2784{
2785 struct drm_device *dev = crtc->dev;
2786 struct drm_framebuffer *fb = NULL;
2787 struct drm_mode_fb_cmd2 fbreq = {
2788 .width = req->width,
2789 .height = req->height,
2790 .pixel_format = DRM_FORMAT_ARGB8888,
2791 .pitches = { req->width * 4 },
2792 .handles = { req->handle },
2793 };
2794 int32_t crtc_x, crtc_y;
2795 uint32_t crtc_w = 0, crtc_h = 0;
2796 uint32_t src_w = 0, src_h = 0;
2797 int ret = 0;
2798
2799 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002800 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002801
2802 /*
2803 * Obtain fb we'll be using (either new or existing) and take an extra
2804 * reference to it if fb != null. setplane will take care of dropping
2805 * the reference if the plane update fails.
2806 */
2807 if (req->flags & DRM_MODE_CURSOR_BO) {
2808 if (req->handle) {
2809 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2810 if (IS_ERR(fb)) {
2811 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2812 return PTR_ERR(fb);
2813 }
2814
2815 drm_framebuffer_reference(fb);
2816 } else {
2817 fb = NULL;
2818 }
2819 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002820 fb = crtc->cursor->fb;
2821 if (fb)
2822 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002823 }
2824
2825 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2826 crtc_x = req->x;
2827 crtc_y = req->y;
2828 } else {
2829 crtc_x = crtc->cursor_x;
2830 crtc_y = crtc->cursor_y;
2831 }
2832
2833 if (fb) {
2834 crtc_w = fb->width;
2835 crtc_h = fb->height;
2836 src_w = fb->width << 16;
2837 src_h = fb->height << 16;
2838 }
2839
2840 /*
2841 * setplane_internal will take care of deref'ing either the old or new
2842 * framebuffer depending on success.
2843 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002844 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002845 crtc_x, crtc_y, crtc_w, crtc_h,
2846 0, 0, src_w, src_h);
2847
2848 /* Update successful; save new cursor position, if necessary */
2849 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2850 crtc->cursor_x = req->x;
2851 crtc->cursor_y = req->y;
2852 }
2853
2854 return ret;
2855}
2856
Dave Airlie4c813d42013-06-20 11:48:52 +10002857static int drm_mode_cursor_common(struct drm_device *dev,
2858 struct drm_mode_cursor2 *req,
2859 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002860{
Dave Airlief453ba02008-11-07 14:05:41 -08002861 struct drm_crtc *crtc;
2862 int ret = 0;
2863
Dave Airliefb3b06c2011-02-08 13:55:21 +10002864 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2865 return -EINVAL;
2866
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002867 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002868 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002869
Rob Clarka2b34e22013-10-05 16:36:52 -04002870 crtc = drm_crtc_find(dev, req->crtc_id);
2871 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002872 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002873 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002874 }
Dave Airlief453ba02008-11-07 14:05:41 -08002875
Matt Roper161d0dc2014-06-10 08:28:10 -07002876 /*
2877 * If this crtc has a universal cursor plane, call that plane's update
2878 * handler rather than using legacy cursor handlers.
2879 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002880 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002881 if (crtc->cursor) {
2882 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2883 goto out;
2884 }
2885
Dave Airlief453ba02008-11-07 14:05:41 -08002886 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002887 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002888 ret = -ENXIO;
2889 goto out;
2890 }
2891 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002892 if (crtc->funcs->cursor_set2)
2893 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2894 req->width, req->height, req->hot_x, req->hot_y);
2895 else
2896 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2897 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002898 }
2899
2900 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2901 if (crtc->funcs->cursor_move) {
2902 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2903 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002904 ret = -EFAULT;
2905 goto out;
2906 }
2907 }
2908out:
Daniel Vetterd059f652014-07-25 18:07:40 +02002909 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01002910
Dave Airlief453ba02008-11-07 14:05:41 -08002911 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002912
2913}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002914
2915
2916/**
2917 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2918 * @dev: drm device for the ioctl
2919 * @data: data pointer for the ioctl
2920 * @file_priv: drm file for the ioctl call
2921 *
2922 * Set the cursor configuration based on user request.
2923 *
2924 * Called by the user via ioctl.
2925 *
2926 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002927 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002928 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002929int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002930 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002931{
2932 struct drm_mode_cursor *req = data;
2933 struct drm_mode_cursor2 new_req;
2934
2935 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2936 new_req.hot_x = new_req.hot_y = 0;
2937
2938 return drm_mode_cursor_common(dev, &new_req, file_priv);
2939}
2940
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002941/**
2942 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2943 * @dev: drm device for the ioctl
2944 * @data: data pointer for the ioctl
2945 * @file_priv: drm file for the ioctl call
2946 *
2947 * Set the cursor configuration based on user request. This implements the 2nd
2948 * version of the cursor ioctl, which allows userspace to additionally specify
2949 * the hotspot of the pointer.
2950 *
2951 * Called by the user via ioctl.
2952 *
2953 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002954 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002955 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002956int drm_mode_cursor2_ioctl(struct drm_device *dev,
2957 void *data, struct drm_file *file_priv)
2958{
2959 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01002960
Dave Airlie4c813d42013-06-20 11:48:52 +10002961 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002962}
2963
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002964/**
2965 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2966 * @bpp: bits per pixels
2967 * @depth: bit depth per pixel
2968 *
2969 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2970 * Useful in fbdev emulation code, since that deals in those values.
2971 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002972uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2973{
2974 uint32_t fmt;
2975
2976 switch (bpp) {
2977 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002978 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002979 break;
2980 case 16:
2981 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002982 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002983 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002984 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002985 break;
2986 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002987 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002988 break;
2989 case 32:
2990 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002991 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002992 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002993 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002994 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002995 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002996 break;
2997 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002998 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2999 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003000 break;
3001 }
3002
3003 return fmt;
3004}
3005EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3006
Dave Airlief453ba02008-11-07 14:05:41 -08003007/**
3008 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003009 * @dev: drm device for the ioctl
3010 * @data: data pointer for the ioctl
3011 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003012 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003013 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003014 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003015 *
3016 * Called by the user via ioctl.
3017 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003018 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003019 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003020 */
3021int drm_mode_addfb(struct drm_device *dev,
3022 void *data, struct drm_file *file_priv)
3023{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003024 struct drm_mode_fb_cmd *or = data;
3025 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003026 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003027
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003028 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003029 r.fb_id = or->fb_id;
3030 r.width = or->width;
3031 r.height = or->height;
3032 r.pitches[0] = or->pitch;
3033 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3034 r.handles[0] = or->handle;
3035
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003036 ret = drm_mode_addfb2(dev, &r, file_priv);
3037 if (ret)
3038 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003039
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003040 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003041
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003042 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003043}
3044
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003045static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003046{
3047 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3048
3049 switch (format) {
3050 case DRM_FORMAT_C8:
3051 case DRM_FORMAT_RGB332:
3052 case DRM_FORMAT_BGR233:
3053 case DRM_FORMAT_XRGB4444:
3054 case DRM_FORMAT_XBGR4444:
3055 case DRM_FORMAT_RGBX4444:
3056 case DRM_FORMAT_BGRX4444:
3057 case DRM_FORMAT_ARGB4444:
3058 case DRM_FORMAT_ABGR4444:
3059 case DRM_FORMAT_RGBA4444:
3060 case DRM_FORMAT_BGRA4444:
3061 case DRM_FORMAT_XRGB1555:
3062 case DRM_FORMAT_XBGR1555:
3063 case DRM_FORMAT_RGBX5551:
3064 case DRM_FORMAT_BGRX5551:
3065 case DRM_FORMAT_ARGB1555:
3066 case DRM_FORMAT_ABGR1555:
3067 case DRM_FORMAT_RGBA5551:
3068 case DRM_FORMAT_BGRA5551:
3069 case DRM_FORMAT_RGB565:
3070 case DRM_FORMAT_BGR565:
3071 case DRM_FORMAT_RGB888:
3072 case DRM_FORMAT_BGR888:
3073 case DRM_FORMAT_XRGB8888:
3074 case DRM_FORMAT_XBGR8888:
3075 case DRM_FORMAT_RGBX8888:
3076 case DRM_FORMAT_BGRX8888:
3077 case DRM_FORMAT_ARGB8888:
3078 case DRM_FORMAT_ABGR8888:
3079 case DRM_FORMAT_RGBA8888:
3080 case DRM_FORMAT_BGRA8888:
3081 case DRM_FORMAT_XRGB2101010:
3082 case DRM_FORMAT_XBGR2101010:
3083 case DRM_FORMAT_RGBX1010102:
3084 case DRM_FORMAT_BGRX1010102:
3085 case DRM_FORMAT_ARGB2101010:
3086 case DRM_FORMAT_ABGR2101010:
3087 case DRM_FORMAT_RGBA1010102:
3088 case DRM_FORMAT_BGRA1010102:
3089 case DRM_FORMAT_YUYV:
3090 case DRM_FORMAT_YVYU:
3091 case DRM_FORMAT_UYVY:
3092 case DRM_FORMAT_VYUY:
3093 case DRM_FORMAT_AYUV:
3094 case DRM_FORMAT_NV12:
3095 case DRM_FORMAT_NV21:
3096 case DRM_FORMAT_NV16:
3097 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003098 case DRM_FORMAT_NV24:
3099 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003100 case DRM_FORMAT_YUV410:
3101 case DRM_FORMAT_YVU410:
3102 case DRM_FORMAT_YUV411:
3103 case DRM_FORMAT_YVU411:
3104 case DRM_FORMAT_YUV420:
3105 case DRM_FORMAT_YVU420:
3106 case DRM_FORMAT_YUV422:
3107 case DRM_FORMAT_YVU422:
3108 case DRM_FORMAT_YUV444:
3109 case DRM_FORMAT_YVU444:
3110 return 0;
3111 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003112 DRM_DEBUG_KMS("invalid pixel format %s\n",
3113 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003114 return -EINVAL;
3115 }
3116}
3117
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003118static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003119{
3120 int ret, hsub, vsub, num_planes, i;
3121
3122 ret = format_check(r);
3123 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003124 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3125 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003126 return ret;
3127 }
3128
3129 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3130 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3131 num_planes = drm_format_num_planes(r->pixel_format);
3132
3133 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003134 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003135 return -EINVAL;
3136 }
3137
3138 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003139 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003140 return -EINVAL;
3141 }
3142
3143 for (i = 0; i < num_planes; i++) {
3144 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003145 unsigned int height = r->height / (i != 0 ? vsub : 1);
3146 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003147
3148 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003149 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003150 return -EINVAL;
3151 }
3152
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003153 if ((uint64_t) width * cpp > UINT_MAX)
3154 return -ERANGE;
3155
3156 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3157 return -ERANGE;
3158
3159 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003160 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003161 return -EINVAL;
3162 }
3163 }
3164
3165 return 0;
3166}
3167
Matt Roperc394c2b2014-06-10 08:28:08 -07003168static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3169 struct drm_mode_fb_cmd2 *r,
3170 struct drm_file *file_priv)
3171{
3172 struct drm_mode_config *config = &dev->mode_config;
3173 struct drm_framebuffer *fb;
3174 int ret;
3175
3176 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3177 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3178 return ERR_PTR(-EINVAL);
3179 }
3180
3181 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3182 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3183 r->width, config->min_width, config->max_width);
3184 return ERR_PTR(-EINVAL);
3185 }
3186 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3187 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3188 r->height, config->min_height, config->max_height);
3189 return ERR_PTR(-EINVAL);
3190 }
3191
3192 ret = framebuffer_check(r);
3193 if (ret)
3194 return ERR_PTR(ret);
3195
3196 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3197 if (IS_ERR(fb)) {
3198 DRM_DEBUG_KMS("could not create framebuffer\n");
3199 return fb;
3200 }
3201
3202 mutex_lock(&file_priv->fbs_lock);
3203 r->fb_id = fb->base.id;
3204 list_add(&fb->filp_head, &file_priv->fbs);
3205 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3206 mutex_unlock(&file_priv->fbs_lock);
3207
3208 return fb;
3209}
3210
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003211/**
3212 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003213 * @dev: drm device for the ioctl
3214 * @data: data pointer for the ioctl
3215 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003216 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003217 * Add a new FB to the specified CRTC, given a user request with format. This is
3218 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3219 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003220 *
3221 * Called by the user via ioctl.
3222 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003223 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003224 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003225 */
3226int drm_mode_addfb2(struct drm_device *dev,
3227 void *data, struct drm_file *file_priv)
3228{
Dave Airlief453ba02008-11-07 14:05:41 -08003229 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003230
Dave Airliefb3b06c2011-02-08 13:55:21 +10003231 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3232 return -EINVAL;
3233
Matt Roperc394c2b2014-06-10 08:28:08 -07003234 fb = add_framebuffer_internal(dev, data, file_priv);
3235 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003236 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003237
Matt Roperc394c2b2014-06-10 08:28:08 -07003238 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003239}
3240
3241/**
3242 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003243 * @dev: drm device for the ioctl
3244 * @data: data pointer for the ioctl
3245 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003246 *
Dave Airlief453ba02008-11-07 14:05:41 -08003247 * Remove the FB specified by the user.
3248 *
3249 * Called by the user via ioctl.
3250 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003251 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003252 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003253 */
3254int drm_mode_rmfb(struct drm_device *dev,
3255 void *data, struct drm_file *file_priv)
3256{
Dave Airlief453ba02008-11-07 14:05:41 -08003257 struct drm_framebuffer *fb = NULL;
3258 struct drm_framebuffer *fbl = NULL;
3259 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003260 int found = 0;
3261
Dave Airliefb3b06c2011-02-08 13:55:21 +10003262 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3263 return -EINVAL;
3264
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003265 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003266 mutex_lock(&dev->mode_config.fb_lock);
3267 fb = __drm_framebuffer_lookup(dev, *id);
3268 if (!fb)
3269 goto fail_lookup;
3270
Dave Airlief453ba02008-11-07 14:05:41 -08003271 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3272 if (fb == fbl)
3273 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003274 if (!found)
3275 goto fail_lookup;
3276
3277 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3278 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003279
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003280 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003281 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003282 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003283
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003284 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003285
Daniel Vetter2b677e82012-12-10 21:16:05 +01003286 return 0;
3287
3288fail_lookup:
3289 mutex_unlock(&dev->mode_config.fb_lock);
3290 mutex_unlock(&file_priv->fbs_lock);
3291
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003292 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003293}
3294
3295/**
3296 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003297 * @dev: drm device for the ioctl
3298 * @data: data pointer for the ioctl
3299 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003300 *
Dave Airlief453ba02008-11-07 14:05:41 -08003301 * Lookup the FB given its ID and return info about it.
3302 *
3303 * Called by the user via ioctl.
3304 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003305 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003306 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003307 */
3308int drm_mode_getfb(struct drm_device *dev,
3309 void *data, struct drm_file *file_priv)
3310{
3311 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003312 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003313 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003314
Dave Airliefb3b06c2011-02-08 13:55:21 +10003315 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3316 return -EINVAL;
3317
Daniel Vetter786b99e2012-12-02 21:53:40 +01003318 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003319 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003320 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003321
3322 r->height = fb->height;
3323 r->width = fb->width;
3324 r->depth = fb->depth;
3325 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003326 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003327 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003328 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003329 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003330 ret = fb->funcs->create_handle(fb, file_priv,
3331 &r->handle);
3332 } else {
3333 /* GET_FB() is an unprivileged ioctl so we must not
3334 * return a buffer-handle to non-master processes! For
3335 * backwards-compatibility reasons, we cannot make
3336 * GET_FB() privileged, so just return an invalid handle
3337 * for non-masters. */
3338 r->handle = 0;
3339 ret = 0;
3340 }
3341 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003342 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003343 }
Dave Airlief453ba02008-11-07 14:05:41 -08003344
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003345 drm_framebuffer_unreference(fb);
3346
Dave Airlief453ba02008-11-07 14:05:41 -08003347 return ret;
3348}
3349
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003350/**
3351 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3352 * @dev: drm device for the ioctl
3353 * @data: data pointer for the ioctl
3354 * @file_priv: drm file for the ioctl call
3355 *
3356 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3357 * rectangle list. Generic userspace which does frontbuffer rendering must call
3358 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3359 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3360 *
3361 * Modesetting drivers which always update the frontbuffer do not need to
3362 * implement the corresponding ->dirty framebuffer callback.
3363 *
3364 * Called by the user via ioctl.
3365 *
3366 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003367 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003368 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003369int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3370 void *data, struct drm_file *file_priv)
3371{
3372 struct drm_clip_rect __user *clips_ptr;
3373 struct drm_clip_rect *clips = NULL;
3374 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003375 struct drm_framebuffer *fb;
3376 unsigned flags;
3377 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003378 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003379
Dave Airliefb3b06c2011-02-08 13:55:21 +10003380 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3381 return -EINVAL;
3382
Daniel Vetter786b99e2012-12-02 21:53:40 +01003383 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003384 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003385 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003386
3387 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003388 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003389
3390 if (!num_clips != !clips_ptr) {
3391 ret = -EINVAL;
3392 goto out_err1;
3393 }
3394
3395 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3396
3397 /* If userspace annotates copy, clips must come in pairs */
3398 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3399 ret = -EINVAL;
3400 goto out_err1;
3401 }
3402
3403 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003404 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3405 ret = -EINVAL;
3406 goto out_err1;
3407 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003408 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003409 if (!clips) {
3410 ret = -ENOMEM;
3411 goto out_err1;
3412 }
3413
3414 ret = copy_from_user(clips, clips_ptr,
3415 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003416 if (ret) {
3417 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003418 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003419 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003420 }
3421
3422 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003423 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3424 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003425 } else {
3426 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003427 }
3428
3429out_err2:
3430 kfree(clips);
3431out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003432 drm_framebuffer_unreference(fb);
3433
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003434 return ret;
3435}
3436
3437
Dave Airlief453ba02008-11-07 14:05:41 -08003438/**
3439 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003440 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003441 *
Dave Airlief453ba02008-11-07 14:05:41 -08003442 * Destroy all the FBs associated with @filp.
3443 *
3444 * Called by the user via ioctl.
3445 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003446 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003447 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003448 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003449void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003450{
Dave Airlief453ba02008-11-07 14:05:41 -08003451 struct drm_device *dev = priv->minor->dev;
3452 struct drm_framebuffer *fb, *tfb;
3453
Daniel Vetter1b116292014-09-24 21:51:06 +02003454 /*
3455 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003456 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003457 * avoid upsetting lockdep since the universal cursor code adds a
3458 * framebuffer while holding mutex locks.
3459 *
3460 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3461 * locks is impossible here since no one else but this function can get
3462 * at it any more.
3463 */
Dave Airlief453ba02008-11-07 14:05:41 -08003464 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003465
3466 mutex_lock(&dev->mode_config.fb_lock);
3467 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3468 __drm_framebuffer_unregister(dev, fb);
3469 mutex_unlock(&dev->mode_config.fb_lock);
3470
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003471 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003472
3473 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003474 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003475 }
Dave Airlief453ba02008-11-07 14:05:41 -08003476}
3477
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003478/**
3479 * drm_property_create - create a new property type
3480 * @dev: drm device
3481 * @flags: flags specifying the property type
3482 * @name: name of the property
3483 * @num_values: number of pre-defined values
3484 *
3485 * This creates a new generic drm property which can then be attached to a drm
3486 * object with drm_object_attach_property. The returned property object must be
3487 * freed with drm_property_destroy.
3488 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003489 * Note that the DRM core keeps a per-device list of properties and that, if
3490 * drm_mode_config_cleanup() is called, it will destroy all properties created
3491 * by the driver.
3492 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003493 * Returns:
3494 * A pointer to the newly created property on success, NULL on failure.
3495 */
Dave Airlief453ba02008-11-07 14:05:41 -08003496struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3497 const char *name, int num_values)
3498{
3499 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003500 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003501
3502 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3503 if (!property)
3504 return NULL;
3505
Rob Clark98f75de2014-05-30 11:37:03 -04003506 property->dev = dev;
3507
Dave Airlief453ba02008-11-07 14:05:41 -08003508 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003509 property->values = kcalloc(num_values, sizeof(uint64_t),
3510 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003511 if (!property->values)
3512 goto fail;
3513 }
3514
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003515 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3516 if (ret)
3517 goto fail;
3518
Dave Airlief453ba02008-11-07 14:05:41 -08003519 property->flags = flags;
3520 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003521 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003522
Vinson Lee471dd2e2011-11-10 11:55:40 -08003523 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003524 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003525 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3526 }
Dave Airlief453ba02008-11-07 14:05:41 -08003527
3528 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003529
3530 WARN_ON(!drm_property_type_valid(property));
3531
Dave Airlief453ba02008-11-07 14:05:41 -08003532 return property;
3533fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003534 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003535 kfree(property);
3536 return NULL;
3537}
3538EXPORT_SYMBOL(drm_property_create);
3539
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003540/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003541 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003542 * @dev: drm device
3543 * @flags: flags specifying the property type
3544 * @name: name of the property
3545 * @props: enumeration lists with property values
3546 * @num_values: number of pre-defined values
3547 *
3548 * This creates a new generic drm property which can then be attached to a drm
3549 * object with drm_object_attach_property. The returned property object must be
3550 * freed with drm_property_destroy.
3551 *
3552 * Userspace is only allowed to set one of the predefined values for enumeration
3553 * properties.
3554 *
3555 * Returns:
3556 * A pointer to the newly created property on success, NULL on failure.
3557 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003558struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3559 const char *name,
3560 const struct drm_prop_enum_list *props,
3561 int num_values)
3562{
3563 struct drm_property *property;
3564 int i, ret;
3565
3566 flags |= DRM_MODE_PROP_ENUM;
3567
3568 property = drm_property_create(dev, flags, name, num_values);
3569 if (!property)
3570 return NULL;
3571
3572 for (i = 0; i < num_values; i++) {
3573 ret = drm_property_add_enum(property, i,
3574 props[i].type,
3575 props[i].name);
3576 if (ret) {
3577 drm_property_destroy(dev, property);
3578 return NULL;
3579 }
3580 }
3581
3582 return property;
3583}
3584EXPORT_SYMBOL(drm_property_create_enum);
3585
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003586/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003587 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003588 * @dev: drm device
3589 * @flags: flags specifying the property type
3590 * @name: name of the property
3591 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003592 * @num_props: size of the @props array
3593 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003594 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003595 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003596 * object with drm_object_attach_property. The returned property object must be
3597 * freed with drm_property_destroy.
3598 *
3599 * Compared to plain enumeration properties userspace is allowed to set any
3600 * or'ed together combination of the predefined property bitflag values
3601 *
3602 * Returns:
3603 * A pointer to the newly created property on success, NULL on failure.
3604 */
Rob Clark49e27542012-05-17 02:23:26 -06003605struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3606 int flags, const char *name,
3607 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303608 int num_props,
3609 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003610{
3611 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303612 int i, ret, index = 0;
3613 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003614
3615 flags |= DRM_MODE_PROP_BITMASK;
3616
3617 property = drm_property_create(dev, flags, name, num_values);
3618 if (!property)
3619 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303620 for (i = 0; i < num_props; i++) {
3621 if (!(supported_bits & (1ULL << props[i].type)))
3622 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003623
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303624 if (WARN_ON(index >= num_values)) {
3625 drm_property_destroy(dev, property);
3626 return NULL;
3627 }
3628
3629 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003630 props[i].type,
3631 props[i].name);
3632 if (ret) {
3633 drm_property_destroy(dev, property);
3634 return NULL;
3635 }
3636 }
3637
3638 return property;
3639}
3640EXPORT_SYMBOL(drm_property_create_bitmask);
3641
Rob Clarkebc44cf2012-09-12 22:22:31 -05003642static struct drm_property *property_create_range(struct drm_device *dev,
3643 int flags, const char *name,
3644 uint64_t min, uint64_t max)
3645{
3646 struct drm_property *property;
3647
3648 property = drm_property_create(dev, flags, name, 2);
3649 if (!property)
3650 return NULL;
3651
3652 property->values[0] = min;
3653 property->values[1] = max;
3654
3655 return property;
3656}
3657
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003658/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003659 * drm_property_create_range - create a new ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003660 * @dev: drm device
3661 * @flags: flags specifying the property type
3662 * @name: name of the property
3663 * @min: minimum value of the property
3664 * @max: maximum value of the property
3665 *
3666 * This creates a new generic drm property which can then be attached to a drm
3667 * object with drm_object_attach_property. The returned property object must be
3668 * freed with drm_property_destroy.
3669 *
Masanari Iida32197aa2014-10-20 23:53:13 +09003670 * Userspace is allowed to set any integer value in the (min, max) range
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003671 * inclusive.
3672 *
3673 * Returns:
3674 * A pointer to the newly created property on success, NULL on failure.
3675 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003676struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3677 const char *name,
3678 uint64_t min, uint64_t max)
3679{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003680 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3681 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003682}
3683EXPORT_SYMBOL(drm_property_create_range);
3684
Rob Clarkebc44cf2012-09-12 22:22:31 -05003685struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3686 int flags, const char *name,
3687 int64_t min, int64_t max)
3688{
3689 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3690 name, I642U64(min), I642U64(max));
3691}
3692EXPORT_SYMBOL(drm_property_create_signed_range);
3693
Rob Clark98f75de2014-05-30 11:37:03 -04003694struct drm_property *drm_property_create_object(struct drm_device *dev,
3695 int flags, const char *name, uint32_t type)
3696{
3697 struct drm_property *property;
3698
3699 flags |= DRM_MODE_PROP_OBJECT;
3700
3701 property = drm_property_create(dev, flags, name, 1);
3702 if (!property)
3703 return NULL;
3704
3705 property->values[0] = type;
3706
3707 return property;
3708}
3709EXPORT_SYMBOL(drm_property_create_object);
3710
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003711/**
3712 * drm_property_add_enum - add a possible value to an enumeration property
3713 * @property: enumeration property to change
3714 * @index: index of the new enumeration
3715 * @value: value of the new enumeration
3716 * @name: symbolic name of the new enumeration
3717 *
3718 * This functions adds enumerations to a property.
3719 *
3720 * It's use is deprecated, drivers should use one of the more specific helpers
3721 * to directly create the property with all enumerations already attached.
3722 *
3723 * Returns:
3724 * Zero on success, error code on failure.
3725 */
Dave Airlief453ba02008-11-07 14:05:41 -08003726int drm_property_add_enum(struct drm_property *property, int index,
3727 uint64_t value, const char *name)
3728{
3729 struct drm_property_enum *prop_enum;
3730
Rob Clark5ea22f22014-05-30 11:34:01 -04003731 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3732 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003733 return -EINVAL;
3734
3735 /*
3736 * Bitmask enum properties have the additional constraint of values
3737 * from 0 to 63
3738 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003739 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3740 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003741 return -EINVAL;
3742
Daniel Vetter3758b342014-11-19 18:38:10 +01003743 if (!list_empty(&property->enum_list)) {
3744 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003745 if (prop_enum->value == value) {
3746 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3747 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3748 return 0;
3749 }
3750 }
3751 }
3752
3753 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3754 if (!prop_enum)
3755 return -ENOMEM;
3756
3757 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3758 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3759 prop_enum->value = value;
3760
3761 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01003762 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003763 return 0;
3764}
3765EXPORT_SYMBOL(drm_property_add_enum);
3766
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003767/**
3768 * drm_property_destroy - destroy a drm property
3769 * @dev: drm device
3770 * @property: property to destry
3771 *
3772 * This function frees a property including any attached resources like
3773 * enumeration values.
3774 */
Dave Airlief453ba02008-11-07 14:05:41 -08003775void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3776{
3777 struct drm_property_enum *prop_enum, *pt;
3778
Daniel Vetter3758b342014-11-19 18:38:10 +01003779 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003780 list_del(&prop_enum->head);
3781 kfree(prop_enum);
3782 }
3783
3784 if (property->num_values)
3785 kfree(property->values);
3786 drm_mode_object_put(dev, &property->base);
3787 list_del(&property->head);
3788 kfree(property);
3789}
3790EXPORT_SYMBOL(drm_property_destroy);
3791
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003792/**
3793 * drm_object_attach_property - attach a property to a modeset object
3794 * @obj: drm modeset object
3795 * @property: property to attach
3796 * @init_val: initial value of the property
3797 *
3798 * This attaches the given property to the modeset object with the given initial
3799 * value. Currently this function cannot fail since the properties are stored in
3800 * a statically sized array.
3801 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003802void drm_object_attach_property(struct drm_mode_object *obj,
3803 struct drm_property *property,
3804 uint64_t init_val)
3805{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003806 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003807
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003808 if (count == DRM_OBJECT_MAX_PROPERTY) {
3809 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3810 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3811 "you see this message on the same object type.\n",
3812 obj->type);
3813 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003814 }
3815
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003816 obj->properties->ids[count] = property->base.id;
3817 obj->properties->values[count] = init_val;
3818 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003819}
3820EXPORT_SYMBOL(drm_object_attach_property);
3821
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003822/**
3823 * drm_object_property_set_value - set the value of a property
3824 * @obj: drm mode object to set property value for
3825 * @property: property to set
3826 * @val: value the property should be set to
3827 *
3828 * This functions sets a given property on a given object. This function only
3829 * changes the software state of the property, it does not call into the
3830 * driver's ->set_property callback.
3831 *
3832 * Returns:
3833 * Zero on success, error code on failure.
3834 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003835int drm_object_property_set_value(struct drm_mode_object *obj,
3836 struct drm_property *property, uint64_t val)
3837{
3838 int i;
3839
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003840 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003841 if (obj->properties->ids[i] == property->base.id) {
3842 obj->properties->values[i] = val;
3843 return 0;
3844 }
3845 }
3846
3847 return -EINVAL;
3848}
3849EXPORT_SYMBOL(drm_object_property_set_value);
3850
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003851/**
3852 * drm_object_property_get_value - retrieve the value of a property
3853 * @obj: drm mode object to get property value from
3854 * @property: property to retrieve
3855 * @val: storage for the property value
3856 *
3857 * This function retrieves the softare state of the given property for the given
3858 * property. Since there is no driver callback to retrieve the current property
3859 * value this might be out of sync with the hardware, depending upon the driver
3860 * and property.
3861 *
3862 * Returns:
3863 * Zero on success, error code on failure.
3864 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003865int drm_object_property_get_value(struct drm_mode_object *obj,
3866 struct drm_property *property, uint64_t *val)
3867{
3868 int i;
3869
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003870 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003871 if (obj->properties->ids[i] == property->base.id) {
3872 *val = obj->properties->values[i];
3873 return 0;
3874 }
3875 }
3876
3877 return -EINVAL;
3878}
3879EXPORT_SYMBOL(drm_object_property_get_value);
3880
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003881/**
Daniel Vetter1a498632014-11-19 18:38:09 +01003882 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003883 * @dev: DRM device
3884 * @data: ioctl data
3885 * @file_priv: DRM file info
3886 *
Daniel Vetter1a498632014-11-19 18:38:09 +01003887 * This function retrieves the metadata for a given property, like the different
3888 * possible values for an enum property or the limits for a range property.
3889 *
3890 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003891 *
3892 * Called by the user via ioctl.
3893 *
3894 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003895 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003896 */
Dave Airlief453ba02008-11-07 14:05:41 -08003897int drm_mode_getproperty_ioctl(struct drm_device *dev,
3898 void *data, struct drm_file *file_priv)
3899{
Dave Airlief453ba02008-11-07 14:05:41 -08003900 struct drm_mode_get_property *out_resp = data;
3901 struct drm_property *property;
3902 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003903 int value_count = 0;
3904 int ret = 0, i;
3905 int copied;
3906 struct drm_property_enum *prop_enum;
3907 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003908 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003909
Dave Airliefb3b06c2011-02-08 13:55:21 +10003910 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3911 return -EINVAL;
3912
Daniel Vetter84849902012-12-02 00:28:11 +01003913 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003914 property = drm_property_find(dev, out_resp->prop_id);
3915 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003916 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003917 goto done;
3918 }
Dave Airlief453ba02008-11-07 14:05:41 -08003919
Rob Clark5ea22f22014-05-30 11:34:01 -04003920 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3921 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01003922 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08003923 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08003924 }
3925
3926 value_count = property->num_values;
3927
3928 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3929 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3930 out_resp->flags = property->flags;
3931
3932 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003933 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003934 for (i = 0; i < value_count; i++) {
3935 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3936 ret = -EFAULT;
3937 goto done;
3938 }
3939 }
3940 }
3941 out_resp->count_values = value_count;
3942
Rob Clark5ea22f22014-05-30 11:34:01 -04003943 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3944 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003945 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3946 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003947 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01003948 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003949
3950 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3951 ret = -EFAULT;
3952 goto done;
3953 }
3954
3955 if (copy_to_user(&enum_ptr[copied].name,
3956 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3957 ret = -EFAULT;
3958 goto done;
3959 }
3960 copied++;
3961 }
3962 }
3963 out_resp->count_enum_blobs = enum_count;
3964 }
3965
Daniel Vetter3758b342014-11-19 18:38:10 +01003966 /*
3967 * NOTE: The idea seems to have been to use this to read all the blob
3968 * property values. But nothing ever added them to the corresponding
3969 * list, userspace always used the special-purpose get_blob ioctl to
3970 * read the value for a blob property. It also doesn't make a lot of
3971 * sense to return values here when everything else is just metadata for
3972 * the property itself.
3973 */
3974 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
3975 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003976done:
Daniel Vetter84849902012-12-02 00:28:11 +01003977 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003978 return ret;
3979}
3980
Thierry Redingecbbe592014-05-13 11:36:13 +02003981static struct drm_property_blob *
3982drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02003983 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08003984{
3985 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003986 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003987
3988 if (!length || !data)
3989 return NULL;
3990
3991 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3992 if (!blob)
3993 return NULL;
3994
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003995 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3996 if (ret) {
3997 kfree(blob);
3998 return NULL;
3999 }
4000
Dave Airlief453ba02008-11-07 14:05:41 -08004001 blob->length = length;
4002
4003 memcpy(blob->data, data, length);
4004
Dave Airlief453ba02008-11-07 14:05:41 -08004005 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
4006 return blob;
4007}
4008
4009static void drm_property_destroy_blob(struct drm_device *dev,
4010 struct drm_property_blob *blob)
4011{
4012 drm_mode_object_put(dev, &blob->base);
4013 list_del(&blob->head);
4014 kfree(blob);
4015}
4016
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004017/**
4018 * drm_mode_getblob_ioctl - get the contents of a blob property value
4019 * @dev: DRM device
4020 * @data: ioctl data
4021 * @file_priv: DRM file info
4022 *
4023 * This function retrieves the contents of a blob property. The value stored in
4024 * an object's blob property is just a normal modeset object id.
4025 *
4026 * Called by the user via ioctl.
4027 *
4028 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004029 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004030 */
Dave Airlief453ba02008-11-07 14:05:41 -08004031int drm_mode_getblob_ioctl(struct drm_device *dev,
4032 void *data, struct drm_file *file_priv)
4033{
Dave Airlief453ba02008-11-07 14:05:41 -08004034 struct drm_mode_get_blob *out_resp = data;
4035 struct drm_property_blob *blob;
4036 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004037 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004038
Dave Airliefb3b06c2011-02-08 13:55:21 +10004039 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4040 return -EINVAL;
4041
Daniel Vetter84849902012-12-02 00:28:11 +01004042 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004043 blob = drm_property_blob_find(dev, out_resp->blob_id);
4044 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004045 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004046 goto done;
4047 }
Dave Airlief453ba02008-11-07 14:05:41 -08004048
4049 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004050 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004051 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004052 ret = -EFAULT;
4053 goto done;
4054 }
4055 }
4056 out_resp->length = blob->length;
4057
4058done:
Daniel Vetter84849902012-12-02 00:28:11 +01004059 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004060 return ret;
4061}
4062
Dave Airliecc7096f2014-10-22 12:03:04 +10004063/**
4064 * drm_mode_connector_set_path_property - set tile property on connector
4065 * @connector: connector to set property on.
4066 * @path: path to use for property.
4067 *
4068 * This creates a property to expose to userspace to specify a
4069 * connector path. This is mainly used for DisplayPort MST where
4070 * connectors have a topology and we want to allow userspace to give
4071 * them more meaningful names.
4072 *
4073 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004074 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004075 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004076int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004077 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004078{
4079 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004080 size_t size = strlen(path) + 1;
4081 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004082
4083 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4084 size, path);
4085 if (!connector->path_blob_ptr)
4086 return -EINVAL;
4087
4088 ret = drm_object_property_set_value(&connector->base,
4089 dev->mode_config.path_property,
4090 connector->path_blob_ptr->base.id);
4091 return ret;
4092}
4093EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4094
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004095/**
4096 * drm_mode_connector_update_edid_property - update the edid property of a connector
4097 * @connector: drm connector
4098 * @edid: new value of the edid property
4099 *
4100 * This function creates a new blob modeset object and assigns its id to the
4101 * connector's edid property.
4102 *
4103 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004104 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004105 */
Dave Airlief453ba02008-11-07 14:05:41 -08004106int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004107 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004108{
4109 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004110 size_t size;
4111 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004112
Thomas Wood4cf2b282014-06-18 17:52:33 +01004113 /* ignore requests to set edid when overridden */
4114 if (connector->override_edid)
4115 return 0;
4116
Dave Airlief453ba02008-11-07 14:05:41 -08004117 if (connector->edid_blob_ptr)
4118 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4119
4120 /* Delete edid, when there is none. */
4121 if (!edid) {
4122 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05004123 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08004124 return ret;
4125 }
4126
Adam Jackson7466f4c2010-03-29 21:43:23 +00004127 size = EDID_LENGTH * (1 + edid->extensions);
4128 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4129 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00004130 if (!connector->edid_blob_ptr)
4131 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08004132
Rob Clark58495562012-10-11 20:50:56 -05004133 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08004134 dev->mode_config.edid_property,
4135 connector->edid_blob_ptr->base.id);
4136
4137 return ret;
4138}
4139EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4140
Paulo Zanoni26a34812012-05-15 18:08:59 -03004141static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004142 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004143{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004144 int i;
4145
Paulo Zanoni26a34812012-05-15 18:08:59 -03004146 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4147 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004148
4149 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004150 if (value < property->values[0] || value > property->values[1])
4151 return false;
4152 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004153 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4154 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004155
Rob Clarkebc44cf2012-09-12 22:22:31 -05004156 if (svalue < U642I64(property->values[0]) ||
4157 svalue > U642I64(property->values[1]))
4158 return false;
4159 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004160 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004161 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004162
Rob Clark49e27542012-05-17 02:23:26 -06004163 for (i = 0; i < property->num_values; i++)
4164 valid_mask |= (1ULL << property->values[i]);
4165 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004166 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004167 /* Only the driver knows */
4168 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004169 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4170 struct drm_mode_object *obj;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004171
Rob Clark98f75de2014-05-30 11:37:03 -04004172 /* a zero value for an object property translates to null: */
4173 if (value == 0)
4174 return true;
4175 /*
4176 * NOTE: use _object_find() directly to bypass restriction on
4177 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4178 * object this could race against object finalization, so it
4179 * simply tells us that the object *was* valid. Which is good
4180 * enough.
4181 */
4182 obj = _object_find(property->dev, value, property->values[0]);
4183 return obj != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004184 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004185
4186 for (i = 0; i < property->num_values; i++)
4187 if (property->values[i] == value)
4188 return true;
4189 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004190}
4191
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004192/**
4193 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4194 * @dev: DRM device
4195 * @data: ioctl data
4196 * @file_priv: DRM file info
4197 *
4198 * This function sets the current value for a connectors's property. It also
4199 * calls into a driver's ->set_property callback to update the hardware state
4200 *
4201 * Called by the user via ioctl.
4202 *
4203 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004204 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004205 */
Dave Airlief453ba02008-11-07 14:05:41 -08004206int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4207 void *data, struct drm_file *file_priv)
4208{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004209 struct drm_mode_connector_set_property *conn_set_prop = data;
4210 struct drm_mode_obj_set_property obj_set_prop = {
4211 .value = conn_set_prop->value,
4212 .prop_id = conn_set_prop->prop_id,
4213 .obj_id = conn_set_prop->connector_id,
4214 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4215 };
Dave Airlief453ba02008-11-07 14:05:41 -08004216
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004217 /* It does all the locking and checking we need */
4218 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004219}
4220
Paulo Zanonic5431882012-05-15 18:09:02 -03004221static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4222 struct drm_property *property,
4223 uint64_t value)
4224{
4225 int ret = -EINVAL;
4226 struct drm_connector *connector = obj_to_connector(obj);
4227
4228 /* Do DPMS ourselves */
4229 if (property == connector->dev->mode_config.dpms_property) {
4230 if (connector->funcs->dpms)
4231 (*connector->funcs->dpms)(connector, (int)value);
4232 ret = 0;
4233 } else if (connector->funcs->set_property)
4234 ret = connector->funcs->set_property(connector, property, value);
4235
4236 /* store the property value if successful */
4237 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004238 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004239 return ret;
4240}
4241
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004242static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4243 struct drm_property *property,
4244 uint64_t value)
4245{
4246 int ret = -EINVAL;
4247 struct drm_crtc *crtc = obj_to_crtc(obj);
4248
4249 if (crtc->funcs->set_property)
4250 ret = crtc->funcs->set_property(crtc, property, value);
4251 if (!ret)
4252 drm_object_property_set_value(obj, property, value);
4253
4254 return ret;
4255}
4256
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004257/**
4258 * drm_mode_plane_set_obj_prop - set the value of a property
4259 * @plane: drm plane object to set property value for
4260 * @property: property to set
4261 * @value: value the property should be set to
4262 *
4263 * This functions sets a given property on a given plane object. This function
4264 * calls the driver's ->set_property callback and changes the software state of
4265 * the property if the callback succeeds.
4266 *
4267 * Returns:
4268 * Zero on success, error code on failure.
4269 */
4270int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4271 struct drm_property *property,
4272 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004273{
4274 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004275 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004276
4277 if (plane->funcs->set_property)
4278 ret = plane->funcs->set_property(plane, property, value);
4279 if (!ret)
4280 drm_object_property_set_value(obj, property, value);
4281
4282 return ret;
4283}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004284EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004285
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004286/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004287 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004288 * @dev: DRM device
4289 * @data: ioctl data
4290 * @file_priv: DRM file info
4291 *
4292 * This function retrieves the current value for an object's property. Compared
4293 * to the connector specific ioctl this one is extended to also work on crtc and
4294 * plane objects.
4295 *
4296 * Called by the user via ioctl.
4297 *
4298 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004299 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004300 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004301int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4302 struct drm_file *file_priv)
4303{
4304 struct drm_mode_obj_get_properties *arg = data;
4305 struct drm_mode_object *obj;
4306 int ret = 0;
4307 int i;
4308 int copied = 0;
4309 int props_count = 0;
4310 uint32_t __user *props_ptr;
4311 uint64_t __user *prop_values_ptr;
4312
4313 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4314 return -EINVAL;
4315
Daniel Vetter84849902012-12-02 00:28:11 +01004316 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004317
4318 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4319 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004320 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004321 goto out;
4322 }
4323 if (!obj->properties) {
4324 ret = -EINVAL;
4325 goto out;
4326 }
4327
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004328 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004329
4330 /* This ioctl is called twice, once to determine how much space is
4331 * needed, and the 2nd time to fill it. */
4332 if ((arg->count_props >= props_count) && props_count) {
4333 copied = 0;
4334 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4335 prop_values_ptr = (uint64_t __user *)(unsigned long)
4336 (arg->prop_values_ptr);
4337 for (i = 0; i < props_count; i++) {
4338 if (put_user(obj->properties->ids[i],
4339 props_ptr + copied)) {
4340 ret = -EFAULT;
4341 goto out;
4342 }
4343 if (put_user(obj->properties->values[i],
4344 prop_values_ptr + copied)) {
4345 ret = -EFAULT;
4346 goto out;
4347 }
4348 copied++;
4349 }
4350 }
4351 arg->count_props = props_count;
4352out:
Daniel Vetter84849902012-12-02 00:28:11 +01004353 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004354 return ret;
4355}
4356
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004357/**
4358 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4359 * @dev: DRM device
4360 * @data: ioctl data
4361 * @file_priv: DRM file info
4362 *
4363 * This function sets the current value for an object's property. It also calls
4364 * into a driver's ->set_property callback to update the hardware state.
4365 * Compared to the connector specific ioctl this one is extended to also work on
4366 * crtc and plane objects.
4367 *
4368 * Called by the user via ioctl.
4369 *
4370 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004371 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004372 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004373int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4374 struct drm_file *file_priv)
4375{
4376 struct drm_mode_obj_set_property *arg = data;
4377 struct drm_mode_object *arg_obj;
4378 struct drm_mode_object *prop_obj;
4379 struct drm_property *property;
4380 int ret = -EINVAL;
4381 int i;
4382
4383 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4384 return -EINVAL;
4385
Daniel Vetter84849902012-12-02 00:28:11 +01004386 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004387
4388 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004389 if (!arg_obj) {
4390 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004391 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004392 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004393 if (!arg_obj->properties)
4394 goto out;
4395
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004396 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03004397 if (arg_obj->properties->ids[i] == arg->prop_id)
4398 break;
4399
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004400 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004401 goto out;
4402
4403 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4404 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004405 if (!prop_obj) {
4406 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004407 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004408 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004409 property = obj_to_property(prop_obj);
4410
4411 if (!drm_property_change_is_valid(property, arg->value))
4412 goto out;
4413
4414 switch (arg_obj->type) {
4415 case DRM_MODE_OBJECT_CONNECTOR:
4416 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4417 arg->value);
4418 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004419 case DRM_MODE_OBJECT_CRTC:
4420 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4421 break;
Rob Clark4d939142012-05-17 02:23:27 -06004422 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004423 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4424 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06004425 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004426 }
4427
4428out:
Daniel Vetter84849902012-12-02 00:28:11 +01004429 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004430 return ret;
4431}
4432
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004433/**
4434 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4435 * @connector: connector to attach
4436 * @encoder: encoder to attach @connector to
4437 *
4438 * This function links up a connector to an encoder. Note that the routing
4439 * restrictions between encoders and crtcs are exposed to userspace through the
4440 * possible_clones and possible_crtcs bitmasks.
4441 *
4442 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004443 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004444 */
Dave Airlief453ba02008-11-07 14:05:41 -08004445int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4446 struct drm_encoder *encoder)
4447{
4448 int i;
4449
4450 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4451 if (connector->encoder_ids[i] == 0) {
4452 connector->encoder_ids[i] = encoder->base.id;
4453 return 0;
4454 }
4455 }
4456 return -ENOMEM;
4457}
4458EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4459
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004460/**
4461 * drm_mode_crtc_set_gamma_size - set the gamma table size
4462 * @crtc: CRTC to set the gamma table size for
4463 * @gamma_size: size of the gamma table
4464 *
4465 * Drivers which support gamma tables should set this to the supported gamma
4466 * table size when initializing the CRTC. Currently the drm core only supports a
4467 * fixed gamma table size.
4468 *
4469 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004470 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004471 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004472int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004473 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004474{
4475 crtc->gamma_size = gamma_size;
4476
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01004477 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4478 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08004479 if (!crtc->gamma_store) {
4480 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004481 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004482 }
4483
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004484 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004485}
4486EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4487
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004488/**
4489 * drm_mode_gamma_set_ioctl - set the gamma table
4490 * @dev: DRM device
4491 * @data: ioctl data
4492 * @file_priv: DRM file info
4493 *
4494 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4495 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4496 *
4497 * Called by the user via ioctl.
4498 *
4499 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004500 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004501 */
Dave Airlief453ba02008-11-07 14:05:41 -08004502int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4503 void *data, struct drm_file *file_priv)
4504{
4505 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004506 struct drm_crtc *crtc;
4507 void *r_base, *g_base, *b_base;
4508 int size;
4509 int ret = 0;
4510
Dave Airliefb3b06c2011-02-08 13:55:21 +10004511 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4512 return -EINVAL;
4513
Daniel Vetter84849902012-12-02 00:28:11 +01004514 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004515 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4516 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004517 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004518 goto out;
4519 }
Dave Airlief453ba02008-11-07 14:05:41 -08004520
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004521 if (crtc->funcs->gamma_set == NULL) {
4522 ret = -ENOSYS;
4523 goto out;
4524 }
4525
Dave Airlief453ba02008-11-07 14:05:41 -08004526 /* memcpy into gamma store */
4527 if (crtc_lut->gamma_size != crtc->gamma_size) {
4528 ret = -EINVAL;
4529 goto out;
4530 }
4531
4532 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4533 r_base = crtc->gamma_store;
4534 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4535 ret = -EFAULT;
4536 goto out;
4537 }
4538
4539 g_base = r_base + size;
4540 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4541 ret = -EFAULT;
4542 goto out;
4543 }
4544
4545 b_base = g_base + size;
4546 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4547 ret = -EFAULT;
4548 goto out;
4549 }
4550
James Simmons72034252010-08-03 01:33:19 +01004551 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004552
4553out:
Daniel Vetter84849902012-12-02 00:28:11 +01004554 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004555 return ret;
4556
4557}
4558
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004559/**
4560 * drm_mode_gamma_get_ioctl - get the gamma table
4561 * @dev: DRM device
4562 * @data: ioctl data
4563 * @file_priv: DRM file info
4564 *
4565 * Copy the current gamma table into the storage provided. This also provides
4566 * the gamma table size the driver expects, which can be used to size the
4567 * allocated storage.
4568 *
4569 * Called by the user via ioctl.
4570 *
4571 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004572 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004573 */
Dave Airlief453ba02008-11-07 14:05:41 -08004574int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4575 void *data, struct drm_file *file_priv)
4576{
4577 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004578 struct drm_crtc *crtc;
4579 void *r_base, *g_base, *b_base;
4580 int size;
4581 int ret = 0;
4582
Dave Airliefb3b06c2011-02-08 13:55:21 +10004583 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4584 return -EINVAL;
4585
Daniel Vetter84849902012-12-02 00:28:11 +01004586 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004587 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4588 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004589 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004590 goto out;
4591 }
Dave Airlief453ba02008-11-07 14:05:41 -08004592
4593 /* memcpy into gamma store */
4594 if (crtc_lut->gamma_size != crtc->gamma_size) {
4595 ret = -EINVAL;
4596 goto out;
4597 }
4598
4599 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4600 r_base = crtc->gamma_store;
4601 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4602 ret = -EFAULT;
4603 goto out;
4604 }
4605
4606 g_base = r_base + size;
4607 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4608 ret = -EFAULT;
4609 goto out;
4610 }
4611
4612 b_base = g_base + size;
4613 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4614 ret = -EFAULT;
4615 goto out;
4616 }
4617out:
Daniel Vetter84849902012-12-02 00:28:11 +01004618 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004619 return ret;
4620}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004621
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004622/**
4623 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4624 * @dev: DRM device
4625 * @data: ioctl data
4626 * @file_priv: DRM file info
4627 *
4628 * This schedules an asynchronous update on a given CRTC, called page flip.
4629 * Optionally a drm event is generated to signal the completion of the event.
4630 * Generic drivers cannot assume that a pageflip with changed framebuffer
4631 * properties (including driver specific metadata like tiling layout) will work,
4632 * but some drivers support e.g. pixel format changes through the pageflip
4633 * ioctl.
4634 *
4635 * Called by the user via ioctl.
4636 *
4637 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004638 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004639 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004640int drm_mode_page_flip_ioctl(struct drm_device *dev,
4641 void *data, struct drm_file *file_priv)
4642{
4643 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004644 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02004645 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004646 struct drm_pending_vblank_event *e = NULL;
4647 unsigned long flags;
4648 int ret = -EINVAL;
4649
4650 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4651 page_flip->reserved != 0)
4652 return -EINVAL;
4653
Keith Packard62f21042013-07-22 18:50:00 -07004654 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4655 return -EINVAL;
4656
Rob Clarka2b34e22013-10-05 16:36:52 -04004657 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4658 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004659 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004660
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01004661 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07004662 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004663 /* The framebuffer is currently unbound, presumably
4664 * due to a hotplug event, that userspace has not
4665 * yet discovered.
4666 */
4667 ret = -EBUSY;
4668 goto out;
4669 }
4670
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004671 if (crtc->funcs->page_flip == NULL)
4672 goto out;
4673
Daniel Vetter786b99e2012-12-02 21:53:40 +01004674 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004675 if (!fb) {
4676 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004677 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004678 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004679
Damien Lespiauc11e9282013-09-25 16:45:30 +01004680 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4681 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004682 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004683
Matt Roperf4510a22014-04-01 15:22:40 -07004684 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004685 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4686 ret = -EINVAL;
4687 goto out;
4688 }
4689
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004690 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4691 ret = -ENOMEM;
4692 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004693 if (file_priv->event_space < sizeof(e->event)) {
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004694 spin_unlock_irqrestore(&dev->event_lock, flags);
4695 goto out;
4696 }
Thierry Redingf76511b2014-12-10 13:03:40 +01004697 file_priv->event_space -= sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004698 spin_unlock_irqrestore(&dev->event_lock, flags);
4699
Thierry Redingf76511b2014-12-10 13:03:40 +01004700 e = kzalloc(sizeof(*e), GFP_KERNEL);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004701 if (e == NULL) {
4702 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004703 file_priv->event_space += sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004704 spin_unlock_irqrestore(&dev->event_lock, flags);
4705 goto out;
4706 }
4707
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004708 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01004709 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004710 e->event.user_data = page_flip->user_data;
4711 e->base.event = &e->event.base;
4712 e->base.file_priv = file_priv;
4713 e->base.destroy =
4714 (void (*) (struct drm_pending_event *)) kfree;
4715 }
4716
Daniel Vetter3d30a592014-07-27 13:42:42 +02004717 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004718 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004719 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004720 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4721 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004722 file_priv->event_space += sizeof(e->event);
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004723 spin_unlock_irqrestore(&dev->event_lock, flags);
4724 kfree(e);
4725 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004726 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02004727 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004728 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004729 /*
4730 * Warn if the driver hasn't properly updated the crtc->fb
4731 * field to reflect that the new framebuffer is now used.
4732 * Failing to do so will screw with the reference counting
4733 * on framebuffers.
4734 */
Matt Roperf4510a22014-04-01 15:22:40 -07004735 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004736 /* Unref only the old framebuffer. */
4737 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004738 }
4739
4740out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004741 if (fb)
4742 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02004743 if (crtc->primary->old_fb)
4744 drm_framebuffer_unreference(crtc->primary->old_fb);
4745 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02004746 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004747
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004748 return ret;
4749}
Chris Wilsoneb033552011-01-24 15:11:08 +00004750
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004751/**
4752 * drm_mode_config_reset - call ->reset callbacks
4753 * @dev: drm device
4754 *
4755 * This functions calls all the crtc's, encoder's and connector's ->reset
4756 * callback. Drivers can use this in e.g. their driver load or resume code to
4757 * reset hardware and software state.
4758 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004759void drm_mode_config_reset(struct drm_device *dev)
4760{
4761 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004762 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00004763 struct drm_encoder *encoder;
4764 struct drm_connector *connector;
4765
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004766 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4767 if (plane->funcs->reset)
4768 plane->funcs->reset(plane);
4769
Chris Wilsoneb033552011-01-24 15:11:08 +00004770 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4771 if (crtc->funcs->reset)
4772 crtc->funcs->reset(crtc);
4773
4774 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4775 if (encoder->funcs->reset)
4776 encoder->funcs->reset(encoder);
4777
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004778 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4779 connector->status = connector_status_unknown;
4780
Chris Wilsoneb033552011-01-24 15:11:08 +00004781 if (connector->funcs->reset)
4782 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004783 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004784}
4785EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004786
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004787/**
4788 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4789 * @dev: DRM device
4790 * @data: ioctl data
4791 * @file_priv: DRM file info
4792 *
4793 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4794 * TTM or something else entirely) and returns the resulting buffer handle. This
4795 * handle can then be wrapped up into a framebuffer modeset object.
4796 *
4797 * Note that userspace is not allowed to use such objects for render
4798 * acceleration - drivers must create their own private ioctls for such a use
4799 * case.
4800 *
4801 * Called by the user via ioctl.
4802 *
4803 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004804 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004805 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004806int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4807 void *data, struct drm_file *file_priv)
4808{
4809 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004810 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004811
4812 if (!dev->driver->dumb_create)
4813 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004814 if (!args->width || !args->height || !args->bpp)
4815 return -EINVAL;
4816
4817 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02004818 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01004819 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02004820 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01004821 return -EINVAL;
4822 stride = cpp * args->width;
4823 if (args->height > 0xffffffffU / stride)
4824 return -EINVAL;
4825
4826 /* test for wrap-around */
4827 size = args->height * stride;
4828 if (PAGE_ALIGN(size) == 0)
4829 return -EINVAL;
4830
Thierry Redingf6085952014-11-03 11:14:14 +01004831 /*
4832 * handle, pitch and size are output parameters. Zero them out to
4833 * prevent drivers from accidentally using uninitialized data. Since
4834 * not all existing userspace is clearing these fields properly we
4835 * cannot reject IOCTL with garbage in them.
4836 */
4837 args->handle = 0;
4838 args->pitch = 0;
4839 args->size = 0;
4840
Dave Airlieff72145b2011-02-07 12:16:14 +10004841 return dev->driver->dumb_create(file_priv, dev, args);
4842}
4843
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004844/**
4845 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4846 * @dev: DRM device
4847 * @data: ioctl data
4848 * @file_priv: DRM file info
4849 *
4850 * Allocate an offset in the drm device node's address space to be able to
4851 * memory map a dumb buffer.
4852 *
4853 * Called by the user via ioctl.
4854 *
4855 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004856 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004857 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004858int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4859 void *data, struct drm_file *file_priv)
4860{
4861 struct drm_mode_map_dumb *args = data;
4862
4863 /* call driver ioctl to get mmap offset */
4864 if (!dev->driver->dumb_map_offset)
4865 return -ENOSYS;
4866
4867 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4868}
4869
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004870/**
4871 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4872 * @dev: DRM device
4873 * @data: ioctl data
4874 * @file_priv: DRM file info
4875 *
4876 * This destroys the userspace handle for the given dumb backing storage buffer.
4877 * Since buffer objects must be reference counted in the kernel a buffer object
4878 * won't be immediately freed if a framebuffer modeset object still uses it.
4879 *
4880 * Called by the user via ioctl.
4881 *
4882 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004883 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004884 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004885int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4886 void *data, struct drm_file *file_priv)
4887{
4888 struct drm_mode_destroy_dumb *args = data;
4889
4890 if (!dev->driver->dumb_destroy)
4891 return -ENOSYS;
4892
4893 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4894}
Dave Airlie248dbc22011-11-29 20:02:54 +00004895
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004896/**
4897 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4898 * @format: pixel format (DRM_FORMAT_*)
4899 * @depth: storage for the depth value
4900 * @bpp: storage for the bpp value
4901 *
4902 * This only supports RGB formats here for compat with code that doesn't use
4903 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004904 */
4905void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4906 int *bpp)
4907{
4908 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004909 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004910 case DRM_FORMAT_RGB332:
4911 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00004912 *depth = 8;
4913 *bpp = 8;
4914 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004915 case DRM_FORMAT_XRGB1555:
4916 case DRM_FORMAT_XBGR1555:
4917 case DRM_FORMAT_RGBX5551:
4918 case DRM_FORMAT_BGRX5551:
4919 case DRM_FORMAT_ARGB1555:
4920 case DRM_FORMAT_ABGR1555:
4921 case DRM_FORMAT_RGBA5551:
4922 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00004923 *depth = 15;
4924 *bpp = 16;
4925 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004926 case DRM_FORMAT_RGB565:
4927 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00004928 *depth = 16;
4929 *bpp = 16;
4930 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004931 case DRM_FORMAT_RGB888:
4932 case DRM_FORMAT_BGR888:
4933 *depth = 24;
4934 *bpp = 24;
4935 break;
4936 case DRM_FORMAT_XRGB8888:
4937 case DRM_FORMAT_XBGR8888:
4938 case DRM_FORMAT_RGBX8888:
4939 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004940 *depth = 24;
4941 *bpp = 32;
4942 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004943 case DRM_FORMAT_XRGB2101010:
4944 case DRM_FORMAT_XBGR2101010:
4945 case DRM_FORMAT_RGBX1010102:
4946 case DRM_FORMAT_BGRX1010102:
4947 case DRM_FORMAT_ARGB2101010:
4948 case DRM_FORMAT_ABGR2101010:
4949 case DRM_FORMAT_RGBA1010102:
4950 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00004951 *depth = 30;
4952 *bpp = 32;
4953 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02004954 case DRM_FORMAT_ARGB8888:
4955 case DRM_FORMAT_ABGR8888:
4956 case DRM_FORMAT_RGBA8888:
4957 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00004958 *depth = 32;
4959 *bpp = 32;
4960 break;
4961 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03004962 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4963 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00004964 *depth = 0;
4965 *bpp = 0;
4966 break;
4967 }
4968}
4969EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03004970
4971/**
4972 * drm_format_num_planes - get the number of planes for format
4973 * @format: pixel format (DRM_FORMAT_*)
4974 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004975 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004976 * The number of planes used by the specified pixel format.
4977 */
4978int drm_format_num_planes(uint32_t format)
4979{
4980 switch (format) {
4981 case DRM_FORMAT_YUV410:
4982 case DRM_FORMAT_YVU410:
4983 case DRM_FORMAT_YUV411:
4984 case DRM_FORMAT_YVU411:
4985 case DRM_FORMAT_YUV420:
4986 case DRM_FORMAT_YVU420:
4987 case DRM_FORMAT_YUV422:
4988 case DRM_FORMAT_YVU422:
4989 case DRM_FORMAT_YUV444:
4990 case DRM_FORMAT_YVU444:
4991 return 3;
4992 case DRM_FORMAT_NV12:
4993 case DRM_FORMAT_NV21:
4994 case DRM_FORMAT_NV16:
4995 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02004996 case DRM_FORMAT_NV24:
4997 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03004998 return 2;
4999 default:
5000 return 1;
5001 }
5002}
5003EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005004
5005/**
5006 * drm_format_plane_cpp - determine the bytes per pixel value
5007 * @format: pixel format (DRM_FORMAT_*)
5008 * @plane: plane index
5009 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005010 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005011 * The bytes per pixel value for the specified plane.
5012 */
5013int drm_format_plane_cpp(uint32_t format, int plane)
5014{
5015 unsigned int depth;
5016 int bpp;
5017
5018 if (plane >= drm_format_num_planes(format))
5019 return 0;
5020
5021 switch (format) {
5022 case DRM_FORMAT_YUYV:
5023 case DRM_FORMAT_YVYU:
5024 case DRM_FORMAT_UYVY:
5025 case DRM_FORMAT_VYUY:
5026 return 2;
5027 case DRM_FORMAT_NV12:
5028 case DRM_FORMAT_NV21:
5029 case DRM_FORMAT_NV16:
5030 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005031 case DRM_FORMAT_NV24:
5032 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005033 return plane ? 2 : 1;
5034 case DRM_FORMAT_YUV410:
5035 case DRM_FORMAT_YVU410:
5036 case DRM_FORMAT_YUV411:
5037 case DRM_FORMAT_YVU411:
5038 case DRM_FORMAT_YUV420:
5039 case DRM_FORMAT_YVU420:
5040 case DRM_FORMAT_YUV422:
5041 case DRM_FORMAT_YVU422:
5042 case DRM_FORMAT_YUV444:
5043 case DRM_FORMAT_YVU444:
5044 return 1;
5045 default:
5046 drm_fb_get_bpp_depth(format, &depth, &bpp);
5047 return bpp >> 3;
5048 }
5049}
5050EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005051
5052/**
5053 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5054 * @format: pixel format (DRM_FORMAT_*)
5055 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005056 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005057 * The horizontal chroma subsampling factor for the
5058 * specified pixel format.
5059 */
5060int drm_format_horz_chroma_subsampling(uint32_t format)
5061{
5062 switch (format) {
5063 case DRM_FORMAT_YUV411:
5064 case DRM_FORMAT_YVU411:
5065 case DRM_FORMAT_YUV410:
5066 case DRM_FORMAT_YVU410:
5067 return 4;
5068 case DRM_FORMAT_YUYV:
5069 case DRM_FORMAT_YVYU:
5070 case DRM_FORMAT_UYVY:
5071 case DRM_FORMAT_VYUY:
5072 case DRM_FORMAT_NV12:
5073 case DRM_FORMAT_NV21:
5074 case DRM_FORMAT_NV16:
5075 case DRM_FORMAT_NV61:
5076 case DRM_FORMAT_YUV422:
5077 case DRM_FORMAT_YVU422:
5078 case DRM_FORMAT_YUV420:
5079 case DRM_FORMAT_YVU420:
5080 return 2;
5081 default:
5082 return 1;
5083 }
5084}
5085EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5086
5087/**
5088 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5089 * @format: pixel format (DRM_FORMAT_*)
5090 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005091 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005092 * The vertical chroma subsampling factor for the
5093 * specified pixel format.
5094 */
5095int drm_format_vert_chroma_subsampling(uint32_t format)
5096{
5097 switch (format) {
5098 case DRM_FORMAT_YUV410:
5099 case DRM_FORMAT_YVU410:
5100 return 4;
5101 case DRM_FORMAT_YUV420:
5102 case DRM_FORMAT_YVU420:
5103 case DRM_FORMAT_NV12:
5104 case DRM_FORMAT_NV21:
5105 return 2;
5106 default:
5107 return 1;
5108 }
5109}
5110EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005111
5112/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305113 * drm_rotation_simplify() - Try to simplify the rotation
5114 * @rotation: Rotation to be simplified
5115 * @supported_rotations: Supported rotations
5116 *
5117 * Attempt to simplify the rotation to a form that is supported.
5118 * Eg. if the hardware supports everything except DRM_REFLECT_X
5119 * one could call this function like this:
5120 *
5121 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5122 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5123 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5124 *
5125 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5126 * transforms the hardware supports, this function may not
5127 * be able to produce a supported transform, so the caller should
5128 * check the result afterwards.
5129 */
5130unsigned int drm_rotation_simplify(unsigned int rotation,
5131 unsigned int supported_rotations)
5132{
5133 if (rotation & ~supported_rotations) {
5134 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5135 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5136 }
5137
5138 return rotation;
5139}
5140EXPORT_SYMBOL(drm_rotation_simplify);
5141
5142/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005143 * drm_mode_config_init - initialize DRM mode_configuration structure
5144 * @dev: DRM device
5145 *
5146 * Initialize @dev's mode_config structure, used for tracking the graphics
5147 * configuration of @dev.
5148 *
5149 * Since this initializes the modeset locks, no locking is possible. Which is no
5150 * problem, since this should happen single threaded at init time. It is the
5151 * driver's problem to ensure this guarantee.
5152 *
5153 */
5154void drm_mode_config_init(struct drm_device *dev)
5155{
5156 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005157 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005158 mutex_init(&dev->mode_config.idr_mutex);
5159 mutex_init(&dev->mode_config.fb_lock);
5160 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5161 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5162 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04005163 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005164 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5165 INIT_LIST_HEAD(&dev->mode_config.property_list);
5166 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5167 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5168 idr_init(&dev->mode_config.crtc_idr);
5169
5170 drm_modeset_lock_all(dev);
5171 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04005172 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005173 drm_modeset_unlock_all(dev);
5174
5175 /* Just to be sure */
5176 dev->mode_config.num_fb = 0;
5177 dev->mode_config.num_connector = 0;
5178 dev->mode_config.num_crtc = 0;
5179 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005180 dev->mode_config.num_overlay_plane = 0;
5181 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005182}
5183EXPORT_SYMBOL(drm_mode_config_init);
5184
5185/**
5186 * drm_mode_config_cleanup - free up DRM mode_config info
5187 * @dev: DRM device
5188 *
5189 * Free up all the connectors and CRTCs associated with this DRM device, then
5190 * free up the framebuffers and associated buffer objects.
5191 *
5192 * Note that since this /should/ happen single-threaded at driver/device
5193 * teardown time, no locking is required. It's the driver's job to ensure that
5194 * this guarantee actually holds true.
5195 *
5196 * FIXME: cleanup any dangling user buffer objects too
5197 */
5198void drm_mode_config_cleanup(struct drm_device *dev)
5199{
5200 struct drm_connector *connector, *ot;
5201 struct drm_crtc *crtc, *ct;
5202 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04005203 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005204 struct drm_framebuffer *fb, *fbt;
5205 struct drm_property *property, *pt;
5206 struct drm_property_blob *blob, *bt;
5207 struct drm_plane *plane, *plt;
5208
5209 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5210 head) {
5211 encoder->funcs->destroy(encoder);
5212 }
5213
Sean Paul3b336ec2013-08-14 16:47:37 -04005214 list_for_each_entry_safe(bridge, brt,
5215 &dev->mode_config.bridge_list, head) {
5216 bridge->funcs->destroy(bridge);
5217 }
5218
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005219 list_for_each_entry_safe(connector, ot,
5220 &dev->mode_config.connector_list, head) {
5221 connector->funcs->destroy(connector);
5222 }
5223
5224 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5225 head) {
5226 drm_property_destroy(dev, property);
5227 }
5228
5229 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5230 head) {
5231 drm_property_destroy_blob(dev, blob);
5232 }
5233
5234 /*
5235 * Single-threaded teardown context, so it's not required to grab the
5236 * fb_lock to protect against concurrent fb_list access. Contrary, it
5237 * would actually deadlock with the drm_framebuffer_cleanup function.
5238 *
5239 * Also, if there are any framebuffers left, that's a driver leak now,
5240 * so politely WARN about this.
5241 */
5242 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5243 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5244 drm_framebuffer_remove(fb);
5245 }
5246
5247 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5248 head) {
5249 plane->funcs->destroy(plane);
5250 }
5251
5252 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5253 crtc->funcs->destroy(crtc);
5254 }
5255
5256 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005257 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005258}
5259EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305260
5261struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5262 unsigned int supported_rotations)
5263{
5264 static const struct drm_prop_enum_list props[] = {
5265 { DRM_ROTATE_0, "rotate-0" },
5266 { DRM_ROTATE_90, "rotate-90" },
5267 { DRM_ROTATE_180, "rotate-180" },
5268 { DRM_ROTATE_270, "rotate-270" },
5269 { DRM_REFLECT_X, "reflect-x" },
5270 { DRM_REFLECT_Y, "reflect-y" },
5271 };
5272
5273 return drm_property_create_bitmask(dev, 0, "rotation",
5274 props, ARRAY_SIZE(props),
5275 supported_rotations);
5276}
5277EXPORT_SYMBOL(drm_mode_create_rotation_property);