blob: 7675826c63f214f8c385a5ea9dffe9d7029b4fe1 [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,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020048 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +000049 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:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200309 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800310 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100311int drm_mode_object_get(struct drm_device *dev,
312 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800313{
Dave Airlie2ee39452014-07-24 11:53:45 +1000314 return drm_mode_object_get_reg(dev, obj, obj_type, true);
315}
Dave Airlief453ba02008-11-07 14:05:41 -0800316
Dave Airlie2ee39452014-07-24 11:53:45 +1000317static void drm_mode_object_register(struct drm_device *dev,
318 struct drm_mode_object *obj)
319{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000320 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000321 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000322 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800323}
324
325/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100326 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800327 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100328 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800329 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100330 * Free @id from @dev's unique identifier pool. Note that despite the _get
331 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
332 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800333 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100334void drm_mode_object_put(struct drm_device *dev,
335 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800336{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000337 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800338 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000339 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800340}
341
Rob Clark98f75de2014-05-30 11:37:03 -0400342static struct drm_mode_object *_object_find(struct drm_device *dev,
343 uint32_t id, uint32_t type)
344{
345 struct drm_mode_object *obj = NULL;
346
347 mutex_lock(&dev->mode_config.idr_mutex);
348 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200349 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
350 obj = NULL;
351 if (obj && obj->id != id)
352 obj = NULL;
353 /* don't leak out unref'd fb's */
Daniel Stone6bcacf52015-04-20 19:22:55 +0100354 if (obj &&
355 (obj->type == DRM_MODE_OBJECT_FB ||
356 obj->type == DRM_MODE_OBJECT_BLOB))
Rob Clark98f75de2014-05-30 11:37:03 -0400357 obj = NULL;
358 mutex_unlock(&dev->mode_config.idr_mutex);
359
360 return obj;
361}
362
Daniel Vetter786b99e2012-12-02 21:53:40 +0100363/**
364 * drm_mode_object_find - look up a drm object with static lifetime
365 * @dev: drm device
366 * @id: id of the mode object
367 * @type: type of the mode object
368 *
369 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400370 * are reference counted, they need special treatment. Even with
371 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
372 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100373 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200374struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
375 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800376{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000377 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800378
Daniel Vetter786b99e2012-12-02 21:53:40 +0100379 /* Framebuffers are reference counted and need their own lookup
380 * function.*/
Daniel Stone6bcacf52015-04-20 19:22:55 +0100381 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
Rob Clark98f75de2014-05-30 11:37:03 -0400382 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800383 return obj;
384}
385EXPORT_SYMBOL(drm_mode_object_find);
386
387/**
Dave Airlief453ba02008-11-07 14:05:41 -0800388 * drm_framebuffer_init - initialize a framebuffer
389 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100390 * @fb: framebuffer to be initialized
391 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800392 *
Dave Airlief453ba02008-11-07 14:05:41 -0800393 * Allocates an ID for the framebuffer's parent mode object, sets its mode
394 * functions & device file and adds it to the master fd list.
395 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100396 * IMPORTANT:
397 * This functions publishes the fb and makes it available for concurrent access
398 * by other users. Which means by this point the fb _must_ be fully set up -
399 * since all the fb attributes are invariant over its lifetime, no further
400 * locking but only correct reference counting is required.
401 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100402 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200403 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800404 */
405int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
406 const struct drm_framebuffer_funcs *funcs)
407{
408 int ret;
409
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100410 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000411 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100412 INIT_LIST_HEAD(&fb->filp_head);
413 fb->dev = dev;
414 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000415
Dave Airlief453ba02008-11-07 14:05:41 -0800416 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200417 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100418 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800419
Dave Airlief453ba02008-11-07 14:05:41 -0800420 dev->mode_config.num_fb++;
421 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100422out:
423 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800424
Lukas Wunner3c67d832015-10-15 11:56:56 +0200425 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800426}
427EXPORT_SYMBOL(drm_framebuffer_init);
428
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200429/* dev->mode_config.fb_lock must be held! */
430static void __drm_framebuffer_unregister(struct drm_device *dev,
431 struct drm_framebuffer *fb)
432{
Liu Ying32709792016-02-29 11:21:10 +0800433 drm_mode_object_put(dev, &fb->base);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200434
435 fb->base.id = 0;
436}
437
Rob Clarkf7eff602012-09-05 21:48:38 +0000438static void drm_framebuffer_free(struct kref *kref)
439{
440 struct drm_framebuffer *fb =
441 container_of(kref, struct drm_framebuffer, refcount);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200442 struct drm_device *dev = fb->dev;
443
444 /*
445 * The lookup idr holds a weak reference, which has not necessarily been
446 * removed at this point. Check for that.
447 */
448 mutex_lock(&dev->mode_config.fb_lock);
449 if (fb->base.id) {
450 /* Mark fb as reaped and drop idr ref. */
451 __drm_framebuffer_unregister(dev, fb);
452 }
453 mutex_unlock(&dev->mode_config.fb_lock);
454
Rob Clarkf7eff602012-09-05 21:48:38 +0000455 fb->funcs->destroy(fb);
456}
457
Daniel Vetter2b677e82012-12-10 21:16:05 +0100458static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
459 uint32_t id)
460{
461 struct drm_mode_object *obj = NULL;
462 struct drm_framebuffer *fb;
463
464 mutex_lock(&dev->mode_config.idr_mutex);
465 obj = idr_find(&dev->mode_config.crtc_idr, id);
466 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
467 fb = NULL;
468 else
469 fb = obj_to_fb(obj);
470 mutex_unlock(&dev->mode_config.idr_mutex);
471
472 return fb;
473}
474
Rob Clarkf7eff602012-09-05 21:48:38 +0000475/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100476 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
477 * @dev: drm device
478 * @id: id of the fb object
479 *
480 * If successful, this grabs an additional reference to the framebuffer -
481 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100482 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100483 */
484struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
485 uint32_t id)
486{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100487 struct drm_framebuffer *fb;
488
489 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100490 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200491 if (fb) {
492 if (!kref_get_unless_zero(&fb->refcount))
493 fb = NULL;
494 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100495 mutex_unlock(&dev->mode_config.fb_lock);
496
497 return fb;
498}
499EXPORT_SYMBOL(drm_framebuffer_lookup);
500
501/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000502 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100503 * @fb: framebuffer to unref
504 *
505 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000506 */
507void drm_framebuffer_unreference(struct drm_framebuffer *fb)
508{
Rob Clark82912722014-03-18 10:07:08 -0400509 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000510 kref_put(&fb->refcount, drm_framebuffer_free);
511}
512EXPORT_SYMBOL(drm_framebuffer_unreference);
513
514/**
515 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100516 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100517 *
518 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000519 */
520void drm_framebuffer_reference(struct drm_framebuffer *fb)
521{
Rob Clark82912722014-03-18 10:07:08 -0400522 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000523 kref_get(&fb->refcount);
524}
525EXPORT_SYMBOL(drm_framebuffer_reference);
526
Dave Airlief453ba02008-11-07 14:05:41 -0800527/**
Daniel Vetter36206362012-12-10 20:42:17 +0100528 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
529 * @fb: fb to unregister
530 *
531 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
532 * those used for fbdev. Note that the caller must hold a reference of it's own,
533 * i.e. the object may not be destroyed through this call (since it'll lead to a
534 * locking inversion).
535 */
536void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
537{
Daniel Vettera39a3572015-08-25 15:45:11 +0200538 struct drm_device *dev;
539
540 if (!fb)
541 return;
542
543 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100544
545 mutex_lock(&dev->mode_config.fb_lock);
546 /* Mark fb as reaped and drop idr ref. */
547 __drm_framebuffer_unregister(dev, fb);
548 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100549}
550EXPORT_SYMBOL(drm_framebuffer_unregister_private);
551
552/**
Dave Airlief453ba02008-11-07 14:05:41 -0800553 * drm_framebuffer_cleanup - remove a framebuffer object
554 * @fb: framebuffer to remove
555 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100556 * Cleanup framebuffer. This function is intended to be used from the drivers
557 * ->destroy callback. It can also be used to clean up driver private
558 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100559 *
560 * Note that this function does not remove the fb from active usuage - if it is
561 * still used anywhere, hilarity can ensue since userspace could call getfb on
562 * the id and get back -EINVAL. Obviously no concern at driver unload time.
563 *
564 * Also, the framebuffer will not be removed from the lookup idr - for
565 * user-created framebuffers this will happen in in the rmfb ioctl. For
566 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
567 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800568 */
569void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
570{
571 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100572
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100573 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000574 list_del(&fb->head);
575 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100576 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000577}
578EXPORT_SYMBOL(drm_framebuffer_cleanup);
579
580/**
581 * drm_framebuffer_remove - remove and unreference a framebuffer object
582 * @fb: framebuffer to remove
583 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000584 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100585 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100586 * passed-in framebuffer. Might take the modeset locks.
587 *
588 * Note that this function optimizes the cleanup away if the caller holds the
589 * last reference to the framebuffer. It is also guaranteed to not take the
590 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000591 */
592void drm_framebuffer_remove(struct drm_framebuffer *fb)
593{
Daniel Vettera39a3572015-08-25 15:45:11 +0200594 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800595 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800596 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000597 struct drm_mode_set set;
598 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800599
Daniel Vettera39a3572015-08-25 15:45:11 +0200600 if (!fb)
601 return;
602
603 dev = fb->dev;
604
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100605 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100606
Daniel Vetterb62584e2012-12-11 16:51:35 +0100607 /*
608 * drm ABI mandates that we remove any deleted framebuffers from active
609 * useage. But since most sane clients only remove framebuffers they no
610 * longer need, try to optimize this away.
611 *
612 * Since we're holding a reference ourselves, observing a refcount of 1
613 * means that we're the last holder and can skip it. Also, the refcount
614 * can never increase from 1 again, so we don't need any barriers or
615 * locks.
616 *
617 * Note that userspace could try to race with use and instate a new
618 * usage _after_ we've cleared all current ones. End result will be an
619 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
620 * in this manner.
621 */
622 if (atomic_read(&fb->refcount.refcount) > 1) {
623 drm_modeset_lock_all(dev);
624 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200625 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700626 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100627 /* should turn off the crtc */
628 memset(&set, 0, sizeof(struct drm_mode_set));
629 set.crtc = crtc;
630 set.fb = NULL;
631 ret = drm_mode_set_config_internal(&set);
632 if (ret)
633 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
634 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000635 }
Dave Airlief453ba02008-11-07 14:05:41 -0800636
Daniel Vetter6295d602015-07-09 23:44:25 +0200637 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300638 if (plane->fb == fb)
639 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800640 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100641 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800642 }
643
Rob Clarkf7eff602012-09-05 21:48:38 +0000644 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800645}
Rob Clarkf7eff602012-09-05 21:48:38 +0000646EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800647
Rob Clark51fd3712013-11-19 12:10:12 -0500648DEFINE_WW_CLASS(crtc_ww_class);
649
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200650static unsigned int drm_num_crtcs(struct drm_device *dev)
651{
652 unsigned int num = 0;
653 struct drm_crtc *tmp;
654
655 drm_for_each_crtc(tmp, dev) {
656 num++;
657 }
658
659 return num;
660}
661
Dave Airlief453ba02008-11-07 14:05:41 -0800662/**
Matt Ropere13161a2014-04-01 15:22:38 -0700663 * drm_crtc_init_with_planes - Initialise a new CRTC object with
664 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800665 * @dev: DRM device
666 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700667 * @primary: Primary plane for CRTC
668 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800669 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200670 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800671 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000672 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200673 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100674 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200675 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800676 */
Matt Ropere13161a2014-04-01 15:22:38 -0700677int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
678 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700679 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200680 const struct drm_crtc_funcs *funcs,
681 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800682{
Rob Clark51fd3712013-11-19 12:10:12 -0500683 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200684 int ret;
685
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100686 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
687 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
688
Dave Airlief453ba02008-11-07 14:05:41 -0800689 crtc->dev = dev;
690 crtc->funcs = funcs;
691
Rob Clark51fd3712013-11-19 12:10:12 -0500692 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200693 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
694 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100695 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800696
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200697 if (name) {
698 va_list ap;
699
700 va_start(ap, name);
701 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
702 va_end(ap);
703 } else {
704 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
705 drm_num_crtcs(dev));
706 }
707 if (!crtc->name) {
708 drm_mode_object_put(dev, &crtc->base);
709 return -ENOMEM;
710 }
711
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300712 crtc->base.properties = &crtc->properties;
713
Rob Clark51fd3712013-11-19 12:10:12 -0500714 list_add_tail(&crtc->head, &config->crtc_list);
715 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200716
Matt Ropere13161a2014-04-01 15:22:38 -0700717 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700718 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700719 if (primary)
720 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700721 if (cursor)
722 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700723
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100724 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
725 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100726 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100727 }
728
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100729 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800730}
Matt Ropere13161a2014-04-01 15:22:38 -0700731EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800732
733/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000734 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800735 * @crtc: CRTC to cleanup
736 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000737 * This function cleans up @crtc and removes it from the DRM mode setting
738 * core. Note that the function does *not* free the crtc structure itself,
739 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800740 */
741void drm_crtc_cleanup(struct drm_crtc *crtc)
742{
743 struct drm_device *dev = crtc->dev;
744
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000745 kfree(crtc->gamma_store);
746 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800747
Rob Clark51fd3712013-11-19 12:10:12 -0500748 drm_modeset_lock_fini(&crtc->mutex);
749
Dave Airlief453ba02008-11-07 14:05:41 -0800750 drm_mode_object_put(dev, &crtc->base);
751 list_del(&crtc->head);
752 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100753
754 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
755 if (crtc->state && crtc->funcs->atomic_destroy_state)
756 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100757
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200758 kfree(crtc->name);
759
Thierry Redinga18c0af2014-12-10 11:38:49 +0100760 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800761}
762EXPORT_SYMBOL(drm_crtc_cleanup);
763
764/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000765 * drm_crtc_index - find the index of a registered CRTC
766 * @crtc: CRTC to find index for
767 *
768 * Given a registered CRTC, return the index of that CRTC within a DRM
769 * device's list of CRTCs.
770 */
771unsigned int drm_crtc_index(struct drm_crtc *crtc)
772{
773 unsigned int index = 0;
774 struct drm_crtc *tmp;
775
Daniel Vettere4f62542015-07-09 23:44:35 +0200776 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000777 if (tmp == crtc)
778 return index;
779
780 index++;
781 }
782
783 BUG();
784}
785EXPORT_SYMBOL(drm_crtc_index);
786
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100787/*
Dave Airlief453ba02008-11-07 14:05:41 -0800788 * drm_mode_remove - remove and free a mode
789 * @connector: connector list to modify
790 * @mode: mode to remove
791 *
Dave Airlief453ba02008-11-07 14:05:41 -0800792 * Remove @mode from @connector's mode list, then free it.
793 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100794static void drm_mode_remove(struct drm_connector *connector,
795 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800796{
797 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100798 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800799}
Dave Airlief453ba02008-11-07 14:05:41 -0800800
801/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200802 * drm_display_info_set_bus_formats - set the supported bus formats
803 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100804 * @formats: array containing the supported bus formats
805 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200806 *
807 * Store the supported bus formats in display info structure.
808 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
809 * a full list of available formats.
810 */
811int drm_display_info_set_bus_formats(struct drm_display_info *info,
812 const u32 *formats,
813 unsigned int num_formats)
814{
815 u32 *fmts = NULL;
816
817 if (!formats && num_formats)
818 return -EINVAL;
819
820 if (formats && num_formats) {
821 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
822 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300823 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200824 return -ENOMEM;
825 }
826
827 kfree(info->bus_formats);
828 info->bus_formats = fmts;
829 info->num_bus_formats = num_formats;
830
831 return 0;
832}
833EXPORT_SYMBOL(drm_display_info_set_bus_formats);
834
835/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200836 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
837 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200838 *
839 * The kernel supports per-connector configration of its consoles through
840 * use of the video= parameter. This function parses that option and
841 * extracts the user's specified mode (or enable/disable status) for a
842 * particular connector. This is typically only used during the early fbdev
843 * setup.
844 */
845static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
846{
847 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
848 char *option = NULL;
849
850 if (fb_get_options(connector->name, &option))
851 return;
852
853 if (!drm_mode_parse_command_line_for_connector(option,
854 connector,
855 mode))
856 return;
857
858 if (mode->force) {
859 const char *s;
860
861 switch (mode->force) {
862 case DRM_FORCE_OFF:
863 s = "OFF";
864 break;
865 case DRM_FORCE_ON_DIGITAL:
866 s = "ON - dig";
867 break;
868 default:
869 case DRM_FORCE_ON:
870 s = "ON";
871 break;
872 }
873
874 DRM_INFO("forcing %s connector %s\n", connector->name, s);
875 connector->force = mode->force;
876 }
877
878 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
879 connector->name,
880 mode->xres, mode->yres,
881 mode->refresh_specified ? mode->refresh : 60,
882 mode->rb ? " reduced blanking" : "",
883 mode->margins ? " with margins" : "",
884 mode->interlace ? " interlaced" : "");
885}
886
887/**
Dave Airlief453ba02008-11-07 14:05:41 -0800888 * drm_connector_init - Init a preallocated connector
889 * @dev: DRM device
890 * @connector: the connector to init
891 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100892 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800893 *
Dave Airlief453ba02008-11-07 14:05:41 -0800894 * Initialises a preallocated connector. Connectors should be
895 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200896 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100897 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200898 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800899 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200900int drm_connector_init(struct drm_device *dev,
901 struct drm_connector *connector,
902 const struct drm_connector_funcs *funcs,
903 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800904{
Rob Clarkae16c592014-12-18 16:01:54 -0500905 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200906 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400907 struct ida *connector_ida =
908 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200909
Daniel Vetter84849902012-12-02 00:28:11 +0100910 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800911
Dave Airlie2ee39452014-07-24 11:53:45 +1000912 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200913 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300914 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200915
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300916 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800917 connector->dev = dev;
918 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800919 connector->connector_type = connector_type;
920 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400921 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
922 if (connector->connector_type_id < 0) {
923 ret = connector->connector_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300924 goto out_put;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400925 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300926 connector->name =
927 kasprintf(GFP_KERNEL, "%s-%d",
928 drm_connector_enum_list[connector_type].name,
929 connector->connector_type_id);
930 if (!connector->name) {
931 ret = -ENOMEM;
932 goto out_put;
933 }
934
Dave Airlief453ba02008-11-07 14:05:41 -0800935 INIT_LIST_HEAD(&connector->probed_modes);
936 INIT_LIST_HEAD(&connector->modes);
937 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000938 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800939
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200940 drm_connector_get_cmdline_mode(connector);
941
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100942 /* We should add connectors at the end to avoid upsetting the connector
943 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500944 list_add_tail(&connector->head, &config->connector_list);
945 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800946
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200947 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500948 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500949 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200950 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800951
Rob Clark58495562012-10-11 20:50:56 -0500952 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500953 config->dpms_property, 0);
954
955 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
956 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
957 }
Dave Airlief453ba02008-11-07 14:05:41 -0800958
Thomas Wood30f65702014-06-18 17:52:32 +0100959 connector->debugfs_entry = NULL;
960
Jani Nikula2abdd312014-05-14 16:58:19 +0300961out_put:
962 if (ret)
963 drm_mode_object_put(dev, &connector->base);
964
965out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100966 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200967
968 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800969}
970EXPORT_SYMBOL(drm_connector_init);
971
972/**
973 * drm_connector_cleanup - cleans up an initialised connector
974 * @connector: connector to cleanup
975 *
Dave Airlief453ba02008-11-07 14:05:41 -0800976 * Cleans up the connector but doesn't free the object.
977 */
978void drm_connector_cleanup(struct drm_connector *connector)
979{
980 struct drm_device *dev = connector->dev;
981 struct drm_display_mode *mode, *t;
982
Dave Airlie40d9b042014-10-20 16:29:33 +1000983 if (connector->tile_group) {
984 drm_mode_put_tile_group(dev, connector->tile_group);
985 connector->tile_group = NULL;
986 }
987
Dave Airlief453ba02008-11-07 14:05:41 -0800988 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
989 drm_mode_remove(connector, mode);
990
991 list_for_each_entry_safe(mode, t, &connector->modes, head)
992 drm_mode_remove(connector, mode);
993
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400994 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
995 connector->connector_type_id);
996
Boris Brezillonb5571e92014-07-22 12:09:10 +0200997 kfree(connector->display_info.bus_formats);
Dave Airlief453ba02008-11-07 14:05:41 -0800998 drm_mode_object_put(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300999 kfree(connector->name);
1000 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001001 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001002 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +01001003
1004 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1005 if (connector->state && connector->funcs->atomic_destroy_state)
1006 connector->funcs->atomic_destroy_state(connector,
1007 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001008
1009 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001010}
1011EXPORT_SYMBOL(drm_connector_cleanup);
1012
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001013/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001014 * drm_connector_index - find the index of a registered connector
1015 * @connector: connector to find index for
1016 *
1017 * Given a registered connector, return the index of that connector within a DRM
1018 * device's list of connectors.
1019 */
1020unsigned int drm_connector_index(struct drm_connector *connector)
1021{
1022 unsigned int index = 0;
1023 struct drm_connector *tmp;
Daniel Vetterc7eb76f2014-11-19 18:38:06 +01001024 struct drm_mode_config *config = &connector->dev->mode_config;
1025
1026 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
Daniel Vetter10f637b2014-07-29 13:47:11 +02001027
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001028 drm_for_each_connector(tmp, connector->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001029 if (tmp == connector)
1030 return index;
1031
1032 index++;
1033 }
1034
1035 BUG();
1036}
1037EXPORT_SYMBOL(drm_connector_index);
1038
1039/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001040 * drm_connector_register - register a connector
1041 * @connector: the connector to register
1042 *
1043 * Register userspace interfaces for a connector
1044 *
1045 * Returns:
1046 * Zero on success, error code on failure.
1047 */
1048int drm_connector_register(struct drm_connector *connector)
1049{
Thomas Wood30f65702014-06-18 17:52:32 +01001050 int ret;
1051
Dave Airlie2ee39452014-07-24 11:53:45 +10001052 drm_mode_object_register(connector->dev, &connector->base);
1053
Thomas Wood30f65702014-06-18 17:52:32 +01001054 ret = drm_sysfs_connector_add(connector);
1055 if (ret)
1056 return ret;
1057
1058 ret = drm_debugfs_connector_add(connector);
1059 if (ret) {
1060 drm_sysfs_connector_remove(connector);
1061 return ret;
1062 }
1063
1064 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001065}
1066EXPORT_SYMBOL(drm_connector_register);
1067
1068/**
1069 * drm_connector_unregister - unregister a connector
1070 * @connector: the connector to unregister
1071 *
1072 * Unregister userspace interfaces for a connector
1073 */
1074void drm_connector_unregister(struct drm_connector *connector)
1075{
1076 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001077 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001078}
1079EXPORT_SYMBOL(drm_connector_unregister);
1080
Thomas Wood34ea3d32014-05-29 16:57:41 +01001081/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001082 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001083 * @dev: drm device
1084 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001085 * This functions unregisters all connectors from sysfs and other places so
1086 * that userspace can no longer access them. Drivers should call this as the
1087 * first step tearing down the device instace, or when the underlying
1088 * physical device disappeared (e.g. USB unplug), right before calling
1089 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001090 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001091void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001092{
1093 struct drm_connector *connector;
1094
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001095 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001096 drm_for_each_connector(connector, dev)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001097 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001098}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001099EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001100
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001101/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001102 * drm_encoder_init - Init a preallocated encoder
1103 * @dev: drm device
1104 * @encoder: the encoder to init
1105 * @funcs: callbacks for this encoder
1106 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001107 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001108 *
1109 * Initialises a preallocated encoder. Encoder should be
1110 * subclassed as part of driver encoder objects.
1111 *
1112 * Returns:
1113 * Zero on success, error code on failure.
1114 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001115int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001116 struct drm_encoder *encoder,
1117 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001118 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001119{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001120 int ret;
1121
Daniel Vetter84849902012-12-02 00:28:11 +01001122 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001123
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001124 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1125 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001126 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001127
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001128 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001129 encoder->encoder_type = encoder_type;
1130 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001131 if (name) {
1132 va_list ap;
1133
1134 va_start(ap, name);
1135 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1136 va_end(ap);
1137 } else {
1138 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1139 drm_encoder_enum_list[encoder_type].name,
1140 encoder->base.id);
1141 }
Jani Nikulae5748942014-05-14 16:58:20 +03001142 if (!encoder->name) {
1143 ret = -ENOMEM;
1144 goto out_put;
1145 }
Dave Airlief453ba02008-11-07 14:05:41 -08001146
1147 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1148 dev->mode_config.num_encoder++;
1149
Jani Nikulae5748942014-05-14 16:58:20 +03001150out_put:
1151 if (ret)
1152 drm_mode_object_put(dev, &encoder->base);
1153
1154out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001155 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001156
1157 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001158}
1159EXPORT_SYMBOL(drm_encoder_init);
1160
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001161/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001162 * drm_encoder_index - find the index of a registered encoder
1163 * @encoder: encoder to find index for
1164 *
1165 * Given a registered encoder, return the index of that encoder within a DRM
1166 * device's list of encoders.
1167 */
1168unsigned int drm_encoder_index(struct drm_encoder *encoder)
1169{
1170 unsigned int index = 0;
1171 struct drm_encoder *tmp;
1172
1173 drm_for_each_encoder(tmp, encoder->dev) {
1174 if (tmp == encoder)
1175 return index;
1176
1177 index++;
1178 }
1179
1180 BUG();
1181}
1182EXPORT_SYMBOL(drm_encoder_index);
1183
1184/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001185 * drm_encoder_cleanup - cleans up an initialised encoder
1186 * @encoder: encoder to cleanup
1187 *
1188 * Cleans up the encoder but doesn't free the object.
1189 */
Dave Airlief453ba02008-11-07 14:05:41 -08001190void drm_encoder_cleanup(struct drm_encoder *encoder)
1191{
1192 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001193
Daniel Vetter84849902012-12-02 00:28:11 +01001194 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001195 drm_mode_object_put(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001196 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001197 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001198 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001199 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001200
1201 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001202}
1203EXPORT_SYMBOL(drm_encoder_cleanup);
1204
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001205static unsigned int drm_num_planes(struct drm_device *dev)
1206{
1207 unsigned int num = 0;
1208 struct drm_plane *tmp;
1209
1210 drm_for_each_plane(tmp, dev) {
1211 num++;
1212 }
1213
1214 return num;
1215}
1216
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001217/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001218 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001219 * @dev: DRM device
1220 * @plane: plane object to init
1221 * @possible_crtcs: bitmask of possible CRTCs
1222 * @funcs: callbacks for the new plane
1223 * @formats: array of supported formats (%DRM_FORMAT_*)
1224 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001225 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001226 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001227 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001228 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001229 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001230 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001231 * Zero on success, error code on failure.
1232 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001233int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1234 unsigned long possible_crtcs,
1235 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001236 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001237 enum drm_plane_type type,
1238 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001239{
Rob Clark6b4959f2014-12-18 16:01:53 -05001240 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001241 int ret;
1242
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001243 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1244 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001245 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001246
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001247 drm_modeset_lock_init(&plane->mutex);
1248
Rob Clark4d939142012-05-17 02:23:27 -06001249 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001250 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001251 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001252 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1253 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001254 if (!plane->format_types) {
1255 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1256 drm_mode_object_put(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001257 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001258 }
1259
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001260 if (name) {
1261 va_list ap;
1262
1263 va_start(ap, name);
1264 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1265 va_end(ap);
1266 } else {
1267 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1268 drm_num_planes(dev));
1269 }
1270 if (!plane->name) {
1271 kfree(plane->format_types);
1272 drm_mode_object_put(dev, &plane->base);
1273 return -ENOMEM;
1274 }
1275
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001276 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001277 plane->format_count = format_count;
1278 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001279 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001280
Rob Clark6b4959f2014-12-18 16:01:53 -05001281 list_add_tail(&plane->head, &config->plane_list);
1282 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001283 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001284 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001285
Rob Clark9922ab52014-04-01 20:16:57 -04001286 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001287 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001288 plane->type);
1289
Rob Clark6b4959f2014-12-18 16:01:53 -05001290 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1291 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1292 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1293 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1294 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1295 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1296 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1297 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1298 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1299 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1300 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1301 }
1302
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001303 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001304}
Matt Roperdc415ff2014-04-01 15:22:36 -07001305EXPORT_SYMBOL(drm_universal_plane_init);
1306
1307/**
1308 * drm_plane_init - Initialize a legacy plane
1309 * @dev: DRM device
1310 * @plane: plane object to init
1311 * @possible_crtcs: bitmask of possible CRTCs
1312 * @funcs: callbacks for the new plane
1313 * @formats: array of supported formats (%DRM_FORMAT_*)
1314 * @format_count: number of elements in @formats
1315 * @is_primary: plane type (primary vs overlay)
1316 *
1317 * Legacy API to initialize a DRM plane.
1318 *
1319 * New drivers should call drm_universal_plane_init() instead.
1320 *
1321 * Returns:
1322 * Zero on success, error code on failure.
1323 */
1324int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1325 unsigned long possible_crtcs,
1326 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001327 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001328 bool is_primary)
1329{
1330 enum drm_plane_type type;
1331
1332 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1333 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001334 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001335}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001336EXPORT_SYMBOL(drm_plane_init);
1337
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001338/**
1339 * drm_plane_cleanup - Clean up the core plane usage
1340 * @plane: plane to cleanup
1341 *
1342 * This function cleans up @plane and removes it from the DRM mode setting
1343 * core. Note that the function does *not* free the plane structure itself,
1344 * this is the responsibility of the caller.
1345 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001346void drm_plane_cleanup(struct drm_plane *plane)
1347{
1348 struct drm_device *dev = plane->dev;
1349
Daniel Vetter84849902012-12-02 00:28:11 +01001350 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001351 kfree(plane->format_types);
1352 drm_mode_object_put(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001353
1354 BUG_ON(list_empty(&plane->head));
1355
1356 list_del(&plane->head);
1357 dev->mode_config.num_total_plane--;
1358 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1359 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001360 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001361
1362 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1363 if (plane->state && plane->funcs->atomic_destroy_state)
1364 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001365
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001366 kfree(plane->name);
1367
Thierry Redinga18c0af2014-12-10 11:38:49 +01001368 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001369}
1370EXPORT_SYMBOL(drm_plane_cleanup);
1371
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001372/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001373 * drm_plane_index - find the index of a registered plane
1374 * @plane: plane to find index for
1375 *
1376 * Given a registered plane, return the index of that CRTC within a DRM
1377 * device's list of planes.
1378 */
1379unsigned int drm_plane_index(struct drm_plane *plane)
1380{
1381 unsigned int index = 0;
1382 struct drm_plane *tmp;
1383
Daniel Vettere4f62542015-07-09 23:44:35 +02001384 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001385 if (tmp == plane)
1386 return index;
1387
1388 index++;
1389 }
1390
1391 BUG();
1392}
1393EXPORT_SYMBOL(drm_plane_index);
1394
1395/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001396 * drm_plane_from_index - find the registered plane at an index
1397 * @dev: DRM device
1398 * @idx: index of registered plane to find for
1399 *
1400 * Given a plane index, return the registered plane from DRM device's
1401 * list of planes with matching index.
1402 */
1403struct drm_plane *
1404drm_plane_from_index(struct drm_device *dev, int idx)
1405{
1406 struct drm_plane *plane;
1407 unsigned int i = 0;
1408
Daniel Vetter6295d602015-07-09 23:44:25 +02001409 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001410 if (i == idx)
1411 return plane;
1412 i++;
1413 }
1414 return NULL;
1415}
1416EXPORT_SYMBOL(drm_plane_from_index);
1417
1418/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001419 * drm_plane_force_disable - Forcibly disable a plane
1420 * @plane: plane to disable
1421 *
1422 * Forces the plane to be disabled.
1423 *
1424 * Used when the plane's current framebuffer is destroyed,
1425 * and when restoring fbdev mode.
1426 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001427void drm_plane_force_disable(struct drm_plane *plane)
1428{
1429 int ret;
1430
Daniel Vetter3d30a592014-07-27 13:42:42 +02001431 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001432 return;
1433
Daniel Vetter3d30a592014-07-27 13:42:42 +02001434 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001435 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001436 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001437 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001438 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001439 return;
1440 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001441 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001442 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001443 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001444 plane->fb = NULL;
1445 plane->crtc = NULL;
1446}
1447EXPORT_SYMBOL(drm_plane_force_disable);
1448
Rob Clark6b4959f2014-12-18 16:01:53 -05001449static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001450{
Rob Clark356af0e2014-12-18 16:01:52 -05001451 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001452
1453 /*
1454 * Standard properties (apply to all connectors)
1455 */
Rob Clark356af0e2014-12-18 16:01:52 -05001456 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001457 DRM_MODE_PROP_IMMUTABLE,
1458 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001459 if (!prop)
1460 return -ENOMEM;
1461 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001462
Rob Clark356af0e2014-12-18 16:01:52 -05001463 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001464 "DPMS", drm_dpms_enum_list,
1465 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001466 if (!prop)
1467 return -ENOMEM;
1468 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001469
Rob Clark356af0e2014-12-18 16:01:52 -05001470 prop = drm_property_create(dev,
1471 DRM_MODE_PROP_BLOB |
1472 DRM_MODE_PROP_IMMUTABLE,
1473 "PATH", 0);
1474 if (!prop)
1475 return -ENOMEM;
1476 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001477
Rob Clark356af0e2014-12-18 16:01:52 -05001478 prop = drm_property_create(dev,
1479 DRM_MODE_PROP_BLOB |
1480 DRM_MODE_PROP_IMMUTABLE,
1481 "TILE", 0);
1482 if (!prop)
1483 return -ENOMEM;
1484 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001485
Rob Clark6b4959f2014-12-18 16:01:53 -05001486 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001487 "type", drm_plane_type_enum_list,
1488 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001489 if (!prop)
1490 return -ENOMEM;
1491 dev->mode_config.plane_type_property = prop;
1492
1493 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1494 "SRC_X", 0, UINT_MAX);
1495 if (!prop)
1496 return -ENOMEM;
1497 dev->mode_config.prop_src_x = prop;
1498
1499 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1500 "SRC_Y", 0, UINT_MAX);
1501 if (!prop)
1502 return -ENOMEM;
1503 dev->mode_config.prop_src_y = prop;
1504
1505 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1506 "SRC_W", 0, UINT_MAX);
1507 if (!prop)
1508 return -ENOMEM;
1509 dev->mode_config.prop_src_w = prop;
1510
1511 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1512 "SRC_H", 0, UINT_MAX);
1513 if (!prop)
1514 return -ENOMEM;
1515 dev->mode_config.prop_src_h = prop;
1516
1517 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1518 "CRTC_X", INT_MIN, INT_MAX);
1519 if (!prop)
1520 return -ENOMEM;
1521 dev->mode_config.prop_crtc_x = prop;
1522
1523 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1524 "CRTC_Y", INT_MIN, INT_MAX);
1525 if (!prop)
1526 return -ENOMEM;
1527 dev->mode_config.prop_crtc_y = prop;
1528
1529 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1530 "CRTC_W", 0, INT_MAX);
1531 if (!prop)
1532 return -ENOMEM;
1533 dev->mode_config.prop_crtc_w = prop;
1534
1535 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1536 "CRTC_H", 0, INT_MAX);
1537 if (!prop)
1538 return -ENOMEM;
1539 dev->mode_config.prop_crtc_h = prop;
1540
1541 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1542 "FB_ID", DRM_MODE_OBJECT_FB);
1543 if (!prop)
1544 return -ENOMEM;
1545 dev->mode_config.prop_fb_id = prop;
1546
1547 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1548 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1549 if (!prop)
1550 return -ENOMEM;
1551 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001552
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001553 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1554 "ACTIVE");
1555 if (!prop)
1556 return -ENOMEM;
1557 dev->mode_config.prop_active = prop;
1558
Daniel Stone955f3c32015-05-25 19:11:52 +01001559 prop = drm_property_create(dev,
1560 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1561 "MODE_ID", 0);
1562 if (!prop)
1563 return -ENOMEM;
1564 dev->mode_config.prop_mode_id = prop;
1565
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001566 prop = drm_property_create(dev,
1567 DRM_MODE_PROP_BLOB,
1568 "DEGAMMA_LUT", 0);
1569 if (!prop)
1570 return -ENOMEM;
1571 dev->mode_config.degamma_lut_property = prop;
1572
1573 prop = drm_property_create_range(dev,
1574 DRM_MODE_PROP_IMMUTABLE,
1575 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1576 if (!prop)
1577 return -ENOMEM;
1578 dev->mode_config.degamma_lut_size_property = prop;
1579
1580 prop = drm_property_create(dev,
1581 DRM_MODE_PROP_BLOB,
1582 "CTM", 0);
1583 if (!prop)
1584 return -ENOMEM;
1585 dev->mode_config.ctm_property = prop;
1586
1587 prop = drm_property_create(dev,
1588 DRM_MODE_PROP_BLOB,
1589 "GAMMA_LUT", 0);
1590 if (!prop)
1591 return -ENOMEM;
1592 dev->mode_config.gamma_lut_property = prop;
1593
1594 prop = drm_property_create_range(dev,
1595 DRM_MODE_PROP_IMMUTABLE,
1596 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1597 if (!prop)
1598 return -ENOMEM;
1599 dev->mode_config.gamma_lut_size_property = prop;
1600
Rob Clark9922ab52014-04-01 20:16:57 -04001601 return 0;
1602}
1603
Dave Airlief453ba02008-11-07 14:05:41 -08001604/**
1605 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1606 * @dev: DRM device
1607 *
1608 * Called by a driver the first time a DVI-I connector is made.
1609 */
1610int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1611{
1612 struct drm_property *dvi_i_selector;
1613 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001614
1615 if (dev->mode_config.dvi_i_select_subconnector_property)
1616 return 0;
1617
1618 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001619 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001620 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001621 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001622 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001623 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1624
Sascha Hauer4a67d392012-02-06 10:58:17 +01001625 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001626 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001627 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001628 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001629 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1630
1631 return 0;
1632}
1633EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1634
1635/**
1636 * drm_create_tv_properties - create TV specific connector properties
1637 * @dev: DRM device
1638 * @num_modes: number of different TV formats (modes) supported
1639 * @modes: array of pointers to strings containing name of each format
1640 *
1641 * Called by a driver's TV initialization routine, this function creates
1642 * the TV specific connector properties for a given device. Caller is
1643 * responsible for allocating a list of format names and passing them to
1644 * this routine.
1645 */
Thierry Reding2f763312014-10-13 12:45:57 +02001646int drm_mode_create_tv_properties(struct drm_device *dev,
1647 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001648 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001649{
1650 struct drm_property *tv_selector;
1651 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001652 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001653
1654 if (dev->mode_config.tv_select_subconnector_property)
1655 return 0;
1656
1657 /*
1658 * Basic connector properties
1659 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001660 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001661 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001662 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001663 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001664 if (!tv_selector)
1665 goto nomem;
1666
Dave Airlief453ba02008-11-07 14:05:41 -08001667 dev->mode_config.tv_select_subconnector_property = tv_selector;
1668
1669 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001670 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1671 "subconnector",
1672 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001673 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001674 if (!tv_subconnector)
1675 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001676 dev->mode_config.tv_subconnector_property = tv_subconnector;
1677
1678 /*
1679 * Other, TV specific properties: margins & TV modes.
1680 */
1681 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001682 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001683 if (!dev->mode_config.tv_left_margin_property)
1684 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001685
1686 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001687 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001688 if (!dev->mode_config.tv_right_margin_property)
1689 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001690
1691 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001692 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001693 if (!dev->mode_config.tv_top_margin_property)
1694 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001695
1696 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001697 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001698 if (!dev->mode_config.tv_bottom_margin_property)
1699 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001700
1701 dev->mode_config.tv_mode_property =
1702 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1703 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001704 if (!dev->mode_config.tv_mode_property)
1705 goto nomem;
1706
Dave Airlief453ba02008-11-07 14:05:41 -08001707 for (i = 0; i < num_modes; i++)
1708 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1709 i, modes[i]);
1710
Francisco Jerezb6b79022009-08-02 04:19:20 +02001711 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001712 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001713 if (!dev->mode_config.tv_brightness_property)
1714 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001715
1716 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001717 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001718 if (!dev->mode_config.tv_contrast_property)
1719 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001720
1721 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001722 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001723 if (!dev->mode_config.tv_flicker_reduction_property)
1724 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001725
Francisco Jereza75f0232009-08-12 02:30:10 +02001726 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001727 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001728 if (!dev->mode_config.tv_overscan_property)
1729 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001730
1731 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001732 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001733 if (!dev->mode_config.tv_saturation_property)
1734 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001735
1736 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001737 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001738 if (!dev->mode_config.tv_hue_property)
1739 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001740
Dave Airlief453ba02008-11-07 14:05:41 -08001741 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001742nomem:
1743 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001744}
1745EXPORT_SYMBOL(drm_mode_create_tv_properties);
1746
1747/**
1748 * drm_mode_create_scaling_mode_property - create scaling mode property
1749 * @dev: DRM device
1750 *
1751 * Called by a driver the first time it's needed, must be attached to desired
1752 * connectors.
1753 */
1754int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1755{
1756 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001757
1758 if (dev->mode_config.scaling_mode_property)
1759 return 0;
1760
1761 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001762 drm_property_create_enum(dev, 0, "scaling mode",
1763 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001764 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001765
1766 dev->mode_config.scaling_mode_property = scaling_mode;
1767
1768 return 0;
1769}
1770EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1771
1772/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301773 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1774 * @dev: DRM device
1775 *
1776 * Called by a driver the first time it's needed, must be attached to desired
1777 * connectors.
1778 *
1779 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001780 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301781 */
1782int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1783{
1784 if (dev->mode_config.aspect_ratio_property)
1785 return 0;
1786
1787 dev->mode_config.aspect_ratio_property =
1788 drm_property_create_enum(dev, 0, "aspect ratio",
1789 drm_aspect_ratio_enum_list,
1790 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1791
1792 if (dev->mode_config.aspect_ratio_property == NULL)
1793 return -ENOMEM;
1794
1795 return 0;
1796}
1797EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1798
1799/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001800 * drm_mode_create_dirty_property - create dirty property
1801 * @dev: DRM device
1802 *
1803 * Called by a driver the first time it's needed, must be attached to desired
1804 * connectors.
1805 */
1806int drm_mode_create_dirty_info_property(struct drm_device *dev)
1807{
1808 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001809
1810 if (dev->mode_config.dirty_info_property)
1811 return 0;
1812
1813 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001814 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001815 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001816 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001817 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001818 dev->mode_config.dirty_info_property = dirty_info;
1819
1820 return 0;
1821}
1822EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1823
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001824/**
1825 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1826 * @dev: DRM device
1827 *
1828 * Create the the suggested x/y offset property for connectors.
1829 */
1830int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1831{
1832 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1833 return 0;
1834
1835 dev->mode_config.suggested_x_property =
1836 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1837
1838 dev->mode_config.suggested_y_property =
1839 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1840
1841 if (dev->mode_config.suggested_x_property == NULL ||
1842 dev->mode_config.suggested_y_property == NULL)
1843 return -ENOMEM;
1844 return 0;
1845}
1846EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1847
Dave Airlief453ba02008-11-07 14:05:41 -08001848/**
Dave Airlief453ba02008-11-07 14:05:41 -08001849 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001850 * @dev: drm device for the ioctl
1851 * @data: data pointer for the ioctl
1852 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001853 *
Dave Airlief453ba02008-11-07 14:05:41 -08001854 * Construct a set of configuration description structures and return
1855 * them to the user, including CRTC, connector and framebuffer configuration.
1856 *
1857 * Called by the user via ioctl.
1858 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001859 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001860 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001861 */
1862int drm_mode_getresources(struct drm_device *dev, void *data,
1863 struct drm_file *file_priv)
1864{
1865 struct drm_mode_card_res *card_res = data;
1866 struct list_head *lh;
1867 struct drm_framebuffer *fb;
1868 struct drm_connector *connector;
1869 struct drm_crtc *crtc;
1870 struct drm_encoder *encoder;
1871 int ret = 0;
1872 int connector_count = 0;
1873 int crtc_count = 0;
1874 int fb_count = 0;
1875 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001876 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001877 uint32_t __user *fb_id;
1878 uint32_t __user *crtc_id;
1879 uint32_t __user *connector_id;
1880 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001881
Dave Airliefb3b06c2011-02-08 13:55:21 +10001882 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1883 return -EINVAL;
1884
Dave Airlief453ba02008-11-07 14:05:41 -08001885
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001886 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001887 /*
1888 * For the non-control nodes we need to limit the list of resources
1889 * by IDs in the group list for this node
1890 */
1891 list_for_each(lh, &file_priv->fbs)
1892 fb_count++;
1893
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001894 /* handle this in 4 parts */
1895 /* FBs */
1896 if (card_res->count_fbs >= fb_count) {
1897 copied = 0;
1898 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1899 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1900 if (put_user(fb->base.id, fb_id + copied)) {
1901 mutex_unlock(&file_priv->fbs_lock);
1902 return -EFAULT;
1903 }
1904 copied++;
1905 }
1906 }
1907 card_res->count_fbs = fb_count;
1908 mutex_unlock(&file_priv->fbs_lock);
1909
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001910 /* mode_config.mutex protects the connector list against e.g. DP MST
1911 * connector hot-adding. CRTC/Plane lists are invariant. */
1912 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001913 drm_for_each_crtc(crtc, dev)
1914 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001915
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001916 drm_for_each_connector(connector, dev)
1917 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001918
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001919 drm_for_each_encoder(encoder, dev)
1920 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001921
1922 card_res->max_height = dev->mode_config.max_height;
1923 card_res->min_height = dev->mode_config.min_height;
1924 card_res->max_width = dev->mode_config.max_width;
1925 card_res->min_width = dev->mode_config.min_width;
1926
Dave Airlief453ba02008-11-07 14:05:41 -08001927 /* CRTCs */
1928 if (card_res->count_crtcs >= crtc_count) {
1929 copied = 0;
1930 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001931 drm_for_each_crtc(crtc, dev) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001932 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1933 crtc->base.id, crtc->name);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001934 if (put_user(crtc->base.id, crtc_id + copied)) {
1935 ret = -EFAULT;
1936 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001937 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001938 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001939 }
1940 }
1941 card_res->count_crtcs = crtc_count;
1942
1943 /* Encoders */
1944 if (card_res->count_encoders >= encoder_count) {
1945 copied = 0;
1946 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001947 drm_for_each_encoder(encoder, dev) {
1948 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1949 encoder->name);
1950 if (put_user(encoder->base.id, encoder_id +
1951 copied)) {
1952 ret = -EFAULT;
1953 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001954 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001955 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001956 }
1957 }
1958 card_res->count_encoders = encoder_count;
1959
1960 /* Connectors */
1961 if (card_res->count_connectors >= connector_count) {
1962 copied = 0;
1963 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001964 drm_for_each_connector(connector, dev) {
1965 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1966 connector->base.id,
1967 connector->name);
1968 if (put_user(connector->base.id,
1969 connector_id + copied)) {
1970 ret = -EFAULT;
1971 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001972 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001973 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001974 }
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->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002014 if (crtc->primary->fb)
2015 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002016 else
2017 crtc_resp->fb_id = 0;
2018
Daniel Vetter31c946e2015-02-22 12:24:17 +01002019 if (crtc->state) {
2020 crtc_resp->x = crtc->primary->state->src_x >> 16;
2021 crtc_resp->y = crtc->primary->state->src_y >> 16;
2022 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002023 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002024 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002025
Daniel Vetter31c946e2015-02-22 12:24:17 +01002026 } else {
2027 crtc_resp->mode_valid = 0;
2028 }
Dave Airlief453ba02008-11-07 14:05:41 -08002029 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002030 crtc_resp->x = crtc->x;
2031 crtc_resp->y = crtc->y;
2032 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002033 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002034 crtc_resp->mode_valid = 1;
2035
2036 } else {
2037 crtc_resp->mode_valid = 0;
2038 }
Dave Airlief453ba02008-11-07 14:05:41 -08002039 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002040 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002041
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002042 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002043}
2044
Damien Lespiau61d8e322013-09-25 16:45:22 +01002045static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2046 const struct drm_file *file_priv)
2047{
2048 /*
2049 * If user-space hasn't configured the driver to expose the stereo 3D
2050 * modes, don't expose them.
2051 */
2052 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2053 return false;
2054
2055 return true;
2056}
2057
Daniel Vetterabd69c52014-11-25 23:50:05 +01002058static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2059{
2060 /* For atomic drivers only state objects are synchronously updated and
2061 * protected by modeset locks, so check those first. */
2062 if (connector->state)
2063 return connector->state->best_encoder;
2064 return connector->encoder;
2065}
2066
Rob Clark95cbf112014-12-18 16:01:49 -05002067/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002068static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002069 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2070 uint32_t *arg_count_props)
2071{
Rob Clark88a48e22014-12-18 16:01:50 -05002072 int props_count;
2073 int i, ret, copied;
2074
2075 props_count = obj->properties->count;
2076 if (!atomic)
2077 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002078
2079 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002080 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002081 struct drm_property *prop = obj->properties->properties[i];
2082 uint64_t val;
2083
Rob Clark88a48e22014-12-18 16:01:50 -05002084 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2085 continue;
2086
Rob Clark95cbf112014-12-18 16:01:49 -05002087 ret = drm_object_property_get_value(obj, prop, &val);
2088 if (ret)
2089 return ret;
2090
2091 if (put_user(prop->base.id, prop_ptr + copied))
2092 return -EFAULT;
2093
2094 if (put_user(val, prop_values + copied))
2095 return -EFAULT;
2096
2097 copied++;
2098 }
2099 }
2100 *arg_count_props = props_count;
2101
2102 return 0;
2103}
2104
Dave Airlief453ba02008-11-07 14:05:41 -08002105/**
2106 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002107 * @dev: drm device for the ioctl
2108 * @data: data pointer for the ioctl
2109 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002110 *
Dave Airlief453ba02008-11-07 14:05:41 -08002111 * Construct a connector configuration structure to return to the user.
2112 *
2113 * Called by the user via ioctl.
2114 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002115 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002116 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002117 */
2118int drm_mode_getconnector(struct drm_device *dev, void *data,
2119 struct drm_file *file_priv)
2120{
2121 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002122 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002123 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002124 struct drm_display_mode *mode;
2125 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002126 int encoders_count = 0;
2127 int ret = 0;
2128 int copied = 0;
2129 int i;
2130 struct drm_mode_modeinfo u_mode;
2131 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002132 uint32_t __user *encoder_ptr;
2133
Dave Airliefb3b06c2011-02-08 13:55:21 +10002134 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2135 return -EINVAL;
2136
Dave Airlief453ba02008-11-07 14:05:41 -08002137 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2138
Jerome Glisse94401062010-07-15 15:43:25 -04002139 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002140
Daniel Vetter7b240562012-12-12 00:35:33 +01002141 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002142
Rob Clarka2b34e22013-10-05 16:36:52 -04002143 connector = drm_connector_find(dev, out_resp->connector_id);
2144 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002145 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002146 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002147 }
Dave Airlief453ba02008-11-07 14:05:41 -08002148
Thierry Reding01073b02014-12-10 13:03:38 +01002149 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2150 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002151 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002152
2153 if (out_resp->count_modes == 0) {
2154 connector->funcs->fill_modes(connector,
2155 dev->mode_config.max_width,
2156 dev->mode_config.max_height);
2157 }
2158
2159 /* delayed so we get modes regardless of pre-fill_modes state */
2160 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002161 if (drm_mode_expose_to_userspace(mode, file_priv))
2162 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002163
2164 out_resp->connector_id = connector->base.id;
2165 out_resp->connector_type = connector->connector_type;
2166 out_resp->connector_type_id = connector->connector_type_id;
2167 out_resp->mm_width = connector->display_info.width_mm;
2168 out_resp->mm_height = connector->display_info.height_mm;
2169 out_resp->subpixel = connector->display_info.subpixel_order;
2170 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002171
2172 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002173 encoder = drm_connector_get_encoder(connector);
2174 if (encoder)
2175 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002176 else
2177 out_resp->encoder_id = 0;
2178
2179 /*
2180 * This ioctl is called twice, once to determine how much space is
2181 * needed, and the 2nd time to fill it.
2182 */
2183 if ((out_resp->count_modes >= mode_count) && mode_count) {
2184 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002185 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002186 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002187 if (!drm_mode_expose_to_userspace(mode, file_priv))
2188 continue;
2189
Daniel Stone934a8a82015-05-22 13:34:48 +01002190 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002191 if (copy_to_user(mode_ptr + copied,
2192 &u_mode, sizeof(u_mode))) {
2193 ret = -EFAULT;
2194 goto out;
2195 }
2196 copied++;
2197 }
2198 }
2199 out_resp->count_modes = mode_count;
2200
Rob Clark88a48e22014-12-18 16:01:50 -05002201 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002202 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2203 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2204 &out_resp->count_props);
2205 if (ret)
2206 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002207
2208 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2209 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002210 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002211 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2212 if (connector->encoder_ids[i] != 0) {
2213 if (put_user(connector->encoder_ids[i],
2214 encoder_ptr + copied)) {
2215 ret = -EFAULT;
2216 goto out;
2217 }
2218 copied++;
2219 }
2220 }
2221 }
2222 out_resp->count_encoders = encoders_count;
2223
2224out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002225 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002226
2227out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002228 mutex_unlock(&dev->mode_config.mutex);
2229
Dave Airlief453ba02008-11-07 14:05:41 -08002230 return ret;
2231}
2232
Daniel Vetterabd69c52014-11-25 23:50:05 +01002233static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2234{
2235 struct drm_connector *connector;
2236 struct drm_device *dev = encoder->dev;
2237 bool uses_atomic = false;
2238
2239 /* For atomic drivers only state objects are synchronously updated and
2240 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002241 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002242 if (!connector->state)
2243 continue;
2244
2245 uses_atomic = true;
2246
2247 if (connector->state->best_encoder != encoder)
2248 continue;
2249
2250 return connector->state->crtc;
2251 }
2252
2253 /* Don't return stale data (e.g. pending async disable). */
2254 if (uses_atomic)
2255 return NULL;
2256
2257 return encoder->crtc;
2258}
2259
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002260/**
2261 * drm_mode_getencoder - get encoder configuration
2262 * @dev: drm device for the ioctl
2263 * @data: data pointer for the ioctl
2264 * @file_priv: drm file for the ioctl call
2265 *
2266 * Construct a encoder configuration structure to return to the user.
2267 *
2268 * Called by the user via ioctl.
2269 *
2270 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002271 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002272 */
Dave Airlief453ba02008-11-07 14:05:41 -08002273int drm_mode_getencoder(struct drm_device *dev, void *data,
2274 struct drm_file *file_priv)
2275{
2276 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002277 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002278 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002279
Dave Airliefb3b06c2011-02-08 13:55:21 +10002280 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2281 return -EINVAL;
2282
Rob Clarka2b34e22013-10-05 16:36:52 -04002283 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002284 if (!encoder)
2285 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002286
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002287 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002288 crtc = drm_encoder_get_crtc(encoder);
2289 if (crtc)
2290 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002291 else
2292 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002293 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2294
Dave Airlief453ba02008-11-07 14:05:41 -08002295 enc_resp->encoder_type = encoder->encoder_type;
2296 enc_resp->encoder_id = encoder->base.id;
2297 enc_resp->possible_crtcs = encoder->possible_crtcs;
2298 enc_resp->possible_clones = encoder->possible_clones;
2299
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002300 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002301}
2302
2303/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002304 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002305 * @dev: DRM device
2306 * @data: ioctl data
2307 * @file_priv: DRM file info
2308 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002309 * Construct a list of plane ids to return to the user.
2310 *
2311 * Called by the user via ioctl.
2312 *
2313 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002314 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002315 */
2316int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002317 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002318{
2319 struct drm_mode_get_plane_res *plane_resp = data;
2320 struct drm_mode_config *config;
2321 struct drm_plane *plane;
2322 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002323 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002324 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325
2326 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2327 return -EINVAL;
2328
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002329 config = &dev->mode_config;
2330
Matt Roper681e7ec2014-04-01 15:22:42 -07002331 if (file_priv->universal_planes)
2332 num_planes = config->num_total_plane;
2333 else
2334 num_planes = config->num_overlay_plane;
2335
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002336 /*
2337 * This ioctl is called twice, once to determine how much space is
2338 * needed, and the 2nd time to fill it.
2339 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002340 if (num_planes &&
2341 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002342 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002343
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002344 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002345 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002346 /*
2347 * Unless userspace set the 'universal planes'
2348 * capability bit, only advertise overlays.
2349 */
2350 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2351 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002352 continue;
2353
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002354 if (put_user(plane->base.id, plane_ptr + copied))
2355 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002356 copied++;
2357 }
2358 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002359 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002360
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002361 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002362}
2363
2364/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002365 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002366 * @dev: DRM device
2367 * @data: ioctl data
2368 * @file_priv: DRM file info
2369 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002370 * Construct a plane configuration structure to return to the user.
2371 *
2372 * Called by the user via ioctl.
2373 *
2374 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002375 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002376 */
2377int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002378 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002379{
2380 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002381 struct drm_plane *plane;
2382 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002383
2384 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2385 return -EINVAL;
2386
Rob Clarka2b34e22013-10-05 16:36:52 -04002387 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002388 if (!plane)
2389 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002390
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002391 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002392 if (plane->crtc)
2393 plane_resp->crtc_id = plane->crtc->base.id;
2394 else
2395 plane_resp->crtc_id = 0;
2396
2397 if (plane->fb)
2398 plane_resp->fb_id = plane->fb->base.id;
2399 else
2400 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002401 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002402
2403 plane_resp->plane_id = plane->base.id;
2404 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002405 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002406
2407 /*
2408 * This ioctl is called twice, once to determine how much space is
2409 * needed, and the 2nd time to fill it.
2410 */
2411 if (plane->format_count &&
2412 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002413 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002414 if (copy_to_user(format_ptr,
2415 plane->format_types,
2416 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002417 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002418 }
2419 }
2420 plane_resp->count_format_types = plane->format_count;
2421
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002422 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002423}
2424
Laurent Pinchartead86102015-03-05 02:25:43 +02002425/**
2426 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2427 * @plane: plane to check for format support
2428 * @format: the pixel format
2429 *
2430 * Returns:
2431 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2432 * otherwise.
2433 */
2434int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2435{
2436 unsigned int i;
2437
2438 for (i = 0; i < plane->format_count; i++) {
2439 if (format == plane->format_types[i])
2440 return 0;
2441 }
2442
2443 return -EINVAL;
2444}
2445
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002446static int check_src_coords(uint32_t src_x, uint32_t src_y,
2447 uint32_t src_w, uint32_t src_h,
2448 const struct drm_framebuffer *fb)
2449{
2450 unsigned int fb_width, fb_height;
2451
2452 fb_width = fb->width << 16;
2453 fb_height = fb->height << 16;
2454
2455 /* Make sure source coordinates are inside the fb. */
2456 if (src_w > fb_width ||
2457 src_x > fb_width - src_w ||
2458 src_h > fb_height ||
2459 src_y > fb_height - src_h) {
2460 DRM_DEBUG_KMS("Invalid source coordinates "
2461 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2462 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2463 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2464 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2465 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2466 return -ENOSPC;
2467 }
2468
2469 return 0;
2470}
2471
Matt Roperb36552b2014-06-10 08:28:09 -07002472/*
2473 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002474 *
Matt Roperb36552b2014-06-10 08:28:09 -07002475 * Note that we assume an extra reference has already been taken on fb. If the
2476 * update fails, this reference will be dropped before return; if it succeeds,
2477 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002478 *
Matt Roperb36552b2014-06-10 08:28:09 -07002479 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002480 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002481static int __setplane_internal(struct drm_plane *plane,
2482 struct drm_crtc *crtc,
2483 struct drm_framebuffer *fb,
2484 int32_t crtc_x, int32_t crtc_y,
2485 uint32_t crtc_w, uint32_t crtc_h,
2486 /* src_{x,y,w,h} values are 16.16 fixed point */
2487 uint32_t src_x, uint32_t src_y,
2488 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002489{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002490 int ret = 0;
2491
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002492 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002493 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002494 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002495 ret = plane->funcs->disable_plane(plane);
2496 if (!ret) {
2497 plane->crtc = NULL;
2498 plane->fb = NULL;
2499 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002500 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002501 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002502 goto out;
2503 }
2504
Matt Roper7f994f32014-05-29 08:06:51 -07002505 /* Check whether this plane is usable on this CRTC */
2506 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2507 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2508 ret = -EINVAL;
2509 goto out;
2510 }
2511
Ville Syrjälä62443be2011-12-20 00:06:47 +02002512 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002513 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2514 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002515 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2516 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002517 goto out;
2518 }
2519
Matt Roper3968be92015-04-13 11:06:13 -07002520 /* Give drivers some help against integer overflows */
2521 if (crtc_w > INT_MAX ||
2522 crtc_x > INT_MAX - (int32_t) crtc_w ||
2523 crtc_h > INT_MAX ||
2524 crtc_y > INT_MAX - (int32_t) crtc_h) {
2525 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2526 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002527 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002528 goto out;
2529 }
2530
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002531 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2532 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002533 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002534
Daniel Vetter3d30a592014-07-27 13:42:42 +02002535 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002536 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002537 crtc_x, crtc_y, crtc_w, crtc_h,
2538 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002539 if (!ret) {
2540 plane->crtc = crtc;
2541 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002542 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002543 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002544 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002545 }
2546
2547out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002548 if (fb)
2549 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002550 if (plane->old_fb)
2551 drm_framebuffer_unreference(plane->old_fb);
2552 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002553
2554 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002555}
Matt Roperb36552b2014-06-10 08:28:09 -07002556
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002557static int setplane_internal(struct drm_plane *plane,
2558 struct drm_crtc *crtc,
2559 struct drm_framebuffer *fb,
2560 int32_t crtc_x, int32_t crtc_y,
2561 uint32_t crtc_w, uint32_t crtc_h,
2562 /* src_{x,y,w,h} values are 16.16 fixed point */
2563 uint32_t src_x, uint32_t src_y,
2564 uint32_t src_w, uint32_t src_h)
2565{
2566 int ret;
2567
2568 drm_modeset_lock_all(plane->dev);
2569 ret = __setplane_internal(plane, crtc, fb,
2570 crtc_x, crtc_y, crtc_w, crtc_h,
2571 src_x, src_y, src_w, src_h);
2572 drm_modeset_unlock_all(plane->dev);
2573
2574 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002575}
2576
2577/**
2578 * drm_mode_setplane - configure a plane's configuration
2579 * @dev: DRM device
2580 * @data: ioctl data*
2581 * @file_priv: DRM file info
2582 *
2583 * Set plane configuration, including placement, fb, scaling, and other factors.
2584 * Or pass a NULL fb to disable (planes may be disabled without providing a
2585 * valid crtc).
2586 *
2587 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002588 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002589 */
2590int drm_mode_setplane(struct drm_device *dev, void *data,
2591 struct drm_file *file_priv)
2592{
2593 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002594 struct drm_plane *plane;
2595 struct drm_crtc *crtc = NULL;
2596 struct drm_framebuffer *fb = NULL;
2597
2598 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2599 return -EINVAL;
2600
Matt Roperb36552b2014-06-10 08:28:09 -07002601 /*
2602 * First, find the plane, crtc, and fb objects. If not available,
2603 * we don't bother to call the driver.
2604 */
Rob Clark933f6222014-11-25 20:33:11 -05002605 plane = drm_plane_find(dev, plane_req->plane_id);
2606 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002607 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2608 plane_req->plane_id);
2609 return -ENOENT;
2610 }
Matt Roperb36552b2014-06-10 08:28:09 -07002611
2612 if (plane_req->fb_id) {
2613 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2614 if (!fb) {
2615 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2616 plane_req->fb_id);
2617 return -ENOENT;
2618 }
2619
Rob Clark933f6222014-11-25 20:33:11 -05002620 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2621 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002622 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2623 plane_req->crtc_id);
2624 return -ENOENT;
2625 }
Matt Roperb36552b2014-06-10 08:28:09 -07002626 }
2627
Matt Roper161d0dc2014-06-10 08:28:10 -07002628 /*
2629 * setplane_internal will take care of deref'ing either the old or new
2630 * framebuffer depending on success.
2631 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002632 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002633 plane_req->crtc_x, plane_req->crtc_y,
2634 plane_req->crtc_w, plane_req->crtc_h,
2635 plane_req->src_x, plane_req->src_y,
2636 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002637}
2638
2639/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002640 * drm_mode_set_config_internal - helper to call ->set_config
2641 * @set: modeset config to set
2642 *
2643 * This is a little helper to wrap internal calls to the ->set_config driver
2644 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002645 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002646 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002647 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002648 */
2649int drm_mode_set_config_internal(struct drm_mode_set *set)
2650{
2651 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002652 struct drm_framebuffer *fb;
2653 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002654 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002655
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002656 /*
2657 * NOTE: ->set_config can also disable other crtcs (if we steal all
2658 * connectors from it), hence we need to refcount the fbs across all
2659 * crtcs. Atomic modeset will have saner semantics ...
2660 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002661 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002662 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002663
Daniel Vetterb0d12322012-12-11 01:07:12 +01002664 fb = set->fb;
2665
2666 ret = crtc->funcs->set_config(set);
2667 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002668 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002669 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002670 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002671
Daniel Vettere4f62542015-07-09 23:44:35 +02002672 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002673 if (tmp->primary->fb)
2674 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002675 if (tmp->primary->old_fb)
2676 drm_framebuffer_unreference(tmp->primary->old_fb);
2677 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002678 }
2679
2680 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002681}
2682EXPORT_SYMBOL(drm_mode_set_config_internal);
2683
Matt Roperaf936292014-04-01 15:22:34 -07002684/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002685 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2686 * @mode: mode to query
2687 * @hdisplay: hdisplay value to fill in
2688 * @vdisplay: vdisplay value to fill in
2689 *
2690 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2691 * the appropriate layout.
2692 */
2693void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2694 int *hdisplay, int *vdisplay)
2695{
2696 struct drm_display_mode adjusted;
2697
2698 drm_mode_copy(&adjusted, mode);
2699 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2700 *hdisplay = adjusted.crtc_hdisplay;
2701 *vdisplay = adjusted.crtc_vdisplay;
2702}
2703EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2704
2705/**
Matt Roperaf936292014-04-01 15:22:34 -07002706 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2707 * CRTC viewport
2708 * @crtc: CRTC that framebuffer will be displayed on
2709 * @x: x panning
2710 * @y: y panning
2711 * @mode: mode that framebuffer will be displayed under
2712 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002713 */
Matt Roperaf936292014-04-01 15:22:34 -07002714int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2715 int x, int y,
2716 const struct drm_display_mode *mode,
2717 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002718
2719{
2720 int hdisplay, vdisplay;
2721
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002722 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002723
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002724 if (crtc->state &&
2725 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2726 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002727 swap(hdisplay, vdisplay);
2728
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002729 return check_src_coords(x << 16, y << 16,
2730 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002731}
Matt Roperaf936292014-04-01 15:22:34 -07002732EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002733
Daniel Vetter2d13b672012-12-11 13:47:23 +01002734/**
Dave Airlief453ba02008-11-07 14:05:41 -08002735 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002736 * @dev: drm device for the ioctl
2737 * @data: data pointer for the ioctl
2738 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002739 *
Dave Airlief453ba02008-11-07 14:05:41 -08002740 * Build a new CRTC configuration based on user request.
2741 *
2742 * Called by the user via ioctl.
2743 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002744 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002745 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002746 */
2747int drm_mode_setcrtc(struct drm_device *dev, void *data,
2748 struct drm_file *file_priv)
2749{
2750 struct drm_mode_config *config = &dev->mode_config;
2751 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002752 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002753 struct drm_connector **connector_set = NULL, *connector;
2754 struct drm_framebuffer *fb = NULL;
2755 struct drm_display_mode *mode = NULL;
2756 struct drm_mode_set set;
2757 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002758 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002759 int i;
2760
Dave Airliefb3b06c2011-02-08 13:55:21 +10002761 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2762 return -EINVAL;
2763
Zhao Junwang01447e92015-07-07 17:08:35 +08002764 /*
2765 * Universal plane src offsets are only 16.16, prevent havoc for
2766 * drivers using universal plane code internally.
2767 */
2768 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002769 return -ERANGE;
2770
Daniel Vetter84849902012-12-02 00:28:11 +01002771 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002772 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2773 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002774 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002775 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002776 goto out;
2777 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002778 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002779
2780 if (crtc_req->mode_valid) {
2781 /* If we have a mode we need a framebuffer. */
2782 /* If we pass -1, set the mode with the currently bound fb */
2783 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002784 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002785 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2786 ret = -EINVAL;
2787 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002788 }
Matt Roperf4510a22014-04-01 15:22:40 -07002789 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002790 /* Make refcounting symmetric with the lookup path. */
2791 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002792 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002793 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2794 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002795 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2796 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002797 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002798 goto out;
2799 }
Dave Airlief453ba02008-11-07 14:05:41 -08002800 }
2801
2802 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002803 if (!mode) {
2804 ret = -ENOMEM;
2805 goto out;
2806 }
2807
Daniel Stone934a8a82015-05-22 13:34:48 +01002808 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002809 if (ret) {
2810 DRM_DEBUG_KMS("Invalid mode\n");
2811 goto out;
2812 }
2813
Dave Airlief453ba02008-11-07 14:05:41 -08002814 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002815
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002816 /*
2817 * Check whether the primary plane supports the fb pixel format.
2818 * Drivers not implementing the universal planes API use a
2819 * default formats list provided by the DRM core which doesn't
2820 * match real hardware capabilities. Skip the check in that
2821 * case.
2822 */
2823 if (!crtc->primary->format_default) {
2824 ret = drm_plane_check_pixel_format(crtc->primary,
2825 fb->pixel_format);
2826 if (ret) {
2827 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2828 drm_get_format_name(fb->pixel_format));
2829 goto out;
2830 }
2831 }
2832
Damien Lespiauc11e9282013-09-25 16:45:30 +01002833 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2834 mode, fb);
2835 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002836 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002837
Dave Airlief453ba02008-11-07 14:05:41 -08002838 }
2839
2840 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002841 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002842 ret = -EINVAL;
2843 goto out;
2844 }
2845
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002846 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002847 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002848 crtc_req->count_connectors);
2849 ret = -EINVAL;
2850 goto out;
2851 }
2852
2853 if (crtc_req->count_connectors > 0) {
2854 u32 out_id;
2855
2856 /* Avoid unbounded kernel memory allocation */
2857 if (crtc_req->count_connectors > config->num_connector) {
2858 ret = -EINVAL;
2859 goto out;
2860 }
2861
Thierry Reding2f6c5382014-12-10 13:03:36 +01002862 connector_set = kmalloc_array(crtc_req->count_connectors,
2863 sizeof(struct drm_connector *),
2864 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002865 if (!connector_set) {
2866 ret = -ENOMEM;
2867 goto out;
2868 }
2869
2870 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002871 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002872 if (get_user(out_id, &set_connectors_ptr[i])) {
2873 ret = -EFAULT;
2874 goto out;
2875 }
2876
Rob Clarka2b34e22013-10-05 16:36:52 -04002877 connector = drm_connector_find(dev, out_id);
2878 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002879 DRM_DEBUG_KMS("Connector id %d unknown\n",
2880 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002881 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002882 goto out;
2883 }
Jerome Glisse94401062010-07-15 15:43:25 -04002884 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2885 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002886 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002887
2888 connector_set[i] = connector;
2889 }
2890 }
2891
2892 set.crtc = crtc;
2893 set.x = crtc_req->x;
2894 set.y = crtc_req->y;
2895 set.mode = mode;
2896 set.connectors = connector_set;
2897 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002898 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002899 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002900
2901out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002902 if (fb)
2903 drm_framebuffer_unreference(fb);
2904
Dave Airlief453ba02008-11-07 14:05:41 -08002905 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002906 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002907 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002908 return ret;
2909}
2910
Matt Roper161d0dc2014-06-10 08:28:10 -07002911/**
2912 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2913 * universal plane handler call
2914 * @crtc: crtc to update cursor for
2915 * @req: data pointer for the ioctl
2916 * @file_priv: drm file for the ioctl call
2917 *
2918 * Legacy cursor ioctl's work directly with driver buffer handles. To
2919 * translate legacy ioctl calls into universal plane handler calls, we need to
2920 * wrap the native buffer handle in a drm_framebuffer.
2921 *
2922 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2923 * buffer with a pitch of 4*width; the universal plane interface should be used
2924 * directly in cases where the hardware can support other buffer settings and
2925 * userspace wants to make use of these capabilities.
2926 *
2927 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002928 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002929 */
2930static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2931 struct drm_mode_cursor2 *req,
2932 struct drm_file *file_priv)
2933{
2934 struct drm_device *dev = crtc->dev;
2935 struct drm_framebuffer *fb = NULL;
2936 struct drm_mode_fb_cmd2 fbreq = {
2937 .width = req->width,
2938 .height = req->height,
2939 .pixel_format = DRM_FORMAT_ARGB8888,
2940 .pitches = { req->width * 4 },
2941 .handles = { req->handle },
2942 };
2943 int32_t crtc_x, crtc_y;
2944 uint32_t crtc_w = 0, crtc_h = 0;
2945 uint32_t src_w = 0, src_h = 0;
2946 int ret = 0;
2947
2948 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002949 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002950
2951 /*
2952 * Obtain fb we'll be using (either new or existing) and take an extra
2953 * reference to it if fb != null. setplane will take care of dropping
2954 * the reference if the plane update fails.
2955 */
2956 if (req->flags & DRM_MODE_CURSOR_BO) {
2957 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002958 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002959 if (IS_ERR(fb)) {
2960 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2961 return PTR_ERR(fb);
2962 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002963 } else {
2964 fb = NULL;
2965 }
2966 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002967 fb = crtc->cursor->fb;
2968 if (fb)
2969 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002970 }
2971
2972 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2973 crtc_x = req->x;
2974 crtc_y = req->y;
2975 } else {
2976 crtc_x = crtc->cursor_x;
2977 crtc_y = crtc->cursor_y;
2978 }
2979
2980 if (fb) {
2981 crtc_w = fb->width;
2982 crtc_h = fb->height;
2983 src_w = fb->width << 16;
2984 src_h = fb->height << 16;
2985 }
2986
2987 /*
2988 * setplane_internal will take care of deref'ing either the old or new
2989 * framebuffer depending on success.
2990 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002991 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002992 crtc_x, crtc_y, crtc_w, crtc_h,
2993 0, 0, src_w, src_h);
2994
2995 /* Update successful; save new cursor position, if necessary */
2996 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2997 crtc->cursor_x = req->x;
2998 crtc->cursor_y = req->y;
2999 }
3000
3001 return ret;
3002}
3003
Dave Airlie4c813d42013-06-20 11:48:52 +10003004static int drm_mode_cursor_common(struct drm_device *dev,
3005 struct drm_mode_cursor2 *req,
3006 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003007{
Dave Airlief453ba02008-11-07 14:05:41 -08003008 struct drm_crtc *crtc;
3009 int ret = 0;
3010
Dave Airliefb3b06c2011-02-08 13:55:21 +10003011 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3012 return -EINVAL;
3013
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003014 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003015 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003016
Rob Clarka2b34e22013-10-05 16:36:52 -04003017 crtc = drm_crtc_find(dev, req->crtc_id);
3018 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003019 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003020 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003021 }
Dave Airlief453ba02008-11-07 14:05:41 -08003022
Matt Roper161d0dc2014-06-10 08:28:10 -07003023 /*
3024 * If this crtc has a universal cursor plane, call that plane's update
3025 * handler rather than using legacy cursor handlers.
3026 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003027 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003028 if (crtc->cursor) {
3029 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3030 goto out;
3031 }
3032
Dave Airlief453ba02008-11-07 14:05:41 -08003033 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003034 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003035 ret = -ENXIO;
3036 goto out;
3037 }
3038 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003039 if (crtc->funcs->cursor_set2)
3040 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3041 req->width, req->height, req->hot_x, req->hot_y);
3042 else
3043 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3044 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003045 }
3046
3047 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3048 if (crtc->funcs->cursor_move) {
3049 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3050 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003051 ret = -EFAULT;
3052 goto out;
3053 }
3054 }
3055out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003056 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003057
Dave Airlief453ba02008-11-07 14:05:41 -08003058 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003059
3060}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003061
3062
3063/**
3064 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3065 * @dev: drm device for the ioctl
3066 * @data: data pointer for the ioctl
3067 * @file_priv: drm file for the ioctl call
3068 *
3069 * Set the cursor configuration based on user request.
3070 *
3071 * Called by the user via ioctl.
3072 *
3073 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003074 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003075 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003076int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003077 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003078{
3079 struct drm_mode_cursor *req = data;
3080 struct drm_mode_cursor2 new_req;
3081
3082 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3083 new_req.hot_x = new_req.hot_y = 0;
3084
3085 return drm_mode_cursor_common(dev, &new_req, file_priv);
3086}
3087
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003088/**
3089 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3090 * @dev: drm device for the ioctl
3091 * @data: data pointer for the ioctl
3092 * @file_priv: drm file for the ioctl call
3093 *
3094 * Set the cursor configuration based on user request. This implements the 2nd
3095 * version of the cursor ioctl, which allows userspace to additionally specify
3096 * the hotspot of the pointer.
3097 *
3098 * Called by the user via ioctl.
3099 *
3100 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003101 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003102 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003103int drm_mode_cursor2_ioctl(struct drm_device *dev,
3104 void *data, struct drm_file *file_priv)
3105{
3106 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003107
Dave Airlie4c813d42013-06-20 11:48:52 +10003108 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003109}
3110
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003111/**
3112 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3113 * @bpp: bits per pixels
3114 * @depth: bit depth per pixel
3115 *
3116 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3117 * Useful in fbdev emulation code, since that deals in those values.
3118 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003119uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3120{
3121 uint32_t fmt;
3122
3123 switch (bpp) {
3124 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003125 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003126 break;
3127 case 16:
3128 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003129 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003130 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003131 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003132 break;
3133 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003134 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003135 break;
3136 case 32:
3137 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003138 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003139 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003140 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003141 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003142 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003143 break;
3144 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003145 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3146 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003147 break;
3148 }
3149
3150 return fmt;
3151}
3152EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3153
Dave Airlief453ba02008-11-07 14:05:41 -08003154/**
3155 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003156 * @dev: drm device for the ioctl
3157 * @data: data pointer for the ioctl
3158 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003159 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003160 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003161 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003162 *
3163 * Called by the user via ioctl.
3164 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003165 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003166 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003167 */
3168int drm_mode_addfb(struct drm_device *dev,
3169 void *data, struct drm_file *file_priv)
3170{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003171 struct drm_mode_fb_cmd *or = data;
3172 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003173 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003174
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003175 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003176 r.fb_id = or->fb_id;
3177 r.width = or->width;
3178 r.height = or->height;
3179 r.pitches[0] = or->pitch;
3180 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3181 r.handles[0] = or->handle;
3182
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003183 ret = drm_mode_addfb2(dev, &r, file_priv);
3184 if (ret)
3185 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003186
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003187 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003188
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003189 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003190}
3191
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003192static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003193{
3194 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3195
3196 switch (format) {
3197 case DRM_FORMAT_C8:
3198 case DRM_FORMAT_RGB332:
3199 case DRM_FORMAT_BGR233:
3200 case DRM_FORMAT_XRGB4444:
3201 case DRM_FORMAT_XBGR4444:
3202 case DRM_FORMAT_RGBX4444:
3203 case DRM_FORMAT_BGRX4444:
3204 case DRM_FORMAT_ARGB4444:
3205 case DRM_FORMAT_ABGR4444:
3206 case DRM_FORMAT_RGBA4444:
3207 case DRM_FORMAT_BGRA4444:
3208 case DRM_FORMAT_XRGB1555:
3209 case DRM_FORMAT_XBGR1555:
3210 case DRM_FORMAT_RGBX5551:
3211 case DRM_FORMAT_BGRX5551:
3212 case DRM_FORMAT_ARGB1555:
3213 case DRM_FORMAT_ABGR1555:
3214 case DRM_FORMAT_RGBA5551:
3215 case DRM_FORMAT_BGRA5551:
3216 case DRM_FORMAT_RGB565:
3217 case DRM_FORMAT_BGR565:
3218 case DRM_FORMAT_RGB888:
3219 case DRM_FORMAT_BGR888:
3220 case DRM_FORMAT_XRGB8888:
3221 case DRM_FORMAT_XBGR8888:
3222 case DRM_FORMAT_RGBX8888:
3223 case DRM_FORMAT_BGRX8888:
3224 case DRM_FORMAT_ARGB8888:
3225 case DRM_FORMAT_ABGR8888:
3226 case DRM_FORMAT_RGBA8888:
3227 case DRM_FORMAT_BGRA8888:
3228 case DRM_FORMAT_XRGB2101010:
3229 case DRM_FORMAT_XBGR2101010:
3230 case DRM_FORMAT_RGBX1010102:
3231 case DRM_FORMAT_BGRX1010102:
3232 case DRM_FORMAT_ARGB2101010:
3233 case DRM_FORMAT_ABGR2101010:
3234 case DRM_FORMAT_RGBA1010102:
3235 case DRM_FORMAT_BGRA1010102:
3236 case DRM_FORMAT_YUYV:
3237 case DRM_FORMAT_YVYU:
3238 case DRM_FORMAT_UYVY:
3239 case DRM_FORMAT_VYUY:
3240 case DRM_FORMAT_AYUV:
3241 case DRM_FORMAT_NV12:
3242 case DRM_FORMAT_NV21:
3243 case DRM_FORMAT_NV16:
3244 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003245 case DRM_FORMAT_NV24:
3246 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003247 case DRM_FORMAT_YUV410:
3248 case DRM_FORMAT_YVU410:
3249 case DRM_FORMAT_YUV411:
3250 case DRM_FORMAT_YVU411:
3251 case DRM_FORMAT_YUV420:
3252 case DRM_FORMAT_YVU420:
3253 case DRM_FORMAT_YUV422:
3254 case DRM_FORMAT_YVU422:
3255 case DRM_FORMAT_YUV444:
3256 case DRM_FORMAT_YVU444:
3257 return 0;
3258 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003259 DRM_DEBUG_KMS("invalid pixel format %s\n",
3260 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003261 return -EINVAL;
3262 }
3263}
3264
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003265static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003266{
3267 int ret, hsub, vsub, num_planes, i;
3268
3269 ret = format_check(r);
3270 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003271 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3272 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003273 return ret;
3274 }
3275
3276 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3277 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3278 num_planes = drm_format_num_planes(r->pixel_format);
3279
3280 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003281 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003282 return -EINVAL;
3283 }
3284
3285 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003286 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003287 return -EINVAL;
3288 }
3289
3290 for (i = 0; i < num_planes; i++) {
3291 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003292 unsigned int height = r->height / (i != 0 ? vsub : 1);
3293 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003294
3295 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003296 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003297 return -EINVAL;
3298 }
3299
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003300 if ((uint64_t) width * cpp > UINT_MAX)
3301 return -ERANGE;
3302
3303 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3304 return -ERANGE;
3305
3306 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003307 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003308 return -EINVAL;
3309 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003310
3311 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3312 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3313 r->modifier[i], i);
3314 return -EINVAL;
3315 }
Rob Clark570655b2015-01-30 20:18:11 +05303316
3317 /* modifier specific checks: */
3318 switch (r->modifier[i]) {
3319 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3320 /* NOTE: the pitch restriction may be lifted later if it turns
3321 * out that no hw has this restriction:
3322 */
3323 if (r->pixel_format != DRM_FORMAT_NV12 ||
3324 width % 128 || height % 32 ||
3325 r->pitches[i] % 128) {
3326 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3327 return -EINVAL;
3328 }
3329 break;
3330
3331 default:
3332 break;
3333 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003334 }
3335
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003336 for (i = num_planes; i < 4; i++) {
3337 if (r->modifier[i]) {
3338 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3339 return -EINVAL;
3340 }
3341
3342 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3343 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3344 continue;
3345
3346 if (r->handles[i]) {
3347 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3348 return -EINVAL;
3349 }
3350
3351 if (r->pitches[i]) {
3352 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3353 return -EINVAL;
3354 }
3355
3356 if (r->offsets[i]) {
3357 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3358 return -EINVAL;
3359 }
3360 }
3361
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003362 return 0;
3363}
3364
Chris Wilson9a6f5132015-02-25 13:45:26 +00003365static struct drm_framebuffer *
3366internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003367 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003368 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003369{
3370 struct drm_mode_config *config = &dev->mode_config;
3371 struct drm_framebuffer *fb;
3372 int ret;
3373
Rob Clarke3eb3252015-02-05 14:41:52 +00003374 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003375 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3376 return ERR_PTR(-EINVAL);
3377 }
3378
3379 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3380 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3381 r->width, config->min_width, config->max_width);
3382 return ERR_PTR(-EINVAL);
3383 }
3384 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3385 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3386 r->height, config->min_height, config->max_height);
3387 return ERR_PTR(-EINVAL);
3388 }
3389
Rob Clarke3eb3252015-02-05 14:41:52 +00003390 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3391 !dev->mode_config.allow_fb_modifiers) {
3392 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3393 return ERR_PTR(-EINVAL);
3394 }
3395
Matt Roperc394c2b2014-06-10 08:28:08 -07003396 ret = framebuffer_check(r);
3397 if (ret)
3398 return ERR_PTR(ret);
3399
3400 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3401 if (IS_ERR(fb)) {
3402 DRM_DEBUG_KMS("could not create framebuffer\n");
3403 return fb;
3404 }
3405
Matt Roperc394c2b2014-06-10 08:28:08 -07003406 return fb;
3407}
3408
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003409/**
3410 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003411 * @dev: drm device for the ioctl
3412 * @data: data pointer for the ioctl
3413 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003414 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003415 * Add a new FB to the specified CRTC, given a user request with format. This is
3416 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3417 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003418 *
3419 * Called by the user via ioctl.
3420 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003421 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003422 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003423 */
3424int drm_mode_addfb2(struct drm_device *dev,
3425 void *data, struct drm_file *file_priv)
3426{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003427 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003428 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003429
Dave Airliefb3b06c2011-02-08 13:55:21 +10003430 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3431 return -EINVAL;
3432
Chris Wilson9a6f5132015-02-25 13:45:26 +00003433 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003434 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003435 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003436
Chris Wilson9a6f5132015-02-25 13:45:26 +00003437 /* Transfer ownership to the filp for reaping on close */
3438
3439 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3440 mutex_lock(&file_priv->fbs_lock);
3441 r->fb_id = fb->base.id;
3442 list_add(&fb->filp_head, &file_priv->fbs);
3443 mutex_unlock(&file_priv->fbs_lock);
3444
Matt Roperc394c2b2014-06-10 08:28:08 -07003445 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003446}
3447
3448/**
3449 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003450 * @dev: drm device for the ioctl
3451 * @data: data pointer for the ioctl
3452 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003453 *
Dave Airlief453ba02008-11-07 14:05:41 -08003454 * Remove the FB specified by the user.
3455 *
3456 * Called by the user via ioctl.
3457 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003458 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003459 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003460 */
3461int drm_mode_rmfb(struct drm_device *dev,
3462 void *data, struct drm_file *file_priv)
3463{
Dave Airlief453ba02008-11-07 14:05:41 -08003464 struct drm_framebuffer *fb = NULL;
3465 struct drm_framebuffer *fbl = NULL;
3466 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003467 int found = 0;
3468
Dave Airliefb3b06c2011-02-08 13:55:21 +10003469 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3470 return -EINVAL;
3471
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003472 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003473 mutex_lock(&dev->mode_config.fb_lock);
3474 fb = __drm_framebuffer_lookup(dev, *id);
3475 if (!fb)
3476 goto fail_lookup;
3477
Dave Airlief453ba02008-11-07 14:05:41 -08003478 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3479 if (fb == fbl)
3480 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003481 if (!found)
3482 goto fail_lookup;
3483
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003484 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003485 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003486 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003487
Maarten Lankhorst13803132015-09-09 16:40:56 +02003488 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003489
Daniel Vetter2b677e82012-12-10 21:16:05 +01003490 return 0;
3491
3492fail_lookup:
3493 mutex_unlock(&dev->mode_config.fb_lock);
3494 mutex_unlock(&file_priv->fbs_lock);
3495
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003496 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003497}
3498
3499/**
3500 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003501 * @dev: drm device for the ioctl
3502 * @data: data pointer for the ioctl
3503 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003504 *
Dave Airlief453ba02008-11-07 14:05:41 -08003505 * Lookup the FB given its ID and return info about it.
3506 *
3507 * Called by the user via ioctl.
3508 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003509 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003510 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003511 */
3512int drm_mode_getfb(struct drm_device *dev,
3513 void *data, struct drm_file *file_priv)
3514{
3515 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003516 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003517 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003518
Dave Airliefb3b06c2011-02-08 13:55:21 +10003519 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3520 return -EINVAL;
3521
Daniel Vetter786b99e2012-12-02 21:53:40 +01003522 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003523 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003524 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003525
3526 r->height = fb->height;
3527 r->width = fb->width;
3528 r->depth = fb->depth;
3529 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003530 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003531 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003532 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003533 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003534 ret = fb->funcs->create_handle(fb, file_priv,
3535 &r->handle);
3536 } else {
3537 /* GET_FB() is an unprivileged ioctl so we must not
3538 * return a buffer-handle to non-master processes! For
3539 * backwards-compatibility reasons, we cannot make
3540 * GET_FB() privileged, so just return an invalid handle
3541 * for non-masters. */
3542 r->handle = 0;
3543 ret = 0;
3544 }
3545 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003546 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003547 }
Dave Airlief453ba02008-11-07 14:05:41 -08003548
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003549 drm_framebuffer_unreference(fb);
3550
Dave Airlief453ba02008-11-07 14:05:41 -08003551 return ret;
3552}
3553
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003554/**
3555 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3556 * @dev: drm device for the ioctl
3557 * @data: data pointer for the ioctl
3558 * @file_priv: drm file for the ioctl call
3559 *
3560 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3561 * rectangle list. Generic userspace which does frontbuffer rendering must call
3562 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3563 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3564 *
3565 * Modesetting drivers which always update the frontbuffer do not need to
3566 * implement the corresponding ->dirty framebuffer callback.
3567 *
3568 * Called by the user via ioctl.
3569 *
3570 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003571 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003572 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003573int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3574 void *data, struct drm_file *file_priv)
3575{
3576 struct drm_clip_rect __user *clips_ptr;
3577 struct drm_clip_rect *clips = NULL;
3578 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003579 struct drm_framebuffer *fb;
3580 unsigned flags;
3581 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003582 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003583
Dave Airliefb3b06c2011-02-08 13:55:21 +10003584 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3585 return -EINVAL;
3586
Daniel Vetter786b99e2012-12-02 21:53:40 +01003587 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003588 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003589 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003590
3591 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003592 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003593
3594 if (!num_clips != !clips_ptr) {
3595 ret = -EINVAL;
3596 goto out_err1;
3597 }
3598
3599 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3600
3601 /* If userspace annotates copy, clips must come in pairs */
3602 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3603 ret = -EINVAL;
3604 goto out_err1;
3605 }
3606
3607 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003608 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3609 ret = -EINVAL;
3610 goto out_err1;
3611 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003612 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003613 if (!clips) {
3614 ret = -ENOMEM;
3615 goto out_err1;
3616 }
3617
3618 ret = copy_from_user(clips, clips_ptr,
3619 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003620 if (ret) {
3621 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003622 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003623 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003624 }
3625
3626 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003627 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3628 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003629 } else {
3630 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003631 }
3632
3633out_err2:
3634 kfree(clips);
3635out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003636 drm_framebuffer_unreference(fb);
3637
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003638 return ret;
3639}
3640
3641
Dave Airlief453ba02008-11-07 14:05:41 -08003642/**
3643 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003644 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003645 *
Dave Airlief453ba02008-11-07 14:05:41 -08003646 * Destroy all the FBs associated with @filp.
3647 *
3648 * Called by the user via ioctl.
3649 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003650 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003651 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003652 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003653void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003654{
Dave Airlief453ba02008-11-07 14:05:41 -08003655 struct drm_framebuffer *fb, *tfb;
3656
Daniel Vetter1b116292014-09-24 21:51:06 +02003657 /*
3658 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003659 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003660 * avoid upsetting lockdep since the universal cursor code adds a
3661 * framebuffer while holding mutex locks.
3662 *
3663 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3664 * locks is impossible here since no one else but this function can get
3665 * at it any more.
3666 */
Dave Airlief453ba02008-11-07 14:05:41 -08003667 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003668 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003669
Maarten Lankhorst73f75702015-09-09 16:40:57 +02003670 /* This drops the fpriv->fbs reference. */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003671 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003672 }
Dave Airlief453ba02008-11-07 14:05:41 -08003673}
3674
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003675/**
3676 * drm_property_create - create a new property type
3677 * @dev: drm device
3678 * @flags: flags specifying the property type
3679 * @name: name of the property
3680 * @num_values: number of pre-defined values
3681 *
3682 * This creates a new generic drm property which can then be attached to a drm
3683 * object with drm_object_attach_property. The returned property object must be
3684 * freed with drm_property_destroy.
3685 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003686 * Note that the DRM core keeps a per-device list of properties and that, if
3687 * drm_mode_config_cleanup() is called, it will destroy all properties created
3688 * by the driver.
3689 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003690 * Returns:
3691 * A pointer to the newly created property on success, NULL on failure.
3692 */
Dave Airlief453ba02008-11-07 14:05:41 -08003693struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3694 const char *name, int num_values)
3695{
3696 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003697 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003698
3699 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3700 if (!property)
3701 return NULL;
3702
Rob Clark98f75de2014-05-30 11:37:03 -04003703 property->dev = dev;
3704
Dave Airlief453ba02008-11-07 14:05:41 -08003705 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003706 property->values = kcalloc(num_values, sizeof(uint64_t),
3707 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003708 if (!property->values)
3709 goto fail;
3710 }
3711
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003712 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3713 if (ret)
3714 goto fail;
3715
Dave Airlief453ba02008-11-07 14:05:41 -08003716 property->flags = flags;
3717 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003718 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003719
Vinson Lee471dd2e2011-11-10 11:55:40 -08003720 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003721 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003722 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3723 }
Dave Airlief453ba02008-11-07 14:05:41 -08003724
3725 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003726
3727 WARN_ON(!drm_property_type_valid(property));
3728
Dave Airlief453ba02008-11-07 14:05:41 -08003729 return property;
3730fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003731 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003732 kfree(property);
3733 return NULL;
3734}
3735EXPORT_SYMBOL(drm_property_create);
3736
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003737/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003738 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003739 * @dev: drm device
3740 * @flags: flags specifying the property type
3741 * @name: name of the property
3742 * @props: enumeration lists with property values
3743 * @num_values: number of pre-defined values
3744 *
3745 * This creates a new generic drm property which can then be attached to a drm
3746 * object with drm_object_attach_property. The returned property object must be
3747 * freed with drm_property_destroy.
3748 *
3749 * Userspace is only allowed to set one of the predefined values for enumeration
3750 * properties.
3751 *
3752 * Returns:
3753 * A pointer to the newly created property on success, NULL on failure.
3754 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003755struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3756 const char *name,
3757 const struct drm_prop_enum_list *props,
3758 int num_values)
3759{
3760 struct drm_property *property;
3761 int i, ret;
3762
3763 flags |= DRM_MODE_PROP_ENUM;
3764
3765 property = drm_property_create(dev, flags, name, num_values);
3766 if (!property)
3767 return NULL;
3768
3769 for (i = 0; i < num_values; i++) {
3770 ret = drm_property_add_enum(property, i,
3771 props[i].type,
3772 props[i].name);
3773 if (ret) {
3774 drm_property_destroy(dev, property);
3775 return NULL;
3776 }
3777 }
3778
3779 return property;
3780}
3781EXPORT_SYMBOL(drm_property_create_enum);
3782
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003783/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003784 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003785 * @dev: drm device
3786 * @flags: flags specifying the property type
3787 * @name: name of the property
3788 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003789 * @num_props: size of the @props array
3790 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003791 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003792 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003793 * object with drm_object_attach_property. The returned property object must be
3794 * freed with drm_property_destroy.
3795 *
3796 * Compared to plain enumeration properties userspace is allowed to set any
3797 * or'ed together combination of the predefined property bitflag values
3798 *
3799 * Returns:
3800 * A pointer to the newly created property on success, NULL on failure.
3801 */
Rob Clark49e27542012-05-17 02:23:26 -06003802struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3803 int flags, const char *name,
3804 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303805 int num_props,
3806 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003807{
3808 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303809 int i, ret, index = 0;
3810 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003811
3812 flags |= DRM_MODE_PROP_BITMASK;
3813
3814 property = drm_property_create(dev, flags, name, num_values);
3815 if (!property)
3816 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303817 for (i = 0; i < num_props; i++) {
3818 if (!(supported_bits & (1ULL << props[i].type)))
3819 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003820
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303821 if (WARN_ON(index >= num_values)) {
3822 drm_property_destroy(dev, property);
3823 return NULL;
3824 }
3825
3826 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003827 props[i].type,
3828 props[i].name);
3829 if (ret) {
3830 drm_property_destroy(dev, property);
3831 return NULL;
3832 }
3833 }
3834
3835 return property;
3836}
3837EXPORT_SYMBOL(drm_property_create_bitmask);
3838
Rob Clarkebc44cf2012-09-12 22:22:31 -05003839static struct drm_property *property_create_range(struct drm_device *dev,
3840 int flags, const char *name,
3841 uint64_t min, uint64_t max)
3842{
3843 struct drm_property *property;
3844
3845 property = drm_property_create(dev, flags, name, 2);
3846 if (!property)
3847 return NULL;
3848
3849 property->values[0] = min;
3850 property->values[1] = max;
3851
3852 return property;
3853}
3854
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003855/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003856 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003857 * @dev: drm device
3858 * @flags: flags specifying the property type
3859 * @name: name of the property
3860 * @min: minimum value of the property
3861 * @max: maximum value of the property
3862 *
3863 * This creates a new generic drm property which can then be attached to a drm
3864 * object with drm_object_attach_property. The returned property object must be
3865 * freed with drm_property_destroy.
3866 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003867 * Userspace is allowed to set any unsigned integer value in the (min, max)
3868 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003869 *
3870 * Returns:
3871 * A pointer to the newly created property on success, NULL on failure.
3872 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003873struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3874 const char *name,
3875 uint64_t min, uint64_t max)
3876{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003877 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3878 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003879}
3880EXPORT_SYMBOL(drm_property_create_range);
3881
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003882/**
3883 * drm_property_create_signed_range - create a new signed ranged property type
3884 * @dev: drm device
3885 * @flags: flags specifying the property type
3886 * @name: name of the property
3887 * @min: minimum value of the property
3888 * @max: maximum value of the property
3889 *
3890 * This creates a new generic drm property which can then be attached to a drm
3891 * object with drm_object_attach_property. The returned property object must be
3892 * freed with drm_property_destroy.
3893 *
3894 * Userspace is allowed to set any signed integer value in the (min, max)
3895 * range inclusive.
3896 *
3897 * Returns:
3898 * A pointer to the newly created property on success, NULL on failure.
3899 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003900struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3901 int flags, const char *name,
3902 int64_t min, int64_t max)
3903{
3904 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3905 name, I642U64(min), I642U64(max));
3906}
3907EXPORT_SYMBOL(drm_property_create_signed_range);
3908
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003909/**
3910 * drm_property_create_object - create a new object property type
3911 * @dev: drm device
3912 * @flags: flags specifying the property type
3913 * @name: name of the property
3914 * @type: object type from DRM_MODE_OBJECT_* defines
3915 *
3916 * This creates a new generic drm property which can then be attached to a drm
3917 * object with drm_object_attach_property. The returned property object must be
3918 * freed with drm_property_destroy.
3919 *
3920 * Userspace is only allowed to set this to any property value of the given
3921 * @type. Only useful for atomic properties, which is enforced.
3922 *
3923 * Returns:
3924 * A pointer to the newly created property on success, NULL on failure.
3925 */
Rob Clark98f75de2014-05-30 11:37:03 -04003926struct drm_property *drm_property_create_object(struct drm_device *dev,
3927 int flags, const char *name, uint32_t type)
3928{
3929 struct drm_property *property;
3930
3931 flags |= DRM_MODE_PROP_OBJECT;
3932
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003933 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3934 return NULL;
3935
Rob Clark98f75de2014-05-30 11:37:03 -04003936 property = drm_property_create(dev, flags, name, 1);
3937 if (!property)
3938 return NULL;
3939
3940 property->values[0] = type;
3941
3942 return property;
3943}
3944EXPORT_SYMBOL(drm_property_create_object);
3945
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003946/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003947 * drm_property_create_bool - create a new boolean property type
3948 * @dev: drm device
3949 * @flags: flags specifying the property type
3950 * @name: name of the property
3951 *
3952 * This creates a new generic drm property which can then be attached to a drm
3953 * object with drm_object_attach_property. The returned property object must be
3954 * freed with drm_property_destroy.
3955 *
3956 * This is implemented as a ranged property with only {0, 1} as valid values.
3957 *
3958 * Returns:
3959 * A pointer to the newly created property on success, NULL on failure.
3960 */
3961struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3962 const char *name)
3963{
3964 return drm_property_create_range(dev, flags, name, 0, 1);
3965}
3966EXPORT_SYMBOL(drm_property_create_bool);
3967
3968/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003969 * drm_property_add_enum - add a possible value to an enumeration property
3970 * @property: enumeration property to change
3971 * @index: index of the new enumeration
3972 * @value: value of the new enumeration
3973 * @name: symbolic name of the new enumeration
3974 *
3975 * This functions adds enumerations to a property.
3976 *
3977 * It's use is deprecated, drivers should use one of the more specific helpers
3978 * to directly create the property with all enumerations already attached.
3979 *
3980 * Returns:
3981 * Zero on success, error code on failure.
3982 */
Dave Airlief453ba02008-11-07 14:05:41 -08003983int drm_property_add_enum(struct drm_property *property, int index,
3984 uint64_t value, const char *name)
3985{
3986 struct drm_property_enum *prop_enum;
3987
Rob Clark5ea22f22014-05-30 11:34:01 -04003988 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3989 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003990 return -EINVAL;
3991
3992 /*
3993 * Bitmask enum properties have the additional constraint of values
3994 * from 0 to 63
3995 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003996 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3997 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003998 return -EINVAL;
3999
Daniel Vetter3758b342014-11-19 18:38:10 +01004000 if (!list_empty(&property->enum_list)) {
4001 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004002 if (prop_enum->value == value) {
4003 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4004 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4005 return 0;
4006 }
4007 }
4008 }
4009
4010 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4011 if (!prop_enum)
4012 return -ENOMEM;
4013
4014 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4015 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4016 prop_enum->value = value;
4017
4018 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004019 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004020 return 0;
4021}
4022EXPORT_SYMBOL(drm_property_add_enum);
4023
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004024/**
4025 * drm_property_destroy - destroy a drm property
4026 * @dev: drm device
4027 * @property: property to destry
4028 *
4029 * This function frees a property including any attached resources like
4030 * enumeration values.
4031 */
Dave Airlief453ba02008-11-07 14:05:41 -08004032void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4033{
4034 struct drm_property_enum *prop_enum, *pt;
4035
Daniel Vetter3758b342014-11-19 18:38:10 +01004036 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004037 list_del(&prop_enum->head);
4038 kfree(prop_enum);
4039 }
4040
4041 if (property->num_values)
4042 kfree(property->values);
4043 drm_mode_object_put(dev, &property->base);
4044 list_del(&property->head);
4045 kfree(property);
4046}
4047EXPORT_SYMBOL(drm_property_destroy);
4048
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004049/**
4050 * drm_object_attach_property - attach a property to a modeset object
4051 * @obj: drm modeset object
4052 * @property: property to attach
4053 * @init_val: initial value of the property
4054 *
4055 * This attaches the given property to the modeset object with the given initial
4056 * value. Currently this function cannot fail since the properties are stored in
4057 * a statically sized array.
4058 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004059void drm_object_attach_property(struct drm_mode_object *obj,
4060 struct drm_property *property,
4061 uint64_t init_val)
4062{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004063 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004064
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004065 if (count == DRM_OBJECT_MAX_PROPERTY) {
4066 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4067 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4068 "you see this message on the same object type.\n",
4069 obj->type);
4070 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004071 }
4072
Rob Clarkb17cd752014-12-16 18:05:30 -05004073 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004074 obj->properties->values[count] = init_val;
4075 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004076 if (property->flags & DRM_MODE_PROP_ATOMIC)
4077 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004078}
4079EXPORT_SYMBOL(drm_object_attach_property);
4080
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004081/**
4082 * drm_object_property_set_value - set the value of a property
4083 * @obj: drm mode object to set property value for
4084 * @property: property to set
4085 * @val: value the property should be set to
4086 *
4087 * This functions sets a given property on a given object. This function only
4088 * changes the software state of the property, it does not call into the
4089 * driver's ->set_property callback.
4090 *
4091 * Returns:
4092 * Zero on success, error code on failure.
4093 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004094int drm_object_property_set_value(struct drm_mode_object *obj,
4095 struct drm_property *property, uint64_t val)
4096{
4097 int i;
4098
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004099 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004100 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004101 obj->properties->values[i] = val;
4102 return 0;
4103 }
4104 }
4105
4106 return -EINVAL;
4107}
4108EXPORT_SYMBOL(drm_object_property_set_value);
4109
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004110/**
4111 * drm_object_property_get_value - retrieve the value of a property
4112 * @obj: drm mode object to get property value from
4113 * @property: property to retrieve
4114 * @val: storage for the property value
4115 *
4116 * This function retrieves the softare state of the given property for the given
4117 * property. Since there is no driver callback to retrieve the current property
4118 * value this might be out of sync with the hardware, depending upon the driver
4119 * and property.
4120 *
4121 * Returns:
4122 * Zero on success, error code on failure.
4123 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004124int drm_object_property_get_value(struct drm_mode_object *obj,
4125 struct drm_property *property, uint64_t *val)
4126{
4127 int i;
4128
Rob Clark88a48e22014-12-18 16:01:50 -05004129 /* read-only properties bypass atomic mechanism and still store
4130 * their value in obj->properties->values[].. mostly to avoid
4131 * having to deal w/ EDID and similar props in atomic paths:
4132 */
4133 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4134 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4135 return drm_atomic_get_property(obj, property, val);
4136
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004137 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004138 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004139 *val = obj->properties->values[i];
4140 return 0;
4141 }
4142 }
4143
4144 return -EINVAL;
4145}
4146EXPORT_SYMBOL(drm_object_property_get_value);
4147
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004148/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004149 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004150 * @dev: DRM device
4151 * @data: ioctl data
4152 * @file_priv: DRM file info
4153 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004154 * This function retrieves the metadata for a given property, like the different
4155 * possible values for an enum property or the limits for a range property.
4156 *
4157 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004158 *
4159 * Called by the user via ioctl.
4160 *
4161 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004162 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004163 */
Dave Airlief453ba02008-11-07 14:05:41 -08004164int drm_mode_getproperty_ioctl(struct drm_device *dev,
4165 void *data, struct drm_file *file_priv)
4166{
Dave Airlief453ba02008-11-07 14:05:41 -08004167 struct drm_mode_get_property *out_resp = data;
4168 struct drm_property *property;
4169 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004170 int value_count = 0;
4171 int ret = 0, i;
4172 int copied;
4173 struct drm_property_enum *prop_enum;
4174 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004175 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004176
Dave Airliefb3b06c2011-02-08 13:55:21 +10004177 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4178 return -EINVAL;
4179
Daniel Vetter84849902012-12-02 00:28:11 +01004180 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004181 property = drm_property_find(dev, out_resp->prop_id);
4182 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004183 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004184 goto done;
4185 }
Dave Airlief453ba02008-11-07 14:05:41 -08004186
Rob Clark5ea22f22014-05-30 11:34:01 -04004187 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4188 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004189 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004190 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004191 }
4192
4193 value_count = property->num_values;
4194
4195 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4196 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4197 out_resp->flags = property->flags;
4198
4199 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004200 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004201 for (i = 0; i < value_count; i++) {
4202 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4203 ret = -EFAULT;
4204 goto done;
4205 }
4206 }
4207 }
4208 out_resp->count_values = value_count;
4209
Rob Clark5ea22f22014-05-30 11:34:01 -04004210 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4211 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004212 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4213 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004214 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004215 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004216
4217 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4218 ret = -EFAULT;
4219 goto done;
4220 }
4221
4222 if (copy_to_user(&enum_ptr[copied].name,
4223 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4224 ret = -EFAULT;
4225 goto done;
4226 }
4227 copied++;
4228 }
4229 }
4230 out_resp->count_enum_blobs = enum_count;
4231 }
4232
Daniel Vetter3758b342014-11-19 18:38:10 +01004233 /*
4234 * NOTE: The idea seems to have been to use this to read all the blob
4235 * property values. But nothing ever added them to the corresponding
4236 * list, userspace always used the special-purpose get_blob ioctl to
4237 * read the value for a blob property. It also doesn't make a lot of
4238 * sense to return values here when everything else is just metadata for
4239 * the property itself.
4240 */
4241 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4242 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004243done:
Daniel Vetter84849902012-12-02 00:28:11 +01004244 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004245 return ret;
4246}
4247
Daniel Stone99531d92015-05-22 13:34:49 +01004248/**
4249 * drm_property_create_blob - Create new blob property
4250 *
4251 * Creates a new blob property for a specified DRM device, optionally
4252 * copying data.
4253 *
4254 * @dev: DRM device to create property for
4255 * @length: Length to allocate for blob data
4256 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004257 *
4258 * Returns:
4259 * New blob property with a single reference on success, or an ERR_PTR
4260 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004261 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004262struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004263drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004264 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004265{
4266 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004267 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004268
Dan Carpenter9ac09342015-10-29 16:37:54 +03004269 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004270 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004271
4272 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4273 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004274 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004275
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004276 /* This must be explicitly initialised, so we can safely call list_del
4277 * on it in the removal handler, even if it isn't in a file list. */
4278 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004279 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004280 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004281
Daniel Stone99531d92015-05-22 13:34:49 +01004282 if (data)
4283 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004284
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004285 mutex_lock(&dev->mode_config.blob_lock);
4286
4287 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4288 if (ret) {
4289 kfree(blob);
4290 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004291 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004292 }
4293
Daniel Stone6bcacf52015-04-20 19:22:55 +01004294 kref_init(&blob->refcount);
4295
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004296 list_add_tail(&blob->head_global,
4297 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004298
4299 mutex_unlock(&dev->mode_config.blob_lock);
4300
Dave Airlief453ba02008-11-07 14:05:41 -08004301 return blob;
4302}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004303EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004304
Daniel Stone6bcacf52015-04-20 19:22:55 +01004305/**
4306 * drm_property_free_blob - Blob property destructor
4307 *
4308 * Internal free function for blob properties; must not be used directly.
4309 *
Daniel Stonef102c162015-05-22 13:34:44 +01004310 * @kref: Reference
Daniel Stone6bcacf52015-04-20 19:22:55 +01004311 */
4312static void drm_property_free_blob(struct kref *kref)
Dave Airlief453ba02008-11-07 14:05:41 -08004313{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004314 struct drm_property_blob *blob =
4315 container_of(kref, struct drm_property_blob, refcount);
4316
4317 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4318
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004319 list_del(&blob->head_global);
4320 list_del(&blob->head_file);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004321 drm_mode_object_put(blob->dev, &blob->base);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004322
Dave Airlief453ba02008-11-07 14:05:41 -08004323 kfree(blob);
4324}
4325
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004326/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004327 * drm_property_unreference_blob - Unreference a blob property
4328 *
4329 * Drop a reference on a blob property. May free the object.
4330 *
Daniel Stonef102c162015-05-22 13:34:44 +01004331 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004332 */
4333void drm_property_unreference_blob(struct drm_property_blob *blob)
4334{
4335 struct drm_device *dev;
4336
4337 if (!blob)
4338 return;
4339
4340 dev = blob->dev;
4341
4342 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4343
4344 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4345 &dev->mode_config.blob_lock))
4346 mutex_unlock(&dev->mode_config.blob_lock);
4347 else
4348 might_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004349}
4350EXPORT_SYMBOL(drm_property_unreference_blob);
4351
4352/**
4353 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4354 *
4355 * Drop a reference on a blob property. May free the object. This must be
4356 * called with blob_lock held.
4357 *
Daniel Stonef102c162015-05-22 13:34:44 +01004358 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004359 */
4360static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4361{
4362 if (!blob)
4363 return;
4364
4365 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4366
4367 kref_put(&blob->refcount, drm_property_free_blob);
4368}
4369
4370/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004371 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4372 * @dev: DRM device
4373 * @file_priv: destroy all blobs owned by this file handle
4374 */
4375void drm_property_destroy_user_blobs(struct drm_device *dev,
4376 struct drm_file *file_priv)
4377{
4378 struct drm_property_blob *blob, *bt;
4379
4380 mutex_lock(&dev->mode_config.blob_lock);
4381
4382 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4383 list_del_init(&blob->head_file);
4384 drm_property_unreference_blob_locked(blob);
4385 }
4386
4387 mutex_unlock(&dev->mode_config.blob_lock);
4388}
4389
4390/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004391 * drm_property_reference_blob - Take a reference on an existing property
4392 *
4393 * Take a new reference on an existing blob property.
4394 *
Daniel Stonef102c162015-05-22 13:34:44 +01004395 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004396 */
4397struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4398{
4399 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4400 kref_get(&blob->refcount);
4401 return blob;
4402}
4403EXPORT_SYMBOL(drm_property_reference_blob);
4404
4405/*
4406 * Like drm_property_lookup_blob, but does not return an additional reference.
4407 * Must be called with blob_lock held.
4408 */
4409static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4410 uint32_t id)
4411{
4412 struct drm_mode_object *obj = NULL;
4413 struct drm_property_blob *blob;
4414
4415 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4416
4417 mutex_lock(&dev->mode_config.idr_mutex);
4418 obj = idr_find(&dev->mode_config.crtc_idr, id);
4419 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4420 blob = NULL;
4421 else
4422 blob = obj_to_blob(obj);
4423 mutex_unlock(&dev->mode_config.idr_mutex);
4424
Dave Airlief453ba02008-11-07 14:05:41 -08004425 return blob;
4426}
4427
Daniel Stone6bcacf52015-04-20 19:22:55 +01004428/**
4429 * drm_property_lookup_blob - look up a blob property and take a reference
4430 * @dev: drm device
4431 * @id: id of the blob property
4432 *
4433 * If successful, this takes an additional reference to the blob property.
4434 * callers need to make sure to eventually unreference the returned property
4435 * again, using @drm_property_unreference_blob.
4436 */
4437struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4438 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004439{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004440 struct drm_property_blob *blob;
4441
4442 mutex_lock(&dev->mode_config.blob_lock);
4443 blob = __drm_property_lookup_blob(dev, id);
4444 if (blob) {
4445 if (!kref_get_unless_zero(&blob->refcount))
4446 blob = NULL;
4447 }
4448 mutex_unlock(&dev->mode_config.blob_lock);
4449
4450 return blob;
4451}
4452EXPORT_SYMBOL(drm_property_lookup_blob);
4453
4454/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004455 * drm_property_replace_global_blob - atomically replace existing blob property
4456 * @dev: drm device
4457 * @replace: location of blob property pointer to be replaced
4458 * @length: length of data for new blob, or 0 for no data
4459 * @data: content for new blob, or NULL for no data
4460 * @obj_holds_id: optional object for property holding blob ID
4461 * @prop_holds_id: optional property holding blob ID
4462 * @return 0 on success or error on failure
4463 *
4464 * This function will atomically replace a global property in the blob list,
4465 * optionally updating a property which holds the ID of that property. It is
4466 * guaranteed to be atomic: no caller will be allowed to see intermediate
4467 * results, and either the entire operation will succeed and clean up the
4468 * previous property, or it will fail and the state will be unchanged.
4469 *
4470 * If length is 0 or data is NULL, no new blob will be created, and the holding
4471 * property, if specified, will be set to 0.
4472 *
4473 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4474 * by holding the relevant modesetting object lock for its parent.
4475 *
4476 * For example, a drm_connector has a 'PATH' property, which contains the ID
4477 * of a blob property with the value of the MST path information. Calling this
4478 * function with replace pointing to the connector's path_blob_ptr, length and
4479 * data set for the new path information, obj_holds_id set to the connector's
4480 * base object, and prop_holds_id set to the path property name, will perform
4481 * a completely atomic update. The access to path_blob_ptr is protected by the
4482 * caller holding a lock on the connector.
4483 */
4484static int drm_property_replace_global_blob(struct drm_device *dev,
4485 struct drm_property_blob **replace,
4486 size_t length,
4487 const void *data,
4488 struct drm_mode_object *obj_holds_id,
4489 struct drm_property *prop_holds_id)
4490{
4491 struct drm_property_blob *new_blob = NULL;
4492 struct drm_property_blob *old_blob = NULL;
4493 int ret;
4494
4495 WARN_ON(replace == NULL);
4496
4497 old_blob = *replace;
4498
4499 if (length && data) {
4500 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004501 if (IS_ERR(new_blob))
4502 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004503 }
4504
4505 /* This does not need to be synchronised with blob_lock, as the
4506 * get_properties ioctl locks all modesetting objects, and
4507 * obj_holds_id must be locked before calling here, so we cannot
4508 * have its value out of sync with the list membership modified
4509 * below under blob_lock. */
4510 if (obj_holds_id) {
4511 ret = drm_object_property_set_value(obj_holds_id,
4512 prop_holds_id,
4513 new_blob ?
4514 new_blob->base.id : 0);
4515 if (ret != 0)
4516 goto err_created;
4517 }
4518
Markus Elfringec530822015-07-05 21:55:10 +02004519 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004520 *replace = new_blob;
4521
4522 return 0;
4523
4524err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004525 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004526 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004527}
4528
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004529/**
4530 * drm_mode_getblob_ioctl - get the contents of a blob property value
4531 * @dev: DRM device
4532 * @data: ioctl data
4533 * @file_priv: DRM file info
4534 *
4535 * This function retrieves the contents of a blob property. The value stored in
4536 * an object's blob property is just a normal modeset object id.
4537 *
4538 * Called by the user via ioctl.
4539 *
4540 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004541 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004542 */
Dave Airlief453ba02008-11-07 14:05:41 -08004543int drm_mode_getblob_ioctl(struct drm_device *dev,
4544 void *data, struct drm_file *file_priv)
4545{
Dave Airlief453ba02008-11-07 14:05:41 -08004546 struct drm_mode_get_blob *out_resp = data;
4547 struct drm_property_blob *blob;
4548 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004549 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004550
Dave Airliefb3b06c2011-02-08 13:55:21 +10004551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4552 return -EINVAL;
4553
Daniel Vetter84849902012-12-02 00:28:11 +01004554 drm_modeset_lock_all(dev);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004555 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004556 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04004557 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004558 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004559 goto done;
4560 }
Dave Airlief453ba02008-11-07 14:05:41 -08004561
4562 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004563 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004564 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004565 ret = -EFAULT;
4566 goto done;
4567 }
4568 }
4569 out_resp->length = blob->length;
4570
4571done:
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004572 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01004573 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004574 return ret;
4575}
4576
Dave Airliecc7096f2014-10-22 12:03:04 +10004577/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004578 * drm_mode_createblob_ioctl - create a new blob property
4579 * @dev: DRM device
4580 * @data: ioctl data
4581 * @file_priv: DRM file info
4582 *
4583 * This function creates a new blob property with user-defined values. In order
4584 * to give us sensible validation and checking when creating, rather than at
4585 * every potential use, we also require a type to be provided upfront.
4586 *
4587 * Called by the user via ioctl.
4588 *
4589 * Returns:
4590 * Zero on success, negative errno on failure.
4591 */
4592int drm_mode_createblob_ioctl(struct drm_device *dev,
4593 void *data, struct drm_file *file_priv)
4594{
4595 struct drm_mode_create_blob *out_resp = data;
4596 struct drm_property_blob *blob;
4597 void __user *blob_ptr;
4598 int ret = 0;
4599
4600 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4601 return -EINVAL;
4602
4603 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4604 if (IS_ERR(blob))
4605 return PTR_ERR(blob);
4606
4607 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4608 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4609 ret = -EFAULT;
4610 goto out_blob;
4611 }
4612
4613 /* Dropping the lock between create_blob and our access here is safe
4614 * as only the same file_priv can remove the blob; at this point, it is
4615 * not associated with any file_priv. */
4616 mutex_lock(&dev->mode_config.blob_lock);
4617 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004618 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004619 mutex_unlock(&dev->mode_config.blob_lock);
4620
4621 return 0;
4622
4623out_blob:
4624 drm_property_unreference_blob(blob);
4625 return ret;
4626}
4627
4628/**
4629 * drm_mode_destroyblob_ioctl - destroy a user blob property
4630 * @dev: DRM device
4631 * @data: ioctl data
4632 * @file_priv: DRM file info
4633 *
4634 * Destroy an existing user-defined blob property.
4635 *
4636 * Called by the user via ioctl.
4637 *
4638 * Returns:
4639 * Zero on success, negative errno on failure.
4640 */
4641int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4642 void *data, struct drm_file *file_priv)
4643{
4644 struct drm_mode_destroy_blob *out_resp = data;
4645 struct drm_property_blob *blob = NULL, *bt;
4646 bool found = false;
4647 int ret = 0;
4648
4649 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4650 return -EINVAL;
4651
4652 mutex_lock(&dev->mode_config.blob_lock);
4653 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4654 if (!blob) {
4655 ret = -ENOENT;
4656 goto err;
4657 }
4658
4659 /* Ensure the property was actually created by this user. */
4660 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4661 if (bt == blob) {
4662 found = true;
4663 break;
4664 }
4665 }
4666
4667 if (!found) {
4668 ret = -EPERM;
4669 goto err;
4670 }
4671
4672 /* We must drop head_file here, because we may not be the last
4673 * reference on the blob. */
4674 list_del_init(&blob->head_file);
4675 drm_property_unreference_blob_locked(blob);
4676 mutex_unlock(&dev->mode_config.blob_lock);
4677
4678 return 0;
4679
4680err:
4681 mutex_unlock(&dev->mode_config.blob_lock);
4682 return ret;
4683}
4684
4685/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004686 * drm_mode_connector_set_path_property - set tile property on connector
4687 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004688 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004689 *
4690 * This creates a property to expose to userspace to specify a
4691 * connector path. This is mainly used for DisplayPort MST where
4692 * connectors have a topology and we want to allow userspace to give
4693 * them more meaningful names.
4694 *
4695 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004696 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004697 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004698int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004699 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004700{
4701 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004702 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004703
Daniel Stoned2ed3432015-04-20 19:22:53 +01004704 ret = drm_property_replace_global_blob(dev,
4705 &connector->path_blob_ptr,
4706 strlen(path) + 1,
4707 path,
4708 &connector->base,
4709 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004710 return ret;
4711}
4712EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4713
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004714/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004715 * drm_mode_connector_set_tile_property - set tile property on connector
4716 * @connector: connector to set property on.
4717 *
4718 * This looks up the tile information for a connector, and creates a
4719 * property for userspace to parse if it exists. The property is of
4720 * the form of 8 integers using ':' as a separator.
4721 *
4722 * Returns:
4723 * Zero on success, errno on failure.
4724 */
4725int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4726{
4727 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004728 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004729 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004730
4731 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004732 ret = drm_property_replace_global_blob(dev,
4733 &connector->tile_blob_ptr,
4734 0,
4735 NULL,
4736 &connector->base,
4737 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004738 return ret;
4739 }
4740
4741 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4742 connector->tile_group->id, connector->tile_is_single_monitor,
4743 connector->num_h_tile, connector->num_v_tile,
4744 connector->tile_h_loc, connector->tile_v_loc,
4745 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004746
Daniel Stoned2ed3432015-04-20 19:22:53 +01004747 ret = drm_property_replace_global_blob(dev,
4748 &connector->tile_blob_ptr,
4749 strlen(tile) + 1,
4750 tile,
4751 &connector->base,
4752 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004753 return ret;
4754}
4755EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4756
4757/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004758 * drm_mode_connector_update_edid_property - update the edid property of a connector
4759 * @connector: drm connector
4760 * @edid: new value of the edid property
4761 *
4762 * This function creates a new blob modeset object and assigns its id to the
4763 * connector's edid property.
4764 *
4765 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004766 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004767 */
Dave Airlief453ba02008-11-07 14:05:41 -08004768int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004769 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004770{
4771 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004772 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004773 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004774
Thomas Wood4cf2b282014-06-18 17:52:33 +01004775 /* ignore requests to set edid when overridden */
4776 if (connector->override_edid)
4777 return 0;
4778
Daniel Stoned2ed3432015-04-20 19:22:53 +01004779 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004780 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004781
Daniel Stoned2ed3432015-04-20 19:22:53 +01004782 ret = drm_property_replace_global_blob(dev,
4783 &connector->edid_blob_ptr,
4784 size,
4785 edid,
4786 &connector->base,
4787 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004788 return ret;
4789}
4790EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4791
Rob Clark3843e712014-12-16 18:05:29 -05004792/* Some properties could refer to dynamic refcnt'd objects, or things that
4793 * need special locking to handle lifetime issues (ie. to ensure the prop
4794 * value doesn't become invalid part way through the property update due to
4795 * race). The value returned by reference via 'obj' should be passed back
4796 * to drm_property_change_valid_put() after the property is set (and the
4797 * object to which the property is attached has a chance to take it's own
4798 * reference).
4799 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004800bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004801 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004802{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004803 int i;
4804
Paulo Zanoni26a34812012-05-15 18:08:59 -03004805 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4806 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004807
Rob Clark3843e712014-12-16 18:05:29 -05004808 *ref = NULL;
4809
Rob Clark5ea22f22014-05-30 11:34:01 -04004810 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004811 if (value < property->values[0] || value > property->values[1])
4812 return false;
4813 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004814 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4815 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004816
Rob Clarkebc44cf2012-09-12 22:22:31 -05004817 if (svalue < U642I64(property->values[0]) ||
4818 svalue > U642I64(property->values[1]))
4819 return false;
4820 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004821 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004822 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004823
Rob Clark49e27542012-05-17 02:23:26 -06004824 for (i = 0; i < property->num_values; i++)
4825 valid_mask |= (1ULL << property->values[i]);
4826 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004827 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004828 struct drm_property_blob *blob;
4829
4830 if (value == 0)
4831 return true;
4832
4833 blob = drm_property_lookup_blob(property->dev, value);
4834 if (blob) {
4835 *ref = &blob->base;
4836 return true;
4837 } else {
4838 return false;
4839 }
Rob Clark98f75de2014-05-30 11:37:03 -04004840 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004841 /* a zero value for an object property translates to null: */
4842 if (value == 0)
4843 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004844
4845 /* handle refcnt'd objects specially: */
4846 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4847 struct drm_framebuffer *fb;
4848 fb = drm_framebuffer_lookup(property->dev, value);
4849 if (fb) {
4850 *ref = &fb->base;
4851 return true;
4852 } else {
4853 return false;
4854 }
4855 } else {
4856 return _object_find(property->dev, value, property->values[0]) != NULL;
4857 }
Paulo Zanoni26a34812012-05-15 18:08:59 -03004858 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004859
4860 for (i = 0; i < property->num_values; i++)
4861 if (property->values[i] == value)
4862 return true;
4863 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004864}
4865
Rob Clarkd34f20d2014-12-18 16:01:56 -05004866void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004867 struct drm_mode_object *ref)
4868{
4869 if (!ref)
4870 return;
4871
4872 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4873 if (property->values[0] == DRM_MODE_OBJECT_FB)
4874 drm_framebuffer_unreference(obj_to_fb(ref));
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004875 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4876 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004877}
4878
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004879/**
4880 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4881 * @dev: DRM device
4882 * @data: ioctl data
4883 * @file_priv: DRM file info
4884 *
4885 * This function sets the current value for a connectors's property. It also
4886 * calls into a driver's ->set_property callback to update the hardware state
4887 *
4888 * Called by the user via ioctl.
4889 *
4890 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004891 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004892 */
Dave Airlief453ba02008-11-07 14:05:41 -08004893int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4894 void *data, struct drm_file *file_priv)
4895{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004896 struct drm_mode_connector_set_property *conn_set_prop = data;
4897 struct drm_mode_obj_set_property obj_set_prop = {
4898 .value = conn_set_prop->value,
4899 .prop_id = conn_set_prop->prop_id,
4900 .obj_id = conn_set_prop->connector_id,
4901 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4902 };
Dave Airlief453ba02008-11-07 14:05:41 -08004903
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004904 /* It does all the locking and checking we need */
4905 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004906}
4907
Paulo Zanonic5431882012-05-15 18:09:02 -03004908static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4909 struct drm_property *property,
4910 uint64_t value)
4911{
4912 int ret = -EINVAL;
4913 struct drm_connector *connector = obj_to_connector(obj);
4914
4915 /* Do DPMS ourselves */
4916 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004917 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004918 } else if (connector->funcs->set_property)
4919 ret = connector->funcs->set_property(connector, property, value);
4920
4921 /* store the property value if successful */
4922 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004923 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004924 return ret;
4925}
4926
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004927static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4928 struct drm_property *property,
4929 uint64_t value)
4930{
4931 int ret = -EINVAL;
4932 struct drm_crtc *crtc = obj_to_crtc(obj);
4933
4934 if (crtc->funcs->set_property)
4935 ret = crtc->funcs->set_property(crtc, property, value);
4936 if (!ret)
4937 drm_object_property_set_value(obj, property, value);
4938
4939 return ret;
4940}
4941
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004942/**
4943 * drm_mode_plane_set_obj_prop - set the value of a property
4944 * @plane: drm plane object to set property value for
4945 * @property: property to set
4946 * @value: value the property should be set to
4947 *
4948 * This functions sets a given property on a given plane object. This function
4949 * calls the driver's ->set_property callback and changes the software state of
4950 * the property if the callback succeeds.
4951 *
4952 * Returns:
4953 * Zero on success, error code on failure.
4954 */
4955int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4956 struct drm_property *property,
4957 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004958{
4959 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004960 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004961
4962 if (plane->funcs->set_property)
4963 ret = plane->funcs->set_property(plane, property, value);
4964 if (!ret)
4965 drm_object_property_set_value(obj, property, value);
4966
4967 return ret;
4968}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004969EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004970
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004971/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004972 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004973 * @dev: DRM device
4974 * @data: ioctl data
4975 * @file_priv: DRM file info
4976 *
4977 * This function retrieves the current value for an object's property. Compared
4978 * to the connector specific ioctl this one is extended to also work on crtc and
4979 * plane objects.
4980 *
4981 * Called by the user via ioctl.
4982 *
4983 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004984 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004985 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004986int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4987 struct drm_file *file_priv)
4988{
4989 struct drm_mode_obj_get_properties *arg = data;
4990 struct drm_mode_object *obj;
4991 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004992
4993 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4994 return -EINVAL;
4995
Daniel Vetter84849902012-12-02 00:28:11 +01004996 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004997
4998 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4999 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005000 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005001 goto out;
5002 }
5003 if (!obj->properties) {
5004 ret = -EINVAL;
5005 goto out;
5006 }
5007
Rob Clark88a48e22014-12-18 16:01:50 -05005008 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005009 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5010 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5011 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005012
Paulo Zanonic5431882012-05-15 18:09:02 -03005013out:
Daniel Vetter84849902012-12-02 00:28:11 +01005014 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005015 return ret;
5016}
5017
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005018/**
5019 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5020 * @dev: DRM device
5021 * @data: ioctl data
5022 * @file_priv: DRM file info
5023 *
5024 * This function sets the current value for an object's property. It also calls
5025 * into a driver's ->set_property callback to update the hardware state.
5026 * Compared to the connector specific ioctl this one is extended to also work on
5027 * crtc and plane objects.
5028 *
5029 * Called by the user via ioctl.
5030 *
5031 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005032 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005033 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005034int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5035 struct drm_file *file_priv)
5036{
5037 struct drm_mode_obj_set_property *arg = data;
5038 struct drm_mode_object *arg_obj;
5039 struct drm_mode_object *prop_obj;
5040 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005041 int i, ret = -EINVAL;
5042 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005043
5044 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5045 return -EINVAL;
5046
Daniel Vetter84849902012-12-02 00:28:11 +01005047 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005048
5049 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005050 if (!arg_obj) {
5051 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005052 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005053 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005054 if (!arg_obj->properties)
5055 goto out;
5056
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005057 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005058 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005059 break;
5060
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005061 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03005062 goto out;
5063
5064 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5065 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005066 if (!prop_obj) {
5067 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005068 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005069 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005070 property = obj_to_property(prop_obj);
5071
Rob Clark3843e712014-12-16 18:05:29 -05005072 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03005073 goto out;
5074
5075 switch (arg_obj->type) {
5076 case DRM_MODE_OBJECT_CONNECTOR:
5077 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5078 arg->value);
5079 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005080 case DRM_MODE_OBJECT_CRTC:
5081 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5082 break;
Rob Clark4d939142012-05-17 02:23:27 -06005083 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005084 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5085 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005086 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005087 }
5088
Rob Clark3843e712014-12-16 18:05:29 -05005089 drm_property_change_valid_put(property, ref);
5090
Paulo Zanonic5431882012-05-15 18:09:02 -03005091out:
Daniel Vetter84849902012-12-02 00:28:11 +01005092 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005093 return ret;
5094}
5095
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005096/**
5097 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5098 * @connector: connector to attach
5099 * @encoder: encoder to attach @connector to
5100 *
5101 * This function links up a connector to an encoder. Note that the routing
5102 * restrictions between encoders and crtcs are exposed to userspace through the
5103 * possible_clones and possible_crtcs bitmasks.
5104 *
5105 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005106 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005107 */
Dave Airlief453ba02008-11-07 14:05:41 -08005108int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5109 struct drm_encoder *encoder)
5110{
5111 int i;
5112
Thierry Redingeb47fe82015-11-16 18:19:53 +01005113 /*
5114 * In the past, drivers have attempted to model the static association
5115 * of connector to encoder in simple connector/encoder devices using a
5116 * direct assignment of connector->encoder = encoder. This connection
5117 * is a logical one and the responsibility of the core, so drivers are
5118 * expected not to mess with this.
5119 *
5120 * Note that the error return should've been enough here, but a large
5121 * majority of drivers ignores the return value, so add in a big WARN
5122 * to get people's attention.
5123 */
5124 if (WARN_ON(connector->encoder))
5125 return -EINVAL;
5126
Dave Airlief453ba02008-11-07 14:05:41 -08005127 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5128 if (connector->encoder_ids[i] == 0) {
5129 connector->encoder_ids[i] = encoder->base.id;
5130 return 0;
5131 }
5132 }
5133 return -ENOMEM;
5134}
5135EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5136
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005137/**
5138 * drm_mode_crtc_set_gamma_size - set the gamma table size
5139 * @crtc: CRTC to set the gamma table size for
5140 * @gamma_size: size of the gamma table
5141 *
5142 * Drivers which support gamma tables should set this to the supported gamma
5143 * table size when initializing the CRTC. Currently the drm core only supports a
5144 * fixed gamma table size.
5145 *
5146 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005147 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005148 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005149int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005150 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005151{
5152 crtc->gamma_size = gamma_size;
5153
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005154 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5155 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005156 if (!crtc->gamma_store) {
5157 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005158 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005159 }
5160
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005161 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005162}
5163EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5164
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005165/**
5166 * drm_mode_gamma_set_ioctl - set the gamma table
5167 * @dev: DRM device
5168 * @data: ioctl data
5169 * @file_priv: DRM file info
5170 *
5171 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5172 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5173 *
5174 * Called by the user via ioctl.
5175 *
5176 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005177 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005178 */
Dave Airlief453ba02008-11-07 14:05:41 -08005179int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5180 void *data, struct drm_file *file_priv)
5181{
5182 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005183 struct drm_crtc *crtc;
5184 void *r_base, *g_base, *b_base;
5185 int size;
5186 int ret = 0;
5187
Dave Airliefb3b06c2011-02-08 13:55:21 +10005188 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5189 return -EINVAL;
5190
Daniel Vetter84849902012-12-02 00:28:11 +01005191 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005192 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5193 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005194 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005195 goto out;
5196 }
Dave Airlief453ba02008-11-07 14:05:41 -08005197
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005198 if (crtc->funcs->gamma_set == NULL) {
5199 ret = -ENOSYS;
5200 goto out;
5201 }
5202
Dave Airlief453ba02008-11-07 14:05:41 -08005203 /* memcpy into gamma store */
5204 if (crtc_lut->gamma_size != crtc->gamma_size) {
5205 ret = -EINVAL;
5206 goto out;
5207 }
5208
5209 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5210 r_base = crtc->gamma_store;
5211 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5212 ret = -EFAULT;
5213 goto out;
5214 }
5215
5216 g_base = r_base + size;
5217 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5218 ret = -EFAULT;
5219 goto out;
5220 }
5221
5222 b_base = g_base + size;
5223 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5224 ret = -EFAULT;
5225 goto out;
5226 }
5227
James Simmons72034252010-08-03 01:33:19 +01005228 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005229
5230out:
Daniel Vetter84849902012-12-02 00:28:11 +01005231 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005232 return ret;
5233
5234}
5235
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005236/**
5237 * drm_mode_gamma_get_ioctl - get the gamma table
5238 * @dev: DRM device
5239 * @data: ioctl data
5240 * @file_priv: DRM file info
5241 *
5242 * Copy the current gamma table into the storage provided. This also provides
5243 * the gamma table size the driver expects, which can be used to size the
5244 * allocated storage.
5245 *
5246 * Called by the user via ioctl.
5247 *
5248 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005249 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005250 */
Dave Airlief453ba02008-11-07 14:05:41 -08005251int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5252 void *data, struct drm_file *file_priv)
5253{
5254 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005255 struct drm_crtc *crtc;
5256 void *r_base, *g_base, *b_base;
5257 int size;
5258 int ret = 0;
5259
Dave Airliefb3b06c2011-02-08 13:55:21 +10005260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5261 return -EINVAL;
5262
Daniel Vetter84849902012-12-02 00:28:11 +01005263 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005264 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5265 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005266 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005267 goto out;
5268 }
Dave Airlief453ba02008-11-07 14:05:41 -08005269
5270 /* memcpy into gamma store */
5271 if (crtc_lut->gamma_size != crtc->gamma_size) {
5272 ret = -EINVAL;
5273 goto out;
5274 }
5275
5276 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5277 r_base = crtc->gamma_store;
5278 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5279 ret = -EFAULT;
5280 goto out;
5281 }
5282
5283 g_base = r_base + size;
5284 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5285 ret = -EFAULT;
5286 goto out;
5287 }
5288
5289 b_base = g_base + size;
5290 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5291 ret = -EFAULT;
5292 goto out;
5293 }
5294out:
Daniel Vetter84849902012-12-02 00:28:11 +01005295 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005296 return ret;
5297}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005298
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005299/**
5300 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5301 * @dev: DRM device
5302 * @data: ioctl data
5303 * @file_priv: DRM file info
5304 *
5305 * This schedules an asynchronous update on a given CRTC, called page flip.
5306 * Optionally a drm event is generated to signal the completion of the event.
5307 * Generic drivers cannot assume that a pageflip with changed framebuffer
5308 * properties (including driver specific metadata like tiling layout) will work,
5309 * but some drivers support e.g. pixel format changes through the pageflip
5310 * ioctl.
5311 *
5312 * Called by the user via ioctl.
5313 *
5314 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005315 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005316 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005317int drm_mode_page_flip_ioctl(struct drm_device *dev,
5318 void *data, struct drm_file *file_priv)
5319{
5320 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005321 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005322 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005323 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005324 int ret = -EINVAL;
5325
5326 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5327 page_flip->reserved != 0)
5328 return -EINVAL;
5329
Keith Packard62f21042013-07-22 18:50:00 -07005330 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5331 return -EINVAL;
5332
Rob Clarka2b34e22013-10-05 16:36:52 -04005333 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5334 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005335 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005336
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005337 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005338 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005339 /* The framebuffer is currently unbound, presumably
5340 * due to a hotplug event, that userspace has not
5341 * yet discovered.
5342 */
5343 ret = -EBUSY;
5344 goto out;
5345 }
5346
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005347 if (crtc->funcs->page_flip == NULL)
5348 goto out;
5349
Daniel Vetter786b99e2012-12-02 21:53:40 +01005350 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005351 if (!fb) {
5352 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005353 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005354 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005355
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005356 if (crtc->state) {
5357 const struct drm_plane_state *state = crtc->primary->state;
5358
5359 ret = check_src_coords(state->src_x, state->src_y,
5360 state->src_w, state->src_h, fb);
5361 } else {
5362 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5363 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005364 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005365 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005366
Matt Roperf4510a22014-04-01 15:22:40 -07005367 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005368 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5369 ret = -EINVAL;
5370 goto out;
5371 }
5372
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005373 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005374 e = kzalloc(sizeof *e, GFP_KERNEL);
5375 if (!e) {
5376 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005377 goto out;
5378 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005379 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005380 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005381 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005382 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5383 if (ret) {
5384 kfree(e);
5385 goto out;
5386 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005387 }
5388
Daniel Vetter3d30a592014-07-27 13:42:42 +02005389 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005390 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005391 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005392 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5393 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005394 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005395 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005396 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005397 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005398 /* Unref only the old framebuffer. */
5399 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005400 }
5401
5402out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005403 if (fb)
5404 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005405 if (crtc->primary->old_fb)
5406 drm_framebuffer_unreference(crtc->primary->old_fb);
5407 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005408 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005409
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005410 return ret;
5411}
Chris Wilsoneb033552011-01-24 15:11:08 +00005412
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005413/**
5414 * drm_mode_config_reset - call ->reset callbacks
5415 * @dev: drm device
5416 *
5417 * This functions calls all the crtc's, encoder's and connector's ->reset
5418 * callback. Drivers can use this in e.g. their driver load or resume code to
5419 * reset hardware and software state.
5420 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005421void drm_mode_config_reset(struct drm_device *dev)
5422{
5423 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005424 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005425 struct drm_encoder *encoder;
5426 struct drm_connector *connector;
5427
Daniel Vettere4f62542015-07-09 23:44:35 +02005428 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005429 if (plane->funcs->reset)
5430 plane->funcs->reset(plane);
5431
Daniel Vettere4f62542015-07-09 23:44:35 +02005432 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005433 if (crtc->funcs->reset)
5434 crtc->funcs->reset(crtc);
5435
Daniel Vettere4f62542015-07-09 23:44:35 +02005436 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005437 if (encoder->funcs->reset)
5438 encoder->funcs->reset(encoder);
5439
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005440 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005441 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005442 if (connector->funcs->reset)
5443 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005444 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005445}
5446EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005447
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005448/**
5449 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5450 * @dev: DRM device
5451 * @data: ioctl data
5452 * @file_priv: DRM file info
5453 *
5454 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5455 * TTM or something else entirely) and returns the resulting buffer handle. This
5456 * handle can then be wrapped up into a framebuffer modeset object.
5457 *
5458 * Note that userspace is not allowed to use such objects for render
5459 * acceleration - drivers must create their own private ioctls for such a use
5460 * case.
5461 *
5462 * Called by the user via ioctl.
5463 *
5464 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005465 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005466 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005467int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5468 void *data, struct drm_file *file_priv)
5469{
5470 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005471 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005472
5473 if (!dev->driver->dumb_create)
5474 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005475 if (!args->width || !args->height || !args->bpp)
5476 return -EINVAL;
5477
5478 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005479 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005480 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005481 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005482 return -EINVAL;
5483 stride = cpp * args->width;
5484 if (args->height > 0xffffffffU / stride)
5485 return -EINVAL;
5486
5487 /* test for wrap-around */
5488 size = args->height * stride;
5489 if (PAGE_ALIGN(size) == 0)
5490 return -EINVAL;
5491
Thierry Redingf6085952014-11-03 11:14:14 +01005492 /*
5493 * handle, pitch and size are output parameters. Zero them out to
5494 * prevent drivers from accidentally using uninitialized data. Since
5495 * not all existing userspace is clearing these fields properly we
5496 * cannot reject IOCTL with garbage in them.
5497 */
5498 args->handle = 0;
5499 args->pitch = 0;
5500 args->size = 0;
5501
Dave Airlieff72145b2011-02-07 12:16:14 +10005502 return dev->driver->dumb_create(file_priv, dev, args);
5503}
5504
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005505/**
5506 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5507 * @dev: DRM device
5508 * @data: ioctl data
5509 * @file_priv: DRM file info
5510 *
5511 * Allocate an offset in the drm device node's address space to be able to
5512 * memory map a dumb buffer.
5513 *
5514 * Called by the user via ioctl.
5515 *
5516 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005517 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005518 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005519int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5520 void *data, struct drm_file *file_priv)
5521{
5522 struct drm_mode_map_dumb *args = data;
5523
5524 /* call driver ioctl to get mmap offset */
5525 if (!dev->driver->dumb_map_offset)
5526 return -ENOSYS;
5527
5528 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5529}
5530
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005531/**
5532 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5533 * @dev: DRM device
5534 * @data: ioctl data
5535 * @file_priv: DRM file info
5536 *
5537 * This destroys the userspace handle for the given dumb backing storage buffer.
5538 * Since buffer objects must be reference counted in the kernel a buffer object
5539 * won't be immediately freed if a framebuffer modeset object still uses it.
5540 *
5541 * Called by the user via ioctl.
5542 *
5543 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005544 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005545 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005546int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5547 void *data, struct drm_file *file_priv)
5548{
5549 struct drm_mode_destroy_dumb *args = data;
5550
5551 if (!dev->driver->dumb_destroy)
5552 return -ENOSYS;
5553
5554 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5555}
Dave Airlie248dbc22011-11-29 20:02:54 +00005556
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005557/**
5558 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5559 * @format: pixel format (DRM_FORMAT_*)
5560 * @depth: storage for the depth value
5561 * @bpp: storage for the bpp value
5562 *
5563 * This only supports RGB formats here for compat with code that doesn't use
5564 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005565 */
5566void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5567 int *bpp)
5568{
5569 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005570 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005571 case DRM_FORMAT_RGB332:
5572 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005573 *depth = 8;
5574 *bpp = 8;
5575 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005576 case DRM_FORMAT_XRGB1555:
5577 case DRM_FORMAT_XBGR1555:
5578 case DRM_FORMAT_RGBX5551:
5579 case DRM_FORMAT_BGRX5551:
5580 case DRM_FORMAT_ARGB1555:
5581 case DRM_FORMAT_ABGR1555:
5582 case DRM_FORMAT_RGBA5551:
5583 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005584 *depth = 15;
5585 *bpp = 16;
5586 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005587 case DRM_FORMAT_RGB565:
5588 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005589 *depth = 16;
5590 *bpp = 16;
5591 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005592 case DRM_FORMAT_RGB888:
5593 case DRM_FORMAT_BGR888:
5594 *depth = 24;
5595 *bpp = 24;
5596 break;
5597 case DRM_FORMAT_XRGB8888:
5598 case DRM_FORMAT_XBGR8888:
5599 case DRM_FORMAT_RGBX8888:
5600 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005601 *depth = 24;
5602 *bpp = 32;
5603 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005604 case DRM_FORMAT_XRGB2101010:
5605 case DRM_FORMAT_XBGR2101010:
5606 case DRM_FORMAT_RGBX1010102:
5607 case DRM_FORMAT_BGRX1010102:
5608 case DRM_FORMAT_ARGB2101010:
5609 case DRM_FORMAT_ABGR2101010:
5610 case DRM_FORMAT_RGBA1010102:
5611 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005612 *depth = 30;
5613 *bpp = 32;
5614 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005615 case DRM_FORMAT_ARGB8888:
5616 case DRM_FORMAT_ABGR8888:
5617 case DRM_FORMAT_RGBA8888:
5618 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005619 *depth = 32;
5620 *bpp = 32;
5621 break;
5622 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005623 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5624 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005625 *depth = 0;
5626 *bpp = 0;
5627 break;
5628 }
5629}
5630EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005631
5632/**
5633 * drm_format_num_planes - get the number of planes for format
5634 * @format: pixel format (DRM_FORMAT_*)
5635 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005636 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005637 * The number of planes used by the specified pixel format.
5638 */
5639int drm_format_num_planes(uint32_t format)
5640{
5641 switch (format) {
5642 case DRM_FORMAT_YUV410:
5643 case DRM_FORMAT_YVU410:
5644 case DRM_FORMAT_YUV411:
5645 case DRM_FORMAT_YVU411:
5646 case DRM_FORMAT_YUV420:
5647 case DRM_FORMAT_YVU420:
5648 case DRM_FORMAT_YUV422:
5649 case DRM_FORMAT_YVU422:
5650 case DRM_FORMAT_YUV444:
5651 case DRM_FORMAT_YVU444:
5652 return 3;
5653 case DRM_FORMAT_NV12:
5654 case DRM_FORMAT_NV21:
5655 case DRM_FORMAT_NV16:
5656 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005657 case DRM_FORMAT_NV24:
5658 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005659 return 2;
5660 default:
5661 return 1;
5662 }
5663}
5664EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005665
5666/**
5667 * drm_format_plane_cpp - determine the bytes per pixel value
5668 * @format: pixel format (DRM_FORMAT_*)
5669 * @plane: plane index
5670 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005671 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005672 * The bytes per pixel value for the specified plane.
5673 */
5674int drm_format_plane_cpp(uint32_t format, int plane)
5675{
5676 unsigned int depth;
5677 int bpp;
5678
5679 if (plane >= drm_format_num_planes(format))
5680 return 0;
5681
5682 switch (format) {
5683 case DRM_FORMAT_YUYV:
5684 case DRM_FORMAT_YVYU:
5685 case DRM_FORMAT_UYVY:
5686 case DRM_FORMAT_VYUY:
5687 return 2;
5688 case DRM_FORMAT_NV12:
5689 case DRM_FORMAT_NV21:
5690 case DRM_FORMAT_NV16:
5691 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005692 case DRM_FORMAT_NV24:
5693 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005694 return plane ? 2 : 1;
5695 case DRM_FORMAT_YUV410:
5696 case DRM_FORMAT_YVU410:
5697 case DRM_FORMAT_YUV411:
5698 case DRM_FORMAT_YVU411:
5699 case DRM_FORMAT_YUV420:
5700 case DRM_FORMAT_YVU420:
5701 case DRM_FORMAT_YUV422:
5702 case DRM_FORMAT_YVU422:
5703 case DRM_FORMAT_YUV444:
5704 case DRM_FORMAT_YVU444:
5705 return 1;
5706 default:
5707 drm_fb_get_bpp_depth(format, &depth, &bpp);
5708 return bpp >> 3;
5709 }
5710}
5711EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005712
5713/**
5714 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5715 * @format: pixel format (DRM_FORMAT_*)
5716 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005717 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005718 * The horizontal chroma subsampling factor for the
5719 * specified pixel format.
5720 */
5721int drm_format_horz_chroma_subsampling(uint32_t format)
5722{
5723 switch (format) {
5724 case DRM_FORMAT_YUV411:
5725 case DRM_FORMAT_YVU411:
5726 case DRM_FORMAT_YUV410:
5727 case DRM_FORMAT_YVU410:
5728 return 4;
5729 case DRM_FORMAT_YUYV:
5730 case DRM_FORMAT_YVYU:
5731 case DRM_FORMAT_UYVY:
5732 case DRM_FORMAT_VYUY:
5733 case DRM_FORMAT_NV12:
5734 case DRM_FORMAT_NV21:
5735 case DRM_FORMAT_NV16:
5736 case DRM_FORMAT_NV61:
5737 case DRM_FORMAT_YUV422:
5738 case DRM_FORMAT_YVU422:
5739 case DRM_FORMAT_YUV420:
5740 case DRM_FORMAT_YVU420:
5741 return 2;
5742 default:
5743 return 1;
5744 }
5745}
5746EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5747
5748/**
5749 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5750 * @format: pixel format (DRM_FORMAT_*)
5751 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005752 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005753 * The vertical chroma subsampling factor for the
5754 * specified pixel format.
5755 */
5756int drm_format_vert_chroma_subsampling(uint32_t format)
5757{
5758 switch (format) {
5759 case DRM_FORMAT_YUV410:
5760 case DRM_FORMAT_YVU410:
5761 return 4;
5762 case DRM_FORMAT_YUV420:
5763 case DRM_FORMAT_YVU420:
5764 case DRM_FORMAT_NV12:
5765 case DRM_FORMAT_NV21:
5766 return 2;
5767 default:
5768 return 1;
5769 }
5770}
5771EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005772
5773/**
Ville Syrjälä4c617162016-02-09 17:29:44 +02005774 * drm_format_plane_width - width of the plane given the first plane
5775 * @width: width of the first plane
5776 * @format: pixel format
5777 * @plane: plane index
5778 *
5779 * Returns:
5780 * The width of @plane, given that the width of the first plane is @width.
5781 */
5782int drm_format_plane_width(int width, uint32_t format, int plane)
5783{
5784 if (plane >= drm_format_num_planes(format))
5785 return 0;
5786
5787 if (plane == 0)
5788 return width;
5789
5790 return width / drm_format_horz_chroma_subsampling(format);
5791}
5792EXPORT_SYMBOL(drm_format_plane_width);
5793
5794/**
5795 * drm_format_plane_height - height of the plane given the first plane
5796 * @height: height of the first plane
5797 * @format: pixel format
5798 * @plane: plane index
5799 *
5800 * Returns:
5801 * The height of @plane, given that the height of the first plane is @height.
5802 */
5803int drm_format_plane_height(int height, uint32_t format, int plane)
5804{
5805 if (plane >= drm_format_num_planes(format))
5806 return 0;
5807
5808 if (plane == 0)
5809 return height;
5810
5811 return height / drm_format_vert_chroma_subsampling(format);
5812}
5813EXPORT_SYMBOL(drm_format_plane_height);
5814
5815/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305816 * drm_rotation_simplify() - Try to simplify the rotation
5817 * @rotation: Rotation to be simplified
5818 * @supported_rotations: Supported rotations
5819 *
5820 * Attempt to simplify the rotation to a form that is supported.
5821 * Eg. if the hardware supports everything except DRM_REFLECT_X
5822 * one could call this function like this:
5823 *
5824 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5825 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5826 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5827 *
5828 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5829 * transforms the hardware supports, this function may not
5830 * be able to produce a supported transform, so the caller should
5831 * check the result afterwards.
5832 */
5833unsigned int drm_rotation_simplify(unsigned int rotation,
5834 unsigned int supported_rotations)
5835{
5836 if (rotation & ~supported_rotations) {
5837 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005838 rotation = (rotation & DRM_REFLECT_MASK) |
5839 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305840 }
5841
5842 return rotation;
5843}
5844EXPORT_SYMBOL(drm_rotation_simplify);
5845
5846/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005847 * drm_mode_config_init - initialize DRM mode_configuration structure
5848 * @dev: DRM device
5849 *
5850 * Initialize @dev's mode_config structure, used for tracking the graphics
5851 * configuration of @dev.
5852 *
5853 * Since this initializes the modeset locks, no locking is possible. Which is no
5854 * problem, since this should happen single threaded at init time. It is the
5855 * driver's problem to ensure this guarantee.
5856 *
5857 */
5858void drm_mode_config_init(struct drm_device *dev)
5859{
5860 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005861 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005862 mutex_init(&dev->mode_config.idr_mutex);
5863 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005864 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005865 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5866 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5867 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5868 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5869 INIT_LIST_HEAD(&dev->mode_config.property_list);
5870 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5871 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5872 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005873 idr_init(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005874
5875 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005876 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005877 drm_modeset_unlock_all(dev);
5878
5879 /* Just to be sure */
5880 dev->mode_config.num_fb = 0;
5881 dev->mode_config.num_connector = 0;
5882 dev->mode_config.num_crtc = 0;
5883 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005884 dev->mode_config.num_overlay_plane = 0;
5885 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005886}
5887EXPORT_SYMBOL(drm_mode_config_init);
5888
5889/**
5890 * drm_mode_config_cleanup - free up DRM mode_config info
5891 * @dev: DRM device
5892 *
5893 * Free up all the connectors and CRTCs associated with this DRM device, then
5894 * free up the framebuffers and associated buffer objects.
5895 *
5896 * Note that since this /should/ happen single-threaded at driver/device
5897 * teardown time, no locking is required. It's the driver's job to ensure that
5898 * this guarantee actually holds true.
5899 *
5900 * FIXME: cleanup any dangling user buffer objects too
5901 */
5902void drm_mode_config_cleanup(struct drm_device *dev)
5903{
5904 struct drm_connector *connector, *ot;
5905 struct drm_crtc *crtc, *ct;
5906 struct drm_encoder *encoder, *enct;
5907 struct drm_framebuffer *fb, *fbt;
5908 struct drm_property *property, *pt;
5909 struct drm_property_blob *blob, *bt;
5910 struct drm_plane *plane, *plt;
5911
5912 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5913 head) {
5914 encoder->funcs->destroy(encoder);
5915 }
5916
5917 list_for_each_entry_safe(connector, ot,
5918 &dev->mode_config.connector_list, head) {
5919 connector->funcs->destroy(connector);
5920 }
5921
5922 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5923 head) {
5924 drm_property_destroy(dev, property);
5925 }
5926
5927 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005928 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005929 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005930 }
5931
5932 /*
5933 * Single-threaded teardown context, so it's not required to grab the
5934 * fb_lock to protect against concurrent fb_list access. Contrary, it
5935 * would actually deadlock with the drm_framebuffer_cleanup function.
5936 *
5937 * Also, if there are any framebuffers left, that's a driver leak now,
5938 * so politely WARN about this.
5939 */
5940 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5941 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Maarten Lankhorstc099b552015-09-09 13:46:21 +02005942 drm_framebuffer_free(&fb->refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005943 }
5944
5945 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5946 head) {
5947 plane->funcs->destroy(plane);
5948 }
5949
5950 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5951 crtc->funcs->destroy(crtc);
5952 }
5953
Dave Airlie138f9eb2014-10-20 16:17:17 +10005954 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005955 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005956 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005957}
5958EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305959
5960struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5961 unsigned int supported_rotations)
5962{
5963 static const struct drm_prop_enum_list props[] = {
5964 { DRM_ROTATE_0, "rotate-0" },
5965 { DRM_ROTATE_90, "rotate-90" },
5966 { DRM_ROTATE_180, "rotate-180" },
5967 { DRM_ROTATE_270, "rotate-270" },
5968 { DRM_REFLECT_X, "reflect-x" },
5969 { DRM_REFLECT_Y, "reflect-y" },
5970 };
5971
5972 return drm_property_create_bitmask(dev, 0, "rotation",
5973 props, ARRAY_SIZE(props),
5974 supported_rotations);
5975}
5976EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005977
5978/**
5979 * DOC: Tile group
5980 *
5981 * Tile groups are used to represent tiled monitors with a unique
5982 * integer identifier. Tiled monitors using DisplayID v1.3 have
5983 * a unique 8-byte handle, we store this in a tile group, so we
5984 * have a common identifier for all tiles in a monitor group.
5985 */
5986static void drm_tile_group_free(struct kref *kref)
5987{
5988 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5989 struct drm_device *dev = tg->dev;
5990 mutex_lock(&dev->mode_config.idr_mutex);
5991 idr_remove(&dev->mode_config.tile_idr, tg->id);
5992 mutex_unlock(&dev->mode_config.idr_mutex);
5993 kfree(tg);
5994}
5995
5996/**
5997 * drm_mode_put_tile_group - drop a reference to a tile group.
5998 * @dev: DRM device
5999 * @tg: tile group to drop reference to.
6000 *
6001 * drop reference to tile group and free if 0.
6002 */
6003void drm_mode_put_tile_group(struct drm_device *dev,
6004 struct drm_tile_group *tg)
6005{
6006 kref_put(&tg->refcount, drm_tile_group_free);
6007}
6008
6009/**
6010 * drm_mode_get_tile_group - get a reference to an existing tile group
6011 * @dev: DRM device
6012 * @topology: 8-bytes unique per monitor.
6013 *
6014 * Use the unique bytes to get a reference to an existing tile group.
6015 *
6016 * RETURNS:
6017 * tile group or NULL if not found.
6018 */
6019struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6020 char topology[8])
6021{
6022 struct drm_tile_group *tg;
6023 int id;
6024 mutex_lock(&dev->mode_config.idr_mutex);
6025 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6026 if (!memcmp(tg->group_data, topology, 8)) {
6027 if (!kref_get_unless_zero(&tg->refcount))
6028 tg = NULL;
6029 mutex_unlock(&dev->mode_config.idr_mutex);
6030 return tg;
6031 }
6032 }
6033 mutex_unlock(&dev->mode_config.idr_mutex);
6034 return NULL;
6035}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006036EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10006037
6038/**
6039 * drm_mode_create_tile_group - create a tile group from a displayid description
6040 * @dev: DRM device
6041 * @topology: 8-bytes unique per monitor.
6042 *
6043 * Create a tile group for the unique monitor, and get a unique
6044 * identifier for the tile group.
6045 *
6046 * RETURNS:
6047 * new tile group or error.
6048 */
6049struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6050 char topology[8])
6051{
6052 struct drm_tile_group *tg;
6053 int ret;
6054
6055 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6056 if (!tg)
6057 return ERR_PTR(-ENOMEM);
6058
6059 kref_init(&tg->refcount);
6060 memcpy(tg->group_data, topology, 8);
6061 tg->dev = dev;
6062
6063 mutex_lock(&dev->mode_config.idr_mutex);
6064 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6065 if (ret >= 0) {
6066 tg->id = ret;
6067 } else {
6068 kfree(tg);
6069 tg = ERR_PTR(ret);
6070 }
6071
6072 mutex_unlock(&dev->mode_config.idr_mutex);
6073 return tg;
6074}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006075EXPORT_SYMBOL(drm_mode_create_tile_group);