blob: 3bac877228fac34be1c76804085ffaff9849c535 [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>
Rob Clark88a48e22014-12-18 16:01:50 -050041#include <drm/drm_atomic.h>
Dave Airlief453ba02008-11-07 14:05:41 -080042
Daniel Vetter8bd441b2014-01-23 15:35:24 +010043#include "drm_crtc_internal.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020044#include "drm_internal.h"
Daniel Vetter8bd441b2014-01-23 15:35:24 +010045
Matt Roperc394c2b2014-06-10 08:28:08 -070046static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
47 struct drm_mode_fb_cmd2 *r,
48 struct drm_file *file_priv);
49
Dave Airlief453ba02008-11-07 14:05:41 -080050/* Avoid boilerplate. I'm tired of typing. */
51#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000052 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080053 { \
54 int i; \
55 for (i = 0; i < ARRAY_SIZE(list); i++) { \
56 if (list[i].type == val) \
57 return list[i].name; \
58 } \
59 return "(unknown)"; \
60 }
61
62/*
63 * Global properties
64 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010065static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
66 { DRM_MODE_DPMS_ON, "On" },
Dave Airlief453ba02008-11-07 14:05:41 -080067 { DRM_MODE_DPMS_STANDBY, "Standby" },
68 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
69 { DRM_MODE_DPMS_OFF, "Off" }
70};
71
72DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
73
Thierry Reding4dfd9092014-12-10 13:03:34 +010074static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
Rob Clark9922ab52014-04-01 20:16:57 -040075 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
76 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
77 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
78};
79
Dave Airlief453ba02008-11-07 14:05:41 -080080/*
81 * Optional properties
82 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010083static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
Jesse Barnes53bd8382009-07-01 10:04:40 -070084 { DRM_MODE_SCALE_NONE, "None" },
85 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
86 { DRM_MODE_SCALE_CENTER, "Center" },
87 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080088};
89
Vandana Kannanff587e42014-06-11 10:46:48 +053090static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
91 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
92 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
93 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
94};
95
Dave Airlief453ba02008-11-07 14:05:41 -080096/*
97 * Non-global properties, but "required" for certain connectors.
98 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010099static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800100 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
101 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
102 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
103};
104
105DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
106
Thierry Reding4dfd9092014-12-10 13:03:34 +0100107static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800108 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
109 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
110 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
111};
112
113DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
114 drm_dvi_i_subconnector_enum_list)
115
Thierry Reding4dfd9092014-12-10 13:03:34 +0100116static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800117 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200121 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800122};
123
124DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
125
Thierry Reding4dfd9092014-12-10 13:03:34 +0100126static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800127 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
128 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
129 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200131 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800132};
133
134DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
135 drm_tv_subconnector_enum_list)
136
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000137static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000138 { DRM_MODE_DIRTY_OFF, "Off" },
139 { DRM_MODE_DIRTY_ON, "On" },
140 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
141};
142
Dave Airlief453ba02008-11-07 14:05:41 -0800143struct drm_conn_prop_enum_list {
144 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000145 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400146 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800147};
148
149/*
150 * Connector and encoder types.
151 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100152static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
153 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400154 { DRM_MODE_CONNECTOR_VGA, "VGA" },
155 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
156 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
157 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
158 { DRM_MODE_CONNECTOR_Composite, "Composite" },
159 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
160 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
161 { DRM_MODE_CONNECTOR_Component, "Component" },
162 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
163 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
164 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
165 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
166 { DRM_MODE_CONNECTOR_TV, "TV" },
167 { DRM_MODE_CONNECTOR_eDP, "eDP" },
168 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300169 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800170};
171
Thierry Reding4dfd9092014-12-10 13:03:34 +0100172static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
173 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -0800174 { DRM_MODE_ENCODER_DAC, "DAC" },
175 { DRM_MODE_ENCODER_TMDS, "TMDS" },
176 { DRM_MODE_ENCODER_LVDS, "LVDS" },
177 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200178 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300179 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000180 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800181};
182
Thierry Reding4dfd9092014-12-10 13:03:34 +0100183static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
Jesse Barnesac1bb362014-02-10 15:32:44 -0800184 { SubPixelUnknown, "Unknown" },
185 { SubPixelHorizontalRGB, "Horizontal RGB" },
186 { SubPixelHorizontalBGR, "Horizontal BGR" },
187 { SubPixelVerticalRGB, "Vertical RGB" },
188 { SubPixelVerticalBGR, "Vertical BGR" },
189 { SubPixelNone, "None" },
190};
191
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400192void drm_connector_ida_init(void)
193{
194 int i;
195
196 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
197 ida_init(&drm_connector_enum_list[i].ida);
198}
199
200void drm_connector_ida_destroy(void)
201{
202 int i;
203
204 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
205 ida_destroy(&drm_connector_enum_list[i].ida);
206}
207
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100208/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100209 * drm_get_connector_status_name - return a string for connector status
210 * @status: connector status to compute name of
211 *
212 * In contrast to the other drm_get_*_name functions this one here returns a
213 * const pointer and hence is threadsafe.
214 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000215const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800216{
217 if (status == connector_status_connected)
218 return "connected";
219 else if (status == connector_status_disconnected)
220 return "disconnected";
221 else
222 return "unknown";
223}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000224EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800225
Jesse Barnesac1bb362014-02-10 15:32:44 -0800226/**
227 * drm_get_subpixel_order_name - return a string for a given subpixel enum
228 * @order: enum of subpixel_order
229 *
230 * Note you could abuse this and return something out of bounds, but that
231 * would be a caller error. No unscrubbed user data should make it here.
232 */
233const char *drm_get_subpixel_order_name(enum subpixel_order order)
234{
235 return drm_subpixel_enum_list[order].name;
236}
237EXPORT_SYMBOL(drm_get_subpixel_order_name);
238
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300239static char printable_char(int c)
240{
241 return isascii(c) && isprint(c) ? c : '?';
242}
243
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100244/**
245 * drm_get_format_name - return a string for drm fourcc format
246 * @format: format to compute name of
247 *
248 * Note that the buffer used by this function is globally shared and owned by
249 * the function itself.
250 *
251 * FIXME: This isn't really multithreading safe.
252 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000253const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300254{
255 static char buf[32];
256
257 snprintf(buf, sizeof(buf),
258 "%c%c%c%c %s-endian (0x%08x)",
259 printable_char(format & 0xff),
260 printable_char((format >> 8) & 0xff),
261 printable_char((format >> 16) & 0xff),
262 printable_char((format >> 24) & 0x7f),
263 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
264 format);
265
266 return buf;
267}
268EXPORT_SYMBOL(drm_get_format_name);
269
Dave Airlie2ee39452014-07-24 11:53:45 +1000270/*
271 * Internal function to assign a slot in the object idr and optionally
272 * register the object into the idr.
273 */
274static int drm_mode_object_get_reg(struct drm_device *dev,
275 struct drm_mode_object *obj,
276 uint32_t obj_type,
277 bool register_obj)
278{
279 int ret;
280
281 mutex_lock(&dev->mode_config.idr_mutex);
282 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
283 if (ret >= 0) {
284 /*
285 * Set up the object linking under the protection of the idr
286 * lock so that other users can't see inconsistent state.
287 */
288 obj->id = ret;
289 obj->type = obj_type;
290 }
291 mutex_unlock(&dev->mode_config.idr_mutex);
292
293 return ret < 0 ? ret : 0;
294}
295
Dave Airlief453ba02008-11-07 14:05:41 -0800296/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100297 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800298 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100299 * @obj: object pointer, used to generate unique ID
300 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800301 *
Dave Airlief453ba02008-11-07 14:05:41 -0800302 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100303 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
304 * modeset identifiers are _not_ reference counted. Hence don't use this for
305 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800306 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100307 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800308 * New unique (relative to other objects in @dev) integer identifier for the
309 * object.
310 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100311int drm_mode_object_get(struct drm_device *dev,
312 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800313{
Dave Airlie2ee39452014-07-24 11:53:45 +1000314 return drm_mode_object_get_reg(dev, obj, obj_type, true);
315}
Dave Airlief453ba02008-11-07 14:05:41 -0800316
Dave Airlie2ee39452014-07-24 11:53:45 +1000317static void drm_mode_object_register(struct drm_device *dev,
318 struct drm_mode_object *obj)
319{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000320 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000321 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000322 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800323}
324
325/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100326 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800327 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100328 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800329 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100330 * Free @id from @dev's unique identifier pool. Note that despite the _get
331 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
332 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800333 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100334void drm_mode_object_put(struct drm_device *dev,
335 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800336{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000337 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800338 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000339 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800340}
341
Rob Clark98f75de2014-05-30 11:37:03 -0400342static struct drm_mode_object *_object_find(struct drm_device *dev,
343 uint32_t id, uint32_t type)
344{
345 struct drm_mode_object *obj = NULL;
346
347 mutex_lock(&dev->mode_config.idr_mutex);
348 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200349 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
350 obj = NULL;
351 if (obj && obj->id != id)
352 obj = NULL;
353 /* don't leak out unref'd fb's */
354 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Rob Clark98f75de2014-05-30 11:37:03 -0400355 obj = NULL;
356 mutex_unlock(&dev->mode_config.idr_mutex);
357
358 return obj;
359}
360
Daniel Vetter786b99e2012-12-02 21:53:40 +0100361/**
362 * drm_mode_object_find - look up a drm object with static lifetime
363 * @dev: drm device
364 * @id: id of the mode object
365 * @type: type of the mode object
366 *
367 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400368 * are reference counted, they need special treatment. Even with
369 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
370 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100371 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200372struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
373 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800374{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000375 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800376
Daniel Vetter786b99e2012-12-02 21:53:40 +0100377 /* Framebuffers are reference counted and need their own lookup
378 * function.*/
379 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400380 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800381 return obj;
382}
383EXPORT_SYMBOL(drm_mode_object_find);
384
385/**
Dave Airlief453ba02008-11-07 14:05:41 -0800386 * drm_framebuffer_init - initialize a framebuffer
387 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100388 * @fb: framebuffer to be initialized
389 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800390 *
Dave Airlief453ba02008-11-07 14:05:41 -0800391 * Allocates an ID for the framebuffer's parent mode object, sets its mode
392 * functions & device file and adds it to the master fd list.
393 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100394 * IMPORTANT:
395 * This functions publishes the fb and makes it available for concurrent access
396 * by other users. Which means by this point the fb _must_ be fully set up -
397 * since all the fb attributes are invariant over its lifetime, no further
398 * locking but only correct reference counting is required.
399 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100400 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200401 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800402 */
403int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
404 const struct drm_framebuffer_funcs *funcs)
405{
406 int ret;
407
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100408 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000409 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100410 INIT_LIST_HEAD(&fb->filp_head);
411 fb->dev = dev;
412 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000413
Dave Airlief453ba02008-11-07 14:05:41 -0800414 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200415 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100416 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800417
Dave Airlief453ba02008-11-07 14:05:41 -0800418 dev->mode_config.num_fb++;
419 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100420out:
421 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800422
423 return 0;
424}
425EXPORT_SYMBOL(drm_framebuffer_init);
426
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200427/* dev->mode_config.fb_lock must be held! */
428static void __drm_framebuffer_unregister(struct drm_device *dev,
429 struct drm_framebuffer *fb)
430{
431 mutex_lock(&dev->mode_config.idr_mutex);
432 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
433 mutex_unlock(&dev->mode_config.idr_mutex);
434
435 fb->base.id = 0;
436}
437
Rob Clarkf7eff602012-09-05 21:48:38 +0000438static void drm_framebuffer_free(struct kref *kref)
439{
440 struct drm_framebuffer *fb =
441 container_of(kref, struct drm_framebuffer, refcount);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200442 struct drm_device *dev = fb->dev;
443
444 /*
445 * The lookup idr holds a weak reference, which has not necessarily been
446 * removed at this point. Check for that.
447 */
448 mutex_lock(&dev->mode_config.fb_lock);
449 if (fb->base.id) {
450 /* Mark fb as reaped and drop idr ref. */
451 __drm_framebuffer_unregister(dev, fb);
452 }
453 mutex_unlock(&dev->mode_config.fb_lock);
454
Rob Clarkf7eff602012-09-05 21:48:38 +0000455 fb->funcs->destroy(fb);
456}
457
Daniel Vetter2b677e82012-12-10 21:16:05 +0100458static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
459 uint32_t id)
460{
461 struct drm_mode_object *obj = NULL;
462 struct drm_framebuffer *fb;
463
464 mutex_lock(&dev->mode_config.idr_mutex);
465 obj = idr_find(&dev->mode_config.crtc_idr, id);
466 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
467 fb = NULL;
468 else
469 fb = obj_to_fb(obj);
470 mutex_unlock(&dev->mode_config.idr_mutex);
471
472 return fb;
473}
474
Rob Clarkf7eff602012-09-05 21:48:38 +0000475/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100476 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
477 * @dev: drm device
478 * @id: id of the fb object
479 *
480 * If successful, this grabs an additional reference to the framebuffer -
481 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100482 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100483 */
484struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
485 uint32_t id)
486{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100487 struct drm_framebuffer *fb;
488
489 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100490 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200491 if (fb) {
492 if (!kref_get_unless_zero(&fb->refcount))
493 fb = NULL;
494 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100495 mutex_unlock(&dev->mode_config.fb_lock);
496
497 return fb;
498}
499EXPORT_SYMBOL(drm_framebuffer_lookup);
500
501/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000502 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100503 * @fb: framebuffer to unref
504 *
505 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000506 */
507void drm_framebuffer_unreference(struct drm_framebuffer *fb)
508{
Rob Clark82912722014-03-18 10:07:08 -0400509 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000510 kref_put(&fb->refcount, drm_framebuffer_free);
511}
512EXPORT_SYMBOL(drm_framebuffer_unreference);
513
514/**
515 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100516 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100517 *
518 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000519 */
520void drm_framebuffer_reference(struct drm_framebuffer *fb)
521{
Rob Clark82912722014-03-18 10:07:08 -0400522 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000523 kref_get(&fb->refcount);
524}
525EXPORT_SYMBOL(drm_framebuffer_reference);
526
Daniel Vetter2b677e82012-12-10 21:16:05 +0100527static void drm_framebuffer_free_bug(struct kref *kref)
528{
529 BUG();
530}
531
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100532static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
533{
Rob Clark82912722014-03-18 10:07:08 -0400534 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100535 kref_put(&fb->refcount, drm_framebuffer_free_bug);
536}
537
Dave Airlief453ba02008-11-07 14:05:41 -0800538/**
Daniel Vetter36206362012-12-10 20:42:17 +0100539 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
540 * @fb: fb to unregister
541 *
542 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
543 * those used for fbdev. Note that the caller must hold a reference of it's own,
544 * i.e. the object may not be destroyed through this call (since it'll lead to a
545 * locking inversion).
546 */
547void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
548{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100549 struct drm_device *dev = fb->dev;
550
551 mutex_lock(&dev->mode_config.fb_lock);
552 /* Mark fb as reaped and drop idr ref. */
553 __drm_framebuffer_unregister(dev, fb);
554 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100555}
556EXPORT_SYMBOL(drm_framebuffer_unregister_private);
557
558/**
Dave Airlief453ba02008-11-07 14:05:41 -0800559 * drm_framebuffer_cleanup - remove a framebuffer object
560 * @fb: framebuffer to remove
561 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100562 * Cleanup framebuffer. This function is intended to be used from the drivers
563 * ->destroy callback. It can also be used to clean up driver private
564 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100565 *
566 * Note that this function does not remove the fb from active usuage - if it is
567 * still used anywhere, hilarity can ensue since userspace could call getfb on
568 * the id and get back -EINVAL. Obviously no concern at driver unload time.
569 *
570 * Also, the framebuffer will not be removed from the lookup idr - for
571 * user-created framebuffers this will happen in in the rmfb ioctl. For
572 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
573 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800574 */
575void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
576{
577 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100578
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100579 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000580 list_del(&fb->head);
581 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100582 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000583}
584EXPORT_SYMBOL(drm_framebuffer_cleanup);
585
586/**
587 * drm_framebuffer_remove - remove and unreference a framebuffer object
588 * @fb: framebuffer to remove
589 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000590 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100591 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100592 * passed-in framebuffer. Might take the modeset locks.
593 *
594 * Note that this function optimizes the cleanup away if the caller holds the
595 * last reference to the framebuffer. It is also guaranteed to not take the
596 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000597 */
598void drm_framebuffer_remove(struct drm_framebuffer *fb)
599{
600 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800601 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800602 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000603 struct drm_mode_set set;
604 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800605
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100606 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100607
Daniel Vetterb62584e2012-12-11 16:51:35 +0100608 /*
609 * drm ABI mandates that we remove any deleted framebuffers from active
610 * useage. But since most sane clients only remove framebuffers they no
611 * longer need, try to optimize this away.
612 *
613 * Since we're holding a reference ourselves, observing a refcount of 1
614 * means that we're the last holder and can skip it. Also, the refcount
615 * can never increase from 1 again, so we don't need any barriers or
616 * locks.
617 *
618 * Note that userspace could try to race with use and instate a new
619 * usage _after_ we've cleared all current ones. End result will be an
620 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
621 * in this manner.
622 */
623 if (atomic_read(&fb->refcount.refcount) > 1) {
624 drm_modeset_lock_all(dev);
625 /* remove from any CRTC */
626 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700627 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100628 /* should turn off the crtc */
629 memset(&set, 0, sizeof(struct drm_mode_set));
630 set.crtc = crtc;
631 set.fb = NULL;
632 ret = drm_mode_set_config_internal(&set);
633 if (ret)
634 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
635 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000636 }
Dave Airlief453ba02008-11-07 14:05:41 -0800637
Daniel Vetterb62584e2012-12-11 16:51:35 +0100638 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300639 if (plane->fb == fb)
640 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800641 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100642 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800643 }
644
Rob Clarkf7eff602012-09-05 21:48:38 +0000645 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800646}
Rob Clarkf7eff602012-09-05 21:48:38 +0000647EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800648
Rob Clark51fd3712013-11-19 12:10:12 -0500649DEFINE_WW_CLASS(crtc_ww_class);
650
Dave Airlief453ba02008-11-07 14:05:41 -0800651/**
Matt Ropere13161a2014-04-01 15:22:38 -0700652 * drm_crtc_init_with_planes - Initialise a new CRTC object with
653 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800654 * @dev: DRM device
655 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700656 * @primary: Primary plane for CRTC
657 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800658 * @funcs: callbacks for the new CRTC
659 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000660 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200661 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100662 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200663 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800664 */
Matt Ropere13161a2014-04-01 15:22:38 -0700665int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
666 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700667 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700668 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800669{
Rob Clark51fd3712013-11-19 12:10:12 -0500670 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200671 int ret;
672
Dave Airlief453ba02008-11-07 14:05:41 -0800673 crtc->dev = dev;
674 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000675 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800676
Rob Clark51fd3712013-11-19 12:10:12 -0500677 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200678 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
679 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100680 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800681
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300682 crtc->base.properties = &crtc->properties;
683
Rob Clark51fd3712013-11-19 12:10:12 -0500684 list_add_tail(&crtc->head, &config->crtc_list);
685 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200686
Matt Ropere13161a2014-04-01 15:22:38 -0700687 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700688 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700689 if (primary)
690 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700691 if (cursor)
692 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700693
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100694 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800695}
Matt Ropere13161a2014-04-01 15:22:38 -0700696EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800697
698/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000699 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800700 * @crtc: CRTC to cleanup
701 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000702 * This function cleans up @crtc and removes it from the DRM mode setting
703 * core. Note that the function does *not* free the crtc structure itself,
704 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800705 */
706void drm_crtc_cleanup(struct drm_crtc *crtc)
707{
708 struct drm_device *dev = crtc->dev;
709
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000710 kfree(crtc->gamma_store);
711 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800712
Rob Clark51fd3712013-11-19 12:10:12 -0500713 drm_modeset_lock_fini(&crtc->mutex);
714
Dave Airlief453ba02008-11-07 14:05:41 -0800715 drm_mode_object_put(dev, &crtc->base);
716 list_del(&crtc->head);
717 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100718
719 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
720 if (crtc->state && crtc->funcs->atomic_destroy_state)
721 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100722
723 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800724}
725EXPORT_SYMBOL(drm_crtc_cleanup);
726
727/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000728 * drm_crtc_index - find the index of a registered CRTC
729 * @crtc: CRTC to find index for
730 *
731 * Given a registered CRTC, return the index of that CRTC within a DRM
732 * device's list of CRTCs.
733 */
734unsigned int drm_crtc_index(struct drm_crtc *crtc)
735{
736 unsigned int index = 0;
737 struct drm_crtc *tmp;
738
739 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
740 if (tmp == crtc)
741 return index;
742
743 index++;
744 }
745
746 BUG();
747}
748EXPORT_SYMBOL(drm_crtc_index);
749
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100750/*
Dave Airlief453ba02008-11-07 14:05:41 -0800751 * drm_mode_remove - remove and free a mode
752 * @connector: connector list to modify
753 * @mode: mode to remove
754 *
Dave Airlief453ba02008-11-07 14:05:41 -0800755 * Remove @mode from @connector's mode list, then free it.
756 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100757static void drm_mode_remove(struct drm_connector *connector,
758 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800759{
760 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100761 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800762}
Dave Airlief453ba02008-11-07 14:05:41 -0800763
764/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200765 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
766 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200767 *
768 * The kernel supports per-connector configration of its consoles through
769 * use of the video= parameter. This function parses that option and
770 * extracts the user's specified mode (or enable/disable status) for a
771 * particular connector. This is typically only used during the early fbdev
772 * setup.
773 */
774static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
775{
776 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
777 char *option = NULL;
778
779 if (fb_get_options(connector->name, &option))
780 return;
781
782 if (!drm_mode_parse_command_line_for_connector(option,
783 connector,
784 mode))
785 return;
786
787 if (mode->force) {
788 const char *s;
789
790 switch (mode->force) {
791 case DRM_FORCE_OFF:
792 s = "OFF";
793 break;
794 case DRM_FORCE_ON_DIGITAL:
795 s = "ON - dig";
796 break;
797 default:
798 case DRM_FORCE_ON:
799 s = "ON";
800 break;
801 }
802
803 DRM_INFO("forcing %s connector %s\n", connector->name, s);
804 connector->force = mode->force;
805 }
806
807 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
808 connector->name,
809 mode->xres, mode->yres,
810 mode->refresh_specified ? mode->refresh : 60,
811 mode->rb ? " reduced blanking" : "",
812 mode->margins ? " with margins" : "",
813 mode->interlace ? " interlaced" : "");
814}
815
816/**
Dave Airlief453ba02008-11-07 14:05:41 -0800817 * drm_connector_init - Init a preallocated connector
818 * @dev: DRM device
819 * @connector: the connector to init
820 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100821 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800822 *
Dave Airlief453ba02008-11-07 14:05:41 -0800823 * Initialises a preallocated connector. Connectors should be
824 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200825 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100826 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200827 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800828 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200829int drm_connector_init(struct drm_device *dev,
830 struct drm_connector *connector,
831 const struct drm_connector_funcs *funcs,
832 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800833{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200834 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400835 struct ida *connector_ida =
836 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200837
Daniel Vetter84849902012-12-02 00:28:11 +0100838 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800839
Dave Airlie2ee39452014-07-24 11:53:45 +1000840 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200841 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300842 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200843
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300844 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800845 connector->dev = dev;
846 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800847 connector->connector_type = connector_type;
848 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400849 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
850 if (connector->connector_type_id < 0) {
851 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300852 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400853 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300854 connector->name =
855 kasprintf(GFP_KERNEL, "%s-%d",
856 drm_connector_enum_list[connector_type].name,
857 connector->connector_type_id);
858 if (!connector->name) {
859 ret = -ENOMEM;
860 goto out_put;
861 }
862
Dave Airlief453ba02008-11-07 14:05:41 -0800863 INIT_LIST_HEAD(&connector->probed_modes);
864 INIT_LIST_HEAD(&connector->modes);
865 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000866 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800867
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200868 drm_connector_get_cmdline_mode(connector);
869
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100870 /* We should add connectors at the end to avoid upsetting the connector
871 * index too much. */
Dave Airlief453ba02008-11-07 14:05:41 -0800872 list_add_tail(&connector->head, &dev->mode_config.connector_list);
873 dev->mode_config.num_connector++;
874
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200875 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500876 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200877 dev->mode_config.edid_property,
878 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800879
Rob Clark58495562012-10-11 20:50:56 -0500880 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800881 dev->mode_config.dpms_property, 0);
882
Thomas Wood30f65702014-06-18 17:52:32 +0100883 connector->debugfs_entry = NULL;
884
Jani Nikula2abdd312014-05-14 16:58:19 +0300885out_put:
886 if (ret)
887 drm_mode_object_put(dev, &connector->base);
888
889out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100890 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200891
892 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800893}
894EXPORT_SYMBOL(drm_connector_init);
895
896/**
897 * drm_connector_cleanup - cleans up an initialised connector
898 * @connector: connector to cleanup
899 *
Dave Airlief453ba02008-11-07 14:05:41 -0800900 * Cleans up the connector but doesn't free the object.
901 */
902void drm_connector_cleanup(struct drm_connector *connector)
903{
904 struct drm_device *dev = connector->dev;
905 struct drm_display_mode *mode, *t;
906
Dave Airlie40d9b042014-10-20 16:29:33 +1000907 if (connector->tile_group) {
908 drm_mode_put_tile_group(dev, connector->tile_group);
909 connector->tile_group = NULL;
910 }
911
Dave Airlief453ba02008-11-07 14:05:41 -0800912 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
913 drm_mode_remove(connector, mode);
914
915 list_for_each_entry_safe(mode, t, &connector->modes, head)
916 drm_mode_remove(connector, mode);
917
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400918 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
919 connector->connector_type_id);
920
Dave Airlief453ba02008-11-07 14:05:41 -0800921 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300922 kfree(connector->name);
923 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800924 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000925 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100926
927 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
928 if (connector->state && connector->funcs->atomic_destroy_state)
929 connector->funcs->atomic_destroy_state(connector,
930 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100931
932 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800933}
934EXPORT_SYMBOL(drm_connector_cleanup);
935
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100936/**
Daniel Vetter10f637b2014-07-29 13:47:11 +0200937 * drm_connector_index - find the index of a registered connector
938 * @connector: connector to find index for
939 *
940 * Given a registered connector, return the index of that connector within a DRM
941 * device's list of connectors.
942 */
943unsigned int drm_connector_index(struct drm_connector *connector)
944{
945 unsigned int index = 0;
946 struct drm_connector *tmp;
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100947 struct drm_mode_config *config = &connector->dev->mode_config;
948
949 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter10f637b2014-07-29 13:47:11 +0200950
951 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
952 if (tmp == connector)
953 return index;
954
955 index++;
956 }
957
958 BUG();
959}
960EXPORT_SYMBOL(drm_connector_index);
961
962/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100963 * drm_connector_register - register a connector
964 * @connector: the connector to register
965 *
966 * Register userspace interfaces for a connector
967 *
968 * Returns:
969 * Zero on success, error code on failure.
970 */
971int drm_connector_register(struct drm_connector *connector)
972{
Thomas Wood30f65702014-06-18 17:52:32 +0100973 int ret;
974
Dave Airlie2ee39452014-07-24 11:53:45 +1000975 drm_mode_object_register(connector->dev, &connector->base);
976
Thomas Wood30f65702014-06-18 17:52:32 +0100977 ret = drm_sysfs_connector_add(connector);
978 if (ret)
979 return ret;
980
981 ret = drm_debugfs_connector_add(connector);
982 if (ret) {
983 drm_sysfs_connector_remove(connector);
984 return ret;
985 }
986
987 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100988}
989EXPORT_SYMBOL(drm_connector_register);
990
991/**
992 * drm_connector_unregister - unregister a connector
993 * @connector: the connector to unregister
994 *
995 * Unregister userspace interfaces for a connector
996 */
997void drm_connector_unregister(struct drm_connector *connector)
998{
999 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001000 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001001}
1002EXPORT_SYMBOL(drm_connector_unregister);
1003
1004
1005/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001006 * drm_connector_unplug_all - unregister connector userspace interfaces
1007 * @dev: drm device
1008 *
1009 * This function unregisters all connector userspace interfaces in sysfs. Should
1010 * be call when the device is disconnected, e.g. from an usb driver's
1011 * ->disconnect callback.
1012 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001013void drm_connector_unplug_all(struct drm_device *dev)
1014{
1015 struct drm_connector *connector;
1016
1017 /* taking the mode config mutex ends up in a clash with sysfs */
1018 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001019 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001020
1021}
1022EXPORT_SYMBOL(drm_connector_unplug_all);
1023
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001024/**
1025 * drm_bridge_init - initialize a drm transcoder/bridge
1026 * @dev: drm device
1027 * @bridge: transcoder/bridge to set up
1028 * @funcs: bridge function table
1029 *
1030 * Initialises a preallocated bridge. Bridges should be
1031 * subclassed as part of driver connector objects.
1032 *
1033 * Returns:
1034 * Zero on success, error code on failure.
1035 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001036int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1037 const struct drm_bridge_funcs *funcs)
1038{
1039 int ret;
1040
1041 drm_modeset_lock_all(dev);
1042
1043 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1044 if (ret)
1045 goto out;
1046
1047 bridge->dev = dev;
1048 bridge->funcs = funcs;
1049
1050 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1051 dev->mode_config.num_bridge++;
1052
1053 out:
1054 drm_modeset_unlock_all(dev);
1055 return ret;
1056}
1057EXPORT_SYMBOL(drm_bridge_init);
1058
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001059/**
1060 * drm_bridge_cleanup - cleans up an initialised bridge
1061 * @bridge: bridge to cleanup
1062 *
1063 * Cleans up the bridge but doesn't free the object.
1064 */
Sean Paul3b336ec2013-08-14 16:47:37 -04001065void drm_bridge_cleanup(struct drm_bridge *bridge)
1066{
1067 struct drm_device *dev = bridge->dev;
1068
1069 drm_modeset_lock_all(dev);
1070 drm_mode_object_put(dev, &bridge->base);
1071 list_del(&bridge->head);
1072 dev->mode_config.num_bridge--;
1073 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001074
1075 memset(bridge, 0, sizeof(*bridge));
Sean Paul3b336ec2013-08-14 16:47:37 -04001076}
1077EXPORT_SYMBOL(drm_bridge_cleanup);
1078
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001079/**
1080 * drm_encoder_init - Init a preallocated encoder
1081 * @dev: drm device
1082 * @encoder: the encoder to init
1083 * @funcs: callbacks for this encoder
1084 * @encoder_type: user visible type of the encoder
1085 *
1086 * Initialises a preallocated encoder. Encoder should be
1087 * subclassed as part of driver encoder objects.
1088 *
1089 * Returns:
1090 * Zero on success, error code on failure.
1091 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001092int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001093 struct drm_encoder *encoder,
1094 const struct drm_encoder_funcs *funcs,
1095 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001096{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001097 int ret;
1098
Daniel Vetter84849902012-12-02 00:28:11 +01001099 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001100
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001101 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1102 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001103 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001104
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001105 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001106 encoder->encoder_type = encoder_type;
1107 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001108 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1109 drm_encoder_enum_list[encoder_type].name,
1110 encoder->base.id);
1111 if (!encoder->name) {
1112 ret = -ENOMEM;
1113 goto out_put;
1114 }
Dave Airlief453ba02008-11-07 14:05:41 -08001115
1116 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1117 dev->mode_config.num_encoder++;
1118
Jani Nikulae5748942014-05-14 16:58:20 +03001119out_put:
1120 if (ret)
1121 drm_mode_object_put(dev, &encoder->base);
1122
1123out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001124 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001125
1126 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001127}
1128EXPORT_SYMBOL(drm_encoder_init);
1129
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001130/**
1131 * drm_encoder_cleanup - cleans up an initialised encoder
1132 * @encoder: encoder to cleanup
1133 *
1134 * Cleans up the encoder but doesn't free the object.
1135 */
Dave Airlief453ba02008-11-07 14:05:41 -08001136void drm_encoder_cleanup(struct drm_encoder *encoder)
1137{
1138 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001139
Daniel Vetter84849902012-12-02 00:28:11 +01001140 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001141 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001142 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001143 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001144 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001145 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001146
1147 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001148}
1149EXPORT_SYMBOL(drm_encoder_cleanup);
1150
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001151/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001152 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001153 * @dev: DRM device
1154 * @plane: plane object to init
1155 * @possible_crtcs: bitmask of possible CRTCs
1156 * @funcs: callbacks for the new plane
1157 * @formats: array of supported formats (%DRM_FORMAT_*)
1158 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001159 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001160 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001161 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001162 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001163 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001164 * Zero on success, error code on failure.
1165 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001166int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1167 unsigned long possible_crtcs,
1168 const struct drm_plane_funcs *funcs,
1169 const uint32_t *formats, uint32_t format_count,
1170 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001171{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001172 int ret;
1173
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001174 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1175 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001176 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001177
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001178 drm_modeset_lock_init(&plane->mutex);
1179
Rob Clark4d939142012-05-17 02:23:27 -06001180 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001181 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001182 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001183 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1184 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001185 if (!plane->format_types) {
1186 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1187 drm_mode_object_put(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001188 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001189 }
1190
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001191 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001192 plane->format_count = format_count;
1193 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001194 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001195
Matt Roperdc415ff2014-04-01 15:22:36 -07001196 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1197 dev->mode_config.num_total_plane++;
1198 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1199 dev->mode_config.num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001200
Rob Clark9922ab52014-04-01 20:16:57 -04001201 drm_object_attach_property(&plane->base,
1202 dev->mode_config.plane_type_property,
1203 plane->type);
1204
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001205 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001206}
Matt Roperdc415ff2014-04-01 15:22:36 -07001207EXPORT_SYMBOL(drm_universal_plane_init);
1208
1209/**
1210 * drm_plane_init - Initialize a legacy plane
1211 * @dev: DRM device
1212 * @plane: plane object to init
1213 * @possible_crtcs: bitmask of possible CRTCs
1214 * @funcs: callbacks for the new plane
1215 * @formats: array of supported formats (%DRM_FORMAT_*)
1216 * @format_count: number of elements in @formats
1217 * @is_primary: plane type (primary vs overlay)
1218 *
1219 * Legacy API to initialize a DRM plane.
1220 *
1221 * New drivers should call drm_universal_plane_init() instead.
1222 *
1223 * Returns:
1224 * Zero on success, error code on failure.
1225 */
1226int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1227 unsigned long possible_crtcs,
1228 const struct drm_plane_funcs *funcs,
1229 const uint32_t *formats, uint32_t format_count,
1230 bool is_primary)
1231{
1232 enum drm_plane_type type;
1233
1234 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1235 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1236 formats, format_count, type);
1237}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001238EXPORT_SYMBOL(drm_plane_init);
1239
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001240/**
1241 * drm_plane_cleanup - Clean up the core plane usage
1242 * @plane: plane to cleanup
1243 *
1244 * This function cleans up @plane and removes it from the DRM mode setting
1245 * core. Note that the function does *not* free the plane structure itself,
1246 * this is the responsibility of the caller.
1247 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001248void drm_plane_cleanup(struct drm_plane *plane)
1249{
1250 struct drm_device *dev = plane->dev;
1251
Daniel Vetter84849902012-12-02 00:28:11 +01001252 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001253 kfree(plane->format_types);
1254 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001255
1256 BUG_ON(list_empty(&plane->head));
1257
1258 list_del(&plane->head);
1259 dev->mode_config.num_total_plane--;
1260 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1261 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001262 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001263
1264 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1265 if (plane->state && plane->funcs->atomic_destroy_state)
1266 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001267
1268 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001269}
1270EXPORT_SYMBOL(drm_plane_cleanup);
1271
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001272/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001273 * drm_plane_index - find the index of a registered plane
1274 * @plane: plane to find index for
1275 *
1276 * Given a registered plane, return the index of that CRTC within a DRM
1277 * device's list of planes.
1278 */
1279unsigned int drm_plane_index(struct drm_plane *plane)
1280{
1281 unsigned int index = 0;
1282 struct drm_plane *tmp;
1283
1284 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1285 if (tmp == plane)
1286 return index;
1287
1288 index++;
1289 }
1290
1291 BUG();
1292}
1293EXPORT_SYMBOL(drm_plane_index);
1294
1295/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001296 * drm_plane_force_disable - Forcibly disable a plane
1297 * @plane: plane to disable
1298 *
1299 * Forces the plane to be disabled.
1300 *
1301 * Used when the plane's current framebuffer is destroyed,
1302 * and when restoring fbdev mode.
1303 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001304void drm_plane_force_disable(struct drm_plane *plane)
1305{
1306 int ret;
1307
Daniel Vetter3d30a592014-07-27 13:42:42 +02001308 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001309 return;
1310
Daniel Vetter3d30a592014-07-27 13:42:42 +02001311 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001312 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001313 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001314 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001315 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001316 return;
1317 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001318 /* disconnect the plane from the fb and crtc: */
Daniel Vetter3d30a592014-07-27 13:42:42 +02001319 __drm_framebuffer_unreference(plane->old_fb);
1320 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001321 plane->fb = NULL;
1322 plane->crtc = NULL;
1323}
1324EXPORT_SYMBOL(drm_plane_force_disable);
1325
Dave Airlief453ba02008-11-07 14:05:41 -08001326static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1327{
1328 struct drm_property *edid;
1329 struct drm_property *dpms;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001330 struct drm_property *dev_path;
Dave Airlief453ba02008-11-07 14:05:41 -08001331
1332 /*
1333 * Standard properties (apply to all connectors)
1334 */
1335 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1336 DRM_MODE_PROP_IMMUTABLE,
1337 "EDID", 0);
1338 dev->mode_config.edid_property = edid;
1339
Sascha Hauer4a67d392012-02-06 10:58:17 +01001340 dpms = drm_property_create_enum(dev, 0,
1341 "DPMS", drm_dpms_enum_list,
1342 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001343 dev->mode_config.dpms_property = dpms;
1344
Dave Airlie43aba7e2014-06-05 14:01:31 +10001345 dev_path = drm_property_create(dev,
1346 DRM_MODE_PROP_BLOB |
1347 DRM_MODE_PROP_IMMUTABLE,
1348 "PATH", 0);
1349 dev->mode_config.path_property = dev_path;
1350
Dave Airlie6f134d72014-10-20 16:30:50 +10001351 dev->mode_config.tile_property = drm_property_create(dev,
1352 DRM_MODE_PROP_BLOB |
1353 DRM_MODE_PROP_IMMUTABLE,
1354 "TILE", 0);
1355
Dave Airlief453ba02008-11-07 14:05:41 -08001356 return 0;
1357}
1358
Rob Clark9922ab52014-04-01 20:16:57 -04001359static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1360{
1361 struct drm_property *type;
1362
1363 /*
1364 * Standard properties (apply to all planes)
1365 */
1366 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1367 "type", drm_plane_type_enum_list,
1368 ARRAY_SIZE(drm_plane_type_enum_list));
1369 dev->mode_config.plane_type_property = type;
1370
1371 return 0;
1372}
1373
Dave Airlief453ba02008-11-07 14:05:41 -08001374/**
1375 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1376 * @dev: DRM device
1377 *
1378 * Called by a driver the first time a DVI-I connector is made.
1379 */
1380int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1381{
1382 struct drm_property *dvi_i_selector;
1383 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001384
1385 if (dev->mode_config.dvi_i_select_subconnector_property)
1386 return 0;
1387
1388 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001389 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001390 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001391 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001392 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001393 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1394
Sascha Hauer4a67d392012-02-06 10:58:17 +01001395 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001396 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001397 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001398 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001399 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1400
1401 return 0;
1402}
1403EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1404
1405/**
1406 * drm_create_tv_properties - create TV specific connector properties
1407 * @dev: DRM device
1408 * @num_modes: number of different TV formats (modes) supported
1409 * @modes: array of pointers to strings containing name of each format
1410 *
1411 * Called by a driver's TV initialization routine, this function creates
1412 * the TV specific connector properties for a given device. Caller is
1413 * responsible for allocating a list of format names and passing them to
1414 * this routine.
1415 */
Thierry Reding2f763312014-10-13 12:45:57 +02001416int drm_mode_create_tv_properties(struct drm_device *dev,
1417 unsigned int num_modes,
Dave Airlief453ba02008-11-07 14:05:41 -08001418 char *modes[])
1419{
1420 struct drm_property *tv_selector;
1421 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001422 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001423
1424 if (dev->mode_config.tv_select_subconnector_property)
1425 return 0;
1426
1427 /*
1428 * Basic connector properties
1429 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001430 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001431 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001432 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001433 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001434 dev->mode_config.tv_select_subconnector_property = tv_selector;
1435
1436 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001437 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1438 "subconnector",
1439 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001440 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001441 dev->mode_config.tv_subconnector_property = tv_subconnector;
1442
1443 /*
1444 * Other, TV specific properties: margins & TV modes.
1445 */
1446 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001447 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001448
1449 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001450 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001451
1452 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001453 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001454
1455 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001456 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001457
1458 dev->mode_config.tv_mode_property =
1459 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1460 "mode", num_modes);
1461 for (i = 0; i < num_modes; i++)
1462 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1463 i, modes[i]);
1464
Francisco Jerezb6b79022009-08-02 04:19:20 +02001465 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001466 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001467
1468 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001469 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001470
1471 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001472 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001473
Francisco Jereza75f0232009-08-12 02:30:10 +02001474 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001475 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001476
1477 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001478 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001479
1480 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001481 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001482
Dave Airlief453ba02008-11-07 14:05:41 -08001483 return 0;
1484}
1485EXPORT_SYMBOL(drm_mode_create_tv_properties);
1486
1487/**
1488 * drm_mode_create_scaling_mode_property - create scaling mode property
1489 * @dev: DRM device
1490 *
1491 * Called by a driver the first time it's needed, must be attached to desired
1492 * connectors.
1493 */
1494int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1495{
1496 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001497
1498 if (dev->mode_config.scaling_mode_property)
1499 return 0;
1500
1501 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001502 drm_property_create_enum(dev, 0, "scaling mode",
1503 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001504 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001505
1506 dev->mode_config.scaling_mode_property = scaling_mode;
1507
1508 return 0;
1509}
1510EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1511
1512/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301513 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1514 * @dev: DRM device
1515 *
1516 * Called by a driver the first time it's needed, must be attached to desired
1517 * connectors.
1518 *
1519 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001520 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301521 */
1522int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1523{
1524 if (dev->mode_config.aspect_ratio_property)
1525 return 0;
1526
1527 dev->mode_config.aspect_ratio_property =
1528 drm_property_create_enum(dev, 0, "aspect ratio",
1529 drm_aspect_ratio_enum_list,
1530 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1531
1532 if (dev->mode_config.aspect_ratio_property == NULL)
1533 return -ENOMEM;
1534
1535 return 0;
1536}
1537EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1538
1539/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001540 * drm_mode_create_dirty_property - create dirty property
1541 * @dev: DRM device
1542 *
1543 * Called by a driver the first time it's needed, must be attached to desired
1544 * connectors.
1545 */
1546int drm_mode_create_dirty_info_property(struct drm_device *dev)
1547{
1548 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001549
1550 if (dev->mode_config.dirty_info_property)
1551 return 0;
1552
1553 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001554 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001555 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001556 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001557 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001558 dev->mode_config.dirty_info_property = dirty_info;
1559
1560 return 0;
1561}
1562EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1563
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001564/**
1565 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1566 * @dev: DRM device
1567 *
1568 * Create the the suggested x/y offset property for connectors.
1569 */
1570int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1571{
1572 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1573 return 0;
1574
1575 dev->mode_config.suggested_x_property =
1576 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1577
1578 dev->mode_config.suggested_y_property =
1579 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1580
1581 if (dev->mode_config.suggested_x_property == NULL ||
1582 dev->mode_config.suggested_y_property == NULL)
1583 return -ENOMEM;
1584 return 0;
1585}
1586EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1587
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001588static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001589{
1590 uint32_t total_objects = 0;
1591
1592 total_objects += dev->mode_config.num_crtc;
1593 total_objects += dev->mode_config.num_connector;
1594 total_objects += dev->mode_config.num_encoder;
Sean Paul3b336ec2013-08-14 16:47:37 -04001595 total_objects += dev->mode_config.num_bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001596
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01001597 group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08001598 if (!group->id_list)
1599 return -ENOMEM;
1600
1601 group->num_crtcs = 0;
1602 group->num_connectors = 0;
1603 group->num_encoders = 0;
Sean Paul3b336ec2013-08-14 16:47:37 -04001604 group->num_bridges = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001605 return 0;
1606}
1607
Dave Airliead222792014-05-02 13:22:19 +10001608void drm_mode_group_destroy(struct drm_mode_group *group)
1609{
1610 kfree(group->id_list);
1611 group->id_list = NULL;
1612}
1613
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001614/*
1615 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1616 * the drm core's responsibility to set up mode control groups.
1617 */
Dave Airlief453ba02008-11-07 14:05:41 -08001618int drm_mode_group_init_legacy_group(struct drm_device *dev,
1619 struct drm_mode_group *group)
1620{
1621 struct drm_crtc *crtc;
1622 struct drm_encoder *encoder;
1623 struct drm_connector *connector;
Sean Paul3b336ec2013-08-14 16:47:37 -04001624 struct drm_bridge *bridge;
Dave Airlief453ba02008-11-07 14:05:41 -08001625 int ret;
1626
Thierry Reding0cc0b222014-12-10 13:03:37 +01001627 ret = drm_mode_group_init(dev, group);
1628 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08001629 return ret;
1630
1631 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1632 group->id_list[group->num_crtcs++] = crtc->base.id;
1633
1634 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1635 group->id_list[group->num_crtcs + group->num_encoders++] =
1636 encoder->base.id;
1637
1638 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1639 group->id_list[group->num_crtcs + group->num_encoders +
1640 group->num_connectors++] = connector->base.id;
1641
Sean Paul3b336ec2013-08-14 16:47:37 -04001642 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1643 group->id_list[group->num_crtcs + group->num_encoders +
1644 group->num_connectors + group->num_bridges++] =
1645 bridge->base.id;
1646
Dave Airlief453ba02008-11-07 14:05:41 -08001647 return 0;
1648}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001649EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001650
Dave Airlie2390cd12014-06-05 14:01:29 +10001651void drm_reinit_primary_mode_group(struct drm_device *dev)
1652{
1653 drm_modeset_lock_all(dev);
1654 drm_mode_group_destroy(&dev->primary->mode_group);
1655 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1656 drm_modeset_unlock_all(dev);
1657}
1658EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1659
Dave Airlief453ba02008-11-07 14:05:41 -08001660/**
Dave Airlief453ba02008-11-07 14:05:41 -08001661 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1662 * @out: drm_mode_modeinfo struct to return to the user
1663 * @in: drm_display_mode to use
1664 *
Dave Airlief453ba02008-11-07 14:05:41 -08001665 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1666 * the user.
1667 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001668static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1669 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001670{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001671 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1672 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1673 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1674 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1675 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1676 "timing values too large for mode info\n");
1677
Dave Airlief453ba02008-11-07 14:05:41 -08001678 out->clock = in->clock;
1679 out->hdisplay = in->hdisplay;
1680 out->hsync_start = in->hsync_start;
1681 out->hsync_end = in->hsync_end;
1682 out->htotal = in->htotal;
1683 out->hskew = in->hskew;
1684 out->vdisplay = in->vdisplay;
1685 out->vsync_start = in->vsync_start;
1686 out->vsync_end = in->vsync_end;
1687 out->vtotal = in->vtotal;
1688 out->vscan = in->vscan;
1689 out->vrefresh = in->vrefresh;
1690 out->flags = in->flags;
1691 out->type = in->type;
1692 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1693 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1694}
1695
1696/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001697 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001698 * @out: drm_display_mode to return to the user
1699 * @in: drm_mode_modeinfo to use
1700 *
Dave Airlief453ba02008-11-07 14:05:41 -08001701 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1702 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001703 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001704 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001705 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001706 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001707static int drm_crtc_convert_umode(struct drm_display_mode *out,
1708 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001709{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001710 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1711 return -ERANGE;
1712
Damien Lespiau5848ad42013-09-27 12:11:50 +01001713 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1714 return -EINVAL;
1715
Dave Airlief453ba02008-11-07 14:05:41 -08001716 out->clock = in->clock;
1717 out->hdisplay = in->hdisplay;
1718 out->hsync_start = in->hsync_start;
1719 out->hsync_end = in->hsync_end;
1720 out->htotal = in->htotal;
1721 out->hskew = in->hskew;
1722 out->vdisplay = in->vdisplay;
1723 out->vsync_start = in->vsync_start;
1724 out->vsync_end = in->vsync_end;
1725 out->vtotal = in->vtotal;
1726 out->vscan = in->vscan;
1727 out->vrefresh = in->vrefresh;
1728 out->flags = in->flags;
1729 out->type = in->type;
1730 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1731 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001732
1733 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001734}
1735
1736/**
1737 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001738 * @dev: drm device for the ioctl
1739 * @data: data pointer for the ioctl
1740 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001741 *
Dave Airlief453ba02008-11-07 14:05:41 -08001742 * Construct a set of configuration description structures and return
1743 * them to the user, including CRTC, connector and framebuffer configuration.
1744 *
1745 * Called by the user via ioctl.
1746 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001747 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001748 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001749 */
1750int drm_mode_getresources(struct drm_device *dev, void *data,
1751 struct drm_file *file_priv)
1752{
1753 struct drm_mode_card_res *card_res = data;
1754 struct list_head *lh;
1755 struct drm_framebuffer *fb;
1756 struct drm_connector *connector;
1757 struct drm_crtc *crtc;
1758 struct drm_encoder *encoder;
1759 int ret = 0;
1760 int connector_count = 0;
1761 int crtc_count = 0;
1762 int fb_count = 0;
1763 int encoder_count = 0;
1764 int copied = 0, i;
1765 uint32_t __user *fb_id;
1766 uint32_t __user *crtc_id;
1767 uint32_t __user *connector_id;
1768 uint32_t __user *encoder_id;
1769 struct drm_mode_group *mode_group;
1770
Dave Airliefb3b06c2011-02-08 13:55:21 +10001771 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1772 return -EINVAL;
1773
Dave Airlief453ba02008-11-07 14:05:41 -08001774
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001775 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001776 /*
1777 * For the non-control nodes we need to limit the list of resources
1778 * by IDs in the group list for this node
1779 */
1780 list_for_each(lh, &file_priv->fbs)
1781 fb_count++;
1782
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001783 /* handle this in 4 parts */
1784 /* FBs */
1785 if (card_res->count_fbs >= fb_count) {
1786 copied = 0;
1787 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1788 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1789 if (put_user(fb->base.id, fb_id + copied)) {
1790 mutex_unlock(&file_priv->fbs_lock);
1791 return -EFAULT;
1792 }
1793 copied++;
1794 }
1795 }
1796 card_res->count_fbs = fb_count;
1797 mutex_unlock(&file_priv->fbs_lock);
1798
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001799 /* mode_config.mutex protects the connector list against e.g. DP MST
1800 * connector hot-adding. CRTC/Plane lists are invariant. */
1801 mutex_lock(&dev->mode_config.mutex);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001802 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001803
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001804 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001805 list_for_each(lh, &dev->mode_config.crtc_list)
1806 crtc_count++;
1807
1808 list_for_each(lh, &dev->mode_config.connector_list)
1809 connector_count++;
1810
1811 list_for_each(lh, &dev->mode_config.encoder_list)
1812 encoder_count++;
1813 } else {
1814
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001815 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001816 crtc_count = mode_group->num_crtcs;
1817 connector_count = mode_group->num_connectors;
1818 encoder_count = mode_group->num_encoders;
1819 }
1820
1821 card_res->max_height = dev->mode_config.max_height;
1822 card_res->min_height = dev->mode_config.min_height;
1823 card_res->max_width = dev->mode_config.max_width;
1824 card_res->min_width = dev->mode_config.min_width;
1825
Dave Airlief453ba02008-11-07 14:05:41 -08001826 /* CRTCs */
1827 if (card_res->count_crtcs >= crtc_count) {
1828 copied = 0;
1829 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001830 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001831 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1832 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001833 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001834 if (put_user(crtc->base.id, crtc_id + copied)) {
1835 ret = -EFAULT;
1836 goto out;
1837 }
1838 copied++;
1839 }
1840 } else {
1841 for (i = 0; i < mode_group->num_crtcs; i++) {
1842 if (put_user(mode_group->id_list[i],
1843 crtc_id + copied)) {
1844 ret = -EFAULT;
1845 goto out;
1846 }
1847 copied++;
1848 }
1849 }
1850 }
1851 card_res->count_crtcs = crtc_count;
1852
1853 /* Encoders */
1854 if (card_res->count_encoders >= encoder_count) {
1855 copied = 0;
1856 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001857 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001858 list_for_each_entry(encoder,
1859 &dev->mode_config.encoder_list,
1860 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001861 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001862 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001863 if (put_user(encoder->base.id, encoder_id +
1864 copied)) {
1865 ret = -EFAULT;
1866 goto out;
1867 }
1868 copied++;
1869 }
1870 } else {
1871 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1872 if (put_user(mode_group->id_list[i],
1873 encoder_id + copied)) {
1874 ret = -EFAULT;
1875 goto out;
1876 }
1877 copied++;
1878 }
1879
1880 }
1881 }
1882 card_res->count_encoders = encoder_count;
1883
1884 /* Connectors */
1885 if (card_res->count_connectors >= connector_count) {
1886 copied = 0;
1887 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001888 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001889 list_for_each_entry(connector,
1890 &dev->mode_config.connector_list,
1891 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001892 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1893 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001894 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001895 if (put_user(connector->base.id,
1896 connector_id + copied)) {
1897 ret = -EFAULT;
1898 goto out;
1899 }
1900 copied++;
1901 }
1902 } else {
1903 int start = mode_group->num_crtcs +
1904 mode_group->num_encoders;
1905 for (i = start; i < start + mode_group->num_connectors; i++) {
1906 if (put_user(mode_group->id_list[i],
1907 connector_id + copied)) {
1908 ret = -EFAULT;
1909 goto out;
1910 }
1911 copied++;
1912 }
1913 }
1914 }
1915 card_res->count_connectors = connector_count;
1916
Jerome Glisse94401062010-07-15 15:43:25 -04001917 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001918 card_res->count_connectors, card_res->count_encoders);
1919
1920out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001921 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001922 return ret;
1923}
1924
1925/**
1926 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001927 * @dev: drm device for the ioctl
1928 * @data: data pointer for the ioctl
1929 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001930 *
Dave Airlief453ba02008-11-07 14:05:41 -08001931 * Construct a CRTC configuration structure to return to the user.
1932 *
1933 * Called by the user via ioctl.
1934 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001935 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001936 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001937 */
1938int drm_mode_getcrtc(struct drm_device *dev,
1939 void *data, struct drm_file *file_priv)
1940{
1941 struct drm_mode_crtc *crtc_resp = data;
1942 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001943
Dave Airliefb3b06c2011-02-08 13:55:21 +10001944 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1945 return -EINVAL;
1946
Rob Clarka2b34e22013-10-05 16:36:52 -04001947 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001948 if (!crtc)
1949 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001950
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001951 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08001952 crtc_resp->x = crtc->x;
1953 crtc_resp->y = crtc->y;
1954 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001955 if (crtc->primary->fb)
1956 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001957 else
1958 crtc_resp->fb_id = 0;
1959
1960 if (crtc->enabled) {
1961
1962 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1963 crtc_resp->mode_valid = 1;
1964
1965 } else {
1966 crtc_resp->mode_valid = 0;
1967 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001968 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08001969
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001970 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001971}
1972
Damien Lespiau61d8e322013-09-25 16:45:22 +01001973static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1974 const struct drm_file *file_priv)
1975{
1976 /*
1977 * If user-space hasn't configured the driver to expose the stereo 3D
1978 * modes, don't expose them.
1979 */
1980 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1981 return false;
1982
1983 return true;
1984}
1985
Daniel Vetterabd69c52014-11-25 23:50:05 +01001986static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1987{
1988 /* For atomic drivers only state objects are synchronously updated and
1989 * protected by modeset locks, so check those first. */
1990 if (connector->state)
1991 return connector->state->best_encoder;
1992 return connector->encoder;
1993}
1994
Rob Clark95cbf112014-12-18 16:01:49 -05001995/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05001996static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05001997 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
1998 uint32_t *arg_count_props)
1999{
Rob Clark88a48e22014-12-18 16:01:50 -05002000 int props_count;
2001 int i, ret, copied;
2002
2003 props_count = obj->properties->count;
2004 if (!atomic)
2005 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002006
2007 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002008 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002009 struct drm_property *prop = obj->properties->properties[i];
2010 uint64_t val;
2011
Rob Clark88a48e22014-12-18 16:01:50 -05002012 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2013 continue;
2014
Rob Clark95cbf112014-12-18 16:01:49 -05002015 ret = drm_object_property_get_value(obj, prop, &val);
2016 if (ret)
2017 return ret;
2018
2019 if (put_user(prop->base.id, prop_ptr + copied))
2020 return -EFAULT;
2021
2022 if (put_user(val, prop_values + copied))
2023 return -EFAULT;
2024
2025 copied++;
2026 }
2027 }
2028 *arg_count_props = props_count;
2029
2030 return 0;
2031}
2032
Dave Airlief453ba02008-11-07 14:05:41 -08002033/**
2034 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002035 * @dev: drm device for the ioctl
2036 * @data: data pointer for the ioctl
2037 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002038 *
Dave Airlief453ba02008-11-07 14:05:41 -08002039 * Construct a connector configuration structure to return to the user.
2040 *
2041 * Called by the user via ioctl.
2042 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002043 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002044 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002045 */
2046int drm_mode_getconnector(struct drm_device *dev, void *data,
2047 struct drm_file *file_priv)
2048{
2049 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002050 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002051 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002052 struct drm_display_mode *mode;
2053 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002054 int encoders_count = 0;
2055 int ret = 0;
2056 int copied = 0;
2057 int i;
2058 struct drm_mode_modeinfo u_mode;
2059 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002060 uint32_t __user *encoder_ptr;
2061
Dave Airliefb3b06c2011-02-08 13:55:21 +10002062 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2063 return -EINVAL;
2064
Dave Airlief453ba02008-11-07 14:05:41 -08002065 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2066
Jerome Glisse94401062010-07-15 15:43:25 -04002067 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002068
Daniel Vetter7b240562012-12-12 00:35:33 +01002069 mutex_lock(&dev->mode_config.mutex);
Rob Clarkccfc0862014-12-18 16:01:48 -05002070 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Dave Airlief453ba02008-11-07 14:05:41 -08002071
Rob Clarka2b34e22013-10-05 16:36:52 -04002072 connector = drm_connector_find(dev, out_resp->connector_id);
2073 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002074 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002075 goto out;
2076 }
Dave Airlief453ba02008-11-07 14:05:41 -08002077
Thierry Reding01073b02014-12-10 13:03:38 +01002078 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2079 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002080 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002081
2082 if (out_resp->count_modes == 0) {
2083 connector->funcs->fill_modes(connector,
2084 dev->mode_config.max_width,
2085 dev->mode_config.max_height);
2086 }
2087
2088 /* delayed so we get modes regardless of pre-fill_modes state */
2089 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002090 if (drm_mode_expose_to_userspace(mode, file_priv))
2091 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002092
2093 out_resp->connector_id = connector->base.id;
2094 out_resp->connector_type = connector->connector_type;
2095 out_resp->connector_type_id = connector->connector_type_id;
2096 out_resp->mm_width = connector->display_info.width_mm;
2097 out_resp->mm_height = connector->display_info.height_mm;
2098 out_resp->subpixel = connector->display_info.subpixel_order;
2099 out_resp->connection = connector->status;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002100 encoder = drm_connector_get_encoder(connector);
2101 if (encoder)
2102 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002103 else
2104 out_resp->encoder_id = 0;
2105
2106 /*
2107 * This ioctl is called twice, once to determine how much space is
2108 * needed, and the 2nd time to fill it.
2109 */
2110 if ((out_resp->count_modes >= mode_count) && mode_count) {
2111 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002112 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002113 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002114 if (!drm_mode_expose_to_userspace(mode, file_priv))
2115 continue;
2116
Dave Airlief453ba02008-11-07 14:05:41 -08002117 drm_crtc_convert_to_umode(&u_mode, mode);
2118 if (copy_to_user(mode_ptr + copied,
2119 &u_mode, sizeof(u_mode))) {
2120 ret = -EFAULT;
2121 goto out;
2122 }
2123 copied++;
2124 }
2125 }
2126 out_resp->count_modes = mode_count;
2127
Rob Clark88a48e22014-12-18 16:01:50 -05002128 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002129 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2130 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2131 &out_resp->count_props);
2132 if (ret)
2133 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002134
2135 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2136 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002137 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002138 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2139 if (connector->encoder_ids[i] != 0) {
2140 if (put_user(connector->encoder_ids[i],
2141 encoder_ptr + copied)) {
2142 ret = -EFAULT;
2143 goto out;
2144 }
2145 copied++;
2146 }
2147 }
2148 }
2149 out_resp->count_encoders = encoders_count;
2150
2151out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002152 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Daniel Vetter7b240562012-12-12 00:35:33 +01002153 mutex_unlock(&dev->mode_config.mutex);
2154
Dave Airlief453ba02008-11-07 14:05:41 -08002155 return ret;
2156}
2157
Daniel Vetterabd69c52014-11-25 23:50:05 +01002158static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2159{
2160 struct drm_connector *connector;
2161 struct drm_device *dev = encoder->dev;
2162 bool uses_atomic = false;
2163
2164 /* For atomic drivers only state objects are synchronously updated and
2165 * protected by modeset locks, so check those first. */
2166 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2167 if (!connector->state)
2168 continue;
2169
2170 uses_atomic = true;
2171
2172 if (connector->state->best_encoder != encoder)
2173 continue;
2174
2175 return connector->state->crtc;
2176 }
2177
2178 /* Don't return stale data (e.g. pending async disable). */
2179 if (uses_atomic)
2180 return NULL;
2181
2182 return encoder->crtc;
2183}
2184
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002185/**
2186 * drm_mode_getencoder - get encoder configuration
2187 * @dev: drm device for the ioctl
2188 * @data: data pointer for the ioctl
2189 * @file_priv: drm file for the ioctl call
2190 *
2191 * Construct a encoder configuration structure to return to the user.
2192 *
2193 * Called by the user via ioctl.
2194 *
2195 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002196 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002197 */
Dave Airlief453ba02008-11-07 14:05:41 -08002198int drm_mode_getencoder(struct drm_device *dev, void *data,
2199 struct drm_file *file_priv)
2200{
2201 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002202 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002203 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002204
Dave Airliefb3b06c2011-02-08 13:55:21 +10002205 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2206 return -EINVAL;
2207
Rob Clarka2b34e22013-10-05 16:36:52 -04002208 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002209 if (!encoder)
2210 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002211
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002212 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002213 crtc = drm_encoder_get_crtc(encoder);
2214 if (crtc)
2215 enc_resp->crtc_id = crtc->base.id;
2216 else if (encoder->crtc)
Dave Airlief453ba02008-11-07 14:05:41 -08002217 enc_resp->crtc_id = encoder->crtc->base.id;
2218 else
2219 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002220 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2221
Dave Airlief453ba02008-11-07 14:05:41 -08002222 enc_resp->encoder_type = encoder->encoder_type;
2223 enc_resp->encoder_id = encoder->base.id;
2224 enc_resp->possible_crtcs = encoder->possible_crtcs;
2225 enc_resp->possible_clones = encoder->possible_clones;
2226
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002227 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002228}
2229
2230/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002231 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002232 * @dev: DRM device
2233 * @data: ioctl data
2234 * @file_priv: DRM file info
2235 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002236 * Construct a list of plane ids to return to the user.
2237 *
2238 * Called by the user via ioctl.
2239 *
2240 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002241 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002242 */
2243int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002244 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002245{
2246 struct drm_mode_get_plane_res *plane_resp = data;
2247 struct drm_mode_config *config;
2248 struct drm_plane *plane;
2249 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002250 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002251 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002252
2253 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2254 return -EINVAL;
2255
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002256 config = &dev->mode_config;
2257
Matt Roper681e7ec2014-04-01 15:22:42 -07002258 if (file_priv->universal_planes)
2259 num_planes = config->num_total_plane;
2260 else
2261 num_planes = config->num_overlay_plane;
2262
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002263 /*
2264 * This ioctl is called twice, once to determine how much space is
2265 * needed, and the 2nd time to fill it.
2266 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002267 if (num_planes &&
2268 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002269 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002270
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002271 /* Plane lists are invariant, no locking needed. */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002272 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002273 /*
2274 * Unless userspace set the 'universal planes'
2275 * capability bit, only advertise overlays.
2276 */
2277 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2278 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002279 continue;
2280
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002281 if (put_user(plane->base.id, plane_ptr + copied))
2282 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002283 copied++;
2284 }
2285 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002286 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002287
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002288 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002289}
2290
2291/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002292 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002293 * @dev: DRM device
2294 * @data: ioctl data
2295 * @file_priv: DRM file info
2296 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002297 * Construct a plane configuration structure to return to the user.
2298 *
2299 * Called by the user via ioctl.
2300 *
2301 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002302 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002303 */
2304int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002305 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002306{
2307 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002308 struct drm_plane *plane;
2309 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002310
2311 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2312 return -EINVAL;
2313
Rob Clarka2b34e22013-10-05 16:36:52 -04002314 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002315 if (!plane)
2316 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002317
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002318 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002319 if (plane->crtc)
2320 plane_resp->crtc_id = plane->crtc->base.id;
2321 else
2322 plane_resp->crtc_id = 0;
2323
2324 if (plane->fb)
2325 plane_resp->fb_id = plane->fb->base.id;
2326 else
2327 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002328 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002329
2330 plane_resp->plane_id = plane->base.id;
2331 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002332 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002333
2334 /*
2335 * This ioctl is called twice, once to determine how much space is
2336 * needed, and the 2nd time to fill it.
2337 */
2338 if (plane->format_count &&
2339 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002340 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002341 if (copy_to_user(format_ptr,
2342 plane->format_types,
2343 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002344 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002345 }
2346 }
2347 plane_resp->count_format_types = plane->format_count;
2348
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002349 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002350}
2351
Matt Roperb36552b2014-06-10 08:28:09 -07002352/*
2353 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002354 *
Matt Roperb36552b2014-06-10 08:28:09 -07002355 * Note that we assume an extra reference has already been taken on fb. If the
2356 * update fails, this reference will be dropped before return; if it succeeds,
2357 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002358 *
Matt Roperb36552b2014-06-10 08:28:09 -07002359 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002360 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002361static int __setplane_internal(struct drm_plane *plane,
2362 struct drm_crtc *crtc,
2363 struct drm_framebuffer *fb,
2364 int32_t crtc_x, int32_t crtc_y,
2365 uint32_t crtc_w, uint32_t crtc_h,
2366 /* src_{x,y,w,h} values are 16.16 fixed point */
2367 uint32_t src_x, uint32_t src_y,
2368 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002369{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002370 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002371 unsigned int fb_width, fb_height;
Thierry Reding2f763312014-10-13 12:45:57 +02002372 unsigned int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002373
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002374 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002375 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002376 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002377 ret = plane->funcs->disable_plane(plane);
2378 if (!ret) {
2379 plane->crtc = NULL;
2380 plane->fb = NULL;
2381 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002382 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002383 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002384 goto out;
2385 }
2386
Matt Roper7f994f32014-05-29 08:06:51 -07002387 /* Check whether this plane is usable on this CRTC */
2388 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2389 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2390 ret = -EINVAL;
2391 goto out;
2392 }
2393
Ville Syrjälä62443be2011-12-20 00:06:47 +02002394 /* Check whether this plane supports the fb pixel format. */
2395 for (i = 0; i < plane->format_count; i++)
2396 if (fb->pixel_format == plane->format_types[i])
2397 break;
2398 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002399 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2400 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002401 ret = -EINVAL;
2402 goto out;
2403 }
2404
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002405 fb_width = fb->width << 16;
2406 fb_height = fb->height << 16;
2407
2408 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002409 if (src_w > fb_width ||
2410 src_x > fb_width - src_w ||
2411 src_h > fb_height ||
2412 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002413 DRM_DEBUG_KMS("Invalid source coordinates "
2414 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002415 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2416 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2417 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2418 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002419 ret = -ENOSPC;
2420 goto out;
2421 }
2422
Daniel Vetter3d30a592014-07-27 13:42:42 +02002423 plane->old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002424 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002425 crtc_x, crtc_y, crtc_w, crtc_h,
2426 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002427 if (!ret) {
2428 plane->crtc = crtc;
2429 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002430 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002431 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002432 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002433 }
2434
2435out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002436 if (fb)
2437 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002438 if (plane->old_fb)
2439 drm_framebuffer_unreference(plane->old_fb);
2440 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002441
2442 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002443}
Matt Roperb36552b2014-06-10 08:28:09 -07002444
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002445static int setplane_internal(struct drm_plane *plane,
2446 struct drm_crtc *crtc,
2447 struct drm_framebuffer *fb,
2448 int32_t crtc_x, int32_t crtc_y,
2449 uint32_t crtc_w, uint32_t crtc_h,
2450 /* src_{x,y,w,h} values are 16.16 fixed point */
2451 uint32_t src_x, uint32_t src_y,
2452 uint32_t src_w, uint32_t src_h)
2453{
2454 int ret;
2455
2456 drm_modeset_lock_all(plane->dev);
2457 ret = __setplane_internal(plane, crtc, fb,
2458 crtc_x, crtc_y, crtc_w, crtc_h,
2459 src_x, src_y, src_w, src_h);
2460 drm_modeset_unlock_all(plane->dev);
2461
2462 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002463}
2464
2465/**
2466 * drm_mode_setplane - configure a plane's configuration
2467 * @dev: DRM device
2468 * @data: ioctl data*
2469 * @file_priv: DRM file info
2470 *
2471 * Set plane configuration, including placement, fb, scaling, and other factors.
2472 * Or pass a NULL fb to disable (planes may be disabled without providing a
2473 * valid crtc).
2474 *
2475 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002476 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002477 */
2478int drm_mode_setplane(struct drm_device *dev, void *data,
2479 struct drm_file *file_priv)
2480{
2481 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002482 struct drm_plane *plane;
2483 struct drm_crtc *crtc = NULL;
2484 struct drm_framebuffer *fb = NULL;
2485
2486 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2487 return -EINVAL;
2488
2489 /* Give drivers some help against integer overflows */
2490 if (plane_req->crtc_w > INT_MAX ||
2491 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2492 plane_req->crtc_h > INT_MAX ||
2493 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2494 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2495 plane_req->crtc_w, plane_req->crtc_h,
2496 plane_req->crtc_x, plane_req->crtc_y);
2497 return -ERANGE;
2498 }
2499
2500 /*
2501 * First, find the plane, crtc, and fb objects. If not available,
2502 * we don't bother to call the driver.
2503 */
Rob Clark933f6222014-11-25 20:33:11 -05002504 plane = drm_plane_find(dev, plane_req->plane_id);
2505 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002506 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2507 plane_req->plane_id);
2508 return -ENOENT;
2509 }
Matt Roperb36552b2014-06-10 08:28:09 -07002510
2511 if (plane_req->fb_id) {
2512 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2513 if (!fb) {
2514 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2515 plane_req->fb_id);
2516 return -ENOENT;
2517 }
2518
Rob Clark933f6222014-11-25 20:33:11 -05002519 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2520 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002521 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2522 plane_req->crtc_id);
2523 return -ENOENT;
2524 }
Matt Roperb36552b2014-06-10 08:28:09 -07002525 }
2526
Matt Roper161d0dc2014-06-10 08:28:10 -07002527 /*
2528 * setplane_internal will take care of deref'ing either the old or new
2529 * framebuffer depending on success.
2530 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002531 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002532 plane_req->crtc_x, plane_req->crtc_y,
2533 plane_req->crtc_w, plane_req->crtc_h,
2534 plane_req->src_x, plane_req->src_y,
2535 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002536}
2537
2538/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002539 * drm_mode_set_config_internal - helper to call ->set_config
2540 * @set: modeset config to set
2541 *
2542 * This is a little helper to wrap internal calls to the ->set_config driver
2543 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002544 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002545 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002546 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002547 */
2548int drm_mode_set_config_internal(struct drm_mode_set *set)
2549{
2550 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002551 struct drm_framebuffer *fb;
2552 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002553 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002554
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002555 /*
2556 * NOTE: ->set_config can also disable other crtcs (if we steal all
2557 * connectors from it), hence we need to refcount the fbs across all
2558 * crtcs. Atomic modeset will have saner semantics ...
2559 */
2560 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002561 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002562
Daniel Vetterb0d12322012-12-11 01:07:12 +01002563 fb = set->fb;
2564
2565 ret = crtc->funcs->set_config(set);
2566 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002567 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002568 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002569 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002570
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002571 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002572 if (tmp->primary->fb)
2573 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002574 if (tmp->primary->old_fb)
2575 drm_framebuffer_unreference(tmp->primary->old_fb);
2576 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002577 }
2578
2579 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002580}
2581EXPORT_SYMBOL(drm_mode_set_config_internal);
2582
Matt Roperaf936292014-04-01 15:22:34 -07002583/**
2584 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2585 * CRTC viewport
2586 * @crtc: CRTC that framebuffer will be displayed on
2587 * @x: x panning
2588 * @y: y panning
2589 * @mode: mode that framebuffer will be displayed under
2590 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002591 */
Matt Roperaf936292014-04-01 15:22:34 -07002592int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2593 int x, int y,
2594 const struct drm_display_mode *mode,
2595 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002596
2597{
2598 int hdisplay, vdisplay;
2599
2600 hdisplay = mode->hdisplay;
2601 vdisplay = mode->vdisplay;
2602
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002603 if (drm_mode_is_stereo(mode)) {
2604 struct drm_display_mode adjusted = *mode;
2605
2606 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2607 hdisplay = adjusted.crtc_hdisplay;
2608 vdisplay = adjusted.crtc_vdisplay;
2609 }
2610
Damien Lespiauc11e9282013-09-25 16:45:30 +01002611 if (crtc->invert_dimensions)
2612 swap(hdisplay, vdisplay);
2613
2614 if (hdisplay > fb->width ||
2615 vdisplay > fb->height ||
2616 x > fb->width - hdisplay ||
2617 y > fb->height - vdisplay) {
2618 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2619 fb->width, fb->height, hdisplay, vdisplay, x, y,
2620 crtc->invert_dimensions ? " (inverted)" : "");
2621 return -ENOSPC;
2622 }
2623
2624 return 0;
2625}
Matt Roperaf936292014-04-01 15:22:34 -07002626EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002627
Daniel Vetter2d13b672012-12-11 13:47:23 +01002628/**
Dave Airlief453ba02008-11-07 14:05:41 -08002629 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002630 * @dev: drm device for the ioctl
2631 * @data: data pointer for the ioctl
2632 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002633 *
Dave Airlief453ba02008-11-07 14:05:41 -08002634 * Build a new CRTC configuration based on user request.
2635 *
2636 * Called by the user via ioctl.
2637 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002638 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002639 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002640 */
2641int drm_mode_setcrtc(struct drm_device *dev, void *data,
2642 struct drm_file *file_priv)
2643{
2644 struct drm_mode_config *config = &dev->mode_config;
2645 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002646 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002647 struct drm_connector **connector_set = NULL, *connector;
2648 struct drm_framebuffer *fb = NULL;
2649 struct drm_display_mode *mode = NULL;
2650 struct drm_mode_set set;
2651 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002652 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002653 int i;
2654
Dave Airliefb3b06c2011-02-08 13:55:21 +10002655 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2656 return -EINVAL;
2657
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002658 /* For some reason crtc x/y offsets are signed internally. */
2659 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2660 return -ERANGE;
2661
Daniel Vetter84849902012-12-02 00:28:11 +01002662 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002663 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2664 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002665 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002666 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002667 goto out;
2668 }
Jerome Glisse94401062010-07-15 15:43:25 -04002669 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002670
2671 if (crtc_req->mode_valid) {
2672 /* If we have a mode we need a framebuffer. */
2673 /* If we pass -1, set the mode with the currently bound fb */
2674 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002675 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002676 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2677 ret = -EINVAL;
2678 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002679 }
Matt Roperf4510a22014-04-01 15:22:40 -07002680 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002681 /* Make refcounting symmetric with the lookup path. */
2682 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002683 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002684 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2685 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002686 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2687 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002688 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002689 goto out;
2690 }
Dave Airlief453ba02008-11-07 14:05:41 -08002691 }
2692
2693 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002694 if (!mode) {
2695 ret = -ENOMEM;
2696 goto out;
2697 }
2698
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002699 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2700 if (ret) {
2701 DRM_DEBUG_KMS("Invalid mode\n");
2702 goto out;
2703 }
2704
Dave Airlief453ba02008-11-07 14:05:41 -08002705 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002706
Damien Lespiauc11e9282013-09-25 16:45:30 +01002707 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2708 mode, fb);
2709 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002710 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002711
Dave Airlief453ba02008-11-07 14:05:41 -08002712 }
2713
2714 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002715 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002716 ret = -EINVAL;
2717 goto out;
2718 }
2719
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002720 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002721 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002722 crtc_req->count_connectors);
2723 ret = -EINVAL;
2724 goto out;
2725 }
2726
2727 if (crtc_req->count_connectors > 0) {
2728 u32 out_id;
2729
2730 /* Avoid unbounded kernel memory allocation */
2731 if (crtc_req->count_connectors > config->num_connector) {
2732 ret = -EINVAL;
2733 goto out;
2734 }
2735
Thierry Reding2f6c5382014-12-10 13:03:36 +01002736 connector_set = kmalloc_array(crtc_req->count_connectors,
2737 sizeof(struct drm_connector *),
2738 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002739 if (!connector_set) {
2740 ret = -ENOMEM;
2741 goto out;
2742 }
2743
2744 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002745 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002746 if (get_user(out_id, &set_connectors_ptr[i])) {
2747 ret = -EFAULT;
2748 goto out;
2749 }
2750
Rob Clarka2b34e22013-10-05 16:36:52 -04002751 connector = drm_connector_find(dev, out_id);
2752 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002753 DRM_DEBUG_KMS("Connector id %d unknown\n",
2754 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002755 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002756 goto out;
2757 }
Jerome Glisse94401062010-07-15 15:43:25 -04002758 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2759 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002760 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002761
2762 connector_set[i] = connector;
2763 }
2764 }
2765
2766 set.crtc = crtc;
2767 set.x = crtc_req->x;
2768 set.y = crtc_req->y;
2769 set.mode = mode;
2770 set.connectors = connector_set;
2771 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002772 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002773 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002774
2775out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002776 if (fb)
2777 drm_framebuffer_unreference(fb);
2778
Dave Airlief453ba02008-11-07 14:05:41 -08002779 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002780 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002781 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002782 return ret;
2783}
2784
Matt Roper161d0dc2014-06-10 08:28:10 -07002785/**
2786 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2787 * universal plane handler call
2788 * @crtc: crtc to update cursor for
2789 * @req: data pointer for the ioctl
2790 * @file_priv: drm file for the ioctl call
2791 *
2792 * Legacy cursor ioctl's work directly with driver buffer handles. To
2793 * translate legacy ioctl calls into universal plane handler calls, we need to
2794 * wrap the native buffer handle in a drm_framebuffer.
2795 *
2796 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2797 * buffer with a pitch of 4*width; the universal plane interface should be used
2798 * directly in cases where the hardware can support other buffer settings and
2799 * userspace wants to make use of these capabilities.
2800 *
2801 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002802 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002803 */
2804static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2805 struct drm_mode_cursor2 *req,
2806 struct drm_file *file_priv)
2807{
2808 struct drm_device *dev = crtc->dev;
2809 struct drm_framebuffer *fb = NULL;
2810 struct drm_mode_fb_cmd2 fbreq = {
2811 .width = req->width,
2812 .height = req->height,
2813 .pixel_format = DRM_FORMAT_ARGB8888,
2814 .pitches = { req->width * 4 },
2815 .handles = { req->handle },
2816 };
2817 int32_t crtc_x, crtc_y;
2818 uint32_t crtc_w = 0, crtc_h = 0;
2819 uint32_t src_w = 0, src_h = 0;
2820 int ret = 0;
2821
2822 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002823 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002824
2825 /*
2826 * Obtain fb we'll be using (either new or existing) and take an extra
2827 * reference to it if fb != null. setplane will take care of dropping
2828 * the reference if the plane update fails.
2829 */
2830 if (req->flags & DRM_MODE_CURSOR_BO) {
2831 if (req->handle) {
2832 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2833 if (IS_ERR(fb)) {
2834 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2835 return PTR_ERR(fb);
2836 }
2837
2838 drm_framebuffer_reference(fb);
2839 } else {
2840 fb = NULL;
2841 }
2842 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002843 fb = crtc->cursor->fb;
2844 if (fb)
2845 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002846 }
2847
2848 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2849 crtc_x = req->x;
2850 crtc_y = req->y;
2851 } else {
2852 crtc_x = crtc->cursor_x;
2853 crtc_y = crtc->cursor_y;
2854 }
2855
2856 if (fb) {
2857 crtc_w = fb->width;
2858 crtc_h = fb->height;
2859 src_w = fb->width << 16;
2860 src_h = fb->height << 16;
2861 }
2862
2863 /*
2864 * setplane_internal will take care of deref'ing either the old or new
2865 * framebuffer depending on success.
2866 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002867 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002868 crtc_x, crtc_y, crtc_w, crtc_h,
2869 0, 0, src_w, src_h);
2870
2871 /* Update successful; save new cursor position, if necessary */
2872 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2873 crtc->cursor_x = req->x;
2874 crtc->cursor_y = req->y;
2875 }
2876
2877 return ret;
2878}
2879
Dave Airlie4c813d42013-06-20 11:48:52 +10002880static int drm_mode_cursor_common(struct drm_device *dev,
2881 struct drm_mode_cursor2 *req,
2882 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002883{
Dave Airlief453ba02008-11-07 14:05:41 -08002884 struct drm_crtc *crtc;
2885 int ret = 0;
2886
Dave Airliefb3b06c2011-02-08 13:55:21 +10002887 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2888 return -EINVAL;
2889
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002890 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002891 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002892
Rob Clarka2b34e22013-10-05 16:36:52 -04002893 crtc = drm_crtc_find(dev, req->crtc_id);
2894 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002895 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002896 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002897 }
Dave Airlief453ba02008-11-07 14:05:41 -08002898
Matt Roper161d0dc2014-06-10 08:28:10 -07002899 /*
2900 * If this crtc has a universal cursor plane, call that plane's update
2901 * handler rather than using legacy cursor handlers.
2902 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002903 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002904 if (crtc->cursor) {
2905 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2906 goto out;
2907 }
2908
Dave Airlief453ba02008-11-07 14:05:41 -08002909 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002910 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002911 ret = -ENXIO;
2912 goto out;
2913 }
2914 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002915 if (crtc->funcs->cursor_set2)
2916 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2917 req->width, req->height, req->hot_x, req->hot_y);
2918 else
2919 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2920 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002921 }
2922
2923 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2924 if (crtc->funcs->cursor_move) {
2925 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2926 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002927 ret = -EFAULT;
2928 goto out;
2929 }
2930 }
2931out:
Daniel Vetterd059f652014-07-25 18:07:40 +02002932 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01002933
Dave Airlief453ba02008-11-07 14:05:41 -08002934 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002935
2936}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002937
2938
2939/**
2940 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2941 * @dev: drm device for the ioctl
2942 * @data: data pointer for the ioctl
2943 * @file_priv: drm file for the ioctl call
2944 *
2945 * Set the cursor configuration based on user request.
2946 *
2947 * Called by the user via ioctl.
2948 *
2949 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002950 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002951 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002952int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002953 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002954{
2955 struct drm_mode_cursor *req = data;
2956 struct drm_mode_cursor2 new_req;
2957
2958 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2959 new_req.hot_x = new_req.hot_y = 0;
2960
2961 return drm_mode_cursor_common(dev, &new_req, file_priv);
2962}
2963
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002964/**
2965 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2966 * @dev: drm device for the ioctl
2967 * @data: data pointer for the ioctl
2968 * @file_priv: drm file for the ioctl call
2969 *
2970 * Set the cursor configuration based on user request. This implements the 2nd
2971 * version of the cursor ioctl, which allows userspace to additionally specify
2972 * the hotspot of the pointer.
2973 *
2974 * Called by the user via ioctl.
2975 *
2976 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002977 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002978 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002979int drm_mode_cursor2_ioctl(struct drm_device *dev,
2980 void *data, struct drm_file *file_priv)
2981{
2982 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01002983
Dave Airlie4c813d42013-06-20 11:48:52 +10002984 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002985}
2986
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002987/**
2988 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2989 * @bpp: bits per pixels
2990 * @depth: bit depth per pixel
2991 *
2992 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2993 * Useful in fbdev emulation code, since that deals in those values.
2994 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002995uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2996{
2997 uint32_t fmt;
2998
2999 switch (bpp) {
3000 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003001 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003002 break;
3003 case 16:
3004 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003005 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003006 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003007 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003008 break;
3009 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003010 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003011 break;
3012 case 32:
3013 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003014 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003015 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003016 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003017 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003018 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003019 break;
3020 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003021 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3022 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003023 break;
3024 }
3025
3026 return fmt;
3027}
3028EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3029
Dave Airlief453ba02008-11-07 14:05:41 -08003030/**
3031 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003032 * @dev: drm device for the ioctl
3033 * @data: data pointer for the ioctl
3034 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003035 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003036 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003037 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003038 *
3039 * Called by the user via ioctl.
3040 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003041 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003042 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003043 */
3044int drm_mode_addfb(struct drm_device *dev,
3045 void *data, struct drm_file *file_priv)
3046{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003047 struct drm_mode_fb_cmd *or = data;
3048 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003049 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003050
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003051 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003052 r.fb_id = or->fb_id;
3053 r.width = or->width;
3054 r.height = or->height;
3055 r.pitches[0] = or->pitch;
3056 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3057 r.handles[0] = or->handle;
3058
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003059 ret = drm_mode_addfb2(dev, &r, file_priv);
3060 if (ret)
3061 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003062
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003063 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003064
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003065 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003066}
3067
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003068static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003069{
3070 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3071
3072 switch (format) {
3073 case DRM_FORMAT_C8:
3074 case DRM_FORMAT_RGB332:
3075 case DRM_FORMAT_BGR233:
3076 case DRM_FORMAT_XRGB4444:
3077 case DRM_FORMAT_XBGR4444:
3078 case DRM_FORMAT_RGBX4444:
3079 case DRM_FORMAT_BGRX4444:
3080 case DRM_FORMAT_ARGB4444:
3081 case DRM_FORMAT_ABGR4444:
3082 case DRM_FORMAT_RGBA4444:
3083 case DRM_FORMAT_BGRA4444:
3084 case DRM_FORMAT_XRGB1555:
3085 case DRM_FORMAT_XBGR1555:
3086 case DRM_FORMAT_RGBX5551:
3087 case DRM_FORMAT_BGRX5551:
3088 case DRM_FORMAT_ARGB1555:
3089 case DRM_FORMAT_ABGR1555:
3090 case DRM_FORMAT_RGBA5551:
3091 case DRM_FORMAT_BGRA5551:
3092 case DRM_FORMAT_RGB565:
3093 case DRM_FORMAT_BGR565:
3094 case DRM_FORMAT_RGB888:
3095 case DRM_FORMAT_BGR888:
3096 case DRM_FORMAT_XRGB8888:
3097 case DRM_FORMAT_XBGR8888:
3098 case DRM_FORMAT_RGBX8888:
3099 case DRM_FORMAT_BGRX8888:
3100 case DRM_FORMAT_ARGB8888:
3101 case DRM_FORMAT_ABGR8888:
3102 case DRM_FORMAT_RGBA8888:
3103 case DRM_FORMAT_BGRA8888:
3104 case DRM_FORMAT_XRGB2101010:
3105 case DRM_FORMAT_XBGR2101010:
3106 case DRM_FORMAT_RGBX1010102:
3107 case DRM_FORMAT_BGRX1010102:
3108 case DRM_FORMAT_ARGB2101010:
3109 case DRM_FORMAT_ABGR2101010:
3110 case DRM_FORMAT_RGBA1010102:
3111 case DRM_FORMAT_BGRA1010102:
3112 case DRM_FORMAT_YUYV:
3113 case DRM_FORMAT_YVYU:
3114 case DRM_FORMAT_UYVY:
3115 case DRM_FORMAT_VYUY:
3116 case DRM_FORMAT_AYUV:
3117 case DRM_FORMAT_NV12:
3118 case DRM_FORMAT_NV21:
3119 case DRM_FORMAT_NV16:
3120 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003121 case DRM_FORMAT_NV24:
3122 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003123 case DRM_FORMAT_YUV410:
3124 case DRM_FORMAT_YVU410:
3125 case DRM_FORMAT_YUV411:
3126 case DRM_FORMAT_YVU411:
3127 case DRM_FORMAT_YUV420:
3128 case DRM_FORMAT_YVU420:
3129 case DRM_FORMAT_YUV422:
3130 case DRM_FORMAT_YVU422:
3131 case DRM_FORMAT_YUV444:
3132 case DRM_FORMAT_YVU444:
3133 return 0;
3134 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003135 DRM_DEBUG_KMS("invalid pixel format %s\n",
3136 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003137 return -EINVAL;
3138 }
3139}
3140
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003141static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003142{
3143 int ret, hsub, vsub, num_planes, i;
3144
3145 ret = format_check(r);
3146 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003147 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3148 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003149 return ret;
3150 }
3151
3152 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3153 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3154 num_planes = drm_format_num_planes(r->pixel_format);
3155
3156 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003157 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003158 return -EINVAL;
3159 }
3160
3161 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003162 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003163 return -EINVAL;
3164 }
3165
3166 for (i = 0; i < num_planes; i++) {
3167 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003168 unsigned int height = r->height / (i != 0 ? vsub : 1);
3169 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003170
3171 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003172 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003173 return -EINVAL;
3174 }
3175
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003176 if ((uint64_t) width * cpp > UINT_MAX)
3177 return -ERANGE;
3178
3179 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3180 return -ERANGE;
3181
3182 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003183 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003184 return -EINVAL;
3185 }
3186 }
3187
3188 return 0;
3189}
3190
Matt Roperc394c2b2014-06-10 08:28:08 -07003191static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3192 struct drm_mode_fb_cmd2 *r,
3193 struct drm_file *file_priv)
3194{
3195 struct drm_mode_config *config = &dev->mode_config;
3196 struct drm_framebuffer *fb;
3197 int ret;
3198
3199 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3200 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3201 return ERR_PTR(-EINVAL);
3202 }
3203
3204 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3205 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3206 r->width, config->min_width, config->max_width);
3207 return ERR_PTR(-EINVAL);
3208 }
3209 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3210 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3211 r->height, config->min_height, config->max_height);
3212 return ERR_PTR(-EINVAL);
3213 }
3214
3215 ret = framebuffer_check(r);
3216 if (ret)
3217 return ERR_PTR(ret);
3218
3219 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3220 if (IS_ERR(fb)) {
3221 DRM_DEBUG_KMS("could not create framebuffer\n");
3222 return fb;
3223 }
3224
3225 mutex_lock(&file_priv->fbs_lock);
3226 r->fb_id = fb->base.id;
3227 list_add(&fb->filp_head, &file_priv->fbs);
3228 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3229 mutex_unlock(&file_priv->fbs_lock);
3230
3231 return fb;
3232}
3233
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003234/**
3235 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003236 * @dev: drm device for the ioctl
3237 * @data: data pointer for the ioctl
3238 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003239 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003240 * Add a new FB to the specified CRTC, given a user request with format. This is
3241 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3242 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003243 *
3244 * Called by the user via ioctl.
3245 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003246 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003247 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003248 */
3249int drm_mode_addfb2(struct drm_device *dev,
3250 void *data, struct drm_file *file_priv)
3251{
Dave Airlief453ba02008-11-07 14:05:41 -08003252 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003253
Dave Airliefb3b06c2011-02-08 13:55:21 +10003254 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3255 return -EINVAL;
3256
Matt Roperc394c2b2014-06-10 08:28:08 -07003257 fb = add_framebuffer_internal(dev, data, file_priv);
3258 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003259 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003260
Matt Roperc394c2b2014-06-10 08:28:08 -07003261 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003262}
3263
3264/**
3265 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003266 * @dev: drm device for the ioctl
3267 * @data: data pointer for the ioctl
3268 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003269 *
Dave Airlief453ba02008-11-07 14:05:41 -08003270 * Remove the FB specified by the user.
3271 *
3272 * Called by the user via ioctl.
3273 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003274 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003275 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003276 */
3277int drm_mode_rmfb(struct drm_device *dev,
3278 void *data, struct drm_file *file_priv)
3279{
Dave Airlief453ba02008-11-07 14:05:41 -08003280 struct drm_framebuffer *fb = NULL;
3281 struct drm_framebuffer *fbl = NULL;
3282 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003283 int found = 0;
3284
Dave Airliefb3b06c2011-02-08 13:55:21 +10003285 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3286 return -EINVAL;
3287
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003288 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003289 mutex_lock(&dev->mode_config.fb_lock);
3290 fb = __drm_framebuffer_lookup(dev, *id);
3291 if (!fb)
3292 goto fail_lookup;
3293
Dave Airlief453ba02008-11-07 14:05:41 -08003294 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3295 if (fb == fbl)
3296 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003297 if (!found)
3298 goto fail_lookup;
3299
3300 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3301 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003302
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003303 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003304 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003305 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003306
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003307 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003308
Daniel Vetter2b677e82012-12-10 21:16:05 +01003309 return 0;
3310
3311fail_lookup:
3312 mutex_unlock(&dev->mode_config.fb_lock);
3313 mutex_unlock(&file_priv->fbs_lock);
3314
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003315 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003316}
3317
3318/**
3319 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003320 * @dev: drm device for the ioctl
3321 * @data: data pointer for the ioctl
3322 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003323 *
Dave Airlief453ba02008-11-07 14:05:41 -08003324 * Lookup the FB given its ID and return info about it.
3325 *
3326 * Called by the user via ioctl.
3327 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003328 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003329 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003330 */
3331int drm_mode_getfb(struct drm_device *dev,
3332 void *data, struct drm_file *file_priv)
3333{
3334 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003335 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003336 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003337
Dave Airliefb3b06c2011-02-08 13:55:21 +10003338 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3339 return -EINVAL;
3340
Daniel Vetter786b99e2012-12-02 21:53:40 +01003341 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003342 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003343 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003344
3345 r->height = fb->height;
3346 r->width = fb->width;
3347 r->depth = fb->depth;
3348 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003349 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003350 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003351 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003352 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003353 ret = fb->funcs->create_handle(fb, file_priv,
3354 &r->handle);
3355 } else {
3356 /* GET_FB() is an unprivileged ioctl so we must not
3357 * return a buffer-handle to non-master processes! For
3358 * backwards-compatibility reasons, we cannot make
3359 * GET_FB() privileged, so just return an invalid handle
3360 * for non-masters. */
3361 r->handle = 0;
3362 ret = 0;
3363 }
3364 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003365 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003366 }
Dave Airlief453ba02008-11-07 14:05:41 -08003367
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003368 drm_framebuffer_unreference(fb);
3369
Dave Airlief453ba02008-11-07 14:05:41 -08003370 return ret;
3371}
3372
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003373/**
3374 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3375 * @dev: drm device for the ioctl
3376 * @data: data pointer for the ioctl
3377 * @file_priv: drm file for the ioctl call
3378 *
3379 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3380 * rectangle list. Generic userspace which does frontbuffer rendering must call
3381 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3382 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3383 *
3384 * Modesetting drivers which always update the frontbuffer do not need to
3385 * implement the corresponding ->dirty framebuffer callback.
3386 *
3387 * Called by the user via ioctl.
3388 *
3389 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003390 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003391 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003392int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3393 void *data, struct drm_file *file_priv)
3394{
3395 struct drm_clip_rect __user *clips_ptr;
3396 struct drm_clip_rect *clips = NULL;
3397 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003398 struct drm_framebuffer *fb;
3399 unsigned flags;
3400 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003401 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003402
Dave Airliefb3b06c2011-02-08 13:55:21 +10003403 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3404 return -EINVAL;
3405
Daniel Vetter786b99e2012-12-02 21:53:40 +01003406 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003407 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003408 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003409
3410 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003411 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003412
3413 if (!num_clips != !clips_ptr) {
3414 ret = -EINVAL;
3415 goto out_err1;
3416 }
3417
3418 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3419
3420 /* If userspace annotates copy, clips must come in pairs */
3421 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3422 ret = -EINVAL;
3423 goto out_err1;
3424 }
3425
3426 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003427 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3428 ret = -EINVAL;
3429 goto out_err1;
3430 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003431 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003432 if (!clips) {
3433 ret = -ENOMEM;
3434 goto out_err1;
3435 }
3436
3437 ret = copy_from_user(clips, clips_ptr,
3438 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003439 if (ret) {
3440 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003441 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003442 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003443 }
3444
3445 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003446 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3447 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003448 } else {
3449 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003450 }
3451
3452out_err2:
3453 kfree(clips);
3454out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003455 drm_framebuffer_unreference(fb);
3456
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003457 return ret;
3458}
3459
3460
Dave Airlief453ba02008-11-07 14:05:41 -08003461/**
3462 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003463 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003464 *
Dave Airlief453ba02008-11-07 14:05:41 -08003465 * Destroy all the FBs associated with @filp.
3466 *
3467 * Called by the user via ioctl.
3468 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003469 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003470 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003471 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003472void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003473{
Dave Airlief453ba02008-11-07 14:05:41 -08003474 struct drm_device *dev = priv->minor->dev;
3475 struct drm_framebuffer *fb, *tfb;
3476
Daniel Vetter1b116292014-09-24 21:51:06 +02003477 /*
3478 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003479 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003480 * avoid upsetting lockdep since the universal cursor code adds a
3481 * framebuffer while holding mutex locks.
3482 *
3483 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3484 * locks is impossible here since no one else but this function can get
3485 * at it any more.
3486 */
Dave Airlief453ba02008-11-07 14:05:41 -08003487 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003488
3489 mutex_lock(&dev->mode_config.fb_lock);
3490 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3491 __drm_framebuffer_unregister(dev, fb);
3492 mutex_unlock(&dev->mode_config.fb_lock);
3493
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003494 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003495
3496 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003497 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003498 }
Dave Airlief453ba02008-11-07 14:05:41 -08003499}
3500
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003501/**
3502 * drm_property_create - create a new property type
3503 * @dev: drm device
3504 * @flags: flags specifying the property type
3505 * @name: name of the property
3506 * @num_values: number of pre-defined values
3507 *
3508 * This creates a new generic drm property which can then be attached to a drm
3509 * object with drm_object_attach_property. The returned property object must be
3510 * freed with drm_property_destroy.
3511 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003512 * Note that the DRM core keeps a per-device list of properties and that, if
3513 * drm_mode_config_cleanup() is called, it will destroy all properties created
3514 * by the driver.
3515 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003516 * Returns:
3517 * A pointer to the newly created property on success, NULL on failure.
3518 */
Dave Airlief453ba02008-11-07 14:05:41 -08003519struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3520 const char *name, int num_values)
3521{
3522 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003523 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003524
3525 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3526 if (!property)
3527 return NULL;
3528
Rob Clark98f75de2014-05-30 11:37:03 -04003529 property->dev = dev;
3530
Dave Airlief453ba02008-11-07 14:05:41 -08003531 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003532 property->values = kcalloc(num_values, sizeof(uint64_t),
3533 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003534 if (!property->values)
3535 goto fail;
3536 }
3537
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003538 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3539 if (ret)
3540 goto fail;
3541
Dave Airlief453ba02008-11-07 14:05:41 -08003542 property->flags = flags;
3543 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003544 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003545
Vinson Lee471dd2e2011-11-10 11:55:40 -08003546 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003547 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003548 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3549 }
Dave Airlief453ba02008-11-07 14:05:41 -08003550
3551 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003552
3553 WARN_ON(!drm_property_type_valid(property));
3554
Dave Airlief453ba02008-11-07 14:05:41 -08003555 return property;
3556fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003557 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003558 kfree(property);
3559 return NULL;
3560}
3561EXPORT_SYMBOL(drm_property_create);
3562
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003563/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003564 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003565 * @dev: drm device
3566 * @flags: flags specifying the property type
3567 * @name: name of the property
3568 * @props: enumeration lists with property values
3569 * @num_values: number of pre-defined values
3570 *
3571 * This creates a new generic drm property which can then be attached to a drm
3572 * object with drm_object_attach_property. The returned property object must be
3573 * freed with drm_property_destroy.
3574 *
3575 * Userspace is only allowed to set one of the predefined values for enumeration
3576 * properties.
3577 *
3578 * Returns:
3579 * A pointer to the newly created property on success, NULL on failure.
3580 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003581struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3582 const char *name,
3583 const struct drm_prop_enum_list *props,
3584 int num_values)
3585{
3586 struct drm_property *property;
3587 int i, ret;
3588
3589 flags |= DRM_MODE_PROP_ENUM;
3590
3591 property = drm_property_create(dev, flags, name, num_values);
3592 if (!property)
3593 return NULL;
3594
3595 for (i = 0; i < num_values; i++) {
3596 ret = drm_property_add_enum(property, i,
3597 props[i].type,
3598 props[i].name);
3599 if (ret) {
3600 drm_property_destroy(dev, property);
3601 return NULL;
3602 }
3603 }
3604
3605 return property;
3606}
3607EXPORT_SYMBOL(drm_property_create_enum);
3608
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003609/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003610 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003611 * @dev: drm device
3612 * @flags: flags specifying the property type
3613 * @name: name of the property
3614 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003615 * @num_props: size of the @props array
3616 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003617 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003618 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003619 * object with drm_object_attach_property. The returned property object must be
3620 * freed with drm_property_destroy.
3621 *
3622 * Compared to plain enumeration properties userspace is allowed to set any
3623 * or'ed together combination of the predefined property bitflag values
3624 *
3625 * Returns:
3626 * A pointer to the newly created property on success, NULL on failure.
3627 */
Rob Clark49e27542012-05-17 02:23:26 -06003628struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3629 int flags, const char *name,
3630 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303631 int num_props,
3632 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003633{
3634 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303635 int i, ret, index = 0;
3636 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003637
3638 flags |= DRM_MODE_PROP_BITMASK;
3639
3640 property = drm_property_create(dev, flags, name, num_values);
3641 if (!property)
3642 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303643 for (i = 0; i < num_props; i++) {
3644 if (!(supported_bits & (1ULL << props[i].type)))
3645 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003646
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303647 if (WARN_ON(index >= num_values)) {
3648 drm_property_destroy(dev, property);
3649 return NULL;
3650 }
3651
3652 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003653 props[i].type,
3654 props[i].name);
3655 if (ret) {
3656 drm_property_destroy(dev, property);
3657 return NULL;
3658 }
3659 }
3660
3661 return property;
3662}
3663EXPORT_SYMBOL(drm_property_create_bitmask);
3664
Rob Clarkebc44cf2012-09-12 22:22:31 -05003665static struct drm_property *property_create_range(struct drm_device *dev,
3666 int flags, const char *name,
3667 uint64_t min, uint64_t max)
3668{
3669 struct drm_property *property;
3670
3671 property = drm_property_create(dev, flags, name, 2);
3672 if (!property)
3673 return NULL;
3674
3675 property->values[0] = min;
3676 property->values[1] = max;
3677
3678 return property;
3679}
3680
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003681/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003682 * drm_property_create_range - create a new ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003683 * @dev: drm device
3684 * @flags: flags specifying the property type
3685 * @name: name of the property
3686 * @min: minimum value of the property
3687 * @max: maximum value of the property
3688 *
3689 * This creates a new generic drm property which can then be attached to a drm
3690 * object with drm_object_attach_property. The returned property object must be
3691 * freed with drm_property_destroy.
3692 *
Masanari Iida32197aa2014-10-20 23:53:13 +09003693 * Userspace is allowed to set any integer value in the (min, max) range
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003694 * inclusive.
3695 *
3696 * Returns:
3697 * A pointer to the newly created property on success, NULL on failure.
3698 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003699struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3700 const char *name,
3701 uint64_t min, uint64_t max)
3702{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003703 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3704 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003705}
3706EXPORT_SYMBOL(drm_property_create_range);
3707
Rob Clarkebc44cf2012-09-12 22:22:31 -05003708struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3709 int flags, const char *name,
3710 int64_t min, int64_t max)
3711{
3712 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3713 name, I642U64(min), I642U64(max));
3714}
3715EXPORT_SYMBOL(drm_property_create_signed_range);
3716
Rob Clark98f75de2014-05-30 11:37:03 -04003717struct drm_property *drm_property_create_object(struct drm_device *dev,
3718 int flags, const char *name, uint32_t type)
3719{
3720 struct drm_property *property;
3721
3722 flags |= DRM_MODE_PROP_OBJECT;
3723
3724 property = drm_property_create(dev, flags, name, 1);
3725 if (!property)
3726 return NULL;
3727
3728 property->values[0] = type;
3729
3730 return property;
3731}
3732EXPORT_SYMBOL(drm_property_create_object);
3733
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003734/**
3735 * drm_property_add_enum - add a possible value to an enumeration property
3736 * @property: enumeration property to change
3737 * @index: index of the new enumeration
3738 * @value: value of the new enumeration
3739 * @name: symbolic name of the new enumeration
3740 *
3741 * This functions adds enumerations to a property.
3742 *
3743 * It's use is deprecated, drivers should use one of the more specific helpers
3744 * to directly create the property with all enumerations already attached.
3745 *
3746 * Returns:
3747 * Zero on success, error code on failure.
3748 */
Dave Airlief453ba02008-11-07 14:05:41 -08003749int drm_property_add_enum(struct drm_property *property, int index,
3750 uint64_t value, const char *name)
3751{
3752 struct drm_property_enum *prop_enum;
3753
Rob Clark5ea22f22014-05-30 11:34:01 -04003754 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3755 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003756 return -EINVAL;
3757
3758 /*
3759 * Bitmask enum properties have the additional constraint of values
3760 * from 0 to 63
3761 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003762 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3763 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003764 return -EINVAL;
3765
Daniel Vetter3758b342014-11-19 18:38:10 +01003766 if (!list_empty(&property->enum_list)) {
3767 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003768 if (prop_enum->value == value) {
3769 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3770 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3771 return 0;
3772 }
3773 }
3774 }
3775
3776 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3777 if (!prop_enum)
3778 return -ENOMEM;
3779
3780 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3781 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3782 prop_enum->value = value;
3783
3784 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01003785 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003786 return 0;
3787}
3788EXPORT_SYMBOL(drm_property_add_enum);
3789
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003790/**
3791 * drm_property_destroy - destroy a drm property
3792 * @dev: drm device
3793 * @property: property to destry
3794 *
3795 * This function frees a property including any attached resources like
3796 * enumeration values.
3797 */
Dave Airlief453ba02008-11-07 14:05:41 -08003798void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3799{
3800 struct drm_property_enum *prop_enum, *pt;
3801
Daniel Vetter3758b342014-11-19 18:38:10 +01003802 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003803 list_del(&prop_enum->head);
3804 kfree(prop_enum);
3805 }
3806
3807 if (property->num_values)
3808 kfree(property->values);
3809 drm_mode_object_put(dev, &property->base);
3810 list_del(&property->head);
3811 kfree(property);
3812}
3813EXPORT_SYMBOL(drm_property_destroy);
3814
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003815/**
3816 * drm_object_attach_property - attach a property to a modeset object
3817 * @obj: drm modeset object
3818 * @property: property to attach
3819 * @init_val: initial value of the property
3820 *
3821 * This attaches the given property to the modeset object with the given initial
3822 * value. Currently this function cannot fail since the properties are stored in
3823 * a statically sized array.
3824 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003825void drm_object_attach_property(struct drm_mode_object *obj,
3826 struct drm_property *property,
3827 uint64_t init_val)
3828{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003829 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003830
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003831 if (count == DRM_OBJECT_MAX_PROPERTY) {
3832 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3833 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3834 "you see this message on the same object type.\n",
3835 obj->type);
3836 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003837 }
3838
Rob Clarkb17cd752014-12-16 18:05:30 -05003839 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003840 obj->properties->values[count] = init_val;
3841 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05003842 if (property->flags & DRM_MODE_PROP_ATOMIC)
3843 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003844}
3845EXPORT_SYMBOL(drm_object_attach_property);
3846
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003847/**
3848 * drm_object_property_set_value - set the value of a property
3849 * @obj: drm mode object to set property value for
3850 * @property: property to set
3851 * @val: value the property should be set to
3852 *
3853 * This functions sets a given property on a given object. This function only
3854 * changes the software state of the property, it does not call into the
3855 * driver's ->set_property callback.
3856 *
3857 * Returns:
3858 * Zero on success, error code on failure.
3859 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003860int drm_object_property_set_value(struct drm_mode_object *obj,
3861 struct drm_property *property, uint64_t val)
3862{
3863 int i;
3864
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003865 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05003866 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003867 obj->properties->values[i] = val;
3868 return 0;
3869 }
3870 }
3871
3872 return -EINVAL;
3873}
3874EXPORT_SYMBOL(drm_object_property_set_value);
3875
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003876/**
3877 * drm_object_property_get_value - retrieve the value of a property
3878 * @obj: drm mode object to get property value from
3879 * @property: property to retrieve
3880 * @val: storage for the property value
3881 *
3882 * This function retrieves the softare state of the given property for the given
3883 * property. Since there is no driver callback to retrieve the current property
3884 * value this might be out of sync with the hardware, depending upon the driver
3885 * and property.
3886 *
3887 * Returns:
3888 * Zero on success, error code on failure.
3889 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003890int drm_object_property_get_value(struct drm_mode_object *obj,
3891 struct drm_property *property, uint64_t *val)
3892{
3893 int i;
3894
Rob Clark88a48e22014-12-18 16:01:50 -05003895 /* read-only properties bypass atomic mechanism and still store
3896 * their value in obj->properties->values[].. mostly to avoid
3897 * having to deal w/ EDID and similar props in atomic paths:
3898 */
3899 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
3900 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
3901 return drm_atomic_get_property(obj, property, val);
3902
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003903 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05003904 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003905 *val = obj->properties->values[i];
3906 return 0;
3907 }
3908 }
3909
3910 return -EINVAL;
3911}
3912EXPORT_SYMBOL(drm_object_property_get_value);
3913
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003914/**
Daniel Vetter1a498632014-11-19 18:38:09 +01003915 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003916 * @dev: DRM device
3917 * @data: ioctl data
3918 * @file_priv: DRM file info
3919 *
Daniel Vetter1a498632014-11-19 18:38:09 +01003920 * This function retrieves the metadata for a given property, like the different
3921 * possible values for an enum property or the limits for a range property.
3922 *
3923 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003924 *
3925 * Called by the user via ioctl.
3926 *
3927 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003928 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003929 */
Dave Airlief453ba02008-11-07 14:05:41 -08003930int drm_mode_getproperty_ioctl(struct drm_device *dev,
3931 void *data, struct drm_file *file_priv)
3932{
Dave Airlief453ba02008-11-07 14:05:41 -08003933 struct drm_mode_get_property *out_resp = data;
3934 struct drm_property *property;
3935 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003936 int value_count = 0;
3937 int ret = 0, i;
3938 int copied;
3939 struct drm_property_enum *prop_enum;
3940 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003941 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003942
Dave Airliefb3b06c2011-02-08 13:55:21 +10003943 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3944 return -EINVAL;
3945
Daniel Vetter84849902012-12-02 00:28:11 +01003946 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003947 property = drm_property_find(dev, out_resp->prop_id);
3948 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003949 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003950 goto done;
3951 }
Dave Airlief453ba02008-11-07 14:05:41 -08003952
Rob Clark5ea22f22014-05-30 11:34:01 -04003953 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3954 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01003955 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08003956 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08003957 }
3958
3959 value_count = property->num_values;
3960
3961 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3962 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3963 out_resp->flags = property->flags;
3964
3965 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003966 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003967 for (i = 0; i < value_count; i++) {
3968 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3969 ret = -EFAULT;
3970 goto done;
3971 }
3972 }
3973 }
3974 out_resp->count_values = value_count;
3975
Rob Clark5ea22f22014-05-30 11:34:01 -04003976 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3977 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003978 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3979 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003980 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01003981 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003982
3983 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3984 ret = -EFAULT;
3985 goto done;
3986 }
3987
3988 if (copy_to_user(&enum_ptr[copied].name,
3989 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3990 ret = -EFAULT;
3991 goto done;
3992 }
3993 copied++;
3994 }
3995 }
3996 out_resp->count_enum_blobs = enum_count;
3997 }
3998
Daniel Vetter3758b342014-11-19 18:38:10 +01003999 /*
4000 * NOTE: The idea seems to have been to use this to read all the blob
4001 * property values. But nothing ever added them to the corresponding
4002 * list, userspace always used the special-purpose get_blob ioctl to
4003 * read the value for a blob property. It also doesn't make a lot of
4004 * sense to return values here when everything else is just metadata for
4005 * the property itself.
4006 */
4007 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4008 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004009done:
Daniel Vetter84849902012-12-02 00:28:11 +01004010 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004011 return ret;
4012}
4013
Thierry Redingecbbe592014-05-13 11:36:13 +02004014static struct drm_property_blob *
4015drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004016 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004017{
4018 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004019 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004020
4021 if (!length || !data)
4022 return NULL;
4023
4024 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4025 if (!blob)
4026 return NULL;
4027
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004028 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4029 if (ret) {
4030 kfree(blob);
4031 return NULL;
4032 }
4033
Dave Airlief453ba02008-11-07 14:05:41 -08004034 blob->length = length;
4035
4036 memcpy(blob->data, data, length);
4037
Dave Airlief453ba02008-11-07 14:05:41 -08004038 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
4039 return blob;
4040}
4041
4042static void drm_property_destroy_blob(struct drm_device *dev,
4043 struct drm_property_blob *blob)
4044{
4045 drm_mode_object_put(dev, &blob->base);
4046 list_del(&blob->head);
4047 kfree(blob);
4048}
4049
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004050/**
4051 * drm_mode_getblob_ioctl - get the contents of a blob property value
4052 * @dev: DRM device
4053 * @data: ioctl data
4054 * @file_priv: DRM file info
4055 *
4056 * This function retrieves the contents of a blob property. The value stored in
4057 * an object's blob property is just a normal modeset object id.
4058 *
4059 * Called by the user via ioctl.
4060 *
4061 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004062 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004063 */
Dave Airlief453ba02008-11-07 14:05:41 -08004064int drm_mode_getblob_ioctl(struct drm_device *dev,
4065 void *data, struct drm_file *file_priv)
4066{
Dave Airlief453ba02008-11-07 14:05:41 -08004067 struct drm_mode_get_blob *out_resp = data;
4068 struct drm_property_blob *blob;
4069 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004070 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004071
Dave Airliefb3b06c2011-02-08 13:55:21 +10004072 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4073 return -EINVAL;
4074
Daniel Vetter84849902012-12-02 00:28:11 +01004075 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004076 blob = drm_property_blob_find(dev, out_resp->blob_id);
4077 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004078 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004079 goto done;
4080 }
Dave Airlief453ba02008-11-07 14:05:41 -08004081
4082 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004083 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004084 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004085 ret = -EFAULT;
4086 goto done;
4087 }
4088 }
4089 out_resp->length = blob->length;
4090
4091done:
Daniel Vetter84849902012-12-02 00:28:11 +01004092 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004093 return ret;
4094}
4095
Dave Airliecc7096f2014-10-22 12:03:04 +10004096/**
4097 * drm_mode_connector_set_path_property - set tile property on connector
4098 * @connector: connector to set property on.
4099 * @path: path to use for property.
4100 *
4101 * This creates a property to expose to userspace to specify a
4102 * connector path. This is mainly used for DisplayPort MST where
4103 * connectors have a topology and we want to allow userspace to give
4104 * them more meaningful names.
4105 *
4106 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004107 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004108 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004109int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004110 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004111{
4112 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004113 size_t size = strlen(path) + 1;
4114 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004115
4116 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4117 size, path);
4118 if (!connector->path_blob_ptr)
4119 return -EINVAL;
4120
4121 ret = drm_object_property_set_value(&connector->base,
4122 dev->mode_config.path_property,
4123 connector->path_blob_ptr->base.id);
4124 return ret;
4125}
4126EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4127
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004128/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004129 * drm_mode_connector_set_tile_property - set tile property on connector
4130 * @connector: connector to set property on.
4131 *
4132 * This looks up the tile information for a connector, and creates a
4133 * property for userspace to parse if it exists. The property is of
4134 * the form of 8 integers using ':' as a separator.
4135 *
4136 * Returns:
4137 * Zero on success, errno on failure.
4138 */
4139int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4140{
4141 struct drm_device *dev = connector->dev;
4142 int ret, size;
4143 char tile[256];
4144
4145 if (connector->tile_blob_ptr)
4146 drm_property_destroy_blob(dev, connector->tile_blob_ptr);
4147
4148 if (!connector->has_tile) {
4149 connector->tile_blob_ptr = NULL;
4150 ret = drm_object_property_set_value(&connector->base,
4151 dev->mode_config.tile_property, 0);
4152 return ret;
4153 }
4154
4155 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4156 connector->tile_group->id, connector->tile_is_single_monitor,
4157 connector->num_h_tile, connector->num_v_tile,
4158 connector->tile_h_loc, connector->tile_v_loc,
4159 connector->tile_h_size, connector->tile_v_size);
4160 size = strlen(tile) + 1;
4161
4162 connector->tile_blob_ptr = drm_property_create_blob(connector->dev,
4163 size, tile);
4164 if (!connector->tile_blob_ptr)
4165 return -EINVAL;
4166
4167 ret = drm_object_property_set_value(&connector->base,
4168 dev->mode_config.tile_property,
4169 connector->tile_blob_ptr->base.id);
4170 return ret;
4171}
4172EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4173
4174/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004175 * drm_mode_connector_update_edid_property - update the edid property of a connector
4176 * @connector: drm connector
4177 * @edid: new value of the edid property
4178 *
4179 * This function creates a new blob modeset object and assigns its id to the
4180 * connector's edid property.
4181 *
4182 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004183 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004184 */
Dave Airlief453ba02008-11-07 14:05:41 -08004185int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004186 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004187{
4188 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004189 size_t size;
4190 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004191
Thomas Wood4cf2b282014-06-18 17:52:33 +01004192 /* ignore requests to set edid when overridden */
4193 if (connector->override_edid)
4194 return 0;
4195
Dave Airlief453ba02008-11-07 14:05:41 -08004196 if (connector->edid_blob_ptr)
4197 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4198
4199 /* Delete edid, when there is none. */
4200 if (!edid) {
4201 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05004202 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08004203 return ret;
4204 }
4205
Adam Jackson7466f4c2010-03-29 21:43:23 +00004206 size = EDID_LENGTH * (1 + edid->extensions);
4207 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4208 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00004209 if (!connector->edid_blob_ptr)
4210 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08004211
Rob Clark58495562012-10-11 20:50:56 -05004212 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08004213 dev->mode_config.edid_property,
4214 connector->edid_blob_ptr->base.id);
4215
4216 return ret;
4217}
4218EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4219
Rob Clark3843e712014-12-16 18:05:29 -05004220/* Some properties could refer to dynamic refcnt'd objects, or things that
4221 * need special locking to handle lifetime issues (ie. to ensure the prop
4222 * value doesn't become invalid part way through the property update due to
4223 * race). The value returned by reference via 'obj' should be passed back
4224 * to drm_property_change_valid_put() after the property is set (and the
4225 * object to which the property is attached has a chance to take it's own
4226 * reference).
4227 */
4228static bool drm_property_change_valid_get(struct drm_property *property,
4229 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004230{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004231 int i;
4232
Paulo Zanoni26a34812012-05-15 18:08:59 -03004233 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4234 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004235
Rob Clark3843e712014-12-16 18:05:29 -05004236 *ref = NULL;
4237
Rob Clark5ea22f22014-05-30 11:34:01 -04004238 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004239 if (value < property->values[0] || value > property->values[1])
4240 return false;
4241 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004242 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4243 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004244
Rob Clarkebc44cf2012-09-12 22:22:31 -05004245 if (svalue < U642I64(property->values[0]) ||
4246 svalue > U642I64(property->values[1]))
4247 return false;
4248 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004249 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004250 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004251
Rob Clark49e27542012-05-17 02:23:26 -06004252 for (i = 0; i < property->num_values; i++)
4253 valid_mask |= (1ULL << property->values[i]);
4254 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004255 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004256 /* Only the driver knows */
4257 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004258 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004259 /* a zero value for an object property translates to null: */
4260 if (value == 0)
4261 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004262
4263 /* handle refcnt'd objects specially: */
4264 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4265 struct drm_framebuffer *fb;
4266 fb = drm_framebuffer_lookup(property->dev, value);
4267 if (fb) {
4268 *ref = &fb->base;
4269 return true;
4270 } else {
4271 return false;
4272 }
4273 } else {
4274 return _object_find(property->dev, value, property->values[0]) != NULL;
4275 }
Paulo Zanoni26a34812012-05-15 18:08:59 -03004276 } else {
4277 int i;
4278 for (i = 0; i < property->num_values; i++)
4279 if (property->values[i] == value)
4280 return true;
4281 return false;
4282 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004283
4284 for (i = 0; i < property->num_values; i++)
4285 if (property->values[i] == value)
4286 return true;
4287 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004288}
4289
Rob Clark3843e712014-12-16 18:05:29 -05004290static void drm_property_change_valid_put(struct drm_property *property,
4291 struct drm_mode_object *ref)
4292{
4293 if (!ref)
4294 return;
4295
4296 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4297 if (property->values[0] == DRM_MODE_OBJECT_FB)
4298 drm_framebuffer_unreference(obj_to_fb(ref));
4299 }
4300}
4301
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004302/**
4303 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4304 * @dev: DRM device
4305 * @data: ioctl data
4306 * @file_priv: DRM file info
4307 *
4308 * This function sets the current value for a connectors's property. It also
4309 * calls into a driver's ->set_property callback to update the hardware state
4310 *
4311 * Called by the user via ioctl.
4312 *
4313 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004314 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004315 */
Dave Airlief453ba02008-11-07 14:05:41 -08004316int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4317 void *data, struct drm_file *file_priv)
4318{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004319 struct drm_mode_connector_set_property *conn_set_prop = data;
4320 struct drm_mode_obj_set_property obj_set_prop = {
4321 .value = conn_set_prop->value,
4322 .prop_id = conn_set_prop->prop_id,
4323 .obj_id = conn_set_prop->connector_id,
4324 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4325 };
Dave Airlief453ba02008-11-07 14:05:41 -08004326
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004327 /* It does all the locking and checking we need */
4328 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004329}
4330
Paulo Zanonic5431882012-05-15 18:09:02 -03004331static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4332 struct drm_property *property,
4333 uint64_t value)
4334{
4335 int ret = -EINVAL;
4336 struct drm_connector *connector = obj_to_connector(obj);
4337
4338 /* Do DPMS ourselves */
4339 if (property == connector->dev->mode_config.dpms_property) {
4340 if (connector->funcs->dpms)
4341 (*connector->funcs->dpms)(connector, (int)value);
4342 ret = 0;
4343 } else if (connector->funcs->set_property)
4344 ret = connector->funcs->set_property(connector, property, value);
4345
4346 /* store the property value if successful */
4347 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004348 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004349 return ret;
4350}
4351
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004352static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4353 struct drm_property *property,
4354 uint64_t value)
4355{
4356 int ret = -EINVAL;
4357 struct drm_crtc *crtc = obj_to_crtc(obj);
4358
4359 if (crtc->funcs->set_property)
4360 ret = crtc->funcs->set_property(crtc, property, value);
4361 if (!ret)
4362 drm_object_property_set_value(obj, property, value);
4363
4364 return ret;
4365}
4366
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004367/**
4368 * drm_mode_plane_set_obj_prop - set the value of a property
4369 * @plane: drm plane object to set property value for
4370 * @property: property to set
4371 * @value: value the property should be set to
4372 *
4373 * This functions sets a given property on a given plane object. This function
4374 * calls the driver's ->set_property callback and changes the software state of
4375 * the property if the callback succeeds.
4376 *
4377 * Returns:
4378 * Zero on success, error code on failure.
4379 */
4380int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4381 struct drm_property *property,
4382 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004383{
4384 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004385 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004386
4387 if (plane->funcs->set_property)
4388 ret = plane->funcs->set_property(plane, property, value);
4389 if (!ret)
4390 drm_object_property_set_value(obj, property, value);
4391
4392 return ret;
4393}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004394EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004395
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004396/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004397 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004398 * @dev: DRM device
4399 * @data: ioctl data
4400 * @file_priv: DRM file info
4401 *
4402 * This function retrieves the current value for an object's property. Compared
4403 * to the connector specific ioctl this one is extended to also work on crtc and
4404 * plane objects.
4405 *
4406 * Called by the user via ioctl.
4407 *
4408 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004409 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004410 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004411int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4412 struct drm_file *file_priv)
4413{
4414 struct drm_mode_obj_get_properties *arg = data;
4415 struct drm_mode_object *obj;
4416 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004417
4418 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4419 return -EINVAL;
4420
Daniel Vetter84849902012-12-02 00:28:11 +01004421 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004422
4423 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4424 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004425 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004426 goto out;
4427 }
4428 if (!obj->properties) {
4429 ret = -EINVAL;
4430 goto out;
4431 }
4432
Rob Clark88a48e22014-12-18 16:01:50 -05004433 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05004434 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4435 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4436 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03004437
Paulo Zanonic5431882012-05-15 18:09:02 -03004438out:
Daniel Vetter84849902012-12-02 00:28:11 +01004439 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004440 return ret;
4441}
4442
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004443/**
4444 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4445 * @dev: DRM device
4446 * @data: ioctl data
4447 * @file_priv: DRM file info
4448 *
4449 * This function sets the current value for an object's property. It also calls
4450 * into a driver's ->set_property callback to update the hardware state.
4451 * Compared to the connector specific ioctl this one is extended to also work on
4452 * crtc and plane objects.
4453 *
4454 * Called by the user via ioctl.
4455 *
4456 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004457 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004458 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004459int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4460 struct drm_file *file_priv)
4461{
4462 struct drm_mode_obj_set_property *arg = data;
4463 struct drm_mode_object *arg_obj;
4464 struct drm_mode_object *prop_obj;
4465 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05004466 int i, ret = -EINVAL;
4467 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004468
4469 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4470 return -EINVAL;
4471
Daniel Vetter84849902012-12-02 00:28:11 +01004472 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004473
4474 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004475 if (!arg_obj) {
4476 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004477 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004478 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004479 if (!arg_obj->properties)
4480 goto out;
4481
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004482 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05004483 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03004484 break;
4485
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004486 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004487 goto out;
4488
4489 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4490 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004491 if (!prop_obj) {
4492 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004493 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004494 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004495 property = obj_to_property(prop_obj);
4496
Rob Clark3843e712014-12-16 18:05:29 -05004497 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03004498 goto out;
4499
4500 switch (arg_obj->type) {
4501 case DRM_MODE_OBJECT_CONNECTOR:
4502 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4503 arg->value);
4504 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004505 case DRM_MODE_OBJECT_CRTC:
4506 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4507 break;
Rob Clark4d939142012-05-17 02:23:27 -06004508 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004509 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4510 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06004511 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004512 }
4513
Rob Clark3843e712014-12-16 18:05:29 -05004514 drm_property_change_valid_put(property, ref);
4515
Paulo Zanonic5431882012-05-15 18:09:02 -03004516out:
Daniel Vetter84849902012-12-02 00:28:11 +01004517 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004518 return ret;
4519}
4520
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004521/**
4522 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4523 * @connector: connector to attach
4524 * @encoder: encoder to attach @connector to
4525 *
4526 * This function links up a connector to an encoder. Note that the routing
4527 * restrictions between encoders and crtcs are exposed to userspace through the
4528 * possible_clones and possible_crtcs bitmasks.
4529 *
4530 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004531 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004532 */
Dave Airlief453ba02008-11-07 14:05:41 -08004533int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4534 struct drm_encoder *encoder)
4535{
4536 int i;
4537
4538 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4539 if (connector->encoder_ids[i] == 0) {
4540 connector->encoder_ids[i] = encoder->base.id;
4541 return 0;
4542 }
4543 }
4544 return -ENOMEM;
4545}
4546EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4547
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004548/**
4549 * drm_mode_crtc_set_gamma_size - set the gamma table size
4550 * @crtc: CRTC to set the gamma table size for
4551 * @gamma_size: size of the gamma table
4552 *
4553 * Drivers which support gamma tables should set this to the supported gamma
4554 * table size when initializing the CRTC. Currently the drm core only supports a
4555 * fixed gamma table size.
4556 *
4557 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004558 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004559 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004560int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004561 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004562{
4563 crtc->gamma_size = gamma_size;
4564
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01004565 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4566 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08004567 if (!crtc->gamma_store) {
4568 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004569 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004570 }
4571
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004572 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004573}
4574EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4575
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004576/**
4577 * drm_mode_gamma_set_ioctl - set the gamma table
4578 * @dev: DRM device
4579 * @data: ioctl data
4580 * @file_priv: DRM file info
4581 *
4582 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4583 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4584 *
4585 * Called by the user via ioctl.
4586 *
4587 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004588 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004589 */
Dave Airlief453ba02008-11-07 14:05:41 -08004590int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4591 void *data, struct drm_file *file_priv)
4592{
4593 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004594 struct drm_crtc *crtc;
4595 void *r_base, *g_base, *b_base;
4596 int size;
4597 int ret = 0;
4598
Dave Airliefb3b06c2011-02-08 13:55:21 +10004599 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4600 return -EINVAL;
4601
Daniel Vetter84849902012-12-02 00:28:11 +01004602 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004603 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4604 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004605 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004606 goto out;
4607 }
Dave Airlief453ba02008-11-07 14:05:41 -08004608
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004609 if (crtc->funcs->gamma_set == NULL) {
4610 ret = -ENOSYS;
4611 goto out;
4612 }
4613
Dave Airlief453ba02008-11-07 14:05:41 -08004614 /* memcpy into gamma store */
4615 if (crtc_lut->gamma_size != crtc->gamma_size) {
4616 ret = -EINVAL;
4617 goto out;
4618 }
4619
4620 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4621 r_base = crtc->gamma_store;
4622 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4623 ret = -EFAULT;
4624 goto out;
4625 }
4626
4627 g_base = r_base + size;
4628 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4629 ret = -EFAULT;
4630 goto out;
4631 }
4632
4633 b_base = g_base + size;
4634 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4635 ret = -EFAULT;
4636 goto out;
4637 }
4638
James Simmons72034252010-08-03 01:33:19 +01004639 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004640
4641out:
Daniel Vetter84849902012-12-02 00:28:11 +01004642 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004643 return ret;
4644
4645}
4646
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004647/**
4648 * drm_mode_gamma_get_ioctl - get the gamma table
4649 * @dev: DRM device
4650 * @data: ioctl data
4651 * @file_priv: DRM file info
4652 *
4653 * Copy the current gamma table into the storage provided. This also provides
4654 * the gamma table size the driver expects, which can be used to size the
4655 * allocated storage.
4656 *
4657 * Called by the user via ioctl.
4658 *
4659 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004660 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004661 */
Dave Airlief453ba02008-11-07 14:05:41 -08004662int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4663 void *data, struct drm_file *file_priv)
4664{
4665 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004666 struct drm_crtc *crtc;
4667 void *r_base, *g_base, *b_base;
4668 int size;
4669 int ret = 0;
4670
Dave Airliefb3b06c2011-02-08 13:55:21 +10004671 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4672 return -EINVAL;
4673
Daniel Vetter84849902012-12-02 00:28:11 +01004674 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004675 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4676 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004677 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004678 goto out;
4679 }
Dave Airlief453ba02008-11-07 14:05:41 -08004680
4681 /* memcpy into gamma store */
4682 if (crtc_lut->gamma_size != crtc->gamma_size) {
4683 ret = -EINVAL;
4684 goto out;
4685 }
4686
4687 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4688 r_base = crtc->gamma_store;
4689 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4690 ret = -EFAULT;
4691 goto out;
4692 }
4693
4694 g_base = r_base + size;
4695 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4696 ret = -EFAULT;
4697 goto out;
4698 }
4699
4700 b_base = g_base + size;
4701 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4702 ret = -EFAULT;
4703 goto out;
4704 }
4705out:
Daniel Vetter84849902012-12-02 00:28:11 +01004706 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004707 return ret;
4708}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004709
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004710/**
4711 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4712 * @dev: DRM device
4713 * @data: ioctl data
4714 * @file_priv: DRM file info
4715 *
4716 * This schedules an asynchronous update on a given CRTC, called page flip.
4717 * Optionally a drm event is generated to signal the completion of the event.
4718 * Generic drivers cannot assume that a pageflip with changed framebuffer
4719 * properties (including driver specific metadata like tiling layout) will work,
4720 * but some drivers support e.g. pixel format changes through the pageflip
4721 * ioctl.
4722 *
4723 * Called by the user via ioctl.
4724 *
4725 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004726 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004727 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004728int drm_mode_page_flip_ioctl(struct drm_device *dev,
4729 void *data, struct drm_file *file_priv)
4730{
4731 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004732 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02004733 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004734 struct drm_pending_vblank_event *e = NULL;
4735 unsigned long flags;
4736 int ret = -EINVAL;
4737
4738 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4739 page_flip->reserved != 0)
4740 return -EINVAL;
4741
Keith Packard62f21042013-07-22 18:50:00 -07004742 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4743 return -EINVAL;
4744
Rob Clarka2b34e22013-10-05 16:36:52 -04004745 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4746 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004747 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004748
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01004749 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07004750 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004751 /* The framebuffer is currently unbound, presumably
4752 * due to a hotplug event, that userspace has not
4753 * yet discovered.
4754 */
4755 ret = -EBUSY;
4756 goto out;
4757 }
4758
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004759 if (crtc->funcs->page_flip == NULL)
4760 goto out;
4761
Daniel Vetter786b99e2012-12-02 21:53:40 +01004762 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004763 if (!fb) {
4764 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004765 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004766 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004767
Damien Lespiauc11e9282013-09-25 16:45:30 +01004768 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4769 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004770 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004771
Matt Roperf4510a22014-04-01 15:22:40 -07004772 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004773 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4774 ret = -EINVAL;
4775 goto out;
4776 }
4777
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004778 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4779 ret = -ENOMEM;
4780 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004781 if (file_priv->event_space < sizeof(e->event)) {
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004782 spin_unlock_irqrestore(&dev->event_lock, flags);
4783 goto out;
4784 }
Thierry Redingf76511b2014-12-10 13:03:40 +01004785 file_priv->event_space -= sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004786 spin_unlock_irqrestore(&dev->event_lock, flags);
4787
Thierry Redingf76511b2014-12-10 13:03:40 +01004788 e = kzalloc(sizeof(*e), GFP_KERNEL);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004789 if (e == NULL) {
4790 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004791 file_priv->event_space += sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004792 spin_unlock_irqrestore(&dev->event_lock, flags);
4793 goto out;
4794 }
4795
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004796 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01004797 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004798 e->event.user_data = page_flip->user_data;
4799 e->base.event = &e->event.base;
4800 e->base.file_priv = file_priv;
4801 e->base.destroy =
4802 (void (*) (struct drm_pending_event *)) kfree;
4803 }
4804
Daniel Vetter3d30a592014-07-27 13:42:42 +02004805 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004806 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004807 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004808 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4809 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004810 file_priv->event_space += sizeof(e->event);
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004811 spin_unlock_irqrestore(&dev->event_lock, flags);
4812 kfree(e);
4813 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004814 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02004815 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004816 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004817 /*
4818 * Warn if the driver hasn't properly updated the crtc->fb
4819 * field to reflect that the new framebuffer is now used.
4820 * Failing to do so will screw with the reference counting
4821 * on framebuffers.
4822 */
Matt Roperf4510a22014-04-01 15:22:40 -07004823 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004824 /* Unref only the old framebuffer. */
4825 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004826 }
4827
4828out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004829 if (fb)
4830 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02004831 if (crtc->primary->old_fb)
4832 drm_framebuffer_unreference(crtc->primary->old_fb);
4833 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02004834 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004835
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004836 return ret;
4837}
Chris Wilsoneb033552011-01-24 15:11:08 +00004838
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004839/**
4840 * drm_mode_config_reset - call ->reset callbacks
4841 * @dev: drm device
4842 *
4843 * This functions calls all the crtc's, encoder's and connector's ->reset
4844 * callback. Drivers can use this in e.g. their driver load or resume code to
4845 * reset hardware and software state.
4846 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004847void drm_mode_config_reset(struct drm_device *dev)
4848{
4849 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004850 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00004851 struct drm_encoder *encoder;
4852 struct drm_connector *connector;
4853
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004854 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4855 if (plane->funcs->reset)
4856 plane->funcs->reset(plane);
4857
Chris Wilsoneb033552011-01-24 15:11:08 +00004858 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4859 if (crtc->funcs->reset)
4860 crtc->funcs->reset(crtc);
4861
4862 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4863 if (encoder->funcs->reset)
4864 encoder->funcs->reset(encoder);
4865
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004866 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4867 connector->status = connector_status_unknown;
4868
Chris Wilsoneb033552011-01-24 15:11:08 +00004869 if (connector->funcs->reset)
4870 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00004871 }
Chris Wilsoneb033552011-01-24 15:11:08 +00004872}
4873EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004874
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004875/**
4876 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4877 * @dev: DRM device
4878 * @data: ioctl data
4879 * @file_priv: DRM file info
4880 *
4881 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4882 * TTM or something else entirely) and returns the resulting buffer handle. This
4883 * handle can then be wrapped up into a framebuffer modeset object.
4884 *
4885 * Note that userspace is not allowed to use such objects for render
4886 * acceleration - drivers must create their own private ioctls for such a use
4887 * case.
4888 *
4889 * Called by the user via ioctl.
4890 *
4891 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004892 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004893 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004894int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4895 void *data, struct drm_file *file_priv)
4896{
4897 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004898 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004899
4900 if (!dev->driver->dumb_create)
4901 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004902 if (!args->width || !args->height || !args->bpp)
4903 return -EINVAL;
4904
4905 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02004906 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01004907 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02004908 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01004909 return -EINVAL;
4910 stride = cpp * args->width;
4911 if (args->height > 0xffffffffU / stride)
4912 return -EINVAL;
4913
4914 /* test for wrap-around */
4915 size = args->height * stride;
4916 if (PAGE_ALIGN(size) == 0)
4917 return -EINVAL;
4918
Thierry Redingf6085952014-11-03 11:14:14 +01004919 /*
4920 * handle, pitch and size are output parameters. Zero them out to
4921 * prevent drivers from accidentally using uninitialized data. Since
4922 * not all existing userspace is clearing these fields properly we
4923 * cannot reject IOCTL with garbage in them.
4924 */
4925 args->handle = 0;
4926 args->pitch = 0;
4927 args->size = 0;
4928
Dave Airlieff72145b2011-02-07 12:16:14 +10004929 return dev->driver->dumb_create(file_priv, dev, args);
4930}
4931
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004932/**
4933 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4934 * @dev: DRM device
4935 * @data: ioctl data
4936 * @file_priv: DRM file info
4937 *
4938 * Allocate an offset in the drm device node's address space to be able to
4939 * memory map a dumb buffer.
4940 *
4941 * Called by the user via ioctl.
4942 *
4943 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004944 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004945 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004946int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4947 void *data, struct drm_file *file_priv)
4948{
4949 struct drm_mode_map_dumb *args = data;
4950
4951 /* call driver ioctl to get mmap offset */
4952 if (!dev->driver->dumb_map_offset)
4953 return -ENOSYS;
4954
4955 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4956}
4957
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004958/**
4959 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4960 * @dev: DRM device
4961 * @data: ioctl data
4962 * @file_priv: DRM file info
4963 *
4964 * This destroys the userspace handle for the given dumb backing storage buffer.
4965 * Since buffer objects must be reference counted in the kernel a buffer object
4966 * won't be immediately freed if a framebuffer modeset object still uses it.
4967 *
4968 * Called by the user via ioctl.
4969 *
4970 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004971 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004972 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004973int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4974 void *data, struct drm_file *file_priv)
4975{
4976 struct drm_mode_destroy_dumb *args = data;
4977
4978 if (!dev->driver->dumb_destroy)
4979 return -ENOSYS;
4980
4981 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4982}
Dave Airlie248dbc22011-11-29 20:02:54 +00004983
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004984/**
4985 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4986 * @format: pixel format (DRM_FORMAT_*)
4987 * @depth: storage for the depth value
4988 * @bpp: storage for the bpp value
4989 *
4990 * This only supports RGB formats here for compat with code that doesn't use
4991 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00004992 */
4993void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4994 int *bpp)
4995{
4996 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02004997 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02004998 case DRM_FORMAT_RGB332:
4999 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005000 *depth = 8;
5001 *bpp = 8;
5002 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005003 case DRM_FORMAT_XRGB1555:
5004 case DRM_FORMAT_XBGR1555:
5005 case DRM_FORMAT_RGBX5551:
5006 case DRM_FORMAT_BGRX5551:
5007 case DRM_FORMAT_ARGB1555:
5008 case DRM_FORMAT_ABGR1555:
5009 case DRM_FORMAT_RGBA5551:
5010 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005011 *depth = 15;
5012 *bpp = 16;
5013 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005014 case DRM_FORMAT_RGB565:
5015 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005016 *depth = 16;
5017 *bpp = 16;
5018 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005019 case DRM_FORMAT_RGB888:
5020 case DRM_FORMAT_BGR888:
5021 *depth = 24;
5022 *bpp = 24;
5023 break;
5024 case DRM_FORMAT_XRGB8888:
5025 case DRM_FORMAT_XBGR8888:
5026 case DRM_FORMAT_RGBX8888:
5027 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005028 *depth = 24;
5029 *bpp = 32;
5030 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005031 case DRM_FORMAT_XRGB2101010:
5032 case DRM_FORMAT_XBGR2101010:
5033 case DRM_FORMAT_RGBX1010102:
5034 case DRM_FORMAT_BGRX1010102:
5035 case DRM_FORMAT_ARGB2101010:
5036 case DRM_FORMAT_ABGR2101010:
5037 case DRM_FORMAT_RGBA1010102:
5038 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005039 *depth = 30;
5040 *bpp = 32;
5041 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005042 case DRM_FORMAT_ARGB8888:
5043 case DRM_FORMAT_ABGR8888:
5044 case DRM_FORMAT_RGBA8888:
5045 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005046 *depth = 32;
5047 *bpp = 32;
5048 break;
5049 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005050 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5051 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005052 *depth = 0;
5053 *bpp = 0;
5054 break;
5055 }
5056}
5057EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005058
5059/**
5060 * drm_format_num_planes - get the number of planes for format
5061 * @format: pixel format (DRM_FORMAT_*)
5062 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005063 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005064 * The number of planes used by the specified pixel format.
5065 */
5066int drm_format_num_planes(uint32_t format)
5067{
5068 switch (format) {
5069 case DRM_FORMAT_YUV410:
5070 case DRM_FORMAT_YVU410:
5071 case DRM_FORMAT_YUV411:
5072 case DRM_FORMAT_YVU411:
5073 case DRM_FORMAT_YUV420:
5074 case DRM_FORMAT_YVU420:
5075 case DRM_FORMAT_YUV422:
5076 case DRM_FORMAT_YVU422:
5077 case DRM_FORMAT_YUV444:
5078 case DRM_FORMAT_YVU444:
5079 return 3;
5080 case DRM_FORMAT_NV12:
5081 case DRM_FORMAT_NV21:
5082 case DRM_FORMAT_NV16:
5083 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005084 case DRM_FORMAT_NV24:
5085 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005086 return 2;
5087 default:
5088 return 1;
5089 }
5090}
5091EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005092
5093/**
5094 * drm_format_plane_cpp - determine the bytes per pixel value
5095 * @format: pixel format (DRM_FORMAT_*)
5096 * @plane: plane index
5097 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005098 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005099 * The bytes per pixel value for the specified plane.
5100 */
5101int drm_format_plane_cpp(uint32_t format, int plane)
5102{
5103 unsigned int depth;
5104 int bpp;
5105
5106 if (plane >= drm_format_num_planes(format))
5107 return 0;
5108
5109 switch (format) {
5110 case DRM_FORMAT_YUYV:
5111 case DRM_FORMAT_YVYU:
5112 case DRM_FORMAT_UYVY:
5113 case DRM_FORMAT_VYUY:
5114 return 2;
5115 case DRM_FORMAT_NV12:
5116 case DRM_FORMAT_NV21:
5117 case DRM_FORMAT_NV16:
5118 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005119 case DRM_FORMAT_NV24:
5120 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005121 return plane ? 2 : 1;
5122 case DRM_FORMAT_YUV410:
5123 case DRM_FORMAT_YVU410:
5124 case DRM_FORMAT_YUV411:
5125 case DRM_FORMAT_YVU411:
5126 case DRM_FORMAT_YUV420:
5127 case DRM_FORMAT_YVU420:
5128 case DRM_FORMAT_YUV422:
5129 case DRM_FORMAT_YVU422:
5130 case DRM_FORMAT_YUV444:
5131 case DRM_FORMAT_YVU444:
5132 return 1;
5133 default:
5134 drm_fb_get_bpp_depth(format, &depth, &bpp);
5135 return bpp >> 3;
5136 }
5137}
5138EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005139
5140/**
5141 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5142 * @format: pixel format (DRM_FORMAT_*)
5143 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005144 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005145 * The horizontal chroma subsampling factor for the
5146 * specified pixel format.
5147 */
5148int drm_format_horz_chroma_subsampling(uint32_t format)
5149{
5150 switch (format) {
5151 case DRM_FORMAT_YUV411:
5152 case DRM_FORMAT_YVU411:
5153 case DRM_FORMAT_YUV410:
5154 case DRM_FORMAT_YVU410:
5155 return 4;
5156 case DRM_FORMAT_YUYV:
5157 case DRM_FORMAT_YVYU:
5158 case DRM_FORMAT_UYVY:
5159 case DRM_FORMAT_VYUY:
5160 case DRM_FORMAT_NV12:
5161 case DRM_FORMAT_NV21:
5162 case DRM_FORMAT_NV16:
5163 case DRM_FORMAT_NV61:
5164 case DRM_FORMAT_YUV422:
5165 case DRM_FORMAT_YVU422:
5166 case DRM_FORMAT_YUV420:
5167 case DRM_FORMAT_YVU420:
5168 return 2;
5169 default:
5170 return 1;
5171 }
5172}
5173EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5174
5175/**
5176 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5177 * @format: pixel format (DRM_FORMAT_*)
5178 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005179 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005180 * The vertical chroma subsampling factor for the
5181 * specified pixel format.
5182 */
5183int drm_format_vert_chroma_subsampling(uint32_t format)
5184{
5185 switch (format) {
5186 case DRM_FORMAT_YUV410:
5187 case DRM_FORMAT_YVU410:
5188 return 4;
5189 case DRM_FORMAT_YUV420:
5190 case DRM_FORMAT_YVU420:
5191 case DRM_FORMAT_NV12:
5192 case DRM_FORMAT_NV21:
5193 return 2;
5194 default:
5195 return 1;
5196 }
5197}
5198EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005199
5200/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305201 * drm_rotation_simplify() - Try to simplify the rotation
5202 * @rotation: Rotation to be simplified
5203 * @supported_rotations: Supported rotations
5204 *
5205 * Attempt to simplify the rotation to a form that is supported.
5206 * Eg. if the hardware supports everything except DRM_REFLECT_X
5207 * one could call this function like this:
5208 *
5209 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5210 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5211 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5212 *
5213 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5214 * transforms the hardware supports, this function may not
5215 * be able to produce a supported transform, so the caller should
5216 * check the result afterwards.
5217 */
5218unsigned int drm_rotation_simplify(unsigned int rotation,
5219 unsigned int supported_rotations)
5220{
5221 if (rotation & ~supported_rotations) {
5222 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5223 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5224 }
5225
5226 return rotation;
5227}
5228EXPORT_SYMBOL(drm_rotation_simplify);
5229
5230/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005231 * drm_mode_config_init - initialize DRM mode_configuration structure
5232 * @dev: DRM device
5233 *
5234 * Initialize @dev's mode_config structure, used for tracking the graphics
5235 * configuration of @dev.
5236 *
5237 * Since this initializes the modeset locks, no locking is possible. Which is no
5238 * problem, since this should happen single threaded at init time. It is the
5239 * driver's problem to ensure this guarantee.
5240 *
5241 */
5242void drm_mode_config_init(struct drm_device *dev)
5243{
5244 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005245 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005246 mutex_init(&dev->mode_config.idr_mutex);
5247 mutex_init(&dev->mode_config.fb_lock);
5248 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5249 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5250 INIT_LIST_HEAD(&dev->mode_config.connector_list);
Sean Paul3b336ec2013-08-14 16:47:37 -04005251 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005252 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5253 INIT_LIST_HEAD(&dev->mode_config.property_list);
5254 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5255 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5256 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005257 idr_init(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005258
5259 drm_modeset_lock_all(dev);
5260 drm_mode_create_standard_connector_properties(dev);
Rob Clark9922ab52014-04-01 20:16:57 -04005261 drm_mode_create_standard_plane_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005262 drm_modeset_unlock_all(dev);
5263
5264 /* Just to be sure */
5265 dev->mode_config.num_fb = 0;
5266 dev->mode_config.num_connector = 0;
5267 dev->mode_config.num_crtc = 0;
5268 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005269 dev->mode_config.num_overlay_plane = 0;
5270 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005271}
5272EXPORT_SYMBOL(drm_mode_config_init);
5273
5274/**
5275 * drm_mode_config_cleanup - free up DRM mode_config info
5276 * @dev: DRM device
5277 *
5278 * Free up all the connectors and CRTCs associated with this DRM device, then
5279 * free up the framebuffers and associated buffer objects.
5280 *
5281 * Note that since this /should/ happen single-threaded at driver/device
5282 * teardown time, no locking is required. It's the driver's job to ensure that
5283 * this guarantee actually holds true.
5284 *
5285 * FIXME: cleanup any dangling user buffer objects too
5286 */
5287void drm_mode_config_cleanup(struct drm_device *dev)
5288{
5289 struct drm_connector *connector, *ot;
5290 struct drm_crtc *crtc, *ct;
5291 struct drm_encoder *encoder, *enct;
Sean Paul3b336ec2013-08-14 16:47:37 -04005292 struct drm_bridge *bridge, *brt;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005293 struct drm_framebuffer *fb, *fbt;
5294 struct drm_property *property, *pt;
5295 struct drm_property_blob *blob, *bt;
5296 struct drm_plane *plane, *plt;
5297
5298 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5299 head) {
5300 encoder->funcs->destroy(encoder);
5301 }
5302
Sean Paul3b336ec2013-08-14 16:47:37 -04005303 list_for_each_entry_safe(bridge, brt,
5304 &dev->mode_config.bridge_list, head) {
5305 bridge->funcs->destroy(bridge);
5306 }
5307
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005308 list_for_each_entry_safe(connector, ot,
5309 &dev->mode_config.connector_list, head) {
5310 connector->funcs->destroy(connector);
5311 }
5312
5313 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5314 head) {
5315 drm_property_destroy(dev, property);
5316 }
5317
5318 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5319 head) {
5320 drm_property_destroy_blob(dev, blob);
5321 }
5322
5323 /*
5324 * Single-threaded teardown context, so it's not required to grab the
5325 * fb_lock to protect against concurrent fb_list access. Contrary, it
5326 * would actually deadlock with the drm_framebuffer_cleanup function.
5327 *
5328 * Also, if there are any framebuffers left, that's a driver leak now,
5329 * so politely WARN about this.
5330 */
5331 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5332 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5333 drm_framebuffer_remove(fb);
5334 }
5335
5336 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5337 head) {
5338 plane->funcs->destroy(plane);
5339 }
5340
5341 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5342 crtc->funcs->destroy(crtc);
5343 }
5344
Dave Airlie138f9eb2014-10-20 16:17:17 +10005345 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005346 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005347 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005348}
5349EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305350
5351struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5352 unsigned int supported_rotations)
5353{
5354 static const struct drm_prop_enum_list props[] = {
5355 { DRM_ROTATE_0, "rotate-0" },
5356 { DRM_ROTATE_90, "rotate-90" },
5357 { DRM_ROTATE_180, "rotate-180" },
5358 { DRM_ROTATE_270, "rotate-270" },
5359 { DRM_REFLECT_X, "reflect-x" },
5360 { DRM_REFLECT_Y, "reflect-y" },
5361 };
5362
5363 return drm_property_create_bitmask(dev, 0, "rotation",
5364 props, ARRAY_SIZE(props),
5365 supported_rotations);
5366}
5367EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005368
5369/**
5370 * DOC: Tile group
5371 *
5372 * Tile groups are used to represent tiled monitors with a unique
5373 * integer identifier. Tiled monitors using DisplayID v1.3 have
5374 * a unique 8-byte handle, we store this in a tile group, so we
5375 * have a common identifier for all tiles in a monitor group.
5376 */
5377static void drm_tile_group_free(struct kref *kref)
5378{
5379 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5380 struct drm_device *dev = tg->dev;
5381 mutex_lock(&dev->mode_config.idr_mutex);
5382 idr_remove(&dev->mode_config.tile_idr, tg->id);
5383 mutex_unlock(&dev->mode_config.idr_mutex);
5384 kfree(tg);
5385}
5386
5387/**
5388 * drm_mode_put_tile_group - drop a reference to a tile group.
5389 * @dev: DRM device
5390 * @tg: tile group to drop reference to.
5391 *
5392 * drop reference to tile group and free if 0.
5393 */
5394void drm_mode_put_tile_group(struct drm_device *dev,
5395 struct drm_tile_group *tg)
5396{
5397 kref_put(&tg->refcount, drm_tile_group_free);
5398}
5399
5400/**
5401 * drm_mode_get_tile_group - get a reference to an existing tile group
5402 * @dev: DRM device
5403 * @topology: 8-bytes unique per monitor.
5404 *
5405 * Use the unique bytes to get a reference to an existing tile group.
5406 *
5407 * RETURNS:
5408 * tile group or NULL if not found.
5409 */
5410struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5411 char topology[8])
5412{
5413 struct drm_tile_group *tg;
5414 int id;
5415 mutex_lock(&dev->mode_config.idr_mutex);
5416 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5417 if (!memcmp(tg->group_data, topology, 8)) {
5418 if (!kref_get_unless_zero(&tg->refcount))
5419 tg = NULL;
5420 mutex_unlock(&dev->mode_config.idr_mutex);
5421 return tg;
5422 }
5423 }
5424 mutex_unlock(&dev->mode_config.idr_mutex);
5425 return NULL;
5426}
5427
5428/**
5429 * drm_mode_create_tile_group - create a tile group from a displayid description
5430 * @dev: DRM device
5431 * @topology: 8-bytes unique per monitor.
5432 *
5433 * Create a tile group for the unique monitor, and get a unique
5434 * identifier for the tile group.
5435 *
5436 * RETURNS:
5437 * new tile group or error.
5438 */
5439struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5440 char topology[8])
5441{
5442 struct drm_tile_group *tg;
5443 int ret;
5444
5445 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5446 if (!tg)
5447 return ERR_PTR(-ENOMEM);
5448
5449 kref_init(&tg->refcount);
5450 memcpy(tg->group_data, topology, 8);
5451 tg->dev = dev;
5452
5453 mutex_lock(&dev->mode_config.idr_mutex);
5454 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5455 if (ret >= 0) {
5456 tg->id = ret;
5457 } else {
5458 kfree(tg);
5459 tg = ERR_PTR(ret);
5460 }
5461
5462 mutex_unlock(&dev->mode_config.idr_mutex);
5463 return tg;
5464}