blob: 4499b12b1870ffe3f727d1b425a109f1290ed19b [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/**
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000326 * drm_mode_object_unregister - 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 *
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000330 * Free @id from @dev's unique identifier pool.
331 * This function can be called multiple times, and guards against
332 * multiple removals.
333 * These modeset identifiers are _not_ reference counted. Hence don't use this
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100334 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800335 */
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000336void drm_mode_object_unregister(struct drm_device *dev,
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100337 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800338{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000339 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000340 if (object->id) {
341 idr_remove(&dev->mode_config.crtc_idr, object->id);
342 object->id = 0;
343 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000344 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800345}
346
Rob Clark98f75de2014-05-30 11:37:03 -0400347static struct drm_mode_object *_object_find(struct drm_device *dev,
348 uint32_t id, uint32_t type)
349{
350 struct drm_mode_object *obj = NULL;
351
352 mutex_lock(&dev->mode_config.idr_mutex);
353 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200354 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
355 obj = NULL;
356 if (obj && obj->id != id)
357 obj = NULL;
358 /* don't leak out unref'd fb's */
Daniel Stone6bcacf52015-04-20 19:22:55 +0100359 if (obj &&
360 (obj->type == DRM_MODE_OBJECT_FB ||
361 obj->type == DRM_MODE_OBJECT_BLOB))
Rob Clark98f75de2014-05-30 11:37:03 -0400362 obj = NULL;
363 mutex_unlock(&dev->mode_config.idr_mutex);
364
365 return obj;
366}
367
Daniel Vetter786b99e2012-12-02 21:53:40 +0100368/**
369 * drm_mode_object_find - look up a drm object with static lifetime
370 * @dev: drm device
371 * @id: id of the mode object
372 * @type: type of the mode object
373 *
374 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400375 * are reference counted, they need special treatment. Even with
376 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
377 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100378 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200379struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
380 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800381{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000382 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800383
Daniel Vetter786b99e2012-12-02 21:53:40 +0100384 /* Framebuffers are reference counted and need their own lookup
385 * function.*/
Daniel Stone6bcacf52015-04-20 19:22:55 +0100386 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
Rob Clark98f75de2014-05-30 11:37:03 -0400387 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800388 return obj;
389}
390EXPORT_SYMBOL(drm_mode_object_find);
391
Dave Airlief55f1f92016-04-15 15:10:33 +1000392static void drm_framebuffer_free(struct kref *kref)
393{
394 struct drm_framebuffer *fb =
395 container_of(kref, struct drm_framebuffer, refcount);
396 struct drm_device *dev = fb->dev;
397
398 /*
399 * The lookup idr holds a weak reference, which has not necessarily been
400 * removed at this point. Check for that.
401 */
402 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlie19ab3f82016-04-15 15:10:34 +1000403 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000404 mutex_unlock(&dev->mode_config.fb_lock);
405
406 fb->funcs->destroy(fb);
407}
408
Dave Airlief453ba02008-11-07 14:05:41 -0800409/**
Dave Airlief453ba02008-11-07 14:05:41 -0800410 * drm_framebuffer_init - initialize a framebuffer
411 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100412 * @fb: framebuffer to be initialized
413 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800414 *
Dave Airlief453ba02008-11-07 14:05:41 -0800415 * Allocates an ID for the framebuffer's parent mode object, sets its mode
416 * functions & device file and adds it to the master fd list.
417 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100418 * IMPORTANT:
419 * This functions publishes the fb and makes it available for concurrent access
420 * by other users. Which means by this point the fb _must_ be fully set up -
421 * since all the fb attributes are invariant over its lifetime, no further
422 * locking but only correct reference counting is required.
423 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100424 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200425 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800426 */
427int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
428 const struct drm_framebuffer_funcs *funcs)
429{
430 int ret;
431
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100432 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000433 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100434 INIT_LIST_HEAD(&fb->filp_head);
435 fb->dev = dev;
436 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000437
Dave Airlief453ba02008-11-07 14:05:41 -0800438 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200439 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100440 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800441
Dave Airlief453ba02008-11-07 14:05:41 -0800442 dev->mode_config.num_fb++;
443 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100444out:
445 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800446
Lukas Wunner3c67d832015-10-15 11:56:56 +0200447 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800448}
449EXPORT_SYMBOL(drm_framebuffer_init);
450
Daniel Vetter2b677e82012-12-10 21:16:05 +0100451static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
452 uint32_t id)
453{
454 struct drm_mode_object *obj = NULL;
455 struct drm_framebuffer *fb;
456
457 mutex_lock(&dev->mode_config.idr_mutex);
458 obj = idr_find(&dev->mode_config.crtc_idr, id);
459 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
460 fb = NULL;
461 else
462 fb = obj_to_fb(obj);
463 mutex_unlock(&dev->mode_config.idr_mutex);
464
465 return fb;
466}
467
Rob Clarkf7eff602012-09-05 21:48:38 +0000468/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100469 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
470 * @dev: drm device
471 * @id: id of the fb object
472 *
473 * If successful, this grabs an additional reference to the framebuffer -
474 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100475 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100476 */
477struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
478 uint32_t id)
479{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100480 struct drm_framebuffer *fb;
481
482 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100483 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter83f45fc2014-08-06 09:10:18 +0200484 if (fb) {
485 if (!kref_get_unless_zero(&fb->refcount))
486 fb = NULL;
487 }
Daniel Vetter786b99e2012-12-02 21:53:40 +0100488 mutex_unlock(&dev->mode_config.fb_lock);
489
490 return fb;
491}
492EXPORT_SYMBOL(drm_framebuffer_lookup);
493
494/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000495 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100496 * @fb: framebuffer to unref
497 *
498 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000499 */
500void drm_framebuffer_unreference(struct drm_framebuffer *fb)
501{
Rob Clark82912722014-03-18 10:07:08 -0400502 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000503 kref_put(&fb->refcount, drm_framebuffer_free);
504}
505EXPORT_SYMBOL(drm_framebuffer_unreference);
506
507/**
508 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100509 * @fb: framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100510 *
511 * This functions increments the fb's refcount.
Rob Clarkf7eff602012-09-05 21:48:38 +0000512 */
513void drm_framebuffer_reference(struct drm_framebuffer *fb)
514{
Rob Clark82912722014-03-18 10:07:08 -0400515 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
Rob Clarkf7eff602012-09-05 21:48:38 +0000516 kref_get(&fb->refcount);
517}
518EXPORT_SYMBOL(drm_framebuffer_reference);
519
Dave Airlief453ba02008-11-07 14:05:41 -0800520/**
Daniel Vetter36206362012-12-10 20:42:17 +0100521 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
522 * @fb: fb to unregister
523 *
524 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
525 * those used for fbdev. Note that the caller must hold a reference of it's own,
526 * i.e. the object may not be destroyed through this call (since it'll lead to a
527 * locking inversion).
528 */
529void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
530{
Daniel Vettera39a3572015-08-25 15:45:11 +0200531 struct drm_device *dev;
532
533 if (!fb)
534 return;
535
536 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100537
538 mutex_lock(&dev->mode_config.fb_lock);
539 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000540 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100541 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100542}
543EXPORT_SYMBOL(drm_framebuffer_unregister_private);
544
545/**
Dave Airlief453ba02008-11-07 14:05:41 -0800546 * drm_framebuffer_cleanup - remove a framebuffer object
547 * @fb: framebuffer to remove
548 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100549 * Cleanup framebuffer. This function is intended to be used from the drivers
550 * ->destroy callback. It can also be used to clean up driver private
551 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100552 *
553 * Note that this function does not remove the fb from active usuage - if it is
554 * still used anywhere, hilarity can ensue since userspace could call getfb on
555 * the id and get back -EINVAL. Obviously no concern at driver unload time.
556 *
557 * Also, the framebuffer will not be removed from the lookup idr - for
558 * user-created framebuffers this will happen in in the rmfb ioctl. For
559 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
560 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800561 */
562void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
563{
564 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100565
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100566 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000567 list_del(&fb->head);
568 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100569 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000570}
571EXPORT_SYMBOL(drm_framebuffer_cleanup);
572
573/**
574 * drm_framebuffer_remove - remove and unreference a framebuffer object
575 * @fb: framebuffer to remove
576 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000577 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100578 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100579 * passed-in framebuffer. Might take the modeset locks.
580 *
581 * Note that this function optimizes the cleanup away if the caller holds the
582 * last reference to the framebuffer. It is also guaranteed to not take the
583 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000584 */
585void drm_framebuffer_remove(struct drm_framebuffer *fb)
586{
Daniel Vettera39a3572015-08-25 15:45:11 +0200587 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800588 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800589 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000590 struct drm_mode_set set;
591 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800592
Daniel Vettera39a3572015-08-25 15:45:11 +0200593 if (!fb)
594 return;
595
596 dev = fb->dev;
597
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100598 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100599
Daniel Vetterb62584e2012-12-11 16:51:35 +0100600 /*
601 * drm ABI mandates that we remove any deleted framebuffers from active
602 * useage. But since most sane clients only remove framebuffers they no
603 * longer need, try to optimize this away.
604 *
605 * Since we're holding a reference ourselves, observing a refcount of 1
606 * means that we're the last holder and can skip it. Also, the refcount
607 * can never increase from 1 again, so we don't need any barriers or
608 * locks.
609 *
610 * Note that userspace could try to race with use and instate a new
611 * usage _after_ we've cleared all current ones. End result will be an
612 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
613 * in this manner.
614 */
615 if (atomic_read(&fb->refcount.refcount) > 1) {
616 drm_modeset_lock_all(dev);
617 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200618 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700619 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100620 /* should turn off the crtc */
621 memset(&set, 0, sizeof(struct drm_mode_set));
622 set.crtc = crtc;
623 set.fb = NULL;
624 ret = drm_mode_set_config_internal(&set);
625 if (ret)
626 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
627 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000628 }
Dave Airlief453ba02008-11-07 14:05:41 -0800629
Daniel Vetter6295d602015-07-09 23:44:25 +0200630 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300631 if (plane->fb == fb)
632 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800633 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100634 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800635 }
636
Rob Clarkf7eff602012-09-05 21:48:38 +0000637 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800638}
Rob Clarkf7eff602012-09-05 21:48:38 +0000639EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800640
Rob Clark51fd3712013-11-19 12:10:12 -0500641DEFINE_WW_CLASS(crtc_ww_class);
642
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200643static unsigned int drm_num_crtcs(struct drm_device *dev)
644{
645 unsigned int num = 0;
646 struct drm_crtc *tmp;
647
648 drm_for_each_crtc(tmp, dev) {
649 num++;
650 }
651
652 return num;
653}
654
Dave Airlief453ba02008-11-07 14:05:41 -0800655/**
Matt Ropere13161a2014-04-01 15:22:38 -0700656 * drm_crtc_init_with_planes - Initialise a new CRTC object with
657 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800658 * @dev: DRM device
659 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700660 * @primary: Primary plane for CRTC
661 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800662 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200663 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800664 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000665 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200666 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100667 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200668 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800669 */
Matt Ropere13161a2014-04-01 15:22:38 -0700670int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
671 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700672 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200673 const struct drm_crtc_funcs *funcs,
674 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800675{
Rob Clark51fd3712013-11-19 12:10:12 -0500676 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200677 int ret;
678
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100679 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
680 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
681
Dave Airlief453ba02008-11-07 14:05:41 -0800682 crtc->dev = dev;
683 crtc->funcs = funcs;
684
Rob Clark51fd3712013-11-19 12:10:12 -0500685 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200686 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
687 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100688 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800689
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200690 if (name) {
691 va_list ap;
692
693 va_start(ap, name);
694 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
695 va_end(ap);
696 } else {
697 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
698 drm_num_crtcs(dev));
699 }
700 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000701 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200702 return -ENOMEM;
703 }
704
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300705 crtc->base.properties = &crtc->properties;
706
Rob Clark51fd3712013-11-19 12:10:12 -0500707 list_add_tail(&crtc->head, &config->crtc_list);
708 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200709
Matt Ropere13161a2014-04-01 15:22:38 -0700710 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700711 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700712 if (primary)
713 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700714 if (cursor)
715 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700716
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100717 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
718 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100719 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100720 }
721
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100722 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800723}
Matt Ropere13161a2014-04-01 15:22:38 -0700724EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800725
726/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000727 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800728 * @crtc: CRTC to cleanup
729 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000730 * This function cleans up @crtc and removes it from the DRM mode setting
731 * core. Note that the function does *not* free the crtc structure itself,
732 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800733 */
734void drm_crtc_cleanup(struct drm_crtc *crtc)
735{
736 struct drm_device *dev = crtc->dev;
737
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000738 kfree(crtc->gamma_store);
739 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800740
Rob Clark51fd3712013-11-19 12:10:12 -0500741 drm_modeset_lock_fini(&crtc->mutex);
742
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000743 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800744 list_del(&crtc->head);
745 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100746
747 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
748 if (crtc->state && crtc->funcs->atomic_destroy_state)
749 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100750
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200751 kfree(crtc->name);
752
Thierry Redinga18c0af2014-12-10 11:38:49 +0100753 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800754}
755EXPORT_SYMBOL(drm_crtc_cleanup);
756
757/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000758 * drm_crtc_index - find the index of a registered CRTC
759 * @crtc: CRTC to find index for
760 *
761 * Given a registered CRTC, return the index of that CRTC within a DRM
762 * device's list of CRTCs.
763 */
764unsigned int drm_crtc_index(struct drm_crtc *crtc)
765{
766 unsigned int index = 0;
767 struct drm_crtc *tmp;
768
Daniel Vettere4f62542015-07-09 23:44:35 +0200769 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000770 if (tmp == crtc)
771 return index;
772
773 index++;
774 }
775
776 BUG();
777}
778EXPORT_SYMBOL(drm_crtc_index);
779
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100780/*
Dave Airlief453ba02008-11-07 14:05:41 -0800781 * drm_mode_remove - remove and free a mode
782 * @connector: connector list to modify
783 * @mode: mode to remove
784 *
Dave Airlief453ba02008-11-07 14:05:41 -0800785 * Remove @mode from @connector's mode list, then free it.
786 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100787static void drm_mode_remove(struct drm_connector *connector,
788 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800789{
790 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100791 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800792}
Dave Airlief453ba02008-11-07 14:05:41 -0800793
794/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200795 * drm_display_info_set_bus_formats - set the supported bus formats
796 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100797 * @formats: array containing the supported bus formats
798 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200799 *
800 * Store the supported bus formats in display info structure.
801 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
802 * a full list of available formats.
803 */
804int drm_display_info_set_bus_formats(struct drm_display_info *info,
805 const u32 *formats,
806 unsigned int num_formats)
807{
808 u32 *fmts = NULL;
809
810 if (!formats && num_formats)
811 return -EINVAL;
812
813 if (formats && num_formats) {
814 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
815 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300816 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200817 return -ENOMEM;
818 }
819
820 kfree(info->bus_formats);
821 info->bus_formats = fmts;
822 info->num_bus_formats = num_formats;
823
824 return 0;
825}
826EXPORT_SYMBOL(drm_display_info_set_bus_formats);
827
828/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200829 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
830 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200831 *
832 * The kernel supports per-connector configration of its consoles through
833 * use of the video= parameter. This function parses that option and
834 * extracts the user's specified mode (or enable/disable status) for a
835 * particular connector. This is typically only used during the early fbdev
836 * setup.
837 */
838static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
839{
840 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
841 char *option = NULL;
842
843 if (fb_get_options(connector->name, &option))
844 return;
845
846 if (!drm_mode_parse_command_line_for_connector(option,
847 connector,
848 mode))
849 return;
850
851 if (mode->force) {
852 const char *s;
853
854 switch (mode->force) {
855 case DRM_FORCE_OFF:
856 s = "OFF";
857 break;
858 case DRM_FORCE_ON_DIGITAL:
859 s = "ON - dig";
860 break;
861 default:
862 case DRM_FORCE_ON:
863 s = "ON";
864 break;
865 }
866
867 DRM_INFO("forcing %s connector %s\n", connector->name, s);
868 connector->force = mode->force;
869 }
870
871 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
872 connector->name,
873 mode->xres, mode->yres,
874 mode->refresh_specified ? mode->refresh : 60,
875 mode->rb ? " reduced blanking" : "",
876 mode->margins ? " with margins" : "",
877 mode->interlace ? " interlaced" : "");
878}
879
880/**
Dave Airlief453ba02008-11-07 14:05:41 -0800881 * drm_connector_init - Init a preallocated connector
882 * @dev: DRM device
883 * @connector: the connector to init
884 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100885 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800886 *
Dave Airlief453ba02008-11-07 14:05:41 -0800887 * Initialises a preallocated connector. Connectors should be
888 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200889 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100890 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200891 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800892 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200893int drm_connector_init(struct drm_device *dev,
894 struct drm_connector *connector,
895 const struct drm_connector_funcs *funcs,
896 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800897{
Rob Clarkae16c592014-12-18 16:01:54 -0500898 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200899 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400900 struct ida *connector_ida =
901 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200902
Daniel Vetter84849902012-12-02 00:28:11 +0100903 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800904
Dave Airlie2ee39452014-07-24 11:53:45 +1000905 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200906 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300907 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200908
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300909 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800910 connector->dev = dev;
911 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100912
913 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
914 if (connector->connector_id < 0) {
915 ret = connector->connector_id;
916 goto out_put;
917 }
918
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;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100924 goto out_put_id;
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;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100932 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300933 }
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;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100960out_put_type_id:
961 if (ret)
962 ida_remove(connector_ida, connector->connector_type_id);
963out_put_id:
964 if (ret)
965 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300966out_put:
967 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000968 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300969
970out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100971 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200972
973 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800974}
975EXPORT_SYMBOL(drm_connector_init);
976
977/**
978 * drm_connector_cleanup - cleans up an initialised connector
979 * @connector: connector to cleanup
980 *
Dave Airlief453ba02008-11-07 14:05:41 -0800981 * Cleans up the connector but doesn't free the object.
982 */
983void drm_connector_cleanup(struct drm_connector *connector)
984{
985 struct drm_device *dev = connector->dev;
986 struct drm_display_mode *mode, *t;
987
Dave Airlie40d9b042014-10-20 16:29:33 +1000988 if (connector->tile_group) {
989 drm_mode_put_tile_group(dev, connector->tile_group);
990 connector->tile_group = NULL;
991 }
992
Dave Airlief453ba02008-11-07 14:05:41 -0800993 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
994 drm_mode_remove(connector, mode);
995
996 list_for_each_entry_safe(mode, t, &connector->modes, head)
997 drm_mode_remove(connector, mode);
998
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400999 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
1000 connector->connector_type_id);
1001
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001002 ida_remove(&dev->mode_config.connector_ida,
1003 connector->connector_id);
1004
Boris Brezillonb5571e92014-07-22 12:09:10 +02001005 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001006 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +03001007 kfree(connector->name);
1008 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001009 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001010 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +01001011
1012 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1013 if (connector->state && connector->funcs->atomic_destroy_state)
1014 connector->funcs->atomic_destroy_state(connector,
1015 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001016
1017 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001018}
1019EXPORT_SYMBOL(drm_connector_cleanup);
1020
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001021/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001022 * drm_connector_register - register a connector
1023 * @connector: the connector to register
1024 *
1025 * Register userspace interfaces for a connector
1026 *
1027 * Returns:
1028 * Zero on success, error code on failure.
1029 */
1030int drm_connector_register(struct drm_connector *connector)
1031{
Thomas Wood30f65702014-06-18 17:52:32 +01001032 int ret;
1033
Dave Airlie2ee39452014-07-24 11:53:45 +10001034 drm_mode_object_register(connector->dev, &connector->base);
1035
Thomas Wood30f65702014-06-18 17:52:32 +01001036 ret = drm_sysfs_connector_add(connector);
1037 if (ret)
1038 return ret;
1039
1040 ret = drm_debugfs_connector_add(connector);
1041 if (ret) {
1042 drm_sysfs_connector_remove(connector);
1043 return ret;
1044 }
1045
1046 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001047}
1048EXPORT_SYMBOL(drm_connector_register);
1049
1050/**
1051 * drm_connector_unregister - unregister a connector
1052 * @connector: the connector to unregister
1053 *
1054 * Unregister userspace interfaces for a connector
1055 */
1056void drm_connector_unregister(struct drm_connector *connector)
1057{
1058 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001059 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001060}
1061EXPORT_SYMBOL(drm_connector_unregister);
1062
Thomas Wood34ea3d32014-05-29 16:57:41 +01001063/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001064 * drm_connector_register_all - register all connectors
1065 * @dev: drm device
1066 *
1067 * This function registers all connectors in sysfs and other places so that
1068 * userspace can start to access them. Drivers can call it after calling
1069 * drm_dev_register() to complete the device registration, if they don't call
1070 * drm_connector_register() on each connector individually.
1071 *
1072 * When a device is unplugged and should be removed from userspace access,
1073 * call drm_connector_unregister_all(), which is the inverse of this
1074 * function.
1075 *
1076 * Returns:
1077 * Zero on success, error code on failure.
1078 */
1079int drm_connector_register_all(struct drm_device *dev)
1080{
1081 struct drm_connector *connector;
1082 int ret;
1083
1084 mutex_lock(&dev->mode_config.mutex);
1085
1086 drm_for_each_connector(connector, dev) {
1087 ret = drm_connector_register(connector);
1088 if (ret)
1089 goto err;
1090 }
1091
1092 mutex_unlock(&dev->mode_config.mutex);
1093
1094 return 0;
1095
1096err:
1097 mutex_unlock(&dev->mode_config.mutex);
1098 drm_connector_unregister_all(dev);
1099 return ret;
1100}
1101EXPORT_SYMBOL(drm_connector_register_all);
1102
1103/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001104 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001105 * @dev: drm device
1106 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001107 * This functions unregisters all connectors from sysfs and other places so
1108 * that userspace can no longer access them. Drivers should call this as the
1109 * first step tearing down the device instace, or when the underlying
1110 * physical device disappeared (e.g. USB unplug), right before calling
1111 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001112 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001113void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001114{
1115 struct drm_connector *connector;
1116
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001117 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001118 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001119 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001120}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001121EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001122
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001123/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001124 * drm_encoder_init - Init a preallocated encoder
1125 * @dev: drm device
1126 * @encoder: the encoder to init
1127 * @funcs: callbacks for this encoder
1128 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001129 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001130 *
1131 * Initialises a preallocated encoder. Encoder should be
1132 * subclassed as part of driver encoder objects.
1133 *
1134 * Returns:
1135 * Zero on success, error code on failure.
1136 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001137int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001138 struct drm_encoder *encoder,
1139 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001140 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001141{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001142 int ret;
1143
Daniel Vetter84849902012-12-02 00:28:11 +01001144 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001145
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001146 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1147 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001148 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001149
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001150 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001151 encoder->encoder_type = encoder_type;
1152 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001153 if (name) {
1154 va_list ap;
1155
1156 va_start(ap, name);
1157 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1158 va_end(ap);
1159 } else {
1160 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1161 drm_encoder_enum_list[encoder_type].name,
1162 encoder->base.id);
1163 }
Jani Nikulae5748942014-05-14 16:58:20 +03001164 if (!encoder->name) {
1165 ret = -ENOMEM;
1166 goto out_put;
1167 }
Dave Airlief453ba02008-11-07 14:05:41 -08001168
1169 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1170 dev->mode_config.num_encoder++;
1171
Jani Nikulae5748942014-05-14 16:58:20 +03001172out_put:
1173 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001174 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001175
1176out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001177 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001178
1179 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001180}
1181EXPORT_SYMBOL(drm_encoder_init);
1182
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001183/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001184 * drm_encoder_index - find the index of a registered encoder
1185 * @encoder: encoder to find index for
1186 *
1187 * Given a registered encoder, return the index of that encoder within a DRM
1188 * device's list of encoders.
1189 */
1190unsigned int drm_encoder_index(struct drm_encoder *encoder)
1191{
1192 unsigned int index = 0;
1193 struct drm_encoder *tmp;
1194
1195 drm_for_each_encoder(tmp, encoder->dev) {
1196 if (tmp == encoder)
1197 return index;
1198
1199 index++;
1200 }
1201
1202 BUG();
1203}
1204EXPORT_SYMBOL(drm_encoder_index);
1205
1206/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001207 * drm_encoder_cleanup - cleans up an initialised encoder
1208 * @encoder: encoder to cleanup
1209 *
1210 * Cleans up the encoder but doesn't free the object.
1211 */
Dave Airlief453ba02008-11-07 14:05:41 -08001212void drm_encoder_cleanup(struct drm_encoder *encoder)
1213{
1214 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001215
Daniel Vetter84849902012-12-02 00:28:11 +01001216 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001217 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001218 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001219 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001220 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001221 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001222
1223 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001224}
1225EXPORT_SYMBOL(drm_encoder_cleanup);
1226
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001227static unsigned int drm_num_planes(struct drm_device *dev)
1228{
1229 unsigned int num = 0;
1230 struct drm_plane *tmp;
1231
1232 drm_for_each_plane(tmp, dev) {
1233 num++;
1234 }
1235
1236 return num;
1237}
1238
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001239/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001240 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001241 * @dev: DRM device
1242 * @plane: plane object to init
1243 * @possible_crtcs: bitmask of possible CRTCs
1244 * @funcs: callbacks for the new plane
1245 * @formats: array of supported formats (%DRM_FORMAT_*)
1246 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001247 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001248 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001249 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001250 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001251 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001252 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001253 * Zero on success, error code on failure.
1254 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001255int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1256 unsigned long possible_crtcs,
1257 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001258 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001259 enum drm_plane_type type,
1260 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001261{
Rob Clark6b4959f2014-12-18 16:01:53 -05001262 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001263 int ret;
1264
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001265 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1266 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001267 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001268
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001269 drm_modeset_lock_init(&plane->mutex);
1270
Rob Clark4d939142012-05-17 02:23:27 -06001271 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001272 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001273 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001274 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1275 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001276 if (!plane->format_types) {
1277 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001278 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001279 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001280 }
1281
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001282 if (name) {
1283 va_list ap;
1284
1285 va_start(ap, name);
1286 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1287 va_end(ap);
1288 } else {
1289 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1290 drm_num_planes(dev));
1291 }
1292 if (!plane->name) {
1293 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001294 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001295 return -ENOMEM;
1296 }
1297
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001298 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001299 plane->format_count = format_count;
1300 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001301 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001302
Rob Clark6b4959f2014-12-18 16:01:53 -05001303 list_add_tail(&plane->head, &config->plane_list);
1304 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001305 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001306 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001307
Rob Clark9922ab52014-04-01 20:16:57 -04001308 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001309 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001310 plane->type);
1311
Rob Clark6b4959f2014-12-18 16:01:53 -05001312 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1313 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1314 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1315 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1316 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1317 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1318 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1319 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1320 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1321 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1322 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1323 }
1324
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001325 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001326}
Matt Roperdc415ff2014-04-01 15:22:36 -07001327EXPORT_SYMBOL(drm_universal_plane_init);
1328
1329/**
1330 * drm_plane_init - Initialize a legacy plane
1331 * @dev: DRM device
1332 * @plane: plane object to init
1333 * @possible_crtcs: bitmask of possible CRTCs
1334 * @funcs: callbacks for the new plane
1335 * @formats: array of supported formats (%DRM_FORMAT_*)
1336 * @format_count: number of elements in @formats
1337 * @is_primary: plane type (primary vs overlay)
1338 *
1339 * Legacy API to initialize a DRM plane.
1340 *
1341 * New drivers should call drm_universal_plane_init() instead.
1342 *
1343 * Returns:
1344 * Zero on success, error code on failure.
1345 */
1346int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1347 unsigned long possible_crtcs,
1348 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001349 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001350 bool is_primary)
1351{
1352 enum drm_plane_type type;
1353
1354 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1355 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001356 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001357}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001358EXPORT_SYMBOL(drm_plane_init);
1359
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001360/**
1361 * drm_plane_cleanup - Clean up the core plane usage
1362 * @plane: plane to cleanup
1363 *
1364 * This function cleans up @plane and removes it from the DRM mode setting
1365 * core. Note that the function does *not* free the plane structure itself,
1366 * this is the responsibility of the caller.
1367 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001368void drm_plane_cleanup(struct drm_plane *plane)
1369{
1370 struct drm_device *dev = plane->dev;
1371
Daniel Vetter84849902012-12-02 00:28:11 +01001372 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001373 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001374 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001375
1376 BUG_ON(list_empty(&plane->head));
1377
1378 list_del(&plane->head);
1379 dev->mode_config.num_total_plane--;
1380 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1381 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001382 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001383
1384 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1385 if (plane->state && plane->funcs->atomic_destroy_state)
1386 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001387
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001388 kfree(plane->name);
1389
Thierry Redinga18c0af2014-12-10 11:38:49 +01001390 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001391}
1392EXPORT_SYMBOL(drm_plane_cleanup);
1393
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001394/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001395 * drm_plane_index - find the index of a registered plane
1396 * @plane: plane to find index for
1397 *
1398 * Given a registered plane, return the index of that CRTC within a DRM
1399 * device's list of planes.
1400 */
1401unsigned int drm_plane_index(struct drm_plane *plane)
1402{
1403 unsigned int index = 0;
1404 struct drm_plane *tmp;
1405
Daniel Vettere4f62542015-07-09 23:44:35 +02001406 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001407 if (tmp == plane)
1408 return index;
1409
1410 index++;
1411 }
1412
1413 BUG();
1414}
1415EXPORT_SYMBOL(drm_plane_index);
1416
1417/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001418 * drm_plane_from_index - find the registered plane at an index
1419 * @dev: DRM device
1420 * @idx: index of registered plane to find for
1421 *
1422 * Given a plane index, return the registered plane from DRM device's
1423 * list of planes with matching index.
1424 */
1425struct drm_plane *
1426drm_plane_from_index(struct drm_device *dev, int idx)
1427{
1428 struct drm_plane *plane;
1429 unsigned int i = 0;
1430
Daniel Vetter6295d602015-07-09 23:44:25 +02001431 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001432 if (i == idx)
1433 return plane;
1434 i++;
1435 }
1436 return NULL;
1437}
1438EXPORT_SYMBOL(drm_plane_from_index);
1439
1440/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001441 * drm_plane_force_disable - Forcibly disable a plane
1442 * @plane: plane to disable
1443 *
1444 * Forces the plane to be disabled.
1445 *
1446 * Used when the plane's current framebuffer is destroyed,
1447 * and when restoring fbdev mode.
1448 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001449void drm_plane_force_disable(struct drm_plane *plane)
1450{
1451 int ret;
1452
Daniel Vetter3d30a592014-07-27 13:42:42 +02001453 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001454 return;
1455
Daniel Vetter3d30a592014-07-27 13:42:42 +02001456 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001457 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001458 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001459 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001460 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001461 return;
1462 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001463 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001464 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001465 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001466 plane->fb = NULL;
1467 plane->crtc = NULL;
1468}
1469EXPORT_SYMBOL(drm_plane_force_disable);
1470
Rob Clark6b4959f2014-12-18 16:01:53 -05001471static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001472{
Rob Clark356af0e2014-12-18 16:01:52 -05001473 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001474
1475 /*
1476 * Standard properties (apply to all connectors)
1477 */
Rob Clark356af0e2014-12-18 16:01:52 -05001478 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001479 DRM_MODE_PROP_IMMUTABLE,
1480 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001481 if (!prop)
1482 return -ENOMEM;
1483 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001484
Rob Clark356af0e2014-12-18 16:01:52 -05001485 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001486 "DPMS", drm_dpms_enum_list,
1487 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001488 if (!prop)
1489 return -ENOMEM;
1490 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001491
Rob Clark356af0e2014-12-18 16:01:52 -05001492 prop = drm_property_create(dev,
1493 DRM_MODE_PROP_BLOB |
1494 DRM_MODE_PROP_IMMUTABLE,
1495 "PATH", 0);
1496 if (!prop)
1497 return -ENOMEM;
1498 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001499
Rob Clark356af0e2014-12-18 16:01:52 -05001500 prop = drm_property_create(dev,
1501 DRM_MODE_PROP_BLOB |
1502 DRM_MODE_PROP_IMMUTABLE,
1503 "TILE", 0);
1504 if (!prop)
1505 return -ENOMEM;
1506 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001507
Rob Clark6b4959f2014-12-18 16:01:53 -05001508 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001509 "type", drm_plane_type_enum_list,
1510 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001511 if (!prop)
1512 return -ENOMEM;
1513 dev->mode_config.plane_type_property = prop;
1514
1515 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1516 "SRC_X", 0, UINT_MAX);
1517 if (!prop)
1518 return -ENOMEM;
1519 dev->mode_config.prop_src_x = prop;
1520
1521 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1522 "SRC_Y", 0, UINT_MAX);
1523 if (!prop)
1524 return -ENOMEM;
1525 dev->mode_config.prop_src_y = prop;
1526
1527 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1528 "SRC_W", 0, UINT_MAX);
1529 if (!prop)
1530 return -ENOMEM;
1531 dev->mode_config.prop_src_w = prop;
1532
1533 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1534 "SRC_H", 0, UINT_MAX);
1535 if (!prop)
1536 return -ENOMEM;
1537 dev->mode_config.prop_src_h = prop;
1538
1539 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1540 "CRTC_X", INT_MIN, INT_MAX);
1541 if (!prop)
1542 return -ENOMEM;
1543 dev->mode_config.prop_crtc_x = prop;
1544
1545 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1546 "CRTC_Y", INT_MIN, INT_MAX);
1547 if (!prop)
1548 return -ENOMEM;
1549 dev->mode_config.prop_crtc_y = prop;
1550
1551 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1552 "CRTC_W", 0, INT_MAX);
1553 if (!prop)
1554 return -ENOMEM;
1555 dev->mode_config.prop_crtc_w = prop;
1556
1557 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1558 "CRTC_H", 0, INT_MAX);
1559 if (!prop)
1560 return -ENOMEM;
1561 dev->mode_config.prop_crtc_h = prop;
1562
1563 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1564 "FB_ID", DRM_MODE_OBJECT_FB);
1565 if (!prop)
1566 return -ENOMEM;
1567 dev->mode_config.prop_fb_id = prop;
1568
1569 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1570 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1571 if (!prop)
1572 return -ENOMEM;
1573 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001574
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001575 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1576 "ACTIVE");
1577 if (!prop)
1578 return -ENOMEM;
1579 dev->mode_config.prop_active = prop;
1580
Daniel Stone955f3c32015-05-25 19:11:52 +01001581 prop = drm_property_create(dev,
1582 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1583 "MODE_ID", 0);
1584 if (!prop)
1585 return -ENOMEM;
1586 dev->mode_config.prop_mode_id = prop;
1587
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001588 prop = drm_property_create(dev,
1589 DRM_MODE_PROP_BLOB,
1590 "DEGAMMA_LUT", 0);
1591 if (!prop)
1592 return -ENOMEM;
1593 dev->mode_config.degamma_lut_property = prop;
1594
1595 prop = drm_property_create_range(dev,
1596 DRM_MODE_PROP_IMMUTABLE,
1597 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1598 if (!prop)
1599 return -ENOMEM;
1600 dev->mode_config.degamma_lut_size_property = prop;
1601
1602 prop = drm_property_create(dev,
1603 DRM_MODE_PROP_BLOB,
1604 "CTM", 0);
1605 if (!prop)
1606 return -ENOMEM;
1607 dev->mode_config.ctm_property = prop;
1608
1609 prop = drm_property_create(dev,
1610 DRM_MODE_PROP_BLOB,
1611 "GAMMA_LUT", 0);
1612 if (!prop)
1613 return -ENOMEM;
1614 dev->mode_config.gamma_lut_property = prop;
1615
1616 prop = drm_property_create_range(dev,
1617 DRM_MODE_PROP_IMMUTABLE,
1618 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1619 if (!prop)
1620 return -ENOMEM;
1621 dev->mode_config.gamma_lut_size_property = prop;
1622
Rob Clark9922ab52014-04-01 20:16:57 -04001623 return 0;
1624}
1625
Dave Airlief453ba02008-11-07 14:05:41 -08001626/**
1627 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1628 * @dev: DRM device
1629 *
1630 * Called by a driver the first time a DVI-I connector is made.
1631 */
1632int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1633{
1634 struct drm_property *dvi_i_selector;
1635 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001636
1637 if (dev->mode_config.dvi_i_select_subconnector_property)
1638 return 0;
1639
1640 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001641 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001642 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001643 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001644 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001645 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1646
Sascha Hauer4a67d392012-02-06 10:58:17 +01001647 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001648 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001649 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001650 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001651 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1652
1653 return 0;
1654}
1655EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1656
1657/**
1658 * drm_create_tv_properties - create TV specific connector properties
1659 * @dev: DRM device
1660 * @num_modes: number of different TV formats (modes) supported
1661 * @modes: array of pointers to strings containing name of each format
1662 *
1663 * Called by a driver's TV initialization routine, this function creates
1664 * the TV specific connector properties for a given device. Caller is
1665 * responsible for allocating a list of format names and passing them to
1666 * this routine.
1667 */
Thierry Reding2f763312014-10-13 12:45:57 +02001668int drm_mode_create_tv_properties(struct drm_device *dev,
1669 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001670 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001671{
1672 struct drm_property *tv_selector;
1673 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001674 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001675
1676 if (dev->mode_config.tv_select_subconnector_property)
1677 return 0;
1678
1679 /*
1680 * Basic connector properties
1681 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001682 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001683 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001684 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001685 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001686 if (!tv_selector)
1687 goto nomem;
1688
Dave Airlief453ba02008-11-07 14:05:41 -08001689 dev->mode_config.tv_select_subconnector_property = tv_selector;
1690
1691 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001692 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1693 "subconnector",
1694 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001695 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001696 if (!tv_subconnector)
1697 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001698 dev->mode_config.tv_subconnector_property = tv_subconnector;
1699
1700 /*
1701 * Other, TV specific properties: margins & TV modes.
1702 */
1703 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001704 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001705 if (!dev->mode_config.tv_left_margin_property)
1706 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001707
1708 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001709 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001710 if (!dev->mode_config.tv_right_margin_property)
1711 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001712
1713 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001714 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001715 if (!dev->mode_config.tv_top_margin_property)
1716 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001717
1718 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001719 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001720 if (!dev->mode_config.tv_bottom_margin_property)
1721 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001722
1723 dev->mode_config.tv_mode_property =
1724 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1725 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001726 if (!dev->mode_config.tv_mode_property)
1727 goto nomem;
1728
Dave Airlief453ba02008-11-07 14:05:41 -08001729 for (i = 0; i < num_modes; i++)
1730 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1731 i, modes[i]);
1732
Francisco Jerezb6b79022009-08-02 04:19:20 +02001733 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001734 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001735 if (!dev->mode_config.tv_brightness_property)
1736 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001737
1738 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001739 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001740 if (!dev->mode_config.tv_contrast_property)
1741 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001742
1743 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001744 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001745 if (!dev->mode_config.tv_flicker_reduction_property)
1746 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001747
Francisco Jereza75f0232009-08-12 02:30:10 +02001748 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001749 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001750 if (!dev->mode_config.tv_overscan_property)
1751 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001752
1753 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001754 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001755 if (!dev->mode_config.tv_saturation_property)
1756 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001757
1758 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001759 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001760 if (!dev->mode_config.tv_hue_property)
1761 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001762
Dave Airlief453ba02008-11-07 14:05:41 -08001763 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001764nomem:
1765 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001766}
1767EXPORT_SYMBOL(drm_mode_create_tv_properties);
1768
1769/**
1770 * drm_mode_create_scaling_mode_property - create scaling mode property
1771 * @dev: DRM device
1772 *
1773 * Called by a driver the first time it's needed, must be attached to desired
1774 * connectors.
1775 */
1776int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1777{
1778 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001779
1780 if (dev->mode_config.scaling_mode_property)
1781 return 0;
1782
1783 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001784 drm_property_create_enum(dev, 0, "scaling mode",
1785 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001786 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001787
1788 dev->mode_config.scaling_mode_property = scaling_mode;
1789
1790 return 0;
1791}
1792EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1793
1794/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301795 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1796 * @dev: DRM device
1797 *
1798 * Called by a driver the first time it's needed, must be attached to desired
1799 * connectors.
1800 *
1801 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001802 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301803 */
1804int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1805{
1806 if (dev->mode_config.aspect_ratio_property)
1807 return 0;
1808
1809 dev->mode_config.aspect_ratio_property =
1810 drm_property_create_enum(dev, 0, "aspect ratio",
1811 drm_aspect_ratio_enum_list,
1812 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1813
1814 if (dev->mode_config.aspect_ratio_property == NULL)
1815 return -ENOMEM;
1816
1817 return 0;
1818}
1819EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1820
1821/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001822 * drm_mode_create_dirty_property - create dirty property
1823 * @dev: DRM device
1824 *
1825 * Called by a driver the first time it's needed, must be attached to desired
1826 * connectors.
1827 */
1828int drm_mode_create_dirty_info_property(struct drm_device *dev)
1829{
1830 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001831
1832 if (dev->mode_config.dirty_info_property)
1833 return 0;
1834
1835 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001836 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001837 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001838 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001839 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001840 dev->mode_config.dirty_info_property = dirty_info;
1841
1842 return 0;
1843}
1844EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1845
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001846/**
1847 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1848 * @dev: DRM device
1849 *
1850 * Create the the suggested x/y offset property for connectors.
1851 */
1852int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1853{
1854 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1855 return 0;
1856
1857 dev->mode_config.suggested_x_property =
1858 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1859
1860 dev->mode_config.suggested_y_property =
1861 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1862
1863 if (dev->mode_config.suggested_x_property == NULL ||
1864 dev->mode_config.suggested_y_property == NULL)
1865 return -ENOMEM;
1866 return 0;
1867}
1868EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1869
Dave Airlief453ba02008-11-07 14:05:41 -08001870/**
Dave Airlief453ba02008-11-07 14:05:41 -08001871 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001872 * @dev: drm device for the ioctl
1873 * @data: data pointer for the ioctl
1874 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001875 *
Dave Airlief453ba02008-11-07 14:05:41 -08001876 * Construct a set of configuration description structures and return
1877 * them to the user, including CRTC, connector and framebuffer configuration.
1878 *
1879 * Called by the user via ioctl.
1880 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001881 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001882 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001883 */
1884int drm_mode_getresources(struct drm_device *dev, void *data,
1885 struct drm_file *file_priv)
1886{
1887 struct drm_mode_card_res *card_res = data;
1888 struct list_head *lh;
1889 struct drm_framebuffer *fb;
1890 struct drm_connector *connector;
1891 struct drm_crtc *crtc;
1892 struct drm_encoder *encoder;
1893 int ret = 0;
1894 int connector_count = 0;
1895 int crtc_count = 0;
1896 int fb_count = 0;
1897 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001898 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001899 uint32_t __user *fb_id;
1900 uint32_t __user *crtc_id;
1901 uint32_t __user *connector_id;
1902 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001903
Dave Airliefb3b06c2011-02-08 13:55:21 +10001904 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1905 return -EINVAL;
1906
Dave Airlief453ba02008-11-07 14:05:41 -08001907
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001908 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001909 /*
1910 * For the non-control nodes we need to limit the list of resources
1911 * by IDs in the group list for this node
1912 */
1913 list_for_each(lh, &file_priv->fbs)
1914 fb_count++;
1915
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001916 /* handle this in 4 parts */
1917 /* FBs */
1918 if (card_res->count_fbs >= fb_count) {
1919 copied = 0;
1920 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1921 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1922 if (put_user(fb->base.id, fb_id + copied)) {
1923 mutex_unlock(&file_priv->fbs_lock);
1924 return -EFAULT;
1925 }
1926 copied++;
1927 }
1928 }
1929 card_res->count_fbs = fb_count;
1930 mutex_unlock(&file_priv->fbs_lock);
1931
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001932 /* mode_config.mutex protects the connector list against e.g. DP MST
1933 * connector hot-adding. CRTC/Plane lists are invariant. */
1934 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001935 drm_for_each_crtc(crtc, dev)
1936 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001937
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001938 drm_for_each_connector(connector, dev)
1939 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001940
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001941 drm_for_each_encoder(encoder, dev)
1942 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001943
1944 card_res->max_height = dev->mode_config.max_height;
1945 card_res->min_height = dev->mode_config.min_height;
1946 card_res->max_width = dev->mode_config.max_width;
1947 card_res->min_width = dev->mode_config.min_width;
1948
Dave Airlief453ba02008-11-07 14:05:41 -08001949 /* CRTCs */
1950 if (card_res->count_crtcs >= crtc_count) {
1951 copied = 0;
1952 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001953 drm_for_each_crtc(crtc, dev) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001954 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1955 crtc->base.id, crtc->name);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001956 if (put_user(crtc->base.id, crtc_id + copied)) {
1957 ret = -EFAULT;
1958 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001959 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001960 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001961 }
1962 }
1963 card_res->count_crtcs = crtc_count;
1964
1965 /* Encoders */
1966 if (card_res->count_encoders >= encoder_count) {
1967 copied = 0;
1968 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001969 drm_for_each_encoder(encoder, dev) {
1970 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1971 encoder->name);
1972 if (put_user(encoder->base.id, encoder_id +
1973 copied)) {
1974 ret = -EFAULT;
1975 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001976 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001977 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001978 }
1979 }
1980 card_res->count_encoders = encoder_count;
1981
1982 /* Connectors */
1983 if (card_res->count_connectors >= connector_count) {
1984 copied = 0;
1985 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001986 drm_for_each_connector(connector, dev) {
1987 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1988 connector->base.id,
1989 connector->name);
1990 if (put_user(connector->base.id,
1991 connector_id + copied)) {
1992 ret = -EFAULT;
1993 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001994 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001995 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001996 }
1997 }
1998 card_res->count_connectors = connector_count;
1999
Jerome Glisse94401062010-07-15 15:43:25 -04002000 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08002001 card_res->count_connectors, card_res->count_encoders);
2002
2003out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002004 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002005 return ret;
2006}
2007
2008/**
2009 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002010 * @dev: drm device for the ioctl
2011 * @data: data pointer for the ioctl
2012 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002013 *
Dave Airlief453ba02008-11-07 14:05:41 -08002014 * Construct a CRTC configuration structure to return to the user.
2015 *
2016 * Called by the user via ioctl.
2017 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002018 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002019 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002020 */
2021int drm_mode_getcrtc(struct drm_device *dev,
2022 void *data, struct drm_file *file_priv)
2023{
2024 struct drm_mode_crtc *crtc_resp = data;
2025 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002026
Dave Airliefb3b06c2011-02-08 13:55:21 +10002027 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2028 return -EINVAL;
2029
Rob Clarka2b34e22013-10-05 16:36:52 -04002030 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002031 if (!crtc)
2032 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002033
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002034 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002035 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002036 if (crtc->primary->fb)
2037 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002038 else
2039 crtc_resp->fb_id = 0;
2040
Daniel Vetter31c946e2015-02-22 12:24:17 +01002041 if (crtc->state) {
2042 crtc_resp->x = crtc->primary->state->src_x >> 16;
2043 crtc_resp->y = crtc->primary->state->src_y >> 16;
2044 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002045 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002046 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002047
Daniel Vetter31c946e2015-02-22 12:24:17 +01002048 } else {
2049 crtc_resp->mode_valid = 0;
2050 }
Dave Airlief453ba02008-11-07 14:05:41 -08002051 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002052 crtc_resp->x = crtc->x;
2053 crtc_resp->y = crtc->y;
2054 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002055 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002056 crtc_resp->mode_valid = 1;
2057
2058 } else {
2059 crtc_resp->mode_valid = 0;
2060 }
Dave Airlief453ba02008-11-07 14:05:41 -08002061 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002062 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002063
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002064 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002065}
2066
Damien Lespiau61d8e322013-09-25 16:45:22 +01002067static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2068 const struct drm_file *file_priv)
2069{
2070 /*
2071 * If user-space hasn't configured the driver to expose the stereo 3D
2072 * modes, don't expose them.
2073 */
2074 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2075 return false;
2076
2077 return true;
2078}
2079
Daniel Vetterabd69c52014-11-25 23:50:05 +01002080static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2081{
2082 /* For atomic drivers only state objects are synchronously updated and
2083 * protected by modeset locks, so check those first. */
2084 if (connector->state)
2085 return connector->state->best_encoder;
2086 return connector->encoder;
2087}
2088
Rob Clark95cbf112014-12-18 16:01:49 -05002089/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002090static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002091 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2092 uint32_t *arg_count_props)
2093{
Rob Clark88a48e22014-12-18 16:01:50 -05002094 int props_count;
2095 int i, ret, copied;
2096
2097 props_count = obj->properties->count;
2098 if (!atomic)
2099 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002100
2101 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002102 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002103 struct drm_property *prop = obj->properties->properties[i];
2104 uint64_t val;
2105
Rob Clark88a48e22014-12-18 16:01:50 -05002106 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2107 continue;
2108
Rob Clark95cbf112014-12-18 16:01:49 -05002109 ret = drm_object_property_get_value(obj, prop, &val);
2110 if (ret)
2111 return ret;
2112
2113 if (put_user(prop->base.id, prop_ptr + copied))
2114 return -EFAULT;
2115
2116 if (put_user(val, prop_values + copied))
2117 return -EFAULT;
2118
2119 copied++;
2120 }
2121 }
2122 *arg_count_props = props_count;
2123
2124 return 0;
2125}
2126
Dave Airlief453ba02008-11-07 14:05:41 -08002127/**
2128 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002129 * @dev: drm device for the ioctl
2130 * @data: data pointer for the ioctl
2131 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002132 *
Dave Airlief453ba02008-11-07 14:05:41 -08002133 * Construct a connector configuration structure to return to the user.
2134 *
2135 * Called by the user via ioctl.
2136 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002137 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002138 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002139 */
2140int drm_mode_getconnector(struct drm_device *dev, void *data,
2141 struct drm_file *file_priv)
2142{
2143 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002144 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002145 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002146 struct drm_display_mode *mode;
2147 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002148 int encoders_count = 0;
2149 int ret = 0;
2150 int copied = 0;
2151 int i;
2152 struct drm_mode_modeinfo u_mode;
2153 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002154 uint32_t __user *encoder_ptr;
2155
Dave Airliefb3b06c2011-02-08 13:55:21 +10002156 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2157 return -EINVAL;
2158
Dave Airlief453ba02008-11-07 14:05:41 -08002159 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2160
Jerome Glisse94401062010-07-15 15:43:25 -04002161 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002162
Daniel Vetter7b240562012-12-12 00:35:33 +01002163 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002164
Rob Clarka2b34e22013-10-05 16:36:52 -04002165 connector = drm_connector_find(dev, out_resp->connector_id);
2166 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002167 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002168 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002169 }
Dave Airlief453ba02008-11-07 14:05:41 -08002170
Thierry Reding01073b02014-12-10 13:03:38 +01002171 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2172 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002173 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002174
2175 if (out_resp->count_modes == 0) {
2176 connector->funcs->fill_modes(connector,
2177 dev->mode_config.max_width,
2178 dev->mode_config.max_height);
2179 }
2180
2181 /* delayed so we get modes regardless of pre-fill_modes state */
2182 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002183 if (drm_mode_expose_to_userspace(mode, file_priv))
2184 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002185
2186 out_resp->connector_id = connector->base.id;
2187 out_resp->connector_type = connector->connector_type;
2188 out_resp->connector_type_id = connector->connector_type_id;
2189 out_resp->mm_width = connector->display_info.width_mm;
2190 out_resp->mm_height = connector->display_info.height_mm;
2191 out_resp->subpixel = connector->display_info.subpixel_order;
2192 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002193
2194 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002195 encoder = drm_connector_get_encoder(connector);
2196 if (encoder)
2197 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002198 else
2199 out_resp->encoder_id = 0;
2200
2201 /*
2202 * This ioctl is called twice, once to determine how much space is
2203 * needed, and the 2nd time to fill it.
2204 */
2205 if ((out_resp->count_modes >= mode_count) && mode_count) {
2206 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002207 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002208 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002209 if (!drm_mode_expose_to_userspace(mode, file_priv))
2210 continue;
2211
Daniel Stone934a8a82015-05-22 13:34:48 +01002212 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002213 if (copy_to_user(mode_ptr + copied,
2214 &u_mode, sizeof(u_mode))) {
2215 ret = -EFAULT;
2216 goto out;
2217 }
2218 copied++;
2219 }
2220 }
2221 out_resp->count_modes = mode_count;
2222
Rob Clark88a48e22014-12-18 16:01:50 -05002223 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002224 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2225 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2226 &out_resp->count_props);
2227 if (ret)
2228 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002229
2230 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2231 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002232 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002233 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2234 if (connector->encoder_ids[i] != 0) {
2235 if (put_user(connector->encoder_ids[i],
2236 encoder_ptr + copied)) {
2237 ret = -EFAULT;
2238 goto out;
2239 }
2240 copied++;
2241 }
2242 }
2243 }
2244 out_resp->count_encoders = encoders_count;
2245
2246out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002247 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002248
2249out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002250 mutex_unlock(&dev->mode_config.mutex);
2251
Dave Airlief453ba02008-11-07 14:05:41 -08002252 return ret;
2253}
2254
Daniel Vetterabd69c52014-11-25 23:50:05 +01002255static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2256{
2257 struct drm_connector *connector;
2258 struct drm_device *dev = encoder->dev;
2259 bool uses_atomic = false;
2260
2261 /* For atomic drivers only state objects are synchronously updated and
2262 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002263 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002264 if (!connector->state)
2265 continue;
2266
2267 uses_atomic = true;
2268
2269 if (connector->state->best_encoder != encoder)
2270 continue;
2271
2272 return connector->state->crtc;
2273 }
2274
2275 /* Don't return stale data (e.g. pending async disable). */
2276 if (uses_atomic)
2277 return NULL;
2278
2279 return encoder->crtc;
2280}
2281
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002282/**
2283 * drm_mode_getencoder - get encoder configuration
2284 * @dev: drm device for the ioctl
2285 * @data: data pointer for the ioctl
2286 * @file_priv: drm file for the ioctl call
2287 *
2288 * Construct a encoder configuration structure to return to the user.
2289 *
2290 * Called by the user via ioctl.
2291 *
2292 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002293 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002294 */
Dave Airlief453ba02008-11-07 14:05:41 -08002295int drm_mode_getencoder(struct drm_device *dev, void *data,
2296 struct drm_file *file_priv)
2297{
2298 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002299 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002300 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002301
Dave Airliefb3b06c2011-02-08 13:55:21 +10002302 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2303 return -EINVAL;
2304
Rob Clarka2b34e22013-10-05 16:36:52 -04002305 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002306 if (!encoder)
2307 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002308
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002309 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002310 crtc = drm_encoder_get_crtc(encoder);
2311 if (crtc)
2312 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002313 else
2314 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002315 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2316
Dave Airlief453ba02008-11-07 14:05:41 -08002317 enc_resp->encoder_type = encoder->encoder_type;
2318 enc_resp->encoder_id = encoder->base.id;
2319 enc_resp->possible_crtcs = encoder->possible_crtcs;
2320 enc_resp->possible_clones = encoder->possible_clones;
2321
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002322 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002323}
2324
2325/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002326 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002327 * @dev: DRM device
2328 * @data: ioctl data
2329 * @file_priv: DRM file info
2330 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002331 * Construct a list of plane ids to return to the user.
2332 *
2333 * Called by the user via ioctl.
2334 *
2335 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002336 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002337 */
2338int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002339 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002340{
2341 struct drm_mode_get_plane_res *plane_resp = data;
2342 struct drm_mode_config *config;
2343 struct drm_plane *plane;
2344 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002345 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002346 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002347
2348 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2349 return -EINVAL;
2350
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002351 config = &dev->mode_config;
2352
Matt Roper681e7ec2014-04-01 15:22:42 -07002353 if (file_priv->universal_planes)
2354 num_planes = config->num_total_plane;
2355 else
2356 num_planes = config->num_overlay_plane;
2357
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002358 /*
2359 * This ioctl is called twice, once to determine how much space is
2360 * needed, and the 2nd time to fill it.
2361 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002362 if (num_planes &&
2363 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002364 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002365
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002366 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002367 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002368 /*
2369 * Unless userspace set the 'universal planes'
2370 * capability bit, only advertise overlays.
2371 */
2372 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2373 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002374 continue;
2375
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002376 if (put_user(plane->base.id, plane_ptr + copied))
2377 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002378 copied++;
2379 }
2380 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002381 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002382
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002383 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002384}
2385
2386/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002387 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002388 * @dev: DRM device
2389 * @data: ioctl data
2390 * @file_priv: DRM file info
2391 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002392 * Construct a plane configuration structure to return to the user.
2393 *
2394 * Called by the user via ioctl.
2395 *
2396 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002397 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002398 */
2399int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002400 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002401{
2402 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002403 struct drm_plane *plane;
2404 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002405
2406 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2407 return -EINVAL;
2408
Rob Clarka2b34e22013-10-05 16:36:52 -04002409 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002410 if (!plane)
2411 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002412
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002413 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002414 if (plane->crtc)
2415 plane_resp->crtc_id = plane->crtc->base.id;
2416 else
2417 plane_resp->crtc_id = 0;
2418
2419 if (plane->fb)
2420 plane_resp->fb_id = plane->fb->base.id;
2421 else
2422 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002423 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002424
2425 plane_resp->plane_id = plane->base.id;
2426 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002427 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002428
2429 /*
2430 * This ioctl is called twice, once to determine how much space is
2431 * needed, and the 2nd time to fill it.
2432 */
2433 if (plane->format_count &&
2434 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002435 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002436 if (copy_to_user(format_ptr,
2437 plane->format_types,
2438 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002439 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002440 }
2441 }
2442 plane_resp->count_format_types = plane->format_count;
2443
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002444 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002445}
2446
Laurent Pinchartead86102015-03-05 02:25:43 +02002447/**
2448 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2449 * @plane: plane to check for format support
2450 * @format: the pixel format
2451 *
2452 * Returns:
2453 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2454 * otherwise.
2455 */
2456int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2457{
2458 unsigned int i;
2459
2460 for (i = 0; i < plane->format_count; i++) {
2461 if (format == plane->format_types[i])
2462 return 0;
2463 }
2464
2465 return -EINVAL;
2466}
2467
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002468static int check_src_coords(uint32_t src_x, uint32_t src_y,
2469 uint32_t src_w, uint32_t src_h,
2470 const struct drm_framebuffer *fb)
2471{
2472 unsigned int fb_width, fb_height;
2473
2474 fb_width = fb->width << 16;
2475 fb_height = fb->height << 16;
2476
2477 /* Make sure source coordinates are inside the fb. */
2478 if (src_w > fb_width ||
2479 src_x > fb_width - src_w ||
2480 src_h > fb_height ||
2481 src_y > fb_height - src_h) {
2482 DRM_DEBUG_KMS("Invalid source coordinates "
2483 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2484 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2485 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2486 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2487 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2488 return -ENOSPC;
2489 }
2490
2491 return 0;
2492}
2493
Matt Roperb36552b2014-06-10 08:28:09 -07002494/*
2495 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002496 *
Matt Roperb36552b2014-06-10 08:28:09 -07002497 * Note that we assume an extra reference has already been taken on fb. If the
2498 * update fails, this reference will be dropped before return; if it succeeds,
2499 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002500 *
Matt Roperb36552b2014-06-10 08:28:09 -07002501 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002502 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002503static int __setplane_internal(struct drm_plane *plane,
2504 struct drm_crtc *crtc,
2505 struct drm_framebuffer *fb,
2506 int32_t crtc_x, int32_t crtc_y,
2507 uint32_t crtc_w, uint32_t crtc_h,
2508 /* src_{x,y,w,h} values are 16.16 fixed point */
2509 uint32_t src_x, uint32_t src_y,
2510 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002511{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002512 int ret = 0;
2513
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002514 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002515 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002516 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002517 ret = plane->funcs->disable_plane(plane);
2518 if (!ret) {
2519 plane->crtc = NULL;
2520 plane->fb = NULL;
2521 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002522 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002523 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002524 goto out;
2525 }
2526
Matt Roper7f994f32014-05-29 08:06:51 -07002527 /* Check whether this plane is usable on this CRTC */
2528 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2529 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2530 ret = -EINVAL;
2531 goto out;
2532 }
2533
Ville Syrjälä62443be2011-12-20 00:06:47 +02002534 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002535 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2536 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002537 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2538 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002539 goto out;
2540 }
2541
Matt Roper3968be92015-04-13 11:06:13 -07002542 /* Give drivers some help against integer overflows */
2543 if (crtc_w > INT_MAX ||
2544 crtc_x > INT_MAX - (int32_t) crtc_w ||
2545 crtc_h > INT_MAX ||
2546 crtc_y > INT_MAX - (int32_t) crtc_h) {
2547 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2548 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002549 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002550 goto out;
2551 }
2552
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002553 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2554 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002555 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002556
Daniel Vetter3d30a592014-07-27 13:42:42 +02002557 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002558 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002559 crtc_x, crtc_y, crtc_w, crtc_h,
2560 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002561 if (!ret) {
2562 plane->crtc = crtc;
2563 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002564 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002565 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002566 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002567 }
2568
2569out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002570 if (fb)
2571 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002572 if (plane->old_fb)
2573 drm_framebuffer_unreference(plane->old_fb);
2574 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002575
2576 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002577}
Matt Roperb36552b2014-06-10 08:28:09 -07002578
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002579static int setplane_internal(struct drm_plane *plane,
2580 struct drm_crtc *crtc,
2581 struct drm_framebuffer *fb,
2582 int32_t crtc_x, int32_t crtc_y,
2583 uint32_t crtc_w, uint32_t crtc_h,
2584 /* src_{x,y,w,h} values are 16.16 fixed point */
2585 uint32_t src_x, uint32_t src_y,
2586 uint32_t src_w, uint32_t src_h)
2587{
2588 int ret;
2589
2590 drm_modeset_lock_all(plane->dev);
2591 ret = __setplane_internal(plane, crtc, fb,
2592 crtc_x, crtc_y, crtc_w, crtc_h,
2593 src_x, src_y, src_w, src_h);
2594 drm_modeset_unlock_all(plane->dev);
2595
2596 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002597}
2598
2599/**
2600 * drm_mode_setplane - configure a plane's configuration
2601 * @dev: DRM device
2602 * @data: ioctl data*
2603 * @file_priv: DRM file info
2604 *
2605 * Set plane configuration, including placement, fb, scaling, and other factors.
2606 * Or pass a NULL fb to disable (planes may be disabled without providing a
2607 * valid crtc).
2608 *
2609 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002610 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002611 */
2612int drm_mode_setplane(struct drm_device *dev, void *data,
2613 struct drm_file *file_priv)
2614{
2615 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002616 struct drm_plane *plane;
2617 struct drm_crtc *crtc = NULL;
2618 struct drm_framebuffer *fb = NULL;
2619
2620 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2621 return -EINVAL;
2622
Matt Roperb36552b2014-06-10 08:28:09 -07002623 /*
2624 * First, find the plane, crtc, and fb objects. If not available,
2625 * we don't bother to call the driver.
2626 */
Rob Clark933f6222014-11-25 20:33:11 -05002627 plane = drm_plane_find(dev, plane_req->plane_id);
2628 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002629 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2630 plane_req->plane_id);
2631 return -ENOENT;
2632 }
Matt Roperb36552b2014-06-10 08:28:09 -07002633
2634 if (plane_req->fb_id) {
2635 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2636 if (!fb) {
2637 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2638 plane_req->fb_id);
2639 return -ENOENT;
2640 }
2641
Rob Clark933f6222014-11-25 20:33:11 -05002642 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2643 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002644 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2645 plane_req->crtc_id);
2646 return -ENOENT;
2647 }
Matt Roperb36552b2014-06-10 08:28:09 -07002648 }
2649
Matt Roper161d0dc2014-06-10 08:28:10 -07002650 /*
2651 * setplane_internal will take care of deref'ing either the old or new
2652 * framebuffer depending on success.
2653 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002654 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002655 plane_req->crtc_x, plane_req->crtc_y,
2656 plane_req->crtc_w, plane_req->crtc_h,
2657 plane_req->src_x, plane_req->src_y,
2658 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002659}
2660
2661/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002662 * drm_mode_set_config_internal - helper to call ->set_config
2663 * @set: modeset config to set
2664 *
2665 * This is a little helper to wrap internal calls to the ->set_config driver
2666 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002667 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002668 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002669 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002670 */
2671int drm_mode_set_config_internal(struct drm_mode_set *set)
2672{
2673 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002674 struct drm_framebuffer *fb;
2675 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002676 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002677
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002678 /*
2679 * NOTE: ->set_config can also disable other crtcs (if we steal all
2680 * connectors from it), hence we need to refcount the fbs across all
2681 * crtcs. Atomic modeset will have saner semantics ...
2682 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002683 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002684 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002685
Daniel Vetterb0d12322012-12-11 01:07:12 +01002686 fb = set->fb;
2687
2688 ret = crtc->funcs->set_config(set);
2689 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002690 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002691 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002692 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002693
Daniel Vettere4f62542015-07-09 23:44:35 +02002694 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002695 if (tmp->primary->fb)
2696 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002697 if (tmp->primary->old_fb)
2698 drm_framebuffer_unreference(tmp->primary->old_fb);
2699 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002700 }
2701
2702 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002703}
2704EXPORT_SYMBOL(drm_mode_set_config_internal);
2705
Matt Roperaf936292014-04-01 15:22:34 -07002706/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002707 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2708 * @mode: mode to query
2709 * @hdisplay: hdisplay value to fill in
2710 * @vdisplay: vdisplay value to fill in
2711 *
2712 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2713 * the appropriate layout.
2714 */
2715void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2716 int *hdisplay, int *vdisplay)
2717{
2718 struct drm_display_mode adjusted;
2719
2720 drm_mode_copy(&adjusted, mode);
2721 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2722 *hdisplay = adjusted.crtc_hdisplay;
2723 *vdisplay = adjusted.crtc_vdisplay;
2724}
2725EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2726
2727/**
Matt Roperaf936292014-04-01 15:22:34 -07002728 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2729 * CRTC viewport
2730 * @crtc: CRTC that framebuffer will be displayed on
2731 * @x: x panning
2732 * @y: y panning
2733 * @mode: mode that framebuffer will be displayed under
2734 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002735 */
Matt Roperaf936292014-04-01 15:22:34 -07002736int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2737 int x, int y,
2738 const struct drm_display_mode *mode,
2739 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002740
2741{
2742 int hdisplay, vdisplay;
2743
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002744 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002745
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002746 if (crtc->state &&
2747 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2748 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002749 swap(hdisplay, vdisplay);
2750
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002751 return check_src_coords(x << 16, y << 16,
2752 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002753}
Matt Roperaf936292014-04-01 15:22:34 -07002754EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002755
Daniel Vetter2d13b672012-12-11 13:47:23 +01002756/**
Dave Airlief453ba02008-11-07 14:05:41 -08002757 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002758 * @dev: drm device for the ioctl
2759 * @data: data pointer for the ioctl
2760 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002761 *
Dave Airlief453ba02008-11-07 14:05:41 -08002762 * Build a new CRTC configuration based on user request.
2763 *
2764 * Called by the user via ioctl.
2765 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002766 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002767 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002768 */
2769int drm_mode_setcrtc(struct drm_device *dev, void *data,
2770 struct drm_file *file_priv)
2771{
2772 struct drm_mode_config *config = &dev->mode_config;
2773 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002774 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002775 struct drm_connector **connector_set = NULL, *connector;
2776 struct drm_framebuffer *fb = NULL;
2777 struct drm_display_mode *mode = NULL;
2778 struct drm_mode_set set;
2779 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002780 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002781 int i;
2782
Dave Airliefb3b06c2011-02-08 13:55:21 +10002783 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2784 return -EINVAL;
2785
Zhao Junwang01447e92015-07-07 17:08:35 +08002786 /*
2787 * Universal plane src offsets are only 16.16, prevent havoc for
2788 * drivers using universal plane code internally.
2789 */
2790 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002791 return -ERANGE;
2792
Daniel Vetter84849902012-12-02 00:28:11 +01002793 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002794 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2795 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002796 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002797 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002798 goto out;
2799 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002800 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002801
2802 if (crtc_req->mode_valid) {
2803 /* If we have a mode we need a framebuffer. */
2804 /* If we pass -1, set the mode with the currently bound fb */
2805 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002806 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002807 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2808 ret = -EINVAL;
2809 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002810 }
Matt Roperf4510a22014-04-01 15:22:40 -07002811 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002812 /* Make refcounting symmetric with the lookup path. */
2813 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002814 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002815 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2816 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002817 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2818 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002819 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002820 goto out;
2821 }
Dave Airlief453ba02008-11-07 14:05:41 -08002822 }
2823
2824 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002825 if (!mode) {
2826 ret = -ENOMEM;
2827 goto out;
2828 }
2829
Daniel Stone934a8a82015-05-22 13:34:48 +01002830 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002831 if (ret) {
2832 DRM_DEBUG_KMS("Invalid mode\n");
2833 goto out;
2834 }
2835
Dave Airlief453ba02008-11-07 14:05:41 -08002836 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002837
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002838 /*
2839 * Check whether the primary plane supports the fb pixel format.
2840 * Drivers not implementing the universal planes API use a
2841 * default formats list provided by the DRM core which doesn't
2842 * match real hardware capabilities. Skip the check in that
2843 * case.
2844 */
2845 if (!crtc->primary->format_default) {
2846 ret = drm_plane_check_pixel_format(crtc->primary,
2847 fb->pixel_format);
2848 if (ret) {
2849 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2850 drm_get_format_name(fb->pixel_format));
2851 goto out;
2852 }
2853 }
2854
Damien Lespiauc11e9282013-09-25 16:45:30 +01002855 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2856 mode, fb);
2857 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002858 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002859
Dave Airlief453ba02008-11-07 14:05:41 -08002860 }
2861
2862 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002863 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002864 ret = -EINVAL;
2865 goto out;
2866 }
2867
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002868 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002869 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002870 crtc_req->count_connectors);
2871 ret = -EINVAL;
2872 goto out;
2873 }
2874
2875 if (crtc_req->count_connectors > 0) {
2876 u32 out_id;
2877
2878 /* Avoid unbounded kernel memory allocation */
2879 if (crtc_req->count_connectors > config->num_connector) {
2880 ret = -EINVAL;
2881 goto out;
2882 }
2883
Thierry Reding2f6c5382014-12-10 13:03:36 +01002884 connector_set = kmalloc_array(crtc_req->count_connectors,
2885 sizeof(struct drm_connector *),
2886 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002887 if (!connector_set) {
2888 ret = -ENOMEM;
2889 goto out;
2890 }
2891
2892 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002893 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002894 if (get_user(out_id, &set_connectors_ptr[i])) {
2895 ret = -EFAULT;
2896 goto out;
2897 }
2898
Rob Clarka2b34e22013-10-05 16:36:52 -04002899 connector = drm_connector_find(dev, out_id);
2900 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002901 DRM_DEBUG_KMS("Connector id %d unknown\n",
2902 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002903 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002904 goto out;
2905 }
Jerome Glisse94401062010-07-15 15:43:25 -04002906 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2907 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002908 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002909
2910 connector_set[i] = connector;
2911 }
2912 }
2913
2914 set.crtc = crtc;
2915 set.x = crtc_req->x;
2916 set.y = crtc_req->y;
2917 set.mode = mode;
2918 set.connectors = connector_set;
2919 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002920 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002921 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002922
2923out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002924 if (fb)
2925 drm_framebuffer_unreference(fb);
2926
Dave Airlief453ba02008-11-07 14:05:41 -08002927 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002928 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002929 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002930 return ret;
2931}
2932
Matt Roper161d0dc2014-06-10 08:28:10 -07002933/**
2934 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2935 * universal plane handler call
2936 * @crtc: crtc to update cursor for
2937 * @req: data pointer for the ioctl
2938 * @file_priv: drm file for the ioctl call
2939 *
2940 * Legacy cursor ioctl's work directly with driver buffer handles. To
2941 * translate legacy ioctl calls into universal plane handler calls, we need to
2942 * wrap the native buffer handle in a drm_framebuffer.
2943 *
2944 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2945 * buffer with a pitch of 4*width; the universal plane interface should be used
2946 * directly in cases where the hardware can support other buffer settings and
2947 * userspace wants to make use of these capabilities.
2948 *
2949 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002950 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002951 */
2952static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2953 struct drm_mode_cursor2 *req,
2954 struct drm_file *file_priv)
2955{
2956 struct drm_device *dev = crtc->dev;
2957 struct drm_framebuffer *fb = NULL;
2958 struct drm_mode_fb_cmd2 fbreq = {
2959 .width = req->width,
2960 .height = req->height,
2961 .pixel_format = DRM_FORMAT_ARGB8888,
2962 .pitches = { req->width * 4 },
2963 .handles = { req->handle },
2964 };
2965 int32_t crtc_x, crtc_y;
2966 uint32_t crtc_w = 0, crtc_h = 0;
2967 uint32_t src_w = 0, src_h = 0;
2968 int ret = 0;
2969
2970 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002971 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002972
2973 /*
2974 * Obtain fb we'll be using (either new or existing) and take an extra
2975 * reference to it if fb != null. setplane will take care of dropping
2976 * the reference if the plane update fails.
2977 */
2978 if (req->flags & DRM_MODE_CURSOR_BO) {
2979 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002980 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002981 if (IS_ERR(fb)) {
2982 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2983 return PTR_ERR(fb);
2984 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002985 } else {
2986 fb = NULL;
2987 }
2988 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002989 fb = crtc->cursor->fb;
2990 if (fb)
2991 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002992 }
2993
2994 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2995 crtc_x = req->x;
2996 crtc_y = req->y;
2997 } else {
2998 crtc_x = crtc->cursor_x;
2999 crtc_y = crtc->cursor_y;
3000 }
3001
3002 if (fb) {
3003 crtc_w = fb->width;
3004 crtc_h = fb->height;
3005 src_w = fb->width << 16;
3006 src_h = fb->height << 16;
3007 }
3008
3009 /*
3010 * setplane_internal will take care of deref'ing either the old or new
3011 * framebuffer depending on success.
3012 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003013 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07003014 crtc_x, crtc_y, crtc_w, crtc_h,
3015 0, 0, src_w, src_h);
3016
3017 /* Update successful; save new cursor position, if necessary */
3018 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3019 crtc->cursor_x = req->x;
3020 crtc->cursor_y = req->y;
3021 }
3022
3023 return ret;
3024}
3025
Dave Airlie4c813d42013-06-20 11:48:52 +10003026static int drm_mode_cursor_common(struct drm_device *dev,
3027 struct drm_mode_cursor2 *req,
3028 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003029{
Dave Airlief453ba02008-11-07 14:05:41 -08003030 struct drm_crtc *crtc;
3031 int ret = 0;
3032
Dave Airliefb3b06c2011-02-08 13:55:21 +10003033 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3034 return -EINVAL;
3035
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003036 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003037 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003038
Rob Clarka2b34e22013-10-05 16:36:52 -04003039 crtc = drm_crtc_find(dev, req->crtc_id);
3040 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003041 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003042 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003043 }
Dave Airlief453ba02008-11-07 14:05:41 -08003044
Matt Roper161d0dc2014-06-10 08:28:10 -07003045 /*
3046 * If this crtc has a universal cursor plane, call that plane's update
3047 * handler rather than using legacy cursor handlers.
3048 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003049 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003050 if (crtc->cursor) {
3051 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3052 goto out;
3053 }
3054
Dave Airlief453ba02008-11-07 14:05:41 -08003055 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003056 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003057 ret = -ENXIO;
3058 goto out;
3059 }
3060 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003061 if (crtc->funcs->cursor_set2)
3062 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3063 req->width, req->height, req->hot_x, req->hot_y);
3064 else
3065 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3066 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003067 }
3068
3069 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3070 if (crtc->funcs->cursor_move) {
3071 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3072 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003073 ret = -EFAULT;
3074 goto out;
3075 }
3076 }
3077out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003078 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003079
Dave Airlief453ba02008-11-07 14:05:41 -08003080 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003081
3082}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003083
3084
3085/**
3086 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3087 * @dev: drm device for the ioctl
3088 * @data: data pointer for the ioctl
3089 * @file_priv: drm file for the ioctl call
3090 *
3091 * Set the cursor configuration based on user request.
3092 *
3093 * Called by the user via ioctl.
3094 *
3095 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003096 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003097 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003098int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003099 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003100{
3101 struct drm_mode_cursor *req = data;
3102 struct drm_mode_cursor2 new_req;
3103
3104 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3105 new_req.hot_x = new_req.hot_y = 0;
3106
3107 return drm_mode_cursor_common(dev, &new_req, file_priv);
3108}
3109
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003110/**
3111 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3112 * @dev: drm device for the ioctl
3113 * @data: data pointer for the ioctl
3114 * @file_priv: drm file for the ioctl call
3115 *
3116 * Set the cursor configuration based on user request. This implements the 2nd
3117 * version of the cursor ioctl, which allows userspace to additionally specify
3118 * the hotspot of the pointer.
3119 *
3120 * Called by the user via ioctl.
3121 *
3122 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003123 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003124 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003125int drm_mode_cursor2_ioctl(struct drm_device *dev,
3126 void *data, struct drm_file *file_priv)
3127{
3128 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003129
Dave Airlie4c813d42013-06-20 11:48:52 +10003130 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003131}
3132
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003133/**
3134 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3135 * @bpp: bits per pixels
3136 * @depth: bit depth per pixel
3137 *
3138 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3139 * Useful in fbdev emulation code, since that deals in those values.
3140 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003141uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3142{
3143 uint32_t fmt;
3144
3145 switch (bpp) {
3146 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003147 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003148 break;
3149 case 16:
3150 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003151 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003152 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003153 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003154 break;
3155 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003156 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003157 break;
3158 case 32:
3159 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003160 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003161 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003162 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003163 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003164 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003165 break;
3166 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003167 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3168 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003169 break;
3170 }
3171
3172 return fmt;
3173}
3174EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3175
Dave Airlief453ba02008-11-07 14:05:41 -08003176/**
3177 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003178 * @dev: drm device for the ioctl
3179 * @data: data pointer for the ioctl
3180 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003181 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003182 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003183 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003184 *
3185 * Called by the user via ioctl.
3186 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003187 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003188 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003189 */
3190int drm_mode_addfb(struct drm_device *dev,
3191 void *data, struct drm_file *file_priv)
3192{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003193 struct drm_mode_fb_cmd *or = data;
3194 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003195 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003196
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003197 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003198 r.fb_id = or->fb_id;
3199 r.width = or->width;
3200 r.height = or->height;
3201 r.pitches[0] = or->pitch;
3202 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3203 r.handles[0] = or->handle;
3204
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003205 ret = drm_mode_addfb2(dev, &r, file_priv);
3206 if (ret)
3207 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003208
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003209 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003210
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003211 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003212}
3213
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003214static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003215{
3216 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3217
3218 switch (format) {
3219 case DRM_FORMAT_C8:
3220 case DRM_FORMAT_RGB332:
3221 case DRM_FORMAT_BGR233:
3222 case DRM_FORMAT_XRGB4444:
3223 case DRM_FORMAT_XBGR4444:
3224 case DRM_FORMAT_RGBX4444:
3225 case DRM_FORMAT_BGRX4444:
3226 case DRM_FORMAT_ARGB4444:
3227 case DRM_FORMAT_ABGR4444:
3228 case DRM_FORMAT_RGBA4444:
3229 case DRM_FORMAT_BGRA4444:
3230 case DRM_FORMAT_XRGB1555:
3231 case DRM_FORMAT_XBGR1555:
3232 case DRM_FORMAT_RGBX5551:
3233 case DRM_FORMAT_BGRX5551:
3234 case DRM_FORMAT_ARGB1555:
3235 case DRM_FORMAT_ABGR1555:
3236 case DRM_FORMAT_RGBA5551:
3237 case DRM_FORMAT_BGRA5551:
3238 case DRM_FORMAT_RGB565:
3239 case DRM_FORMAT_BGR565:
3240 case DRM_FORMAT_RGB888:
3241 case DRM_FORMAT_BGR888:
3242 case DRM_FORMAT_XRGB8888:
3243 case DRM_FORMAT_XBGR8888:
3244 case DRM_FORMAT_RGBX8888:
3245 case DRM_FORMAT_BGRX8888:
3246 case DRM_FORMAT_ARGB8888:
3247 case DRM_FORMAT_ABGR8888:
3248 case DRM_FORMAT_RGBA8888:
3249 case DRM_FORMAT_BGRA8888:
3250 case DRM_FORMAT_XRGB2101010:
3251 case DRM_FORMAT_XBGR2101010:
3252 case DRM_FORMAT_RGBX1010102:
3253 case DRM_FORMAT_BGRX1010102:
3254 case DRM_FORMAT_ARGB2101010:
3255 case DRM_FORMAT_ABGR2101010:
3256 case DRM_FORMAT_RGBA1010102:
3257 case DRM_FORMAT_BGRA1010102:
3258 case DRM_FORMAT_YUYV:
3259 case DRM_FORMAT_YVYU:
3260 case DRM_FORMAT_UYVY:
3261 case DRM_FORMAT_VYUY:
3262 case DRM_FORMAT_AYUV:
3263 case DRM_FORMAT_NV12:
3264 case DRM_FORMAT_NV21:
3265 case DRM_FORMAT_NV16:
3266 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003267 case DRM_FORMAT_NV24:
3268 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003269 case DRM_FORMAT_YUV410:
3270 case DRM_FORMAT_YVU410:
3271 case DRM_FORMAT_YUV411:
3272 case DRM_FORMAT_YVU411:
3273 case DRM_FORMAT_YUV420:
3274 case DRM_FORMAT_YVU420:
3275 case DRM_FORMAT_YUV422:
3276 case DRM_FORMAT_YVU422:
3277 case DRM_FORMAT_YUV444:
3278 case DRM_FORMAT_YVU444:
3279 return 0;
3280 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003281 DRM_DEBUG_KMS("invalid pixel format %s\n",
3282 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003283 return -EINVAL;
3284 }
3285}
3286
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003287static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003288{
3289 int ret, hsub, vsub, num_planes, i;
3290
3291 ret = format_check(r);
3292 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003293 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3294 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003295 return ret;
3296 }
3297
3298 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3299 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3300 num_planes = drm_format_num_planes(r->pixel_format);
3301
3302 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003303 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003304 return -EINVAL;
3305 }
3306
3307 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003308 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003309 return -EINVAL;
3310 }
3311
3312 for (i = 0; i < num_planes; i++) {
3313 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003314 unsigned int height = r->height / (i != 0 ? vsub : 1);
3315 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003316
3317 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003318 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003319 return -EINVAL;
3320 }
3321
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003322 if ((uint64_t) width * cpp > UINT_MAX)
3323 return -ERANGE;
3324
3325 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3326 return -ERANGE;
3327
3328 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003329 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003330 return -EINVAL;
3331 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003332
3333 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3334 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3335 r->modifier[i], i);
3336 return -EINVAL;
3337 }
Rob Clark570655b2015-01-30 20:18:11 +05303338
3339 /* modifier specific checks: */
3340 switch (r->modifier[i]) {
3341 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3342 /* NOTE: the pitch restriction may be lifted later if it turns
3343 * out that no hw has this restriction:
3344 */
3345 if (r->pixel_format != DRM_FORMAT_NV12 ||
3346 width % 128 || height % 32 ||
3347 r->pitches[i] % 128) {
3348 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3349 return -EINVAL;
3350 }
3351 break;
3352
3353 default:
3354 break;
3355 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003356 }
3357
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003358 for (i = num_planes; i < 4; i++) {
3359 if (r->modifier[i]) {
3360 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3361 return -EINVAL;
3362 }
3363
3364 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3365 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3366 continue;
3367
3368 if (r->handles[i]) {
3369 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3370 return -EINVAL;
3371 }
3372
3373 if (r->pitches[i]) {
3374 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3375 return -EINVAL;
3376 }
3377
3378 if (r->offsets[i]) {
3379 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3380 return -EINVAL;
3381 }
3382 }
3383
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003384 return 0;
3385}
3386
Chris Wilson9a6f5132015-02-25 13:45:26 +00003387static struct drm_framebuffer *
3388internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003389 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003390 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003391{
3392 struct drm_mode_config *config = &dev->mode_config;
3393 struct drm_framebuffer *fb;
3394 int ret;
3395
Rob Clarke3eb3252015-02-05 14:41:52 +00003396 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003397 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3398 return ERR_PTR(-EINVAL);
3399 }
3400
3401 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3402 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3403 r->width, config->min_width, config->max_width);
3404 return ERR_PTR(-EINVAL);
3405 }
3406 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3407 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3408 r->height, config->min_height, config->max_height);
3409 return ERR_PTR(-EINVAL);
3410 }
3411
Rob Clarke3eb3252015-02-05 14:41:52 +00003412 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3413 !dev->mode_config.allow_fb_modifiers) {
3414 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3415 return ERR_PTR(-EINVAL);
3416 }
3417
Matt Roperc394c2b2014-06-10 08:28:08 -07003418 ret = framebuffer_check(r);
3419 if (ret)
3420 return ERR_PTR(ret);
3421
3422 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3423 if (IS_ERR(fb)) {
3424 DRM_DEBUG_KMS("could not create framebuffer\n");
3425 return fb;
3426 }
3427
Matt Roperc394c2b2014-06-10 08:28:08 -07003428 return fb;
3429}
3430
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003431/**
3432 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003433 * @dev: drm device for the ioctl
3434 * @data: data pointer for the ioctl
3435 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003436 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003437 * Add a new FB to the specified CRTC, given a user request with format. This is
3438 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3439 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003440 *
3441 * Called by the user via ioctl.
3442 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003443 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003444 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003445 */
3446int drm_mode_addfb2(struct drm_device *dev,
3447 void *data, struct drm_file *file_priv)
3448{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003449 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003450 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003451
Dave Airliefb3b06c2011-02-08 13:55:21 +10003452 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3453 return -EINVAL;
3454
Chris Wilson9a6f5132015-02-25 13:45:26 +00003455 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003456 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003457 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003458
Chris Wilson9a6f5132015-02-25 13:45:26 +00003459 /* Transfer ownership to the filp for reaping on close */
3460
3461 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3462 mutex_lock(&file_priv->fbs_lock);
3463 r->fb_id = fb->base.id;
3464 list_add(&fb->filp_head, &file_priv->fbs);
3465 mutex_unlock(&file_priv->fbs_lock);
3466
Matt Roperc394c2b2014-06-10 08:28:08 -07003467 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003468}
3469
3470/**
3471 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003472 * @dev: drm device for the ioctl
3473 * @data: data pointer for the ioctl
3474 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003475 *
Dave Airlief453ba02008-11-07 14:05:41 -08003476 * Remove the FB specified by the user.
3477 *
3478 * Called by the user via ioctl.
3479 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003480 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003481 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003482 */
3483int drm_mode_rmfb(struct drm_device *dev,
3484 void *data, struct drm_file *file_priv)
3485{
Dave Airlief453ba02008-11-07 14:05:41 -08003486 struct drm_framebuffer *fb = NULL;
3487 struct drm_framebuffer *fbl = NULL;
3488 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003489 int found = 0;
3490
Dave Airliefb3b06c2011-02-08 13:55:21 +10003491 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3492 return -EINVAL;
3493
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003494 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003495 mutex_lock(&dev->mode_config.fb_lock);
3496 fb = __drm_framebuffer_lookup(dev, *id);
3497 if (!fb)
3498 goto fail_lookup;
3499
Dave Airlief453ba02008-11-07 14:05:41 -08003500 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3501 if (fb == fbl)
3502 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01003503 if (!found)
3504 goto fail_lookup;
3505
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003506 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003507 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003508 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003509
Maarten Lankhorst13803132015-09-09 16:40:56 +02003510 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003511
Daniel Vetter2b677e82012-12-10 21:16:05 +01003512 return 0;
3513
3514fail_lookup:
3515 mutex_unlock(&dev->mode_config.fb_lock);
3516 mutex_unlock(&file_priv->fbs_lock);
3517
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003518 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003519}
3520
3521/**
3522 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003523 * @dev: drm device for the ioctl
3524 * @data: data pointer for the ioctl
3525 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003526 *
Dave Airlief453ba02008-11-07 14:05:41 -08003527 * Lookup the FB given its ID and return info about it.
3528 *
3529 * Called by the user via ioctl.
3530 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003531 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003532 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003533 */
3534int drm_mode_getfb(struct drm_device *dev,
3535 void *data, struct drm_file *file_priv)
3536{
3537 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003538 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003539 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003540
Dave Airliefb3b06c2011-02-08 13:55:21 +10003541 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3542 return -EINVAL;
3543
Daniel Vetter786b99e2012-12-02 21:53:40 +01003544 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003545 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003546 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003547
3548 r->height = fb->height;
3549 r->width = fb->width;
3550 r->depth = fb->depth;
3551 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003552 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003553 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003554 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003555 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003556 ret = fb->funcs->create_handle(fb, file_priv,
3557 &r->handle);
3558 } else {
3559 /* GET_FB() is an unprivileged ioctl so we must not
3560 * return a buffer-handle to non-master processes! For
3561 * backwards-compatibility reasons, we cannot make
3562 * GET_FB() privileged, so just return an invalid handle
3563 * for non-masters. */
3564 r->handle = 0;
3565 ret = 0;
3566 }
3567 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003568 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003569 }
Dave Airlief453ba02008-11-07 14:05:41 -08003570
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003571 drm_framebuffer_unreference(fb);
3572
Dave Airlief453ba02008-11-07 14:05:41 -08003573 return ret;
3574}
3575
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003576/**
3577 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3578 * @dev: drm device for the ioctl
3579 * @data: data pointer for the ioctl
3580 * @file_priv: drm file for the ioctl call
3581 *
3582 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3583 * rectangle list. Generic userspace which does frontbuffer rendering must call
3584 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3585 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3586 *
3587 * Modesetting drivers which always update the frontbuffer do not need to
3588 * implement the corresponding ->dirty framebuffer callback.
3589 *
3590 * Called by the user via ioctl.
3591 *
3592 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003593 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003594 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003595int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3596 void *data, struct drm_file *file_priv)
3597{
3598 struct drm_clip_rect __user *clips_ptr;
3599 struct drm_clip_rect *clips = NULL;
3600 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003601 struct drm_framebuffer *fb;
3602 unsigned flags;
3603 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003604 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003605
Dave Airliefb3b06c2011-02-08 13:55:21 +10003606 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3607 return -EINVAL;
3608
Daniel Vetter786b99e2012-12-02 21:53:40 +01003609 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003610 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003611 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003612
3613 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003614 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003615
3616 if (!num_clips != !clips_ptr) {
3617 ret = -EINVAL;
3618 goto out_err1;
3619 }
3620
3621 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3622
3623 /* If userspace annotates copy, clips must come in pairs */
3624 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3625 ret = -EINVAL;
3626 goto out_err1;
3627 }
3628
3629 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003630 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3631 ret = -EINVAL;
3632 goto out_err1;
3633 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003634 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003635 if (!clips) {
3636 ret = -ENOMEM;
3637 goto out_err1;
3638 }
3639
3640 ret = copy_from_user(clips, clips_ptr,
3641 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003642 if (ret) {
3643 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003644 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003645 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003646 }
3647
3648 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003649 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3650 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003651 } else {
3652 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003653 }
3654
3655out_err2:
3656 kfree(clips);
3657out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003658 drm_framebuffer_unreference(fb);
3659
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003660 return ret;
3661}
3662
3663
Dave Airlief453ba02008-11-07 14:05:41 -08003664/**
3665 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003666 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003667 *
Dave Airlief453ba02008-11-07 14:05:41 -08003668 * Destroy all the FBs associated with @filp.
3669 *
3670 * Called by the user via ioctl.
3671 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003672 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003673 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003674 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003675void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003676{
Dave Airlief453ba02008-11-07 14:05:41 -08003677 struct drm_framebuffer *fb, *tfb;
3678
Daniel Vetter1b116292014-09-24 21:51:06 +02003679 /*
3680 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003681 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003682 * avoid upsetting lockdep since the universal cursor code adds a
3683 * framebuffer while holding mutex locks.
3684 *
3685 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3686 * locks is impossible here since no one else but this function can get
3687 * at it any more.
3688 */
Dave Airlief453ba02008-11-07 14:05:41 -08003689 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003690 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003691
Maarten Lankhorst73f75702015-09-09 16:40:57 +02003692 /* This drops the fpriv->fbs reference. */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003693 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003694 }
Dave Airlief453ba02008-11-07 14:05:41 -08003695}
3696
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003697/**
3698 * drm_property_create - create a new property type
3699 * @dev: drm device
3700 * @flags: flags specifying the property type
3701 * @name: name of the property
3702 * @num_values: number of pre-defined values
3703 *
3704 * This creates a new generic drm property which can then be attached to a drm
3705 * object with drm_object_attach_property. The returned property object must be
3706 * freed with drm_property_destroy.
3707 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003708 * Note that the DRM core keeps a per-device list of properties and that, if
3709 * drm_mode_config_cleanup() is called, it will destroy all properties created
3710 * by the driver.
3711 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003712 * Returns:
3713 * A pointer to the newly created property on success, NULL on failure.
3714 */
Dave Airlief453ba02008-11-07 14:05:41 -08003715struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3716 const char *name, int num_values)
3717{
3718 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003719 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003720
3721 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3722 if (!property)
3723 return NULL;
3724
Rob Clark98f75de2014-05-30 11:37:03 -04003725 property->dev = dev;
3726
Dave Airlief453ba02008-11-07 14:05:41 -08003727 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003728 property->values = kcalloc(num_values, sizeof(uint64_t),
3729 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003730 if (!property->values)
3731 goto fail;
3732 }
3733
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003734 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3735 if (ret)
3736 goto fail;
3737
Dave Airlief453ba02008-11-07 14:05:41 -08003738 property->flags = flags;
3739 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003740 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003741
Vinson Lee471dd2e2011-11-10 11:55:40 -08003742 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003743 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003744 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3745 }
Dave Airlief453ba02008-11-07 14:05:41 -08003746
3747 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003748
3749 WARN_ON(!drm_property_type_valid(property));
3750
Dave Airlief453ba02008-11-07 14:05:41 -08003751 return property;
3752fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003753 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003754 kfree(property);
3755 return NULL;
3756}
3757EXPORT_SYMBOL(drm_property_create);
3758
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003759/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003760 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003761 * @dev: drm device
3762 * @flags: flags specifying the property type
3763 * @name: name of the property
3764 * @props: enumeration lists with property values
3765 * @num_values: number of pre-defined values
3766 *
3767 * This creates a new generic drm property which can then be attached to a drm
3768 * object with drm_object_attach_property. The returned property object must be
3769 * freed with drm_property_destroy.
3770 *
3771 * Userspace is only allowed to set one of the predefined values for enumeration
3772 * properties.
3773 *
3774 * Returns:
3775 * A pointer to the newly created property on success, NULL on failure.
3776 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003777struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3778 const char *name,
3779 const struct drm_prop_enum_list *props,
3780 int num_values)
3781{
3782 struct drm_property *property;
3783 int i, ret;
3784
3785 flags |= DRM_MODE_PROP_ENUM;
3786
3787 property = drm_property_create(dev, flags, name, num_values);
3788 if (!property)
3789 return NULL;
3790
3791 for (i = 0; i < num_values; i++) {
3792 ret = drm_property_add_enum(property, i,
3793 props[i].type,
3794 props[i].name);
3795 if (ret) {
3796 drm_property_destroy(dev, property);
3797 return NULL;
3798 }
3799 }
3800
3801 return property;
3802}
3803EXPORT_SYMBOL(drm_property_create_enum);
3804
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003805/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003806 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003807 * @dev: drm device
3808 * @flags: flags specifying the property type
3809 * @name: name of the property
3810 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003811 * @num_props: size of the @props array
3812 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003813 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003814 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003815 * object with drm_object_attach_property. The returned property object must be
3816 * freed with drm_property_destroy.
3817 *
3818 * Compared to plain enumeration properties userspace is allowed to set any
3819 * or'ed together combination of the predefined property bitflag values
3820 *
3821 * Returns:
3822 * A pointer to the newly created property on success, NULL on failure.
3823 */
Rob Clark49e27542012-05-17 02:23:26 -06003824struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3825 int flags, const char *name,
3826 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303827 int num_props,
3828 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003829{
3830 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303831 int i, ret, index = 0;
3832 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003833
3834 flags |= DRM_MODE_PROP_BITMASK;
3835
3836 property = drm_property_create(dev, flags, name, num_values);
3837 if (!property)
3838 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303839 for (i = 0; i < num_props; i++) {
3840 if (!(supported_bits & (1ULL << props[i].type)))
3841 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003842
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303843 if (WARN_ON(index >= num_values)) {
3844 drm_property_destroy(dev, property);
3845 return NULL;
3846 }
3847
3848 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003849 props[i].type,
3850 props[i].name);
3851 if (ret) {
3852 drm_property_destroy(dev, property);
3853 return NULL;
3854 }
3855 }
3856
3857 return property;
3858}
3859EXPORT_SYMBOL(drm_property_create_bitmask);
3860
Rob Clarkebc44cf2012-09-12 22:22:31 -05003861static struct drm_property *property_create_range(struct drm_device *dev,
3862 int flags, const char *name,
3863 uint64_t min, uint64_t max)
3864{
3865 struct drm_property *property;
3866
3867 property = drm_property_create(dev, flags, name, 2);
3868 if (!property)
3869 return NULL;
3870
3871 property->values[0] = min;
3872 property->values[1] = max;
3873
3874 return property;
3875}
3876
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003877/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003878 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003879 * @dev: drm device
3880 * @flags: flags specifying the property type
3881 * @name: name of the property
3882 * @min: minimum value of the property
3883 * @max: maximum value of the property
3884 *
3885 * This creates a new generic drm property which can then be attached to a drm
3886 * object with drm_object_attach_property. The returned property object must be
3887 * freed with drm_property_destroy.
3888 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003889 * Userspace is allowed to set any unsigned integer value in the (min, max)
3890 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003891 *
3892 * Returns:
3893 * A pointer to the newly created property on success, NULL on failure.
3894 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003895struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3896 const char *name,
3897 uint64_t min, uint64_t max)
3898{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003899 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3900 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003901}
3902EXPORT_SYMBOL(drm_property_create_range);
3903
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003904/**
3905 * drm_property_create_signed_range - create a new signed ranged property type
3906 * @dev: drm device
3907 * @flags: flags specifying the property type
3908 * @name: name of the property
3909 * @min: minimum value of the property
3910 * @max: maximum value of the property
3911 *
3912 * This creates a new generic drm property which can then be attached to a drm
3913 * object with drm_object_attach_property. The returned property object must be
3914 * freed with drm_property_destroy.
3915 *
3916 * Userspace is allowed to set any signed integer value in the (min, max)
3917 * range inclusive.
3918 *
3919 * Returns:
3920 * A pointer to the newly created property on success, NULL on failure.
3921 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003922struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3923 int flags, const char *name,
3924 int64_t min, int64_t max)
3925{
3926 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3927 name, I642U64(min), I642U64(max));
3928}
3929EXPORT_SYMBOL(drm_property_create_signed_range);
3930
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003931/**
3932 * drm_property_create_object - create a new object property type
3933 * @dev: drm device
3934 * @flags: flags specifying the property type
3935 * @name: name of the property
3936 * @type: object type from DRM_MODE_OBJECT_* defines
3937 *
3938 * This creates a new generic drm property which can then be attached to a drm
3939 * object with drm_object_attach_property. The returned property object must be
3940 * freed with drm_property_destroy.
3941 *
3942 * Userspace is only allowed to set this to any property value of the given
3943 * @type. Only useful for atomic properties, which is enforced.
3944 *
3945 * Returns:
3946 * A pointer to the newly created property on success, NULL on failure.
3947 */
Rob Clark98f75de2014-05-30 11:37:03 -04003948struct drm_property *drm_property_create_object(struct drm_device *dev,
3949 int flags, const char *name, uint32_t type)
3950{
3951 struct drm_property *property;
3952
3953 flags |= DRM_MODE_PROP_OBJECT;
3954
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003955 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3956 return NULL;
3957
Rob Clark98f75de2014-05-30 11:37:03 -04003958 property = drm_property_create(dev, flags, name, 1);
3959 if (!property)
3960 return NULL;
3961
3962 property->values[0] = type;
3963
3964 return property;
3965}
3966EXPORT_SYMBOL(drm_property_create_object);
3967
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003968/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003969 * drm_property_create_bool - create a new boolean property type
3970 * @dev: drm device
3971 * @flags: flags specifying the property type
3972 * @name: name of the property
3973 *
3974 * This creates a new generic drm property which can then be attached to a drm
3975 * object with drm_object_attach_property. The returned property object must be
3976 * freed with drm_property_destroy.
3977 *
3978 * This is implemented as a ranged property with only {0, 1} as valid values.
3979 *
3980 * Returns:
3981 * A pointer to the newly created property on success, NULL on failure.
3982 */
3983struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3984 const char *name)
3985{
3986 return drm_property_create_range(dev, flags, name, 0, 1);
3987}
3988EXPORT_SYMBOL(drm_property_create_bool);
3989
3990/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003991 * drm_property_add_enum - add a possible value to an enumeration property
3992 * @property: enumeration property to change
3993 * @index: index of the new enumeration
3994 * @value: value of the new enumeration
3995 * @name: symbolic name of the new enumeration
3996 *
3997 * This functions adds enumerations to a property.
3998 *
3999 * It's use is deprecated, drivers should use one of the more specific helpers
4000 * to directly create the property with all enumerations already attached.
4001 *
4002 * Returns:
4003 * Zero on success, error code on failure.
4004 */
Dave Airlief453ba02008-11-07 14:05:41 -08004005int drm_property_add_enum(struct drm_property *property, int index,
4006 uint64_t value, const char *name)
4007{
4008 struct drm_property_enum *prop_enum;
4009
Rob Clark5ea22f22014-05-30 11:34:01 -04004010 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4011 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06004012 return -EINVAL;
4013
4014 /*
4015 * Bitmask enum properties have the additional constraint of values
4016 * from 0 to 63
4017 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004018 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4019 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004020 return -EINVAL;
4021
Daniel Vetter3758b342014-11-19 18:38:10 +01004022 if (!list_empty(&property->enum_list)) {
4023 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004024 if (prop_enum->value == value) {
4025 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4026 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4027 return 0;
4028 }
4029 }
4030 }
4031
4032 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4033 if (!prop_enum)
4034 return -ENOMEM;
4035
4036 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4037 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4038 prop_enum->value = value;
4039
4040 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004041 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004042 return 0;
4043}
4044EXPORT_SYMBOL(drm_property_add_enum);
4045
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004046/**
4047 * drm_property_destroy - destroy a drm property
4048 * @dev: drm device
4049 * @property: property to destry
4050 *
4051 * This function frees a property including any attached resources like
4052 * enumeration values.
4053 */
Dave Airlief453ba02008-11-07 14:05:41 -08004054void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4055{
4056 struct drm_property_enum *prop_enum, *pt;
4057
Daniel Vetter3758b342014-11-19 18:38:10 +01004058 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004059 list_del(&prop_enum->head);
4060 kfree(prop_enum);
4061 }
4062
4063 if (property->num_values)
4064 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004065 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004066 list_del(&property->head);
4067 kfree(property);
4068}
4069EXPORT_SYMBOL(drm_property_destroy);
4070
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004071/**
4072 * drm_object_attach_property - attach a property to a modeset object
4073 * @obj: drm modeset object
4074 * @property: property to attach
4075 * @init_val: initial value of the property
4076 *
4077 * This attaches the given property to the modeset object with the given initial
4078 * value. Currently this function cannot fail since the properties are stored in
4079 * a statically sized array.
4080 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004081void drm_object_attach_property(struct drm_mode_object *obj,
4082 struct drm_property *property,
4083 uint64_t init_val)
4084{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004085 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004086
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004087 if (count == DRM_OBJECT_MAX_PROPERTY) {
4088 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4089 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4090 "you see this message on the same object type.\n",
4091 obj->type);
4092 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004093 }
4094
Rob Clarkb17cd752014-12-16 18:05:30 -05004095 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004096 obj->properties->values[count] = init_val;
4097 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004098 if (property->flags & DRM_MODE_PROP_ATOMIC)
4099 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004100}
4101EXPORT_SYMBOL(drm_object_attach_property);
4102
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004103/**
4104 * drm_object_property_set_value - set the value of a property
4105 * @obj: drm mode object to set property value for
4106 * @property: property to set
4107 * @val: value the property should be set to
4108 *
4109 * This functions sets a given property on a given object. This function only
4110 * changes the software state of the property, it does not call into the
4111 * driver's ->set_property callback.
4112 *
4113 * Returns:
4114 * Zero on success, error code on failure.
4115 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004116int drm_object_property_set_value(struct drm_mode_object *obj,
4117 struct drm_property *property, uint64_t val)
4118{
4119 int i;
4120
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004121 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004122 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004123 obj->properties->values[i] = val;
4124 return 0;
4125 }
4126 }
4127
4128 return -EINVAL;
4129}
4130EXPORT_SYMBOL(drm_object_property_set_value);
4131
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004132/**
4133 * drm_object_property_get_value - retrieve the value of a property
4134 * @obj: drm mode object to get property value from
4135 * @property: property to retrieve
4136 * @val: storage for the property value
4137 *
4138 * This function retrieves the softare state of the given property for the given
4139 * property. Since there is no driver callback to retrieve the current property
4140 * value this might be out of sync with the hardware, depending upon the driver
4141 * and property.
4142 *
4143 * Returns:
4144 * Zero on success, error code on failure.
4145 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004146int drm_object_property_get_value(struct drm_mode_object *obj,
4147 struct drm_property *property, uint64_t *val)
4148{
4149 int i;
4150
Rob Clark88a48e22014-12-18 16:01:50 -05004151 /* read-only properties bypass atomic mechanism and still store
4152 * their value in obj->properties->values[].. mostly to avoid
4153 * having to deal w/ EDID and similar props in atomic paths:
4154 */
4155 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4156 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4157 return drm_atomic_get_property(obj, property, val);
4158
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004159 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004160 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004161 *val = obj->properties->values[i];
4162 return 0;
4163 }
4164 }
4165
4166 return -EINVAL;
4167}
4168EXPORT_SYMBOL(drm_object_property_get_value);
4169
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004170/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004171 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004172 * @dev: DRM device
4173 * @data: ioctl data
4174 * @file_priv: DRM file info
4175 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004176 * This function retrieves the metadata for a given property, like the different
4177 * possible values for an enum property or the limits for a range property.
4178 *
4179 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004180 *
4181 * Called by the user via ioctl.
4182 *
4183 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004184 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004185 */
Dave Airlief453ba02008-11-07 14:05:41 -08004186int drm_mode_getproperty_ioctl(struct drm_device *dev,
4187 void *data, struct drm_file *file_priv)
4188{
Dave Airlief453ba02008-11-07 14:05:41 -08004189 struct drm_mode_get_property *out_resp = data;
4190 struct drm_property *property;
4191 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004192 int value_count = 0;
4193 int ret = 0, i;
4194 int copied;
4195 struct drm_property_enum *prop_enum;
4196 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004197 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004198
Dave Airliefb3b06c2011-02-08 13:55:21 +10004199 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4200 return -EINVAL;
4201
Daniel Vetter84849902012-12-02 00:28:11 +01004202 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004203 property = drm_property_find(dev, out_resp->prop_id);
4204 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004205 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004206 goto done;
4207 }
Dave Airlief453ba02008-11-07 14:05:41 -08004208
Rob Clark5ea22f22014-05-30 11:34:01 -04004209 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4210 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004211 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004212 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004213 }
4214
4215 value_count = property->num_values;
4216
4217 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4218 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4219 out_resp->flags = property->flags;
4220
4221 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004222 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004223 for (i = 0; i < value_count; i++) {
4224 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4225 ret = -EFAULT;
4226 goto done;
4227 }
4228 }
4229 }
4230 out_resp->count_values = value_count;
4231
Rob Clark5ea22f22014-05-30 11:34:01 -04004232 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4233 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004234 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4235 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004236 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004237 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004238
4239 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4240 ret = -EFAULT;
4241 goto done;
4242 }
4243
4244 if (copy_to_user(&enum_ptr[copied].name,
4245 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4246 ret = -EFAULT;
4247 goto done;
4248 }
4249 copied++;
4250 }
4251 }
4252 out_resp->count_enum_blobs = enum_count;
4253 }
4254
Daniel Vetter3758b342014-11-19 18:38:10 +01004255 /*
4256 * NOTE: The idea seems to have been to use this to read all the blob
4257 * property values. But nothing ever added them to the corresponding
4258 * list, userspace always used the special-purpose get_blob ioctl to
4259 * read the value for a blob property. It also doesn't make a lot of
4260 * sense to return values here when everything else is just metadata for
4261 * the property itself.
4262 */
4263 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4264 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004265done:
Daniel Vetter84849902012-12-02 00:28:11 +01004266 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004267 return ret;
4268}
4269
Daniel Stone99531d92015-05-22 13:34:49 +01004270/**
4271 * drm_property_create_blob - Create new blob property
4272 *
4273 * Creates a new blob property for a specified DRM device, optionally
4274 * copying data.
4275 *
4276 * @dev: DRM device to create property for
4277 * @length: Length to allocate for blob data
4278 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004279 *
4280 * Returns:
4281 * New blob property with a single reference on success, or an ERR_PTR
4282 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004283 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004284struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004285drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004286 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004287{
4288 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004289 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004290
Dan Carpenter9ac09342015-10-29 16:37:54 +03004291 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004292 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004293
4294 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4295 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004296 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004297
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004298 /* This must be explicitly initialised, so we can safely call list_del
4299 * on it in the removal handler, even if it isn't in a file list. */
4300 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004301 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004302 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004303
Daniel Stone99531d92015-05-22 13:34:49 +01004304 if (data)
4305 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004306
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004307 mutex_lock(&dev->mode_config.blob_lock);
4308
4309 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4310 if (ret) {
4311 kfree(blob);
4312 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004313 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004314 }
4315
Daniel Stone6bcacf52015-04-20 19:22:55 +01004316 kref_init(&blob->refcount);
4317
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004318 list_add_tail(&blob->head_global,
4319 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004320
4321 mutex_unlock(&dev->mode_config.blob_lock);
4322
Dave Airlief453ba02008-11-07 14:05:41 -08004323 return blob;
4324}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004325EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004326
Daniel Stone6bcacf52015-04-20 19:22:55 +01004327/**
4328 * drm_property_free_blob - Blob property destructor
4329 *
4330 * Internal free function for blob properties; must not be used directly.
4331 *
Daniel Stonef102c162015-05-22 13:34:44 +01004332 * @kref: Reference
Daniel Stone6bcacf52015-04-20 19:22:55 +01004333 */
4334static void drm_property_free_blob(struct kref *kref)
Dave Airlief453ba02008-11-07 14:05:41 -08004335{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004336 struct drm_property_blob *blob =
4337 container_of(kref, struct drm_property_blob, refcount);
4338
4339 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4340
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004341 list_del(&blob->head_global);
4342 list_del(&blob->head_file);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004343 drm_mode_object_unregister(blob->dev, &blob->base);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004344
Dave Airlief453ba02008-11-07 14:05:41 -08004345 kfree(blob);
4346}
4347
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004348/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004349 * drm_property_unreference_blob - Unreference a blob property
4350 *
4351 * Drop a reference on a blob property. May free the object.
4352 *
Daniel Stonef102c162015-05-22 13:34:44 +01004353 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004354 */
4355void drm_property_unreference_blob(struct drm_property_blob *blob)
4356{
4357 struct drm_device *dev;
4358
4359 if (!blob)
4360 return;
4361
4362 dev = blob->dev;
4363
4364 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4365
4366 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4367 &dev->mode_config.blob_lock))
4368 mutex_unlock(&dev->mode_config.blob_lock);
4369 else
4370 might_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004371}
4372EXPORT_SYMBOL(drm_property_unreference_blob);
4373
4374/**
4375 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4376 *
4377 * Drop a reference on a blob property. May free the object. This must be
4378 * called with blob_lock held.
4379 *
Daniel Stonef102c162015-05-22 13:34:44 +01004380 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004381 */
4382static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4383{
4384 if (!blob)
4385 return;
4386
4387 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4388
4389 kref_put(&blob->refcount, drm_property_free_blob);
4390}
4391
4392/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004393 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4394 * @dev: DRM device
4395 * @file_priv: destroy all blobs owned by this file handle
4396 */
4397void drm_property_destroy_user_blobs(struct drm_device *dev,
4398 struct drm_file *file_priv)
4399{
4400 struct drm_property_blob *blob, *bt;
4401
4402 mutex_lock(&dev->mode_config.blob_lock);
4403
4404 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4405 list_del_init(&blob->head_file);
4406 drm_property_unreference_blob_locked(blob);
4407 }
4408
4409 mutex_unlock(&dev->mode_config.blob_lock);
4410}
4411
4412/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004413 * drm_property_reference_blob - Take a reference on an existing property
4414 *
4415 * Take a new reference on an existing blob property.
4416 *
Daniel Stonef102c162015-05-22 13:34:44 +01004417 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004418 */
4419struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4420{
4421 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4422 kref_get(&blob->refcount);
4423 return blob;
4424}
4425EXPORT_SYMBOL(drm_property_reference_blob);
4426
4427/*
4428 * Like drm_property_lookup_blob, but does not return an additional reference.
4429 * Must be called with blob_lock held.
4430 */
4431static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4432 uint32_t id)
4433{
4434 struct drm_mode_object *obj = NULL;
4435 struct drm_property_blob *blob;
4436
4437 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4438
4439 mutex_lock(&dev->mode_config.idr_mutex);
4440 obj = idr_find(&dev->mode_config.crtc_idr, id);
4441 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4442 blob = NULL;
4443 else
4444 blob = obj_to_blob(obj);
4445 mutex_unlock(&dev->mode_config.idr_mutex);
4446
Dave Airlief453ba02008-11-07 14:05:41 -08004447 return blob;
4448}
4449
Daniel Stone6bcacf52015-04-20 19:22:55 +01004450/**
4451 * drm_property_lookup_blob - look up a blob property and take a reference
4452 * @dev: drm device
4453 * @id: id of the blob property
4454 *
4455 * If successful, this takes an additional reference to the blob property.
4456 * callers need to make sure to eventually unreference the returned property
4457 * again, using @drm_property_unreference_blob.
4458 */
4459struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4460 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004461{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004462 struct drm_property_blob *blob;
4463
4464 mutex_lock(&dev->mode_config.blob_lock);
4465 blob = __drm_property_lookup_blob(dev, id);
4466 if (blob) {
4467 if (!kref_get_unless_zero(&blob->refcount))
4468 blob = NULL;
4469 }
4470 mutex_unlock(&dev->mode_config.blob_lock);
4471
4472 return blob;
4473}
4474EXPORT_SYMBOL(drm_property_lookup_blob);
4475
4476/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004477 * drm_property_replace_global_blob - atomically replace existing blob property
4478 * @dev: drm device
4479 * @replace: location of blob property pointer to be replaced
4480 * @length: length of data for new blob, or 0 for no data
4481 * @data: content for new blob, or NULL for no data
4482 * @obj_holds_id: optional object for property holding blob ID
4483 * @prop_holds_id: optional property holding blob ID
4484 * @return 0 on success or error on failure
4485 *
4486 * This function will atomically replace a global property in the blob list,
4487 * optionally updating a property which holds the ID of that property. It is
4488 * guaranteed to be atomic: no caller will be allowed to see intermediate
4489 * results, and either the entire operation will succeed and clean up the
4490 * previous property, or it will fail and the state will be unchanged.
4491 *
4492 * If length is 0 or data is NULL, no new blob will be created, and the holding
4493 * property, if specified, will be set to 0.
4494 *
4495 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4496 * by holding the relevant modesetting object lock for its parent.
4497 *
4498 * For example, a drm_connector has a 'PATH' property, which contains the ID
4499 * of a blob property with the value of the MST path information. Calling this
4500 * function with replace pointing to the connector's path_blob_ptr, length and
4501 * data set for the new path information, obj_holds_id set to the connector's
4502 * base object, and prop_holds_id set to the path property name, will perform
4503 * a completely atomic update. The access to path_blob_ptr is protected by the
4504 * caller holding a lock on the connector.
4505 */
4506static int drm_property_replace_global_blob(struct drm_device *dev,
4507 struct drm_property_blob **replace,
4508 size_t length,
4509 const void *data,
4510 struct drm_mode_object *obj_holds_id,
4511 struct drm_property *prop_holds_id)
4512{
4513 struct drm_property_blob *new_blob = NULL;
4514 struct drm_property_blob *old_blob = NULL;
4515 int ret;
4516
4517 WARN_ON(replace == NULL);
4518
4519 old_blob = *replace;
4520
4521 if (length && data) {
4522 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004523 if (IS_ERR(new_blob))
4524 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004525 }
4526
4527 /* This does not need to be synchronised with blob_lock, as the
4528 * get_properties ioctl locks all modesetting objects, and
4529 * obj_holds_id must be locked before calling here, so we cannot
4530 * have its value out of sync with the list membership modified
4531 * below under blob_lock. */
4532 if (obj_holds_id) {
4533 ret = drm_object_property_set_value(obj_holds_id,
4534 prop_holds_id,
4535 new_blob ?
4536 new_blob->base.id : 0);
4537 if (ret != 0)
4538 goto err_created;
4539 }
4540
Markus Elfringec530822015-07-05 21:55:10 +02004541 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004542 *replace = new_blob;
4543
4544 return 0;
4545
4546err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004547 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004548 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004549}
4550
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004551/**
4552 * drm_mode_getblob_ioctl - get the contents of a blob property value
4553 * @dev: DRM device
4554 * @data: ioctl data
4555 * @file_priv: DRM file info
4556 *
4557 * This function retrieves the contents of a blob property. The value stored in
4558 * an object's blob property is just a normal modeset object id.
4559 *
4560 * Called by the user via ioctl.
4561 *
4562 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004563 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004564 */
Dave Airlief453ba02008-11-07 14:05:41 -08004565int drm_mode_getblob_ioctl(struct drm_device *dev,
4566 void *data, struct drm_file *file_priv)
4567{
Dave Airlief453ba02008-11-07 14:05:41 -08004568 struct drm_mode_get_blob *out_resp = data;
4569 struct drm_property_blob *blob;
4570 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004571 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004572
Dave Airliefb3b06c2011-02-08 13:55:21 +10004573 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4574 return -EINVAL;
4575
Daniel Vetter84849902012-12-02 00:28:11 +01004576 drm_modeset_lock_all(dev);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004577 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004578 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04004579 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004580 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004581 goto done;
4582 }
Dave Airlief453ba02008-11-07 14:05:41 -08004583
4584 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004585 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004586 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004587 ret = -EFAULT;
4588 goto done;
4589 }
4590 }
4591 out_resp->length = blob->length;
4592
4593done:
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004594 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01004595 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004596 return ret;
4597}
4598
Dave Airliecc7096f2014-10-22 12:03:04 +10004599/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004600 * drm_mode_createblob_ioctl - create a new blob property
4601 * @dev: DRM device
4602 * @data: ioctl data
4603 * @file_priv: DRM file info
4604 *
4605 * This function creates a new blob property with user-defined values. In order
4606 * to give us sensible validation and checking when creating, rather than at
4607 * every potential use, we also require a type to be provided upfront.
4608 *
4609 * Called by the user via ioctl.
4610 *
4611 * Returns:
4612 * Zero on success, negative errno on failure.
4613 */
4614int drm_mode_createblob_ioctl(struct drm_device *dev,
4615 void *data, struct drm_file *file_priv)
4616{
4617 struct drm_mode_create_blob *out_resp = data;
4618 struct drm_property_blob *blob;
4619 void __user *blob_ptr;
4620 int ret = 0;
4621
4622 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4623 return -EINVAL;
4624
4625 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4626 if (IS_ERR(blob))
4627 return PTR_ERR(blob);
4628
4629 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4630 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4631 ret = -EFAULT;
4632 goto out_blob;
4633 }
4634
4635 /* Dropping the lock between create_blob and our access here is safe
4636 * as only the same file_priv can remove the blob; at this point, it is
4637 * not associated with any file_priv. */
4638 mutex_lock(&dev->mode_config.blob_lock);
4639 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004640 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004641 mutex_unlock(&dev->mode_config.blob_lock);
4642
4643 return 0;
4644
4645out_blob:
4646 drm_property_unreference_blob(blob);
4647 return ret;
4648}
4649
4650/**
4651 * drm_mode_destroyblob_ioctl - destroy a user blob property
4652 * @dev: DRM device
4653 * @data: ioctl data
4654 * @file_priv: DRM file info
4655 *
4656 * Destroy an existing user-defined blob property.
4657 *
4658 * Called by the user via ioctl.
4659 *
4660 * Returns:
4661 * Zero on success, negative errno on failure.
4662 */
4663int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4664 void *data, struct drm_file *file_priv)
4665{
4666 struct drm_mode_destroy_blob *out_resp = data;
4667 struct drm_property_blob *blob = NULL, *bt;
4668 bool found = false;
4669 int ret = 0;
4670
4671 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4672 return -EINVAL;
4673
4674 mutex_lock(&dev->mode_config.blob_lock);
4675 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4676 if (!blob) {
4677 ret = -ENOENT;
4678 goto err;
4679 }
4680
4681 /* Ensure the property was actually created by this user. */
4682 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4683 if (bt == blob) {
4684 found = true;
4685 break;
4686 }
4687 }
4688
4689 if (!found) {
4690 ret = -EPERM;
4691 goto err;
4692 }
4693
4694 /* We must drop head_file here, because we may not be the last
4695 * reference on the blob. */
4696 list_del_init(&blob->head_file);
4697 drm_property_unreference_blob_locked(blob);
4698 mutex_unlock(&dev->mode_config.blob_lock);
4699
4700 return 0;
4701
4702err:
4703 mutex_unlock(&dev->mode_config.blob_lock);
4704 return ret;
4705}
4706
4707/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004708 * drm_mode_connector_set_path_property - set tile property on connector
4709 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004710 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004711 *
4712 * This creates a property to expose to userspace to specify a
4713 * connector path. This is mainly used for DisplayPort MST where
4714 * connectors have a topology and we want to allow userspace to give
4715 * them more meaningful names.
4716 *
4717 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004718 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004719 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004720int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004721 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004722{
4723 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004724 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004725
Daniel Stoned2ed3432015-04-20 19:22:53 +01004726 ret = drm_property_replace_global_blob(dev,
4727 &connector->path_blob_ptr,
4728 strlen(path) + 1,
4729 path,
4730 &connector->base,
4731 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004732 return ret;
4733}
4734EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4735
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004736/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004737 * drm_mode_connector_set_tile_property - set tile property on connector
4738 * @connector: connector to set property on.
4739 *
4740 * This looks up the tile information for a connector, and creates a
4741 * property for userspace to parse if it exists. The property is of
4742 * the form of 8 integers using ':' as a separator.
4743 *
4744 * Returns:
4745 * Zero on success, errno on failure.
4746 */
4747int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4748{
4749 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004750 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004751 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004752
4753 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004754 ret = drm_property_replace_global_blob(dev,
4755 &connector->tile_blob_ptr,
4756 0,
4757 NULL,
4758 &connector->base,
4759 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004760 return ret;
4761 }
4762
4763 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4764 connector->tile_group->id, connector->tile_is_single_monitor,
4765 connector->num_h_tile, connector->num_v_tile,
4766 connector->tile_h_loc, connector->tile_v_loc,
4767 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004768
Daniel Stoned2ed3432015-04-20 19:22:53 +01004769 ret = drm_property_replace_global_blob(dev,
4770 &connector->tile_blob_ptr,
4771 strlen(tile) + 1,
4772 tile,
4773 &connector->base,
4774 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004775 return ret;
4776}
4777EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4778
4779/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004780 * drm_mode_connector_update_edid_property - update the edid property of a connector
4781 * @connector: drm connector
4782 * @edid: new value of the edid property
4783 *
4784 * This function creates a new blob modeset object and assigns its id to the
4785 * connector's edid property.
4786 *
4787 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004788 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004789 */
Dave Airlief453ba02008-11-07 14:05:41 -08004790int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004791 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004792{
4793 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004794 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004795 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004796
Thomas Wood4cf2b282014-06-18 17:52:33 +01004797 /* ignore requests to set edid when overridden */
4798 if (connector->override_edid)
4799 return 0;
4800
Daniel Stoned2ed3432015-04-20 19:22:53 +01004801 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004802 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004803
Daniel Stoned2ed3432015-04-20 19:22:53 +01004804 ret = drm_property_replace_global_blob(dev,
4805 &connector->edid_blob_ptr,
4806 size,
4807 edid,
4808 &connector->base,
4809 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004810 return ret;
4811}
4812EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4813
Rob Clark3843e712014-12-16 18:05:29 -05004814/* Some properties could refer to dynamic refcnt'd objects, or things that
4815 * need special locking to handle lifetime issues (ie. to ensure the prop
4816 * value doesn't become invalid part way through the property update due to
4817 * race). The value returned by reference via 'obj' should be passed back
4818 * to drm_property_change_valid_put() after the property is set (and the
4819 * object to which the property is attached has a chance to take it's own
4820 * reference).
4821 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004822bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004823 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004824{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004825 int i;
4826
Paulo Zanoni26a34812012-05-15 18:08:59 -03004827 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4828 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004829
Rob Clark3843e712014-12-16 18:05:29 -05004830 *ref = NULL;
4831
Rob Clark5ea22f22014-05-30 11:34:01 -04004832 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004833 if (value < property->values[0] || value > property->values[1])
4834 return false;
4835 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004836 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4837 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004838
Rob Clarkebc44cf2012-09-12 22:22:31 -05004839 if (svalue < U642I64(property->values[0]) ||
4840 svalue > U642I64(property->values[1]))
4841 return false;
4842 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004843 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004844 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004845
Rob Clark49e27542012-05-17 02:23:26 -06004846 for (i = 0; i < property->num_values; i++)
4847 valid_mask |= (1ULL << property->values[i]);
4848 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004849 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004850 struct drm_property_blob *blob;
4851
4852 if (value == 0)
4853 return true;
4854
4855 blob = drm_property_lookup_blob(property->dev, value);
4856 if (blob) {
4857 *ref = &blob->base;
4858 return true;
4859 } else {
4860 return false;
4861 }
Rob Clark98f75de2014-05-30 11:37:03 -04004862 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004863 /* a zero value for an object property translates to null: */
4864 if (value == 0)
4865 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004866
4867 /* handle refcnt'd objects specially: */
4868 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4869 struct drm_framebuffer *fb;
4870 fb = drm_framebuffer_lookup(property->dev, value);
4871 if (fb) {
4872 *ref = &fb->base;
4873 return true;
4874 } else {
4875 return false;
4876 }
4877 } else {
4878 return _object_find(property->dev, value, property->values[0]) != NULL;
4879 }
Paulo Zanoni26a34812012-05-15 18:08:59 -03004880 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004881
4882 for (i = 0; i < property->num_values; i++)
4883 if (property->values[i] == value)
4884 return true;
4885 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004886}
4887
Rob Clarkd34f20d2014-12-18 16:01:56 -05004888void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004889 struct drm_mode_object *ref)
4890{
4891 if (!ref)
4892 return;
4893
4894 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4895 if (property->values[0] == DRM_MODE_OBJECT_FB)
4896 drm_framebuffer_unreference(obj_to_fb(ref));
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004897 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4898 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004899}
4900
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004901/**
4902 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4903 * @dev: DRM device
4904 * @data: ioctl data
4905 * @file_priv: DRM file info
4906 *
4907 * This function sets the current value for a connectors's property. It also
4908 * calls into a driver's ->set_property callback to update the hardware state
4909 *
4910 * Called by the user via ioctl.
4911 *
4912 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004913 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004914 */
Dave Airlief453ba02008-11-07 14:05:41 -08004915int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4916 void *data, struct drm_file *file_priv)
4917{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004918 struct drm_mode_connector_set_property *conn_set_prop = data;
4919 struct drm_mode_obj_set_property obj_set_prop = {
4920 .value = conn_set_prop->value,
4921 .prop_id = conn_set_prop->prop_id,
4922 .obj_id = conn_set_prop->connector_id,
4923 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4924 };
Dave Airlief453ba02008-11-07 14:05:41 -08004925
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004926 /* It does all the locking and checking we need */
4927 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004928}
4929
Paulo Zanonic5431882012-05-15 18:09:02 -03004930static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4931 struct drm_property *property,
4932 uint64_t value)
4933{
4934 int ret = -EINVAL;
4935 struct drm_connector *connector = obj_to_connector(obj);
4936
4937 /* Do DPMS ourselves */
4938 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004939 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004940 } else if (connector->funcs->set_property)
4941 ret = connector->funcs->set_property(connector, property, value);
4942
4943 /* store the property value if successful */
4944 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004945 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004946 return ret;
4947}
4948
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004949static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4950 struct drm_property *property,
4951 uint64_t value)
4952{
4953 int ret = -EINVAL;
4954 struct drm_crtc *crtc = obj_to_crtc(obj);
4955
4956 if (crtc->funcs->set_property)
4957 ret = crtc->funcs->set_property(crtc, property, value);
4958 if (!ret)
4959 drm_object_property_set_value(obj, property, value);
4960
4961 return ret;
4962}
4963
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004964/**
4965 * drm_mode_plane_set_obj_prop - set the value of a property
4966 * @plane: drm plane object to set property value for
4967 * @property: property to set
4968 * @value: value the property should be set to
4969 *
4970 * This functions sets a given property on a given plane object. This function
4971 * calls the driver's ->set_property callback and changes the software state of
4972 * the property if the callback succeeds.
4973 *
4974 * Returns:
4975 * Zero on success, error code on failure.
4976 */
4977int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4978 struct drm_property *property,
4979 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004980{
4981 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004982 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004983
4984 if (plane->funcs->set_property)
4985 ret = plane->funcs->set_property(plane, property, value);
4986 if (!ret)
4987 drm_object_property_set_value(obj, property, value);
4988
4989 return ret;
4990}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004991EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004992
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004993/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004994 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004995 * @dev: DRM device
4996 * @data: ioctl data
4997 * @file_priv: DRM file info
4998 *
4999 * This function retrieves the current value for an object's property. Compared
5000 * to the connector specific ioctl this one is extended to also work on crtc and
5001 * plane objects.
5002 *
5003 * Called by the user via ioctl.
5004 *
5005 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005006 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005007 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005008int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
5009 struct drm_file *file_priv)
5010{
5011 struct drm_mode_obj_get_properties *arg = data;
5012 struct drm_mode_object *obj;
5013 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03005014
5015 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5016 return -EINVAL;
5017
Daniel Vetter84849902012-12-02 00:28:11 +01005018 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005019
5020 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5021 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005022 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005023 goto out;
5024 }
5025 if (!obj->properties) {
5026 ret = -EINVAL;
5027 goto out;
5028 }
5029
Rob Clark88a48e22014-12-18 16:01:50 -05005030 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005031 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5032 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5033 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005034
Paulo Zanonic5431882012-05-15 18:09:02 -03005035out:
Daniel Vetter84849902012-12-02 00:28:11 +01005036 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005037 return ret;
5038}
5039
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005040/**
5041 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5042 * @dev: DRM device
5043 * @data: ioctl data
5044 * @file_priv: DRM file info
5045 *
5046 * This function sets the current value for an object's property. It also calls
5047 * into a driver's ->set_property callback to update the hardware state.
5048 * Compared to the connector specific ioctl this one is extended to also work on
5049 * crtc and plane objects.
5050 *
5051 * Called by the user via ioctl.
5052 *
5053 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005054 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005055 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005056int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5057 struct drm_file *file_priv)
5058{
5059 struct drm_mode_obj_set_property *arg = data;
5060 struct drm_mode_object *arg_obj;
5061 struct drm_mode_object *prop_obj;
5062 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005063 int i, ret = -EINVAL;
5064 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005065
5066 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5067 return -EINVAL;
5068
Daniel Vetter84849902012-12-02 00:28:11 +01005069 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005070
5071 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005072 if (!arg_obj) {
5073 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005074 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005075 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005076 if (!arg_obj->properties)
5077 goto out;
5078
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005079 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005080 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005081 break;
5082
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005083 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03005084 goto out;
5085
5086 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5087 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005088 if (!prop_obj) {
5089 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005090 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005091 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005092 property = obj_to_property(prop_obj);
5093
Rob Clark3843e712014-12-16 18:05:29 -05005094 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03005095 goto out;
5096
5097 switch (arg_obj->type) {
5098 case DRM_MODE_OBJECT_CONNECTOR:
5099 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5100 arg->value);
5101 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005102 case DRM_MODE_OBJECT_CRTC:
5103 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5104 break;
Rob Clark4d939142012-05-17 02:23:27 -06005105 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005106 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5107 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005108 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005109 }
5110
Rob Clark3843e712014-12-16 18:05:29 -05005111 drm_property_change_valid_put(property, ref);
5112
Paulo Zanonic5431882012-05-15 18:09:02 -03005113out:
Daniel Vetter84849902012-12-02 00:28:11 +01005114 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005115 return ret;
5116}
5117
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005118/**
5119 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5120 * @connector: connector to attach
5121 * @encoder: encoder to attach @connector to
5122 *
5123 * This function links up a connector to an encoder. Note that the routing
5124 * restrictions between encoders and crtcs are exposed to userspace through the
5125 * possible_clones and possible_crtcs bitmasks.
5126 *
5127 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005128 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005129 */
Dave Airlief453ba02008-11-07 14:05:41 -08005130int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5131 struct drm_encoder *encoder)
5132{
5133 int i;
5134
Thierry Redingeb47fe82015-11-16 18:19:53 +01005135 /*
5136 * In the past, drivers have attempted to model the static association
5137 * of connector to encoder in simple connector/encoder devices using a
5138 * direct assignment of connector->encoder = encoder. This connection
5139 * is a logical one and the responsibility of the core, so drivers are
5140 * expected not to mess with this.
5141 *
5142 * Note that the error return should've been enough here, but a large
5143 * majority of drivers ignores the return value, so add in a big WARN
5144 * to get people's attention.
5145 */
5146 if (WARN_ON(connector->encoder))
5147 return -EINVAL;
5148
Dave Airlief453ba02008-11-07 14:05:41 -08005149 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5150 if (connector->encoder_ids[i] == 0) {
5151 connector->encoder_ids[i] = encoder->base.id;
5152 return 0;
5153 }
5154 }
5155 return -ENOMEM;
5156}
5157EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5158
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005159/**
5160 * drm_mode_crtc_set_gamma_size - set the gamma table size
5161 * @crtc: CRTC to set the gamma table size for
5162 * @gamma_size: size of the gamma table
5163 *
5164 * Drivers which support gamma tables should set this to the supported gamma
5165 * table size when initializing the CRTC. Currently the drm core only supports a
5166 * fixed gamma table size.
5167 *
5168 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005169 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005170 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005171int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005172 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005173{
5174 crtc->gamma_size = gamma_size;
5175
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005176 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5177 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005178 if (!crtc->gamma_store) {
5179 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005180 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005181 }
5182
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005183 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005184}
5185EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5186
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005187/**
5188 * drm_mode_gamma_set_ioctl - set the gamma table
5189 * @dev: DRM device
5190 * @data: ioctl data
5191 * @file_priv: DRM file info
5192 *
5193 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5194 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5195 *
5196 * Called by the user via ioctl.
5197 *
5198 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005199 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005200 */
Dave Airlief453ba02008-11-07 14:05:41 -08005201int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5202 void *data, struct drm_file *file_priv)
5203{
5204 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005205 struct drm_crtc *crtc;
5206 void *r_base, *g_base, *b_base;
5207 int size;
5208 int ret = 0;
5209
Dave Airliefb3b06c2011-02-08 13:55:21 +10005210 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5211 return -EINVAL;
5212
Daniel Vetter84849902012-12-02 00:28:11 +01005213 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005214 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5215 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005216 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005217 goto out;
5218 }
Dave Airlief453ba02008-11-07 14:05:41 -08005219
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005220 if (crtc->funcs->gamma_set == NULL) {
5221 ret = -ENOSYS;
5222 goto out;
5223 }
5224
Dave Airlief453ba02008-11-07 14:05:41 -08005225 /* memcpy into gamma store */
5226 if (crtc_lut->gamma_size != crtc->gamma_size) {
5227 ret = -EINVAL;
5228 goto out;
5229 }
5230
5231 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5232 r_base = crtc->gamma_store;
5233 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5234 ret = -EFAULT;
5235 goto out;
5236 }
5237
5238 g_base = r_base + size;
5239 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5240 ret = -EFAULT;
5241 goto out;
5242 }
5243
5244 b_base = g_base + size;
5245 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5246 ret = -EFAULT;
5247 goto out;
5248 }
5249
James Simmons72034252010-08-03 01:33:19 +01005250 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005251
5252out:
Daniel Vetter84849902012-12-02 00:28:11 +01005253 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005254 return ret;
5255
5256}
5257
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005258/**
5259 * drm_mode_gamma_get_ioctl - get the gamma table
5260 * @dev: DRM device
5261 * @data: ioctl data
5262 * @file_priv: DRM file info
5263 *
5264 * Copy the current gamma table into the storage provided. This also provides
5265 * the gamma table size the driver expects, which can be used to size the
5266 * allocated storage.
5267 *
5268 * Called by the user via ioctl.
5269 *
5270 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005271 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005272 */
Dave Airlief453ba02008-11-07 14:05:41 -08005273int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5274 void *data, struct drm_file *file_priv)
5275{
5276 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005277 struct drm_crtc *crtc;
5278 void *r_base, *g_base, *b_base;
5279 int size;
5280 int ret = 0;
5281
Dave Airliefb3b06c2011-02-08 13:55:21 +10005282 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5283 return -EINVAL;
5284
Daniel Vetter84849902012-12-02 00:28:11 +01005285 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005286 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5287 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005288 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005289 goto out;
5290 }
Dave Airlief453ba02008-11-07 14:05:41 -08005291
5292 /* memcpy into gamma store */
5293 if (crtc_lut->gamma_size != crtc->gamma_size) {
5294 ret = -EINVAL;
5295 goto out;
5296 }
5297
5298 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5299 r_base = crtc->gamma_store;
5300 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5301 ret = -EFAULT;
5302 goto out;
5303 }
5304
5305 g_base = r_base + size;
5306 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5307 ret = -EFAULT;
5308 goto out;
5309 }
5310
5311 b_base = g_base + size;
5312 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5313 ret = -EFAULT;
5314 goto out;
5315 }
5316out:
Daniel Vetter84849902012-12-02 00:28:11 +01005317 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005318 return ret;
5319}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005320
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005321/**
5322 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5323 * @dev: DRM device
5324 * @data: ioctl data
5325 * @file_priv: DRM file info
5326 *
5327 * This schedules an asynchronous update on a given CRTC, called page flip.
5328 * Optionally a drm event is generated to signal the completion of the event.
5329 * Generic drivers cannot assume that a pageflip with changed framebuffer
5330 * properties (including driver specific metadata like tiling layout) will work,
5331 * but some drivers support e.g. pixel format changes through the pageflip
5332 * ioctl.
5333 *
5334 * Called by the user via ioctl.
5335 *
5336 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005337 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005338 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005339int drm_mode_page_flip_ioctl(struct drm_device *dev,
5340 void *data, struct drm_file *file_priv)
5341{
5342 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005343 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005344 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005345 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005346 int ret = -EINVAL;
5347
5348 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5349 page_flip->reserved != 0)
5350 return -EINVAL;
5351
Keith Packard62f21042013-07-22 18:50:00 -07005352 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5353 return -EINVAL;
5354
Rob Clarka2b34e22013-10-05 16:36:52 -04005355 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5356 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005357 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005358
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005359 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005360 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005361 /* The framebuffer is currently unbound, presumably
5362 * due to a hotplug event, that userspace has not
5363 * yet discovered.
5364 */
5365 ret = -EBUSY;
5366 goto out;
5367 }
5368
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005369 if (crtc->funcs->page_flip == NULL)
5370 goto out;
5371
Daniel Vetter786b99e2012-12-02 21:53:40 +01005372 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005373 if (!fb) {
5374 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005375 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005376 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005377
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005378 if (crtc->state) {
5379 const struct drm_plane_state *state = crtc->primary->state;
5380
5381 ret = check_src_coords(state->src_x, state->src_y,
5382 state->src_w, state->src_h, fb);
5383 } else {
5384 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5385 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005386 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005387 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005388
Matt Roperf4510a22014-04-01 15:22:40 -07005389 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005390 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5391 ret = -EINVAL;
5392 goto out;
5393 }
5394
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005395 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005396 e = kzalloc(sizeof *e, GFP_KERNEL);
5397 if (!e) {
5398 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005399 goto out;
5400 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005401 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005402 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005403 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005404 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5405 if (ret) {
5406 kfree(e);
5407 goto out;
5408 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005409 }
5410
Daniel Vetter3d30a592014-07-27 13:42:42 +02005411 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005412 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005413 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005414 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5415 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005416 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005417 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005418 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005419 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005420 /* Unref only the old framebuffer. */
5421 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005422 }
5423
5424out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005425 if (fb)
5426 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005427 if (crtc->primary->old_fb)
5428 drm_framebuffer_unreference(crtc->primary->old_fb);
5429 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005430 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005431
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005432 return ret;
5433}
Chris Wilsoneb033552011-01-24 15:11:08 +00005434
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005435/**
5436 * drm_mode_config_reset - call ->reset callbacks
5437 * @dev: drm device
5438 *
5439 * This functions calls all the crtc's, encoder's and connector's ->reset
5440 * callback. Drivers can use this in e.g. their driver load or resume code to
5441 * reset hardware and software state.
5442 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005443void drm_mode_config_reset(struct drm_device *dev)
5444{
5445 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005446 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005447 struct drm_encoder *encoder;
5448 struct drm_connector *connector;
5449
Daniel Vettere4f62542015-07-09 23:44:35 +02005450 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005451 if (plane->funcs->reset)
5452 plane->funcs->reset(plane);
5453
Daniel Vettere4f62542015-07-09 23:44:35 +02005454 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005455 if (crtc->funcs->reset)
5456 crtc->funcs->reset(crtc);
5457
Daniel Vettere4f62542015-07-09 23:44:35 +02005458 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005459 if (encoder->funcs->reset)
5460 encoder->funcs->reset(encoder);
5461
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005462 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005463 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005464 if (connector->funcs->reset)
5465 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005466 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005467}
5468EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005469
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005470/**
5471 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5472 * @dev: DRM device
5473 * @data: ioctl data
5474 * @file_priv: DRM file info
5475 *
5476 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5477 * TTM or something else entirely) and returns the resulting buffer handle. This
5478 * handle can then be wrapped up into a framebuffer modeset object.
5479 *
5480 * Note that userspace is not allowed to use such objects for render
5481 * acceleration - drivers must create their own private ioctls for such a use
5482 * case.
5483 *
5484 * Called by the user via ioctl.
5485 *
5486 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005487 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005488 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005489int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5490 void *data, struct drm_file *file_priv)
5491{
5492 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005493 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005494
5495 if (!dev->driver->dumb_create)
5496 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005497 if (!args->width || !args->height || !args->bpp)
5498 return -EINVAL;
5499
5500 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005501 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005502 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005503 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005504 return -EINVAL;
5505 stride = cpp * args->width;
5506 if (args->height > 0xffffffffU / stride)
5507 return -EINVAL;
5508
5509 /* test for wrap-around */
5510 size = args->height * stride;
5511 if (PAGE_ALIGN(size) == 0)
5512 return -EINVAL;
5513
Thierry Redingf6085952014-11-03 11:14:14 +01005514 /*
5515 * handle, pitch and size are output parameters. Zero them out to
5516 * prevent drivers from accidentally using uninitialized data. Since
5517 * not all existing userspace is clearing these fields properly we
5518 * cannot reject IOCTL with garbage in them.
5519 */
5520 args->handle = 0;
5521 args->pitch = 0;
5522 args->size = 0;
5523
Dave Airlieff72145b2011-02-07 12:16:14 +10005524 return dev->driver->dumb_create(file_priv, dev, args);
5525}
5526
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005527/**
5528 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5529 * @dev: DRM device
5530 * @data: ioctl data
5531 * @file_priv: DRM file info
5532 *
5533 * Allocate an offset in the drm device node's address space to be able to
5534 * memory map a dumb buffer.
5535 *
5536 * Called by the user via ioctl.
5537 *
5538 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005539 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005540 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005541int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5542 void *data, struct drm_file *file_priv)
5543{
5544 struct drm_mode_map_dumb *args = data;
5545
5546 /* call driver ioctl to get mmap offset */
5547 if (!dev->driver->dumb_map_offset)
5548 return -ENOSYS;
5549
5550 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5551}
5552
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005553/**
5554 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5555 * @dev: DRM device
5556 * @data: ioctl data
5557 * @file_priv: DRM file info
5558 *
5559 * This destroys the userspace handle for the given dumb backing storage buffer.
5560 * Since buffer objects must be reference counted in the kernel a buffer object
5561 * won't be immediately freed if a framebuffer modeset object still uses it.
5562 *
5563 * Called by the user via ioctl.
5564 *
5565 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005566 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005567 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005568int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5569 void *data, struct drm_file *file_priv)
5570{
5571 struct drm_mode_destroy_dumb *args = data;
5572
5573 if (!dev->driver->dumb_destroy)
5574 return -ENOSYS;
5575
5576 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5577}
Dave Airlie248dbc22011-11-29 20:02:54 +00005578
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005579/**
5580 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5581 * @format: pixel format (DRM_FORMAT_*)
5582 * @depth: storage for the depth value
5583 * @bpp: storage for the bpp value
5584 *
5585 * This only supports RGB formats here for compat with code that doesn't use
5586 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005587 */
5588void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5589 int *bpp)
5590{
5591 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005592 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005593 case DRM_FORMAT_RGB332:
5594 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005595 *depth = 8;
5596 *bpp = 8;
5597 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005598 case DRM_FORMAT_XRGB1555:
5599 case DRM_FORMAT_XBGR1555:
5600 case DRM_FORMAT_RGBX5551:
5601 case DRM_FORMAT_BGRX5551:
5602 case DRM_FORMAT_ARGB1555:
5603 case DRM_FORMAT_ABGR1555:
5604 case DRM_FORMAT_RGBA5551:
5605 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005606 *depth = 15;
5607 *bpp = 16;
5608 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005609 case DRM_FORMAT_RGB565:
5610 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005611 *depth = 16;
5612 *bpp = 16;
5613 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005614 case DRM_FORMAT_RGB888:
5615 case DRM_FORMAT_BGR888:
5616 *depth = 24;
5617 *bpp = 24;
5618 break;
5619 case DRM_FORMAT_XRGB8888:
5620 case DRM_FORMAT_XBGR8888:
5621 case DRM_FORMAT_RGBX8888:
5622 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005623 *depth = 24;
5624 *bpp = 32;
5625 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005626 case DRM_FORMAT_XRGB2101010:
5627 case DRM_FORMAT_XBGR2101010:
5628 case DRM_FORMAT_RGBX1010102:
5629 case DRM_FORMAT_BGRX1010102:
5630 case DRM_FORMAT_ARGB2101010:
5631 case DRM_FORMAT_ABGR2101010:
5632 case DRM_FORMAT_RGBA1010102:
5633 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005634 *depth = 30;
5635 *bpp = 32;
5636 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005637 case DRM_FORMAT_ARGB8888:
5638 case DRM_FORMAT_ABGR8888:
5639 case DRM_FORMAT_RGBA8888:
5640 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005641 *depth = 32;
5642 *bpp = 32;
5643 break;
5644 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005645 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5646 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005647 *depth = 0;
5648 *bpp = 0;
5649 break;
5650 }
5651}
5652EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005653
5654/**
5655 * drm_format_num_planes - get the number of planes for format
5656 * @format: pixel format (DRM_FORMAT_*)
5657 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005658 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005659 * The number of planes used by the specified pixel format.
5660 */
5661int drm_format_num_planes(uint32_t format)
5662{
5663 switch (format) {
5664 case DRM_FORMAT_YUV410:
5665 case DRM_FORMAT_YVU410:
5666 case DRM_FORMAT_YUV411:
5667 case DRM_FORMAT_YVU411:
5668 case DRM_FORMAT_YUV420:
5669 case DRM_FORMAT_YVU420:
5670 case DRM_FORMAT_YUV422:
5671 case DRM_FORMAT_YVU422:
5672 case DRM_FORMAT_YUV444:
5673 case DRM_FORMAT_YVU444:
5674 return 3;
5675 case DRM_FORMAT_NV12:
5676 case DRM_FORMAT_NV21:
5677 case DRM_FORMAT_NV16:
5678 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005679 case DRM_FORMAT_NV24:
5680 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005681 return 2;
5682 default:
5683 return 1;
5684 }
5685}
5686EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005687
5688/**
5689 * drm_format_plane_cpp - determine the bytes per pixel value
5690 * @format: pixel format (DRM_FORMAT_*)
5691 * @plane: plane index
5692 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005693 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005694 * The bytes per pixel value for the specified plane.
5695 */
5696int drm_format_plane_cpp(uint32_t format, int plane)
5697{
5698 unsigned int depth;
5699 int bpp;
5700
5701 if (plane >= drm_format_num_planes(format))
5702 return 0;
5703
5704 switch (format) {
5705 case DRM_FORMAT_YUYV:
5706 case DRM_FORMAT_YVYU:
5707 case DRM_FORMAT_UYVY:
5708 case DRM_FORMAT_VYUY:
5709 return 2;
5710 case DRM_FORMAT_NV12:
5711 case DRM_FORMAT_NV21:
5712 case DRM_FORMAT_NV16:
5713 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005714 case DRM_FORMAT_NV24:
5715 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005716 return plane ? 2 : 1;
5717 case DRM_FORMAT_YUV410:
5718 case DRM_FORMAT_YVU410:
5719 case DRM_FORMAT_YUV411:
5720 case DRM_FORMAT_YVU411:
5721 case DRM_FORMAT_YUV420:
5722 case DRM_FORMAT_YVU420:
5723 case DRM_FORMAT_YUV422:
5724 case DRM_FORMAT_YVU422:
5725 case DRM_FORMAT_YUV444:
5726 case DRM_FORMAT_YVU444:
5727 return 1;
5728 default:
5729 drm_fb_get_bpp_depth(format, &depth, &bpp);
5730 return bpp >> 3;
5731 }
5732}
5733EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005734
5735/**
5736 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5737 * @format: pixel format (DRM_FORMAT_*)
5738 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005739 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005740 * The horizontal chroma subsampling factor for the
5741 * specified pixel format.
5742 */
5743int drm_format_horz_chroma_subsampling(uint32_t format)
5744{
5745 switch (format) {
5746 case DRM_FORMAT_YUV411:
5747 case DRM_FORMAT_YVU411:
5748 case DRM_FORMAT_YUV410:
5749 case DRM_FORMAT_YVU410:
5750 return 4;
5751 case DRM_FORMAT_YUYV:
5752 case DRM_FORMAT_YVYU:
5753 case DRM_FORMAT_UYVY:
5754 case DRM_FORMAT_VYUY:
5755 case DRM_FORMAT_NV12:
5756 case DRM_FORMAT_NV21:
5757 case DRM_FORMAT_NV16:
5758 case DRM_FORMAT_NV61:
5759 case DRM_FORMAT_YUV422:
5760 case DRM_FORMAT_YVU422:
5761 case DRM_FORMAT_YUV420:
5762 case DRM_FORMAT_YVU420:
5763 return 2;
5764 default:
5765 return 1;
5766 }
5767}
5768EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5769
5770/**
5771 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5772 * @format: pixel format (DRM_FORMAT_*)
5773 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005774 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005775 * The vertical chroma subsampling factor for the
5776 * specified pixel format.
5777 */
5778int drm_format_vert_chroma_subsampling(uint32_t format)
5779{
5780 switch (format) {
5781 case DRM_FORMAT_YUV410:
5782 case DRM_FORMAT_YVU410:
5783 return 4;
5784 case DRM_FORMAT_YUV420:
5785 case DRM_FORMAT_YVU420:
5786 case DRM_FORMAT_NV12:
5787 case DRM_FORMAT_NV21:
5788 return 2;
5789 default:
5790 return 1;
5791 }
5792}
5793EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005794
5795/**
Ville Syrjälä4c617162016-02-09 17:29:44 +02005796 * drm_format_plane_width - width of the plane given the first plane
5797 * @width: width of the first plane
5798 * @format: pixel format
5799 * @plane: plane index
5800 *
5801 * Returns:
5802 * The width of @plane, given that the width of the first plane is @width.
5803 */
5804int drm_format_plane_width(int width, uint32_t format, int plane)
5805{
5806 if (plane >= drm_format_num_planes(format))
5807 return 0;
5808
5809 if (plane == 0)
5810 return width;
5811
5812 return width / drm_format_horz_chroma_subsampling(format);
5813}
5814EXPORT_SYMBOL(drm_format_plane_width);
5815
5816/**
5817 * drm_format_plane_height - height of the plane given the first plane
5818 * @height: height of the first plane
5819 * @format: pixel format
5820 * @plane: plane index
5821 *
5822 * Returns:
5823 * The height of @plane, given that the height of the first plane is @height.
5824 */
5825int drm_format_plane_height(int height, uint32_t format, int plane)
5826{
5827 if (plane >= drm_format_num_planes(format))
5828 return 0;
5829
5830 if (plane == 0)
5831 return height;
5832
5833 return height / drm_format_vert_chroma_subsampling(format);
5834}
5835EXPORT_SYMBOL(drm_format_plane_height);
5836
5837/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305838 * drm_rotation_simplify() - Try to simplify the rotation
5839 * @rotation: Rotation to be simplified
5840 * @supported_rotations: Supported rotations
5841 *
5842 * Attempt to simplify the rotation to a form that is supported.
5843 * Eg. if the hardware supports everything except DRM_REFLECT_X
5844 * one could call this function like this:
5845 *
5846 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5847 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5848 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5849 *
5850 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5851 * transforms the hardware supports, this function may not
5852 * be able to produce a supported transform, so the caller should
5853 * check the result afterwards.
5854 */
5855unsigned int drm_rotation_simplify(unsigned int rotation,
5856 unsigned int supported_rotations)
5857{
5858 if (rotation & ~supported_rotations) {
5859 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005860 rotation = (rotation & DRM_REFLECT_MASK) |
5861 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305862 }
5863
5864 return rotation;
5865}
5866EXPORT_SYMBOL(drm_rotation_simplify);
5867
5868/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005869 * drm_mode_config_init - initialize DRM mode_configuration structure
5870 * @dev: DRM device
5871 *
5872 * Initialize @dev's mode_config structure, used for tracking the graphics
5873 * configuration of @dev.
5874 *
5875 * Since this initializes the modeset locks, no locking is possible. Which is no
5876 * problem, since this should happen single threaded at init time. It is the
5877 * driver's problem to ensure this guarantee.
5878 *
5879 */
5880void drm_mode_config_init(struct drm_device *dev)
5881{
5882 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005883 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005884 mutex_init(&dev->mode_config.idr_mutex);
5885 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005886 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005887 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5888 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5889 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5890 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5891 INIT_LIST_HEAD(&dev->mode_config.property_list);
5892 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5893 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5894 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005895 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005896 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005897
5898 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005899 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005900 drm_modeset_unlock_all(dev);
5901
5902 /* Just to be sure */
5903 dev->mode_config.num_fb = 0;
5904 dev->mode_config.num_connector = 0;
5905 dev->mode_config.num_crtc = 0;
5906 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005907 dev->mode_config.num_overlay_plane = 0;
5908 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005909}
5910EXPORT_SYMBOL(drm_mode_config_init);
5911
5912/**
5913 * drm_mode_config_cleanup - free up DRM mode_config info
5914 * @dev: DRM device
5915 *
5916 * Free up all the connectors and CRTCs associated with this DRM device, then
5917 * free up the framebuffers and associated buffer objects.
5918 *
5919 * Note that since this /should/ happen single-threaded at driver/device
5920 * teardown time, no locking is required. It's the driver's job to ensure that
5921 * this guarantee actually holds true.
5922 *
5923 * FIXME: cleanup any dangling user buffer objects too
5924 */
5925void drm_mode_config_cleanup(struct drm_device *dev)
5926{
5927 struct drm_connector *connector, *ot;
5928 struct drm_crtc *crtc, *ct;
5929 struct drm_encoder *encoder, *enct;
5930 struct drm_framebuffer *fb, *fbt;
5931 struct drm_property *property, *pt;
5932 struct drm_property_blob *blob, *bt;
5933 struct drm_plane *plane, *plt;
5934
5935 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5936 head) {
5937 encoder->funcs->destroy(encoder);
5938 }
5939
5940 list_for_each_entry_safe(connector, ot,
5941 &dev->mode_config.connector_list, head) {
5942 connector->funcs->destroy(connector);
5943 }
5944
5945 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5946 head) {
5947 drm_property_destroy(dev, property);
5948 }
5949
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005950 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5951 head) {
5952 plane->funcs->destroy(plane);
5953 }
5954
5955 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5956 crtc->funcs->destroy(crtc);
5957 }
5958
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005959 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005960 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005961 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005962 }
5963
5964 /*
5965 * Single-threaded teardown context, so it's not required to grab the
5966 * fb_lock to protect against concurrent fb_list access. Contrary, it
5967 * would actually deadlock with the drm_framebuffer_cleanup function.
5968 *
5969 * Also, if there are any framebuffers left, that's a driver leak now,
5970 * so politely WARN about this.
5971 */
5972 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5973 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Maarten Lankhorstc099b552015-09-09 13:46:21 +02005974 drm_framebuffer_free(&fb->refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005975 }
5976
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005977 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005978 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005979 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005980 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005981}
5982EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305983
5984struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5985 unsigned int supported_rotations)
5986{
5987 static const struct drm_prop_enum_list props[] = {
5988 { DRM_ROTATE_0, "rotate-0" },
5989 { DRM_ROTATE_90, "rotate-90" },
5990 { DRM_ROTATE_180, "rotate-180" },
5991 { DRM_ROTATE_270, "rotate-270" },
5992 { DRM_REFLECT_X, "reflect-x" },
5993 { DRM_REFLECT_Y, "reflect-y" },
5994 };
5995
5996 return drm_property_create_bitmask(dev, 0, "rotation",
5997 props, ARRAY_SIZE(props),
5998 supported_rotations);
5999}
6000EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10006001
6002/**
6003 * DOC: Tile group
6004 *
6005 * Tile groups are used to represent tiled monitors with a unique
6006 * integer identifier. Tiled monitors using DisplayID v1.3 have
6007 * a unique 8-byte handle, we store this in a tile group, so we
6008 * have a common identifier for all tiles in a monitor group.
6009 */
6010static void drm_tile_group_free(struct kref *kref)
6011{
6012 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
6013 struct drm_device *dev = tg->dev;
6014 mutex_lock(&dev->mode_config.idr_mutex);
6015 idr_remove(&dev->mode_config.tile_idr, tg->id);
6016 mutex_unlock(&dev->mode_config.idr_mutex);
6017 kfree(tg);
6018}
6019
6020/**
6021 * drm_mode_put_tile_group - drop a reference to a tile group.
6022 * @dev: DRM device
6023 * @tg: tile group to drop reference to.
6024 *
6025 * drop reference to tile group and free if 0.
6026 */
6027void drm_mode_put_tile_group(struct drm_device *dev,
6028 struct drm_tile_group *tg)
6029{
6030 kref_put(&tg->refcount, drm_tile_group_free);
6031}
6032
6033/**
6034 * drm_mode_get_tile_group - get a reference to an existing tile group
6035 * @dev: DRM device
6036 * @topology: 8-bytes unique per monitor.
6037 *
6038 * Use the unique bytes to get a reference to an existing tile group.
6039 *
6040 * RETURNS:
6041 * tile group or NULL if not found.
6042 */
6043struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6044 char topology[8])
6045{
6046 struct drm_tile_group *tg;
6047 int id;
6048 mutex_lock(&dev->mode_config.idr_mutex);
6049 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6050 if (!memcmp(tg->group_data, topology, 8)) {
6051 if (!kref_get_unless_zero(&tg->refcount))
6052 tg = NULL;
6053 mutex_unlock(&dev->mode_config.idr_mutex);
6054 return tg;
6055 }
6056 }
6057 mutex_unlock(&dev->mode_config.idr_mutex);
6058 return NULL;
6059}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006060EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10006061
6062/**
6063 * drm_mode_create_tile_group - create a tile group from a displayid description
6064 * @dev: DRM device
6065 * @topology: 8-bytes unique per monitor.
6066 *
6067 * Create a tile group for the unique monitor, and get a unique
6068 * identifier for the tile group.
6069 *
6070 * RETURNS:
6071 * new tile group or error.
6072 */
6073struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6074 char topology[8])
6075{
6076 struct drm_tile_group *tg;
6077 int ret;
6078
6079 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6080 if (!tg)
6081 return ERR_PTR(-ENOMEM);
6082
6083 kref_init(&tg->refcount);
6084 memcpy(tg->group_data, topology, 8);
6085 tg->dev = dev;
6086
6087 mutex_lock(&dev->mode_config.idr_mutex);
6088 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6089 if (ret >= 0) {
6090 tg->id = ret;
6091 } else {
6092 kfree(tg);
6093 tg = ERR_PTR(ret);
6094 }
6095
6096 mutex_unlock(&dev->mode_config.idr_mutex);
6097 return tg;
6098}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006099EXPORT_SYMBOL(drm_mode_create_tile_group);