blob: f6d04c7b5115a965bce68ad265080a46fcad3a6b [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
Chris Wilson9a6f5132015-02-25 13:45:26 +000046static struct drm_framebuffer *
47internal_framebuffer_create(struct drm_device *dev,
48 struct drm_mode_fb_cmd2 *r,
49 struct drm_file *file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -070050
Dave Airlief453ba02008-11-07 14:05:41 -080051/* Avoid boilerplate. I'm tired of typing. */
52#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000053 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080054 { \
55 int i; \
56 for (i = 0; i < ARRAY_SIZE(list); i++) { \
57 if (list[i].type == val) \
58 return list[i].name; \
59 } \
60 return "(unknown)"; \
61 }
62
63/*
64 * Global properties
65 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010066static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
67 { DRM_MODE_DPMS_ON, "On" },
Dave Airlief453ba02008-11-07 14:05:41 -080068 { DRM_MODE_DPMS_STANDBY, "Standby" },
69 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
70 { DRM_MODE_DPMS_OFF, "Off" }
71};
72
73DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
74
Thierry Reding4dfd9092014-12-10 13:03:34 +010075static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
Rob Clark9922ab52014-04-01 20:16:57 -040076 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
77 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
78 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
79};
80
Dave Airlief453ba02008-11-07 14:05:41 -080081/*
82 * Optional properties
83 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010084static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
Jesse Barnes53bd8382009-07-01 10:04:40 -070085 { DRM_MODE_SCALE_NONE, "None" },
86 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
87 { DRM_MODE_SCALE_CENTER, "Center" },
88 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080089};
90
Vandana Kannanff587e42014-06-11 10:46:48 +053091static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
92 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
93 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
94 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
95};
96
Dave Airlief453ba02008-11-07 14:05:41 -080097/*
98 * Non-global properties, but "required" for certain connectors.
99 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100100static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
104};
105
106DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
107
Thierry Reding4dfd9092014-12-10 13:03:34 +0100108static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800109 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
111 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
112};
113
114DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
115 drm_dvi_i_subconnector_enum_list)
116
Thierry Reding4dfd9092014-12-10 13:03:34 +0100117static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800118 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
126
Thierry Reding4dfd9092014-12-10 13:03:34 +0100127static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800128 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
129 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200132 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800133};
134
135DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
136 drm_tv_subconnector_enum_list)
137
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000138static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000139 { DRM_MODE_DIRTY_OFF, "Off" },
140 { DRM_MODE_DIRTY_ON, "On" },
141 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
142};
143
Dave Airlief453ba02008-11-07 14:05:41 -0800144struct drm_conn_prop_enum_list {
145 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000146 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400147 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800148};
149
150/*
151 * Connector and encoder types.
152 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100153static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
154 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400155 { DRM_MODE_CONNECTOR_VGA, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite, "Composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
161 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component, "Component" },
163 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV, "TV" },
168 { DRM_MODE_CONNECTOR_eDP, "eDP" },
169 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300170 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800171};
172
Thierry Reding4dfd9092014-12-10 13:03:34 +0100173static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
174 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -0800175 { DRM_MODE_ENCODER_DAC, "DAC" },
176 { DRM_MODE_ENCODER_TMDS, "TMDS" },
177 { DRM_MODE_ENCODER_LVDS, "LVDS" },
178 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200179 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300180 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000181 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Dave Airlief453ba02008-11-07 14:05:41 -0800182};
183
Thierry Reding4dfd9092014-12-10 13:03:34 +0100184static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
Jesse Barnesac1bb362014-02-10 15:32:44 -0800185 { SubPixelUnknown, "Unknown" },
186 { SubPixelHorizontalRGB, "Horizontal RGB" },
187 { SubPixelHorizontalBGR, "Horizontal BGR" },
188 { SubPixelVerticalRGB, "Vertical RGB" },
189 { SubPixelVerticalBGR, "Vertical BGR" },
190 { SubPixelNone, "None" },
191};
192
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400193void drm_connector_ida_init(void)
194{
195 int i;
196
197 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
198 ida_init(&drm_connector_enum_list[i].ida);
199}
200
201void drm_connector_ida_destroy(void)
202{
203 int i;
204
205 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
206 ida_destroy(&drm_connector_enum_list[i].ida);
207}
208
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100209/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100210 * drm_get_connector_status_name - return a string for connector status
211 * @status: connector status to compute name of
212 *
213 * In contrast to the other drm_get_*_name functions this one here returns a
214 * const pointer and hence is threadsafe.
215 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000216const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800217{
218 if (status == connector_status_connected)
219 return "connected";
220 else if (status == connector_status_disconnected)
221 return "disconnected";
222 else
223 return "unknown";
224}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000225EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800226
Jesse Barnesac1bb362014-02-10 15:32:44 -0800227/**
228 * drm_get_subpixel_order_name - return a string for a given subpixel enum
229 * @order: enum of subpixel_order
230 *
231 * Note you could abuse this and return something out of bounds, but that
232 * would be a caller error. No unscrubbed user data should make it here.
233 */
234const char *drm_get_subpixel_order_name(enum subpixel_order order)
235{
236 return drm_subpixel_enum_list[order].name;
237}
238EXPORT_SYMBOL(drm_get_subpixel_order_name);
239
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300240static char printable_char(int c)
241{
242 return isascii(c) && isprint(c) ? c : '?';
243}
244
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100245/**
246 * drm_get_format_name - return a string for drm fourcc format
247 * @format: format to compute name of
248 *
249 * Note that the buffer used by this function is globally shared and owned by
250 * the function itself.
251 *
252 * FIXME: This isn't really multithreading safe.
253 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000254const char *drm_get_format_name(uint32_t format)
Ville Syrjälä6ba6d032013-06-10 11:15:10 +0300255{
256 static char buf[32];
257
258 snprintf(buf, sizeof(buf),
259 "%c%c%c%c %s-endian (0x%08x)",
260 printable_char(format & 0xff),
261 printable_char((format >> 8) & 0xff),
262 printable_char((format >> 16) & 0xff),
263 printable_char((format >> 24) & 0x7f),
264 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
265 format);
266
267 return buf;
268}
269EXPORT_SYMBOL(drm_get_format_name);
270
Dave Airlie2ee39452014-07-24 11:53:45 +1000271/*
272 * Internal function to assign a slot in the object idr and optionally
273 * register the object into the idr.
274 */
275static int drm_mode_object_get_reg(struct drm_device *dev,
276 struct drm_mode_object *obj,
277 uint32_t obj_type,
278 bool register_obj)
279{
280 int ret;
281
282 mutex_lock(&dev->mode_config.idr_mutex);
283 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
284 if (ret >= 0) {
285 /*
286 * Set up the object linking under the protection of the idr
287 * lock so that other users can't see inconsistent state.
288 */
289 obj->id = ret;
290 obj->type = obj_type;
291 }
292 mutex_unlock(&dev->mode_config.idr_mutex);
293
294 return ret < 0 ? ret : 0;
295}
296
Dave Airlief453ba02008-11-07 14:05:41 -0800297/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100298 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800299 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100300 * @obj: object pointer, used to generate unique ID
301 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800302 *
Dave Airlief453ba02008-11-07 14:05:41 -0800303 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100304 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
305 * modeset identifiers are _not_ reference counted. Hence don't use this for
306 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800307 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100308 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800309 * New unique (relative to other objects in @dev) integer identifier for the
310 * object.
311 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100312int drm_mode_object_get(struct drm_device *dev,
313 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800314{
Dave Airlie2ee39452014-07-24 11:53:45 +1000315 return drm_mode_object_get_reg(dev, obj, obj_type, true);
316}
Dave Airlief453ba02008-11-07 14:05:41 -0800317
Dave Airlie2ee39452014-07-24 11:53:45 +1000318static void drm_mode_object_register(struct drm_device *dev,
319 struct drm_mode_object *obj)
320{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000321 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000322 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000323 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800324}
325
326/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100327 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800328 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100329 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800330 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100331 * Free @id from @dev's unique identifier pool. Note that despite the _get
332 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
333 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800334 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100335void drm_mode_object_put(struct drm_device *dev,
336 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800337{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000338 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800339 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000340 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800341}
342
Rob Clark98f75de2014-05-30 11:37:03 -0400343static struct drm_mode_object *_object_find(struct drm_device *dev,
344 uint32_t id, uint32_t type)
345{
346 struct drm_mode_object *obj = NULL;
347
348 mutex_lock(&dev->mode_config.idr_mutex);
349 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200350 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
351 obj = NULL;
352 if (obj && obj->id != id)
353 obj = NULL;
354 /* don't leak out unref'd fb's */
355 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
Rob Clark98f75de2014-05-30 11:37:03 -0400356 obj = NULL;
357 mutex_unlock(&dev->mode_config.idr_mutex);
358
359 return obj;
360}
361
Daniel Vetter786b99e2012-12-02 21:53:40 +0100362/**
363 * drm_mode_object_find - look up a drm object with static lifetime
364 * @dev: drm device
365 * @id: id of the mode object
366 * @type: type of the mode object
367 *
368 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400369 * are reference counted, they need special treatment. Even with
370 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
371 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100372 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200373struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
374 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800375{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000376 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800377
Daniel Vetter786b99e2012-12-02 21:53:40 +0100378 /* Framebuffers are reference counted and need their own lookup
379 * function.*/
380 WARN_ON(type == DRM_MODE_OBJECT_FB);
Rob Clark98f75de2014-05-30 11:37:03 -0400381 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800382 return obj;
383}
384EXPORT_SYMBOL(drm_mode_object_find);
385
386/**
Dave Airlief453ba02008-11-07 14:05:41 -0800387 * drm_framebuffer_init - initialize a framebuffer
388 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100389 * @fb: framebuffer to be initialized
390 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800391 *
Dave Airlief453ba02008-11-07 14:05:41 -0800392 * Allocates an ID for the framebuffer's parent mode object, sets its mode
393 * functions & device file and adds it to the master fd list.
394 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100395 * IMPORTANT:
396 * This functions publishes the fb and makes it available for concurrent access
397 * by other users. Which means by this point the fb _must_ be fully set up -
398 * since all the fb attributes are invariant over its lifetime, no further
399 * locking but only correct reference counting is required.
400 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100401 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200402 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800403 */
404int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
405 const struct drm_framebuffer_funcs *funcs)
406{
407 int ret;
408
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100409 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000410 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100411 INIT_LIST_HEAD(&fb->filp_head);
412 fb->dev = dev;
413 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000414
Dave Airlief453ba02008-11-07 14:05:41 -0800415 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200416 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100417 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800418
Dave Airlief453ba02008-11-07 14:05:41 -0800419 dev->mode_config.num_fb++;
420 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100421out:
422 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800423
424 return 0;
425}
426EXPORT_SYMBOL(drm_framebuffer_init);
427
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200428/* dev->mode_config.fb_lock must be held! */
429static void __drm_framebuffer_unregister(struct drm_device *dev,
430 struct drm_framebuffer *fb)
431{
432 mutex_lock(&dev->mode_config.idr_mutex);
433 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
434 mutex_unlock(&dev->mode_config.idr_mutex);
435
436 fb->base.id = 0;
437}
438
Rob Clarkf7eff602012-09-05 21:48:38 +0000439static void drm_framebuffer_free(struct kref *kref)
440{
441 struct drm_framebuffer *fb =
442 container_of(kref, struct drm_framebuffer, refcount);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200443 struct drm_device *dev = fb->dev;
444
445 /*
446 * The lookup idr holds a weak reference, which has not necessarily been
447 * removed at this point. Check for that.
448 */
449 mutex_lock(&dev->mode_config.fb_lock);
450 if (fb->base.id) {
451 /* Mark fb as reaped and drop idr ref. */
452 __drm_framebuffer_unregister(dev, fb);
453 }
454 mutex_unlock(&dev->mode_config.fb_lock);
455
Rob Clarkf7eff602012-09-05 21:48:38 +0000456 fb->funcs->destroy(fb);
457}
458
Daniel Vetter2b677e82012-12-10 21:16:05 +0100459static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
460 uint32_t id)
461{
462 struct drm_mode_object *obj = NULL;
463 struct drm_framebuffer *fb;
464
465 mutex_lock(&dev->mode_config.idr_mutex);
466 obj = idr_find(&dev->mode_config.crtc_idr, id);
467 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
468 fb = NULL;
469 else
470 fb = obj_to_fb(obj);
471 mutex_unlock(&dev->mode_config.idr_mutex);
472
473 return fb;
474}
475
Rob Clarkf7eff602012-09-05 21:48:38 +0000476/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100477 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
478 * @dev: drm device
479 * @id: id of the fb object
480 *
481 * If successful, this grabs an additional reference to the framebuffer -
482 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100483 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100484 */
485struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
486 uint32_t id)
487{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100488 struct drm_framebuffer *fb;
489
490 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100491 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200492 if (fb) {
493 if (!kref_get_unless_zero(&fb->refcount))
494 fb = NULL;
495 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100496 mutex_unlock(&dev->mode_config.fb_lock);
497
498 return fb;
499}
500EXPORT_SYMBOL(drm_framebuffer_lookup);
501
502/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000503 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100504 * @fb: framebuffer to unref
505 *
506 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000507 */
508void drm_framebuffer_unreference(struct drm_framebuffer *fb)
509{
Rob Clark82912722014-03-18 10:07:08 -0400510 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000511 kref_put(&fb->refcount, drm_framebuffer_free);
512}
513EXPORT_SYMBOL(drm_framebuffer_unreference);
514
515/**
516 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100517 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100518 *
519 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000520 */
521void drm_framebuffer_reference(struct drm_framebuffer *fb)
522{
Rob Clark82912722014-03-18 10:07:08 -0400523 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000524 kref_get(&fb->refcount);
525}
526EXPORT_SYMBOL(drm_framebuffer_reference);
527
Daniel Vetter2b677e82012-12-10 21:16:05 +0100528static void drm_framebuffer_free_bug(struct kref *kref)
529{
530 BUG();
531}
532
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100533static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
534{
Rob Clark82912722014-03-18 10:07:08 -0400535 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100536 kref_put(&fb->refcount, drm_framebuffer_free_bug);
537}
538
Dave Airlief453ba02008-11-07 14:05:41 -0800539/**
Daniel Vetter36206362012-12-10 20:42:17 +0100540 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
541 * @fb: fb to unregister
542 *
543 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
544 * those used for fbdev. Note that the caller must hold a reference of it's own,
545 * i.e. the object may not be destroyed through this call (since it'll lead to a
546 * locking inversion).
547 */
548void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
549{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100550 struct drm_device *dev = fb->dev;
551
552 mutex_lock(&dev->mode_config.fb_lock);
553 /* Mark fb as reaped and drop idr ref. */
554 __drm_framebuffer_unregister(dev, fb);
555 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100556}
557EXPORT_SYMBOL(drm_framebuffer_unregister_private);
558
559/**
Dave Airlief453ba02008-11-07 14:05:41 -0800560 * drm_framebuffer_cleanup - remove a framebuffer object
561 * @fb: framebuffer to remove
562 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100563 * Cleanup framebuffer. This function is intended to be used from the drivers
564 * ->destroy callback. It can also be used to clean up driver private
565 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100566 *
567 * Note that this function does not remove the fb from active usuage - if it is
568 * still used anywhere, hilarity can ensue since userspace could call getfb on
569 * the id and get back -EINVAL. Obviously no concern at driver unload time.
570 *
571 * Also, the framebuffer will not be removed from the lookup idr - for
572 * user-created framebuffers this will happen in in the rmfb ioctl. For
573 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
574 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800575 */
576void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
577{
578 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100579
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100580 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000581 list_del(&fb->head);
582 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100583 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000584}
585EXPORT_SYMBOL(drm_framebuffer_cleanup);
586
587/**
588 * drm_framebuffer_remove - remove and unreference a framebuffer object
589 * @fb: framebuffer to remove
590 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000591 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100592 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100593 * passed-in framebuffer. Might take the modeset locks.
594 *
595 * Note that this function optimizes the cleanup away if the caller holds the
596 * last reference to the framebuffer. It is also guaranteed to not take the
597 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000598 */
599void drm_framebuffer_remove(struct drm_framebuffer *fb)
600{
601 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800602 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800603 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000604 struct drm_mode_set set;
605 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800606
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100607 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100608
Daniel Vetterb62584e2012-12-11 16:51:35 +0100609 /*
610 * drm ABI mandates that we remove any deleted framebuffers from active
611 * useage. But since most sane clients only remove framebuffers they no
612 * longer need, try to optimize this away.
613 *
614 * Since we're holding a reference ourselves, observing a refcount of 1
615 * means that we're the last holder and can skip it. Also, the refcount
616 * can never increase from 1 again, so we don't need any barriers or
617 * locks.
618 *
619 * Note that userspace could try to race with use and instate a new
620 * usage _after_ we've cleared all current ones. End result will be an
621 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
622 * in this manner.
623 */
624 if (atomic_read(&fb->refcount.refcount) > 1) {
625 drm_modeset_lock_all(dev);
626 /* remove from any CRTC */
627 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700628 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100629 /* should turn off the crtc */
630 memset(&set, 0, sizeof(struct drm_mode_set));
631 set.crtc = crtc;
632 set.fb = NULL;
633 ret = drm_mode_set_config_internal(&set);
634 if (ret)
635 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
636 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000637 }
Dave Airlief453ba02008-11-07 14:05:41 -0800638
Daniel Vetterb62584e2012-12-11 16:51:35 +0100639 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300640 if (plane->fb == fb)
641 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800642 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100643 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800644 }
645
Rob Clarkf7eff602012-09-05 21:48:38 +0000646 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800647}
Rob Clarkf7eff602012-09-05 21:48:38 +0000648EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800649
Rob Clark51fd3712013-11-19 12:10:12 -0500650DEFINE_WW_CLASS(crtc_ww_class);
651
Dave Airlief453ba02008-11-07 14:05:41 -0800652/**
Matt Ropere13161a2014-04-01 15:22:38 -0700653 * drm_crtc_init_with_planes - Initialise a new CRTC object with
654 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800655 * @dev: DRM device
656 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700657 * @primary: Primary plane for CRTC
658 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800659 * @funcs: callbacks for the new CRTC
660 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000661 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200662 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100663 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200664 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800665 */
Matt Ropere13161a2014-04-01 15:22:38 -0700666int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
667 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700668 struct drm_plane *cursor,
Matt Ropere13161a2014-04-01 15:22:38 -0700669 const struct drm_crtc_funcs *funcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800670{
Rob Clark51fd3712013-11-19 12:10:12 -0500671 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200672 int ret;
673
Dave Airlief453ba02008-11-07 14:05:41 -0800674 crtc->dev = dev;
675 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000676 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800677
Rob Clark51fd3712013-11-19 12:10:12 -0500678 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200679 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
680 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100681 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800682
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300683 crtc->base.properties = &crtc->properties;
684
Rob Clark51fd3712013-11-19 12:10:12 -0500685 list_add_tail(&crtc->head, &config->crtc_list);
686 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200687
Matt Ropere13161a2014-04-01 15:22:38 -0700688 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700689 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700690 if (primary)
691 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700692 if (cursor)
693 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700694
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100695 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
696 drm_object_attach_property(&crtc->base, config->prop_active, 0);
697 }
698
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100699 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800700}
Matt Ropere13161a2014-04-01 15:22:38 -0700701EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800702
703/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000704 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800705 * @crtc: CRTC to cleanup
706 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000707 * This function cleans up @crtc and removes it from the DRM mode setting
708 * core. Note that the function does *not* free the crtc structure itself,
709 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800710 */
711void drm_crtc_cleanup(struct drm_crtc *crtc)
712{
713 struct drm_device *dev = crtc->dev;
714
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000715 kfree(crtc->gamma_store);
716 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800717
Rob Clark51fd3712013-11-19 12:10:12 -0500718 drm_modeset_lock_fini(&crtc->mutex);
719
Dave Airlief453ba02008-11-07 14:05:41 -0800720 drm_mode_object_put(dev, &crtc->base);
721 list_del(&crtc->head);
722 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100723
724 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
725 if (crtc->state && crtc->funcs->atomic_destroy_state)
726 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100727
728 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800729}
730EXPORT_SYMBOL(drm_crtc_cleanup);
731
732/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000733 * drm_crtc_index - find the index of a registered CRTC
734 * @crtc: CRTC to find index for
735 *
736 * Given a registered CRTC, return the index of that CRTC within a DRM
737 * device's list of CRTCs.
738 */
739unsigned int drm_crtc_index(struct drm_crtc *crtc)
740{
741 unsigned int index = 0;
742 struct drm_crtc *tmp;
743
744 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
745 if (tmp == crtc)
746 return index;
747
748 index++;
749 }
750
751 BUG();
752}
753EXPORT_SYMBOL(drm_crtc_index);
754
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100755/*
Dave Airlief453ba02008-11-07 14:05:41 -0800756 * drm_mode_remove - remove and free a mode
757 * @connector: connector list to modify
758 * @mode: mode to remove
759 *
Dave Airlief453ba02008-11-07 14:05:41 -0800760 * Remove @mode from @connector's mode list, then free it.
761 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100762static void drm_mode_remove(struct drm_connector *connector,
763 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800764{
765 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100766 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800767}
Dave Airlief453ba02008-11-07 14:05:41 -0800768
769/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200770 * drm_display_info_set_bus_formats - set the supported bus formats
771 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100772 * @formats: array containing the supported bus formats
773 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200774 *
775 * Store the supported bus formats in display info structure.
776 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
777 * a full list of available formats.
778 */
779int drm_display_info_set_bus_formats(struct drm_display_info *info,
780 const u32 *formats,
781 unsigned int num_formats)
782{
783 u32 *fmts = NULL;
784
785 if (!formats && num_formats)
786 return -EINVAL;
787
788 if (formats && num_formats) {
789 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
790 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300791 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200792 return -ENOMEM;
793 }
794
795 kfree(info->bus_formats);
796 info->bus_formats = fmts;
797 info->num_bus_formats = num_formats;
798
799 return 0;
800}
801EXPORT_SYMBOL(drm_display_info_set_bus_formats);
802
803/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200804 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
805 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200806 *
807 * The kernel supports per-connector configration of its consoles through
808 * use of the video= parameter. This function parses that option and
809 * extracts the user's specified mode (or enable/disable status) for a
810 * particular connector. This is typically only used during the early fbdev
811 * setup.
812 */
813static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
814{
815 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
816 char *option = NULL;
817
818 if (fb_get_options(connector->name, &option))
819 return;
820
821 if (!drm_mode_parse_command_line_for_connector(option,
822 connector,
823 mode))
824 return;
825
826 if (mode->force) {
827 const char *s;
828
829 switch (mode->force) {
830 case DRM_FORCE_OFF:
831 s = "OFF";
832 break;
833 case DRM_FORCE_ON_DIGITAL:
834 s = "ON - dig";
835 break;
836 default:
837 case DRM_FORCE_ON:
838 s = "ON";
839 break;
840 }
841
842 DRM_INFO("forcing %s connector %s\n", connector->name, s);
843 connector->force = mode->force;
844 }
845
846 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
847 connector->name,
848 mode->xres, mode->yres,
849 mode->refresh_specified ? mode->refresh : 60,
850 mode->rb ? " reduced blanking" : "",
851 mode->margins ? " with margins" : "",
852 mode->interlace ? " interlaced" : "");
853}
854
855/**
Dave Airlief453ba02008-11-07 14:05:41 -0800856 * drm_connector_init - Init a preallocated connector
857 * @dev: DRM device
858 * @connector: the connector to init
859 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100860 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800861 *
Dave Airlief453ba02008-11-07 14:05:41 -0800862 * Initialises a preallocated connector. Connectors should be
863 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200864 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100865 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200866 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800867 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200868int drm_connector_init(struct drm_device *dev,
869 struct drm_connector *connector,
870 const struct drm_connector_funcs *funcs,
871 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800872{
Rob Clarkae16c592014-12-18 16:01:54 -0500873 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200874 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400875 struct ida *connector_ida =
876 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200877
Daniel Vetter84849902012-12-02 00:28:11 +0100878 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800879
Dave Airlie2ee39452014-07-24 11:53:45 +1000880 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200881 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300882 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200883
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300884 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800885 connector->dev = dev;
886 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800887 connector->connector_type = connector_type;
888 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400889 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
890 if (connector->connector_type_id < 0) {
891 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300892 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400893 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300894 connector->name =
895 kasprintf(GFP_KERNEL, "%s-%d",
896 drm_connector_enum_list[connector_type].name,
897 connector->connector_type_id);
898 if (!connector->name) {
899 ret = -ENOMEM;
900 goto out_put;
901 }
902
Dave Airlief453ba02008-11-07 14:05:41 -0800903 INIT_LIST_HEAD(&connector->probed_modes);
904 INIT_LIST_HEAD(&connector->modes);
905 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000906 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800907
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200908 drm_connector_get_cmdline_mode(connector);
909
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100910 /* We should add connectors at the end to avoid upsetting the connector
911 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500912 list_add_tail(&connector->head, &config->connector_list);
913 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800914
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200915 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500916 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500917 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200918 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800919
Rob Clark58495562012-10-11 20:50:56 -0500920 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500921 config->dpms_property, 0);
922
923 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
924 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
925 }
Dave Airlief453ba02008-11-07 14:05:41 -0800926
Thomas Wood30f65702014-06-18 17:52:32 +0100927 connector->debugfs_entry = NULL;
928
Jani Nikula2abdd312014-05-14 16:58:19 +0300929out_put:
930 if (ret)
931 drm_mode_object_put(dev, &connector->base);
932
933out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100934 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200935
936 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800937}
938EXPORT_SYMBOL(drm_connector_init);
939
940/**
941 * drm_connector_cleanup - cleans up an initialised connector
942 * @connector: connector to cleanup
943 *
Dave Airlief453ba02008-11-07 14:05:41 -0800944 * Cleans up the connector but doesn't free the object.
945 */
946void drm_connector_cleanup(struct drm_connector *connector)
947{
948 struct drm_device *dev = connector->dev;
949 struct drm_display_mode *mode, *t;
950
Dave Airlie40d9b042014-10-20 16:29:33 +1000951 if (connector->tile_group) {
952 drm_mode_put_tile_group(dev, connector->tile_group);
953 connector->tile_group = NULL;
954 }
955
Dave Airlief453ba02008-11-07 14:05:41 -0800956 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
957 drm_mode_remove(connector, mode);
958
959 list_for_each_entry_safe(mode, t, &connector->modes, head)
960 drm_mode_remove(connector, mode);
961
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400962 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
963 connector->connector_type_id);
964
Boris Brezillonb5571e92014-07-22 12:09:10 +0200965 kfree(connector->display_info.bus_formats);
Dave Airlief453ba02008-11-07 14:05:41 -0800966 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300967 kfree(connector->name);
968 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800969 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000970 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100971
972 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
973 if (connector->state && connector->funcs->atomic_destroy_state)
974 connector->funcs->atomic_destroy_state(connector,
975 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100976
977 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800978}
979EXPORT_SYMBOL(drm_connector_cleanup);
980
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100981/**
Daniel Vetter10f637b2014-07-29 13:47:11 +0200982 * drm_connector_index - find the index of a registered connector
983 * @connector: connector to find index for
984 *
985 * Given a registered connector, return the index of that connector within a DRM
986 * device's list of connectors.
987 */
988unsigned int drm_connector_index(struct drm_connector *connector)
989{
990 unsigned int index = 0;
991 struct drm_connector *tmp;
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100992 struct drm_mode_config *config = &connector->dev->mode_config;
993
994 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter10f637b2014-07-29 13:47:11 +0200995
996 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
997 if (tmp == connector)
998 return index;
999
1000 index++;
1001 }
1002
1003 BUG();
1004}
1005EXPORT_SYMBOL(drm_connector_index);
1006
1007/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001008 * drm_connector_register - register a connector
1009 * @connector: the connector to register
1010 *
1011 * Register userspace interfaces for a connector
1012 *
1013 * Returns:
1014 * Zero on success, error code on failure.
1015 */
1016int drm_connector_register(struct drm_connector *connector)
1017{
Thomas Wood30f65702014-06-18 17:52:32 +01001018 int ret;
1019
Dave Airlie2ee39452014-07-24 11:53:45 +10001020 drm_mode_object_register(connector->dev, &connector->base);
1021
Thomas Wood30f65702014-06-18 17:52:32 +01001022 ret = drm_sysfs_connector_add(connector);
1023 if (ret)
1024 return ret;
1025
1026 ret = drm_debugfs_connector_add(connector);
1027 if (ret) {
1028 drm_sysfs_connector_remove(connector);
1029 return ret;
1030 }
1031
1032 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001033}
1034EXPORT_SYMBOL(drm_connector_register);
1035
1036/**
1037 * drm_connector_unregister - unregister a connector
1038 * @connector: the connector to unregister
1039 *
1040 * Unregister userspace interfaces for a connector
1041 */
1042void drm_connector_unregister(struct drm_connector *connector)
1043{
1044 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001045 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001046}
1047EXPORT_SYMBOL(drm_connector_unregister);
1048
1049
1050/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001051 * drm_connector_unplug_all - unregister connector userspace interfaces
1052 * @dev: drm device
1053 *
1054 * This function unregisters all connector userspace interfaces in sysfs. Should
1055 * be call when the device is disconnected, e.g. from an usb driver's
1056 * ->disconnect callback.
1057 */
Dave Airliecbc7e222012-02-20 14:16:40 +00001058void drm_connector_unplug_all(struct drm_device *dev)
1059{
1060 struct drm_connector *connector;
1061
1062 /* taking the mode config mutex ends up in a clash with sysfs */
1063 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001064 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001065
1066}
1067EXPORT_SYMBOL(drm_connector_unplug_all);
1068
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001069/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001070 * drm_encoder_init - Init a preallocated encoder
1071 * @dev: drm device
1072 * @encoder: the encoder to init
1073 * @funcs: callbacks for this encoder
1074 * @encoder_type: user visible type of the encoder
1075 *
1076 * Initialises a preallocated encoder. Encoder should be
1077 * subclassed as part of driver encoder objects.
1078 *
1079 * Returns:
1080 * Zero on success, error code on failure.
1081 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001082int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001083 struct drm_encoder *encoder,
1084 const struct drm_encoder_funcs *funcs,
1085 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -08001086{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001087 int ret;
1088
Daniel Vetter84849902012-12-02 00:28:11 +01001089 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001090
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001091 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1092 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001093 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001094
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001095 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001096 encoder->encoder_type = encoder_type;
1097 encoder->funcs = funcs;
Jani Nikulae5748942014-05-14 16:58:20 +03001098 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1099 drm_encoder_enum_list[encoder_type].name,
1100 encoder->base.id);
1101 if (!encoder->name) {
1102 ret = -ENOMEM;
1103 goto out_put;
1104 }
Dave Airlief453ba02008-11-07 14:05:41 -08001105
1106 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1107 dev->mode_config.num_encoder++;
1108
Jani Nikulae5748942014-05-14 16:58:20 +03001109out_put:
1110 if (ret)
1111 drm_mode_object_put(dev, &encoder->base);
1112
1113out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001114 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001115
1116 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001117}
1118EXPORT_SYMBOL(drm_encoder_init);
1119
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001120/**
1121 * drm_encoder_cleanup - cleans up an initialised encoder
1122 * @encoder: encoder to cleanup
1123 *
1124 * Cleans up the encoder but doesn't free the object.
1125 */
Dave Airlief453ba02008-11-07 14:05:41 -08001126void drm_encoder_cleanup(struct drm_encoder *encoder)
1127{
1128 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001129
Daniel Vetter84849902012-12-02 00:28:11 +01001130 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001131 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001132 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001133 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001134 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001135 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001136
1137 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001138}
1139EXPORT_SYMBOL(drm_encoder_cleanup);
1140
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001141/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001142 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001143 * @dev: DRM device
1144 * @plane: plane object to init
1145 * @possible_crtcs: bitmask of possible CRTCs
1146 * @funcs: callbacks for the new plane
1147 * @formats: array of supported formats (%DRM_FORMAT_*)
1148 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001149 * @type: type of plane (overlay, primary, cursor)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001150 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001151 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001152 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001153 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001154 * Zero on success, error code on failure.
1155 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001156int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1157 unsigned long possible_crtcs,
1158 const struct drm_plane_funcs *funcs,
1159 const uint32_t *formats, uint32_t format_count,
1160 enum drm_plane_type type)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001161{
Rob Clark6b4959f2014-12-18 16:01:53 -05001162 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001163 int ret;
1164
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001165 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1166 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001167 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001168
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001169 drm_modeset_lock_init(&plane->mutex);
1170
Rob Clark4d939142012-05-17 02:23:27 -06001171 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001172 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001173 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001174 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1175 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001176 if (!plane->format_types) {
1177 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1178 drm_mode_object_put(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001179 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001180 }
1181
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001182 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001183 plane->format_count = format_count;
1184 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001185 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001186
Rob Clark6b4959f2014-12-18 16:01:53 -05001187 list_add_tail(&plane->head, &config->plane_list);
1188 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001189 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001190 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001191
Rob Clark9922ab52014-04-01 20:16:57 -04001192 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001193 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001194 plane->type);
1195
Rob Clark6b4959f2014-12-18 16:01:53 -05001196 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1197 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1198 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1199 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1200 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1201 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1202 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1203 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1204 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1205 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1206 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1207 }
1208
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001209 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001210}
Matt Roperdc415ff2014-04-01 15:22:36 -07001211EXPORT_SYMBOL(drm_universal_plane_init);
1212
1213/**
1214 * drm_plane_init - Initialize a legacy plane
1215 * @dev: DRM device
1216 * @plane: plane object to init
1217 * @possible_crtcs: bitmask of possible CRTCs
1218 * @funcs: callbacks for the new plane
1219 * @formats: array of supported formats (%DRM_FORMAT_*)
1220 * @format_count: number of elements in @formats
1221 * @is_primary: plane type (primary vs overlay)
1222 *
1223 * Legacy API to initialize a DRM plane.
1224 *
1225 * New drivers should call drm_universal_plane_init() instead.
1226 *
1227 * Returns:
1228 * Zero on success, error code on failure.
1229 */
1230int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1231 unsigned long possible_crtcs,
1232 const struct drm_plane_funcs *funcs,
1233 const uint32_t *formats, uint32_t format_count,
1234 bool is_primary)
1235{
1236 enum drm_plane_type type;
1237
1238 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1239 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1240 formats, format_count, type);
1241}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001242EXPORT_SYMBOL(drm_plane_init);
1243
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001244/**
1245 * drm_plane_cleanup - Clean up the core plane usage
1246 * @plane: plane to cleanup
1247 *
1248 * This function cleans up @plane and removes it from the DRM mode setting
1249 * core. Note that the function does *not* free the plane structure itself,
1250 * this is the responsibility of the caller.
1251 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001252void drm_plane_cleanup(struct drm_plane *plane)
1253{
1254 struct drm_device *dev = plane->dev;
1255
Daniel Vetter84849902012-12-02 00:28:11 +01001256 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001257 kfree(plane->format_types);
1258 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001259
1260 BUG_ON(list_empty(&plane->head));
1261
1262 list_del(&plane->head);
1263 dev->mode_config.num_total_plane--;
1264 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1265 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001266 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001267
1268 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1269 if (plane->state && plane->funcs->atomic_destroy_state)
1270 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001271
1272 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001273}
1274EXPORT_SYMBOL(drm_plane_cleanup);
1275
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001276/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001277 * drm_plane_index - find the index of a registered plane
1278 * @plane: plane to find index for
1279 *
1280 * Given a registered plane, return the index of that CRTC within a DRM
1281 * device's list of planes.
1282 */
1283unsigned int drm_plane_index(struct drm_plane *plane)
1284{
1285 unsigned int index = 0;
1286 struct drm_plane *tmp;
1287
1288 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1289 if (tmp == plane)
1290 return index;
1291
1292 index++;
1293 }
1294
1295 BUG();
1296}
1297EXPORT_SYMBOL(drm_plane_index);
1298
1299/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001300 * drm_plane_force_disable - Forcibly disable a plane
1301 * @plane: plane to disable
1302 *
1303 * Forces the plane to be disabled.
1304 *
1305 * Used when the plane's current framebuffer is destroyed,
1306 * and when restoring fbdev mode.
1307 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001308void drm_plane_force_disable(struct drm_plane *plane)
1309{
1310 int ret;
1311
Daniel Vetter3d30a592014-07-27 13:42:42 +02001312 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001313 return;
1314
Daniel Vetter3d30a592014-07-27 13:42:42 +02001315 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001316 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001317 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001318 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001319 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001320 return;
1321 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001322 /* disconnect the plane from the fb and crtc: */
Daniel Vetter3d30a592014-07-27 13:42:42 +02001323 __drm_framebuffer_unreference(plane->old_fb);
1324 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001325 plane->fb = NULL;
1326 plane->crtc = NULL;
1327}
1328EXPORT_SYMBOL(drm_plane_force_disable);
1329
Rob Clark6b4959f2014-12-18 16:01:53 -05001330static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001331{
Rob Clark356af0e2014-12-18 16:01:52 -05001332 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001333
1334 /*
1335 * Standard properties (apply to all connectors)
1336 */
Rob Clark356af0e2014-12-18 16:01:52 -05001337 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001338 DRM_MODE_PROP_IMMUTABLE,
1339 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001340 if (!prop)
1341 return -ENOMEM;
1342 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001343
Rob Clark356af0e2014-12-18 16:01:52 -05001344 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001345 "DPMS", drm_dpms_enum_list,
1346 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001347 if (!prop)
1348 return -ENOMEM;
1349 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001350
Rob Clark356af0e2014-12-18 16:01:52 -05001351 prop = drm_property_create(dev,
1352 DRM_MODE_PROP_BLOB |
1353 DRM_MODE_PROP_IMMUTABLE,
1354 "PATH", 0);
1355 if (!prop)
1356 return -ENOMEM;
1357 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001358
Rob Clark356af0e2014-12-18 16:01:52 -05001359 prop = drm_property_create(dev,
1360 DRM_MODE_PROP_BLOB |
1361 DRM_MODE_PROP_IMMUTABLE,
1362 "TILE", 0);
1363 if (!prop)
1364 return -ENOMEM;
1365 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001366
Rob Clark6b4959f2014-12-18 16:01:53 -05001367 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001368 "type", drm_plane_type_enum_list,
1369 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001370 if (!prop)
1371 return -ENOMEM;
1372 dev->mode_config.plane_type_property = prop;
1373
1374 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1375 "SRC_X", 0, UINT_MAX);
1376 if (!prop)
1377 return -ENOMEM;
1378 dev->mode_config.prop_src_x = prop;
1379
1380 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1381 "SRC_Y", 0, UINT_MAX);
1382 if (!prop)
1383 return -ENOMEM;
1384 dev->mode_config.prop_src_y = prop;
1385
1386 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1387 "SRC_W", 0, UINT_MAX);
1388 if (!prop)
1389 return -ENOMEM;
1390 dev->mode_config.prop_src_w = prop;
1391
1392 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1393 "SRC_H", 0, UINT_MAX);
1394 if (!prop)
1395 return -ENOMEM;
1396 dev->mode_config.prop_src_h = prop;
1397
1398 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1399 "CRTC_X", INT_MIN, INT_MAX);
1400 if (!prop)
1401 return -ENOMEM;
1402 dev->mode_config.prop_crtc_x = prop;
1403
1404 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1405 "CRTC_Y", INT_MIN, INT_MAX);
1406 if (!prop)
1407 return -ENOMEM;
1408 dev->mode_config.prop_crtc_y = prop;
1409
1410 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1411 "CRTC_W", 0, INT_MAX);
1412 if (!prop)
1413 return -ENOMEM;
1414 dev->mode_config.prop_crtc_w = prop;
1415
1416 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1417 "CRTC_H", 0, INT_MAX);
1418 if (!prop)
1419 return -ENOMEM;
1420 dev->mode_config.prop_crtc_h = prop;
1421
1422 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1423 "FB_ID", DRM_MODE_OBJECT_FB);
1424 if (!prop)
1425 return -ENOMEM;
1426 dev->mode_config.prop_fb_id = prop;
1427
1428 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1429 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1430 if (!prop)
1431 return -ENOMEM;
1432 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001433
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001434 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1435 "ACTIVE");
1436 if (!prop)
1437 return -ENOMEM;
1438 dev->mode_config.prop_active = prop;
1439
Rob Clark9922ab52014-04-01 20:16:57 -04001440 return 0;
1441}
1442
Dave Airlief453ba02008-11-07 14:05:41 -08001443/**
1444 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1445 * @dev: DRM device
1446 *
1447 * Called by a driver the first time a DVI-I connector is made.
1448 */
1449int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1450{
1451 struct drm_property *dvi_i_selector;
1452 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001453
1454 if (dev->mode_config.dvi_i_select_subconnector_property)
1455 return 0;
1456
1457 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001458 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001459 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001460 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001461 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001462 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1463
Sascha Hauer4a67d392012-02-06 10:58:17 +01001464 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001465 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001466 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001467 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001468 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1469
1470 return 0;
1471}
1472EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1473
1474/**
1475 * drm_create_tv_properties - create TV specific connector properties
1476 * @dev: DRM device
1477 * @num_modes: number of different TV formats (modes) supported
1478 * @modes: array of pointers to strings containing name of each format
1479 *
1480 * Called by a driver's TV initialization routine, this function creates
1481 * the TV specific connector properties for a given device. Caller is
1482 * responsible for allocating a list of format names and passing them to
1483 * this routine.
1484 */
Thierry Reding2f763312014-10-13 12:45:57 +02001485int drm_mode_create_tv_properties(struct drm_device *dev,
1486 unsigned int num_modes,
Dave Airlief453ba02008-11-07 14:05:41 -08001487 char *modes[])
1488{
1489 struct drm_property *tv_selector;
1490 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001491 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001492
1493 if (dev->mode_config.tv_select_subconnector_property)
1494 return 0;
1495
1496 /*
1497 * Basic connector properties
1498 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001499 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001500 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001501 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001502 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001503 dev->mode_config.tv_select_subconnector_property = tv_selector;
1504
1505 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001506 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1507 "subconnector",
1508 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001509 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001510 dev->mode_config.tv_subconnector_property = tv_subconnector;
1511
1512 /*
1513 * Other, TV specific properties: margins & TV modes.
1514 */
1515 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001516 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001517
1518 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001519 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001520
1521 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001522 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001523
1524 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001525 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001526
1527 dev->mode_config.tv_mode_property =
1528 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1529 "mode", num_modes);
1530 for (i = 0; i < num_modes; i++)
1531 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1532 i, modes[i]);
1533
Francisco Jerezb6b79022009-08-02 04:19:20 +02001534 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001535 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001536
1537 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001538 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001539
1540 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001541 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001542
Francisco Jereza75f0232009-08-12 02:30:10 +02001543 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001544 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001545
1546 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001547 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001548
1549 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001550 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001551
Dave Airlief453ba02008-11-07 14:05:41 -08001552 return 0;
1553}
1554EXPORT_SYMBOL(drm_mode_create_tv_properties);
1555
1556/**
1557 * drm_mode_create_scaling_mode_property - create scaling mode property
1558 * @dev: DRM device
1559 *
1560 * Called by a driver the first time it's needed, must be attached to desired
1561 * connectors.
1562 */
1563int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1564{
1565 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001566
1567 if (dev->mode_config.scaling_mode_property)
1568 return 0;
1569
1570 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001571 drm_property_create_enum(dev, 0, "scaling mode",
1572 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001573 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001574
1575 dev->mode_config.scaling_mode_property = scaling_mode;
1576
1577 return 0;
1578}
1579EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1580
1581/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301582 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1583 * @dev: DRM device
1584 *
1585 * Called by a driver the first time it's needed, must be attached to desired
1586 * connectors.
1587 *
1588 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001589 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301590 */
1591int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1592{
1593 if (dev->mode_config.aspect_ratio_property)
1594 return 0;
1595
1596 dev->mode_config.aspect_ratio_property =
1597 drm_property_create_enum(dev, 0, "aspect ratio",
1598 drm_aspect_ratio_enum_list,
1599 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1600
1601 if (dev->mode_config.aspect_ratio_property == NULL)
1602 return -ENOMEM;
1603
1604 return 0;
1605}
1606EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1607
1608/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001609 * drm_mode_create_dirty_property - create dirty property
1610 * @dev: DRM device
1611 *
1612 * Called by a driver the first time it's needed, must be attached to desired
1613 * connectors.
1614 */
1615int drm_mode_create_dirty_info_property(struct drm_device *dev)
1616{
1617 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001618
1619 if (dev->mode_config.dirty_info_property)
1620 return 0;
1621
1622 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001623 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001624 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001625 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001626 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001627 dev->mode_config.dirty_info_property = dirty_info;
1628
1629 return 0;
1630}
1631EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1632
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001633/**
1634 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1635 * @dev: DRM device
1636 *
1637 * Create the the suggested x/y offset property for connectors.
1638 */
1639int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1640{
1641 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1642 return 0;
1643
1644 dev->mode_config.suggested_x_property =
1645 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1646
1647 dev->mode_config.suggested_y_property =
1648 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1649
1650 if (dev->mode_config.suggested_x_property == NULL ||
1651 dev->mode_config.suggested_y_property == NULL)
1652 return -ENOMEM;
1653 return 0;
1654}
1655EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1656
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001657static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001658{
1659 uint32_t total_objects = 0;
1660
1661 total_objects += dev->mode_config.num_crtc;
1662 total_objects += dev->mode_config.num_connector;
1663 total_objects += dev->mode_config.num_encoder;
1664
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01001665 group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08001666 if (!group->id_list)
1667 return -ENOMEM;
1668
1669 group->num_crtcs = 0;
1670 group->num_connectors = 0;
1671 group->num_encoders = 0;
1672 return 0;
1673}
1674
Dave Airliead222792014-05-02 13:22:19 +10001675void drm_mode_group_destroy(struct drm_mode_group *group)
1676{
1677 kfree(group->id_list);
1678 group->id_list = NULL;
1679}
1680
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001681/*
1682 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1683 * the drm core's responsibility to set up mode control groups.
1684 */
Dave Airlief453ba02008-11-07 14:05:41 -08001685int drm_mode_group_init_legacy_group(struct drm_device *dev,
1686 struct drm_mode_group *group)
1687{
1688 struct drm_crtc *crtc;
1689 struct drm_encoder *encoder;
1690 struct drm_connector *connector;
1691 int ret;
1692
Thierry Reding0cc0b222014-12-10 13:03:37 +01001693 ret = drm_mode_group_init(dev, group);
1694 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08001695 return ret;
1696
1697 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1698 group->id_list[group->num_crtcs++] = crtc->base.id;
1699
1700 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1701 group->id_list[group->num_crtcs + group->num_encoders++] =
1702 encoder->base.id;
1703
1704 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1705 group->id_list[group->num_crtcs + group->num_encoders +
1706 group->num_connectors++] = connector->base.id;
1707
1708 return 0;
1709}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001710EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001711
Dave Airlie2390cd12014-06-05 14:01:29 +10001712void drm_reinit_primary_mode_group(struct drm_device *dev)
1713{
1714 drm_modeset_lock_all(dev);
1715 drm_mode_group_destroy(&dev->primary->mode_group);
1716 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1717 drm_modeset_unlock_all(dev);
1718}
1719EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1720
Dave Airlief453ba02008-11-07 14:05:41 -08001721/**
Dave Airlief453ba02008-11-07 14:05:41 -08001722 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1723 * @out: drm_mode_modeinfo struct to return to the user
1724 * @in: drm_display_mode to use
1725 *
Dave Airlief453ba02008-11-07 14:05:41 -08001726 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1727 * the user.
1728 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001729static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1730 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001731{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001732 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1733 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1734 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1735 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1736 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1737 "timing values too large for mode info\n");
1738
Dave Airlief453ba02008-11-07 14:05:41 -08001739 out->clock = in->clock;
1740 out->hdisplay = in->hdisplay;
1741 out->hsync_start = in->hsync_start;
1742 out->hsync_end = in->hsync_end;
1743 out->htotal = in->htotal;
1744 out->hskew = in->hskew;
1745 out->vdisplay = in->vdisplay;
1746 out->vsync_start = in->vsync_start;
1747 out->vsync_end = in->vsync_end;
1748 out->vtotal = in->vtotal;
1749 out->vscan = in->vscan;
1750 out->vrefresh = in->vrefresh;
1751 out->flags = in->flags;
1752 out->type = in->type;
1753 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1754 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1755}
1756
1757/**
Marc-André Lureau74afee72013-10-18 16:11:27 +02001758 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Dave Airlief453ba02008-11-07 14:05:41 -08001759 * @out: drm_display_mode to return to the user
1760 * @in: drm_mode_modeinfo to use
1761 *
Dave Airlief453ba02008-11-07 14:05:41 -08001762 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1763 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001764 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001765 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001766 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001767 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001768static int drm_crtc_convert_umode(struct drm_display_mode *out,
1769 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001770{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001771 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1772 return -ERANGE;
1773
Damien Lespiau5848ad42013-09-27 12:11:50 +01001774 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1775 return -EINVAL;
1776
Dave Airlief453ba02008-11-07 14:05:41 -08001777 out->clock = in->clock;
1778 out->hdisplay = in->hdisplay;
1779 out->hsync_start = in->hsync_start;
1780 out->hsync_end = in->hsync_end;
1781 out->htotal = in->htotal;
1782 out->hskew = in->hskew;
1783 out->vdisplay = in->vdisplay;
1784 out->vsync_start = in->vsync_start;
1785 out->vsync_end = in->vsync_end;
1786 out->vtotal = in->vtotal;
1787 out->vscan = in->vscan;
1788 out->vrefresh = in->vrefresh;
1789 out->flags = in->flags;
1790 out->type = in->type;
1791 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1792 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001793
1794 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001795}
1796
1797/**
1798 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001799 * @dev: drm device for the ioctl
1800 * @data: data pointer for the ioctl
1801 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001802 *
Dave Airlief453ba02008-11-07 14:05:41 -08001803 * Construct a set of configuration description structures and return
1804 * them to the user, including CRTC, connector and framebuffer configuration.
1805 *
1806 * Called by the user via ioctl.
1807 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001808 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001809 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001810 */
1811int drm_mode_getresources(struct drm_device *dev, void *data,
1812 struct drm_file *file_priv)
1813{
1814 struct drm_mode_card_res *card_res = data;
1815 struct list_head *lh;
1816 struct drm_framebuffer *fb;
1817 struct drm_connector *connector;
1818 struct drm_crtc *crtc;
1819 struct drm_encoder *encoder;
1820 int ret = 0;
1821 int connector_count = 0;
1822 int crtc_count = 0;
1823 int fb_count = 0;
1824 int encoder_count = 0;
1825 int copied = 0, i;
1826 uint32_t __user *fb_id;
1827 uint32_t __user *crtc_id;
1828 uint32_t __user *connector_id;
1829 uint32_t __user *encoder_id;
1830 struct drm_mode_group *mode_group;
1831
Dave Airliefb3b06c2011-02-08 13:55:21 +10001832 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1833 return -EINVAL;
1834
Dave Airlief453ba02008-11-07 14:05:41 -08001835
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001836 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001837 /*
1838 * For the non-control nodes we need to limit the list of resources
1839 * by IDs in the group list for this node
1840 */
1841 list_for_each(lh, &file_priv->fbs)
1842 fb_count++;
1843
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001844 /* handle this in 4 parts */
1845 /* FBs */
1846 if (card_res->count_fbs >= fb_count) {
1847 copied = 0;
1848 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1849 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1850 if (put_user(fb->base.id, fb_id + copied)) {
1851 mutex_unlock(&file_priv->fbs_lock);
1852 return -EFAULT;
1853 }
1854 copied++;
1855 }
1856 }
1857 card_res->count_fbs = fb_count;
1858 mutex_unlock(&file_priv->fbs_lock);
1859
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001860 /* mode_config.mutex protects the connector list against e.g. DP MST
1861 * connector hot-adding. CRTC/Plane lists are invariant. */
1862 mutex_lock(&dev->mode_config.mutex);
Thomas Hellstrom43683052014-03-13 11:07:44 +01001863 if (!drm_is_primary_client(file_priv)) {
Dave Airlief453ba02008-11-07 14:05:41 -08001864
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001865 mode_group = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001866 list_for_each(lh, &dev->mode_config.crtc_list)
1867 crtc_count++;
1868
1869 list_for_each(lh, &dev->mode_config.connector_list)
1870 connector_count++;
1871
1872 list_for_each(lh, &dev->mode_config.encoder_list)
1873 encoder_count++;
1874 } else {
1875
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001876 mode_group = &file_priv->master->minor->mode_group;
Dave Airlief453ba02008-11-07 14:05:41 -08001877 crtc_count = mode_group->num_crtcs;
1878 connector_count = mode_group->num_connectors;
1879 encoder_count = mode_group->num_encoders;
1880 }
1881
1882 card_res->max_height = dev->mode_config.max_height;
1883 card_res->min_height = dev->mode_config.min_height;
1884 card_res->max_width = dev->mode_config.max_width;
1885 card_res->min_width = dev->mode_config.min_width;
1886
Dave Airlief453ba02008-11-07 14:05:41 -08001887 /* CRTCs */
1888 if (card_res->count_crtcs >= crtc_count) {
1889 copied = 0;
1890 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001891 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001892 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1893 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001894 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001895 if (put_user(crtc->base.id, crtc_id + copied)) {
1896 ret = -EFAULT;
1897 goto out;
1898 }
1899 copied++;
1900 }
1901 } else {
1902 for (i = 0; i < mode_group->num_crtcs; i++) {
1903 if (put_user(mode_group->id_list[i],
1904 crtc_id + copied)) {
1905 ret = -EFAULT;
1906 goto out;
1907 }
1908 copied++;
1909 }
1910 }
1911 }
1912 card_res->count_crtcs = crtc_count;
1913
1914 /* Encoders */
1915 if (card_res->count_encoders >= encoder_count) {
1916 copied = 0;
1917 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001918 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001919 list_for_each_entry(encoder,
1920 &dev->mode_config.encoder_list,
1921 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001922 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
Jani Nikula83a8cfd2014-06-03 14:56:22 +03001923 encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001924 if (put_user(encoder->base.id, encoder_id +
1925 copied)) {
1926 ret = -EFAULT;
1927 goto out;
1928 }
1929 copied++;
1930 }
1931 } else {
1932 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1933 if (put_user(mode_group->id_list[i],
1934 encoder_id + copied)) {
1935 ret = -EFAULT;
1936 goto out;
1937 }
1938 copied++;
1939 }
1940
1941 }
1942 }
1943 card_res->count_encoders = encoder_count;
1944
1945 /* Connectors */
1946 if (card_res->count_connectors >= connector_count) {
1947 copied = 0;
1948 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01001949 if (!mode_group) {
Dave Airlief453ba02008-11-07 14:05:41 -08001950 list_for_each_entry(connector,
1951 &dev->mode_config.connector_list,
1952 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001953 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1954 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03001955 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001956 if (put_user(connector->base.id,
1957 connector_id + copied)) {
1958 ret = -EFAULT;
1959 goto out;
1960 }
1961 copied++;
1962 }
1963 } else {
1964 int start = mode_group->num_crtcs +
1965 mode_group->num_encoders;
1966 for (i = start; i < start + mode_group->num_connectors; i++) {
1967 if (put_user(mode_group->id_list[i],
1968 connector_id + copied)) {
1969 ret = -EFAULT;
1970 goto out;
1971 }
1972 copied++;
1973 }
1974 }
1975 }
1976 card_res->count_connectors = connector_count;
1977
Jerome Glisse94401062010-07-15 15:43:25 -04001978 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001979 card_res->count_connectors, card_res->count_encoders);
1980
1981out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001982 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001983 return ret;
1984}
1985
1986/**
1987 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001988 * @dev: drm device for the ioctl
1989 * @data: data pointer for the ioctl
1990 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001991 *
Dave Airlief453ba02008-11-07 14:05:41 -08001992 * Construct a CRTC configuration structure to return to the user.
1993 *
1994 * Called by the user via ioctl.
1995 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001996 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001997 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001998 */
1999int drm_mode_getcrtc(struct drm_device *dev,
2000 void *data, struct drm_file *file_priv)
2001{
2002 struct drm_mode_crtc *crtc_resp = data;
2003 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002004
Dave Airliefb3b06c2011-02-08 13:55:21 +10002005 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2006 return -EINVAL;
2007
Rob Clarka2b34e22013-10-05 16:36:52 -04002008 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002009 if (!crtc)
2010 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002011
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002012 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002013 crtc_resp->x = crtc->x;
2014 crtc_resp->y = crtc->y;
2015 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002016 if (crtc->primary->fb)
2017 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002018 else
2019 crtc_resp->fb_id = 0;
2020
2021 if (crtc->enabled) {
2022
2023 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
2024 crtc_resp->mode_valid = 1;
2025
2026 } else {
2027 crtc_resp->mode_valid = 0;
2028 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002029 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002030
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002031 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002032}
2033
Damien Lespiau61d8e322013-09-25 16:45:22 +01002034static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2035 const struct drm_file *file_priv)
2036{
2037 /*
2038 * If user-space hasn't configured the driver to expose the stereo 3D
2039 * modes, don't expose them.
2040 */
2041 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2042 return false;
2043
2044 return true;
2045}
2046
Daniel Vetterabd69c52014-11-25 23:50:05 +01002047static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2048{
2049 /* For atomic drivers only state objects are synchronously updated and
2050 * protected by modeset locks, so check those first. */
2051 if (connector->state)
2052 return connector->state->best_encoder;
2053 return connector->encoder;
2054}
2055
Rob Clark95cbf112014-12-18 16:01:49 -05002056/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002057static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002058 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2059 uint32_t *arg_count_props)
2060{
Rob Clark88a48e22014-12-18 16:01:50 -05002061 int props_count;
2062 int i, ret, copied;
2063
2064 props_count = obj->properties->count;
2065 if (!atomic)
2066 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002067
2068 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002069 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002070 struct drm_property *prop = obj->properties->properties[i];
2071 uint64_t val;
2072
Rob Clark88a48e22014-12-18 16:01:50 -05002073 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2074 continue;
2075
Rob Clark95cbf112014-12-18 16:01:49 -05002076 ret = drm_object_property_get_value(obj, prop, &val);
2077 if (ret)
2078 return ret;
2079
2080 if (put_user(prop->base.id, prop_ptr + copied))
2081 return -EFAULT;
2082
2083 if (put_user(val, prop_values + copied))
2084 return -EFAULT;
2085
2086 copied++;
2087 }
2088 }
2089 *arg_count_props = props_count;
2090
2091 return 0;
2092}
2093
Dave Airlief453ba02008-11-07 14:05:41 -08002094/**
2095 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002096 * @dev: drm device for the ioctl
2097 * @data: data pointer for the ioctl
2098 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002099 *
Dave Airlief453ba02008-11-07 14:05:41 -08002100 * Construct a connector configuration structure to return to the user.
2101 *
2102 * Called by the user via ioctl.
2103 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002104 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002105 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002106 */
2107int drm_mode_getconnector(struct drm_device *dev, void *data,
2108 struct drm_file *file_priv)
2109{
2110 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002111 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002112 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002113 struct drm_display_mode *mode;
2114 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002115 int encoders_count = 0;
2116 int ret = 0;
2117 int copied = 0;
2118 int i;
2119 struct drm_mode_modeinfo u_mode;
2120 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002121 uint32_t __user *encoder_ptr;
2122
Dave Airliefb3b06c2011-02-08 13:55:21 +10002123 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2124 return -EINVAL;
2125
Dave Airlief453ba02008-11-07 14:05:41 -08002126 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2127
Jerome Glisse94401062010-07-15 15:43:25 -04002128 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002129
Daniel Vetter7b240562012-12-12 00:35:33 +01002130 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002131
Rob Clarka2b34e22013-10-05 16:36:52 -04002132 connector = drm_connector_find(dev, out_resp->connector_id);
2133 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002134 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002135 goto out;
2136 }
Dave Airlief453ba02008-11-07 14:05:41 -08002137
Thierry Reding01073b02014-12-10 13:03:38 +01002138 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2139 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002140 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002141
2142 if (out_resp->count_modes == 0) {
2143 connector->funcs->fill_modes(connector,
2144 dev->mode_config.max_width,
2145 dev->mode_config.max_height);
2146 }
2147
2148 /* delayed so we get modes regardless of pre-fill_modes state */
2149 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002150 if (drm_mode_expose_to_userspace(mode, file_priv))
2151 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002152
2153 out_resp->connector_id = connector->base.id;
2154 out_resp->connector_type = connector->connector_type;
2155 out_resp->connector_type_id = connector->connector_type_id;
2156 out_resp->mm_width = connector->display_info.width_mm;
2157 out_resp->mm_height = connector->display_info.height_mm;
2158 out_resp->subpixel = connector->display_info.subpixel_order;
2159 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002160
2161 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002162 encoder = drm_connector_get_encoder(connector);
2163 if (encoder)
2164 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002165 else
2166 out_resp->encoder_id = 0;
2167
2168 /*
2169 * This ioctl is called twice, once to determine how much space is
2170 * needed, and the 2nd time to fill it.
2171 */
2172 if ((out_resp->count_modes >= mode_count) && mode_count) {
2173 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002174 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002175 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002176 if (!drm_mode_expose_to_userspace(mode, file_priv))
2177 continue;
2178
Dave Airlief453ba02008-11-07 14:05:41 -08002179 drm_crtc_convert_to_umode(&u_mode, mode);
2180 if (copy_to_user(mode_ptr + copied,
2181 &u_mode, sizeof(u_mode))) {
2182 ret = -EFAULT;
2183 goto out;
2184 }
2185 copied++;
2186 }
2187 }
2188 out_resp->count_modes = mode_count;
2189
Rob Clark88a48e22014-12-18 16:01:50 -05002190 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002191 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2192 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2193 &out_resp->count_props);
2194 if (ret)
2195 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002196
2197 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2198 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002199 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002200 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2201 if (connector->encoder_ids[i] != 0) {
2202 if (put_user(connector->encoder_ids[i],
2203 encoder_ptr + copied)) {
2204 ret = -EFAULT;
2205 goto out;
2206 }
2207 copied++;
2208 }
2209 }
2210 }
2211 out_resp->count_encoders = encoders_count;
2212
2213out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002214 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Daniel Vetter7b240562012-12-12 00:35:33 +01002215 mutex_unlock(&dev->mode_config.mutex);
2216
Dave Airlief453ba02008-11-07 14:05:41 -08002217 return ret;
2218}
2219
Daniel Vetterabd69c52014-11-25 23:50:05 +01002220static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2221{
2222 struct drm_connector *connector;
2223 struct drm_device *dev = encoder->dev;
2224 bool uses_atomic = false;
2225
2226 /* For atomic drivers only state objects are synchronously updated and
2227 * protected by modeset locks, so check those first. */
2228 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2229 if (!connector->state)
2230 continue;
2231
2232 uses_atomic = true;
2233
2234 if (connector->state->best_encoder != encoder)
2235 continue;
2236
2237 return connector->state->crtc;
2238 }
2239
2240 /* Don't return stale data (e.g. pending async disable). */
2241 if (uses_atomic)
2242 return NULL;
2243
2244 return encoder->crtc;
2245}
2246
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002247/**
2248 * drm_mode_getencoder - get encoder configuration
2249 * @dev: drm device for the ioctl
2250 * @data: data pointer for the ioctl
2251 * @file_priv: drm file for the ioctl call
2252 *
2253 * Construct a encoder configuration structure to return to the user.
2254 *
2255 * Called by the user via ioctl.
2256 *
2257 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002258 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002259 */
Dave Airlief453ba02008-11-07 14:05:41 -08002260int drm_mode_getencoder(struct drm_device *dev, void *data,
2261 struct drm_file *file_priv)
2262{
2263 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002264 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002265 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002266
Dave Airliefb3b06c2011-02-08 13:55:21 +10002267 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2268 return -EINVAL;
2269
Rob Clarka2b34e22013-10-05 16:36:52 -04002270 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002271 if (!encoder)
2272 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002273
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002274 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002275 crtc = drm_encoder_get_crtc(encoder);
2276 if (crtc)
2277 enc_resp->crtc_id = crtc->base.id;
2278 else if (encoder->crtc)
Dave Airlief453ba02008-11-07 14:05:41 -08002279 enc_resp->crtc_id = encoder->crtc->base.id;
2280 else
2281 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002282 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2283
Dave Airlief453ba02008-11-07 14:05:41 -08002284 enc_resp->encoder_type = encoder->encoder_type;
2285 enc_resp->encoder_id = encoder->base.id;
2286 enc_resp->possible_crtcs = encoder->possible_crtcs;
2287 enc_resp->possible_clones = encoder->possible_clones;
2288
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002289 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002290}
2291
2292/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002293 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002294 * @dev: DRM device
2295 * @data: ioctl data
2296 * @file_priv: DRM file info
2297 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002298 * Construct a list of plane ids to return to the user.
2299 *
2300 * Called by the user via ioctl.
2301 *
2302 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002303 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002304 */
2305int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002306 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002307{
2308 struct drm_mode_get_plane_res *plane_resp = data;
2309 struct drm_mode_config *config;
2310 struct drm_plane *plane;
2311 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002312 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002313 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002314
2315 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2316 return -EINVAL;
2317
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002318 config = &dev->mode_config;
2319
Matt Roper681e7ec2014-04-01 15:22:42 -07002320 if (file_priv->universal_planes)
2321 num_planes = config->num_total_plane;
2322 else
2323 num_planes = config->num_overlay_plane;
2324
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325 /*
2326 * This ioctl is called twice, once to determine how much space is
2327 * needed, and the 2nd time to fill it.
2328 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002329 if (num_planes &&
2330 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002331 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002332
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002333 /* Plane lists are invariant, no locking needed. */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002334 list_for_each_entry(plane, &config->plane_list, head) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002335 /*
2336 * Unless userspace set the 'universal planes'
2337 * capability bit, only advertise overlays.
2338 */
2339 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2340 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002341 continue;
2342
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002343 if (put_user(plane->base.id, plane_ptr + copied))
2344 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002345 copied++;
2346 }
2347 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002348 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002349
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002350 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002351}
2352
2353/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002354 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002355 * @dev: DRM device
2356 * @data: ioctl data
2357 * @file_priv: DRM file info
2358 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002359 * Construct a plane configuration structure to return to the user.
2360 *
2361 * Called by the user via ioctl.
2362 *
2363 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002364 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002365 */
2366int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002367 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002368{
2369 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002370 struct drm_plane *plane;
2371 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002372
2373 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2374 return -EINVAL;
2375
Rob Clarka2b34e22013-10-05 16:36:52 -04002376 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002377 if (!plane)
2378 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002379
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002380 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002381 if (plane->crtc)
2382 plane_resp->crtc_id = plane->crtc->base.id;
2383 else
2384 plane_resp->crtc_id = 0;
2385
2386 if (plane->fb)
2387 plane_resp->fb_id = plane->fb->base.id;
2388 else
2389 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002390 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002391
2392 plane_resp->plane_id = plane->base.id;
2393 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002394 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002395
2396 /*
2397 * This ioctl is called twice, once to determine how much space is
2398 * needed, and the 2nd time to fill it.
2399 */
2400 if (plane->format_count &&
2401 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002402 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002403 if (copy_to_user(format_ptr,
2404 plane->format_types,
2405 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002406 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002407 }
2408 }
2409 plane_resp->count_format_types = plane->format_count;
2410
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002411 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002412}
2413
Matt Roperb36552b2014-06-10 08:28:09 -07002414/*
2415 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002416 *
Matt Roperb36552b2014-06-10 08:28:09 -07002417 * Note that we assume an extra reference has already been taken on fb. If the
2418 * update fails, this reference will be dropped before return; if it succeeds,
2419 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002420 *
Matt Roperb36552b2014-06-10 08:28:09 -07002421 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002422 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002423static int __setplane_internal(struct drm_plane *plane,
2424 struct drm_crtc *crtc,
2425 struct drm_framebuffer *fb,
2426 int32_t crtc_x, int32_t crtc_y,
2427 uint32_t crtc_w, uint32_t crtc_h,
2428 /* src_{x,y,w,h} values are 16.16 fixed point */
2429 uint32_t src_x, uint32_t src_y,
2430 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002431{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002432 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002433 unsigned int fb_width, fb_height;
Thierry Reding2f763312014-10-13 12:45:57 +02002434 unsigned int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002435
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002436 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002437 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002438 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002439 ret = plane->funcs->disable_plane(plane);
2440 if (!ret) {
2441 plane->crtc = NULL;
2442 plane->fb = NULL;
2443 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002444 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002445 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002446 goto out;
2447 }
2448
Matt Roper7f994f32014-05-29 08:06:51 -07002449 /* Check whether this plane is usable on this CRTC */
2450 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2451 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2452 ret = -EINVAL;
2453 goto out;
2454 }
2455
Ville Syrjälä62443be2011-12-20 00:06:47 +02002456 /* Check whether this plane supports the fb pixel format. */
2457 for (i = 0; i < plane->format_count; i++)
2458 if (fb->pixel_format == plane->format_types[i])
2459 break;
2460 if (i == plane->format_count) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002461 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2462 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002463 ret = -EINVAL;
2464 goto out;
2465 }
2466
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002467 fb_width = fb->width << 16;
2468 fb_height = fb->height << 16;
2469
2470 /* Make sure source coordinates are inside the fb. */
Matt Roperb36552b2014-06-10 08:28:09 -07002471 if (src_w > fb_width ||
2472 src_x > fb_width - src_w ||
2473 src_h > fb_height ||
2474 src_y > fb_height - src_h) {
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002475 DRM_DEBUG_KMS("Invalid source coordinates "
2476 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
Matt Roperb36552b2014-06-10 08:28:09 -07002477 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2478 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2479 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2480 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002481 ret = -ENOSPC;
2482 goto out;
2483 }
2484
Daniel Vetter3d30a592014-07-27 13:42:42 +02002485 plane->old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002486 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002487 crtc_x, crtc_y, crtc_w, crtc_h,
2488 src_x, src_y, src_w, src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002489 if (!ret) {
2490 plane->crtc = crtc;
2491 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002492 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002493 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002494 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002495 }
2496
2497out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002498 if (fb)
2499 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002500 if (plane->old_fb)
2501 drm_framebuffer_unreference(plane->old_fb);
2502 plane->old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002503
2504 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002505}
Matt Roperb36552b2014-06-10 08:28:09 -07002506
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002507static int setplane_internal(struct drm_plane *plane,
2508 struct drm_crtc *crtc,
2509 struct drm_framebuffer *fb,
2510 int32_t crtc_x, int32_t crtc_y,
2511 uint32_t crtc_w, uint32_t crtc_h,
2512 /* src_{x,y,w,h} values are 16.16 fixed point */
2513 uint32_t src_x, uint32_t src_y,
2514 uint32_t src_w, uint32_t src_h)
2515{
2516 int ret;
2517
2518 drm_modeset_lock_all(plane->dev);
2519 ret = __setplane_internal(plane, crtc, fb,
2520 crtc_x, crtc_y, crtc_w, crtc_h,
2521 src_x, src_y, src_w, src_h);
2522 drm_modeset_unlock_all(plane->dev);
2523
2524 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002525}
2526
2527/**
2528 * drm_mode_setplane - configure a plane's configuration
2529 * @dev: DRM device
2530 * @data: ioctl data*
2531 * @file_priv: DRM file info
2532 *
2533 * Set plane configuration, including placement, fb, scaling, and other factors.
2534 * Or pass a NULL fb to disable (planes may be disabled without providing a
2535 * valid crtc).
2536 *
2537 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002538 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002539 */
2540int drm_mode_setplane(struct drm_device *dev, void *data,
2541 struct drm_file *file_priv)
2542{
2543 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002544 struct drm_plane *plane;
2545 struct drm_crtc *crtc = NULL;
2546 struct drm_framebuffer *fb = NULL;
2547
2548 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2549 return -EINVAL;
2550
2551 /* Give drivers some help against integer overflows */
2552 if (plane_req->crtc_w > INT_MAX ||
2553 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2554 plane_req->crtc_h > INT_MAX ||
2555 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2556 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2557 plane_req->crtc_w, plane_req->crtc_h,
2558 plane_req->crtc_x, plane_req->crtc_y);
2559 return -ERANGE;
2560 }
2561
2562 /*
2563 * First, find the plane, crtc, and fb objects. If not available,
2564 * we don't bother to call the driver.
2565 */
Rob Clark933f6222014-11-25 20:33:11 -05002566 plane = drm_plane_find(dev, plane_req->plane_id);
2567 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002568 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2569 plane_req->plane_id);
2570 return -ENOENT;
2571 }
Matt Roperb36552b2014-06-10 08:28:09 -07002572
2573 if (plane_req->fb_id) {
2574 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2575 if (!fb) {
2576 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2577 plane_req->fb_id);
2578 return -ENOENT;
2579 }
2580
Rob Clark933f6222014-11-25 20:33:11 -05002581 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2582 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002583 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2584 plane_req->crtc_id);
2585 return -ENOENT;
2586 }
Matt Roperb36552b2014-06-10 08:28:09 -07002587 }
2588
Matt Roper161d0dc2014-06-10 08:28:10 -07002589 /*
2590 * setplane_internal will take care of deref'ing either the old or new
2591 * framebuffer depending on success.
2592 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002593 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002594 plane_req->crtc_x, plane_req->crtc_y,
2595 plane_req->crtc_w, plane_req->crtc_h,
2596 plane_req->src_x, plane_req->src_y,
2597 plane_req->src_w, plane_req->src_h);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002598}
2599
2600/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002601 * drm_mode_set_config_internal - helper to call ->set_config
2602 * @set: modeset config to set
2603 *
2604 * This is a little helper to wrap internal calls to the ->set_config driver
2605 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002606 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002607 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002608 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002609 */
2610int drm_mode_set_config_internal(struct drm_mode_set *set)
2611{
2612 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002613 struct drm_framebuffer *fb;
2614 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002615 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002616
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002617 /*
2618 * NOTE: ->set_config can also disable other crtcs (if we steal all
2619 * connectors from it), hence we need to refcount the fbs across all
2620 * crtcs. Atomic modeset will have saner semantics ...
2621 */
2622 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002623 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002624
Daniel Vetterb0d12322012-12-11 01:07:12 +01002625 fb = set->fb;
2626
2627 ret = crtc->funcs->set_config(set);
2628 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002629 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002630 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002631 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002632
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002633 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07002634 if (tmp->primary->fb)
2635 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002636 if (tmp->primary->old_fb)
2637 drm_framebuffer_unreference(tmp->primary->old_fb);
2638 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002639 }
2640
2641 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002642}
2643EXPORT_SYMBOL(drm_mode_set_config_internal);
2644
Matt Roperaf936292014-04-01 15:22:34 -07002645/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002646 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2647 * @mode: mode to query
2648 * @hdisplay: hdisplay value to fill in
2649 * @vdisplay: vdisplay value to fill in
2650 *
2651 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2652 * the appropriate layout.
2653 */
2654void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2655 int *hdisplay, int *vdisplay)
2656{
2657 struct drm_display_mode adjusted;
2658
2659 drm_mode_copy(&adjusted, mode);
2660 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2661 *hdisplay = adjusted.crtc_hdisplay;
2662 *vdisplay = adjusted.crtc_vdisplay;
2663}
2664EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2665
2666/**
Matt Roperaf936292014-04-01 15:22:34 -07002667 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2668 * CRTC viewport
2669 * @crtc: CRTC that framebuffer will be displayed on
2670 * @x: x panning
2671 * @y: y panning
2672 * @mode: mode that framebuffer will be displayed under
2673 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002674 */
Matt Roperaf936292014-04-01 15:22:34 -07002675int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2676 int x, int y,
2677 const struct drm_display_mode *mode,
2678 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002679
2680{
2681 int hdisplay, vdisplay;
2682
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002683 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002684
Damien Lespiauc11e9282013-09-25 16:45:30 +01002685 if (crtc->invert_dimensions)
2686 swap(hdisplay, vdisplay);
2687
2688 if (hdisplay > fb->width ||
2689 vdisplay > fb->height ||
2690 x > fb->width - hdisplay ||
2691 y > fb->height - vdisplay) {
2692 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2693 fb->width, fb->height, hdisplay, vdisplay, x, y,
2694 crtc->invert_dimensions ? " (inverted)" : "");
2695 return -ENOSPC;
2696 }
2697
2698 return 0;
2699}
Matt Roperaf936292014-04-01 15:22:34 -07002700EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002701
Daniel Vetter2d13b672012-12-11 13:47:23 +01002702/**
Dave Airlief453ba02008-11-07 14:05:41 -08002703 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002704 * @dev: drm device for the ioctl
2705 * @data: data pointer for the ioctl
2706 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002707 *
Dave Airlief453ba02008-11-07 14:05:41 -08002708 * Build a new CRTC configuration based on user request.
2709 *
2710 * Called by the user via ioctl.
2711 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002712 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002713 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002714 */
2715int drm_mode_setcrtc(struct drm_device *dev, void *data,
2716 struct drm_file *file_priv)
2717{
2718 struct drm_mode_config *config = &dev->mode_config;
2719 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002720 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002721 struct drm_connector **connector_set = NULL, *connector;
2722 struct drm_framebuffer *fb = NULL;
2723 struct drm_display_mode *mode = NULL;
2724 struct drm_mode_set set;
2725 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002726 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002727 int i;
2728
Dave Airliefb3b06c2011-02-08 13:55:21 +10002729 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2730 return -EINVAL;
2731
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002732 /* For some reason crtc x/y offsets are signed internally. */
2733 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2734 return -ERANGE;
2735
Daniel Vetter84849902012-12-02 00:28:11 +01002736 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002737 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2738 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002739 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002740 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002741 goto out;
2742 }
Jerome Glisse94401062010-07-15 15:43:25 -04002743 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002744
2745 if (crtc_req->mode_valid) {
2746 /* If we have a mode we need a framebuffer. */
2747 /* If we pass -1, set the mode with the currently bound fb */
2748 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002749 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002750 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2751 ret = -EINVAL;
2752 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002753 }
Matt Roperf4510a22014-04-01 15:22:40 -07002754 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002755 /* Make refcounting symmetric with the lookup path. */
2756 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002757 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002758 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2759 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002760 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2761 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002762 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002763 goto out;
2764 }
Dave Airlief453ba02008-11-07 14:05:41 -08002765 }
2766
2767 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002768 if (!mode) {
2769 ret = -ENOMEM;
2770 goto out;
2771 }
2772
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002773 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2774 if (ret) {
2775 DRM_DEBUG_KMS("Invalid mode\n");
2776 goto out;
2777 }
2778
Ville Syrjälä23e1ce82014-12-17 13:56:24 +02002779 mode->status = drm_mode_validate_basic(mode);
2780 if (mode->status != MODE_OK) {
2781 ret = -EINVAL;
2782 goto out;
2783 }
2784
Dave Airlief453ba02008-11-07 14:05:41 -08002785 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002786
Damien Lespiauc11e9282013-09-25 16:45:30 +01002787 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2788 mode, fb);
2789 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002790 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002791
Dave Airlief453ba02008-11-07 14:05:41 -08002792 }
2793
2794 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002795 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002796 ret = -EINVAL;
2797 goto out;
2798 }
2799
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002800 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002801 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002802 crtc_req->count_connectors);
2803 ret = -EINVAL;
2804 goto out;
2805 }
2806
2807 if (crtc_req->count_connectors > 0) {
2808 u32 out_id;
2809
2810 /* Avoid unbounded kernel memory allocation */
2811 if (crtc_req->count_connectors > config->num_connector) {
2812 ret = -EINVAL;
2813 goto out;
2814 }
2815
Thierry Reding2f6c5382014-12-10 13:03:36 +01002816 connector_set = kmalloc_array(crtc_req->count_connectors,
2817 sizeof(struct drm_connector *),
2818 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002819 if (!connector_set) {
2820 ret = -ENOMEM;
2821 goto out;
2822 }
2823
2824 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002825 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002826 if (get_user(out_id, &set_connectors_ptr[i])) {
2827 ret = -EFAULT;
2828 goto out;
2829 }
2830
Rob Clarka2b34e22013-10-05 16:36:52 -04002831 connector = drm_connector_find(dev, out_id);
2832 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002833 DRM_DEBUG_KMS("Connector id %d unknown\n",
2834 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002835 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002836 goto out;
2837 }
Jerome Glisse94401062010-07-15 15:43:25 -04002838 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2839 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002840 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002841
2842 connector_set[i] = connector;
2843 }
2844 }
2845
2846 set.crtc = crtc;
2847 set.x = crtc_req->x;
2848 set.y = crtc_req->y;
2849 set.mode = mode;
2850 set.connectors = connector_set;
2851 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002852 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002853 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002854
2855out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002856 if (fb)
2857 drm_framebuffer_unreference(fb);
2858
Dave Airlief453ba02008-11-07 14:05:41 -08002859 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002860 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002861 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002862 return ret;
2863}
2864
Matt Roper161d0dc2014-06-10 08:28:10 -07002865/**
2866 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2867 * universal plane handler call
2868 * @crtc: crtc to update cursor for
2869 * @req: data pointer for the ioctl
2870 * @file_priv: drm file for the ioctl call
2871 *
2872 * Legacy cursor ioctl's work directly with driver buffer handles. To
2873 * translate legacy ioctl calls into universal plane handler calls, we need to
2874 * wrap the native buffer handle in a drm_framebuffer.
2875 *
2876 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2877 * buffer with a pitch of 4*width; the universal plane interface should be used
2878 * directly in cases where the hardware can support other buffer settings and
2879 * userspace wants to make use of these capabilities.
2880 *
2881 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002882 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002883 */
2884static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2885 struct drm_mode_cursor2 *req,
2886 struct drm_file *file_priv)
2887{
2888 struct drm_device *dev = crtc->dev;
2889 struct drm_framebuffer *fb = NULL;
2890 struct drm_mode_fb_cmd2 fbreq = {
2891 .width = req->width,
2892 .height = req->height,
2893 .pixel_format = DRM_FORMAT_ARGB8888,
2894 .pitches = { req->width * 4 },
2895 .handles = { req->handle },
2896 };
2897 int32_t crtc_x, crtc_y;
2898 uint32_t crtc_w = 0, crtc_h = 0;
2899 uint32_t src_w = 0, src_h = 0;
2900 int ret = 0;
2901
2902 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002903 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002904
2905 /*
2906 * Obtain fb we'll be using (either new or existing) and take an extra
2907 * reference to it if fb != null. setplane will take care of dropping
2908 * the reference if the plane update fails.
2909 */
2910 if (req->flags & DRM_MODE_CURSOR_BO) {
2911 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002912 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002913 if (IS_ERR(fb)) {
2914 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2915 return PTR_ERR(fb);
2916 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002917 } else {
2918 fb = NULL;
2919 }
2920 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002921 fb = crtc->cursor->fb;
2922 if (fb)
2923 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002924 }
2925
2926 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2927 crtc_x = req->x;
2928 crtc_y = req->y;
2929 } else {
2930 crtc_x = crtc->cursor_x;
2931 crtc_y = crtc->cursor_y;
2932 }
2933
2934 if (fb) {
2935 crtc_w = fb->width;
2936 crtc_h = fb->height;
2937 src_w = fb->width << 16;
2938 src_h = fb->height << 16;
2939 }
2940
2941 /*
2942 * setplane_internal will take care of deref'ing either the old or new
2943 * framebuffer depending on success.
2944 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002945 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002946 crtc_x, crtc_y, crtc_w, crtc_h,
2947 0, 0, src_w, src_h);
2948
2949 /* Update successful; save new cursor position, if necessary */
2950 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2951 crtc->cursor_x = req->x;
2952 crtc->cursor_y = req->y;
2953 }
2954
2955 return ret;
2956}
2957
Dave Airlie4c813d42013-06-20 11:48:52 +10002958static int drm_mode_cursor_common(struct drm_device *dev,
2959 struct drm_mode_cursor2 *req,
2960 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002961{
Dave Airlief453ba02008-11-07 14:05:41 -08002962 struct drm_crtc *crtc;
2963 int ret = 0;
2964
Dave Airliefb3b06c2011-02-08 13:55:21 +10002965 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2966 return -EINVAL;
2967
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002968 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002969 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002970
Rob Clarka2b34e22013-10-05 16:36:52 -04002971 crtc = drm_crtc_find(dev, req->crtc_id);
2972 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002973 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002974 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002975 }
Dave Airlief453ba02008-11-07 14:05:41 -08002976
Matt Roper161d0dc2014-06-10 08:28:10 -07002977 /*
2978 * If this crtc has a universal cursor plane, call that plane's update
2979 * handler rather than using legacy cursor handlers.
2980 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002981 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002982 if (crtc->cursor) {
2983 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2984 goto out;
2985 }
2986
Dave Airlief453ba02008-11-07 14:05:41 -08002987 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002988 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002989 ret = -ENXIO;
2990 goto out;
2991 }
2992 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002993 if (crtc->funcs->cursor_set2)
2994 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2995 req->width, req->height, req->hot_x, req->hot_y);
2996 else
2997 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2998 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002999 }
3000
3001 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3002 if (crtc->funcs->cursor_move) {
3003 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3004 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003005 ret = -EFAULT;
3006 goto out;
3007 }
3008 }
3009out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003010 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003011
Dave Airlief453ba02008-11-07 14:05:41 -08003012 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003013
3014}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003015
3016
3017/**
3018 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3019 * @dev: drm device for the ioctl
3020 * @data: data pointer for the ioctl
3021 * @file_priv: drm file for the ioctl call
3022 *
3023 * Set the cursor configuration based on user request.
3024 *
3025 * Called by the user via ioctl.
3026 *
3027 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003028 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003029 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003030int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003031 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003032{
3033 struct drm_mode_cursor *req = data;
3034 struct drm_mode_cursor2 new_req;
3035
3036 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3037 new_req.hot_x = new_req.hot_y = 0;
3038
3039 return drm_mode_cursor_common(dev, &new_req, file_priv);
3040}
3041
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003042/**
3043 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3044 * @dev: drm device for the ioctl
3045 * @data: data pointer for the ioctl
3046 * @file_priv: drm file for the ioctl call
3047 *
3048 * Set the cursor configuration based on user request. This implements the 2nd
3049 * version of the cursor ioctl, which allows userspace to additionally specify
3050 * the hotspot of the pointer.
3051 *
3052 * Called by the user via ioctl.
3053 *
3054 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003055 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003056 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003057int drm_mode_cursor2_ioctl(struct drm_device *dev,
3058 void *data, struct drm_file *file_priv)
3059{
3060 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003061
Dave Airlie4c813d42013-06-20 11:48:52 +10003062 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003063}
3064
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003065/**
3066 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3067 * @bpp: bits per pixels
3068 * @depth: bit depth per pixel
3069 *
3070 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3071 * Useful in fbdev emulation code, since that deals in those values.
3072 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003073uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3074{
3075 uint32_t fmt;
3076
3077 switch (bpp) {
3078 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003079 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003080 break;
3081 case 16:
3082 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003083 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003084 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003085 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003086 break;
3087 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003088 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003089 break;
3090 case 32:
3091 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003092 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003093 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003094 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003095 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003096 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003097 break;
3098 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003099 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3100 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003101 break;
3102 }
3103
3104 return fmt;
3105}
3106EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3107
Dave Airlief453ba02008-11-07 14:05:41 -08003108/**
3109 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003110 * @dev: drm device for the ioctl
3111 * @data: data pointer for the ioctl
3112 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003113 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003114 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003115 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003116 *
3117 * Called by the user via ioctl.
3118 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003119 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003120 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003121 */
3122int drm_mode_addfb(struct drm_device *dev,
3123 void *data, struct drm_file *file_priv)
3124{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003125 struct drm_mode_fb_cmd *or = data;
3126 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003127 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003128
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003129 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003130 r.fb_id = or->fb_id;
3131 r.width = or->width;
3132 r.height = or->height;
3133 r.pitches[0] = or->pitch;
3134 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3135 r.handles[0] = or->handle;
3136
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003137 ret = drm_mode_addfb2(dev, &r, file_priv);
3138 if (ret)
3139 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003140
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003141 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003142
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003143 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003144}
3145
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003146static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003147{
3148 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3149
3150 switch (format) {
3151 case DRM_FORMAT_C8:
3152 case DRM_FORMAT_RGB332:
3153 case DRM_FORMAT_BGR233:
3154 case DRM_FORMAT_XRGB4444:
3155 case DRM_FORMAT_XBGR4444:
3156 case DRM_FORMAT_RGBX4444:
3157 case DRM_FORMAT_BGRX4444:
3158 case DRM_FORMAT_ARGB4444:
3159 case DRM_FORMAT_ABGR4444:
3160 case DRM_FORMAT_RGBA4444:
3161 case DRM_FORMAT_BGRA4444:
3162 case DRM_FORMAT_XRGB1555:
3163 case DRM_FORMAT_XBGR1555:
3164 case DRM_FORMAT_RGBX5551:
3165 case DRM_FORMAT_BGRX5551:
3166 case DRM_FORMAT_ARGB1555:
3167 case DRM_FORMAT_ABGR1555:
3168 case DRM_FORMAT_RGBA5551:
3169 case DRM_FORMAT_BGRA5551:
3170 case DRM_FORMAT_RGB565:
3171 case DRM_FORMAT_BGR565:
3172 case DRM_FORMAT_RGB888:
3173 case DRM_FORMAT_BGR888:
3174 case DRM_FORMAT_XRGB8888:
3175 case DRM_FORMAT_XBGR8888:
3176 case DRM_FORMAT_RGBX8888:
3177 case DRM_FORMAT_BGRX8888:
3178 case DRM_FORMAT_ARGB8888:
3179 case DRM_FORMAT_ABGR8888:
3180 case DRM_FORMAT_RGBA8888:
3181 case DRM_FORMAT_BGRA8888:
3182 case DRM_FORMAT_XRGB2101010:
3183 case DRM_FORMAT_XBGR2101010:
3184 case DRM_FORMAT_RGBX1010102:
3185 case DRM_FORMAT_BGRX1010102:
3186 case DRM_FORMAT_ARGB2101010:
3187 case DRM_FORMAT_ABGR2101010:
3188 case DRM_FORMAT_RGBA1010102:
3189 case DRM_FORMAT_BGRA1010102:
3190 case DRM_FORMAT_YUYV:
3191 case DRM_FORMAT_YVYU:
3192 case DRM_FORMAT_UYVY:
3193 case DRM_FORMAT_VYUY:
3194 case DRM_FORMAT_AYUV:
3195 case DRM_FORMAT_NV12:
3196 case DRM_FORMAT_NV21:
3197 case DRM_FORMAT_NV16:
3198 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003199 case DRM_FORMAT_NV24:
3200 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003201 case DRM_FORMAT_YUV410:
3202 case DRM_FORMAT_YVU410:
3203 case DRM_FORMAT_YUV411:
3204 case DRM_FORMAT_YVU411:
3205 case DRM_FORMAT_YUV420:
3206 case DRM_FORMAT_YVU420:
3207 case DRM_FORMAT_YUV422:
3208 case DRM_FORMAT_YVU422:
3209 case DRM_FORMAT_YUV444:
3210 case DRM_FORMAT_YVU444:
3211 return 0;
3212 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003213 DRM_DEBUG_KMS("invalid pixel format %s\n",
3214 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003215 return -EINVAL;
3216 }
3217}
3218
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003219static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003220{
3221 int ret, hsub, vsub, num_planes, i;
3222
3223 ret = format_check(r);
3224 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003225 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3226 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003227 return ret;
3228 }
3229
3230 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3231 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3232 num_planes = drm_format_num_planes(r->pixel_format);
3233
3234 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003235 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003236 return -EINVAL;
3237 }
3238
3239 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003240 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003241 return -EINVAL;
3242 }
3243
3244 for (i = 0; i < num_planes; i++) {
3245 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003246 unsigned int height = r->height / (i != 0 ? vsub : 1);
3247 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003248
3249 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003250 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003251 return -EINVAL;
3252 }
3253
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003254 if ((uint64_t) width * cpp > UINT_MAX)
3255 return -ERANGE;
3256
3257 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3258 return -ERANGE;
3259
3260 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003261 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003262 return -EINVAL;
3263 }
3264 }
3265
3266 return 0;
3267}
3268
Chris Wilson9a6f5132015-02-25 13:45:26 +00003269static struct drm_framebuffer *
3270internal_framebuffer_create(struct drm_device *dev,
3271 struct drm_mode_fb_cmd2 *r,
3272 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003273{
3274 struct drm_mode_config *config = &dev->mode_config;
3275 struct drm_framebuffer *fb;
3276 int ret;
3277
3278 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3279 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3280 return ERR_PTR(-EINVAL);
3281 }
3282
3283 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3284 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3285 r->width, config->min_width, config->max_width);
3286 return ERR_PTR(-EINVAL);
3287 }
3288 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3289 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3290 r->height, config->min_height, config->max_height);
3291 return ERR_PTR(-EINVAL);
3292 }
3293
3294 ret = framebuffer_check(r);
3295 if (ret)
3296 return ERR_PTR(ret);
3297
3298 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3299 if (IS_ERR(fb)) {
3300 DRM_DEBUG_KMS("could not create framebuffer\n");
3301 return fb;
3302 }
3303
Matt Roperc394c2b2014-06-10 08:28:08 -07003304 return fb;
3305}
3306
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003307/**
3308 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003309 * @dev: drm device for the ioctl
3310 * @data: data pointer for the ioctl
3311 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003312 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003313 * Add a new FB to the specified CRTC, given a user request with format. This is
3314 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3315 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003316 *
3317 * Called by the user via ioctl.
3318 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003319 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003320 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003321 */
3322int drm_mode_addfb2(struct drm_device *dev,
3323 void *data, struct drm_file *file_priv)
3324{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003325 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003326 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003327
Dave Airliefb3b06c2011-02-08 13:55:21 +10003328 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3329 return -EINVAL;
3330
Chris Wilson9a6f5132015-02-25 13:45:26 +00003331 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003332 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003333 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003334
Chris Wilson9a6f5132015-02-25 13:45:26 +00003335 /* Transfer ownership to the filp for reaping on close */
3336
3337 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3338 mutex_lock(&file_priv->fbs_lock);
3339 r->fb_id = fb->base.id;
3340 list_add(&fb->filp_head, &file_priv->fbs);
3341 mutex_unlock(&file_priv->fbs_lock);
3342
Matt Roperc394c2b2014-06-10 08:28:08 -07003343 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003344}
3345
3346/**
3347 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003348 * @dev: drm device for the ioctl
3349 * @data: data pointer for the ioctl
3350 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003351 *
Dave Airlief453ba02008-11-07 14:05:41 -08003352 * Remove the FB specified by the user.
3353 *
3354 * Called by the user via ioctl.
3355 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003356 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003357 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003358 */
3359int drm_mode_rmfb(struct drm_device *dev,
3360 void *data, struct drm_file *file_priv)
3361{
Dave Airlief453ba02008-11-07 14:05:41 -08003362 struct drm_framebuffer *fb = NULL;
3363 struct drm_framebuffer *fbl = NULL;
3364 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003365 int found = 0;
3366
Dave Airliefb3b06c2011-02-08 13:55:21 +10003367 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3368 return -EINVAL;
3369
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003370 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003371 mutex_lock(&dev->mode_config.fb_lock);
3372 fb = __drm_framebuffer_lookup(dev, *id);
3373 if (!fb)
3374 goto fail_lookup;
3375
Dave Airlief453ba02008-11-07 14:05:41 -08003376 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3377 if (fb == fbl)
3378 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003379 if (!found)
3380 goto fail_lookup;
3381
3382 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3383 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003384
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003385 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003386 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003387 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003388
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003389 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003390
Daniel Vetter2b677e82012-12-10 21:16:05 +01003391 return 0;
3392
3393fail_lookup:
3394 mutex_unlock(&dev->mode_config.fb_lock);
3395 mutex_unlock(&file_priv->fbs_lock);
3396
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003397 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003398}
3399
3400/**
3401 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003402 * @dev: drm device for the ioctl
3403 * @data: data pointer for the ioctl
3404 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003405 *
Dave Airlief453ba02008-11-07 14:05:41 -08003406 * Lookup the FB given its ID and return info about it.
3407 *
3408 * Called by the user via ioctl.
3409 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003410 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003411 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003412 */
3413int drm_mode_getfb(struct drm_device *dev,
3414 void *data, struct drm_file *file_priv)
3415{
3416 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003417 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003418 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003419
Dave Airliefb3b06c2011-02-08 13:55:21 +10003420 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3421 return -EINVAL;
3422
Daniel Vetter786b99e2012-12-02 21:53:40 +01003423 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003424 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003425 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003426
3427 r->height = fb->height;
3428 r->width = fb->width;
3429 r->depth = fb->depth;
3430 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003431 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003432 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003433 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003434 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003435 ret = fb->funcs->create_handle(fb, file_priv,
3436 &r->handle);
3437 } else {
3438 /* GET_FB() is an unprivileged ioctl so we must not
3439 * return a buffer-handle to non-master processes! For
3440 * backwards-compatibility reasons, we cannot make
3441 * GET_FB() privileged, so just return an invalid handle
3442 * for non-masters. */
3443 r->handle = 0;
3444 ret = 0;
3445 }
3446 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003447 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003448 }
Dave Airlief453ba02008-11-07 14:05:41 -08003449
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003450 drm_framebuffer_unreference(fb);
3451
Dave Airlief453ba02008-11-07 14:05:41 -08003452 return ret;
3453}
3454
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003455/**
3456 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3457 * @dev: drm device for the ioctl
3458 * @data: data pointer for the ioctl
3459 * @file_priv: drm file for the ioctl call
3460 *
3461 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3462 * rectangle list. Generic userspace which does frontbuffer rendering must call
3463 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3464 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3465 *
3466 * Modesetting drivers which always update the frontbuffer do not need to
3467 * implement the corresponding ->dirty framebuffer callback.
3468 *
3469 * Called by the user via ioctl.
3470 *
3471 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003472 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003473 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003474int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3475 void *data, struct drm_file *file_priv)
3476{
3477 struct drm_clip_rect __user *clips_ptr;
3478 struct drm_clip_rect *clips = NULL;
3479 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003480 struct drm_framebuffer *fb;
3481 unsigned flags;
3482 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003483 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003484
Dave Airliefb3b06c2011-02-08 13:55:21 +10003485 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3486 return -EINVAL;
3487
Daniel Vetter786b99e2012-12-02 21:53:40 +01003488 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003489 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003490 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003491
3492 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003493 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003494
3495 if (!num_clips != !clips_ptr) {
3496 ret = -EINVAL;
3497 goto out_err1;
3498 }
3499
3500 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3501
3502 /* If userspace annotates copy, clips must come in pairs */
3503 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3504 ret = -EINVAL;
3505 goto out_err1;
3506 }
3507
3508 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003509 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3510 ret = -EINVAL;
3511 goto out_err1;
3512 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003513 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003514 if (!clips) {
3515 ret = -ENOMEM;
3516 goto out_err1;
3517 }
3518
3519 ret = copy_from_user(clips, clips_ptr,
3520 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003521 if (ret) {
3522 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003523 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003524 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003525 }
3526
3527 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003528 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3529 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003530 } else {
3531 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003532 }
3533
3534out_err2:
3535 kfree(clips);
3536out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003537 drm_framebuffer_unreference(fb);
3538
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003539 return ret;
3540}
3541
3542
Dave Airlief453ba02008-11-07 14:05:41 -08003543/**
3544 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003545 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003546 *
Dave Airlief453ba02008-11-07 14:05:41 -08003547 * Destroy all the FBs associated with @filp.
3548 *
3549 * Called by the user via ioctl.
3550 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003551 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003552 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003553 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003554void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003555{
Dave Airlief453ba02008-11-07 14:05:41 -08003556 struct drm_device *dev = priv->minor->dev;
3557 struct drm_framebuffer *fb, *tfb;
3558
Daniel Vetter1b116292014-09-24 21:51:06 +02003559 /*
3560 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003561 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003562 * avoid upsetting lockdep since the universal cursor code adds a
3563 * framebuffer while holding mutex locks.
3564 *
3565 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3566 * locks is impossible here since no one else but this function can get
3567 * at it any more.
3568 */
Dave Airlief453ba02008-11-07 14:05:41 -08003569 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01003570
3571 mutex_lock(&dev->mode_config.fb_lock);
3572 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3573 __drm_framebuffer_unregister(dev, fb);
3574 mutex_unlock(&dev->mode_config.fb_lock);
3575
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003576 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003577
3578 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00003579 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003580 }
Dave Airlief453ba02008-11-07 14:05:41 -08003581}
3582
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003583/**
3584 * drm_property_create - create a new property type
3585 * @dev: drm device
3586 * @flags: flags specifying the property type
3587 * @name: name of the property
3588 * @num_values: number of pre-defined values
3589 *
3590 * This creates a new generic drm property which can then be attached to a drm
3591 * object with drm_object_attach_property. The returned property object must be
3592 * freed with drm_property_destroy.
3593 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003594 * Note that the DRM core keeps a per-device list of properties and that, if
3595 * drm_mode_config_cleanup() is called, it will destroy all properties created
3596 * by the driver.
3597 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003598 * Returns:
3599 * A pointer to the newly created property on success, NULL on failure.
3600 */
Dave Airlief453ba02008-11-07 14:05:41 -08003601struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3602 const char *name, int num_values)
3603{
3604 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003605 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003606
3607 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3608 if (!property)
3609 return NULL;
3610
Rob Clark98f75de2014-05-30 11:37:03 -04003611 property->dev = dev;
3612
Dave Airlief453ba02008-11-07 14:05:41 -08003613 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003614 property->values = kcalloc(num_values, sizeof(uint64_t),
3615 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003616 if (!property->values)
3617 goto fail;
3618 }
3619
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003620 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3621 if (ret)
3622 goto fail;
3623
Dave Airlief453ba02008-11-07 14:05:41 -08003624 property->flags = flags;
3625 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003626 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003627
Vinson Lee471dd2e2011-11-10 11:55:40 -08003628 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003629 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003630 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3631 }
Dave Airlief453ba02008-11-07 14:05:41 -08003632
3633 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003634
3635 WARN_ON(!drm_property_type_valid(property));
3636
Dave Airlief453ba02008-11-07 14:05:41 -08003637 return property;
3638fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003639 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003640 kfree(property);
3641 return NULL;
3642}
3643EXPORT_SYMBOL(drm_property_create);
3644
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003645/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003646 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003647 * @dev: drm device
3648 * @flags: flags specifying the property type
3649 * @name: name of the property
3650 * @props: enumeration lists with property values
3651 * @num_values: number of pre-defined values
3652 *
3653 * This creates a new generic drm property which can then be attached to a drm
3654 * object with drm_object_attach_property. The returned property object must be
3655 * freed with drm_property_destroy.
3656 *
3657 * Userspace is only allowed to set one of the predefined values for enumeration
3658 * properties.
3659 *
3660 * Returns:
3661 * A pointer to the newly created property on success, NULL on failure.
3662 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003663struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3664 const char *name,
3665 const struct drm_prop_enum_list *props,
3666 int num_values)
3667{
3668 struct drm_property *property;
3669 int i, ret;
3670
3671 flags |= DRM_MODE_PROP_ENUM;
3672
3673 property = drm_property_create(dev, flags, name, num_values);
3674 if (!property)
3675 return NULL;
3676
3677 for (i = 0; i < num_values; i++) {
3678 ret = drm_property_add_enum(property, i,
3679 props[i].type,
3680 props[i].name);
3681 if (ret) {
3682 drm_property_destroy(dev, property);
3683 return NULL;
3684 }
3685 }
3686
3687 return property;
3688}
3689EXPORT_SYMBOL(drm_property_create_enum);
3690
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003691/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003692 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003693 * @dev: drm device
3694 * @flags: flags specifying the property type
3695 * @name: name of the property
3696 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003697 * @num_props: size of the @props array
3698 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003699 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003700 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003701 * object with drm_object_attach_property. The returned property object must be
3702 * freed with drm_property_destroy.
3703 *
3704 * Compared to plain enumeration properties userspace is allowed to set any
3705 * or'ed together combination of the predefined property bitflag values
3706 *
3707 * Returns:
3708 * A pointer to the newly created property on success, NULL on failure.
3709 */
Rob Clark49e27542012-05-17 02:23:26 -06003710struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3711 int flags, const char *name,
3712 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303713 int num_props,
3714 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003715{
3716 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303717 int i, ret, index = 0;
3718 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003719
3720 flags |= DRM_MODE_PROP_BITMASK;
3721
3722 property = drm_property_create(dev, flags, name, num_values);
3723 if (!property)
3724 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303725 for (i = 0; i < num_props; i++) {
3726 if (!(supported_bits & (1ULL << props[i].type)))
3727 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003728
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303729 if (WARN_ON(index >= num_values)) {
3730 drm_property_destroy(dev, property);
3731 return NULL;
3732 }
3733
3734 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003735 props[i].type,
3736 props[i].name);
3737 if (ret) {
3738 drm_property_destroy(dev, property);
3739 return NULL;
3740 }
3741 }
3742
3743 return property;
3744}
3745EXPORT_SYMBOL(drm_property_create_bitmask);
3746
Rob Clarkebc44cf2012-09-12 22:22:31 -05003747static struct drm_property *property_create_range(struct drm_device *dev,
3748 int flags, const char *name,
3749 uint64_t min, uint64_t max)
3750{
3751 struct drm_property *property;
3752
3753 property = drm_property_create(dev, flags, name, 2);
3754 if (!property)
3755 return NULL;
3756
3757 property->values[0] = min;
3758 property->values[1] = max;
3759
3760 return property;
3761}
3762
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003763/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003764 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003765 * @dev: drm device
3766 * @flags: flags specifying the property type
3767 * @name: name of the property
3768 * @min: minimum value of the property
3769 * @max: maximum value of the property
3770 *
3771 * This creates a new generic drm property which can then be attached to a drm
3772 * object with drm_object_attach_property. The returned property object must be
3773 * freed with drm_property_destroy.
3774 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003775 * Userspace is allowed to set any unsigned integer value in the (min, max)
3776 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003777 *
3778 * Returns:
3779 * A pointer to the newly created property on success, NULL on failure.
3780 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003781struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3782 const char *name,
3783 uint64_t min, uint64_t max)
3784{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003785 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3786 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003787}
3788EXPORT_SYMBOL(drm_property_create_range);
3789
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003790/**
3791 * drm_property_create_signed_range - create a new signed ranged property type
3792 * @dev: drm device
3793 * @flags: flags specifying the property type
3794 * @name: name of the property
3795 * @min: minimum value of the property
3796 * @max: maximum value of the property
3797 *
3798 * This creates a new generic drm property which can then be attached to a drm
3799 * object with drm_object_attach_property. The returned property object must be
3800 * freed with drm_property_destroy.
3801 *
3802 * Userspace is allowed to set any signed integer value in the (min, max)
3803 * range inclusive.
3804 *
3805 * Returns:
3806 * A pointer to the newly created property on success, NULL on failure.
3807 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003808struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3809 int flags, const char *name,
3810 int64_t min, int64_t max)
3811{
3812 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3813 name, I642U64(min), I642U64(max));
3814}
3815EXPORT_SYMBOL(drm_property_create_signed_range);
3816
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003817/**
3818 * drm_property_create_object - create a new object property type
3819 * @dev: drm device
3820 * @flags: flags specifying the property type
3821 * @name: name of the property
3822 * @type: object type from DRM_MODE_OBJECT_* defines
3823 *
3824 * This creates a new generic drm property which can then be attached to a drm
3825 * object with drm_object_attach_property. The returned property object must be
3826 * freed with drm_property_destroy.
3827 *
3828 * Userspace is only allowed to set this to any property value of the given
3829 * @type. Only useful for atomic properties, which is enforced.
3830 *
3831 * Returns:
3832 * A pointer to the newly created property on success, NULL on failure.
3833 */
Rob Clark98f75de2014-05-30 11:37:03 -04003834struct drm_property *drm_property_create_object(struct drm_device *dev,
3835 int flags, const char *name, uint32_t type)
3836{
3837 struct drm_property *property;
3838
3839 flags |= DRM_MODE_PROP_OBJECT;
3840
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003841 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3842 return NULL;
3843
Rob Clark98f75de2014-05-30 11:37:03 -04003844 property = drm_property_create(dev, flags, name, 1);
3845 if (!property)
3846 return NULL;
3847
3848 property->values[0] = type;
3849
3850 return property;
3851}
3852EXPORT_SYMBOL(drm_property_create_object);
3853
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003854/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003855 * drm_property_create_bool - create a new boolean property type
3856 * @dev: drm device
3857 * @flags: flags specifying the property type
3858 * @name: name of the property
3859 *
3860 * This creates a new generic drm property which can then be attached to a drm
3861 * object with drm_object_attach_property. The returned property object must be
3862 * freed with drm_property_destroy.
3863 *
3864 * This is implemented as a ranged property with only {0, 1} as valid values.
3865 *
3866 * Returns:
3867 * A pointer to the newly created property on success, NULL on failure.
3868 */
3869struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3870 const char *name)
3871{
3872 return drm_property_create_range(dev, flags, name, 0, 1);
3873}
3874EXPORT_SYMBOL(drm_property_create_bool);
3875
3876/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003877 * drm_property_add_enum - add a possible value to an enumeration property
3878 * @property: enumeration property to change
3879 * @index: index of the new enumeration
3880 * @value: value of the new enumeration
3881 * @name: symbolic name of the new enumeration
3882 *
3883 * This functions adds enumerations to a property.
3884 *
3885 * It's use is deprecated, drivers should use one of the more specific helpers
3886 * to directly create the property with all enumerations already attached.
3887 *
3888 * Returns:
3889 * Zero on success, error code on failure.
3890 */
Dave Airlief453ba02008-11-07 14:05:41 -08003891int drm_property_add_enum(struct drm_property *property, int index,
3892 uint64_t value, const char *name)
3893{
3894 struct drm_property_enum *prop_enum;
3895
Rob Clark5ea22f22014-05-30 11:34:01 -04003896 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3897 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003898 return -EINVAL;
3899
3900 /*
3901 * Bitmask enum properties have the additional constraint of values
3902 * from 0 to 63
3903 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003904 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3905 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003906 return -EINVAL;
3907
Daniel Vetter3758b342014-11-19 18:38:10 +01003908 if (!list_empty(&property->enum_list)) {
3909 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003910 if (prop_enum->value == value) {
3911 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3912 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3913 return 0;
3914 }
3915 }
3916 }
3917
3918 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3919 if (!prop_enum)
3920 return -ENOMEM;
3921
3922 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3923 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3924 prop_enum->value = value;
3925
3926 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01003927 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003928 return 0;
3929}
3930EXPORT_SYMBOL(drm_property_add_enum);
3931
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003932/**
3933 * drm_property_destroy - destroy a drm property
3934 * @dev: drm device
3935 * @property: property to destry
3936 *
3937 * This function frees a property including any attached resources like
3938 * enumeration values.
3939 */
Dave Airlief453ba02008-11-07 14:05:41 -08003940void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3941{
3942 struct drm_property_enum *prop_enum, *pt;
3943
Daniel Vetter3758b342014-11-19 18:38:10 +01003944 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003945 list_del(&prop_enum->head);
3946 kfree(prop_enum);
3947 }
3948
3949 if (property->num_values)
3950 kfree(property->values);
3951 drm_mode_object_put(dev, &property->base);
3952 list_del(&property->head);
3953 kfree(property);
3954}
3955EXPORT_SYMBOL(drm_property_destroy);
3956
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003957/**
3958 * drm_object_attach_property - attach a property to a modeset object
3959 * @obj: drm modeset object
3960 * @property: property to attach
3961 * @init_val: initial value of the property
3962 *
3963 * This attaches the given property to the modeset object with the given initial
3964 * value. Currently this function cannot fail since the properties are stored in
3965 * a statically sized array.
3966 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003967void drm_object_attach_property(struct drm_mode_object *obj,
3968 struct drm_property *property,
3969 uint64_t init_val)
3970{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003971 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003972
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003973 if (count == DRM_OBJECT_MAX_PROPERTY) {
3974 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3975 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3976 "you see this message on the same object type.\n",
3977 obj->type);
3978 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003979 }
3980
Rob Clarkb17cd752014-12-16 18:05:30 -05003981 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003982 obj->properties->values[count] = init_val;
3983 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05003984 if (property->flags & DRM_MODE_PROP_ATOMIC)
3985 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003986}
3987EXPORT_SYMBOL(drm_object_attach_property);
3988
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003989/**
3990 * drm_object_property_set_value - set the value of a property
3991 * @obj: drm mode object to set property value for
3992 * @property: property to set
3993 * @val: value the property should be set to
3994 *
3995 * This functions sets a given property on a given object. This function only
3996 * changes the software state of the property, it does not call into the
3997 * driver's ->set_property callback.
3998 *
3999 * Returns:
4000 * Zero on success, error code on failure.
4001 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004002int drm_object_property_set_value(struct drm_mode_object *obj,
4003 struct drm_property *property, uint64_t val)
4004{
4005 int i;
4006
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004007 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004008 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004009 obj->properties->values[i] = val;
4010 return 0;
4011 }
4012 }
4013
4014 return -EINVAL;
4015}
4016EXPORT_SYMBOL(drm_object_property_set_value);
4017
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004018/**
4019 * drm_object_property_get_value - retrieve the value of a property
4020 * @obj: drm mode object to get property value from
4021 * @property: property to retrieve
4022 * @val: storage for the property value
4023 *
4024 * This function retrieves the softare state of the given property for the given
4025 * property. Since there is no driver callback to retrieve the current property
4026 * value this might be out of sync with the hardware, depending upon the driver
4027 * and property.
4028 *
4029 * Returns:
4030 * Zero on success, error code on failure.
4031 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004032int drm_object_property_get_value(struct drm_mode_object *obj,
4033 struct drm_property *property, uint64_t *val)
4034{
4035 int i;
4036
Rob Clark88a48e22014-12-18 16:01:50 -05004037 /* read-only properties bypass atomic mechanism and still store
4038 * their value in obj->properties->values[].. mostly to avoid
4039 * having to deal w/ EDID and similar props in atomic paths:
4040 */
4041 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4042 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4043 return drm_atomic_get_property(obj, property, val);
4044
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004045 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004046 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004047 *val = obj->properties->values[i];
4048 return 0;
4049 }
4050 }
4051
4052 return -EINVAL;
4053}
4054EXPORT_SYMBOL(drm_object_property_get_value);
4055
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004056/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004057 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004058 * @dev: DRM device
4059 * @data: ioctl data
4060 * @file_priv: DRM file info
4061 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004062 * This function retrieves the metadata for a given property, like the different
4063 * possible values for an enum property or the limits for a range property.
4064 *
4065 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004066 *
4067 * Called by the user via ioctl.
4068 *
4069 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004070 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004071 */
Dave Airlief453ba02008-11-07 14:05:41 -08004072int drm_mode_getproperty_ioctl(struct drm_device *dev,
4073 void *data, struct drm_file *file_priv)
4074{
Dave Airlief453ba02008-11-07 14:05:41 -08004075 struct drm_mode_get_property *out_resp = data;
4076 struct drm_property *property;
4077 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004078 int value_count = 0;
4079 int ret = 0, i;
4080 int copied;
4081 struct drm_property_enum *prop_enum;
4082 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004083 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004084
Dave Airliefb3b06c2011-02-08 13:55:21 +10004085 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4086 return -EINVAL;
4087
Daniel Vetter84849902012-12-02 00:28:11 +01004088 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004089 property = drm_property_find(dev, out_resp->prop_id);
4090 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004091 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004092 goto done;
4093 }
Dave Airlief453ba02008-11-07 14:05:41 -08004094
Rob Clark5ea22f22014-05-30 11:34:01 -04004095 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4096 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004097 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004098 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004099 }
4100
4101 value_count = property->num_values;
4102
4103 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4104 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4105 out_resp->flags = property->flags;
4106
4107 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004108 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004109 for (i = 0; i < value_count; i++) {
4110 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4111 ret = -EFAULT;
4112 goto done;
4113 }
4114 }
4115 }
4116 out_resp->count_values = value_count;
4117
Rob Clark5ea22f22014-05-30 11:34:01 -04004118 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4119 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004120 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4121 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004122 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004123 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004124
4125 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4126 ret = -EFAULT;
4127 goto done;
4128 }
4129
4130 if (copy_to_user(&enum_ptr[copied].name,
4131 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4132 ret = -EFAULT;
4133 goto done;
4134 }
4135 copied++;
4136 }
4137 }
4138 out_resp->count_enum_blobs = enum_count;
4139 }
4140
Daniel Vetter3758b342014-11-19 18:38:10 +01004141 /*
4142 * NOTE: The idea seems to have been to use this to read all the blob
4143 * property values. But nothing ever added them to the corresponding
4144 * list, userspace always used the special-purpose get_blob ioctl to
4145 * read the value for a blob property. It also doesn't make a lot of
4146 * sense to return values here when everything else is just metadata for
4147 * the property itself.
4148 */
4149 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4150 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004151done:
Daniel Vetter84849902012-12-02 00:28:11 +01004152 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004153 return ret;
4154}
4155
Thierry Redingecbbe592014-05-13 11:36:13 +02004156static struct drm_property_blob *
4157drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004158 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004159{
4160 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004161 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004162
4163 if (!length || !data)
4164 return NULL;
4165
4166 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4167 if (!blob)
4168 return NULL;
4169
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004170 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4171 if (ret) {
4172 kfree(blob);
4173 return NULL;
4174 }
4175
Dave Airlief453ba02008-11-07 14:05:41 -08004176 blob->length = length;
4177
4178 memcpy(blob->data, data, length);
4179
Dave Airlief453ba02008-11-07 14:05:41 -08004180 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
4181 return blob;
4182}
4183
4184static void drm_property_destroy_blob(struct drm_device *dev,
4185 struct drm_property_blob *blob)
4186{
4187 drm_mode_object_put(dev, &blob->base);
4188 list_del(&blob->head);
4189 kfree(blob);
4190}
4191
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004192/**
4193 * drm_mode_getblob_ioctl - get the contents of a blob property value
4194 * @dev: DRM device
4195 * @data: ioctl data
4196 * @file_priv: DRM file info
4197 *
4198 * This function retrieves the contents of a blob property. The value stored in
4199 * an object's blob property is just a normal modeset object id.
4200 *
4201 * Called by the user via ioctl.
4202 *
4203 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004204 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004205 */
Dave Airlief453ba02008-11-07 14:05:41 -08004206int drm_mode_getblob_ioctl(struct drm_device *dev,
4207 void *data, struct drm_file *file_priv)
4208{
Dave Airlief453ba02008-11-07 14:05:41 -08004209 struct drm_mode_get_blob *out_resp = data;
4210 struct drm_property_blob *blob;
4211 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004212 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004213
Dave Airliefb3b06c2011-02-08 13:55:21 +10004214 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4215 return -EINVAL;
4216
Daniel Vetter84849902012-12-02 00:28:11 +01004217 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004218 blob = drm_property_blob_find(dev, out_resp->blob_id);
4219 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004220 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004221 goto done;
4222 }
Dave Airlief453ba02008-11-07 14:05:41 -08004223
4224 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004225 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004226 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004227 ret = -EFAULT;
4228 goto done;
4229 }
4230 }
4231 out_resp->length = blob->length;
4232
4233done:
Daniel Vetter84849902012-12-02 00:28:11 +01004234 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004235 return ret;
4236}
4237
Dave Airliecc7096f2014-10-22 12:03:04 +10004238/**
4239 * drm_mode_connector_set_path_property - set tile property on connector
4240 * @connector: connector to set property on.
4241 * @path: path to use for property.
4242 *
4243 * This creates a property to expose to userspace to specify a
4244 * connector path. This is mainly used for DisplayPort MST where
4245 * connectors have a topology and we want to allow userspace to give
4246 * them more meaningful names.
4247 *
4248 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004249 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004250 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004251int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004252 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004253{
4254 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004255 size_t size = strlen(path) + 1;
4256 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004257
4258 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4259 size, path);
4260 if (!connector->path_blob_ptr)
4261 return -EINVAL;
4262
4263 ret = drm_object_property_set_value(&connector->base,
4264 dev->mode_config.path_property,
4265 connector->path_blob_ptr->base.id);
4266 return ret;
4267}
4268EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4269
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004270/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004271 * drm_mode_connector_set_tile_property - set tile property on connector
4272 * @connector: connector to set property on.
4273 *
4274 * This looks up the tile information for a connector, and creates a
4275 * property for userspace to parse if it exists. The property is of
4276 * the form of 8 integers using ':' as a separator.
4277 *
4278 * Returns:
4279 * Zero on success, errno on failure.
4280 */
4281int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4282{
4283 struct drm_device *dev = connector->dev;
4284 int ret, size;
4285 char tile[256];
4286
4287 if (connector->tile_blob_ptr)
4288 drm_property_destroy_blob(dev, connector->tile_blob_ptr);
4289
4290 if (!connector->has_tile) {
4291 connector->tile_blob_ptr = NULL;
4292 ret = drm_object_property_set_value(&connector->base,
4293 dev->mode_config.tile_property, 0);
4294 return ret;
4295 }
4296
4297 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4298 connector->tile_group->id, connector->tile_is_single_monitor,
4299 connector->num_h_tile, connector->num_v_tile,
4300 connector->tile_h_loc, connector->tile_v_loc,
4301 connector->tile_h_size, connector->tile_v_size);
4302 size = strlen(tile) + 1;
4303
4304 connector->tile_blob_ptr = drm_property_create_blob(connector->dev,
4305 size, tile);
4306 if (!connector->tile_blob_ptr)
4307 return -EINVAL;
4308
4309 ret = drm_object_property_set_value(&connector->base,
4310 dev->mode_config.tile_property,
4311 connector->tile_blob_ptr->base.id);
4312 return ret;
4313}
4314EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4315
4316/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004317 * drm_mode_connector_update_edid_property - update the edid property of a connector
4318 * @connector: drm connector
4319 * @edid: new value of the edid property
4320 *
4321 * This function creates a new blob modeset object and assigns its id to the
4322 * connector's edid property.
4323 *
4324 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004325 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004326 */
Dave Airlief453ba02008-11-07 14:05:41 -08004327int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004328 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004329{
4330 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004331 size_t size;
4332 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004333
Thomas Wood4cf2b282014-06-18 17:52:33 +01004334 /* ignore requests to set edid when overridden */
4335 if (connector->override_edid)
4336 return 0;
4337
Dave Airlief453ba02008-11-07 14:05:41 -08004338 if (connector->edid_blob_ptr)
4339 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4340
4341 /* Delete edid, when there is none. */
4342 if (!edid) {
4343 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05004344 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08004345 return ret;
4346 }
4347
Adam Jackson7466f4c2010-03-29 21:43:23 +00004348 size = EDID_LENGTH * (1 + edid->extensions);
4349 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4350 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00004351 if (!connector->edid_blob_ptr)
4352 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08004353
Rob Clark58495562012-10-11 20:50:56 -05004354 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08004355 dev->mode_config.edid_property,
4356 connector->edid_blob_ptr->base.id);
4357
4358 return ret;
4359}
4360EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4361
Rob Clark3843e712014-12-16 18:05:29 -05004362/* Some properties could refer to dynamic refcnt'd objects, or things that
4363 * need special locking to handle lifetime issues (ie. to ensure the prop
4364 * value doesn't become invalid part way through the property update due to
4365 * race). The value returned by reference via 'obj' should be passed back
4366 * to drm_property_change_valid_put() after the property is set (and the
4367 * object to which the property is attached has a chance to take it's own
4368 * reference).
4369 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004370bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004371 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004372{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004373 int i;
4374
Paulo Zanoni26a34812012-05-15 18:08:59 -03004375 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4376 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004377
Rob Clark3843e712014-12-16 18:05:29 -05004378 *ref = NULL;
4379
Rob Clark5ea22f22014-05-30 11:34:01 -04004380 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004381 if (value < property->values[0] || value > property->values[1])
4382 return false;
4383 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004384 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4385 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004386
Rob Clarkebc44cf2012-09-12 22:22:31 -05004387 if (svalue < U642I64(property->values[0]) ||
4388 svalue > U642I64(property->values[1]))
4389 return false;
4390 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004391 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004392 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004393
Rob Clark49e27542012-05-17 02:23:26 -06004394 for (i = 0; i < property->num_values; i++)
4395 valid_mask |= (1ULL << property->values[i]);
4396 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004397 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Ville Syrjäläc4a56752012-10-25 18:05:06 +00004398 /* Only the driver knows */
4399 return true;
Rob Clark98f75de2014-05-30 11:37:03 -04004400 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004401 /* a zero value for an object property translates to null: */
4402 if (value == 0)
4403 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004404
4405 /* handle refcnt'd objects specially: */
4406 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4407 struct drm_framebuffer *fb;
4408 fb = drm_framebuffer_lookup(property->dev, value);
4409 if (fb) {
4410 *ref = &fb->base;
4411 return true;
4412 } else {
4413 return false;
4414 }
4415 } else {
4416 return _object_find(property->dev, value, property->values[0]) != NULL;
4417 }
Paulo Zanoni26a34812012-05-15 18:08:59 -03004418 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004419
4420 for (i = 0; i < property->num_values; i++)
4421 if (property->values[i] == value)
4422 return true;
4423 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004424}
4425
Rob Clarkd34f20d2014-12-18 16:01:56 -05004426void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004427 struct drm_mode_object *ref)
4428{
4429 if (!ref)
4430 return;
4431
4432 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4433 if (property->values[0] == DRM_MODE_OBJECT_FB)
4434 drm_framebuffer_unreference(obj_to_fb(ref));
4435 }
4436}
4437
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004438/**
4439 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4440 * @dev: DRM device
4441 * @data: ioctl data
4442 * @file_priv: DRM file info
4443 *
4444 * This function sets the current value for a connectors's property. It also
4445 * calls into a driver's ->set_property callback to update the hardware state
4446 *
4447 * Called by the user via ioctl.
4448 *
4449 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004450 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004451 */
Dave Airlief453ba02008-11-07 14:05:41 -08004452int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4453 void *data, struct drm_file *file_priv)
4454{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004455 struct drm_mode_connector_set_property *conn_set_prop = data;
4456 struct drm_mode_obj_set_property obj_set_prop = {
4457 .value = conn_set_prop->value,
4458 .prop_id = conn_set_prop->prop_id,
4459 .obj_id = conn_set_prop->connector_id,
4460 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4461 };
Dave Airlief453ba02008-11-07 14:05:41 -08004462
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004463 /* It does all the locking and checking we need */
4464 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004465}
4466
Paulo Zanonic5431882012-05-15 18:09:02 -03004467static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4468 struct drm_property *property,
4469 uint64_t value)
4470{
4471 int ret = -EINVAL;
4472 struct drm_connector *connector = obj_to_connector(obj);
4473
4474 /* Do DPMS ourselves */
4475 if (property == connector->dev->mode_config.dpms_property) {
4476 if (connector->funcs->dpms)
4477 (*connector->funcs->dpms)(connector, (int)value);
4478 ret = 0;
4479 } else if (connector->funcs->set_property)
4480 ret = connector->funcs->set_property(connector, property, value);
4481
4482 /* store the property value if successful */
4483 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004484 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004485 return ret;
4486}
4487
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004488static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4489 struct drm_property *property,
4490 uint64_t value)
4491{
4492 int ret = -EINVAL;
4493 struct drm_crtc *crtc = obj_to_crtc(obj);
4494
4495 if (crtc->funcs->set_property)
4496 ret = crtc->funcs->set_property(crtc, property, value);
4497 if (!ret)
4498 drm_object_property_set_value(obj, property, value);
4499
4500 return ret;
4501}
4502
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004503/**
4504 * drm_mode_plane_set_obj_prop - set the value of a property
4505 * @plane: drm plane object to set property value for
4506 * @property: property to set
4507 * @value: value the property should be set to
4508 *
4509 * This functions sets a given property on a given plane object. This function
4510 * calls the driver's ->set_property callback and changes the software state of
4511 * the property if the callback succeeds.
4512 *
4513 * Returns:
4514 * Zero on success, error code on failure.
4515 */
4516int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4517 struct drm_property *property,
4518 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004519{
4520 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004521 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004522
4523 if (plane->funcs->set_property)
4524 ret = plane->funcs->set_property(plane, property, value);
4525 if (!ret)
4526 drm_object_property_set_value(obj, property, value);
4527
4528 return ret;
4529}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004530EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004531
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004532/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004533 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004534 * @dev: DRM device
4535 * @data: ioctl data
4536 * @file_priv: DRM file info
4537 *
4538 * This function retrieves the current value for an object's property. Compared
4539 * to the connector specific ioctl this one is extended to also work on crtc and
4540 * plane objects.
4541 *
4542 * Called by the user via ioctl.
4543 *
4544 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004545 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004546 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004547int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4548 struct drm_file *file_priv)
4549{
4550 struct drm_mode_obj_get_properties *arg = data;
4551 struct drm_mode_object *obj;
4552 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004553
4554 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4555 return -EINVAL;
4556
Daniel Vetter84849902012-12-02 00:28:11 +01004557 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004558
4559 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4560 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004561 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004562 goto out;
4563 }
4564 if (!obj->properties) {
4565 ret = -EINVAL;
4566 goto out;
4567 }
4568
Rob Clark88a48e22014-12-18 16:01:50 -05004569 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05004570 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4571 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4572 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03004573
Paulo Zanonic5431882012-05-15 18:09:02 -03004574out:
Daniel Vetter84849902012-12-02 00:28:11 +01004575 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004576 return ret;
4577}
4578
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004579/**
4580 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4581 * @dev: DRM device
4582 * @data: ioctl data
4583 * @file_priv: DRM file info
4584 *
4585 * This function sets the current value for an object's property. It also calls
4586 * into a driver's ->set_property callback to update the hardware state.
4587 * Compared to the connector specific ioctl this one is extended to also work on
4588 * crtc and plane objects.
4589 *
4590 * Called by the user via ioctl.
4591 *
4592 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004593 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004594 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004595int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4596 struct drm_file *file_priv)
4597{
4598 struct drm_mode_obj_set_property *arg = data;
4599 struct drm_mode_object *arg_obj;
4600 struct drm_mode_object *prop_obj;
4601 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05004602 int i, ret = -EINVAL;
4603 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004604
4605 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4606 return -EINVAL;
4607
Daniel Vetter84849902012-12-02 00:28:11 +01004608 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004609
4610 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004611 if (!arg_obj) {
4612 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004613 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004614 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004615 if (!arg_obj->properties)
4616 goto out;
4617
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004618 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05004619 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03004620 break;
4621
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004622 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03004623 goto out;
4624
4625 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4626 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004627 if (!prop_obj) {
4628 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004629 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004630 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004631 property = obj_to_property(prop_obj);
4632
Rob Clark3843e712014-12-16 18:05:29 -05004633 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03004634 goto out;
4635
4636 switch (arg_obj->type) {
4637 case DRM_MODE_OBJECT_CONNECTOR:
4638 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4639 arg->value);
4640 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004641 case DRM_MODE_OBJECT_CRTC:
4642 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4643 break;
Rob Clark4d939142012-05-17 02:23:27 -06004644 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004645 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4646 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06004647 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004648 }
4649
Rob Clark3843e712014-12-16 18:05:29 -05004650 drm_property_change_valid_put(property, ref);
4651
Paulo Zanonic5431882012-05-15 18:09:02 -03004652out:
Daniel Vetter84849902012-12-02 00:28:11 +01004653 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004654 return ret;
4655}
4656
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004657/**
4658 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4659 * @connector: connector to attach
4660 * @encoder: encoder to attach @connector to
4661 *
4662 * This function links up a connector to an encoder. Note that the routing
4663 * restrictions between encoders and crtcs are exposed to userspace through the
4664 * possible_clones and possible_crtcs bitmasks.
4665 *
4666 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004667 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004668 */
Dave Airlief453ba02008-11-07 14:05:41 -08004669int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4670 struct drm_encoder *encoder)
4671{
4672 int i;
4673
4674 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4675 if (connector->encoder_ids[i] == 0) {
4676 connector->encoder_ids[i] = encoder->base.id;
4677 return 0;
4678 }
4679 }
4680 return -ENOMEM;
4681}
4682EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4683
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004684/**
4685 * drm_mode_crtc_set_gamma_size - set the gamma table size
4686 * @crtc: CRTC to set the gamma table size for
4687 * @gamma_size: size of the gamma table
4688 *
4689 * Drivers which support gamma tables should set this to the supported gamma
4690 * table size when initializing the CRTC. Currently the drm core only supports a
4691 * fixed gamma table size.
4692 *
4693 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004694 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004695 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004696int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004697 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004698{
4699 crtc->gamma_size = gamma_size;
4700
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01004701 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4702 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08004703 if (!crtc->gamma_store) {
4704 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004705 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004706 }
4707
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004708 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004709}
4710EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4711
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004712/**
4713 * drm_mode_gamma_set_ioctl - set the gamma table
4714 * @dev: DRM device
4715 * @data: ioctl data
4716 * @file_priv: DRM file info
4717 *
4718 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4719 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4720 *
4721 * Called by the user via ioctl.
4722 *
4723 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004724 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004725 */
Dave Airlief453ba02008-11-07 14:05:41 -08004726int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4727 void *data, struct drm_file *file_priv)
4728{
4729 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004730 struct drm_crtc *crtc;
4731 void *r_base, *g_base, *b_base;
4732 int size;
4733 int ret = 0;
4734
Dave Airliefb3b06c2011-02-08 13:55:21 +10004735 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4736 return -EINVAL;
4737
Daniel Vetter84849902012-12-02 00:28:11 +01004738 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004739 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4740 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004741 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004742 goto out;
4743 }
Dave Airlief453ba02008-11-07 14:05:41 -08004744
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004745 if (crtc->funcs->gamma_set == NULL) {
4746 ret = -ENOSYS;
4747 goto out;
4748 }
4749
Dave Airlief453ba02008-11-07 14:05:41 -08004750 /* memcpy into gamma store */
4751 if (crtc_lut->gamma_size != crtc->gamma_size) {
4752 ret = -EINVAL;
4753 goto out;
4754 }
4755
4756 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4757 r_base = crtc->gamma_store;
4758 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4759 ret = -EFAULT;
4760 goto out;
4761 }
4762
4763 g_base = r_base + size;
4764 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4765 ret = -EFAULT;
4766 goto out;
4767 }
4768
4769 b_base = g_base + size;
4770 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4771 ret = -EFAULT;
4772 goto out;
4773 }
4774
James Simmons72034252010-08-03 01:33:19 +01004775 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004776
4777out:
Daniel Vetter84849902012-12-02 00:28:11 +01004778 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004779 return ret;
4780
4781}
4782
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004783/**
4784 * drm_mode_gamma_get_ioctl - get the gamma table
4785 * @dev: DRM device
4786 * @data: ioctl data
4787 * @file_priv: DRM file info
4788 *
4789 * Copy the current gamma table into the storage provided. This also provides
4790 * the gamma table size the driver expects, which can be used to size the
4791 * allocated storage.
4792 *
4793 * Called by the user via ioctl.
4794 *
4795 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004796 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004797 */
Dave Airlief453ba02008-11-07 14:05:41 -08004798int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4799 void *data, struct drm_file *file_priv)
4800{
4801 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004802 struct drm_crtc *crtc;
4803 void *r_base, *g_base, *b_base;
4804 int size;
4805 int ret = 0;
4806
Dave Airliefb3b06c2011-02-08 13:55:21 +10004807 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4808 return -EINVAL;
4809
Daniel Vetter84849902012-12-02 00:28:11 +01004810 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004811 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4812 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004813 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004814 goto out;
4815 }
Dave Airlief453ba02008-11-07 14:05:41 -08004816
4817 /* memcpy into gamma store */
4818 if (crtc_lut->gamma_size != crtc->gamma_size) {
4819 ret = -EINVAL;
4820 goto out;
4821 }
4822
4823 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4824 r_base = crtc->gamma_store;
4825 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4826 ret = -EFAULT;
4827 goto out;
4828 }
4829
4830 g_base = r_base + size;
4831 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4832 ret = -EFAULT;
4833 goto out;
4834 }
4835
4836 b_base = g_base + size;
4837 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4838 ret = -EFAULT;
4839 goto out;
4840 }
4841out:
Daniel Vetter84849902012-12-02 00:28:11 +01004842 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004843 return ret;
4844}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004845
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004846/**
4847 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4848 * @dev: DRM device
4849 * @data: ioctl data
4850 * @file_priv: DRM file info
4851 *
4852 * This schedules an asynchronous update on a given CRTC, called page flip.
4853 * Optionally a drm event is generated to signal the completion of the event.
4854 * Generic drivers cannot assume that a pageflip with changed framebuffer
4855 * properties (including driver specific metadata like tiling layout) will work,
4856 * but some drivers support e.g. pixel format changes through the pageflip
4857 * ioctl.
4858 *
4859 * Called by the user via ioctl.
4860 *
4861 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004862 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004863 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004864int drm_mode_page_flip_ioctl(struct drm_device *dev,
4865 void *data, struct drm_file *file_priv)
4866{
4867 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004868 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02004869 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004870 struct drm_pending_vblank_event *e = NULL;
4871 unsigned long flags;
4872 int ret = -EINVAL;
4873
4874 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4875 page_flip->reserved != 0)
4876 return -EINVAL;
4877
Keith Packard62f21042013-07-22 18:50:00 -07004878 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4879 return -EINVAL;
4880
Rob Clarka2b34e22013-10-05 16:36:52 -04004881 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4882 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004883 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004884
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01004885 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07004886 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004887 /* The framebuffer is currently unbound, presumably
4888 * due to a hotplug event, that userspace has not
4889 * yet discovered.
4890 */
4891 ret = -EBUSY;
4892 goto out;
4893 }
4894
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004895 if (crtc->funcs->page_flip == NULL)
4896 goto out;
4897
Daniel Vetter786b99e2012-12-02 21:53:40 +01004898 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004899 if (!fb) {
4900 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004901 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004902 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004903
Damien Lespiauc11e9282013-09-25 16:45:30 +01004904 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4905 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004906 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004907
Matt Roperf4510a22014-04-01 15:22:40 -07004908 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004909 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4910 ret = -EINVAL;
4911 goto out;
4912 }
4913
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004914 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4915 ret = -ENOMEM;
4916 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004917 if (file_priv->event_space < sizeof(e->event)) {
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004918 spin_unlock_irqrestore(&dev->event_lock, flags);
4919 goto out;
4920 }
Thierry Redingf76511b2014-12-10 13:03:40 +01004921 file_priv->event_space -= sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004922 spin_unlock_irqrestore(&dev->event_lock, flags);
4923
Thierry Redingf76511b2014-12-10 13:03:40 +01004924 e = kzalloc(sizeof(*e), GFP_KERNEL);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004925 if (e == NULL) {
4926 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004927 file_priv->event_space += sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004928 spin_unlock_irqrestore(&dev->event_lock, flags);
4929 goto out;
4930 }
4931
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004932 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01004933 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004934 e->event.user_data = page_flip->user_data;
4935 e->base.event = &e->event.base;
4936 e->base.file_priv = file_priv;
4937 e->base.destroy =
4938 (void (*) (struct drm_pending_event *)) kfree;
4939 }
4940
Daniel Vetter3d30a592014-07-27 13:42:42 +02004941 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004942 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004943 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004944 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4945 spin_lock_irqsave(&dev->event_lock, flags);
Thierry Redingf76511b2014-12-10 13:03:40 +01004946 file_priv->event_space += sizeof(e->event);
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09004947 spin_unlock_irqrestore(&dev->event_lock, flags);
4948 kfree(e);
4949 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01004950 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02004951 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004952 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01004953 /*
4954 * Warn if the driver hasn't properly updated the crtc->fb
4955 * field to reflect that the new framebuffer is now used.
4956 * Failing to do so will screw with the reference counting
4957 * on framebuffers.
4958 */
Matt Roperf4510a22014-04-01 15:22:40 -07004959 WARN_ON(crtc->primary->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004960 /* Unref only the old framebuffer. */
4961 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004962 }
4963
4964out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004965 if (fb)
4966 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02004967 if (crtc->primary->old_fb)
4968 drm_framebuffer_unreference(crtc->primary->old_fb);
4969 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02004970 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004971
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004972 return ret;
4973}
Chris Wilsoneb033552011-01-24 15:11:08 +00004974
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004975/**
4976 * drm_mode_config_reset - call ->reset callbacks
4977 * @dev: drm device
4978 *
4979 * This functions calls all the crtc's, encoder's and connector's ->reset
4980 * callback. Drivers can use this in e.g. their driver load or resume code to
4981 * reset hardware and software state.
4982 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004983void drm_mode_config_reset(struct drm_device *dev)
4984{
4985 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004986 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00004987 struct drm_encoder *encoder;
4988 struct drm_connector *connector;
4989
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004990 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4991 if (plane->funcs->reset)
4992 plane->funcs->reset(plane);
4993
Chris Wilsoneb033552011-01-24 15:11:08 +00004994 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4995 if (crtc->funcs->reset)
4996 crtc->funcs->reset(crtc);
4997
4998 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4999 if (encoder->funcs->reset)
5000 encoder->funcs->reset(encoder);
5001
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00005002 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5003 connector->status = connector_status_unknown;
5004
Chris Wilsoneb033552011-01-24 15:11:08 +00005005 if (connector->funcs->reset)
5006 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00005007 }
Chris Wilsoneb033552011-01-24 15:11:08 +00005008}
5009EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005010
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005011/**
5012 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5013 * @dev: DRM device
5014 * @data: ioctl data
5015 * @file_priv: DRM file info
5016 *
5017 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5018 * TTM or something else entirely) and returns the resulting buffer handle. This
5019 * handle can then be wrapped up into a framebuffer modeset object.
5020 *
5021 * Note that userspace is not allowed to use such objects for render
5022 * acceleration - drivers must create their own private ioctls for such a use
5023 * case.
5024 *
5025 * Called by the user via ioctl.
5026 *
5027 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005028 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005029 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005030int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5031 void *data, struct drm_file *file_priv)
5032{
5033 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005034 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005035
5036 if (!dev->driver->dumb_create)
5037 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005038 if (!args->width || !args->height || !args->bpp)
5039 return -EINVAL;
5040
5041 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005042 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005043 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005044 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005045 return -EINVAL;
5046 stride = cpp * args->width;
5047 if (args->height > 0xffffffffU / stride)
5048 return -EINVAL;
5049
5050 /* test for wrap-around */
5051 size = args->height * stride;
5052 if (PAGE_ALIGN(size) == 0)
5053 return -EINVAL;
5054
Thierry Redingf6085952014-11-03 11:14:14 +01005055 /*
5056 * handle, pitch and size are output parameters. Zero them out to
5057 * prevent drivers from accidentally using uninitialized data. Since
5058 * not all existing userspace is clearing these fields properly we
5059 * cannot reject IOCTL with garbage in them.
5060 */
5061 args->handle = 0;
5062 args->pitch = 0;
5063 args->size = 0;
5064
Dave Airlieff72145b2011-02-07 12:16:14 +10005065 return dev->driver->dumb_create(file_priv, dev, args);
5066}
5067
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005068/**
5069 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5070 * @dev: DRM device
5071 * @data: ioctl data
5072 * @file_priv: DRM file info
5073 *
5074 * Allocate an offset in the drm device node's address space to be able to
5075 * memory map a dumb buffer.
5076 *
5077 * Called by the user via ioctl.
5078 *
5079 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005080 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005081 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005082int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5083 void *data, struct drm_file *file_priv)
5084{
5085 struct drm_mode_map_dumb *args = data;
5086
5087 /* call driver ioctl to get mmap offset */
5088 if (!dev->driver->dumb_map_offset)
5089 return -ENOSYS;
5090
5091 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5092}
5093
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005094/**
5095 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5096 * @dev: DRM device
5097 * @data: ioctl data
5098 * @file_priv: DRM file info
5099 *
5100 * This destroys the userspace handle for the given dumb backing storage buffer.
5101 * Since buffer objects must be reference counted in the kernel a buffer object
5102 * won't be immediately freed if a framebuffer modeset object still uses it.
5103 *
5104 * Called by the user via ioctl.
5105 *
5106 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005107 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005108 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005109int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5110 void *data, struct drm_file *file_priv)
5111{
5112 struct drm_mode_destroy_dumb *args = data;
5113
5114 if (!dev->driver->dumb_destroy)
5115 return -ENOSYS;
5116
5117 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5118}
Dave Airlie248dbc22011-11-29 20:02:54 +00005119
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005120/**
5121 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5122 * @format: pixel format (DRM_FORMAT_*)
5123 * @depth: storage for the depth value
5124 * @bpp: storage for the bpp value
5125 *
5126 * This only supports RGB formats here for compat with code that doesn't use
5127 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005128 */
5129void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5130 int *bpp)
5131{
5132 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005133 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005134 case DRM_FORMAT_RGB332:
5135 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005136 *depth = 8;
5137 *bpp = 8;
5138 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005139 case DRM_FORMAT_XRGB1555:
5140 case DRM_FORMAT_XBGR1555:
5141 case DRM_FORMAT_RGBX5551:
5142 case DRM_FORMAT_BGRX5551:
5143 case DRM_FORMAT_ARGB1555:
5144 case DRM_FORMAT_ABGR1555:
5145 case DRM_FORMAT_RGBA5551:
5146 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005147 *depth = 15;
5148 *bpp = 16;
5149 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005150 case DRM_FORMAT_RGB565:
5151 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005152 *depth = 16;
5153 *bpp = 16;
5154 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005155 case DRM_FORMAT_RGB888:
5156 case DRM_FORMAT_BGR888:
5157 *depth = 24;
5158 *bpp = 24;
5159 break;
5160 case DRM_FORMAT_XRGB8888:
5161 case DRM_FORMAT_XBGR8888:
5162 case DRM_FORMAT_RGBX8888:
5163 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005164 *depth = 24;
5165 *bpp = 32;
5166 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005167 case DRM_FORMAT_XRGB2101010:
5168 case DRM_FORMAT_XBGR2101010:
5169 case DRM_FORMAT_RGBX1010102:
5170 case DRM_FORMAT_BGRX1010102:
5171 case DRM_FORMAT_ARGB2101010:
5172 case DRM_FORMAT_ABGR2101010:
5173 case DRM_FORMAT_RGBA1010102:
5174 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005175 *depth = 30;
5176 *bpp = 32;
5177 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005178 case DRM_FORMAT_ARGB8888:
5179 case DRM_FORMAT_ABGR8888:
5180 case DRM_FORMAT_RGBA8888:
5181 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005182 *depth = 32;
5183 *bpp = 32;
5184 break;
5185 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005186 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5187 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005188 *depth = 0;
5189 *bpp = 0;
5190 break;
5191 }
5192}
5193EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005194
5195/**
5196 * drm_format_num_planes - get the number of planes for format
5197 * @format: pixel format (DRM_FORMAT_*)
5198 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005199 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005200 * The number of planes used by the specified pixel format.
5201 */
5202int drm_format_num_planes(uint32_t format)
5203{
5204 switch (format) {
5205 case DRM_FORMAT_YUV410:
5206 case DRM_FORMAT_YVU410:
5207 case DRM_FORMAT_YUV411:
5208 case DRM_FORMAT_YVU411:
5209 case DRM_FORMAT_YUV420:
5210 case DRM_FORMAT_YVU420:
5211 case DRM_FORMAT_YUV422:
5212 case DRM_FORMAT_YVU422:
5213 case DRM_FORMAT_YUV444:
5214 case DRM_FORMAT_YVU444:
5215 return 3;
5216 case DRM_FORMAT_NV12:
5217 case DRM_FORMAT_NV21:
5218 case DRM_FORMAT_NV16:
5219 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005220 case DRM_FORMAT_NV24:
5221 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005222 return 2;
5223 default:
5224 return 1;
5225 }
5226}
5227EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005228
5229/**
5230 * drm_format_plane_cpp - determine the bytes per pixel value
5231 * @format: pixel format (DRM_FORMAT_*)
5232 * @plane: plane index
5233 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005234 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005235 * The bytes per pixel value for the specified plane.
5236 */
5237int drm_format_plane_cpp(uint32_t format, int plane)
5238{
5239 unsigned int depth;
5240 int bpp;
5241
5242 if (plane >= drm_format_num_planes(format))
5243 return 0;
5244
5245 switch (format) {
5246 case DRM_FORMAT_YUYV:
5247 case DRM_FORMAT_YVYU:
5248 case DRM_FORMAT_UYVY:
5249 case DRM_FORMAT_VYUY:
5250 return 2;
5251 case DRM_FORMAT_NV12:
5252 case DRM_FORMAT_NV21:
5253 case DRM_FORMAT_NV16:
5254 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005255 case DRM_FORMAT_NV24:
5256 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005257 return plane ? 2 : 1;
5258 case DRM_FORMAT_YUV410:
5259 case DRM_FORMAT_YVU410:
5260 case DRM_FORMAT_YUV411:
5261 case DRM_FORMAT_YVU411:
5262 case DRM_FORMAT_YUV420:
5263 case DRM_FORMAT_YVU420:
5264 case DRM_FORMAT_YUV422:
5265 case DRM_FORMAT_YVU422:
5266 case DRM_FORMAT_YUV444:
5267 case DRM_FORMAT_YVU444:
5268 return 1;
5269 default:
5270 drm_fb_get_bpp_depth(format, &depth, &bpp);
5271 return bpp >> 3;
5272 }
5273}
5274EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005275
5276/**
5277 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5278 * @format: pixel format (DRM_FORMAT_*)
5279 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005280 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005281 * The horizontal chroma subsampling factor for the
5282 * specified pixel format.
5283 */
5284int drm_format_horz_chroma_subsampling(uint32_t format)
5285{
5286 switch (format) {
5287 case DRM_FORMAT_YUV411:
5288 case DRM_FORMAT_YVU411:
5289 case DRM_FORMAT_YUV410:
5290 case DRM_FORMAT_YVU410:
5291 return 4;
5292 case DRM_FORMAT_YUYV:
5293 case DRM_FORMAT_YVYU:
5294 case DRM_FORMAT_UYVY:
5295 case DRM_FORMAT_VYUY:
5296 case DRM_FORMAT_NV12:
5297 case DRM_FORMAT_NV21:
5298 case DRM_FORMAT_NV16:
5299 case DRM_FORMAT_NV61:
5300 case DRM_FORMAT_YUV422:
5301 case DRM_FORMAT_YVU422:
5302 case DRM_FORMAT_YUV420:
5303 case DRM_FORMAT_YVU420:
5304 return 2;
5305 default:
5306 return 1;
5307 }
5308}
5309EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5310
5311/**
5312 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5313 * @format: pixel format (DRM_FORMAT_*)
5314 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005315 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005316 * The vertical chroma subsampling factor for the
5317 * specified pixel format.
5318 */
5319int drm_format_vert_chroma_subsampling(uint32_t format)
5320{
5321 switch (format) {
5322 case DRM_FORMAT_YUV410:
5323 case DRM_FORMAT_YVU410:
5324 return 4;
5325 case DRM_FORMAT_YUV420:
5326 case DRM_FORMAT_YVU420:
5327 case DRM_FORMAT_NV12:
5328 case DRM_FORMAT_NV21:
5329 return 2;
5330 default:
5331 return 1;
5332 }
5333}
5334EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005335
5336/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305337 * drm_rotation_simplify() - Try to simplify the rotation
5338 * @rotation: Rotation to be simplified
5339 * @supported_rotations: Supported rotations
5340 *
5341 * Attempt to simplify the rotation to a form that is supported.
5342 * Eg. if the hardware supports everything except DRM_REFLECT_X
5343 * one could call this function like this:
5344 *
5345 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5346 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5347 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5348 *
5349 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5350 * transforms the hardware supports, this function may not
5351 * be able to produce a supported transform, so the caller should
5352 * check the result afterwards.
5353 */
5354unsigned int drm_rotation_simplify(unsigned int rotation,
5355 unsigned int supported_rotations)
5356{
5357 if (rotation & ~supported_rotations) {
5358 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5359 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5360 }
5361
5362 return rotation;
5363}
5364EXPORT_SYMBOL(drm_rotation_simplify);
5365
5366/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005367 * drm_mode_config_init - initialize DRM mode_configuration structure
5368 * @dev: DRM device
5369 *
5370 * Initialize @dev's mode_config structure, used for tracking the graphics
5371 * configuration of @dev.
5372 *
5373 * Since this initializes the modeset locks, no locking is possible. Which is no
5374 * problem, since this should happen single threaded at init time. It is the
5375 * driver's problem to ensure this guarantee.
5376 *
5377 */
5378void drm_mode_config_init(struct drm_device *dev)
5379{
5380 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005381 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005382 mutex_init(&dev->mode_config.idr_mutex);
5383 mutex_init(&dev->mode_config.fb_lock);
5384 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5385 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5386 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5387 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5388 INIT_LIST_HEAD(&dev->mode_config.property_list);
5389 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5390 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5391 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005392 idr_init(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005393
5394 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005395 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005396 drm_modeset_unlock_all(dev);
5397
5398 /* Just to be sure */
5399 dev->mode_config.num_fb = 0;
5400 dev->mode_config.num_connector = 0;
5401 dev->mode_config.num_crtc = 0;
5402 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005403 dev->mode_config.num_overlay_plane = 0;
5404 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005405}
5406EXPORT_SYMBOL(drm_mode_config_init);
5407
5408/**
5409 * drm_mode_config_cleanup - free up DRM mode_config info
5410 * @dev: DRM device
5411 *
5412 * Free up all the connectors and CRTCs associated with this DRM device, then
5413 * free up the framebuffers and associated buffer objects.
5414 *
5415 * Note that since this /should/ happen single-threaded at driver/device
5416 * teardown time, no locking is required. It's the driver's job to ensure that
5417 * this guarantee actually holds true.
5418 *
5419 * FIXME: cleanup any dangling user buffer objects too
5420 */
5421void drm_mode_config_cleanup(struct drm_device *dev)
5422{
5423 struct drm_connector *connector, *ot;
5424 struct drm_crtc *crtc, *ct;
5425 struct drm_encoder *encoder, *enct;
5426 struct drm_framebuffer *fb, *fbt;
5427 struct drm_property *property, *pt;
5428 struct drm_property_blob *blob, *bt;
5429 struct drm_plane *plane, *plt;
5430
5431 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5432 head) {
5433 encoder->funcs->destroy(encoder);
5434 }
5435
5436 list_for_each_entry_safe(connector, ot,
5437 &dev->mode_config.connector_list, head) {
5438 connector->funcs->destroy(connector);
5439 }
5440
5441 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5442 head) {
5443 drm_property_destroy(dev, property);
5444 }
5445
5446 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5447 head) {
5448 drm_property_destroy_blob(dev, blob);
5449 }
5450
5451 /*
5452 * Single-threaded teardown context, so it's not required to grab the
5453 * fb_lock to protect against concurrent fb_list access. Contrary, it
5454 * would actually deadlock with the drm_framebuffer_cleanup function.
5455 *
5456 * Also, if there are any framebuffers left, that's a driver leak now,
5457 * so politely WARN about this.
5458 */
5459 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5460 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5461 drm_framebuffer_remove(fb);
5462 }
5463
5464 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5465 head) {
5466 plane->funcs->destroy(plane);
5467 }
5468
5469 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5470 crtc->funcs->destroy(crtc);
5471 }
5472
Dave Airlie138f9eb2014-10-20 16:17:17 +10005473 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005474 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005475 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005476}
5477EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305478
5479struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5480 unsigned int supported_rotations)
5481{
5482 static const struct drm_prop_enum_list props[] = {
5483 { DRM_ROTATE_0, "rotate-0" },
5484 { DRM_ROTATE_90, "rotate-90" },
5485 { DRM_ROTATE_180, "rotate-180" },
5486 { DRM_ROTATE_270, "rotate-270" },
5487 { DRM_REFLECT_X, "reflect-x" },
5488 { DRM_REFLECT_Y, "reflect-y" },
5489 };
5490
5491 return drm_property_create_bitmask(dev, 0, "rotation",
5492 props, ARRAY_SIZE(props),
5493 supported_rotations);
5494}
5495EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005496
5497/**
5498 * DOC: Tile group
5499 *
5500 * Tile groups are used to represent tiled monitors with a unique
5501 * integer identifier. Tiled monitors using DisplayID v1.3 have
5502 * a unique 8-byte handle, we store this in a tile group, so we
5503 * have a common identifier for all tiles in a monitor group.
5504 */
5505static void drm_tile_group_free(struct kref *kref)
5506{
5507 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5508 struct drm_device *dev = tg->dev;
5509 mutex_lock(&dev->mode_config.idr_mutex);
5510 idr_remove(&dev->mode_config.tile_idr, tg->id);
5511 mutex_unlock(&dev->mode_config.idr_mutex);
5512 kfree(tg);
5513}
5514
5515/**
5516 * drm_mode_put_tile_group - drop a reference to a tile group.
5517 * @dev: DRM device
5518 * @tg: tile group to drop reference to.
5519 *
5520 * drop reference to tile group and free if 0.
5521 */
5522void drm_mode_put_tile_group(struct drm_device *dev,
5523 struct drm_tile_group *tg)
5524{
5525 kref_put(&tg->refcount, drm_tile_group_free);
5526}
5527
5528/**
5529 * drm_mode_get_tile_group - get a reference to an existing tile group
5530 * @dev: DRM device
5531 * @topology: 8-bytes unique per monitor.
5532 *
5533 * Use the unique bytes to get a reference to an existing tile group.
5534 *
5535 * RETURNS:
5536 * tile group or NULL if not found.
5537 */
5538struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5539 char topology[8])
5540{
5541 struct drm_tile_group *tg;
5542 int id;
5543 mutex_lock(&dev->mode_config.idr_mutex);
5544 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5545 if (!memcmp(tg->group_data, topology, 8)) {
5546 if (!kref_get_unless_zero(&tg->refcount))
5547 tg = NULL;
5548 mutex_unlock(&dev->mode_config.idr_mutex);
5549 return tg;
5550 }
5551 }
5552 mutex_unlock(&dev->mode_config.idr_mutex);
5553 return NULL;
5554}
5555
5556/**
5557 * drm_mode_create_tile_group - create a tile group from a displayid description
5558 * @dev: DRM device
5559 * @topology: 8-bytes unique per monitor.
5560 *
5561 * Create a tile group for the unique monitor, and get a unique
5562 * identifier for the tile group.
5563 *
5564 * RETURNS:
5565 * new tile group or error.
5566 */
5567struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5568 char topology[8])
5569{
5570 struct drm_tile_group *tg;
5571 int ret;
5572
5573 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5574 if (!tg)
5575 return ERR_PTR(-ENOMEM);
5576
5577 kref_init(&tg->refcount);
5578 memcpy(tg->group_data, topology, 8);
5579 tg->dev = dev;
5580
5581 mutex_lock(&dev->mode_config.idr_mutex);
5582 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5583 if (ret >= 0) {
5584 tg->id = ret;
5585 } else {
5586 kfree(tg);
5587 tg = ERR_PTR(ret);
5588 }
5589
5590 mutex_unlock(&dev->mode_config.idr_mutex);
5591 return tg;
5592}