blob: d078a5c34d48a1a06f5bfb07c452e1153ac9d95f [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,
Dave Airlied0f37cf2016-04-15 15:10:36 +1000278 bool register_obj,
279 void (*obj_free_cb)(struct kref *kref))
Dave Airlie2ee39452014-07-24 11:53:45 +1000280{
281 int ret;
282
283 mutex_lock(&dev->mode_config.idr_mutex);
284 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
285 if (ret >= 0) {
286 /*
287 * Set up the object linking under the protection of the idr
288 * lock so that other users can't see inconsistent state.
289 */
290 obj->id = ret;
291 obj->type = obj_type;
Dave Airlied0f37cf2016-04-15 15:10:36 +1000292 if (obj_free_cb) {
293 obj->free_cb = obj_free_cb;
294 kref_init(&obj->refcount);
295 }
Dave Airlie2ee39452014-07-24 11:53:45 +1000296 }
297 mutex_unlock(&dev->mode_config.idr_mutex);
298
299 return ret < 0 ? ret : 0;
300}
301
Dave Airlief453ba02008-11-07 14:05:41 -0800302/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100303 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800304 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100305 * @obj: object pointer, used to generate unique ID
306 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800307 *
Dave Airlief453ba02008-11-07 14:05:41 -0800308 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100309 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
310 * modeset identifiers are _not_ reference counted. Hence don't use this for
311 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800312 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100313 * Returns:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200314 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800315 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100316int drm_mode_object_get(struct drm_device *dev,
317 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800318{
Dave Airlied0f37cf2016-04-15 15:10:36 +1000319 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
Dave Airlie2ee39452014-07-24 11:53:45 +1000320}
Dave Airlief453ba02008-11-07 14:05:41 -0800321
Dave Airlie2ee39452014-07-24 11:53:45 +1000322static void drm_mode_object_register(struct drm_device *dev,
323 struct drm_mode_object *obj)
324{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000325 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000326 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000327 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800328}
329
330/**
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000331 * drm_mode_object_unregister - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800332 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100333 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800334 *
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000335 * Free @id from @dev's unique identifier pool.
336 * This function can be called multiple times, and guards against
337 * multiple removals.
338 * These modeset identifiers are _not_ reference counted. Hence don't use this
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100339 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800340 */
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000341void drm_mode_object_unregister(struct drm_device *dev,
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100342 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800343{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000344 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000345 if (object->id) {
346 idr_remove(&dev->mode_config.crtc_idr, object->id);
347 object->id = 0;
348 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000349 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800350}
351
Rob Clark98f75de2014-05-30 11:37:03 -0400352static struct drm_mode_object *_object_find(struct drm_device *dev,
353 uint32_t id, uint32_t type)
354{
355 struct drm_mode_object *obj = NULL;
356
357 mutex_lock(&dev->mode_config.idr_mutex);
358 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200359 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
360 obj = NULL;
361 if (obj && obj->id != id)
362 obj = NULL;
363 /* don't leak out unref'd fb's */
Daniel Stone6bcacf52015-04-20 19:22:55 +0100364 if (obj &&
Dave Airliecee26ac2016-04-15 15:10:37 +1000365 obj->type == DRM_MODE_OBJECT_BLOB)
Rob Clark98f75de2014-05-30 11:37:03 -0400366 obj = NULL;
Dave Airlie72fe90b2016-04-15 15:10:40 +1000367
368 if (obj && obj->free_cb) {
369 if (!kref_get_unless_zero(&obj->refcount))
370 obj = NULL;
371 }
Rob Clark98f75de2014-05-30 11:37:03 -0400372 mutex_unlock(&dev->mode_config.idr_mutex);
373
374 return obj;
375}
376
Daniel Vetter786b99e2012-12-02 21:53:40 +0100377/**
378 * drm_mode_object_find - look up a drm object with static lifetime
379 * @dev: drm device
380 * @id: id of the mode object
381 * @type: type of the mode object
382 *
383 * Note that framebuffers cannot be looked up with this functions - since those
Rob Clark98f75de2014-05-30 11:37:03 -0400384 * are reference counted, they need special treatment. Even with
385 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
386 * rather than WARN_ON()).
Daniel Vetter786b99e2012-12-02 21:53:40 +0100387 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200388struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
389 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800390{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000391 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800392
Daniel Vetter786b99e2012-12-02 21:53:40 +0100393 /* Framebuffers are reference counted and need their own lookup
394 * function.*/
Daniel Stone6bcacf52015-04-20 19:22:55 +0100395 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
Rob Clark98f75de2014-05-30 11:37:03 -0400396 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800397 return obj;
398}
399EXPORT_SYMBOL(drm_mode_object_find);
400
Dave Airlied0f37cf2016-04-15 15:10:36 +1000401void drm_mode_object_unreference(struct drm_mode_object *obj)
402{
403 if (obj->free_cb) {
404 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
405 kref_put(&obj->refcount, obj->free_cb);
406 }
407}
408EXPORT_SYMBOL(drm_mode_object_unreference);
409
410/**
411 * drm_mode_object_reference - incr the fb refcnt
412 * @obj: mode_object
413 *
414 * This function operates only on refcounted objects.
415 * This functions increments the object's refcount.
416 */
417void drm_mode_object_reference(struct drm_mode_object *obj)
418{
419 if (obj->free_cb) {
420 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
421 kref_get(&obj->refcount);
422 }
423}
424EXPORT_SYMBOL(drm_mode_object_reference);
425
Dave Airlief55f1f92016-04-15 15:10:33 +1000426static void drm_framebuffer_free(struct kref *kref)
427{
428 struct drm_framebuffer *fb =
Dave Airlied0f37cf2016-04-15 15:10:36 +1000429 container_of(kref, struct drm_framebuffer, base.refcount);
Dave Airlief55f1f92016-04-15 15:10:33 +1000430 struct drm_device *dev = fb->dev;
431
432 /*
433 * The lookup idr holds a weak reference, which has not necessarily been
434 * removed at this point. Check for that.
435 */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000436 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000437
438 fb->funcs->destroy(fb);
439}
440
Dave Airlief453ba02008-11-07 14:05:41 -0800441/**
Dave Airlief453ba02008-11-07 14:05:41 -0800442 * drm_framebuffer_init - initialize a framebuffer
443 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100444 * @fb: framebuffer to be initialized
445 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800446 *
Dave Airlief453ba02008-11-07 14:05:41 -0800447 * Allocates an ID for the framebuffer's parent mode object, sets its mode
448 * functions & device file and adds it to the master fd list.
449 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100450 * IMPORTANT:
451 * This functions publishes the fb and makes it available for concurrent access
452 * by other users. Which means by this point the fb _must_ be fully set up -
453 * since all the fb attributes are invariant over its lifetime, no further
454 * locking but only correct reference counting is required.
455 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100456 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200457 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800458 */
459int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
460 const struct drm_framebuffer_funcs *funcs)
461{
462 int ret;
463
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100464 INIT_LIST_HEAD(&fb->filp_head);
465 fb->dev = dev;
466 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000467
Dave Airlied0f37cf2016-04-15 15:10:36 +1000468 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
Dave Airlie9cd47422016-04-15 15:10:38 +1000469 false, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200470 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100471 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800472
Dave Airlie9cd47422016-04-15 15:10:38 +1000473 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800474 dev->mode_config.num_fb++;
475 list_add(&fb->head, &dev->mode_config.fb_list);
Dave Airlie2ddea3f2016-04-15 15:10:41 +1000476 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800477
Dave Airlie9cd47422016-04-15 15:10:38 +1000478 drm_mode_object_register(dev, &fb->base);
Dave Airlie9cd47422016-04-15 15:10:38 +1000479out:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200480 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800481}
482EXPORT_SYMBOL(drm_framebuffer_init);
483
Rob Clarkf7eff602012-09-05 21:48:38 +0000484/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100485 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
486 * @dev: drm device
487 * @id: id of the fb object
488 *
489 * If successful, this grabs an additional reference to the framebuffer -
490 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100491 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100492 */
493struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
494 uint32_t id)
495{
Dave Airliecee26ac2016-04-15 15:10:37 +1000496 struct drm_mode_object *obj;
497 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100498
Dave Airliecee26ac2016-04-15 15:10:37 +1000499 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
Dave Airlie72fe90b2016-04-15 15:10:40 +1000500 if (obj)
Dave Airliecee26ac2016-04-15 15:10:37 +1000501 fb = obj_to_fb(obj);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100502 return fb;
503}
504EXPORT_SYMBOL(drm_framebuffer_lookup);
505
506/**
Daniel Vetter36206362012-12-10 20:42:17 +0100507 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
508 * @fb: fb to unregister
509 *
510 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
511 * those used for fbdev. Note that the caller must hold a reference of it's own,
512 * i.e. the object may not be destroyed through this call (since it'll lead to a
513 * locking inversion).
514 */
515void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
516{
Daniel Vettera39a3572015-08-25 15:45:11 +0200517 struct drm_device *dev;
518
519 if (!fb)
520 return;
521
522 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100523
Daniel Vetter2b677e82012-12-10 21:16:05 +0100524 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000525 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter36206362012-12-10 20:42:17 +0100526}
527EXPORT_SYMBOL(drm_framebuffer_unregister_private);
528
529/**
Dave Airlief453ba02008-11-07 14:05:41 -0800530 * drm_framebuffer_cleanup - remove a framebuffer object
531 * @fb: framebuffer to remove
532 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100533 * Cleanup framebuffer. This function is intended to be used from the drivers
534 * ->destroy callback. It can also be used to clean up driver private
535 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100536 *
537 * Note that this function does not remove the fb from active usuage - if it is
538 * still used anywhere, hilarity can ensue since userspace could call getfb on
539 * the id and get back -EINVAL. Obviously no concern at driver unload time.
540 *
541 * Also, the framebuffer will not be removed from the lookup idr - for
542 * user-created framebuffers this will happen in in the rmfb ioctl. For
543 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
544 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800545 */
546void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
547{
548 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100549
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100550 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000551 list_del(&fb->head);
552 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100553 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000554}
555EXPORT_SYMBOL(drm_framebuffer_cleanup);
556
557/**
558 * drm_framebuffer_remove - remove and unreference a framebuffer object
559 * @fb: framebuffer to remove
560 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000561 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100562 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100563 * passed-in framebuffer. Might take the modeset locks.
564 *
565 * Note that this function optimizes the cleanup away if the caller holds the
566 * last reference to the framebuffer. It is also guaranteed to not take the
567 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000568 */
569void drm_framebuffer_remove(struct drm_framebuffer *fb)
570{
Daniel Vettera39a3572015-08-25 15:45:11 +0200571 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800572 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800573 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000574 struct drm_mode_set set;
575 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800576
Daniel Vettera39a3572015-08-25 15:45:11 +0200577 if (!fb)
578 return;
579
580 dev = fb->dev;
581
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100582 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100583
Daniel Vetterb62584e2012-12-11 16:51:35 +0100584 /*
585 * drm ABI mandates that we remove any deleted framebuffers from active
586 * useage. But since most sane clients only remove framebuffers they no
587 * longer need, try to optimize this away.
588 *
589 * Since we're holding a reference ourselves, observing a refcount of 1
590 * means that we're the last holder and can skip it. Also, the refcount
591 * can never increase from 1 again, so we don't need any barriers or
592 * locks.
593 *
594 * Note that userspace could try to race with use and instate a new
595 * usage _after_ we've cleared all current ones. End result will be an
596 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
597 * in this manner.
598 */
Dave Airlie747a5982016-04-15 15:10:35 +1000599 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100600 drm_modeset_lock_all(dev);
601 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200602 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700603 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100604 /* should turn off the crtc */
605 memset(&set, 0, sizeof(struct drm_mode_set));
606 set.crtc = crtc;
607 set.fb = NULL;
608 ret = drm_mode_set_config_internal(&set);
609 if (ret)
610 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
611 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000612 }
Dave Airlief453ba02008-11-07 14:05:41 -0800613
Daniel Vetter6295d602015-07-09 23:44:25 +0200614 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300615 if (plane->fb == fb)
616 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800617 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100618 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800619 }
620
Rob Clarkf7eff602012-09-05 21:48:38 +0000621 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800622}
Rob Clarkf7eff602012-09-05 21:48:38 +0000623EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800624
Rob Clark51fd3712013-11-19 12:10:12 -0500625DEFINE_WW_CLASS(crtc_ww_class);
626
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200627static unsigned int drm_num_crtcs(struct drm_device *dev)
628{
629 unsigned int num = 0;
630 struct drm_crtc *tmp;
631
632 drm_for_each_crtc(tmp, dev) {
633 num++;
634 }
635
636 return num;
637}
638
Dave Airlief453ba02008-11-07 14:05:41 -0800639/**
Matt Ropere13161a2014-04-01 15:22:38 -0700640 * drm_crtc_init_with_planes - Initialise a new CRTC object with
641 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800642 * @dev: DRM device
643 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700644 * @primary: Primary plane for CRTC
645 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800646 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200647 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800648 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000649 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200650 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100651 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200652 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800653 */
Matt Ropere13161a2014-04-01 15:22:38 -0700654int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
655 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700656 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200657 const struct drm_crtc_funcs *funcs,
658 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800659{
Rob Clark51fd3712013-11-19 12:10:12 -0500660 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200661 int ret;
662
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100663 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
664 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
665
Dave Airlief453ba02008-11-07 14:05:41 -0800666 crtc->dev = dev;
667 crtc->funcs = funcs;
668
Rob Clark51fd3712013-11-19 12:10:12 -0500669 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200670 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
671 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100672 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800673
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200674 if (name) {
675 va_list ap;
676
677 va_start(ap, name);
678 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
679 va_end(ap);
680 } else {
681 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
682 drm_num_crtcs(dev));
683 }
684 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000685 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200686 return -ENOMEM;
687 }
688
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300689 crtc->base.properties = &crtc->properties;
690
Rob Clark51fd3712013-11-19 12:10:12 -0500691 list_add_tail(&crtc->head, &config->crtc_list);
692 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200693
Matt Ropere13161a2014-04-01 15:22:38 -0700694 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700695 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700696 if (primary)
697 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700698 if (cursor)
699 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700700
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100701 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
702 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100703 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100704 }
705
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100706 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800707}
Matt Ropere13161a2014-04-01 15:22:38 -0700708EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800709
710/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000711 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800712 * @crtc: CRTC to cleanup
713 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000714 * This function cleans up @crtc and removes it from the DRM mode setting
715 * core. Note that the function does *not* free the crtc structure itself,
716 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800717 */
718void drm_crtc_cleanup(struct drm_crtc *crtc)
719{
720 struct drm_device *dev = crtc->dev;
721
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000722 kfree(crtc->gamma_store);
723 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800724
Rob Clark51fd3712013-11-19 12:10:12 -0500725 drm_modeset_lock_fini(&crtc->mutex);
726
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000727 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800728 list_del(&crtc->head);
729 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100730
731 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
732 if (crtc->state && crtc->funcs->atomic_destroy_state)
733 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100734
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200735 kfree(crtc->name);
736
Thierry Redinga18c0af2014-12-10 11:38:49 +0100737 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800738}
739EXPORT_SYMBOL(drm_crtc_cleanup);
740
741/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000742 * drm_crtc_index - find the index of a registered CRTC
743 * @crtc: CRTC to find index for
744 *
745 * Given a registered CRTC, return the index of that CRTC within a DRM
746 * device's list of CRTCs.
747 */
748unsigned int drm_crtc_index(struct drm_crtc *crtc)
749{
750 unsigned int index = 0;
751 struct drm_crtc *tmp;
752
Daniel Vettere4f62542015-07-09 23:44:35 +0200753 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000754 if (tmp == crtc)
755 return index;
756
757 index++;
758 }
759
760 BUG();
761}
762EXPORT_SYMBOL(drm_crtc_index);
763
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100764/*
Dave Airlief453ba02008-11-07 14:05:41 -0800765 * drm_mode_remove - remove and free a mode
766 * @connector: connector list to modify
767 * @mode: mode to remove
768 *
Dave Airlief453ba02008-11-07 14:05:41 -0800769 * Remove @mode from @connector's mode list, then free it.
770 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100771static void drm_mode_remove(struct drm_connector *connector,
772 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800773{
774 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100775 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800776}
Dave Airlief453ba02008-11-07 14:05:41 -0800777
778/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200779 * drm_display_info_set_bus_formats - set the supported bus formats
780 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100781 * @formats: array containing the supported bus formats
782 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200783 *
784 * Store the supported bus formats in display info structure.
785 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
786 * a full list of available formats.
787 */
788int drm_display_info_set_bus_formats(struct drm_display_info *info,
789 const u32 *formats,
790 unsigned int num_formats)
791{
792 u32 *fmts = NULL;
793
794 if (!formats && num_formats)
795 return -EINVAL;
796
797 if (formats && num_formats) {
798 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
799 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300800 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200801 return -ENOMEM;
802 }
803
804 kfree(info->bus_formats);
805 info->bus_formats = fmts;
806 info->num_bus_formats = num_formats;
807
808 return 0;
809}
810EXPORT_SYMBOL(drm_display_info_set_bus_formats);
811
812/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200813 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
814 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200815 *
816 * The kernel supports per-connector configration of its consoles through
817 * use of the video= parameter. This function parses that option and
818 * extracts the user's specified mode (or enable/disable status) for a
819 * particular connector. This is typically only used during the early fbdev
820 * setup.
821 */
822static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
823{
824 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
825 char *option = NULL;
826
827 if (fb_get_options(connector->name, &option))
828 return;
829
830 if (!drm_mode_parse_command_line_for_connector(option,
831 connector,
832 mode))
833 return;
834
835 if (mode->force) {
836 const char *s;
837
838 switch (mode->force) {
839 case DRM_FORCE_OFF:
840 s = "OFF";
841 break;
842 case DRM_FORCE_ON_DIGITAL:
843 s = "ON - dig";
844 break;
845 default:
846 case DRM_FORCE_ON:
847 s = "ON";
848 break;
849 }
850
851 DRM_INFO("forcing %s connector %s\n", connector->name, s);
852 connector->force = mode->force;
853 }
854
855 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
856 connector->name,
857 mode->xres, mode->yres,
858 mode->refresh_specified ? mode->refresh : 60,
859 mode->rb ? " reduced blanking" : "",
860 mode->margins ? " with margins" : "",
861 mode->interlace ? " interlaced" : "");
862}
863
864/**
Dave Airlief453ba02008-11-07 14:05:41 -0800865 * drm_connector_init - Init a preallocated connector
866 * @dev: DRM device
867 * @connector: the connector to init
868 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100869 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800870 *
Dave Airlief453ba02008-11-07 14:05:41 -0800871 * Initialises a preallocated connector. Connectors should be
872 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200873 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100874 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200875 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800876 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200877int drm_connector_init(struct drm_device *dev,
878 struct drm_connector *connector,
879 const struct drm_connector_funcs *funcs,
880 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800881{
Rob Clarkae16c592014-12-18 16:01:54 -0500882 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200883 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400884 struct ida *connector_ida =
885 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200886
Daniel Vetter84849902012-12-02 00:28:11 +0100887 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800888
Dave Airlied0f37cf2016-04-15 15:10:36 +1000889 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false, NULL);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200890 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300891 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200892
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300893 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800894 connector->dev = dev;
895 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100896
897 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
898 if (connector->connector_id < 0) {
899 ret = connector->connector_id;
900 goto out_put;
901 }
902
Dave Airlief453ba02008-11-07 14:05:41 -0800903 connector->connector_type = connector_type;
904 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400905 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
906 if (connector->connector_type_id < 0) {
907 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100908 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400909 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300910 connector->name =
911 kasprintf(GFP_KERNEL, "%s-%d",
912 drm_connector_enum_list[connector_type].name,
913 connector->connector_type_id);
914 if (!connector->name) {
915 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100916 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300917 }
918
Dave Airlief453ba02008-11-07 14:05:41 -0800919 INIT_LIST_HEAD(&connector->probed_modes);
920 INIT_LIST_HEAD(&connector->modes);
921 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000922 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800923
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200924 drm_connector_get_cmdline_mode(connector);
925
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100926 /* We should add connectors at the end to avoid upsetting the connector
927 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500928 list_add_tail(&connector->head, &config->connector_list);
929 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800930
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200931 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500932 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500933 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200934 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800935
Rob Clark58495562012-10-11 20:50:56 -0500936 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500937 config->dpms_property, 0);
938
939 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
940 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
941 }
Dave Airlief453ba02008-11-07 14:05:41 -0800942
Thomas Wood30f65702014-06-18 17:52:32 +0100943 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100944out_put_type_id:
945 if (ret)
946 ida_remove(connector_ida, connector->connector_type_id);
947out_put_id:
948 if (ret)
949 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300950out_put:
951 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000952 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300953
954out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100955 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200956
957 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800958}
959EXPORT_SYMBOL(drm_connector_init);
960
961/**
962 * drm_connector_cleanup - cleans up an initialised connector
963 * @connector: connector to cleanup
964 *
Dave Airlief453ba02008-11-07 14:05:41 -0800965 * Cleans up the connector but doesn't free the object.
966 */
967void drm_connector_cleanup(struct drm_connector *connector)
968{
969 struct drm_device *dev = connector->dev;
970 struct drm_display_mode *mode, *t;
971
Dave Airlie40d9b042014-10-20 16:29:33 +1000972 if (connector->tile_group) {
973 drm_mode_put_tile_group(dev, connector->tile_group);
974 connector->tile_group = NULL;
975 }
976
Dave Airlief453ba02008-11-07 14:05:41 -0800977 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
978 drm_mode_remove(connector, mode);
979
980 list_for_each_entry_safe(mode, t, &connector->modes, head)
981 drm_mode_remove(connector, mode);
982
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400983 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
984 connector->connector_type_id);
985
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100986 ida_remove(&dev->mode_config.connector_ida,
987 connector->connector_id);
988
Boris Brezillonb5571e92014-07-22 12:09:10 +0200989 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000990 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300991 kfree(connector->name);
992 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800993 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000994 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100995
996 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
997 if (connector->state && connector->funcs->atomic_destroy_state)
998 connector->funcs->atomic_destroy_state(connector,
999 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001000
1001 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001002}
1003EXPORT_SYMBOL(drm_connector_cleanup);
1004
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001005/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001006 * drm_connector_register - register a connector
1007 * @connector: the connector to register
1008 *
1009 * Register userspace interfaces for a connector
1010 *
1011 * Returns:
1012 * Zero on success, error code on failure.
1013 */
1014int drm_connector_register(struct drm_connector *connector)
1015{
Thomas Wood30f65702014-06-18 17:52:32 +01001016 int ret;
1017
Dave Airlie2ee39452014-07-24 11:53:45 +10001018 drm_mode_object_register(connector->dev, &connector->base);
1019
Thomas Wood30f65702014-06-18 17:52:32 +01001020 ret = drm_sysfs_connector_add(connector);
1021 if (ret)
1022 return ret;
1023
1024 ret = drm_debugfs_connector_add(connector);
1025 if (ret) {
1026 drm_sysfs_connector_remove(connector);
1027 return ret;
1028 }
1029
1030 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001031}
1032EXPORT_SYMBOL(drm_connector_register);
1033
1034/**
1035 * drm_connector_unregister - unregister a connector
1036 * @connector: the connector to unregister
1037 *
1038 * Unregister userspace interfaces for a connector
1039 */
1040void drm_connector_unregister(struct drm_connector *connector)
1041{
1042 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001043 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001044}
1045EXPORT_SYMBOL(drm_connector_unregister);
1046
Thomas Wood34ea3d32014-05-29 16:57:41 +01001047/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001048 * drm_connector_register_all - register all connectors
1049 * @dev: drm device
1050 *
1051 * This function registers all connectors in sysfs and other places so that
1052 * userspace can start to access them. Drivers can call it after calling
1053 * drm_dev_register() to complete the device registration, if they don't call
1054 * drm_connector_register() on each connector individually.
1055 *
1056 * When a device is unplugged and should be removed from userspace access,
1057 * call drm_connector_unregister_all(), which is the inverse of this
1058 * function.
1059 *
1060 * Returns:
1061 * Zero on success, error code on failure.
1062 */
1063int drm_connector_register_all(struct drm_device *dev)
1064{
1065 struct drm_connector *connector;
1066 int ret;
1067
1068 mutex_lock(&dev->mode_config.mutex);
1069
1070 drm_for_each_connector(connector, dev) {
1071 ret = drm_connector_register(connector);
1072 if (ret)
1073 goto err;
1074 }
1075
1076 mutex_unlock(&dev->mode_config.mutex);
1077
1078 return 0;
1079
1080err:
1081 mutex_unlock(&dev->mode_config.mutex);
1082 drm_connector_unregister_all(dev);
1083 return ret;
1084}
1085EXPORT_SYMBOL(drm_connector_register_all);
1086
1087/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001088 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001089 * @dev: drm device
1090 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001091 * This functions unregisters all connectors from sysfs and other places so
1092 * that userspace can no longer access them. Drivers should call this as the
1093 * first step tearing down the device instace, or when the underlying
1094 * physical device disappeared (e.g. USB unplug), right before calling
1095 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001096 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001097void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001098{
1099 struct drm_connector *connector;
1100
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001101 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001102 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001103 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001104}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001105EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001106
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001107/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001108 * drm_encoder_init - Init a preallocated encoder
1109 * @dev: drm device
1110 * @encoder: the encoder to init
1111 * @funcs: callbacks for this encoder
1112 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001113 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001114 *
1115 * Initialises a preallocated encoder. Encoder should be
1116 * subclassed as part of driver encoder objects.
1117 *
1118 * Returns:
1119 * Zero on success, error code on failure.
1120 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001121int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001122 struct drm_encoder *encoder,
1123 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001124 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001125{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001126 int ret;
1127
Daniel Vetter84849902012-12-02 00:28:11 +01001128 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001129
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001130 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1131 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001132 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001133
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001134 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001135 encoder->encoder_type = encoder_type;
1136 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001137 if (name) {
1138 va_list ap;
1139
1140 va_start(ap, name);
1141 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1142 va_end(ap);
1143 } else {
1144 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1145 drm_encoder_enum_list[encoder_type].name,
1146 encoder->base.id);
1147 }
Jani Nikulae5748942014-05-14 16:58:20 +03001148 if (!encoder->name) {
1149 ret = -ENOMEM;
1150 goto out_put;
1151 }
Dave Airlief453ba02008-11-07 14:05:41 -08001152
1153 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1154 dev->mode_config.num_encoder++;
1155
Jani Nikulae5748942014-05-14 16:58:20 +03001156out_put:
1157 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001158 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001159
1160out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001161 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001162
1163 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001164}
1165EXPORT_SYMBOL(drm_encoder_init);
1166
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001167/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001168 * drm_encoder_index - find the index of a registered encoder
1169 * @encoder: encoder to find index for
1170 *
1171 * Given a registered encoder, return the index of that encoder within a DRM
1172 * device's list of encoders.
1173 */
1174unsigned int drm_encoder_index(struct drm_encoder *encoder)
1175{
1176 unsigned int index = 0;
1177 struct drm_encoder *tmp;
1178
1179 drm_for_each_encoder(tmp, encoder->dev) {
1180 if (tmp == encoder)
1181 return index;
1182
1183 index++;
1184 }
1185
1186 BUG();
1187}
1188EXPORT_SYMBOL(drm_encoder_index);
1189
1190/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001191 * drm_encoder_cleanup - cleans up an initialised encoder
1192 * @encoder: encoder to cleanup
1193 *
1194 * Cleans up the encoder but doesn't free the object.
1195 */
Dave Airlief453ba02008-11-07 14:05:41 -08001196void drm_encoder_cleanup(struct drm_encoder *encoder)
1197{
1198 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001199
Daniel Vetter84849902012-12-02 00:28:11 +01001200 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001201 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001202 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001203 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001204 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001205 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001206
1207 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001208}
1209EXPORT_SYMBOL(drm_encoder_cleanup);
1210
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001211static unsigned int drm_num_planes(struct drm_device *dev)
1212{
1213 unsigned int num = 0;
1214 struct drm_plane *tmp;
1215
1216 drm_for_each_plane(tmp, dev) {
1217 num++;
1218 }
1219
1220 return num;
1221}
1222
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001223/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001224 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001225 * @dev: DRM device
1226 * @plane: plane object to init
1227 * @possible_crtcs: bitmask of possible CRTCs
1228 * @funcs: callbacks for the new plane
1229 * @formats: array of supported formats (%DRM_FORMAT_*)
1230 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001231 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001232 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001233 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001234 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001235 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001236 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001237 * Zero on success, error code on failure.
1238 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001239int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1240 unsigned long possible_crtcs,
1241 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001242 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001243 enum drm_plane_type type,
1244 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001245{
Rob Clark6b4959f2014-12-18 16:01:53 -05001246 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001247 int ret;
1248
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001249 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1250 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001251 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001252
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001253 drm_modeset_lock_init(&plane->mutex);
1254
Rob Clark4d939142012-05-17 02:23:27 -06001255 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001256 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001257 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001258 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1259 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001260 if (!plane->format_types) {
1261 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001262 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001263 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001264 }
1265
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001266 if (name) {
1267 va_list ap;
1268
1269 va_start(ap, name);
1270 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1271 va_end(ap);
1272 } else {
1273 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1274 drm_num_planes(dev));
1275 }
1276 if (!plane->name) {
1277 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001278 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001279 return -ENOMEM;
1280 }
1281
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001282 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001283 plane->format_count = format_count;
1284 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001285 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001286
Rob Clark6b4959f2014-12-18 16:01:53 -05001287 list_add_tail(&plane->head, &config->plane_list);
1288 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001289 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001290 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001291
Rob Clark9922ab52014-04-01 20:16:57 -04001292 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001293 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001294 plane->type);
1295
Rob Clark6b4959f2014-12-18 16:01:53 -05001296 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1297 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1298 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1299 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1300 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1301 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1302 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1303 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1304 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1305 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1306 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1307 }
1308
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001309 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001310}
Matt Roperdc415ff2014-04-01 15:22:36 -07001311EXPORT_SYMBOL(drm_universal_plane_init);
1312
1313/**
1314 * drm_plane_init - Initialize a legacy plane
1315 * @dev: DRM device
1316 * @plane: plane object to init
1317 * @possible_crtcs: bitmask of possible CRTCs
1318 * @funcs: callbacks for the new plane
1319 * @formats: array of supported formats (%DRM_FORMAT_*)
1320 * @format_count: number of elements in @formats
1321 * @is_primary: plane type (primary vs overlay)
1322 *
1323 * Legacy API to initialize a DRM plane.
1324 *
1325 * New drivers should call drm_universal_plane_init() instead.
1326 *
1327 * Returns:
1328 * Zero on success, error code on failure.
1329 */
1330int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1331 unsigned long possible_crtcs,
1332 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001333 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001334 bool is_primary)
1335{
1336 enum drm_plane_type type;
1337
1338 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1339 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001340 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001341}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001342EXPORT_SYMBOL(drm_plane_init);
1343
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001344/**
1345 * drm_plane_cleanup - Clean up the core plane usage
1346 * @plane: plane to cleanup
1347 *
1348 * This function cleans up @plane and removes it from the DRM mode setting
1349 * core. Note that the function does *not* free the plane structure itself,
1350 * this is the responsibility of the caller.
1351 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001352void drm_plane_cleanup(struct drm_plane *plane)
1353{
1354 struct drm_device *dev = plane->dev;
1355
Daniel Vetter84849902012-12-02 00:28:11 +01001356 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001357 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001358 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001359
1360 BUG_ON(list_empty(&plane->head));
1361
1362 list_del(&plane->head);
1363 dev->mode_config.num_total_plane--;
1364 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1365 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001366 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001367
1368 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1369 if (plane->state && plane->funcs->atomic_destroy_state)
1370 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001371
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001372 kfree(plane->name);
1373
Thierry Redinga18c0af2014-12-10 11:38:49 +01001374 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001375}
1376EXPORT_SYMBOL(drm_plane_cleanup);
1377
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001378/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001379 * drm_plane_index - find the index of a registered plane
1380 * @plane: plane to find index for
1381 *
1382 * Given a registered plane, return the index of that CRTC within a DRM
1383 * device's list of planes.
1384 */
1385unsigned int drm_plane_index(struct drm_plane *plane)
1386{
1387 unsigned int index = 0;
1388 struct drm_plane *tmp;
1389
Daniel Vettere4f62542015-07-09 23:44:35 +02001390 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001391 if (tmp == plane)
1392 return index;
1393
1394 index++;
1395 }
1396
1397 BUG();
1398}
1399EXPORT_SYMBOL(drm_plane_index);
1400
1401/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001402 * drm_plane_from_index - find the registered plane at an index
1403 * @dev: DRM device
1404 * @idx: index of registered plane to find for
1405 *
1406 * Given a plane index, return the registered plane from DRM device's
1407 * list of planes with matching index.
1408 */
1409struct drm_plane *
1410drm_plane_from_index(struct drm_device *dev, int idx)
1411{
1412 struct drm_plane *plane;
1413 unsigned int i = 0;
1414
Daniel Vetter6295d602015-07-09 23:44:25 +02001415 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001416 if (i == idx)
1417 return plane;
1418 i++;
1419 }
1420 return NULL;
1421}
1422EXPORT_SYMBOL(drm_plane_from_index);
1423
1424/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001425 * drm_plane_force_disable - Forcibly disable a plane
1426 * @plane: plane to disable
1427 *
1428 * Forces the plane to be disabled.
1429 *
1430 * Used when the plane's current framebuffer is destroyed,
1431 * and when restoring fbdev mode.
1432 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001433void drm_plane_force_disable(struct drm_plane *plane)
1434{
1435 int ret;
1436
Daniel Vetter3d30a592014-07-27 13:42:42 +02001437 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001438 return;
1439
Daniel Vetter3d30a592014-07-27 13:42:42 +02001440 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001441 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001442 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001443 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001444 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001445 return;
1446 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001447 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001448 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001449 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001450 plane->fb = NULL;
1451 plane->crtc = NULL;
1452}
1453EXPORT_SYMBOL(drm_plane_force_disable);
1454
Rob Clark6b4959f2014-12-18 16:01:53 -05001455static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001456{
Rob Clark356af0e2014-12-18 16:01:52 -05001457 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001458
1459 /*
1460 * Standard properties (apply to all connectors)
1461 */
Rob Clark356af0e2014-12-18 16:01:52 -05001462 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001463 DRM_MODE_PROP_IMMUTABLE,
1464 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001465 if (!prop)
1466 return -ENOMEM;
1467 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001468
Rob Clark356af0e2014-12-18 16:01:52 -05001469 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001470 "DPMS", drm_dpms_enum_list,
1471 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001472 if (!prop)
1473 return -ENOMEM;
1474 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001475
Rob Clark356af0e2014-12-18 16:01:52 -05001476 prop = drm_property_create(dev,
1477 DRM_MODE_PROP_BLOB |
1478 DRM_MODE_PROP_IMMUTABLE,
1479 "PATH", 0);
1480 if (!prop)
1481 return -ENOMEM;
1482 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001483
Rob Clark356af0e2014-12-18 16:01:52 -05001484 prop = drm_property_create(dev,
1485 DRM_MODE_PROP_BLOB |
1486 DRM_MODE_PROP_IMMUTABLE,
1487 "TILE", 0);
1488 if (!prop)
1489 return -ENOMEM;
1490 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001491
Rob Clark6b4959f2014-12-18 16:01:53 -05001492 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001493 "type", drm_plane_type_enum_list,
1494 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001495 if (!prop)
1496 return -ENOMEM;
1497 dev->mode_config.plane_type_property = prop;
1498
1499 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1500 "SRC_X", 0, UINT_MAX);
1501 if (!prop)
1502 return -ENOMEM;
1503 dev->mode_config.prop_src_x = prop;
1504
1505 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1506 "SRC_Y", 0, UINT_MAX);
1507 if (!prop)
1508 return -ENOMEM;
1509 dev->mode_config.prop_src_y = prop;
1510
1511 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1512 "SRC_W", 0, UINT_MAX);
1513 if (!prop)
1514 return -ENOMEM;
1515 dev->mode_config.prop_src_w = prop;
1516
1517 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1518 "SRC_H", 0, UINT_MAX);
1519 if (!prop)
1520 return -ENOMEM;
1521 dev->mode_config.prop_src_h = prop;
1522
1523 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1524 "CRTC_X", INT_MIN, INT_MAX);
1525 if (!prop)
1526 return -ENOMEM;
1527 dev->mode_config.prop_crtc_x = prop;
1528
1529 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1530 "CRTC_Y", INT_MIN, INT_MAX);
1531 if (!prop)
1532 return -ENOMEM;
1533 dev->mode_config.prop_crtc_y = prop;
1534
1535 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1536 "CRTC_W", 0, INT_MAX);
1537 if (!prop)
1538 return -ENOMEM;
1539 dev->mode_config.prop_crtc_w = prop;
1540
1541 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1542 "CRTC_H", 0, INT_MAX);
1543 if (!prop)
1544 return -ENOMEM;
1545 dev->mode_config.prop_crtc_h = prop;
1546
1547 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1548 "FB_ID", DRM_MODE_OBJECT_FB);
1549 if (!prop)
1550 return -ENOMEM;
1551 dev->mode_config.prop_fb_id = prop;
1552
1553 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1554 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1555 if (!prop)
1556 return -ENOMEM;
1557 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001558
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001559 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1560 "ACTIVE");
1561 if (!prop)
1562 return -ENOMEM;
1563 dev->mode_config.prop_active = prop;
1564
Daniel Stone955f3c32015-05-25 19:11:52 +01001565 prop = drm_property_create(dev,
1566 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1567 "MODE_ID", 0);
1568 if (!prop)
1569 return -ENOMEM;
1570 dev->mode_config.prop_mode_id = prop;
1571
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001572 prop = drm_property_create(dev,
1573 DRM_MODE_PROP_BLOB,
1574 "DEGAMMA_LUT", 0);
1575 if (!prop)
1576 return -ENOMEM;
1577 dev->mode_config.degamma_lut_property = prop;
1578
1579 prop = drm_property_create_range(dev,
1580 DRM_MODE_PROP_IMMUTABLE,
1581 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1582 if (!prop)
1583 return -ENOMEM;
1584 dev->mode_config.degamma_lut_size_property = prop;
1585
1586 prop = drm_property_create(dev,
1587 DRM_MODE_PROP_BLOB,
1588 "CTM", 0);
1589 if (!prop)
1590 return -ENOMEM;
1591 dev->mode_config.ctm_property = prop;
1592
1593 prop = drm_property_create(dev,
1594 DRM_MODE_PROP_BLOB,
1595 "GAMMA_LUT", 0);
1596 if (!prop)
1597 return -ENOMEM;
1598 dev->mode_config.gamma_lut_property = prop;
1599
1600 prop = drm_property_create_range(dev,
1601 DRM_MODE_PROP_IMMUTABLE,
1602 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1603 if (!prop)
1604 return -ENOMEM;
1605 dev->mode_config.gamma_lut_size_property = prop;
1606
Rob Clark9922ab52014-04-01 20:16:57 -04001607 return 0;
1608}
1609
Dave Airlief453ba02008-11-07 14:05:41 -08001610/**
1611 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1612 * @dev: DRM device
1613 *
1614 * Called by a driver the first time a DVI-I connector is made.
1615 */
1616int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1617{
1618 struct drm_property *dvi_i_selector;
1619 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001620
1621 if (dev->mode_config.dvi_i_select_subconnector_property)
1622 return 0;
1623
1624 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001625 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001626 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001627 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001628 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001629 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1630
Sascha Hauer4a67d392012-02-06 10:58:17 +01001631 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001632 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001633 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001634 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001635 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1636
1637 return 0;
1638}
1639EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1640
1641/**
1642 * drm_create_tv_properties - create TV specific connector properties
1643 * @dev: DRM device
1644 * @num_modes: number of different TV formats (modes) supported
1645 * @modes: array of pointers to strings containing name of each format
1646 *
1647 * Called by a driver's TV initialization routine, this function creates
1648 * the TV specific connector properties for a given device. Caller is
1649 * responsible for allocating a list of format names and passing them to
1650 * this routine.
1651 */
Thierry Reding2f763312014-10-13 12:45:57 +02001652int drm_mode_create_tv_properties(struct drm_device *dev,
1653 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001654 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001655{
1656 struct drm_property *tv_selector;
1657 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001658 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001659
1660 if (dev->mode_config.tv_select_subconnector_property)
1661 return 0;
1662
1663 /*
1664 * Basic connector properties
1665 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001666 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001667 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001668 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001669 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001670 if (!tv_selector)
1671 goto nomem;
1672
Dave Airlief453ba02008-11-07 14:05:41 -08001673 dev->mode_config.tv_select_subconnector_property = tv_selector;
1674
1675 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001676 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1677 "subconnector",
1678 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001679 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001680 if (!tv_subconnector)
1681 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001682 dev->mode_config.tv_subconnector_property = tv_subconnector;
1683
1684 /*
1685 * Other, TV specific properties: margins & TV modes.
1686 */
1687 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001688 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001689 if (!dev->mode_config.tv_left_margin_property)
1690 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001691
1692 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001693 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001694 if (!dev->mode_config.tv_right_margin_property)
1695 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001696
1697 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001698 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001699 if (!dev->mode_config.tv_top_margin_property)
1700 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001701
1702 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001703 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001704 if (!dev->mode_config.tv_bottom_margin_property)
1705 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001706
1707 dev->mode_config.tv_mode_property =
1708 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1709 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001710 if (!dev->mode_config.tv_mode_property)
1711 goto nomem;
1712
Dave Airlief453ba02008-11-07 14:05:41 -08001713 for (i = 0; i < num_modes; i++)
1714 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1715 i, modes[i]);
1716
Francisco Jerezb6b79022009-08-02 04:19:20 +02001717 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001718 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001719 if (!dev->mode_config.tv_brightness_property)
1720 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001721
1722 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001723 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001724 if (!dev->mode_config.tv_contrast_property)
1725 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001726
1727 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001728 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001729 if (!dev->mode_config.tv_flicker_reduction_property)
1730 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001731
Francisco Jereza75f0232009-08-12 02:30:10 +02001732 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001733 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001734 if (!dev->mode_config.tv_overscan_property)
1735 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001736
1737 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001738 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001739 if (!dev->mode_config.tv_saturation_property)
1740 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001741
1742 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001743 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001744 if (!dev->mode_config.tv_hue_property)
1745 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001746
Dave Airlief453ba02008-11-07 14:05:41 -08001747 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001748nomem:
1749 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001750}
1751EXPORT_SYMBOL(drm_mode_create_tv_properties);
1752
1753/**
1754 * drm_mode_create_scaling_mode_property - create scaling mode property
1755 * @dev: DRM device
1756 *
1757 * Called by a driver the first time it's needed, must be attached to desired
1758 * connectors.
1759 */
1760int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1761{
1762 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001763
1764 if (dev->mode_config.scaling_mode_property)
1765 return 0;
1766
1767 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001768 drm_property_create_enum(dev, 0, "scaling mode",
1769 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001770 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001771
1772 dev->mode_config.scaling_mode_property = scaling_mode;
1773
1774 return 0;
1775}
1776EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1777
1778/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301779 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1780 * @dev: DRM device
1781 *
1782 * Called by a driver the first time it's needed, must be attached to desired
1783 * connectors.
1784 *
1785 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001786 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301787 */
1788int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1789{
1790 if (dev->mode_config.aspect_ratio_property)
1791 return 0;
1792
1793 dev->mode_config.aspect_ratio_property =
1794 drm_property_create_enum(dev, 0, "aspect ratio",
1795 drm_aspect_ratio_enum_list,
1796 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1797
1798 if (dev->mode_config.aspect_ratio_property == NULL)
1799 return -ENOMEM;
1800
1801 return 0;
1802}
1803EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1804
1805/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001806 * drm_mode_create_dirty_property - create dirty property
1807 * @dev: DRM device
1808 *
1809 * Called by a driver the first time it's needed, must be attached to desired
1810 * connectors.
1811 */
1812int drm_mode_create_dirty_info_property(struct drm_device *dev)
1813{
1814 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001815
1816 if (dev->mode_config.dirty_info_property)
1817 return 0;
1818
1819 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001820 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001821 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001822 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001823 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001824 dev->mode_config.dirty_info_property = dirty_info;
1825
1826 return 0;
1827}
1828EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1829
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001830/**
1831 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1832 * @dev: DRM device
1833 *
1834 * Create the the suggested x/y offset property for connectors.
1835 */
1836int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1837{
1838 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1839 return 0;
1840
1841 dev->mode_config.suggested_x_property =
1842 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1843
1844 dev->mode_config.suggested_y_property =
1845 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1846
1847 if (dev->mode_config.suggested_x_property == NULL ||
1848 dev->mode_config.suggested_y_property == NULL)
1849 return -ENOMEM;
1850 return 0;
1851}
1852EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1853
Dave Airlief453ba02008-11-07 14:05:41 -08001854/**
Dave Airlief453ba02008-11-07 14:05:41 -08001855 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001856 * @dev: drm device for the ioctl
1857 * @data: data pointer for the ioctl
1858 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001859 *
Dave Airlief453ba02008-11-07 14:05:41 -08001860 * Construct a set of configuration description structures and return
1861 * them to the user, including CRTC, connector and framebuffer configuration.
1862 *
1863 * Called by the user via ioctl.
1864 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001865 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001866 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001867 */
1868int drm_mode_getresources(struct drm_device *dev, void *data,
1869 struct drm_file *file_priv)
1870{
1871 struct drm_mode_card_res *card_res = data;
1872 struct list_head *lh;
1873 struct drm_framebuffer *fb;
1874 struct drm_connector *connector;
1875 struct drm_crtc *crtc;
1876 struct drm_encoder *encoder;
1877 int ret = 0;
1878 int connector_count = 0;
1879 int crtc_count = 0;
1880 int fb_count = 0;
1881 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001882 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001883 uint32_t __user *fb_id;
1884 uint32_t __user *crtc_id;
1885 uint32_t __user *connector_id;
1886 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001887
Dave Airliefb3b06c2011-02-08 13:55:21 +10001888 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1889 return -EINVAL;
1890
Dave Airlief453ba02008-11-07 14:05:41 -08001891
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001892 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001893 /*
1894 * For the non-control nodes we need to limit the list of resources
1895 * by IDs in the group list for this node
1896 */
1897 list_for_each(lh, &file_priv->fbs)
1898 fb_count++;
1899
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001900 /* handle this in 4 parts */
1901 /* FBs */
1902 if (card_res->count_fbs >= fb_count) {
1903 copied = 0;
1904 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1905 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1906 if (put_user(fb->base.id, fb_id + copied)) {
1907 mutex_unlock(&file_priv->fbs_lock);
1908 return -EFAULT;
1909 }
1910 copied++;
1911 }
1912 }
1913 card_res->count_fbs = fb_count;
1914 mutex_unlock(&file_priv->fbs_lock);
1915
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001916 /* mode_config.mutex protects the connector list against e.g. DP MST
1917 * connector hot-adding. CRTC/Plane lists are invariant. */
1918 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001919 drm_for_each_crtc(crtc, dev)
1920 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001921
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001922 drm_for_each_connector(connector, dev)
1923 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001924
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001925 drm_for_each_encoder(encoder, dev)
1926 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001927
1928 card_res->max_height = dev->mode_config.max_height;
1929 card_res->min_height = dev->mode_config.min_height;
1930 card_res->max_width = dev->mode_config.max_width;
1931 card_res->min_width = dev->mode_config.min_width;
1932
Dave Airlief453ba02008-11-07 14:05:41 -08001933 /* CRTCs */
1934 if (card_res->count_crtcs >= crtc_count) {
1935 copied = 0;
1936 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001937 drm_for_each_crtc(crtc, dev) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001938 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1939 crtc->base.id, crtc->name);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001940 if (put_user(crtc->base.id, crtc_id + copied)) {
1941 ret = -EFAULT;
1942 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001943 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001944 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001945 }
1946 }
1947 card_res->count_crtcs = crtc_count;
1948
1949 /* Encoders */
1950 if (card_res->count_encoders >= encoder_count) {
1951 copied = 0;
1952 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001953 drm_for_each_encoder(encoder, dev) {
1954 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1955 encoder->name);
1956 if (put_user(encoder->base.id, encoder_id +
1957 copied)) {
1958 ret = -EFAULT;
1959 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001960 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001961 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001962 }
1963 }
1964 card_res->count_encoders = encoder_count;
1965
1966 /* Connectors */
1967 if (card_res->count_connectors >= connector_count) {
1968 copied = 0;
1969 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001970 drm_for_each_connector(connector, dev) {
1971 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1972 connector->base.id,
1973 connector->name);
1974 if (put_user(connector->base.id,
1975 connector_id + copied)) {
1976 ret = -EFAULT;
1977 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001978 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001979 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001980 }
1981 }
1982 card_res->count_connectors = connector_count;
1983
Jerome Glisse94401062010-07-15 15:43:25 -04001984 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001985 card_res->count_connectors, card_res->count_encoders);
1986
1987out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001988 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001989 return ret;
1990}
1991
1992/**
1993 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001994 * @dev: drm device for the ioctl
1995 * @data: data pointer for the ioctl
1996 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001997 *
Dave Airlief453ba02008-11-07 14:05:41 -08001998 * Construct a CRTC configuration structure to return to the user.
1999 *
2000 * Called by the user via ioctl.
2001 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002002 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002003 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002004 */
2005int drm_mode_getcrtc(struct drm_device *dev,
2006 void *data, struct drm_file *file_priv)
2007{
2008 struct drm_mode_crtc *crtc_resp = data;
2009 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002010
Dave Airliefb3b06c2011-02-08 13:55:21 +10002011 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2012 return -EINVAL;
2013
Rob Clarka2b34e22013-10-05 16:36:52 -04002014 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002015 if (!crtc)
2016 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002017
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002018 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002019 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002020 if (crtc->primary->fb)
2021 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002022 else
2023 crtc_resp->fb_id = 0;
2024
Daniel Vetter31c946e2015-02-22 12:24:17 +01002025 if (crtc->state) {
2026 crtc_resp->x = crtc->primary->state->src_x >> 16;
2027 crtc_resp->y = crtc->primary->state->src_y >> 16;
2028 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002029 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002030 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002031
Daniel Vetter31c946e2015-02-22 12:24:17 +01002032 } else {
2033 crtc_resp->mode_valid = 0;
2034 }
Dave Airlief453ba02008-11-07 14:05:41 -08002035 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002036 crtc_resp->x = crtc->x;
2037 crtc_resp->y = crtc->y;
2038 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002039 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002040 crtc_resp->mode_valid = 1;
2041
2042 } else {
2043 crtc_resp->mode_valid = 0;
2044 }
Dave Airlief453ba02008-11-07 14:05:41 -08002045 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002046 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002047
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002048 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002049}
2050
Damien Lespiau61d8e322013-09-25 16:45:22 +01002051static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2052 const struct drm_file *file_priv)
2053{
2054 /*
2055 * If user-space hasn't configured the driver to expose the stereo 3D
2056 * modes, don't expose them.
2057 */
2058 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2059 return false;
2060
2061 return true;
2062}
2063
Daniel Vetterabd69c52014-11-25 23:50:05 +01002064static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2065{
2066 /* For atomic drivers only state objects are synchronously updated and
2067 * protected by modeset locks, so check those first. */
2068 if (connector->state)
2069 return connector->state->best_encoder;
2070 return connector->encoder;
2071}
2072
Rob Clark95cbf112014-12-18 16:01:49 -05002073/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002074static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002075 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2076 uint32_t *arg_count_props)
2077{
Rob Clark88a48e22014-12-18 16:01:50 -05002078 int props_count;
2079 int i, ret, copied;
2080
2081 props_count = obj->properties->count;
2082 if (!atomic)
2083 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002084
2085 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002086 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002087 struct drm_property *prop = obj->properties->properties[i];
2088 uint64_t val;
2089
Rob Clark88a48e22014-12-18 16:01:50 -05002090 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2091 continue;
2092
Rob Clark95cbf112014-12-18 16:01:49 -05002093 ret = drm_object_property_get_value(obj, prop, &val);
2094 if (ret)
2095 return ret;
2096
2097 if (put_user(prop->base.id, prop_ptr + copied))
2098 return -EFAULT;
2099
2100 if (put_user(val, prop_values + copied))
2101 return -EFAULT;
2102
2103 copied++;
2104 }
2105 }
2106 *arg_count_props = props_count;
2107
2108 return 0;
2109}
2110
Dave Airlief453ba02008-11-07 14:05:41 -08002111/**
2112 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002113 * @dev: drm device for the ioctl
2114 * @data: data pointer for the ioctl
2115 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002116 *
Dave Airlief453ba02008-11-07 14:05:41 -08002117 * Construct a connector configuration structure to return to the user.
2118 *
2119 * Called by the user via ioctl.
2120 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002121 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002122 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002123 */
2124int drm_mode_getconnector(struct drm_device *dev, void *data,
2125 struct drm_file *file_priv)
2126{
2127 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002128 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002129 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002130 struct drm_display_mode *mode;
2131 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002132 int encoders_count = 0;
2133 int ret = 0;
2134 int copied = 0;
2135 int i;
2136 struct drm_mode_modeinfo u_mode;
2137 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002138 uint32_t __user *encoder_ptr;
2139
Dave Airliefb3b06c2011-02-08 13:55:21 +10002140 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2141 return -EINVAL;
2142
Dave Airlief453ba02008-11-07 14:05:41 -08002143 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2144
Jerome Glisse94401062010-07-15 15:43:25 -04002145 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002146
Daniel Vetter7b240562012-12-12 00:35:33 +01002147 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002148
Rob Clarka2b34e22013-10-05 16:36:52 -04002149 connector = drm_connector_find(dev, out_resp->connector_id);
2150 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002151 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002152 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002153 }
Dave Airlief453ba02008-11-07 14:05:41 -08002154
Thierry Reding01073b02014-12-10 13:03:38 +01002155 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2156 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002157 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002158
2159 if (out_resp->count_modes == 0) {
2160 connector->funcs->fill_modes(connector,
2161 dev->mode_config.max_width,
2162 dev->mode_config.max_height);
2163 }
2164
2165 /* delayed so we get modes regardless of pre-fill_modes state */
2166 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002167 if (drm_mode_expose_to_userspace(mode, file_priv))
2168 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002169
2170 out_resp->connector_id = connector->base.id;
2171 out_resp->connector_type = connector->connector_type;
2172 out_resp->connector_type_id = connector->connector_type_id;
2173 out_resp->mm_width = connector->display_info.width_mm;
2174 out_resp->mm_height = connector->display_info.height_mm;
2175 out_resp->subpixel = connector->display_info.subpixel_order;
2176 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002177
2178 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002179 encoder = drm_connector_get_encoder(connector);
2180 if (encoder)
2181 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002182 else
2183 out_resp->encoder_id = 0;
2184
2185 /*
2186 * This ioctl is called twice, once to determine how much space is
2187 * needed, and the 2nd time to fill it.
2188 */
2189 if ((out_resp->count_modes >= mode_count) && mode_count) {
2190 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002191 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002192 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002193 if (!drm_mode_expose_to_userspace(mode, file_priv))
2194 continue;
2195
Daniel Stone934a8a82015-05-22 13:34:48 +01002196 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002197 if (copy_to_user(mode_ptr + copied,
2198 &u_mode, sizeof(u_mode))) {
2199 ret = -EFAULT;
2200 goto out;
2201 }
2202 copied++;
2203 }
2204 }
2205 out_resp->count_modes = mode_count;
2206
Rob Clark88a48e22014-12-18 16:01:50 -05002207 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002208 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2209 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2210 &out_resp->count_props);
2211 if (ret)
2212 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002213
2214 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2215 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002216 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002217 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2218 if (connector->encoder_ids[i] != 0) {
2219 if (put_user(connector->encoder_ids[i],
2220 encoder_ptr + copied)) {
2221 ret = -EFAULT;
2222 goto out;
2223 }
2224 copied++;
2225 }
2226 }
2227 }
2228 out_resp->count_encoders = encoders_count;
2229
2230out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002231 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002232
2233out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002234 mutex_unlock(&dev->mode_config.mutex);
2235
Dave Airlief453ba02008-11-07 14:05:41 -08002236 return ret;
2237}
2238
Daniel Vetterabd69c52014-11-25 23:50:05 +01002239static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2240{
2241 struct drm_connector *connector;
2242 struct drm_device *dev = encoder->dev;
2243 bool uses_atomic = false;
2244
2245 /* For atomic drivers only state objects are synchronously updated and
2246 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002247 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002248 if (!connector->state)
2249 continue;
2250
2251 uses_atomic = true;
2252
2253 if (connector->state->best_encoder != encoder)
2254 continue;
2255
2256 return connector->state->crtc;
2257 }
2258
2259 /* Don't return stale data (e.g. pending async disable). */
2260 if (uses_atomic)
2261 return NULL;
2262
2263 return encoder->crtc;
2264}
2265
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002266/**
2267 * drm_mode_getencoder - get encoder configuration
2268 * @dev: drm device for the ioctl
2269 * @data: data pointer for the ioctl
2270 * @file_priv: drm file for the ioctl call
2271 *
2272 * Construct a encoder configuration structure to return to the user.
2273 *
2274 * Called by the user via ioctl.
2275 *
2276 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002277 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002278 */
Dave Airlief453ba02008-11-07 14:05:41 -08002279int drm_mode_getencoder(struct drm_device *dev, void *data,
2280 struct drm_file *file_priv)
2281{
2282 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002283 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002284 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002285
Dave Airliefb3b06c2011-02-08 13:55:21 +10002286 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2287 return -EINVAL;
2288
Rob Clarka2b34e22013-10-05 16:36:52 -04002289 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002290 if (!encoder)
2291 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002292
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002293 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002294 crtc = drm_encoder_get_crtc(encoder);
2295 if (crtc)
2296 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002297 else
2298 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002299 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2300
Dave Airlief453ba02008-11-07 14:05:41 -08002301 enc_resp->encoder_type = encoder->encoder_type;
2302 enc_resp->encoder_id = encoder->base.id;
2303 enc_resp->possible_crtcs = encoder->possible_crtcs;
2304 enc_resp->possible_clones = encoder->possible_clones;
2305
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002306 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002307}
2308
2309/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002310 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002311 * @dev: DRM device
2312 * @data: ioctl data
2313 * @file_priv: DRM file info
2314 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002315 * Construct a list of plane ids to return to the user.
2316 *
2317 * Called by the user via ioctl.
2318 *
2319 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002320 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002321 */
2322int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002323 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002324{
2325 struct drm_mode_get_plane_res *plane_resp = data;
2326 struct drm_mode_config *config;
2327 struct drm_plane *plane;
2328 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002329 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002330 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002331
2332 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2333 return -EINVAL;
2334
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002335 config = &dev->mode_config;
2336
Matt Roper681e7ec2014-04-01 15:22:42 -07002337 if (file_priv->universal_planes)
2338 num_planes = config->num_total_plane;
2339 else
2340 num_planes = config->num_overlay_plane;
2341
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002342 /*
2343 * This ioctl is called twice, once to determine how much space is
2344 * needed, and the 2nd time to fill it.
2345 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002346 if (num_planes &&
2347 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002348 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002349
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002350 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002351 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002352 /*
2353 * Unless userspace set the 'universal planes'
2354 * capability bit, only advertise overlays.
2355 */
2356 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2357 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002358 continue;
2359
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002360 if (put_user(plane->base.id, plane_ptr + copied))
2361 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002362 copied++;
2363 }
2364 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002365 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002366
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002367 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002368}
2369
2370/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002371 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002372 * @dev: DRM device
2373 * @data: ioctl data
2374 * @file_priv: DRM file info
2375 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002376 * Construct a plane configuration structure to return to the user.
2377 *
2378 * Called by the user via ioctl.
2379 *
2380 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002381 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002382 */
2383int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002384 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002385{
2386 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002387 struct drm_plane *plane;
2388 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002389
2390 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2391 return -EINVAL;
2392
Rob Clarka2b34e22013-10-05 16:36:52 -04002393 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002394 if (!plane)
2395 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002396
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002397 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002398 if (plane->crtc)
2399 plane_resp->crtc_id = plane->crtc->base.id;
2400 else
2401 plane_resp->crtc_id = 0;
2402
2403 if (plane->fb)
2404 plane_resp->fb_id = plane->fb->base.id;
2405 else
2406 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002407 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002408
2409 plane_resp->plane_id = plane->base.id;
2410 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002411 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002412
2413 /*
2414 * This ioctl is called twice, once to determine how much space is
2415 * needed, and the 2nd time to fill it.
2416 */
2417 if (plane->format_count &&
2418 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002419 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002420 if (copy_to_user(format_ptr,
2421 plane->format_types,
2422 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002423 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002424 }
2425 }
2426 plane_resp->count_format_types = plane->format_count;
2427
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002428 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002429}
2430
Laurent Pinchartead86102015-03-05 02:25:43 +02002431/**
2432 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2433 * @plane: plane to check for format support
2434 * @format: the pixel format
2435 *
2436 * Returns:
2437 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2438 * otherwise.
2439 */
2440int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2441{
2442 unsigned int i;
2443
2444 for (i = 0; i < plane->format_count; i++) {
2445 if (format == plane->format_types[i])
2446 return 0;
2447 }
2448
2449 return -EINVAL;
2450}
2451
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002452static int check_src_coords(uint32_t src_x, uint32_t src_y,
2453 uint32_t src_w, uint32_t src_h,
2454 const struct drm_framebuffer *fb)
2455{
2456 unsigned int fb_width, fb_height;
2457
2458 fb_width = fb->width << 16;
2459 fb_height = fb->height << 16;
2460
2461 /* Make sure source coordinates are inside the fb. */
2462 if (src_w > fb_width ||
2463 src_x > fb_width - src_w ||
2464 src_h > fb_height ||
2465 src_y > fb_height - src_h) {
2466 DRM_DEBUG_KMS("Invalid source coordinates "
2467 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2468 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2469 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2470 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2471 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2472 return -ENOSPC;
2473 }
2474
2475 return 0;
2476}
2477
Matt Roperb36552b2014-06-10 08:28:09 -07002478/*
2479 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002480 *
Matt Roperb36552b2014-06-10 08:28:09 -07002481 * Note that we assume an extra reference has already been taken on fb. If the
2482 * update fails, this reference will be dropped before return; if it succeeds,
2483 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002484 *
Matt Roperb36552b2014-06-10 08:28:09 -07002485 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002486 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002487static int __setplane_internal(struct drm_plane *plane,
2488 struct drm_crtc *crtc,
2489 struct drm_framebuffer *fb,
2490 int32_t crtc_x, int32_t crtc_y,
2491 uint32_t crtc_w, uint32_t crtc_h,
2492 /* src_{x,y,w,h} values are 16.16 fixed point */
2493 uint32_t src_x, uint32_t src_y,
2494 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002495{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002496 int ret = 0;
2497
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002498 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002499 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002500 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002501 ret = plane->funcs->disable_plane(plane);
2502 if (!ret) {
2503 plane->crtc = NULL;
2504 plane->fb = NULL;
2505 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002506 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002507 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002508 goto out;
2509 }
2510
Matt Roper7f994f32014-05-29 08:06:51 -07002511 /* Check whether this plane is usable on this CRTC */
2512 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2513 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2514 ret = -EINVAL;
2515 goto out;
2516 }
2517
Ville Syrjälä62443be2011-12-20 00:06:47 +02002518 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002519 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2520 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002521 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2522 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002523 goto out;
2524 }
2525
Matt Roper3968be92015-04-13 11:06:13 -07002526 /* Give drivers some help against integer overflows */
2527 if (crtc_w > INT_MAX ||
2528 crtc_x > INT_MAX - (int32_t) crtc_w ||
2529 crtc_h > INT_MAX ||
2530 crtc_y > INT_MAX - (int32_t) crtc_h) {
2531 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2532 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002533 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002534 goto out;
2535 }
2536
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002537 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2538 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002539 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002540
Daniel Vetter3d30a592014-07-27 13:42:42 +02002541 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002542 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002543 crtc_x, crtc_y, crtc_w, crtc_h,
2544 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002545 if (!ret) {
2546 plane->crtc = crtc;
2547 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002548 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002549 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002550 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002551 }
2552
2553out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002554 if (fb)
2555 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002556 if (plane->old_fb)
2557 drm_framebuffer_unreference(plane->old_fb);
2558 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002559
2560 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002561}
Matt Roperb36552b2014-06-10 08:28:09 -07002562
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002563static int setplane_internal(struct drm_plane *plane,
2564 struct drm_crtc *crtc,
2565 struct drm_framebuffer *fb,
2566 int32_t crtc_x, int32_t crtc_y,
2567 uint32_t crtc_w, uint32_t crtc_h,
2568 /* src_{x,y,w,h} values are 16.16 fixed point */
2569 uint32_t src_x, uint32_t src_y,
2570 uint32_t src_w, uint32_t src_h)
2571{
2572 int ret;
2573
2574 drm_modeset_lock_all(plane->dev);
2575 ret = __setplane_internal(plane, crtc, fb,
2576 crtc_x, crtc_y, crtc_w, crtc_h,
2577 src_x, src_y, src_w, src_h);
2578 drm_modeset_unlock_all(plane->dev);
2579
2580 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002581}
2582
2583/**
2584 * drm_mode_setplane - configure a plane's configuration
2585 * @dev: DRM device
2586 * @data: ioctl data*
2587 * @file_priv: DRM file info
2588 *
2589 * Set plane configuration, including placement, fb, scaling, and other factors.
2590 * Or pass a NULL fb to disable (planes may be disabled without providing a
2591 * valid crtc).
2592 *
2593 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002594 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002595 */
2596int drm_mode_setplane(struct drm_device *dev, void *data,
2597 struct drm_file *file_priv)
2598{
2599 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002600 struct drm_plane *plane;
2601 struct drm_crtc *crtc = NULL;
2602 struct drm_framebuffer *fb = NULL;
2603
2604 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2605 return -EINVAL;
2606
Matt Roperb36552b2014-06-10 08:28:09 -07002607 /*
2608 * First, find the plane, crtc, and fb objects. If not available,
2609 * we don't bother to call the driver.
2610 */
Rob Clark933f6222014-11-25 20:33:11 -05002611 plane = drm_plane_find(dev, plane_req->plane_id);
2612 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002613 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2614 plane_req->plane_id);
2615 return -ENOENT;
2616 }
Matt Roperb36552b2014-06-10 08:28:09 -07002617
2618 if (plane_req->fb_id) {
2619 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2620 if (!fb) {
2621 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2622 plane_req->fb_id);
2623 return -ENOENT;
2624 }
2625
Rob Clark933f6222014-11-25 20:33:11 -05002626 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2627 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002628 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2629 plane_req->crtc_id);
2630 return -ENOENT;
2631 }
Matt Roperb36552b2014-06-10 08:28:09 -07002632 }
2633
Matt Roper161d0dc2014-06-10 08:28:10 -07002634 /*
2635 * setplane_internal will take care of deref'ing either the old or new
2636 * framebuffer depending on success.
2637 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002638 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002639 plane_req->crtc_x, plane_req->crtc_y,
2640 plane_req->crtc_w, plane_req->crtc_h,
2641 plane_req->src_x, plane_req->src_y,
2642 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002643}
2644
2645/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002646 * drm_mode_set_config_internal - helper to call ->set_config
2647 * @set: modeset config to set
2648 *
2649 * This is a little helper to wrap internal calls to the ->set_config driver
2650 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002651 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002652 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002653 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002654 */
2655int drm_mode_set_config_internal(struct drm_mode_set *set)
2656{
2657 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002658 struct drm_framebuffer *fb;
2659 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002660 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002661
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002662 /*
2663 * NOTE: ->set_config can also disable other crtcs (if we steal all
2664 * connectors from it), hence we need to refcount the fbs across all
2665 * crtcs. Atomic modeset will have saner semantics ...
2666 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002667 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002668 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002669
Daniel Vetterb0d12322012-12-11 01:07:12 +01002670 fb = set->fb;
2671
2672 ret = crtc->funcs->set_config(set);
2673 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002674 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002675 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002676 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002677
Daniel Vettere4f62542015-07-09 23:44:35 +02002678 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002679 if (tmp->primary->fb)
2680 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002681 if (tmp->primary->old_fb)
2682 drm_framebuffer_unreference(tmp->primary->old_fb);
2683 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002684 }
2685
2686 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002687}
2688EXPORT_SYMBOL(drm_mode_set_config_internal);
2689
Matt Roperaf936292014-04-01 15:22:34 -07002690/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002691 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2692 * @mode: mode to query
2693 * @hdisplay: hdisplay value to fill in
2694 * @vdisplay: vdisplay value to fill in
2695 *
2696 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2697 * the appropriate layout.
2698 */
2699void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2700 int *hdisplay, int *vdisplay)
2701{
2702 struct drm_display_mode adjusted;
2703
2704 drm_mode_copy(&adjusted, mode);
2705 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2706 *hdisplay = adjusted.crtc_hdisplay;
2707 *vdisplay = adjusted.crtc_vdisplay;
2708}
2709EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2710
2711/**
Matt Roperaf936292014-04-01 15:22:34 -07002712 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2713 * CRTC viewport
2714 * @crtc: CRTC that framebuffer will be displayed on
2715 * @x: x panning
2716 * @y: y panning
2717 * @mode: mode that framebuffer will be displayed under
2718 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002719 */
Matt Roperaf936292014-04-01 15:22:34 -07002720int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2721 int x, int y,
2722 const struct drm_display_mode *mode,
2723 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002724
2725{
2726 int hdisplay, vdisplay;
2727
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002728 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002729
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002730 if (crtc->state &&
2731 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2732 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002733 swap(hdisplay, vdisplay);
2734
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002735 return check_src_coords(x << 16, y << 16,
2736 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002737}
Matt Roperaf936292014-04-01 15:22:34 -07002738EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002739
Daniel Vetter2d13b672012-12-11 13:47:23 +01002740/**
Dave Airlief453ba02008-11-07 14:05:41 -08002741 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002742 * @dev: drm device for the ioctl
2743 * @data: data pointer for the ioctl
2744 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002745 *
Dave Airlief453ba02008-11-07 14:05:41 -08002746 * Build a new CRTC configuration based on user request.
2747 *
2748 * Called by the user via ioctl.
2749 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002750 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002751 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002752 */
2753int drm_mode_setcrtc(struct drm_device *dev, void *data,
2754 struct drm_file *file_priv)
2755{
2756 struct drm_mode_config *config = &dev->mode_config;
2757 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002758 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002759 struct drm_connector **connector_set = NULL, *connector;
2760 struct drm_framebuffer *fb = NULL;
2761 struct drm_display_mode *mode = NULL;
2762 struct drm_mode_set set;
2763 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002764 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002765 int i;
2766
Dave Airliefb3b06c2011-02-08 13:55:21 +10002767 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2768 return -EINVAL;
2769
Zhao Junwang01447e92015-07-07 17:08:35 +08002770 /*
2771 * Universal plane src offsets are only 16.16, prevent havoc for
2772 * drivers using universal plane code internally.
2773 */
2774 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002775 return -ERANGE;
2776
Daniel Vetter84849902012-12-02 00:28:11 +01002777 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002778 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2779 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002780 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002781 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002782 goto out;
2783 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002784 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002785
2786 if (crtc_req->mode_valid) {
2787 /* If we have a mode we need a framebuffer. */
2788 /* If we pass -1, set the mode with the currently bound fb */
2789 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002790 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002791 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2792 ret = -EINVAL;
2793 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002794 }
Matt Roperf4510a22014-04-01 15:22:40 -07002795 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002796 /* Make refcounting symmetric with the lookup path. */
2797 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002798 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002799 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2800 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002801 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2802 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002803 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002804 goto out;
2805 }
Dave Airlief453ba02008-11-07 14:05:41 -08002806 }
2807
2808 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002809 if (!mode) {
2810 ret = -ENOMEM;
2811 goto out;
2812 }
2813
Daniel Stone934a8a82015-05-22 13:34:48 +01002814 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002815 if (ret) {
2816 DRM_DEBUG_KMS("Invalid mode\n");
2817 goto out;
2818 }
2819
Dave Airlief453ba02008-11-07 14:05:41 -08002820 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002821
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002822 /*
2823 * Check whether the primary plane supports the fb pixel format.
2824 * Drivers not implementing the universal planes API use a
2825 * default formats list provided by the DRM core which doesn't
2826 * match real hardware capabilities. Skip the check in that
2827 * case.
2828 */
2829 if (!crtc->primary->format_default) {
2830 ret = drm_plane_check_pixel_format(crtc->primary,
2831 fb->pixel_format);
2832 if (ret) {
2833 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2834 drm_get_format_name(fb->pixel_format));
2835 goto out;
2836 }
2837 }
2838
Damien Lespiauc11e9282013-09-25 16:45:30 +01002839 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2840 mode, fb);
2841 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002842 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002843
Dave Airlief453ba02008-11-07 14:05:41 -08002844 }
2845
2846 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002847 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002848 ret = -EINVAL;
2849 goto out;
2850 }
2851
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002852 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002853 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002854 crtc_req->count_connectors);
2855 ret = -EINVAL;
2856 goto out;
2857 }
2858
2859 if (crtc_req->count_connectors > 0) {
2860 u32 out_id;
2861
2862 /* Avoid unbounded kernel memory allocation */
2863 if (crtc_req->count_connectors > config->num_connector) {
2864 ret = -EINVAL;
2865 goto out;
2866 }
2867
Thierry Reding2f6c5382014-12-10 13:03:36 +01002868 connector_set = kmalloc_array(crtc_req->count_connectors,
2869 sizeof(struct drm_connector *),
2870 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002871 if (!connector_set) {
2872 ret = -ENOMEM;
2873 goto out;
2874 }
2875
2876 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002877 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002878 if (get_user(out_id, &set_connectors_ptr[i])) {
2879 ret = -EFAULT;
2880 goto out;
2881 }
2882
Rob Clarka2b34e22013-10-05 16:36:52 -04002883 connector = drm_connector_find(dev, out_id);
2884 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002885 DRM_DEBUG_KMS("Connector id %d unknown\n",
2886 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002887 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002888 goto out;
2889 }
Jerome Glisse94401062010-07-15 15:43:25 -04002890 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2891 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002892 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002893
2894 connector_set[i] = connector;
2895 }
2896 }
2897
2898 set.crtc = crtc;
2899 set.x = crtc_req->x;
2900 set.y = crtc_req->y;
2901 set.mode = mode;
2902 set.connectors = connector_set;
2903 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002904 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002905 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002906
2907out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002908 if (fb)
2909 drm_framebuffer_unreference(fb);
2910
Dave Airlief453ba02008-11-07 14:05:41 -08002911 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002912 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002913 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002914 return ret;
2915}
2916
Matt Roper161d0dc2014-06-10 08:28:10 -07002917/**
2918 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2919 * universal plane handler call
2920 * @crtc: crtc to update cursor for
2921 * @req: data pointer for the ioctl
2922 * @file_priv: drm file for the ioctl call
2923 *
2924 * Legacy cursor ioctl's work directly with driver buffer handles. To
2925 * translate legacy ioctl calls into universal plane handler calls, we need to
2926 * wrap the native buffer handle in a drm_framebuffer.
2927 *
2928 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2929 * buffer with a pitch of 4*width; the universal plane interface should be used
2930 * directly in cases where the hardware can support other buffer settings and
2931 * userspace wants to make use of these capabilities.
2932 *
2933 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002934 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002935 */
2936static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2937 struct drm_mode_cursor2 *req,
2938 struct drm_file *file_priv)
2939{
2940 struct drm_device *dev = crtc->dev;
2941 struct drm_framebuffer *fb = NULL;
2942 struct drm_mode_fb_cmd2 fbreq = {
2943 .width = req->width,
2944 .height = req->height,
2945 .pixel_format = DRM_FORMAT_ARGB8888,
2946 .pitches = { req->width * 4 },
2947 .handles = { req->handle },
2948 };
2949 int32_t crtc_x, crtc_y;
2950 uint32_t crtc_w = 0, crtc_h = 0;
2951 uint32_t src_w = 0, src_h = 0;
2952 int ret = 0;
2953
2954 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002955 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002956
2957 /*
2958 * Obtain fb we'll be using (either new or existing) and take an extra
2959 * reference to it if fb != null. setplane will take care of dropping
2960 * the reference if the plane update fails.
2961 */
2962 if (req->flags & DRM_MODE_CURSOR_BO) {
2963 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002964 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002965 if (IS_ERR(fb)) {
2966 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2967 return PTR_ERR(fb);
2968 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002969 } else {
2970 fb = NULL;
2971 }
2972 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002973 fb = crtc->cursor->fb;
2974 if (fb)
2975 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002976 }
2977
2978 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2979 crtc_x = req->x;
2980 crtc_y = req->y;
2981 } else {
2982 crtc_x = crtc->cursor_x;
2983 crtc_y = crtc->cursor_y;
2984 }
2985
2986 if (fb) {
2987 crtc_w = fb->width;
2988 crtc_h = fb->height;
2989 src_w = fb->width << 16;
2990 src_h = fb->height << 16;
2991 }
2992
2993 /*
2994 * setplane_internal will take care of deref'ing either the old or new
2995 * framebuffer depending on success.
2996 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002997 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002998 crtc_x, crtc_y, crtc_w, crtc_h,
2999 0, 0, src_w, src_h);
3000
3001 /* Update successful; save new cursor position, if necessary */
3002 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3003 crtc->cursor_x = req->x;
3004 crtc->cursor_y = req->y;
3005 }
3006
3007 return ret;
3008}
3009
Dave Airlie4c813d42013-06-20 11:48:52 +10003010static int drm_mode_cursor_common(struct drm_device *dev,
3011 struct drm_mode_cursor2 *req,
3012 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003013{
Dave Airlief453ba02008-11-07 14:05:41 -08003014 struct drm_crtc *crtc;
3015 int ret = 0;
3016
Dave Airliefb3b06c2011-02-08 13:55:21 +10003017 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3018 return -EINVAL;
3019
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003020 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003021 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003022
Rob Clarka2b34e22013-10-05 16:36:52 -04003023 crtc = drm_crtc_find(dev, req->crtc_id);
3024 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003025 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003026 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003027 }
Dave Airlief453ba02008-11-07 14:05:41 -08003028
Matt Roper161d0dc2014-06-10 08:28:10 -07003029 /*
3030 * If this crtc has a universal cursor plane, call that plane's update
3031 * handler rather than using legacy cursor handlers.
3032 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003033 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003034 if (crtc->cursor) {
3035 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3036 goto out;
3037 }
3038
Dave Airlief453ba02008-11-07 14:05:41 -08003039 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003040 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003041 ret = -ENXIO;
3042 goto out;
3043 }
3044 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003045 if (crtc->funcs->cursor_set2)
3046 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3047 req->width, req->height, req->hot_x, req->hot_y);
3048 else
3049 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3050 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003051 }
3052
3053 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3054 if (crtc->funcs->cursor_move) {
3055 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3056 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003057 ret = -EFAULT;
3058 goto out;
3059 }
3060 }
3061out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003062 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003063
Dave Airlief453ba02008-11-07 14:05:41 -08003064 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003065
3066}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003067
3068
3069/**
3070 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3071 * @dev: drm device for the ioctl
3072 * @data: data pointer for the ioctl
3073 * @file_priv: drm file for the ioctl call
3074 *
3075 * Set the cursor configuration based on user request.
3076 *
3077 * Called by the user via ioctl.
3078 *
3079 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003080 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003081 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003082int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003083 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003084{
3085 struct drm_mode_cursor *req = data;
3086 struct drm_mode_cursor2 new_req;
3087
3088 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3089 new_req.hot_x = new_req.hot_y = 0;
3090
3091 return drm_mode_cursor_common(dev, &new_req, file_priv);
3092}
3093
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003094/**
3095 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3096 * @dev: drm device for the ioctl
3097 * @data: data pointer for the ioctl
3098 * @file_priv: drm file for the ioctl call
3099 *
3100 * Set the cursor configuration based on user request. This implements the 2nd
3101 * version of the cursor ioctl, which allows userspace to additionally specify
3102 * the hotspot of the pointer.
3103 *
3104 * Called by the user via ioctl.
3105 *
3106 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003107 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003108 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003109int drm_mode_cursor2_ioctl(struct drm_device *dev,
3110 void *data, struct drm_file *file_priv)
3111{
3112 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003113
Dave Airlie4c813d42013-06-20 11:48:52 +10003114 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003115}
3116
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003117/**
3118 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3119 * @bpp: bits per pixels
3120 * @depth: bit depth per pixel
3121 *
3122 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3123 * Useful in fbdev emulation code, since that deals in those values.
3124 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003125uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3126{
3127 uint32_t fmt;
3128
3129 switch (bpp) {
3130 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003131 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003132 break;
3133 case 16:
3134 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003135 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003136 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003137 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003138 break;
3139 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003140 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003141 break;
3142 case 32:
3143 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003144 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003145 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003146 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003147 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003148 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003149 break;
3150 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003151 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3152 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003153 break;
3154 }
3155
3156 return fmt;
3157}
3158EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3159
Dave Airlief453ba02008-11-07 14:05:41 -08003160/**
3161 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003162 * @dev: drm device for the ioctl
3163 * @data: data pointer for the ioctl
3164 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003165 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003166 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003167 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003168 *
3169 * Called by the user via ioctl.
3170 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003171 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003172 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003173 */
3174int drm_mode_addfb(struct drm_device *dev,
3175 void *data, struct drm_file *file_priv)
3176{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003177 struct drm_mode_fb_cmd *or = data;
3178 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003179 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003180
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003181 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003182 r.fb_id = or->fb_id;
3183 r.width = or->width;
3184 r.height = or->height;
3185 r.pitches[0] = or->pitch;
3186 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3187 r.handles[0] = or->handle;
3188
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003189 ret = drm_mode_addfb2(dev, &r, file_priv);
3190 if (ret)
3191 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003192
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003193 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003194
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003195 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003196}
3197
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003198static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003199{
3200 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3201
3202 switch (format) {
3203 case DRM_FORMAT_C8:
3204 case DRM_FORMAT_RGB332:
3205 case DRM_FORMAT_BGR233:
3206 case DRM_FORMAT_XRGB4444:
3207 case DRM_FORMAT_XBGR4444:
3208 case DRM_FORMAT_RGBX4444:
3209 case DRM_FORMAT_BGRX4444:
3210 case DRM_FORMAT_ARGB4444:
3211 case DRM_FORMAT_ABGR4444:
3212 case DRM_FORMAT_RGBA4444:
3213 case DRM_FORMAT_BGRA4444:
3214 case DRM_FORMAT_XRGB1555:
3215 case DRM_FORMAT_XBGR1555:
3216 case DRM_FORMAT_RGBX5551:
3217 case DRM_FORMAT_BGRX5551:
3218 case DRM_FORMAT_ARGB1555:
3219 case DRM_FORMAT_ABGR1555:
3220 case DRM_FORMAT_RGBA5551:
3221 case DRM_FORMAT_BGRA5551:
3222 case DRM_FORMAT_RGB565:
3223 case DRM_FORMAT_BGR565:
3224 case DRM_FORMAT_RGB888:
3225 case DRM_FORMAT_BGR888:
3226 case DRM_FORMAT_XRGB8888:
3227 case DRM_FORMAT_XBGR8888:
3228 case DRM_FORMAT_RGBX8888:
3229 case DRM_FORMAT_BGRX8888:
3230 case DRM_FORMAT_ARGB8888:
3231 case DRM_FORMAT_ABGR8888:
3232 case DRM_FORMAT_RGBA8888:
3233 case DRM_FORMAT_BGRA8888:
3234 case DRM_FORMAT_XRGB2101010:
3235 case DRM_FORMAT_XBGR2101010:
3236 case DRM_FORMAT_RGBX1010102:
3237 case DRM_FORMAT_BGRX1010102:
3238 case DRM_FORMAT_ARGB2101010:
3239 case DRM_FORMAT_ABGR2101010:
3240 case DRM_FORMAT_RGBA1010102:
3241 case DRM_FORMAT_BGRA1010102:
3242 case DRM_FORMAT_YUYV:
3243 case DRM_FORMAT_YVYU:
3244 case DRM_FORMAT_UYVY:
3245 case DRM_FORMAT_VYUY:
3246 case DRM_FORMAT_AYUV:
3247 case DRM_FORMAT_NV12:
3248 case DRM_FORMAT_NV21:
3249 case DRM_FORMAT_NV16:
3250 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003251 case DRM_FORMAT_NV24:
3252 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003253 case DRM_FORMAT_YUV410:
3254 case DRM_FORMAT_YVU410:
3255 case DRM_FORMAT_YUV411:
3256 case DRM_FORMAT_YVU411:
3257 case DRM_FORMAT_YUV420:
3258 case DRM_FORMAT_YVU420:
3259 case DRM_FORMAT_YUV422:
3260 case DRM_FORMAT_YVU422:
3261 case DRM_FORMAT_YUV444:
3262 case DRM_FORMAT_YVU444:
3263 return 0;
3264 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003265 DRM_DEBUG_KMS("invalid pixel format %s\n",
3266 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003267 return -EINVAL;
3268 }
3269}
3270
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003271static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003272{
3273 int ret, hsub, vsub, num_planes, i;
3274
3275 ret = format_check(r);
3276 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003277 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3278 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003279 return ret;
3280 }
3281
3282 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3283 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3284 num_planes = drm_format_num_planes(r->pixel_format);
3285
3286 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003287 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003288 return -EINVAL;
3289 }
3290
3291 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003292 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003293 return -EINVAL;
3294 }
3295
3296 for (i = 0; i < num_planes; i++) {
3297 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003298 unsigned int height = r->height / (i != 0 ? vsub : 1);
3299 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003300
3301 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003302 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003303 return -EINVAL;
3304 }
3305
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003306 if ((uint64_t) width * cpp > UINT_MAX)
3307 return -ERANGE;
3308
3309 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3310 return -ERANGE;
3311
3312 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003313 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003314 return -EINVAL;
3315 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003316
3317 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3318 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3319 r->modifier[i], i);
3320 return -EINVAL;
3321 }
Rob Clark570655b2015-01-30 20:18:11 +05303322
3323 /* modifier specific checks: */
3324 switch (r->modifier[i]) {
3325 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3326 /* NOTE: the pitch restriction may be lifted later if it turns
3327 * out that no hw has this restriction:
3328 */
3329 if (r->pixel_format != DRM_FORMAT_NV12 ||
3330 width % 128 || height % 32 ||
3331 r->pitches[i] % 128) {
3332 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3333 return -EINVAL;
3334 }
3335 break;
3336
3337 default:
3338 break;
3339 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003340 }
3341
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003342 for (i = num_planes; i < 4; i++) {
3343 if (r->modifier[i]) {
3344 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3345 return -EINVAL;
3346 }
3347
3348 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3349 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3350 continue;
3351
3352 if (r->handles[i]) {
3353 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3354 return -EINVAL;
3355 }
3356
3357 if (r->pitches[i]) {
3358 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3359 return -EINVAL;
3360 }
3361
3362 if (r->offsets[i]) {
3363 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3364 return -EINVAL;
3365 }
3366 }
3367
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003368 return 0;
3369}
3370
Chris Wilson9a6f5132015-02-25 13:45:26 +00003371static struct drm_framebuffer *
3372internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003373 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003374 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003375{
3376 struct drm_mode_config *config = &dev->mode_config;
3377 struct drm_framebuffer *fb;
3378 int ret;
3379
Rob Clarke3eb3252015-02-05 14:41:52 +00003380 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003381 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3382 return ERR_PTR(-EINVAL);
3383 }
3384
3385 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3386 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3387 r->width, config->min_width, config->max_width);
3388 return ERR_PTR(-EINVAL);
3389 }
3390 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3391 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3392 r->height, config->min_height, config->max_height);
3393 return ERR_PTR(-EINVAL);
3394 }
3395
Rob Clarke3eb3252015-02-05 14:41:52 +00003396 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3397 !dev->mode_config.allow_fb_modifiers) {
3398 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3399 return ERR_PTR(-EINVAL);
3400 }
3401
Matt Roperc394c2b2014-06-10 08:28:08 -07003402 ret = framebuffer_check(r);
3403 if (ret)
3404 return ERR_PTR(ret);
3405
3406 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3407 if (IS_ERR(fb)) {
3408 DRM_DEBUG_KMS("could not create framebuffer\n");
3409 return fb;
3410 }
3411
Matt Roperc394c2b2014-06-10 08:28:08 -07003412 return fb;
3413}
3414
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003415/**
3416 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003417 * @dev: drm device for the ioctl
3418 * @data: data pointer for the ioctl
3419 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003420 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003421 * Add a new FB to the specified CRTC, given a user request with format. This is
3422 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3423 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003424 *
3425 * Called by the user via ioctl.
3426 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003427 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003428 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003429 */
3430int drm_mode_addfb2(struct drm_device *dev,
3431 void *data, struct drm_file *file_priv)
3432{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003433 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003434 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003435
Dave Airliefb3b06c2011-02-08 13:55:21 +10003436 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3437 return -EINVAL;
3438
Chris Wilson9a6f5132015-02-25 13:45:26 +00003439 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003440 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003441 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003442
Chris Wilson9a6f5132015-02-25 13:45:26 +00003443 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003444 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003445
3446 /* Transfer ownership to the filp for reaping on close */
3447 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003448 list_add(&fb->filp_head, &file_priv->fbs);
3449 mutex_unlock(&file_priv->fbs_lock);
3450
Matt Roperc394c2b2014-06-10 08:28:08 -07003451 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003452}
3453
3454/**
3455 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003456 * @dev: drm device for the ioctl
3457 * @data: data pointer for the ioctl
3458 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003459 *
Dave Airlief453ba02008-11-07 14:05:41 -08003460 * Remove the FB specified by the user.
3461 *
3462 * Called by the user via ioctl.
3463 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003464 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003465 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003466 */
3467int drm_mode_rmfb(struct drm_device *dev,
3468 void *data, struct drm_file *file_priv)
3469{
Dave Airlief453ba02008-11-07 14:05:41 -08003470 struct drm_framebuffer *fb = NULL;
3471 struct drm_framebuffer *fbl = NULL;
3472 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003473 int found = 0;
3474
Dave Airliefb3b06c2011-02-08 13:55:21 +10003475 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3476 return -EINVAL;
3477
Dave Airlie72fe90b2016-04-15 15:10:40 +10003478 fb = drm_framebuffer_lookup(dev, *id);
3479 if (!fb)
3480 return -ENOENT;
3481
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003482 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003483 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3484 if (fb == fbl)
3485 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003486 if (!found) {
3487 mutex_unlock(&file_priv->fbs_lock);
3488 goto fail_unref;
3489 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003490
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003491 list_del_init(&fb->filp_head);
3492 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003493
Dave Airlie72fe90b2016-04-15 15:10:40 +10003494 /* we now own the reference that was stored in the fbs list */
3495 drm_framebuffer_unreference(fb);
3496
3497 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003498 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003499
Daniel Vetter2b677e82012-12-10 21:16:05 +01003500 return 0;
3501
Dave Airlie72fe90b2016-04-15 15:10:40 +10003502fail_unref:
3503 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003504 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003505}
3506
3507/**
3508 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003509 * @dev: drm device for the ioctl
3510 * @data: data pointer for the ioctl
3511 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003512 *
Dave Airlief453ba02008-11-07 14:05:41 -08003513 * Lookup the FB given its ID and return info about it.
3514 *
3515 * Called by the user via ioctl.
3516 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003517 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003518 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003519 */
3520int drm_mode_getfb(struct drm_device *dev,
3521 void *data, struct drm_file *file_priv)
3522{
3523 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003524 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003525 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003526
Dave Airliefb3b06c2011-02-08 13:55:21 +10003527 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3528 return -EINVAL;
3529
Daniel Vetter786b99e2012-12-02 21:53:40 +01003530 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003531 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003532 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003533
3534 r->height = fb->height;
3535 r->width = fb->width;
3536 r->depth = fb->depth;
3537 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003538 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003539 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003540 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003541 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003542 ret = fb->funcs->create_handle(fb, file_priv,
3543 &r->handle);
3544 } else {
3545 /* GET_FB() is an unprivileged ioctl so we must not
3546 * return a buffer-handle to non-master processes! For
3547 * backwards-compatibility reasons, we cannot make
3548 * GET_FB() privileged, so just return an invalid handle
3549 * for non-masters. */
3550 r->handle = 0;
3551 ret = 0;
3552 }
3553 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003554 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003555 }
Dave Airlief453ba02008-11-07 14:05:41 -08003556
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003557 drm_framebuffer_unreference(fb);
3558
Dave Airlief453ba02008-11-07 14:05:41 -08003559 return ret;
3560}
3561
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003562/**
3563 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3564 * @dev: drm device for the ioctl
3565 * @data: data pointer for the ioctl
3566 * @file_priv: drm file for the ioctl call
3567 *
3568 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3569 * rectangle list. Generic userspace which does frontbuffer rendering must call
3570 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3571 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3572 *
3573 * Modesetting drivers which always update the frontbuffer do not need to
3574 * implement the corresponding ->dirty framebuffer callback.
3575 *
3576 * Called by the user via ioctl.
3577 *
3578 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003579 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003580 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003581int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3582 void *data, struct drm_file *file_priv)
3583{
3584 struct drm_clip_rect __user *clips_ptr;
3585 struct drm_clip_rect *clips = NULL;
3586 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003587 struct drm_framebuffer *fb;
3588 unsigned flags;
3589 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003590 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003591
Dave Airliefb3b06c2011-02-08 13:55:21 +10003592 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3593 return -EINVAL;
3594
Daniel Vetter786b99e2012-12-02 21:53:40 +01003595 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003596 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003597 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003598
3599 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003600 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003601
3602 if (!num_clips != !clips_ptr) {
3603 ret = -EINVAL;
3604 goto out_err1;
3605 }
3606
3607 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3608
3609 /* If userspace annotates copy, clips must come in pairs */
3610 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3611 ret = -EINVAL;
3612 goto out_err1;
3613 }
3614
3615 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003616 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3617 ret = -EINVAL;
3618 goto out_err1;
3619 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003620 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003621 if (!clips) {
3622 ret = -ENOMEM;
3623 goto out_err1;
3624 }
3625
3626 ret = copy_from_user(clips, clips_ptr,
3627 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003628 if (ret) {
3629 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003630 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003631 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003632 }
3633
3634 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003635 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3636 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003637 } else {
3638 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003639 }
3640
3641out_err2:
3642 kfree(clips);
3643out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003644 drm_framebuffer_unreference(fb);
3645
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003646 return ret;
3647}
3648
3649
Dave Airlief453ba02008-11-07 14:05:41 -08003650/**
3651 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003652 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003653 *
Dave Airlief453ba02008-11-07 14:05:41 -08003654 * Destroy all the FBs associated with @filp.
3655 *
3656 * Called by the user via ioctl.
3657 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003658 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003659 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003660 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003661void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003662{
Dave Airlief453ba02008-11-07 14:05:41 -08003663 struct drm_framebuffer *fb, *tfb;
3664
Daniel Vetter1b116292014-09-24 21:51:06 +02003665 /*
3666 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003667 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003668 * avoid upsetting lockdep since the universal cursor code adds a
3669 * framebuffer while holding mutex locks.
3670 *
3671 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3672 * locks is impossible here since no one else but this function can get
3673 * at it any more.
3674 */
Dave Airlief453ba02008-11-07 14:05:41 -08003675 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003676 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003677
Maarten Lankhorst73f75702015-09-09 16:40:57 +02003678 /* This drops the fpriv->fbs reference. */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003679 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003680 }
Dave Airlief453ba02008-11-07 14:05:41 -08003681}
3682
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003683/**
3684 * drm_property_create - create a new property type
3685 * @dev: drm device
3686 * @flags: flags specifying the property type
3687 * @name: name of the property
3688 * @num_values: number of pre-defined values
3689 *
3690 * This creates a new generic drm property which can then be attached to a drm
3691 * object with drm_object_attach_property. The returned property object must be
3692 * freed with drm_property_destroy.
3693 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003694 * Note that the DRM core keeps a per-device list of properties and that, if
3695 * drm_mode_config_cleanup() is called, it will destroy all properties created
3696 * by the driver.
3697 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003698 * Returns:
3699 * A pointer to the newly created property on success, NULL on failure.
3700 */
Dave Airlief453ba02008-11-07 14:05:41 -08003701struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3702 const char *name, int num_values)
3703{
3704 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003705 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003706
3707 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3708 if (!property)
3709 return NULL;
3710
Rob Clark98f75de2014-05-30 11:37:03 -04003711 property->dev = dev;
3712
Dave Airlief453ba02008-11-07 14:05:41 -08003713 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003714 property->values = kcalloc(num_values, sizeof(uint64_t),
3715 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003716 if (!property->values)
3717 goto fail;
3718 }
3719
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003720 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3721 if (ret)
3722 goto fail;
3723
Dave Airlief453ba02008-11-07 14:05:41 -08003724 property->flags = flags;
3725 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003726 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003727
Vinson Lee471dd2e2011-11-10 11:55:40 -08003728 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003729 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003730 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3731 }
Dave Airlief453ba02008-11-07 14:05:41 -08003732
3733 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003734
3735 WARN_ON(!drm_property_type_valid(property));
3736
Dave Airlief453ba02008-11-07 14:05:41 -08003737 return property;
3738fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003739 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003740 kfree(property);
3741 return NULL;
3742}
3743EXPORT_SYMBOL(drm_property_create);
3744
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003745/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003746 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003747 * @dev: drm device
3748 * @flags: flags specifying the property type
3749 * @name: name of the property
3750 * @props: enumeration lists with property values
3751 * @num_values: number of pre-defined values
3752 *
3753 * This creates a new generic drm property which can then be attached to a drm
3754 * object with drm_object_attach_property. The returned property object must be
3755 * freed with drm_property_destroy.
3756 *
3757 * Userspace is only allowed to set one of the predefined values for enumeration
3758 * properties.
3759 *
3760 * Returns:
3761 * A pointer to the newly created property on success, NULL on failure.
3762 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003763struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3764 const char *name,
3765 const struct drm_prop_enum_list *props,
3766 int num_values)
3767{
3768 struct drm_property *property;
3769 int i, ret;
3770
3771 flags |= DRM_MODE_PROP_ENUM;
3772
3773 property = drm_property_create(dev, flags, name, num_values);
3774 if (!property)
3775 return NULL;
3776
3777 for (i = 0; i < num_values; i++) {
3778 ret = drm_property_add_enum(property, i,
3779 props[i].type,
3780 props[i].name);
3781 if (ret) {
3782 drm_property_destroy(dev, property);
3783 return NULL;
3784 }
3785 }
3786
3787 return property;
3788}
3789EXPORT_SYMBOL(drm_property_create_enum);
3790
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003791/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003792 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003793 * @dev: drm device
3794 * @flags: flags specifying the property type
3795 * @name: name of the property
3796 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003797 * @num_props: size of the @props array
3798 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003799 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003800 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003801 * object with drm_object_attach_property. The returned property object must be
3802 * freed with drm_property_destroy.
3803 *
3804 * Compared to plain enumeration properties userspace is allowed to set any
3805 * or'ed together combination of the predefined property bitflag values
3806 *
3807 * Returns:
3808 * A pointer to the newly created property on success, NULL on failure.
3809 */
Rob Clark49e27542012-05-17 02:23:26 -06003810struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3811 int flags, const char *name,
3812 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303813 int num_props,
3814 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003815{
3816 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303817 int i, ret, index = 0;
3818 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003819
3820 flags |= DRM_MODE_PROP_BITMASK;
3821
3822 property = drm_property_create(dev, flags, name, num_values);
3823 if (!property)
3824 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303825 for (i = 0; i < num_props; i++) {
3826 if (!(supported_bits & (1ULL << props[i].type)))
3827 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003828
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303829 if (WARN_ON(index >= num_values)) {
3830 drm_property_destroy(dev, property);
3831 return NULL;
3832 }
3833
3834 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003835 props[i].type,
3836 props[i].name);
3837 if (ret) {
3838 drm_property_destroy(dev, property);
3839 return NULL;
3840 }
3841 }
3842
3843 return property;
3844}
3845EXPORT_SYMBOL(drm_property_create_bitmask);
3846
Rob Clarkebc44cf2012-09-12 22:22:31 -05003847static struct drm_property *property_create_range(struct drm_device *dev,
3848 int flags, const char *name,
3849 uint64_t min, uint64_t max)
3850{
3851 struct drm_property *property;
3852
3853 property = drm_property_create(dev, flags, name, 2);
3854 if (!property)
3855 return NULL;
3856
3857 property->values[0] = min;
3858 property->values[1] = max;
3859
3860 return property;
3861}
3862
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003863/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003864 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003865 * @dev: drm device
3866 * @flags: flags specifying the property type
3867 * @name: name of the property
3868 * @min: minimum value of the property
3869 * @max: maximum value of the property
3870 *
3871 * This creates a new generic drm property which can then be attached to a drm
3872 * object with drm_object_attach_property. The returned property object must be
3873 * freed with drm_property_destroy.
3874 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003875 * Userspace is allowed to set any unsigned integer value in the (min, max)
3876 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003877 *
3878 * Returns:
3879 * A pointer to the newly created property on success, NULL on failure.
3880 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003881struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3882 const char *name,
3883 uint64_t min, uint64_t max)
3884{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003885 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3886 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003887}
3888EXPORT_SYMBOL(drm_property_create_range);
3889
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003890/**
3891 * drm_property_create_signed_range - create a new signed ranged property type
3892 * @dev: drm device
3893 * @flags: flags specifying the property type
3894 * @name: name of the property
3895 * @min: minimum value of the property
3896 * @max: maximum value of the property
3897 *
3898 * This creates a new generic drm property which can then be attached to a drm
3899 * object with drm_object_attach_property. The returned property object must be
3900 * freed with drm_property_destroy.
3901 *
3902 * Userspace is allowed to set any signed integer value in the (min, max)
3903 * range inclusive.
3904 *
3905 * Returns:
3906 * A pointer to the newly created property on success, NULL on failure.
3907 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003908struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3909 int flags, const char *name,
3910 int64_t min, int64_t max)
3911{
3912 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3913 name, I642U64(min), I642U64(max));
3914}
3915EXPORT_SYMBOL(drm_property_create_signed_range);
3916
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003917/**
3918 * drm_property_create_object - create a new object property type
3919 * @dev: drm device
3920 * @flags: flags specifying the property type
3921 * @name: name of the property
3922 * @type: object type from DRM_MODE_OBJECT_* defines
3923 *
3924 * This creates a new generic drm property which can then be attached to a drm
3925 * object with drm_object_attach_property. The returned property object must be
3926 * freed with drm_property_destroy.
3927 *
3928 * Userspace is only allowed to set this to any property value of the given
3929 * @type. Only useful for atomic properties, which is enforced.
3930 *
3931 * Returns:
3932 * A pointer to the newly created property on success, NULL on failure.
3933 */
Rob Clark98f75de2014-05-30 11:37:03 -04003934struct drm_property *drm_property_create_object(struct drm_device *dev,
3935 int flags, const char *name, uint32_t type)
3936{
3937 struct drm_property *property;
3938
3939 flags |= DRM_MODE_PROP_OBJECT;
3940
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003941 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3942 return NULL;
3943
Rob Clark98f75de2014-05-30 11:37:03 -04003944 property = drm_property_create(dev, flags, name, 1);
3945 if (!property)
3946 return NULL;
3947
3948 property->values[0] = type;
3949
3950 return property;
3951}
3952EXPORT_SYMBOL(drm_property_create_object);
3953
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003954/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003955 * drm_property_create_bool - create a new boolean property type
3956 * @dev: drm device
3957 * @flags: flags specifying the property type
3958 * @name: name of the property
3959 *
3960 * This creates a new generic drm property which can then be attached to a drm
3961 * object with drm_object_attach_property. The returned property object must be
3962 * freed with drm_property_destroy.
3963 *
3964 * This is implemented as a ranged property with only {0, 1} as valid values.
3965 *
3966 * Returns:
3967 * A pointer to the newly created property on success, NULL on failure.
3968 */
3969struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3970 const char *name)
3971{
3972 return drm_property_create_range(dev, flags, name, 0, 1);
3973}
3974EXPORT_SYMBOL(drm_property_create_bool);
3975
3976/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003977 * drm_property_add_enum - add a possible value to an enumeration property
3978 * @property: enumeration property to change
3979 * @index: index of the new enumeration
3980 * @value: value of the new enumeration
3981 * @name: symbolic name of the new enumeration
3982 *
3983 * This functions adds enumerations to a property.
3984 *
3985 * It's use is deprecated, drivers should use one of the more specific helpers
3986 * to directly create the property with all enumerations already attached.
3987 *
3988 * Returns:
3989 * Zero on success, error code on failure.
3990 */
Dave Airlief453ba02008-11-07 14:05:41 -08003991int drm_property_add_enum(struct drm_property *property, int index,
3992 uint64_t value, const char *name)
3993{
3994 struct drm_property_enum *prop_enum;
3995
Rob Clark5ea22f22014-05-30 11:34:01 -04003996 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3997 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003998 return -EINVAL;
3999
4000 /*
4001 * Bitmask enum properties have the additional constraint of values
4002 * from 0 to 63
4003 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004004 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4005 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004006 return -EINVAL;
4007
Daniel Vetter3758b342014-11-19 18:38:10 +01004008 if (!list_empty(&property->enum_list)) {
4009 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004010 if (prop_enum->value == value) {
4011 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4012 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4013 return 0;
4014 }
4015 }
4016 }
4017
4018 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4019 if (!prop_enum)
4020 return -ENOMEM;
4021
4022 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4023 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4024 prop_enum->value = value;
4025
4026 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004027 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004028 return 0;
4029}
4030EXPORT_SYMBOL(drm_property_add_enum);
4031
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004032/**
4033 * drm_property_destroy - destroy a drm property
4034 * @dev: drm device
4035 * @property: property to destry
4036 *
4037 * This function frees a property including any attached resources like
4038 * enumeration values.
4039 */
Dave Airlief453ba02008-11-07 14:05:41 -08004040void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4041{
4042 struct drm_property_enum *prop_enum, *pt;
4043
Daniel Vetter3758b342014-11-19 18:38:10 +01004044 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004045 list_del(&prop_enum->head);
4046 kfree(prop_enum);
4047 }
4048
4049 if (property->num_values)
4050 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004051 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004052 list_del(&property->head);
4053 kfree(property);
4054}
4055EXPORT_SYMBOL(drm_property_destroy);
4056
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004057/**
4058 * drm_object_attach_property - attach a property to a modeset object
4059 * @obj: drm modeset object
4060 * @property: property to attach
4061 * @init_val: initial value of the property
4062 *
4063 * This attaches the given property to the modeset object with the given initial
4064 * value. Currently this function cannot fail since the properties are stored in
4065 * a statically sized array.
4066 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004067void drm_object_attach_property(struct drm_mode_object *obj,
4068 struct drm_property *property,
4069 uint64_t init_val)
4070{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004071 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004072
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004073 if (count == DRM_OBJECT_MAX_PROPERTY) {
4074 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4075 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4076 "you see this message on the same object type.\n",
4077 obj->type);
4078 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004079 }
4080
Rob Clarkb17cd752014-12-16 18:05:30 -05004081 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004082 obj->properties->values[count] = init_val;
4083 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004084 if (property->flags & DRM_MODE_PROP_ATOMIC)
4085 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004086}
4087EXPORT_SYMBOL(drm_object_attach_property);
4088
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004089/**
4090 * drm_object_property_set_value - set the value of a property
4091 * @obj: drm mode object to set property value for
4092 * @property: property to set
4093 * @val: value the property should be set to
4094 *
4095 * This functions sets a given property on a given object. This function only
4096 * changes the software state of the property, it does not call into the
4097 * driver's ->set_property callback.
4098 *
4099 * Returns:
4100 * Zero on success, error code on failure.
4101 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004102int drm_object_property_set_value(struct drm_mode_object *obj,
4103 struct drm_property *property, uint64_t val)
4104{
4105 int i;
4106
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004107 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004108 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004109 obj->properties->values[i] = val;
4110 return 0;
4111 }
4112 }
4113
4114 return -EINVAL;
4115}
4116EXPORT_SYMBOL(drm_object_property_set_value);
4117
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004118/**
4119 * drm_object_property_get_value - retrieve the value of a property
4120 * @obj: drm mode object to get property value from
4121 * @property: property to retrieve
4122 * @val: storage for the property value
4123 *
4124 * This function retrieves the softare state of the given property for the given
4125 * property. Since there is no driver callback to retrieve the current property
4126 * value this might be out of sync with the hardware, depending upon the driver
4127 * and property.
4128 *
4129 * Returns:
4130 * Zero on success, error code on failure.
4131 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004132int drm_object_property_get_value(struct drm_mode_object *obj,
4133 struct drm_property *property, uint64_t *val)
4134{
4135 int i;
4136
Rob Clark88a48e22014-12-18 16:01:50 -05004137 /* read-only properties bypass atomic mechanism and still store
4138 * their value in obj->properties->values[].. mostly to avoid
4139 * having to deal w/ EDID and similar props in atomic paths:
4140 */
4141 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4142 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4143 return drm_atomic_get_property(obj, property, val);
4144
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004145 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004146 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004147 *val = obj->properties->values[i];
4148 return 0;
4149 }
4150 }
4151
4152 return -EINVAL;
4153}
4154EXPORT_SYMBOL(drm_object_property_get_value);
4155
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004156/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004157 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004158 * @dev: DRM device
4159 * @data: ioctl data
4160 * @file_priv: DRM file info
4161 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004162 * This function retrieves the metadata for a given property, like the different
4163 * possible values for an enum property or the limits for a range property.
4164 *
4165 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004166 *
4167 * Called by the user via ioctl.
4168 *
4169 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004170 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004171 */
Dave Airlief453ba02008-11-07 14:05:41 -08004172int drm_mode_getproperty_ioctl(struct drm_device *dev,
4173 void *data, struct drm_file *file_priv)
4174{
Dave Airlief453ba02008-11-07 14:05:41 -08004175 struct drm_mode_get_property *out_resp = data;
4176 struct drm_property *property;
4177 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004178 int value_count = 0;
4179 int ret = 0, i;
4180 int copied;
4181 struct drm_property_enum *prop_enum;
4182 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004183 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004184
Dave Airliefb3b06c2011-02-08 13:55:21 +10004185 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4186 return -EINVAL;
4187
Daniel Vetter84849902012-12-02 00:28:11 +01004188 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004189 property = drm_property_find(dev, out_resp->prop_id);
4190 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004191 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004192 goto done;
4193 }
Dave Airlief453ba02008-11-07 14:05:41 -08004194
Rob Clark5ea22f22014-05-30 11:34:01 -04004195 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4196 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004197 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004198 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004199 }
4200
4201 value_count = property->num_values;
4202
4203 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4204 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4205 out_resp->flags = property->flags;
4206
4207 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004208 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004209 for (i = 0; i < value_count; i++) {
4210 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4211 ret = -EFAULT;
4212 goto done;
4213 }
4214 }
4215 }
4216 out_resp->count_values = value_count;
4217
Rob Clark5ea22f22014-05-30 11:34:01 -04004218 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4219 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004220 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4221 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004222 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004223 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004224
4225 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4226 ret = -EFAULT;
4227 goto done;
4228 }
4229
4230 if (copy_to_user(&enum_ptr[copied].name,
4231 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4232 ret = -EFAULT;
4233 goto done;
4234 }
4235 copied++;
4236 }
4237 }
4238 out_resp->count_enum_blobs = enum_count;
4239 }
4240
Daniel Vetter3758b342014-11-19 18:38:10 +01004241 /*
4242 * NOTE: The idea seems to have been to use this to read all the blob
4243 * property values. But nothing ever added them to the corresponding
4244 * list, userspace always used the special-purpose get_blob ioctl to
4245 * read the value for a blob property. It also doesn't make a lot of
4246 * sense to return values here when everything else is just metadata for
4247 * the property itself.
4248 */
4249 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4250 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004251done:
Daniel Vetter84849902012-12-02 00:28:11 +01004252 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004253 return ret;
4254}
4255
Daniel Stone99531d92015-05-22 13:34:49 +01004256/**
4257 * drm_property_create_blob - Create new blob property
4258 *
4259 * Creates a new blob property for a specified DRM device, optionally
4260 * copying data.
4261 *
4262 * @dev: DRM device to create property for
4263 * @length: Length to allocate for blob data
4264 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004265 *
4266 * Returns:
4267 * New blob property with a single reference on success, or an ERR_PTR
4268 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004269 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004270struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004271drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004272 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004273{
4274 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004275 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004276
Dan Carpenter9ac09342015-10-29 16:37:54 +03004277 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004278 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004279
4280 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4281 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004282 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004283
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004284 /* This must be explicitly initialised, so we can safely call list_del
4285 * on it in the removal handler, even if it isn't in a file list. */
4286 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004287 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004288 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004289
Daniel Stone99531d92015-05-22 13:34:49 +01004290 if (data)
4291 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004292
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004293 mutex_lock(&dev->mode_config.blob_lock);
4294
4295 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4296 if (ret) {
4297 kfree(blob);
4298 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004299 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004300 }
4301
Daniel Stone6bcacf52015-04-20 19:22:55 +01004302 kref_init(&blob->refcount);
4303
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004304 list_add_tail(&blob->head_global,
4305 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004306
4307 mutex_unlock(&dev->mode_config.blob_lock);
4308
Dave Airlief453ba02008-11-07 14:05:41 -08004309 return blob;
4310}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004311EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004312
Daniel Stone6bcacf52015-04-20 19:22:55 +01004313/**
4314 * drm_property_free_blob - Blob property destructor
4315 *
4316 * Internal free function for blob properties; must not be used directly.
4317 *
Daniel Stonef102c162015-05-22 13:34:44 +01004318 * @kref: Reference
Daniel Stone6bcacf52015-04-20 19:22:55 +01004319 */
4320static void drm_property_free_blob(struct kref *kref)
Dave Airlief453ba02008-11-07 14:05:41 -08004321{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004322 struct drm_property_blob *blob =
4323 container_of(kref, struct drm_property_blob, refcount);
4324
4325 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4326
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004327 list_del(&blob->head_global);
4328 list_del(&blob->head_file);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004329 drm_mode_object_unregister(blob->dev, &blob->base);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004330
Dave Airlief453ba02008-11-07 14:05:41 -08004331 kfree(blob);
4332}
4333
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004334/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004335 * drm_property_unreference_blob - Unreference a blob property
4336 *
4337 * Drop a reference on a blob property. May free the object.
4338 *
Daniel Stonef102c162015-05-22 13:34:44 +01004339 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004340 */
4341void drm_property_unreference_blob(struct drm_property_blob *blob)
4342{
4343 struct drm_device *dev;
4344
4345 if (!blob)
4346 return;
4347
4348 dev = blob->dev;
4349
4350 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4351
4352 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4353 &dev->mode_config.blob_lock))
4354 mutex_unlock(&dev->mode_config.blob_lock);
4355 else
4356 might_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004357}
4358EXPORT_SYMBOL(drm_property_unreference_blob);
4359
4360/**
4361 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4362 *
4363 * Drop a reference on a blob property. May free the object. This must be
4364 * called with blob_lock held.
4365 *
Daniel Stonef102c162015-05-22 13:34:44 +01004366 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004367 */
4368static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4369{
4370 if (!blob)
4371 return;
4372
4373 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4374
4375 kref_put(&blob->refcount, drm_property_free_blob);
4376}
4377
4378/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004379 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4380 * @dev: DRM device
4381 * @file_priv: destroy all blobs owned by this file handle
4382 */
4383void drm_property_destroy_user_blobs(struct drm_device *dev,
4384 struct drm_file *file_priv)
4385{
4386 struct drm_property_blob *blob, *bt;
4387
4388 mutex_lock(&dev->mode_config.blob_lock);
4389
4390 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4391 list_del_init(&blob->head_file);
4392 drm_property_unreference_blob_locked(blob);
4393 }
4394
4395 mutex_unlock(&dev->mode_config.blob_lock);
4396}
4397
4398/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004399 * drm_property_reference_blob - Take a reference on an existing property
4400 *
4401 * Take a new reference on an existing blob property.
4402 *
Daniel Stonef102c162015-05-22 13:34:44 +01004403 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004404 */
4405struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4406{
4407 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4408 kref_get(&blob->refcount);
4409 return blob;
4410}
4411EXPORT_SYMBOL(drm_property_reference_blob);
4412
4413/*
4414 * Like drm_property_lookup_blob, but does not return an additional reference.
4415 * Must be called with blob_lock held.
4416 */
4417static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4418 uint32_t id)
4419{
4420 struct drm_mode_object *obj = NULL;
4421 struct drm_property_blob *blob;
4422
4423 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4424
4425 mutex_lock(&dev->mode_config.idr_mutex);
4426 obj = idr_find(&dev->mode_config.crtc_idr, id);
4427 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4428 blob = NULL;
4429 else
4430 blob = obj_to_blob(obj);
4431 mutex_unlock(&dev->mode_config.idr_mutex);
4432
Dave Airlief453ba02008-11-07 14:05:41 -08004433 return blob;
4434}
4435
Daniel Stone6bcacf52015-04-20 19:22:55 +01004436/**
4437 * drm_property_lookup_blob - look up a blob property and take a reference
4438 * @dev: drm device
4439 * @id: id of the blob property
4440 *
4441 * If successful, this takes an additional reference to the blob property.
4442 * callers need to make sure to eventually unreference the returned property
4443 * again, using @drm_property_unreference_blob.
4444 */
4445struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4446 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004447{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004448 struct drm_property_blob *blob;
4449
4450 mutex_lock(&dev->mode_config.blob_lock);
4451 blob = __drm_property_lookup_blob(dev, id);
4452 if (blob) {
4453 if (!kref_get_unless_zero(&blob->refcount))
4454 blob = NULL;
4455 }
4456 mutex_unlock(&dev->mode_config.blob_lock);
4457
4458 return blob;
4459}
4460EXPORT_SYMBOL(drm_property_lookup_blob);
4461
4462/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004463 * drm_property_replace_global_blob - atomically replace existing blob property
4464 * @dev: drm device
4465 * @replace: location of blob property pointer to be replaced
4466 * @length: length of data for new blob, or 0 for no data
4467 * @data: content for new blob, or NULL for no data
4468 * @obj_holds_id: optional object for property holding blob ID
4469 * @prop_holds_id: optional property holding blob ID
4470 * @return 0 on success or error on failure
4471 *
4472 * This function will atomically replace a global property in the blob list,
4473 * optionally updating a property which holds the ID of that property. It is
4474 * guaranteed to be atomic: no caller will be allowed to see intermediate
4475 * results, and either the entire operation will succeed and clean up the
4476 * previous property, or it will fail and the state will be unchanged.
4477 *
4478 * If length is 0 or data is NULL, no new blob will be created, and the holding
4479 * property, if specified, will be set to 0.
4480 *
4481 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4482 * by holding the relevant modesetting object lock for its parent.
4483 *
4484 * For example, a drm_connector has a 'PATH' property, which contains the ID
4485 * of a blob property with the value of the MST path information. Calling this
4486 * function with replace pointing to the connector's path_blob_ptr, length and
4487 * data set for the new path information, obj_holds_id set to the connector's
4488 * base object, and prop_holds_id set to the path property name, will perform
4489 * a completely atomic update. The access to path_blob_ptr is protected by the
4490 * caller holding a lock on the connector.
4491 */
4492static int drm_property_replace_global_blob(struct drm_device *dev,
4493 struct drm_property_blob **replace,
4494 size_t length,
4495 const void *data,
4496 struct drm_mode_object *obj_holds_id,
4497 struct drm_property *prop_holds_id)
4498{
4499 struct drm_property_blob *new_blob = NULL;
4500 struct drm_property_blob *old_blob = NULL;
4501 int ret;
4502
4503 WARN_ON(replace == NULL);
4504
4505 old_blob = *replace;
4506
4507 if (length && data) {
4508 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004509 if (IS_ERR(new_blob))
4510 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004511 }
4512
4513 /* This does not need to be synchronised with blob_lock, as the
4514 * get_properties ioctl locks all modesetting objects, and
4515 * obj_holds_id must be locked before calling here, so we cannot
4516 * have its value out of sync with the list membership modified
4517 * below under blob_lock. */
4518 if (obj_holds_id) {
4519 ret = drm_object_property_set_value(obj_holds_id,
4520 prop_holds_id,
4521 new_blob ?
4522 new_blob->base.id : 0);
4523 if (ret != 0)
4524 goto err_created;
4525 }
4526
Markus Elfringec530822015-07-05 21:55:10 +02004527 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004528 *replace = new_blob;
4529
4530 return 0;
4531
4532err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004533 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004534 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004535}
4536
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004537/**
4538 * drm_mode_getblob_ioctl - get the contents of a blob property value
4539 * @dev: DRM device
4540 * @data: ioctl data
4541 * @file_priv: DRM file info
4542 *
4543 * This function retrieves the contents of a blob property. The value stored in
4544 * an object's blob property is just a normal modeset object id.
4545 *
4546 * Called by the user via ioctl.
4547 *
4548 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004549 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004550 */
Dave Airlief453ba02008-11-07 14:05:41 -08004551int drm_mode_getblob_ioctl(struct drm_device *dev,
4552 void *data, struct drm_file *file_priv)
4553{
Dave Airlief453ba02008-11-07 14:05:41 -08004554 struct drm_mode_get_blob *out_resp = data;
4555 struct drm_property_blob *blob;
4556 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004557 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004558
Dave Airliefb3b06c2011-02-08 13:55:21 +10004559 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4560 return -EINVAL;
4561
Daniel Vetter84849902012-12-02 00:28:11 +01004562 drm_modeset_lock_all(dev);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004563 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004564 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04004565 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004566 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004567 goto done;
4568 }
Dave Airlief453ba02008-11-07 14:05:41 -08004569
4570 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004571 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004572 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004573 ret = -EFAULT;
4574 goto done;
4575 }
4576 }
4577 out_resp->length = blob->length;
4578
4579done:
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004580 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01004581 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004582 return ret;
4583}
4584
Dave Airliecc7096f2014-10-22 12:03:04 +10004585/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004586 * drm_mode_createblob_ioctl - create a new blob property
4587 * @dev: DRM device
4588 * @data: ioctl data
4589 * @file_priv: DRM file info
4590 *
4591 * This function creates a new blob property with user-defined values. In order
4592 * to give us sensible validation and checking when creating, rather than at
4593 * every potential use, we also require a type to be provided upfront.
4594 *
4595 * Called by the user via ioctl.
4596 *
4597 * Returns:
4598 * Zero on success, negative errno on failure.
4599 */
4600int drm_mode_createblob_ioctl(struct drm_device *dev,
4601 void *data, struct drm_file *file_priv)
4602{
4603 struct drm_mode_create_blob *out_resp = data;
4604 struct drm_property_blob *blob;
4605 void __user *blob_ptr;
4606 int ret = 0;
4607
4608 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4609 return -EINVAL;
4610
4611 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4612 if (IS_ERR(blob))
4613 return PTR_ERR(blob);
4614
4615 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4616 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4617 ret = -EFAULT;
4618 goto out_blob;
4619 }
4620
4621 /* Dropping the lock between create_blob and our access here is safe
4622 * as only the same file_priv can remove the blob; at this point, it is
4623 * not associated with any file_priv. */
4624 mutex_lock(&dev->mode_config.blob_lock);
4625 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004626 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004627 mutex_unlock(&dev->mode_config.blob_lock);
4628
4629 return 0;
4630
4631out_blob:
4632 drm_property_unreference_blob(blob);
4633 return ret;
4634}
4635
4636/**
4637 * drm_mode_destroyblob_ioctl - destroy a user blob property
4638 * @dev: DRM device
4639 * @data: ioctl data
4640 * @file_priv: DRM file info
4641 *
4642 * Destroy an existing user-defined blob property.
4643 *
4644 * Called by the user via ioctl.
4645 *
4646 * Returns:
4647 * Zero on success, negative errno on failure.
4648 */
4649int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4650 void *data, struct drm_file *file_priv)
4651{
4652 struct drm_mode_destroy_blob *out_resp = data;
4653 struct drm_property_blob *blob = NULL, *bt;
4654 bool found = false;
4655 int ret = 0;
4656
4657 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4658 return -EINVAL;
4659
4660 mutex_lock(&dev->mode_config.blob_lock);
4661 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4662 if (!blob) {
4663 ret = -ENOENT;
4664 goto err;
4665 }
4666
4667 /* Ensure the property was actually created by this user. */
4668 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4669 if (bt == blob) {
4670 found = true;
4671 break;
4672 }
4673 }
4674
4675 if (!found) {
4676 ret = -EPERM;
4677 goto err;
4678 }
4679
4680 /* We must drop head_file here, because we may not be the last
4681 * reference on the blob. */
4682 list_del_init(&blob->head_file);
4683 drm_property_unreference_blob_locked(blob);
4684 mutex_unlock(&dev->mode_config.blob_lock);
4685
4686 return 0;
4687
4688err:
4689 mutex_unlock(&dev->mode_config.blob_lock);
4690 return ret;
4691}
4692
4693/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004694 * drm_mode_connector_set_path_property - set tile property on connector
4695 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004696 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004697 *
4698 * This creates a property to expose to userspace to specify a
4699 * connector path. This is mainly used for DisplayPort MST where
4700 * connectors have a topology and we want to allow userspace to give
4701 * them more meaningful names.
4702 *
4703 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004704 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004705 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004706int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004707 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004708{
4709 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004710 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004711
Daniel Stoned2ed3432015-04-20 19:22:53 +01004712 ret = drm_property_replace_global_blob(dev,
4713 &connector->path_blob_ptr,
4714 strlen(path) + 1,
4715 path,
4716 &connector->base,
4717 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004718 return ret;
4719}
4720EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4721
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004722/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004723 * drm_mode_connector_set_tile_property - set tile property on connector
4724 * @connector: connector to set property on.
4725 *
4726 * This looks up the tile information for a connector, and creates a
4727 * property for userspace to parse if it exists. The property is of
4728 * the form of 8 integers using ':' as a separator.
4729 *
4730 * Returns:
4731 * Zero on success, errno on failure.
4732 */
4733int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4734{
4735 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004736 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004737 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004738
4739 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004740 ret = drm_property_replace_global_blob(dev,
4741 &connector->tile_blob_ptr,
4742 0,
4743 NULL,
4744 &connector->base,
4745 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004746 return ret;
4747 }
4748
4749 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4750 connector->tile_group->id, connector->tile_is_single_monitor,
4751 connector->num_h_tile, connector->num_v_tile,
4752 connector->tile_h_loc, connector->tile_v_loc,
4753 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004754
Daniel Stoned2ed3432015-04-20 19:22:53 +01004755 ret = drm_property_replace_global_blob(dev,
4756 &connector->tile_blob_ptr,
4757 strlen(tile) + 1,
4758 tile,
4759 &connector->base,
4760 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004761 return ret;
4762}
4763EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4764
4765/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004766 * drm_mode_connector_update_edid_property - update the edid property of a connector
4767 * @connector: drm connector
4768 * @edid: new value of the edid property
4769 *
4770 * This function creates a new blob modeset object and assigns its id to the
4771 * connector's edid property.
4772 *
4773 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004774 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004775 */
Dave Airlief453ba02008-11-07 14:05:41 -08004776int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004777 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004778{
4779 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004780 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004781 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004782
Thomas Wood4cf2b282014-06-18 17:52:33 +01004783 /* ignore requests to set edid when overridden */
4784 if (connector->override_edid)
4785 return 0;
4786
Daniel Stoned2ed3432015-04-20 19:22:53 +01004787 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004788 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004789
Daniel Stoned2ed3432015-04-20 19:22:53 +01004790 ret = drm_property_replace_global_blob(dev,
4791 &connector->edid_blob_ptr,
4792 size,
4793 edid,
4794 &connector->base,
4795 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004796 return ret;
4797}
4798EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4799
Rob Clark3843e712014-12-16 18:05:29 -05004800/* Some properties could refer to dynamic refcnt'd objects, or things that
4801 * need special locking to handle lifetime issues (ie. to ensure the prop
4802 * value doesn't become invalid part way through the property update due to
4803 * race). The value returned by reference via 'obj' should be passed back
4804 * to drm_property_change_valid_put() after the property is set (and the
4805 * object to which the property is attached has a chance to take it's own
4806 * reference).
4807 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004808bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004809 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004810{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004811 int i;
4812
Paulo Zanoni26a34812012-05-15 18:08:59 -03004813 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4814 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004815
Rob Clark3843e712014-12-16 18:05:29 -05004816 *ref = NULL;
4817
Rob Clark5ea22f22014-05-30 11:34:01 -04004818 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004819 if (value < property->values[0] || value > property->values[1])
4820 return false;
4821 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004822 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4823 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004824
Rob Clarkebc44cf2012-09-12 22:22:31 -05004825 if (svalue < U642I64(property->values[0]) ||
4826 svalue > U642I64(property->values[1]))
4827 return false;
4828 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004829 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004830 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004831
Rob Clark49e27542012-05-17 02:23:26 -06004832 for (i = 0; i < property->num_values; i++)
4833 valid_mask |= (1ULL << property->values[i]);
4834 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004835 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004836 struct drm_property_blob *blob;
4837
4838 if (value == 0)
4839 return true;
4840
4841 blob = drm_property_lookup_blob(property->dev, value);
4842 if (blob) {
4843 *ref = &blob->base;
4844 return true;
4845 } else {
4846 return false;
4847 }
Rob Clark98f75de2014-05-30 11:37:03 -04004848 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004849 /* a zero value for an object property translates to null: */
4850 if (value == 0)
4851 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004852
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004853 return _object_find(property->dev, value, property->values[0]) != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004854 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004855
4856 for (i = 0; i < property->num_values; i++)
4857 if (property->values[i] == value)
4858 return true;
4859 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004860}
4861
Rob Clarkd34f20d2014-12-18 16:01:56 -05004862void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004863 struct drm_mode_object *ref)
4864{
4865 if (!ref)
4866 return;
4867
4868 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004869 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004870 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4871 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004872}
4873
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004874/**
4875 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4876 * @dev: DRM device
4877 * @data: ioctl data
4878 * @file_priv: DRM file info
4879 *
4880 * This function sets the current value for a connectors's property. It also
4881 * calls into a driver's ->set_property callback to update the hardware state
4882 *
4883 * Called by the user via ioctl.
4884 *
4885 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004886 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004887 */
Dave Airlief453ba02008-11-07 14:05:41 -08004888int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4889 void *data, struct drm_file *file_priv)
4890{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004891 struct drm_mode_connector_set_property *conn_set_prop = data;
4892 struct drm_mode_obj_set_property obj_set_prop = {
4893 .value = conn_set_prop->value,
4894 .prop_id = conn_set_prop->prop_id,
4895 .obj_id = conn_set_prop->connector_id,
4896 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4897 };
Dave Airlief453ba02008-11-07 14:05:41 -08004898
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004899 /* It does all the locking and checking we need */
4900 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004901}
4902
Paulo Zanonic5431882012-05-15 18:09:02 -03004903static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4904 struct drm_property *property,
4905 uint64_t value)
4906{
4907 int ret = -EINVAL;
4908 struct drm_connector *connector = obj_to_connector(obj);
4909
4910 /* Do DPMS ourselves */
4911 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004912 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004913 } else if (connector->funcs->set_property)
4914 ret = connector->funcs->set_property(connector, property, value);
4915
4916 /* store the property value if successful */
4917 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004918 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004919 return ret;
4920}
4921
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004922static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4923 struct drm_property *property,
4924 uint64_t value)
4925{
4926 int ret = -EINVAL;
4927 struct drm_crtc *crtc = obj_to_crtc(obj);
4928
4929 if (crtc->funcs->set_property)
4930 ret = crtc->funcs->set_property(crtc, property, value);
4931 if (!ret)
4932 drm_object_property_set_value(obj, property, value);
4933
4934 return ret;
4935}
4936
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004937/**
4938 * drm_mode_plane_set_obj_prop - set the value of a property
4939 * @plane: drm plane object to set property value for
4940 * @property: property to set
4941 * @value: value the property should be set to
4942 *
4943 * This functions sets a given property on a given plane object. This function
4944 * calls the driver's ->set_property callback and changes the software state of
4945 * the property if the callback succeeds.
4946 *
4947 * Returns:
4948 * Zero on success, error code on failure.
4949 */
4950int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4951 struct drm_property *property,
4952 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004953{
4954 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004955 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004956
4957 if (plane->funcs->set_property)
4958 ret = plane->funcs->set_property(plane, property, value);
4959 if (!ret)
4960 drm_object_property_set_value(obj, property, value);
4961
4962 return ret;
4963}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004964EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004965
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004966/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004967 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004968 * @dev: DRM device
4969 * @data: ioctl data
4970 * @file_priv: DRM file info
4971 *
4972 * This function retrieves the current value for an object's property. Compared
4973 * to the connector specific ioctl this one is extended to also work on crtc and
4974 * plane objects.
4975 *
4976 * Called by the user via ioctl.
4977 *
4978 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004979 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004980 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004981int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4982 struct drm_file *file_priv)
4983{
4984 struct drm_mode_obj_get_properties *arg = data;
4985 struct drm_mode_object *obj;
4986 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004987
4988 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4989 return -EINVAL;
4990
Daniel Vetter84849902012-12-02 00:28:11 +01004991 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004992
4993 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4994 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004995 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004996 goto out;
4997 }
4998 if (!obj->properties) {
4999 ret = -EINVAL;
5000 goto out;
5001 }
5002
Rob Clark88a48e22014-12-18 16:01:50 -05005003 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005004 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5005 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5006 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005007
Paulo Zanonic5431882012-05-15 18:09:02 -03005008out:
Daniel Vetter84849902012-12-02 00:28:11 +01005009 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005010 return ret;
5011}
5012
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005013/**
5014 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5015 * @dev: DRM device
5016 * @data: ioctl data
5017 * @file_priv: DRM file info
5018 *
5019 * This function sets the current value for an object's property. It also calls
5020 * into a driver's ->set_property callback to update the hardware state.
5021 * Compared to the connector specific ioctl this one is extended to also work on
5022 * crtc and plane objects.
5023 *
5024 * Called by the user via ioctl.
5025 *
5026 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005027 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005028 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005029int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5030 struct drm_file *file_priv)
5031{
5032 struct drm_mode_obj_set_property *arg = data;
5033 struct drm_mode_object *arg_obj;
5034 struct drm_mode_object *prop_obj;
5035 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005036 int i, ret = -EINVAL;
5037 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005038
5039 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5040 return -EINVAL;
5041
Daniel Vetter84849902012-12-02 00:28:11 +01005042 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005043
5044 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005045 if (!arg_obj) {
5046 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005047 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005048 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005049 if (!arg_obj->properties)
5050 goto out;
5051
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005052 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005053 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005054 break;
5055
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005056 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03005057 goto out;
5058
5059 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5060 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005061 if (!prop_obj) {
5062 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005063 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005064 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005065 property = obj_to_property(prop_obj);
5066
Rob Clark3843e712014-12-16 18:05:29 -05005067 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03005068 goto out;
5069
5070 switch (arg_obj->type) {
5071 case DRM_MODE_OBJECT_CONNECTOR:
5072 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5073 arg->value);
5074 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005075 case DRM_MODE_OBJECT_CRTC:
5076 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5077 break;
Rob Clark4d939142012-05-17 02:23:27 -06005078 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005079 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5080 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005081 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005082 }
5083
Rob Clark3843e712014-12-16 18:05:29 -05005084 drm_property_change_valid_put(property, ref);
5085
Paulo Zanonic5431882012-05-15 18:09:02 -03005086out:
Daniel Vetter84849902012-12-02 00:28:11 +01005087 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005088 return ret;
5089}
5090
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005091/**
5092 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5093 * @connector: connector to attach
5094 * @encoder: encoder to attach @connector to
5095 *
5096 * This function links up a connector to an encoder. Note that the routing
5097 * restrictions between encoders and crtcs are exposed to userspace through the
5098 * possible_clones and possible_crtcs bitmasks.
5099 *
5100 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005101 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005102 */
Dave Airlief453ba02008-11-07 14:05:41 -08005103int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5104 struct drm_encoder *encoder)
5105{
5106 int i;
5107
Thierry Redingeb47fe82015-11-16 18:19:53 +01005108 /*
5109 * In the past, drivers have attempted to model the static association
5110 * of connector to encoder in simple connector/encoder devices using a
5111 * direct assignment of connector->encoder = encoder. This connection
5112 * is a logical one and the responsibility of the core, so drivers are
5113 * expected not to mess with this.
5114 *
5115 * Note that the error return should've been enough here, but a large
5116 * majority of drivers ignores the return value, so add in a big WARN
5117 * to get people's attention.
5118 */
5119 if (WARN_ON(connector->encoder))
5120 return -EINVAL;
5121
Dave Airlief453ba02008-11-07 14:05:41 -08005122 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5123 if (connector->encoder_ids[i] == 0) {
5124 connector->encoder_ids[i] = encoder->base.id;
5125 return 0;
5126 }
5127 }
5128 return -ENOMEM;
5129}
5130EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5131
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005132/**
5133 * drm_mode_crtc_set_gamma_size - set the gamma table size
5134 * @crtc: CRTC to set the gamma table size for
5135 * @gamma_size: size of the gamma table
5136 *
5137 * Drivers which support gamma tables should set this to the supported gamma
5138 * table size when initializing the CRTC. Currently the drm core only supports a
5139 * fixed gamma table size.
5140 *
5141 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005142 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005143 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005144int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005145 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005146{
5147 crtc->gamma_size = gamma_size;
5148
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005149 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5150 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005151 if (!crtc->gamma_store) {
5152 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005153 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005154 }
5155
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005156 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005157}
5158EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5159
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005160/**
5161 * drm_mode_gamma_set_ioctl - set the gamma table
5162 * @dev: DRM device
5163 * @data: ioctl data
5164 * @file_priv: DRM file info
5165 *
5166 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5167 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5168 *
5169 * Called by the user via ioctl.
5170 *
5171 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005172 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005173 */
Dave Airlief453ba02008-11-07 14:05:41 -08005174int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5175 void *data, struct drm_file *file_priv)
5176{
5177 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005178 struct drm_crtc *crtc;
5179 void *r_base, *g_base, *b_base;
5180 int size;
5181 int ret = 0;
5182
Dave Airliefb3b06c2011-02-08 13:55:21 +10005183 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5184 return -EINVAL;
5185
Daniel Vetter84849902012-12-02 00:28:11 +01005186 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005187 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5188 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005189 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005190 goto out;
5191 }
Dave Airlief453ba02008-11-07 14:05:41 -08005192
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005193 if (crtc->funcs->gamma_set == NULL) {
5194 ret = -ENOSYS;
5195 goto out;
5196 }
5197
Dave Airlief453ba02008-11-07 14:05:41 -08005198 /* memcpy into gamma store */
5199 if (crtc_lut->gamma_size != crtc->gamma_size) {
5200 ret = -EINVAL;
5201 goto out;
5202 }
5203
5204 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5205 r_base = crtc->gamma_store;
5206 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5207 ret = -EFAULT;
5208 goto out;
5209 }
5210
5211 g_base = r_base + size;
5212 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5213 ret = -EFAULT;
5214 goto out;
5215 }
5216
5217 b_base = g_base + size;
5218 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5219 ret = -EFAULT;
5220 goto out;
5221 }
5222
James Simmons72034252010-08-03 01:33:19 +01005223 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005224
5225out:
Daniel Vetter84849902012-12-02 00:28:11 +01005226 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005227 return ret;
5228
5229}
5230
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005231/**
5232 * drm_mode_gamma_get_ioctl - get the gamma table
5233 * @dev: DRM device
5234 * @data: ioctl data
5235 * @file_priv: DRM file info
5236 *
5237 * Copy the current gamma table into the storage provided. This also provides
5238 * the gamma table size the driver expects, which can be used to size the
5239 * allocated storage.
5240 *
5241 * Called by the user via ioctl.
5242 *
5243 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005244 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005245 */
Dave Airlief453ba02008-11-07 14:05:41 -08005246int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5247 void *data, struct drm_file *file_priv)
5248{
5249 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005250 struct drm_crtc *crtc;
5251 void *r_base, *g_base, *b_base;
5252 int size;
5253 int ret = 0;
5254
Dave Airliefb3b06c2011-02-08 13:55:21 +10005255 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5256 return -EINVAL;
5257
Daniel Vetter84849902012-12-02 00:28:11 +01005258 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005259 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5260 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005261 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005262 goto out;
5263 }
Dave Airlief453ba02008-11-07 14:05:41 -08005264
5265 /* memcpy into gamma store */
5266 if (crtc_lut->gamma_size != crtc->gamma_size) {
5267 ret = -EINVAL;
5268 goto out;
5269 }
5270
5271 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5272 r_base = crtc->gamma_store;
5273 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5274 ret = -EFAULT;
5275 goto out;
5276 }
5277
5278 g_base = r_base + size;
5279 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5280 ret = -EFAULT;
5281 goto out;
5282 }
5283
5284 b_base = g_base + size;
5285 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5286 ret = -EFAULT;
5287 goto out;
5288 }
5289out:
Daniel Vetter84849902012-12-02 00:28:11 +01005290 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005291 return ret;
5292}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005293
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005294/**
5295 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5296 * @dev: DRM device
5297 * @data: ioctl data
5298 * @file_priv: DRM file info
5299 *
5300 * This schedules an asynchronous update on a given CRTC, called page flip.
5301 * Optionally a drm event is generated to signal the completion of the event.
5302 * Generic drivers cannot assume that a pageflip with changed framebuffer
5303 * properties (including driver specific metadata like tiling layout) will work,
5304 * but some drivers support e.g. pixel format changes through the pageflip
5305 * ioctl.
5306 *
5307 * Called by the user via ioctl.
5308 *
5309 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005310 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005311 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005312int drm_mode_page_flip_ioctl(struct drm_device *dev,
5313 void *data, struct drm_file *file_priv)
5314{
5315 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005316 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005317 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005318 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005319 int ret = -EINVAL;
5320
5321 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5322 page_flip->reserved != 0)
5323 return -EINVAL;
5324
Keith Packard62f21042013-07-22 18:50:00 -07005325 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5326 return -EINVAL;
5327
Rob Clarka2b34e22013-10-05 16:36:52 -04005328 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5329 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005330 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005331
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005332 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005333 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005334 /* The framebuffer is currently unbound, presumably
5335 * due to a hotplug event, that userspace has not
5336 * yet discovered.
5337 */
5338 ret = -EBUSY;
5339 goto out;
5340 }
5341
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005342 if (crtc->funcs->page_flip == NULL)
5343 goto out;
5344
Daniel Vetter786b99e2012-12-02 21:53:40 +01005345 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005346 if (!fb) {
5347 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005348 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005349 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005350
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005351 if (crtc->state) {
5352 const struct drm_plane_state *state = crtc->primary->state;
5353
5354 ret = check_src_coords(state->src_x, state->src_y,
5355 state->src_w, state->src_h, fb);
5356 } else {
5357 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5358 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005359 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005360 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005361
Matt Roperf4510a22014-04-01 15:22:40 -07005362 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005363 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5364 ret = -EINVAL;
5365 goto out;
5366 }
5367
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005368 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005369 e = kzalloc(sizeof *e, GFP_KERNEL);
5370 if (!e) {
5371 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005372 goto out;
5373 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005374 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005375 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005376 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005377 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5378 if (ret) {
5379 kfree(e);
5380 goto out;
5381 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005382 }
5383
Daniel Vetter3d30a592014-07-27 13:42:42 +02005384 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005385 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005386 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005387 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5388 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005389 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005390 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005391 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005392 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005393 /* Unref only the old framebuffer. */
5394 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005395 }
5396
5397out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005398 if (fb)
5399 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005400 if (crtc->primary->old_fb)
5401 drm_framebuffer_unreference(crtc->primary->old_fb);
5402 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005403 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005404
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005405 return ret;
5406}
Chris Wilsoneb033552011-01-24 15:11:08 +00005407
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005408/**
5409 * drm_mode_config_reset - call ->reset callbacks
5410 * @dev: drm device
5411 *
5412 * This functions calls all the crtc's, encoder's and connector's ->reset
5413 * callback. Drivers can use this in e.g. their driver load or resume code to
5414 * reset hardware and software state.
5415 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005416void drm_mode_config_reset(struct drm_device *dev)
5417{
5418 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005419 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005420 struct drm_encoder *encoder;
5421 struct drm_connector *connector;
5422
Daniel Vettere4f62542015-07-09 23:44:35 +02005423 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005424 if (plane->funcs->reset)
5425 plane->funcs->reset(plane);
5426
Daniel Vettere4f62542015-07-09 23:44:35 +02005427 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005428 if (crtc->funcs->reset)
5429 crtc->funcs->reset(crtc);
5430
Daniel Vettere4f62542015-07-09 23:44:35 +02005431 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005432 if (encoder->funcs->reset)
5433 encoder->funcs->reset(encoder);
5434
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005435 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005436 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005437 if (connector->funcs->reset)
5438 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005439 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005440}
5441EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005442
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005443/**
5444 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5445 * @dev: DRM device
5446 * @data: ioctl data
5447 * @file_priv: DRM file info
5448 *
5449 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5450 * TTM or something else entirely) and returns the resulting buffer handle. This
5451 * handle can then be wrapped up into a framebuffer modeset object.
5452 *
5453 * Note that userspace is not allowed to use such objects for render
5454 * acceleration - drivers must create their own private ioctls for such a use
5455 * case.
5456 *
5457 * Called by the user via ioctl.
5458 *
5459 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005460 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005461 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005462int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5463 void *data, struct drm_file *file_priv)
5464{
5465 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005466 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005467
5468 if (!dev->driver->dumb_create)
5469 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005470 if (!args->width || !args->height || !args->bpp)
5471 return -EINVAL;
5472
5473 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005474 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005475 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005476 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005477 return -EINVAL;
5478 stride = cpp * args->width;
5479 if (args->height > 0xffffffffU / stride)
5480 return -EINVAL;
5481
5482 /* test for wrap-around */
5483 size = args->height * stride;
5484 if (PAGE_ALIGN(size) == 0)
5485 return -EINVAL;
5486
Thierry Redingf6085952014-11-03 11:14:14 +01005487 /*
5488 * handle, pitch and size are output parameters. Zero them out to
5489 * prevent drivers from accidentally using uninitialized data. Since
5490 * not all existing userspace is clearing these fields properly we
5491 * cannot reject IOCTL with garbage in them.
5492 */
5493 args->handle = 0;
5494 args->pitch = 0;
5495 args->size = 0;
5496
Dave Airlieff72145b2011-02-07 12:16:14 +10005497 return dev->driver->dumb_create(file_priv, dev, args);
5498}
5499
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005500/**
5501 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5502 * @dev: DRM device
5503 * @data: ioctl data
5504 * @file_priv: DRM file info
5505 *
5506 * Allocate an offset in the drm device node's address space to be able to
5507 * memory map a dumb buffer.
5508 *
5509 * Called by the user via ioctl.
5510 *
5511 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005512 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005513 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005514int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5515 void *data, struct drm_file *file_priv)
5516{
5517 struct drm_mode_map_dumb *args = data;
5518
5519 /* call driver ioctl to get mmap offset */
5520 if (!dev->driver->dumb_map_offset)
5521 return -ENOSYS;
5522
5523 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5524}
5525
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005526/**
5527 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5528 * @dev: DRM device
5529 * @data: ioctl data
5530 * @file_priv: DRM file info
5531 *
5532 * This destroys the userspace handle for the given dumb backing storage buffer.
5533 * Since buffer objects must be reference counted in the kernel a buffer object
5534 * won't be immediately freed if a framebuffer modeset object still uses it.
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_destroy_dumb_ioctl(struct drm_device *dev,
5542 void *data, struct drm_file *file_priv)
5543{
5544 struct drm_mode_destroy_dumb *args = data;
5545
5546 if (!dev->driver->dumb_destroy)
5547 return -ENOSYS;
5548
5549 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5550}
Dave Airlie248dbc22011-11-29 20:02:54 +00005551
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005552/**
5553 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5554 * @format: pixel format (DRM_FORMAT_*)
5555 * @depth: storage for the depth value
5556 * @bpp: storage for the bpp value
5557 *
5558 * This only supports RGB formats here for compat with code that doesn't use
5559 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005560 */
5561void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5562 int *bpp)
5563{
5564 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005565 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005566 case DRM_FORMAT_RGB332:
5567 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005568 *depth = 8;
5569 *bpp = 8;
5570 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005571 case DRM_FORMAT_XRGB1555:
5572 case DRM_FORMAT_XBGR1555:
5573 case DRM_FORMAT_RGBX5551:
5574 case DRM_FORMAT_BGRX5551:
5575 case DRM_FORMAT_ARGB1555:
5576 case DRM_FORMAT_ABGR1555:
5577 case DRM_FORMAT_RGBA5551:
5578 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005579 *depth = 15;
5580 *bpp = 16;
5581 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005582 case DRM_FORMAT_RGB565:
5583 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005584 *depth = 16;
5585 *bpp = 16;
5586 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005587 case DRM_FORMAT_RGB888:
5588 case DRM_FORMAT_BGR888:
5589 *depth = 24;
5590 *bpp = 24;
5591 break;
5592 case DRM_FORMAT_XRGB8888:
5593 case DRM_FORMAT_XBGR8888:
5594 case DRM_FORMAT_RGBX8888:
5595 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005596 *depth = 24;
5597 *bpp = 32;
5598 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005599 case DRM_FORMAT_XRGB2101010:
5600 case DRM_FORMAT_XBGR2101010:
5601 case DRM_FORMAT_RGBX1010102:
5602 case DRM_FORMAT_BGRX1010102:
5603 case DRM_FORMAT_ARGB2101010:
5604 case DRM_FORMAT_ABGR2101010:
5605 case DRM_FORMAT_RGBA1010102:
5606 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005607 *depth = 30;
5608 *bpp = 32;
5609 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005610 case DRM_FORMAT_ARGB8888:
5611 case DRM_FORMAT_ABGR8888:
5612 case DRM_FORMAT_RGBA8888:
5613 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005614 *depth = 32;
5615 *bpp = 32;
5616 break;
5617 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005618 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5619 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005620 *depth = 0;
5621 *bpp = 0;
5622 break;
5623 }
5624}
5625EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005626
5627/**
5628 * drm_format_num_planes - get the number of planes for format
5629 * @format: pixel format (DRM_FORMAT_*)
5630 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005631 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005632 * The number of planes used by the specified pixel format.
5633 */
5634int drm_format_num_planes(uint32_t format)
5635{
5636 switch (format) {
5637 case DRM_FORMAT_YUV410:
5638 case DRM_FORMAT_YVU410:
5639 case DRM_FORMAT_YUV411:
5640 case DRM_FORMAT_YVU411:
5641 case DRM_FORMAT_YUV420:
5642 case DRM_FORMAT_YVU420:
5643 case DRM_FORMAT_YUV422:
5644 case DRM_FORMAT_YVU422:
5645 case DRM_FORMAT_YUV444:
5646 case DRM_FORMAT_YVU444:
5647 return 3;
5648 case DRM_FORMAT_NV12:
5649 case DRM_FORMAT_NV21:
5650 case DRM_FORMAT_NV16:
5651 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005652 case DRM_FORMAT_NV24:
5653 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005654 return 2;
5655 default:
5656 return 1;
5657 }
5658}
5659EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005660
5661/**
5662 * drm_format_plane_cpp - determine the bytes per pixel value
5663 * @format: pixel format (DRM_FORMAT_*)
5664 * @plane: plane index
5665 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005666 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005667 * The bytes per pixel value for the specified plane.
5668 */
5669int drm_format_plane_cpp(uint32_t format, int plane)
5670{
5671 unsigned int depth;
5672 int bpp;
5673
5674 if (plane >= drm_format_num_planes(format))
5675 return 0;
5676
5677 switch (format) {
5678 case DRM_FORMAT_YUYV:
5679 case DRM_FORMAT_YVYU:
5680 case DRM_FORMAT_UYVY:
5681 case DRM_FORMAT_VYUY:
5682 return 2;
5683 case DRM_FORMAT_NV12:
5684 case DRM_FORMAT_NV21:
5685 case DRM_FORMAT_NV16:
5686 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005687 case DRM_FORMAT_NV24:
5688 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005689 return plane ? 2 : 1;
5690 case DRM_FORMAT_YUV410:
5691 case DRM_FORMAT_YVU410:
5692 case DRM_FORMAT_YUV411:
5693 case DRM_FORMAT_YVU411:
5694 case DRM_FORMAT_YUV420:
5695 case DRM_FORMAT_YVU420:
5696 case DRM_FORMAT_YUV422:
5697 case DRM_FORMAT_YVU422:
5698 case DRM_FORMAT_YUV444:
5699 case DRM_FORMAT_YVU444:
5700 return 1;
5701 default:
5702 drm_fb_get_bpp_depth(format, &depth, &bpp);
5703 return bpp >> 3;
5704 }
5705}
5706EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005707
5708/**
5709 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5710 * @format: pixel format (DRM_FORMAT_*)
5711 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005712 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005713 * The horizontal chroma subsampling factor for the
5714 * specified pixel format.
5715 */
5716int drm_format_horz_chroma_subsampling(uint32_t format)
5717{
5718 switch (format) {
5719 case DRM_FORMAT_YUV411:
5720 case DRM_FORMAT_YVU411:
5721 case DRM_FORMAT_YUV410:
5722 case DRM_FORMAT_YVU410:
5723 return 4;
5724 case DRM_FORMAT_YUYV:
5725 case DRM_FORMAT_YVYU:
5726 case DRM_FORMAT_UYVY:
5727 case DRM_FORMAT_VYUY:
5728 case DRM_FORMAT_NV12:
5729 case DRM_FORMAT_NV21:
5730 case DRM_FORMAT_NV16:
5731 case DRM_FORMAT_NV61:
5732 case DRM_FORMAT_YUV422:
5733 case DRM_FORMAT_YVU422:
5734 case DRM_FORMAT_YUV420:
5735 case DRM_FORMAT_YVU420:
5736 return 2;
5737 default:
5738 return 1;
5739 }
5740}
5741EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5742
5743/**
5744 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5745 * @format: pixel format (DRM_FORMAT_*)
5746 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005747 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005748 * The vertical chroma subsampling factor for the
5749 * specified pixel format.
5750 */
5751int drm_format_vert_chroma_subsampling(uint32_t format)
5752{
5753 switch (format) {
5754 case DRM_FORMAT_YUV410:
5755 case DRM_FORMAT_YVU410:
5756 return 4;
5757 case DRM_FORMAT_YUV420:
5758 case DRM_FORMAT_YVU420:
5759 case DRM_FORMAT_NV12:
5760 case DRM_FORMAT_NV21:
5761 return 2;
5762 default:
5763 return 1;
5764 }
5765}
5766EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005767
5768/**
Ville Syrjälä4c617162016-02-09 17:29:44 +02005769 * drm_format_plane_width - width of the plane given the first plane
5770 * @width: width of the first plane
5771 * @format: pixel format
5772 * @plane: plane index
5773 *
5774 * Returns:
5775 * The width of @plane, given that the width of the first plane is @width.
5776 */
5777int drm_format_plane_width(int width, uint32_t format, int plane)
5778{
5779 if (plane >= drm_format_num_planes(format))
5780 return 0;
5781
5782 if (plane == 0)
5783 return width;
5784
5785 return width / drm_format_horz_chroma_subsampling(format);
5786}
5787EXPORT_SYMBOL(drm_format_plane_width);
5788
5789/**
5790 * drm_format_plane_height - height of the plane given the first plane
5791 * @height: height of the first plane
5792 * @format: pixel format
5793 * @plane: plane index
5794 *
5795 * Returns:
5796 * The height of @plane, given that the height of the first plane is @height.
5797 */
5798int drm_format_plane_height(int height, uint32_t format, int plane)
5799{
5800 if (plane >= drm_format_num_planes(format))
5801 return 0;
5802
5803 if (plane == 0)
5804 return height;
5805
5806 return height / drm_format_vert_chroma_subsampling(format);
5807}
5808EXPORT_SYMBOL(drm_format_plane_height);
5809
5810/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305811 * drm_rotation_simplify() - Try to simplify the rotation
5812 * @rotation: Rotation to be simplified
5813 * @supported_rotations: Supported rotations
5814 *
5815 * Attempt to simplify the rotation to a form that is supported.
5816 * Eg. if the hardware supports everything except DRM_REFLECT_X
5817 * one could call this function like this:
5818 *
5819 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5820 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5821 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5822 *
5823 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5824 * transforms the hardware supports, this function may not
5825 * be able to produce a supported transform, so the caller should
5826 * check the result afterwards.
5827 */
5828unsigned int drm_rotation_simplify(unsigned int rotation,
5829 unsigned int supported_rotations)
5830{
5831 if (rotation & ~supported_rotations) {
5832 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005833 rotation = (rotation & DRM_REFLECT_MASK) |
5834 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305835 }
5836
5837 return rotation;
5838}
5839EXPORT_SYMBOL(drm_rotation_simplify);
5840
5841/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005842 * drm_mode_config_init - initialize DRM mode_configuration structure
5843 * @dev: DRM device
5844 *
5845 * Initialize @dev's mode_config structure, used for tracking the graphics
5846 * configuration of @dev.
5847 *
5848 * Since this initializes the modeset locks, no locking is possible. Which is no
5849 * problem, since this should happen single threaded at init time. It is the
5850 * driver's problem to ensure this guarantee.
5851 *
5852 */
5853void drm_mode_config_init(struct drm_device *dev)
5854{
5855 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005856 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005857 mutex_init(&dev->mode_config.idr_mutex);
5858 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005859 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005860 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5861 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5862 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5863 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5864 INIT_LIST_HEAD(&dev->mode_config.property_list);
5865 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5866 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5867 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005868 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005869 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005870
5871 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005872 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005873 drm_modeset_unlock_all(dev);
5874
5875 /* Just to be sure */
5876 dev->mode_config.num_fb = 0;
5877 dev->mode_config.num_connector = 0;
5878 dev->mode_config.num_crtc = 0;
5879 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005880 dev->mode_config.num_overlay_plane = 0;
5881 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005882}
5883EXPORT_SYMBOL(drm_mode_config_init);
5884
5885/**
5886 * drm_mode_config_cleanup - free up DRM mode_config info
5887 * @dev: DRM device
5888 *
5889 * Free up all the connectors and CRTCs associated with this DRM device, then
5890 * free up the framebuffers and associated buffer objects.
5891 *
5892 * Note that since this /should/ happen single-threaded at driver/device
5893 * teardown time, no locking is required. It's the driver's job to ensure that
5894 * this guarantee actually holds true.
5895 *
5896 * FIXME: cleanup any dangling user buffer objects too
5897 */
5898void drm_mode_config_cleanup(struct drm_device *dev)
5899{
5900 struct drm_connector *connector, *ot;
5901 struct drm_crtc *crtc, *ct;
5902 struct drm_encoder *encoder, *enct;
5903 struct drm_framebuffer *fb, *fbt;
5904 struct drm_property *property, *pt;
5905 struct drm_property_blob *blob, *bt;
5906 struct drm_plane *plane, *plt;
5907
5908 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5909 head) {
5910 encoder->funcs->destroy(encoder);
5911 }
5912
5913 list_for_each_entry_safe(connector, ot,
5914 &dev->mode_config.connector_list, head) {
5915 connector->funcs->destroy(connector);
5916 }
5917
5918 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5919 head) {
5920 drm_property_destroy(dev, property);
5921 }
5922
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005923 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5924 head) {
5925 plane->funcs->destroy(plane);
5926 }
5927
5928 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5929 crtc->funcs->destroy(crtc);
5930 }
5931
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005932 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005933 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005934 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005935 }
5936
5937 /*
5938 * Single-threaded teardown context, so it's not required to grab the
5939 * fb_lock to protect against concurrent fb_list access. Contrary, it
5940 * would actually deadlock with the drm_framebuffer_cleanup function.
5941 *
5942 * Also, if there are any framebuffers left, that's a driver leak now,
5943 * so politely WARN about this.
5944 */
5945 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5946 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005947 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005948 }
5949
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005950 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005951 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005952 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005953 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005954}
5955EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305956
5957struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5958 unsigned int supported_rotations)
5959{
5960 static const struct drm_prop_enum_list props[] = {
5961 { DRM_ROTATE_0, "rotate-0" },
5962 { DRM_ROTATE_90, "rotate-90" },
5963 { DRM_ROTATE_180, "rotate-180" },
5964 { DRM_ROTATE_270, "rotate-270" },
5965 { DRM_REFLECT_X, "reflect-x" },
5966 { DRM_REFLECT_Y, "reflect-y" },
5967 };
5968
5969 return drm_property_create_bitmask(dev, 0, "rotation",
5970 props, ARRAY_SIZE(props),
5971 supported_rotations);
5972}
5973EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005974
5975/**
5976 * DOC: Tile group
5977 *
5978 * Tile groups are used to represent tiled monitors with a unique
5979 * integer identifier. Tiled monitors using DisplayID v1.3 have
5980 * a unique 8-byte handle, we store this in a tile group, so we
5981 * have a common identifier for all tiles in a monitor group.
5982 */
5983static void drm_tile_group_free(struct kref *kref)
5984{
5985 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5986 struct drm_device *dev = tg->dev;
5987 mutex_lock(&dev->mode_config.idr_mutex);
5988 idr_remove(&dev->mode_config.tile_idr, tg->id);
5989 mutex_unlock(&dev->mode_config.idr_mutex);
5990 kfree(tg);
5991}
5992
5993/**
5994 * drm_mode_put_tile_group - drop a reference to a tile group.
5995 * @dev: DRM device
5996 * @tg: tile group to drop reference to.
5997 *
5998 * drop reference to tile group and free if 0.
5999 */
6000void drm_mode_put_tile_group(struct drm_device *dev,
6001 struct drm_tile_group *tg)
6002{
6003 kref_put(&tg->refcount, drm_tile_group_free);
6004}
6005
6006/**
6007 * drm_mode_get_tile_group - get a reference to an existing tile group
6008 * @dev: DRM device
6009 * @topology: 8-bytes unique per monitor.
6010 *
6011 * Use the unique bytes to get a reference to an existing tile group.
6012 *
6013 * RETURNS:
6014 * tile group or NULL if not found.
6015 */
6016struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6017 char topology[8])
6018{
6019 struct drm_tile_group *tg;
6020 int id;
6021 mutex_lock(&dev->mode_config.idr_mutex);
6022 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6023 if (!memcmp(tg->group_data, topology, 8)) {
6024 if (!kref_get_unless_zero(&tg->refcount))
6025 tg = NULL;
6026 mutex_unlock(&dev->mode_config.idr_mutex);
6027 return tg;
6028 }
6029 }
6030 mutex_unlock(&dev->mode_config.idr_mutex);
6031 return NULL;
6032}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006033EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10006034
6035/**
6036 * drm_mode_create_tile_group - create a tile group from a displayid description
6037 * @dev: DRM device
6038 * @topology: 8-bytes unique per monitor.
6039 *
6040 * Create a tile group for the unique monitor, and get a unique
6041 * identifier for the tile group.
6042 *
6043 * RETURNS:
6044 * new tile group or error.
6045 */
6046struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6047 char topology[8])
6048{
6049 struct drm_tile_group *tg;
6050 int ret;
6051
6052 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6053 if (!tg)
6054 return ERR_PTR(-ENOMEM);
6055
6056 kref_init(&tg->refcount);
6057 memcpy(tg->group_data, topology, 8);
6058 tg->dev = dev;
6059
6060 mutex_lock(&dev->mode_config.idr_mutex);
6061 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6062 if (ret >= 0) {
6063 tg->id = ret;
6064 } else {
6065 kfree(tg);
6066 tg = ERR_PTR(ret);
6067 }
6068
6069 mutex_unlock(&dev->mode_config.idr_mutex);
6070 return tg;
6071}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006072EXPORT_SYMBOL(drm_mode_create_tile_group);