blob: e1de475761439265fa13bbd10ba37139d35f3ca9 [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 */
436 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlie19ab3f82016-04-15 15:10:34 +1000437 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000438 mutex_unlock(&dev->mode_config.fb_lock);
439
440 fb->funcs->destroy(fb);
441}
442
Dave Airlief453ba02008-11-07 14:05:41 -0800443/**
Dave Airlief453ba02008-11-07 14:05:41 -0800444 * drm_framebuffer_init - initialize a framebuffer
445 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100446 * @fb: framebuffer to be initialized
447 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800448 *
Dave Airlief453ba02008-11-07 14:05:41 -0800449 * Allocates an ID for the framebuffer's parent mode object, sets its mode
450 * functions & device file and adds it to the master fd list.
451 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100452 * IMPORTANT:
453 * This functions publishes the fb and makes it available for concurrent access
454 * by other users. Which means by this point the fb _must_ be fully set up -
455 * since all the fb attributes are invariant over its lifetime, no further
456 * locking but only correct reference counting is required.
457 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100458 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200459 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800460 */
461int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
462 const struct drm_framebuffer_funcs *funcs)
463{
464 int ret;
465
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100466 INIT_LIST_HEAD(&fb->filp_head);
467 fb->dev = dev;
468 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000469
Dave Airlied0f37cf2016-04-15 15:10:36 +1000470 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
Dave Airlie9cd47422016-04-15 15:10:38 +1000471 false, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200472 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100473 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800474
Dave Airlie9cd47422016-04-15 15:10:38 +1000475 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800476 dev->mode_config.num_fb++;
477 list_add(&fb->head, &dev->mode_config.fb_list);
478
Dave Airlie9cd47422016-04-15 15:10:38 +1000479 drm_mode_object_register(dev, &fb->base);
480 mutex_unlock(&dev->mode_config.fb_lock);
481out:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200482 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800483}
484EXPORT_SYMBOL(drm_framebuffer_init);
485
Rob Clarkf7eff602012-09-05 21:48:38 +0000486/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100487 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
488 * @dev: drm device
489 * @id: id of the fb object
490 *
491 * If successful, this grabs an additional reference to the framebuffer -
492 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100493 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100494 */
495struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
496 uint32_t id)
497{
Dave Airliecee26ac2016-04-15 15:10:37 +1000498 struct drm_mode_object *obj;
499 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100500
501 mutex_lock(&dev->mode_config.fb_lock);
Dave Airliecee26ac2016-04-15 15:10:37 +1000502 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
Dave Airlie72fe90b2016-04-15 15:10:40 +1000503 if (obj)
Dave Airliecee26ac2016-04-15 15:10:37 +1000504 fb = obj_to_fb(obj);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100505 mutex_unlock(&dev->mode_config.fb_lock);
506
507 return fb;
508}
509EXPORT_SYMBOL(drm_framebuffer_lookup);
510
511/**
Daniel Vetter36206362012-12-10 20:42:17 +0100512 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
513 * @fb: fb to unregister
514 *
515 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
516 * those used for fbdev. Note that the caller must hold a reference of it's own,
517 * i.e. the object may not be destroyed through this call (since it'll lead to a
518 * locking inversion).
519 */
520void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
521{
Daniel Vettera39a3572015-08-25 15:45:11 +0200522 struct drm_device *dev;
523
524 if (!fb)
525 return;
526
527 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100528
529 mutex_lock(&dev->mode_config.fb_lock);
530 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000531 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100532 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100533}
534EXPORT_SYMBOL(drm_framebuffer_unregister_private);
535
536/**
Dave Airlief453ba02008-11-07 14:05:41 -0800537 * drm_framebuffer_cleanup - remove a framebuffer object
538 * @fb: framebuffer to remove
539 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100540 * Cleanup framebuffer. This function is intended to be used from the drivers
541 * ->destroy callback. It can also be used to clean up driver private
542 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100543 *
544 * Note that this function does not remove the fb from active usuage - if it is
545 * still used anywhere, hilarity can ensue since userspace could call getfb on
546 * the id and get back -EINVAL. Obviously no concern at driver unload time.
547 *
548 * Also, the framebuffer will not be removed from the lookup idr - for
549 * user-created framebuffers this will happen in in the rmfb ioctl. For
550 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
551 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800552 */
553void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
554{
555 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100556
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100557 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000558 list_del(&fb->head);
559 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100560 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000561}
562EXPORT_SYMBOL(drm_framebuffer_cleanup);
563
564/**
565 * drm_framebuffer_remove - remove and unreference a framebuffer object
566 * @fb: framebuffer to remove
567 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000568 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100569 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100570 * passed-in framebuffer. Might take the modeset locks.
571 *
572 * Note that this function optimizes the cleanup away if the caller holds the
573 * last reference to the framebuffer. It is also guaranteed to not take the
574 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000575 */
576void drm_framebuffer_remove(struct drm_framebuffer *fb)
577{
Daniel Vettera39a3572015-08-25 15:45:11 +0200578 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800579 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800580 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000581 struct drm_mode_set set;
582 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800583
Daniel Vettera39a3572015-08-25 15:45:11 +0200584 if (!fb)
585 return;
586
587 dev = fb->dev;
588
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100589 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100590
Daniel Vetterb62584e2012-12-11 16:51:35 +0100591 /*
592 * drm ABI mandates that we remove any deleted framebuffers from active
593 * useage. But since most sane clients only remove framebuffers they no
594 * longer need, try to optimize this away.
595 *
596 * Since we're holding a reference ourselves, observing a refcount of 1
597 * means that we're the last holder and can skip it. Also, the refcount
598 * can never increase from 1 again, so we don't need any barriers or
599 * locks.
600 *
601 * Note that userspace could try to race with use and instate a new
602 * usage _after_ we've cleared all current ones. End result will be an
603 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
604 * in this manner.
605 */
Dave Airlie747a5982016-04-15 15:10:35 +1000606 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100607 drm_modeset_lock_all(dev);
608 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200609 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700610 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100611 /* should turn off the crtc */
612 memset(&set, 0, sizeof(struct drm_mode_set));
613 set.crtc = crtc;
614 set.fb = NULL;
615 ret = drm_mode_set_config_internal(&set);
616 if (ret)
617 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
618 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000619 }
Dave Airlief453ba02008-11-07 14:05:41 -0800620
Daniel Vetter6295d602015-07-09 23:44:25 +0200621 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300622 if (plane->fb == fb)
623 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800624 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100625 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800626 }
627
Rob Clarkf7eff602012-09-05 21:48:38 +0000628 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800629}
Rob Clarkf7eff602012-09-05 21:48:38 +0000630EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800631
Rob Clark51fd3712013-11-19 12:10:12 -0500632DEFINE_WW_CLASS(crtc_ww_class);
633
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200634static unsigned int drm_num_crtcs(struct drm_device *dev)
635{
636 unsigned int num = 0;
637 struct drm_crtc *tmp;
638
639 drm_for_each_crtc(tmp, dev) {
640 num++;
641 }
642
643 return num;
644}
645
Dave Airlief453ba02008-11-07 14:05:41 -0800646/**
Matt Ropere13161a2014-04-01 15:22:38 -0700647 * drm_crtc_init_with_planes - Initialise a new CRTC object with
648 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800649 * @dev: DRM device
650 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700651 * @primary: Primary plane for CRTC
652 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800653 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200654 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800655 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000656 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200657 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100658 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200659 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800660 */
Matt Ropere13161a2014-04-01 15:22:38 -0700661int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
662 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700663 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200664 const struct drm_crtc_funcs *funcs,
665 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800666{
Rob Clark51fd3712013-11-19 12:10:12 -0500667 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200668 int ret;
669
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100670 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
671 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
672
Dave Airlief453ba02008-11-07 14:05:41 -0800673 crtc->dev = dev;
674 crtc->funcs = funcs;
675
Rob Clark51fd3712013-11-19 12:10:12 -0500676 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200677 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
678 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100679 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800680
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200681 if (name) {
682 va_list ap;
683
684 va_start(ap, name);
685 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
686 va_end(ap);
687 } else {
688 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
689 drm_num_crtcs(dev));
690 }
691 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000692 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200693 return -ENOMEM;
694 }
695
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300696 crtc->base.properties = &crtc->properties;
697
Rob Clark51fd3712013-11-19 12:10:12 -0500698 list_add_tail(&crtc->head, &config->crtc_list);
699 config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200700
Matt Ropere13161a2014-04-01 15:22:38 -0700701 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700702 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700703 if (primary)
704 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700705 if (cursor)
706 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700707
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100708 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
709 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100710 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100711 }
712
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100713 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800714}
Matt Ropere13161a2014-04-01 15:22:38 -0700715EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800716
717/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000718 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800719 * @crtc: CRTC to cleanup
720 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000721 * This function cleans up @crtc and removes it from the DRM mode setting
722 * core. Note that the function does *not* free the crtc structure itself,
723 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800724 */
725void drm_crtc_cleanup(struct drm_crtc *crtc)
726{
727 struct drm_device *dev = crtc->dev;
728
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000729 kfree(crtc->gamma_store);
730 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800731
Rob Clark51fd3712013-11-19 12:10:12 -0500732 drm_modeset_lock_fini(&crtc->mutex);
733
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000734 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800735 list_del(&crtc->head);
736 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100737
738 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
739 if (crtc->state && crtc->funcs->atomic_destroy_state)
740 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100741
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200742 kfree(crtc->name);
743
Thierry Redinga18c0af2014-12-10 11:38:49 +0100744 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800745}
746EXPORT_SYMBOL(drm_crtc_cleanup);
747
748/**
Russell Kingdb5f7a62014-01-02 21:27:33 +0000749 * drm_crtc_index - find the index of a registered CRTC
750 * @crtc: CRTC to find index for
751 *
752 * Given a registered CRTC, return the index of that CRTC within a DRM
753 * device's list of CRTCs.
754 */
755unsigned int drm_crtc_index(struct drm_crtc *crtc)
756{
757 unsigned int index = 0;
758 struct drm_crtc *tmp;
759
Daniel Vettere4f62542015-07-09 23:44:35 +0200760 drm_for_each_crtc(tmp, crtc->dev) {
Russell Kingdb5f7a62014-01-02 21:27:33 +0000761 if (tmp == crtc)
762 return index;
763
764 index++;
765 }
766
767 BUG();
768}
769EXPORT_SYMBOL(drm_crtc_index);
770
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100771/*
Dave Airlief453ba02008-11-07 14:05:41 -0800772 * drm_mode_remove - remove and free a mode
773 * @connector: connector list to modify
774 * @mode: mode to remove
775 *
Dave Airlief453ba02008-11-07 14:05:41 -0800776 * Remove @mode from @connector's mode list, then free it.
777 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100778static void drm_mode_remove(struct drm_connector *connector,
779 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800780{
781 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100782 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800783}
Dave Airlief453ba02008-11-07 14:05:41 -0800784
785/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200786 * drm_display_info_set_bus_formats - set the supported bus formats
787 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100788 * @formats: array containing the supported bus formats
789 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200790 *
791 * Store the supported bus formats in display info structure.
792 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
793 * a full list of available formats.
794 */
795int drm_display_info_set_bus_formats(struct drm_display_info *info,
796 const u32 *formats,
797 unsigned int num_formats)
798{
799 u32 *fmts = NULL;
800
801 if (!formats && num_formats)
802 return -EINVAL;
803
804 if (formats && num_formats) {
805 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
806 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300807 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200808 return -ENOMEM;
809 }
810
811 kfree(info->bus_formats);
812 info->bus_formats = fmts;
813 info->num_bus_formats = num_formats;
814
815 return 0;
816}
817EXPORT_SYMBOL(drm_display_info_set_bus_formats);
818
819/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200820 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
821 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200822 *
823 * The kernel supports per-connector configration of its consoles through
824 * use of the video= parameter. This function parses that option and
825 * extracts the user's specified mode (or enable/disable status) for a
826 * particular connector. This is typically only used during the early fbdev
827 * setup.
828 */
829static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
830{
831 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
832 char *option = NULL;
833
834 if (fb_get_options(connector->name, &option))
835 return;
836
837 if (!drm_mode_parse_command_line_for_connector(option,
838 connector,
839 mode))
840 return;
841
842 if (mode->force) {
843 const char *s;
844
845 switch (mode->force) {
846 case DRM_FORCE_OFF:
847 s = "OFF";
848 break;
849 case DRM_FORCE_ON_DIGITAL:
850 s = "ON - dig";
851 break;
852 default:
853 case DRM_FORCE_ON:
854 s = "ON";
855 break;
856 }
857
858 DRM_INFO("forcing %s connector %s\n", connector->name, s);
859 connector->force = mode->force;
860 }
861
862 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
863 connector->name,
864 mode->xres, mode->yres,
865 mode->refresh_specified ? mode->refresh : 60,
866 mode->rb ? " reduced blanking" : "",
867 mode->margins ? " with margins" : "",
868 mode->interlace ? " interlaced" : "");
869}
870
871/**
Dave Airlief453ba02008-11-07 14:05:41 -0800872 * drm_connector_init - Init a preallocated connector
873 * @dev: DRM device
874 * @connector: the connector to init
875 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100876 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800877 *
Dave Airlief453ba02008-11-07 14:05:41 -0800878 * Initialises a preallocated connector. Connectors should be
879 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200880 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100881 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200882 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800883 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200884int drm_connector_init(struct drm_device *dev,
885 struct drm_connector *connector,
886 const struct drm_connector_funcs *funcs,
887 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800888{
Rob Clarkae16c592014-12-18 16:01:54 -0500889 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200890 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400891 struct ida *connector_ida =
892 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200893
Daniel Vetter84849902012-12-02 00:28:11 +0100894 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800895
Dave Airlied0f37cf2016-04-15 15:10:36 +1000896 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false, NULL);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200897 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300898 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200899
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300900 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800901 connector->dev = dev;
902 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100903
904 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
905 if (connector->connector_id < 0) {
906 ret = connector->connector_id;
907 goto out_put;
908 }
909
Dave Airlief453ba02008-11-07 14:05:41 -0800910 connector->connector_type = connector_type;
911 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400912 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
913 if (connector->connector_type_id < 0) {
914 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100915 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400916 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300917 connector->name =
918 kasprintf(GFP_KERNEL, "%s-%d",
919 drm_connector_enum_list[connector_type].name,
920 connector->connector_type_id);
921 if (!connector->name) {
922 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100923 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300924 }
925
Dave Airlief453ba02008-11-07 14:05:41 -0800926 INIT_LIST_HEAD(&connector->probed_modes);
927 INIT_LIST_HEAD(&connector->modes);
928 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000929 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800930
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200931 drm_connector_get_cmdline_mode(connector);
932
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100933 /* We should add connectors at the end to avoid upsetting the connector
934 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500935 list_add_tail(&connector->head, &config->connector_list);
936 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800937
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200938 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500939 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500940 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200941 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800942
Rob Clark58495562012-10-11 20:50:56 -0500943 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500944 config->dpms_property, 0);
945
946 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
947 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
948 }
Dave Airlief453ba02008-11-07 14:05:41 -0800949
Thomas Wood30f65702014-06-18 17:52:32 +0100950 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100951out_put_type_id:
952 if (ret)
953 ida_remove(connector_ida, connector->connector_type_id);
954out_put_id:
955 if (ret)
956 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300957out_put:
958 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000959 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300960
961out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100962 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200963
964 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800965}
966EXPORT_SYMBOL(drm_connector_init);
967
968/**
969 * drm_connector_cleanup - cleans up an initialised connector
970 * @connector: connector to cleanup
971 *
Dave Airlief453ba02008-11-07 14:05:41 -0800972 * Cleans up the connector but doesn't free the object.
973 */
974void drm_connector_cleanup(struct drm_connector *connector)
975{
976 struct drm_device *dev = connector->dev;
977 struct drm_display_mode *mode, *t;
978
Dave Airlie40d9b042014-10-20 16:29:33 +1000979 if (connector->tile_group) {
980 drm_mode_put_tile_group(dev, connector->tile_group);
981 connector->tile_group = NULL;
982 }
983
Dave Airlief453ba02008-11-07 14:05:41 -0800984 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
985 drm_mode_remove(connector, mode);
986
987 list_for_each_entry_safe(mode, t, &connector->modes, head)
988 drm_mode_remove(connector, mode);
989
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400990 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
991 connector->connector_type_id);
992
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100993 ida_remove(&dev->mode_config.connector_ida,
994 connector->connector_id);
995
Boris Brezillonb5571e92014-07-22 12:09:10 +0200996 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000997 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300998 kfree(connector->name);
999 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08001000 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001001 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +01001002
1003 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1004 if (connector->state && connector->funcs->atomic_destroy_state)
1005 connector->funcs->atomic_destroy_state(connector,
1006 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001007
1008 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001009}
1010EXPORT_SYMBOL(drm_connector_cleanup);
1011
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001012/**
Thomas Wood34ea3d32014-05-29 16:57:41 +01001013 * drm_connector_register - register a connector
1014 * @connector: the connector to register
1015 *
1016 * Register userspace interfaces for a connector
1017 *
1018 * Returns:
1019 * Zero on success, error code on failure.
1020 */
1021int drm_connector_register(struct drm_connector *connector)
1022{
Thomas Wood30f65702014-06-18 17:52:32 +01001023 int ret;
1024
Dave Airlie2ee39452014-07-24 11:53:45 +10001025 drm_mode_object_register(connector->dev, &connector->base);
1026
Thomas Wood30f65702014-06-18 17:52:32 +01001027 ret = drm_sysfs_connector_add(connector);
1028 if (ret)
1029 return ret;
1030
1031 ret = drm_debugfs_connector_add(connector);
1032 if (ret) {
1033 drm_sysfs_connector_remove(connector);
1034 return ret;
1035 }
1036
1037 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001038}
1039EXPORT_SYMBOL(drm_connector_register);
1040
1041/**
1042 * drm_connector_unregister - unregister a connector
1043 * @connector: the connector to unregister
1044 *
1045 * Unregister userspace interfaces for a connector
1046 */
1047void drm_connector_unregister(struct drm_connector *connector)
1048{
1049 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001050 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001051}
1052EXPORT_SYMBOL(drm_connector_unregister);
1053
Thomas Wood34ea3d32014-05-29 16:57:41 +01001054/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001055 * drm_connector_register_all - register all connectors
1056 * @dev: drm device
1057 *
1058 * This function registers all connectors in sysfs and other places so that
1059 * userspace can start to access them. Drivers can call it after calling
1060 * drm_dev_register() to complete the device registration, if they don't call
1061 * drm_connector_register() on each connector individually.
1062 *
1063 * When a device is unplugged and should be removed from userspace access,
1064 * call drm_connector_unregister_all(), which is the inverse of this
1065 * function.
1066 *
1067 * Returns:
1068 * Zero on success, error code on failure.
1069 */
1070int drm_connector_register_all(struct drm_device *dev)
1071{
1072 struct drm_connector *connector;
1073 int ret;
1074
1075 mutex_lock(&dev->mode_config.mutex);
1076
1077 drm_for_each_connector(connector, dev) {
1078 ret = drm_connector_register(connector);
1079 if (ret)
1080 goto err;
1081 }
1082
1083 mutex_unlock(&dev->mode_config.mutex);
1084
1085 return 0;
1086
1087err:
1088 mutex_unlock(&dev->mode_config.mutex);
1089 drm_connector_unregister_all(dev);
1090 return ret;
1091}
1092EXPORT_SYMBOL(drm_connector_register_all);
1093
1094/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001095 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001096 * @dev: drm device
1097 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001098 * This functions unregisters all connectors from sysfs and other places so
1099 * that userspace can no longer access them. Drivers should call this as the
1100 * first step tearing down the device instace, or when the underlying
1101 * physical device disappeared (e.g. USB unplug), right before calling
1102 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001103 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001104void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001105{
1106 struct drm_connector *connector;
1107
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001108 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001109 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001110 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001111}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001112EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001113
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001114/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001115 * drm_encoder_init - Init a preallocated encoder
1116 * @dev: drm device
1117 * @encoder: the encoder to init
1118 * @funcs: callbacks for this encoder
1119 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001120 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001121 *
1122 * Initialises a preallocated encoder. Encoder should be
1123 * subclassed as part of driver encoder objects.
1124 *
1125 * Returns:
1126 * Zero on success, error code on failure.
1127 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001128int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001129 struct drm_encoder *encoder,
1130 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001131 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001132{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001133 int ret;
1134
Daniel Vetter84849902012-12-02 00:28:11 +01001135 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001136
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001137 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1138 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001139 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001140
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001141 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001142 encoder->encoder_type = encoder_type;
1143 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001144 if (name) {
1145 va_list ap;
1146
1147 va_start(ap, name);
1148 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1149 va_end(ap);
1150 } else {
1151 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1152 drm_encoder_enum_list[encoder_type].name,
1153 encoder->base.id);
1154 }
Jani Nikulae5748942014-05-14 16:58:20 +03001155 if (!encoder->name) {
1156 ret = -ENOMEM;
1157 goto out_put;
1158 }
Dave Airlief453ba02008-11-07 14:05:41 -08001159
1160 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1161 dev->mode_config.num_encoder++;
1162
Jani Nikulae5748942014-05-14 16:58:20 +03001163out_put:
1164 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001165 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001166
1167out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001168 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001169
1170 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001171}
1172EXPORT_SYMBOL(drm_encoder_init);
1173
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001174/**
Maarten Lankhorst47d77772016-01-07 10:59:18 +01001175 * drm_encoder_index - find the index of a registered encoder
1176 * @encoder: encoder to find index for
1177 *
1178 * Given a registered encoder, return the index of that encoder within a DRM
1179 * device's list of encoders.
1180 */
1181unsigned int drm_encoder_index(struct drm_encoder *encoder)
1182{
1183 unsigned int index = 0;
1184 struct drm_encoder *tmp;
1185
1186 drm_for_each_encoder(tmp, encoder->dev) {
1187 if (tmp == encoder)
1188 return index;
1189
1190 index++;
1191 }
1192
1193 BUG();
1194}
1195EXPORT_SYMBOL(drm_encoder_index);
1196
1197/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001198 * drm_encoder_cleanup - cleans up an initialised encoder
1199 * @encoder: encoder to cleanup
1200 *
1201 * Cleans up the encoder but doesn't free the object.
1202 */
Dave Airlief453ba02008-11-07 14:05:41 -08001203void drm_encoder_cleanup(struct drm_encoder *encoder)
1204{
1205 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001206
Daniel Vetter84849902012-12-02 00:28:11 +01001207 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001208 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001209 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001210 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001211 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001212 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001213
1214 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001215}
1216EXPORT_SYMBOL(drm_encoder_cleanup);
1217
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001218static unsigned int drm_num_planes(struct drm_device *dev)
1219{
1220 unsigned int num = 0;
1221 struct drm_plane *tmp;
1222
1223 drm_for_each_plane(tmp, dev) {
1224 num++;
1225 }
1226
1227 return num;
1228}
1229
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001230/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001231 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001232 * @dev: DRM device
1233 * @plane: plane object to init
1234 * @possible_crtcs: bitmask of possible CRTCs
1235 * @funcs: callbacks for the new plane
1236 * @formats: array of supported formats (%DRM_FORMAT_*)
1237 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001238 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001239 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001240 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001241 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001242 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001243 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001244 * Zero on success, error code on failure.
1245 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001246int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1247 unsigned long possible_crtcs,
1248 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001249 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001250 enum drm_plane_type type,
1251 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001252{
Rob Clark6b4959f2014-12-18 16:01:53 -05001253 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001254 int ret;
1255
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001256 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1257 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001258 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001259
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001260 drm_modeset_lock_init(&plane->mutex);
1261
Rob Clark4d939142012-05-17 02:23:27 -06001262 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001263 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001264 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001265 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1266 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001267 if (!plane->format_types) {
1268 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001269 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001270 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001271 }
1272
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001273 if (name) {
1274 va_list ap;
1275
1276 va_start(ap, name);
1277 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1278 va_end(ap);
1279 } else {
1280 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1281 drm_num_planes(dev));
1282 }
1283 if (!plane->name) {
1284 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001285 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001286 return -ENOMEM;
1287 }
1288
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001289 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001290 plane->format_count = format_count;
1291 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001292 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001293
Rob Clark6b4959f2014-12-18 16:01:53 -05001294 list_add_tail(&plane->head, &config->plane_list);
1295 config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001296 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001297 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001298
Rob Clark9922ab52014-04-01 20:16:57 -04001299 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001300 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001301 plane->type);
1302
Rob Clark6b4959f2014-12-18 16:01:53 -05001303 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1304 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1305 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1306 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1307 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1308 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1309 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1310 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1311 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1312 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1313 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1314 }
1315
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001316 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001317}
Matt Roperdc415ff2014-04-01 15:22:36 -07001318EXPORT_SYMBOL(drm_universal_plane_init);
1319
1320/**
1321 * drm_plane_init - Initialize a legacy plane
1322 * @dev: DRM device
1323 * @plane: plane object to init
1324 * @possible_crtcs: bitmask of possible CRTCs
1325 * @funcs: callbacks for the new plane
1326 * @formats: array of supported formats (%DRM_FORMAT_*)
1327 * @format_count: number of elements in @formats
1328 * @is_primary: plane type (primary vs overlay)
1329 *
1330 * Legacy API to initialize a DRM plane.
1331 *
1332 * New drivers should call drm_universal_plane_init() instead.
1333 *
1334 * Returns:
1335 * Zero on success, error code on failure.
1336 */
1337int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1338 unsigned long possible_crtcs,
1339 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001340 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001341 bool is_primary)
1342{
1343 enum drm_plane_type type;
1344
1345 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1346 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001347 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001348}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001349EXPORT_SYMBOL(drm_plane_init);
1350
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001351/**
1352 * drm_plane_cleanup - Clean up the core plane usage
1353 * @plane: plane to cleanup
1354 *
1355 * This function cleans up @plane and removes it from the DRM mode setting
1356 * core. Note that the function does *not* free the plane structure itself,
1357 * this is the responsibility of the caller.
1358 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001359void drm_plane_cleanup(struct drm_plane *plane)
1360{
1361 struct drm_device *dev = plane->dev;
1362
Daniel Vetter84849902012-12-02 00:28:11 +01001363 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001364 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001365 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001366
1367 BUG_ON(list_empty(&plane->head));
1368
1369 list_del(&plane->head);
1370 dev->mode_config.num_total_plane--;
1371 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1372 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001373 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001374
1375 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1376 if (plane->state && plane->funcs->atomic_destroy_state)
1377 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001378
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001379 kfree(plane->name);
1380
Thierry Redinga18c0af2014-12-10 11:38:49 +01001381 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001382}
1383EXPORT_SYMBOL(drm_plane_cleanup);
1384
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001385/**
Daniel Vetter10f637b2014-07-29 13:47:11 +02001386 * drm_plane_index - find the index of a registered plane
1387 * @plane: plane to find index for
1388 *
1389 * Given a registered plane, return the index of that CRTC within a DRM
1390 * device's list of planes.
1391 */
1392unsigned int drm_plane_index(struct drm_plane *plane)
1393{
1394 unsigned int index = 0;
1395 struct drm_plane *tmp;
1396
Daniel Vettere4f62542015-07-09 23:44:35 +02001397 drm_for_each_plane(tmp, plane->dev) {
Daniel Vetter10f637b2014-07-29 13:47:11 +02001398 if (tmp == plane)
1399 return index;
1400
1401 index++;
1402 }
1403
1404 BUG();
1405}
1406EXPORT_SYMBOL(drm_plane_index);
1407
1408/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001409 * drm_plane_from_index - find the registered plane at an index
1410 * @dev: DRM device
1411 * @idx: index of registered plane to find for
1412 *
1413 * Given a plane index, return the registered plane from DRM device's
1414 * list of planes with matching index.
1415 */
1416struct drm_plane *
1417drm_plane_from_index(struct drm_device *dev, int idx)
1418{
1419 struct drm_plane *plane;
1420 unsigned int i = 0;
1421
Daniel Vetter6295d602015-07-09 23:44:25 +02001422 drm_for_each_plane(plane, dev) {
Chandra Konduruf81338a2015-04-09 17:36:21 -07001423 if (i == idx)
1424 return plane;
1425 i++;
1426 }
1427 return NULL;
1428}
1429EXPORT_SYMBOL(drm_plane_from_index);
1430
1431/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001432 * drm_plane_force_disable - Forcibly disable a plane
1433 * @plane: plane to disable
1434 *
1435 * Forces the plane to be disabled.
1436 *
1437 * Used when the plane's current framebuffer is destroyed,
1438 * and when restoring fbdev mode.
1439 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001440void drm_plane_force_disable(struct drm_plane *plane)
1441{
1442 int ret;
1443
Daniel Vetter3d30a592014-07-27 13:42:42 +02001444 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001445 return;
1446
Daniel Vetter3d30a592014-07-27 13:42:42 +02001447 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001448 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001449 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001450 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001451 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001452 return;
1453 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001454 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001455 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001456 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001457 plane->fb = NULL;
1458 plane->crtc = NULL;
1459}
1460EXPORT_SYMBOL(drm_plane_force_disable);
1461
Rob Clark6b4959f2014-12-18 16:01:53 -05001462static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001463{
Rob Clark356af0e2014-12-18 16:01:52 -05001464 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001465
1466 /*
1467 * Standard properties (apply to all connectors)
1468 */
Rob Clark356af0e2014-12-18 16:01:52 -05001469 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001470 DRM_MODE_PROP_IMMUTABLE,
1471 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001472 if (!prop)
1473 return -ENOMEM;
1474 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001475
Rob Clark356af0e2014-12-18 16:01:52 -05001476 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001477 "DPMS", drm_dpms_enum_list,
1478 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001479 if (!prop)
1480 return -ENOMEM;
1481 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001482
Rob Clark356af0e2014-12-18 16:01:52 -05001483 prop = drm_property_create(dev,
1484 DRM_MODE_PROP_BLOB |
1485 DRM_MODE_PROP_IMMUTABLE,
1486 "PATH", 0);
1487 if (!prop)
1488 return -ENOMEM;
1489 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001490
Rob Clark356af0e2014-12-18 16:01:52 -05001491 prop = drm_property_create(dev,
1492 DRM_MODE_PROP_BLOB |
1493 DRM_MODE_PROP_IMMUTABLE,
1494 "TILE", 0);
1495 if (!prop)
1496 return -ENOMEM;
1497 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001498
Rob Clark6b4959f2014-12-18 16:01:53 -05001499 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001500 "type", drm_plane_type_enum_list,
1501 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001502 if (!prop)
1503 return -ENOMEM;
1504 dev->mode_config.plane_type_property = prop;
1505
1506 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1507 "SRC_X", 0, UINT_MAX);
1508 if (!prop)
1509 return -ENOMEM;
1510 dev->mode_config.prop_src_x = prop;
1511
1512 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1513 "SRC_Y", 0, UINT_MAX);
1514 if (!prop)
1515 return -ENOMEM;
1516 dev->mode_config.prop_src_y = prop;
1517
1518 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1519 "SRC_W", 0, UINT_MAX);
1520 if (!prop)
1521 return -ENOMEM;
1522 dev->mode_config.prop_src_w = prop;
1523
1524 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1525 "SRC_H", 0, UINT_MAX);
1526 if (!prop)
1527 return -ENOMEM;
1528 dev->mode_config.prop_src_h = prop;
1529
1530 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1531 "CRTC_X", INT_MIN, INT_MAX);
1532 if (!prop)
1533 return -ENOMEM;
1534 dev->mode_config.prop_crtc_x = prop;
1535
1536 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1537 "CRTC_Y", INT_MIN, INT_MAX);
1538 if (!prop)
1539 return -ENOMEM;
1540 dev->mode_config.prop_crtc_y = prop;
1541
1542 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1543 "CRTC_W", 0, INT_MAX);
1544 if (!prop)
1545 return -ENOMEM;
1546 dev->mode_config.prop_crtc_w = prop;
1547
1548 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1549 "CRTC_H", 0, INT_MAX);
1550 if (!prop)
1551 return -ENOMEM;
1552 dev->mode_config.prop_crtc_h = prop;
1553
1554 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1555 "FB_ID", DRM_MODE_OBJECT_FB);
1556 if (!prop)
1557 return -ENOMEM;
1558 dev->mode_config.prop_fb_id = prop;
1559
1560 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1561 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1562 if (!prop)
1563 return -ENOMEM;
1564 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001565
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001566 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1567 "ACTIVE");
1568 if (!prop)
1569 return -ENOMEM;
1570 dev->mode_config.prop_active = prop;
1571
Daniel Stone955f3c32015-05-25 19:11:52 +01001572 prop = drm_property_create(dev,
1573 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1574 "MODE_ID", 0);
1575 if (!prop)
1576 return -ENOMEM;
1577 dev->mode_config.prop_mode_id = prop;
1578
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001579 prop = drm_property_create(dev,
1580 DRM_MODE_PROP_BLOB,
1581 "DEGAMMA_LUT", 0);
1582 if (!prop)
1583 return -ENOMEM;
1584 dev->mode_config.degamma_lut_property = prop;
1585
1586 prop = drm_property_create_range(dev,
1587 DRM_MODE_PROP_IMMUTABLE,
1588 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1589 if (!prop)
1590 return -ENOMEM;
1591 dev->mode_config.degamma_lut_size_property = prop;
1592
1593 prop = drm_property_create(dev,
1594 DRM_MODE_PROP_BLOB,
1595 "CTM", 0);
1596 if (!prop)
1597 return -ENOMEM;
1598 dev->mode_config.ctm_property = prop;
1599
1600 prop = drm_property_create(dev,
1601 DRM_MODE_PROP_BLOB,
1602 "GAMMA_LUT", 0);
1603 if (!prop)
1604 return -ENOMEM;
1605 dev->mode_config.gamma_lut_property = prop;
1606
1607 prop = drm_property_create_range(dev,
1608 DRM_MODE_PROP_IMMUTABLE,
1609 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1610 if (!prop)
1611 return -ENOMEM;
1612 dev->mode_config.gamma_lut_size_property = prop;
1613
Rob Clark9922ab52014-04-01 20:16:57 -04001614 return 0;
1615}
1616
Dave Airlief453ba02008-11-07 14:05:41 -08001617/**
1618 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1619 * @dev: DRM device
1620 *
1621 * Called by a driver the first time a DVI-I connector is made.
1622 */
1623int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1624{
1625 struct drm_property *dvi_i_selector;
1626 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001627
1628 if (dev->mode_config.dvi_i_select_subconnector_property)
1629 return 0;
1630
1631 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001632 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001633 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001634 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001635 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001636 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1637
Sascha Hauer4a67d392012-02-06 10:58:17 +01001638 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001639 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001640 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001641 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001642 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1643
1644 return 0;
1645}
1646EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1647
1648/**
1649 * drm_create_tv_properties - create TV specific connector properties
1650 * @dev: DRM device
1651 * @num_modes: number of different TV formats (modes) supported
1652 * @modes: array of pointers to strings containing name of each format
1653 *
1654 * Called by a driver's TV initialization routine, this function creates
1655 * the TV specific connector properties for a given device. Caller is
1656 * responsible for allocating a list of format names and passing them to
1657 * this routine.
1658 */
Thierry Reding2f763312014-10-13 12:45:57 +02001659int drm_mode_create_tv_properties(struct drm_device *dev,
1660 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001661 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001662{
1663 struct drm_property *tv_selector;
1664 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001665 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001666
1667 if (dev->mode_config.tv_select_subconnector_property)
1668 return 0;
1669
1670 /*
1671 * Basic connector properties
1672 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001673 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001674 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001675 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001676 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001677 if (!tv_selector)
1678 goto nomem;
1679
Dave Airlief453ba02008-11-07 14:05:41 -08001680 dev->mode_config.tv_select_subconnector_property = tv_selector;
1681
1682 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001683 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1684 "subconnector",
1685 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001686 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001687 if (!tv_subconnector)
1688 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001689 dev->mode_config.tv_subconnector_property = tv_subconnector;
1690
1691 /*
1692 * Other, TV specific properties: margins & TV modes.
1693 */
1694 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001695 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001696 if (!dev->mode_config.tv_left_margin_property)
1697 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001698
1699 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001700 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001701 if (!dev->mode_config.tv_right_margin_property)
1702 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001703
1704 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001705 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001706 if (!dev->mode_config.tv_top_margin_property)
1707 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001708
1709 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001710 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001711 if (!dev->mode_config.tv_bottom_margin_property)
1712 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001713
1714 dev->mode_config.tv_mode_property =
1715 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1716 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001717 if (!dev->mode_config.tv_mode_property)
1718 goto nomem;
1719
Dave Airlief453ba02008-11-07 14:05:41 -08001720 for (i = 0; i < num_modes; i++)
1721 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1722 i, modes[i]);
1723
Francisco Jerezb6b79022009-08-02 04:19:20 +02001724 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001725 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001726 if (!dev->mode_config.tv_brightness_property)
1727 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001728
1729 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001730 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001731 if (!dev->mode_config.tv_contrast_property)
1732 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001733
1734 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001735 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001736 if (!dev->mode_config.tv_flicker_reduction_property)
1737 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001738
Francisco Jereza75f0232009-08-12 02:30:10 +02001739 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001740 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001741 if (!dev->mode_config.tv_overscan_property)
1742 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001743
1744 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001745 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001746 if (!dev->mode_config.tv_saturation_property)
1747 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001748
1749 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001750 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001751 if (!dev->mode_config.tv_hue_property)
1752 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001753
Dave Airlief453ba02008-11-07 14:05:41 -08001754 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001755nomem:
1756 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001757}
1758EXPORT_SYMBOL(drm_mode_create_tv_properties);
1759
1760/**
1761 * drm_mode_create_scaling_mode_property - create scaling mode property
1762 * @dev: DRM device
1763 *
1764 * Called by a driver the first time it's needed, must be attached to desired
1765 * connectors.
1766 */
1767int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1768{
1769 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001770
1771 if (dev->mode_config.scaling_mode_property)
1772 return 0;
1773
1774 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001775 drm_property_create_enum(dev, 0, "scaling mode",
1776 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001777 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001778
1779 dev->mode_config.scaling_mode_property = scaling_mode;
1780
1781 return 0;
1782}
1783EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1784
1785/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301786 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1787 * @dev: DRM device
1788 *
1789 * Called by a driver the first time it's needed, must be attached to desired
1790 * connectors.
1791 *
1792 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001793 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301794 */
1795int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1796{
1797 if (dev->mode_config.aspect_ratio_property)
1798 return 0;
1799
1800 dev->mode_config.aspect_ratio_property =
1801 drm_property_create_enum(dev, 0, "aspect ratio",
1802 drm_aspect_ratio_enum_list,
1803 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1804
1805 if (dev->mode_config.aspect_ratio_property == NULL)
1806 return -ENOMEM;
1807
1808 return 0;
1809}
1810EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1811
1812/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001813 * drm_mode_create_dirty_property - create dirty property
1814 * @dev: DRM device
1815 *
1816 * Called by a driver the first time it's needed, must be attached to desired
1817 * connectors.
1818 */
1819int drm_mode_create_dirty_info_property(struct drm_device *dev)
1820{
1821 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001822
1823 if (dev->mode_config.dirty_info_property)
1824 return 0;
1825
1826 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001827 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001828 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001829 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001830 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001831 dev->mode_config.dirty_info_property = dirty_info;
1832
1833 return 0;
1834}
1835EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1836
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001837/**
1838 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1839 * @dev: DRM device
1840 *
1841 * Create the the suggested x/y offset property for connectors.
1842 */
1843int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1844{
1845 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1846 return 0;
1847
1848 dev->mode_config.suggested_x_property =
1849 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1850
1851 dev->mode_config.suggested_y_property =
1852 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1853
1854 if (dev->mode_config.suggested_x_property == NULL ||
1855 dev->mode_config.suggested_y_property == NULL)
1856 return -ENOMEM;
1857 return 0;
1858}
1859EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1860
Dave Airlief453ba02008-11-07 14:05:41 -08001861/**
Dave Airlief453ba02008-11-07 14:05:41 -08001862 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001863 * @dev: drm device for the ioctl
1864 * @data: data pointer for the ioctl
1865 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001866 *
Dave Airlief453ba02008-11-07 14:05:41 -08001867 * Construct a set of configuration description structures and return
1868 * them to the user, including CRTC, connector and framebuffer configuration.
1869 *
1870 * Called by the user via ioctl.
1871 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001872 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001873 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001874 */
1875int drm_mode_getresources(struct drm_device *dev, void *data,
1876 struct drm_file *file_priv)
1877{
1878 struct drm_mode_card_res *card_res = data;
1879 struct list_head *lh;
1880 struct drm_framebuffer *fb;
1881 struct drm_connector *connector;
1882 struct drm_crtc *crtc;
1883 struct drm_encoder *encoder;
1884 int ret = 0;
1885 int connector_count = 0;
1886 int crtc_count = 0;
1887 int fb_count = 0;
1888 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001889 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001890 uint32_t __user *fb_id;
1891 uint32_t __user *crtc_id;
1892 uint32_t __user *connector_id;
1893 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001894
Dave Airliefb3b06c2011-02-08 13:55:21 +10001895 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1896 return -EINVAL;
1897
Dave Airlief453ba02008-11-07 14:05:41 -08001898
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001899 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001900 /*
1901 * For the non-control nodes we need to limit the list of resources
1902 * by IDs in the group list for this node
1903 */
1904 list_for_each(lh, &file_priv->fbs)
1905 fb_count++;
1906
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001907 /* handle this in 4 parts */
1908 /* FBs */
1909 if (card_res->count_fbs >= fb_count) {
1910 copied = 0;
1911 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1912 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1913 if (put_user(fb->base.id, fb_id + copied)) {
1914 mutex_unlock(&file_priv->fbs_lock);
1915 return -EFAULT;
1916 }
1917 copied++;
1918 }
1919 }
1920 card_res->count_fbs = fb_count;
1921 mutex_unlock(&file_priv->fbs_lock);
1922
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001923 /* mode_config.mutex protects the connector list against e.g. DP MST
1924 * connector hot-adding. CRTC/Plane lists are invariant. */
1925 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001926 drm_for_each_crtc(crtc, dev)
1927 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001928
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001929 drm_for_each_connector(connector, dev)
1930 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001931
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001932 drm_for_each_encoder(encoder, dev)
1933 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001934
1935 card_res->max_height = dev->mode_config.max_height;
1936 card_res->min_height = dev->mode_config.min_height;
1937 card_res->max_width = dev->mode_config.max_width;
1938 card_res->min_width = dev->mode_config.min_width;
1939
Dave Airlief453ba02008-11-07 14:05:41 -08001940 /* CRTCs */
1941 if (card_res->count_crtcs >= crtc_count) {
1942 copied = 0;
1943 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001944 drm_for_each_crtc(crtc, dev) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001945 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1946 crtc->base.id, crtc->name);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001947 if (put_user(crtc->base.id, crtc_id + copied)) {
1948 ret = -EFAULT;
1949 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001950 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001951 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001952 }
1953 }
1954 card_res->count_crtcs = crtc_count;
1955
1956 /* Encoders */
1957 if (card_res->count_encoders >= encoder_count) {
1958 copied = 0;
1959 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001960 drm_for_each_encoder(encoder, dev) {
1961 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1962 encoder->name);
1963 if (put_user(encoder->base.id, encoder_id +
1964 copied)) {
1965 ret = -EFAULT;
1966 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001967 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001968 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001969 }
1970 }
1971 card_res->count_encoders = encoder_count;
1972
1973 /* Connectors */
1974 if (card_res->count_connectors >= connector_count) {
1975 copied = 0;
1976 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001977 drm_for_each_connector(connector, dev) {
1978 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1979 connector->base.id,
1980 connector->name);
1981 if (put_user(connector->base.id,
1982 connector_id + copied)) {
1983 ret = -EFAULT;
1984 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001985 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001986 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001987 }
1988 }
1989 card_res->count_connectors = connector_count;
1990
Jerome Glisse94401062010-07-15 15:43:25 -04001991 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001992 card_res->count_connectors, card_res->count_encoders);
1993
1994out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001995 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001996 return ret;
1997}
1998
1999/**
2000 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002001 * @dev: drm device for the ioctl
2002 * @data: data pointer for the ioctl
2003 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002004 *
Dave Airlief453ba02008-11-07 14:05:41 -08002005 * Construct a CRTC configuration structure to return to the user.
2006 *
2007 * Called by the user via ioctl.
2008 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002009 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002010 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002011 */
2012int drm_mode_getcrtc(struct drm_device *dev,
2013 void *data, struct drm_file *file_priv)
2014{
2015 struct drm_mode_crtc *crtc_resp = data;
2016 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002017
Dave Airliefb3b06c2011-02-08 13:55:21 +10002018 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2019 return -EINVAL;
2020
Rob Clarka2b34e22013-10-05 16:36:52 -04002021 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002022 if (!crtc)
2023 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002024
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002025 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08002026 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07002027 if (crtc->primary->fb)
2028 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002029 else
2030 crtc_resp->fb_id = 0;
2031
Daniel Vetter31c946e2015-02-22 12:24:17 +01002032 if (crtc->state) {
2033 crtc_resp->x = crtc->primary->state->src_x >> 16;
2034 crtc_resp->y = crtc->primary->state->src_y >> 16;
2035 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002036 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002037 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08002038
Daniel Vetter31c946e2015-02-22 12:24:17 +01002039 } else {
2040 crtc_resp->mode_valid = 0;
2041 }
Dave Airlief453ba02008-11-07 14:05:41 -08002042 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01002043 crtc_resp->x = crtc->x;
2044 crtc_resp->y = crtc->y;
2045 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01002046 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01002047 crtc_resp->mode_valid = 1;
2048
2049 } else {
2050 crtc_resp->mode_valid = 0;
2051 }
Dave Airlief453ba02008-11-07 14:05:41 -08002052 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002053 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08002054
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002055 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002056}
2057
Damien Lespiau61d8e322013-09-25 16:45:22 +01002058static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2059 const struct drm_file *file_priv)
2060{
2061 /*
2062 * If user-space hasn't configured the driver to expose the stereo 3D
2063 * modes, don't expose them.
2064 */
2065 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2066 return false;
2067
2068 return true;
2069}
2070
Daniel Vetterabd69c52014-11-25 23:50:05 +01002071static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2072{
2073 /* For atomic drivers only state objects are synchronously updated and
2074 * protected by modeset locks, so check those first. */
2075 if (connector->state)
2076 return connector->state->best_encoder;
2077 return connector->encoder;
2078}
2079
Rob Clark95cbf112014-12-18 16:01:49 -05002080/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002081static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002082 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2083 uint32_t *arg_count_props)
2084{
Rob Clark88a48e22014-12-18 16:01:50 -05002085 int props_count;
2086 int i, ret, copied;
2087
2088 props_count = obj->properties->count;
2089 if (!atomic)
2090 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002091
2092 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002093 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002094 struct drm_property *prop = obj->properties->properties[i];
2095 uint64_t val;
2096
Rob Clark88a48e22014-12-18 16:01:50 -05002097 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2098 continue;
2099
Rob Clark95cbf112014-12-18 16:01:49 -05002100 ret = drm_object_property_get_value(obj, prop, &val);
2101 if (ret)
2102 return ret;
2103
2104 if (put_user(prop->base.id, prop_ptr + copied))
2105 return -EFAULT;
2106
2107 if (put_user(val, prop_values + copied))
2108 return -EFAULT;
2109
2110 copied++;
2111 }
2112 }
2113 *arg_count_props = props_count;
2114
2115 return 0;
2116}
2117
Dave Airlief453ba02008-11-07 14:05:41 -08002118/**
2119 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002120 * @dev: drm device for the ioctl
2121 * @data: data pointer for the ioctl
2122 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002123 *
Dave Airlief453ba02008-11-07 14:05:41 -08002124 * Construct a connector configuration structure to return to the user.
2125 *
2126 * Called by the user via ioctl.
2127 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002128 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002129 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002130 */
2131int drm_mode_getconnector(struct drm_device *dev, void *data,
2132 struct drm_file *file_priv)
2133{
2134 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002135 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002136 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002137 struct drm_display_mode *mode;
2138 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002139 int encoders_count = 0;
2140 int ret = 0;
2141 int copied = 0;
2142 int i;
2143 struct drm_mode_modeinfo u_mode;
2144 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002145 uint32_t __user *encoder_ptr;
2146
Dave Airliefb3b06c2011-02-08 13:55:21 +10002147 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2148 return -EINVAL;
2149
Dave Airlief453ba02008-11-07 14:05:41 -08002150 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2151
Jerome Glisse94401062010-07-15 15:43:25 -04002152 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002153
Daniel Vetter7b240562012-12-12 00:35:33 +01002154 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002155
Rob Clarka2b34e22013-10-05 16:36:52 -04002156 connector = drm_connector_find(dev, out_resp->connector_id);
2157 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002158 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002159 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002160 }
Dave Airlief453ba02008-11-07 14:05:41 -08002161
Thierry Reding01073b02014-12-10 13:03:38 +01002162 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2163 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002164 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002165
2166 if (out_resp->count_modes == 0) {
2167 connector->funcs->fill_modes(connector,
2168 dev->mode_config.max_width,
2169 dev->mode_config.max_height);
2170 }
2171
2172 /* delayed so we get modes regardless of pre-fill_modes state */
2173 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002174 if (drm_mode_expose_to_userspace(mode, file_priv))
2175 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002176
2177 out_resp->connector_id = connector->base.id;
2178 out_resp->connector_type = connector->connector_type;
2179 out_resp->connector_type_id = connector->connector_type_id;
2180 out_resp->mm_width = connector->display_info.width_mm;
2181 out_resp->mm_height = connector->display_info.height_mm;
2182 out_resp->subpixel = connector->display_info.subpixel_order;
2183 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002184
2185 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002186 encoder = drm_connector_get_encoder(connector);
2187 if (encoder)
2188 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002189 else
2190 out_resp->encoder_id = 0;
2191
2192 /*
2193 * This ioctl is called twice, once to determine how much space is
2194 * needed, and the 2nd time to fill it.
2195 */
2196 if ((out_resp->count_modes >= mode_count) && mode_count) {
2197 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002198 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002199 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002200 if (!drm_mode_expose_to_userspace(mode, file_priv))
2201 continue;
2202
Daniel Stone934a8a82015-05-22 13:34:48 +01002203 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002204 if (copy_to_user(mode_ptr + copied,
2205 &u_mode, sizeof(u_mode))) {
2206 ret = -EFAULT;
2207 goto out;
2208 }
2209 copied++;
2210 }
2211 }
2212 out_resp->count_modes = mode_count;
2213
Rob Clark88a48e22014-12-18 16:01:50 -05002214 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002215 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2216 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2217 &out_resp->count_props);
2218 if (ret)
2219 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002220
2221 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2222 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002223 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002224 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2225 if (connector->encoder_ids[i] != 0) {
2226 if (put_user(connector->encoder_ids[i],
2227 encoder_ptr + copied)) {
2228 ret = -EFAULT;
2229 goto out;
2230 }
2231 copied++;
2232 }
2233 }
2234 }
2235 out_resp->count_encoders = encoders_count;
2236
2237out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002238 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002239
2240out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002241 mutex_unlock(&dev->mode_config.mutex);
2242
Dave Airlief453ba02008-11-07 14:05:41 -08002243 return ret;
2244}
2245
Daniel Vetterabd69c52014-11-25 23:50:05 +01002246static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2247{
2248 struct drm_connector *connector;
2249 struct drm_device *dev = encoder->dev;
2250 bool uses_atomic = false;
2251
2252 /* For atomic drivers only state objects are synchronously updated and
2253 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002254 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002255 if (!connector->state)
2256 continue;
2257
2258 uses_atomic = true;
2259
2260 if (connector->state->best_encoder != encoder)
2261 continue;
2262
2263 return connector->state->crtc;
2264 }
2265
2266 /* Don't return stale data (e.g. pending async disable). */
2267 if (uses_atomic)
2268 return NULL;
2269
2270 return encoder->crtc;
2271}
2272
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002273/**
2274 * drm_mode_getencoder - get encoder configuration
2275 * @dev: drm device for the ioctl
2276 * @data: data pointer for the ioctl
2277 * @file_priv: drm file for the ioctl call
2278 *
2279 * Construct a encoder configuration structure to return to the user.
2280 *
2281 * Called by the user via ioctl.
2282 *
2283 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002284 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002285 */
Dave Airlief453ba02008-11-07 14:05:41 -08002286int drm_mode_getencoder(struct drm_device *dev, void *data,
2287 struct drm_file *file_priv)
2288{
2289 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002290 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002291 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002292
Dave Airliefb3b06c2011-02-08 13:55:21 +10002293 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2294 return -EINVAL;
2295
Rob Clarka2b34e22013-10-05 16:36:52 -04002296 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002297 if (!encoder)
2298 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002299
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002300 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002301 crtc = drm_encoder_get_crtc(encoder);
2302 if (crtc)
2303 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002304 else
2305 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002306 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2307
Dave Airlief453ba02008-11-07 14:05:41 -08002308 enc_resp->encoder_type = encoder->encoder_type;
2309 enc_resp->encoder_id = encoder->base.id;
2310 enc_resp->possible_crtcs = encoder->possible_crtcs;
2311 enc_resp->possible_clones = encoder->possible_clones;
2312
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002313 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002314}
2315
2316/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002317 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002318 * @dev: DRM device
2319 * @data: ioctl data
2320 * @file_priv: DRM file info
2321 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002322 * Construct a list of plane ids to return to the user.
2323 *
2324 * Called by the user via ioctl.
2325 *
2326 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002327 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002328 */
2329int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002330 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002331{
2332 struct drm_mode_get_plane_res *plane_resp = data;
2333 struct drm_mode_config *config;
2334 struct drm_plane *plane;
2335 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002336 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002337 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002338
2339 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2340 return -EINVAL;
2341
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002342 config = &dev->mode_config;
2343
Matt Roper681e7ec2014-04-01 15:22:42 -07002344 if (file_priv->universal_planes)
2345 num_planes = config->num_total_plane;
2346 else
2347 num_planes = config->num_overlay_plane;
2348
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002349 /*
2350 * This ioctl is called twice, once to determine how much space is
2351 * needed, and the 2nd time to fill it.
2352 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002353 if (num_planes &&
2354 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002355 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002356
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002357 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002358 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002359 /*
2360 * Unless userspace set the 'universal planes'
2361 * capability bit, only advertise overlays.
2362 */
2363 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2364 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002365 continue;
2366
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002367 if (put_user(plane->base.id, plane_ptr + copied))
2368 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002369 copied++;
2370 }
2371 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002372 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002373
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002374 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002375}
2376
2377/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002378 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002379 * @dev: DRM device
2380 * @data: ioctl data
2381 * @file_priv: DRM file info
2382 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002383 * Construct a plane configuration structure to return to the user.
2384 *
2385 * Called by the user via ioctl.
2386 *
2387 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002388 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002389 */
2390int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002391 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002392{
2393 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002394 struct drm_plane *plane;
2395 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002396
2397 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2398 return -EINVAL;
2399
Rob Clarka2b34e22013-10-05 16:36:52 -04002400 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002401 if (!plane)
2402 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002403
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002404 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002405 if (plane->crtc)
2406 plane_resp->crtc_id = plane->crtc->base.id;
2407 else
2408 plane_resp->crtc_id = 0;
2409
2410 if (plane->fb)
2411 plane_resp->fb_id = plane->fb->base.id;
2412 else
2413 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002414 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002415
2416 plane_resp->plane_id = plane->base.id;
2417 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002418 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002419
2420 /*
2421 * This ioctl is called twice, once to determine how much space is
2422 * needed, and the 2nd time to fill it.
2423 */
2424 if (plane->format_count &&
2425 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002426 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002427 if (copy_to_user(format_ptr,
2428 plane->format_types,
2429 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002430 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002431 }
2432 }
2433 plane_resp->count_format_types = plane->format_count;
2434
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002435 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002436}
2437
Laurent Pinchartead86102015-03-05 02:25:43 +02002438/**
2439 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2440 * @plane: plane to check for format support
2441 * @format: the pixel format
2442 *
2443 * Returns:
2444 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2445 * otherwise.
2446 */
2447int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2448{
2449 unsigned int i;
2450
2451 for (i = 0; i < plane->format_count; i++) {
2452 if (format == plane->format_types[i])
2453 return 0;
2454 }
2455
2456 return -EINVAL;
2457}
2458
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002459static int check_src_coords(uint32_t src_x, uint32_t src_y,
2460 uint32_t src_w, uint32_t src_h,
2461 const struct drm_framebuffer *fb)
2462{
2463 unsigned int fb_width, fb_height;
2464
2465 fb_width = fb->width << 16;
2466 fb_height = fb->height << 16;
2467
2468 /* Make sure source coordinates are inside the fb. */
2469 if (src_w > fb_width ||
2470 src_x > fb_width - src_w ||
2471 src_h > fb_height ||
2472 src_y > fb_height - src_h) {
2473 DRM_DEBUG_KMS("Invalid source coordinates "
2474 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2475 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2476 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2477 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2478 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2479 return -ENOSPC;
2480 }
2481
2482 return 0;
2483}
2484
Matt Roperb36552b2014-06-10 08:28:09 -07002485/*
2486 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002487 *
Matt Roperb36552b2014-06-10 08:28:09 -07002488 * Note that we assume an extra reference has already been taken on fb. If the
2489 * update fails, this reference will be dropped before return; if it succeeds,
2490 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002491 *
Matt Roperb36552b2014-06-10 08:28:09 -07002492 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002493 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002494static int __setplane_internal(struct drm_plane *plane,
2495 struct drm_crtc *crtc,
2496 struct drm_framebuffer *fb,
2497 int32_t crtc_x, int32_t crtc_y,
2498 uint32_t crtc_w, uint32_t crtc_h,
2499 /* src_{x,y,w,h} values are 16.16 fixed point */
2500 uint32_t src_x, uint32_t src_y,
2501 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002502{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002503 int ret = 0;
2504
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002505 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002506 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002507 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002508 ret = plane->funcs->disable_plane(plane);
2509 if (!ret) {
2510 plane->crtc = NULL;
2511 plane->fb = NULL;
2512 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002513 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002514 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002515 goto out;
2516 }
2517
Matt Roper7f994f32014-05-29 08:06:51 -07002518 /* Check whether this plane is usable on this CRTC */
2519 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2520 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2521 ret = -EINVAL;
2522 goto out;
2523 }
2524
Ville Syrjälä62443be2011-12-20 00:06:47 +02002525 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002526 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2527 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002528 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2529 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002530 goto out;
2531 }
2532
Matt Roper3968be92015-04-13 11:06:13 -07002533 /* Give drivers some help against integer overflows */
2534 if (crtc_w > INT_MAX ||
2535 crtc_x > INT_MAX - (int32_t) crtc_w ||
2536 crtc_h > INT_MAX ||
2537 crtc_y > INT_MAX - (int32_t) crtc_h) {
2538 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2539 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002540 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002541 goto out;
2542 }
2543
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002544 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2545 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002546 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002547
Daniel Vetter3d30a592014-07-27 13:42:42 +02002548 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002549 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002550 crtc_x, crtc_y, crtc_w, crtc_h,
2551 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002552 if (!ret) {
2553 plane->crtc = crtc;
2554 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002555 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002556 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002557 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002558 }
2559
2560out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002561 if (fb)
2562 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002563 if (plane->old_fb)
2564 drm_framebuffer_unreference(plane->old_fb);
2565 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002566
2567 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002568}
Matt Roperb36552b2014-06-10 08:28:09 -07002569
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002570static int setplane_internal(struct drm_plane *plane,
2571 struct drm_crtc *crtc,
2572 struct drm_framebuffer *fb,
2573 int32_t crtc_x, int32_t crtc_y,
2574 uint32_t crtc_w, uint32_t crtc_h,
2575 /* src_{x,y,w,h} values are 16.16 fixed point */
2576 uint32_t src_x, uint32_t src_y,
2577 uint32_t src_w, uint32_t src_h)
2578{
2579 int ret;
2580
2581 drm_modeset_lock_all(plane->dev);
2582 ret = __setplane_internal(plane, crtc, fb,
2583 crtc_x, crtc_y, crtc_w, crtc_h,
2584 src_x, src_y, src_w, src_h);
2585 drm_modeset_unlock_all(plane->dev);
2586
2587 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002588}
2589
2590/**
2591 * drm_mode_setplane - configure a plane's configuration
2592 * @dev: DRM device
2593 * @data: ioctl data*
2594 * @file_priv: DRM file info
2595 *
2596 * Set plane configuration, including placement, fb, scaling, and other factors.
2597 * Or pass a NULL fb to disable (planes may be disabled without providing a
2598 * valid crtc).
2599 *
2600 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002601 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002602 */
2603int drm_mode_setplane(struct drm_device *dev, void *data,
2604 struct drm_file *file_priv)
2605{
2606 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002607 struct drm_plane *plane;
2608 struct drm_crtc *crtc = NULL;
2609 struct drm_framebuffer *fb = NULL;
2610
2611 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2612 return -EINVAL;
2613
Matt Roperb36552b2014-06-10 08:28:09 -07002614 /*
2615 * First, find the plane, crtc, and fb objects. If not available,
2616 * we don't bother to call the driver.
2617 */
Rob Clark933f6222014-11-25 20:33:11 -05002618 plane = drm_plane_find(dev, plane_req->plane_id);
2619 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002620 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2621 plane_req->plane_id);
2622 return -ENOENT;
2623 }
Matt Roperb36552b2014-06-10 08:28:09 -07002624
2625 if (plane_req->fb_id) {
2626 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2627 if (!fb) {
2628 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2629 plane_req->fb_id);
2630 return -ENOENT;
2631 }
2632
Rob Clark933f6222014-11-25 20:33:11 -05002633 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2634 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002635 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2636 plane_req->crtc_id);
2637 return -ENOENT;
2638 }
Matt Roperb36552b2014-06-10 08:28:09 -07002639 }
2640
Matt Roper161d0dc2014-06-10 08:28:10 -07002641 /*
2642 * setplane_internal will take care of deref'ing either the old or new
2643 * framebuffer depending on success.
2644 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002645 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002646 plane_req->crtc_x, plane_req->crtc_y,
2647 plane_req->crtc_w, plane_req->crtc_h,
2648 plane_req->src_x, plane_req->src_y,
2649 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002650}
2651
2652/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002653 * drm_mode_set_config_internal - helper to call ->set_config
2654 * @set: modeset config to set
2655 *
2656 * This is a little helper to wrap internal calls to the ->set_config driver
2657 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002658 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002659 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002660 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002661 */
2662int drm_mode_set_config_internal(struct drm_mode_set *set)
2663{
2664 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002665 struct drm_framebuffer *fb;
2666 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002667 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002668
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002669 /*
2670 * NOTE: ->set_config can also disable other crtcs (if we steal all
2671 * connectors from it), hence we need to refcount the fbs across all
2672 * crtcs. Atomic modeset will have saner semantics ...
2673 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002674 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002675 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002676
Daniel Vetterb0d12322012-12-11 01:07:12 +01002677 fb = set->fb;
2678
2679 ret = crtc->funcs->set_config(set);
2680 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002681 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002682 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002683 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002684
Daniel Vettere4f62542015-07-09 23:44:35 +02002685 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002686 if (tmp->primary->fb)
2687 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002688 if (tmp->primary->old_fb)
2689 drm_framebuffer_unreference(tmp->primary->old_fb);
2690 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002691 }
2692
2693 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002694}
2695EXPORT_SYMBOL(drm_mode_set_config_internal);
2696
Matt Roperaf936292014-04-01 15:22:34 -07002697/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002698 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2699 * @mode: mode to query
2700 * @hdisplay: hdisplay value to fill in
2701 * @vdisplay: vdisplay value to fill in
2702 *
2703 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2704 * the appropriate layout.
2705 */
2706void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2707 int *hdisplay, int *vdisplay)
2708{
2709 struct drm_display_mode adjusted;
2710
2711 drm_mode_copy(&adjusted, mode);
2712 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2713 *hdisplay = adjusted.crtc_hdisplay;
2714 *vdisplay = adjusted.crtc_vdisplay;
2715}
2716EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2717
2718/**
Matt Roperaf936292014-04-01 15:22:34 -07002719 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2720 * CRTC viewport
2721 * @crtc: CRTC that framebuffer will be displayed on
2722 * @x: x panning
2723 * @y: y panning
2724 * @mode: mode that framebuffer will be displayed under
2725 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002726 */
Matt Roperaf936292014-04-01 15:22:34 -07002727int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2728 int x, int y,
2729 const struct drm_display_mode *mode,
2730 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002731
2732{
2733 int hdisplay, vdisplay;
2734
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002735 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002736
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002737 if (crtc->state &&
2738 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2739 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002740 swap(hdisplay, vdisplay);
2741
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002742 return check_src_coords(x << 16, y << 16,
2743 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002744}
Matt Roperaf936292014-04-01 15:22:34 -07002745EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002746
Daniel Vetter2d13b672012-12-11 13:47:23 +01002747/**
Dave Airlief453ba02008-11-07 14:05:41 -08002748 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002749 * @dev: drm device for the ioctl
2750 * @data: data pointer for the ioctl
2751 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002752 *
Dave Airlief453ba02008-11-07 14:05:41 -08002753 * Build a new CRTC configuration based on user request.
2754 *
2755 * Called by the user via ioctl.
2756 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002757 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002758 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002759 */
2760int drm_mode_setcrtc(struct drm_device *dev, void *data,
2761 struct drm_file *file_priv)
2762{
2763 struct drm_mode_config *config = &dev->mode_config;
2764 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002765 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002766 struct drm_connector **connector_set = NULL, *connector;
2767 struct drm_framebuffer *fb = NULL;
2768 struct drm_display_mode *mode = NULL;
2769 struct drm_mode_set set;
2770 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002771 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002772 int i;
2773
Dave Airliefb3b06c2011-02-08 13:55:21 +10002774 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2775 return -EINVAL;
2776
Zhao Junwang01447e92015-07-07 17:08:35 +08002777 /*
2778 * Universal plane src offsets are only 16.16, prevent havoc for
2779 * drivers using universal plane code internally.
2780 */
2781 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002782 return -ERANGE;
2783
Daniel Vetter84849902012-12-02 00:28:11 +01002784 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002785 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2786 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002787 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002788 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002789 goto out;
2790 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002791 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002792
2793 if (crtc_req->mode_valid) {
2794 /* If we have a mode we need a framebuffer. */
2795 /* If we pass -1, set the mode with the currently bound fb */
2796 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002797 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002798 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2799 ret = -EINVAL;
2800 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002801 }
Matt Roperf4510a22014-04-01 15:22:40 -07002802 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002803 /* Make refcounting symmetric with the lookup path. */
2804 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002805 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002806 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2807 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002808 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2809 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002810 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002811 goto out;
2812 }
Dave Airlief453ba02008-11-07 14:05:41 -08002813 }
2814
2815 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002816 if (!mode) {
2817 ret = -ENOMEM;
2818 goto out;
2819 }
2820
Daniel Stone934a8a82015-05-22 13:34:48 +01002821 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002822 if (ret) {
2823 DRM_DEBUG_KMS("Invalid mode\n");
2824 goto out;
2825 }
2826
Dave Airlief453ba02008-11-07 14:05:41 -08002827 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002828
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002829 /*
2830 * Check whether the primary plane supports the fb pixel format.
2831 * Drivers not implementing the universal planes API use a
2832 * default formats list provided by the DRM core which doesn't
2833 * match real hardware capabilities. Skip the check in that
2834 * case.
2835 */
2836 if (!crtc->primary->format_default) {
2837 ret = drm_plane_check_pixel_format(crtc->primary,
2838 fb->pixel_format);
2839 if (ret) {
2840 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2841 drm_get_format_name(fb->pixel_format));
2842 goto out;
2843 }
2844 }
2845
Damien Lespiauc11e9282013-09-25 16:45:30 +01002846 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2847 mode, fb);
2848 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002849 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002850
Dave Airlief453ba02008-11-07 14:05:41 -08002851 }
2852
2853 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002854 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002855 ret = -EINVAL;
2856 goto out;
2857 }
2858
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002859 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002860 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002861 crtc_req->count_connectors);
2862 ret = -EINVAL;
2863 goto out;
2864 }
2865
2866 if (crtc_req->count_connectors > 0) {
2867 u32 out_id;
2868
2869 /* Avoid unbounded kernel memory allocation */
2870 if (crtc_req->count_connectors > config->num_connector) {
2871 ret = -EINVAL;
2872 goto out;
2873 }
2874
Thierry Reding2f6c5382014-12-10 13:03:36 +01002875 connector_set = kmalloc_array(crtc_req->count_connectors,
2876 sizeof(struct drm_connector *),
2877 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002878 if (!connector_set) {
2879 ret = -ENOMEM;
2880 goto out;
2881 }
2882
2883 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002884 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002885 if (get_user(out_id, &set_connectors_ptr[i])) {
2886 ret = -EFAULT;
2887 goto out;
2888 }
2889
Rob Clarka2b34e22013-10-05 16:36:52 -04002890 connector = drm_connector_find(dev, out_id);
2891 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002892 DRM_DEBUG_KMS("Connector id %d unknown\n",
2893 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002894 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002895 goto out;
2896 }
Jerome Glisse94401062010-07-15 15:43:25 -04002897 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2898 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002899 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002900
2901 connector_set[i] = connector;
2902 }
2903 }
2904
2905 set.crtc = crtc;
2906 set.x = crtc_req->x;
2907 set.y = crtc_req->y;
2908 set.mode = mode;
2909 set.connectors = connector_set;
2910 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002911 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002912 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002913
2914out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002915 if (fb)
2916 drm_framebuffer_unreference(fb);
2917
Dave Airlief453ba02008-11-07 14:05:41 -08002918 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002919 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002920 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002921 return ret;
2922}
2923
Matt Roper161d0dc2014-06-10 08:28:10 -07002924/**
2925 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2926 * universal plane handler call
2927 * @crtc: crtc to update cursor for
2928 * @req: data pointer for the ioctl
2929 * @file_priv: drm file for the ioctl call
2930 *
2931 * Legacy cursor ioctl's work directly with driver buffer handles. To
2932 * translate legacy ioctl calls into universal plane handler calls, we need to
2933 * wrap the native buffer handle in a drm_framebuffer.
2934 *
2935 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2936 * buffer with a pitch of 4*width; the universal plane interface should be used
2937 * directly in cases where the hardware can support other buffer settings and
2938 * userspace wants to make use of these capabilities.
2939 *
2940 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002941 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002942 */
2943static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2944 struct drm_mode_cursor2 *req,
2945 struct drm_file *file_priv)
2946{
2947 struct drm_device *dev = crtc->dev;
2948 struct drm_framebuffer *fb = NULL;
2949 struct drm_mode_fb_cmd2 fbreq = {
2950 .width = req->width,
2951 .height = req->height,
2952 .pixel_format = DRM_FORMAT_ARGB8888,
2953 .pitches = { req->width * 4 },
2954 .handles = { req->handle },
2955 };
2956 int32_t crtc_x, crtc_y;
2957 uint32_t crtc_w = 0, crtc_h = 0;
2958 uint32_t src_w = 0, src_h = 0;
2959 int ret = 0;
2960
2961 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002962 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002963
2964 /*
2965 * Obtain fb we'll be using (either new or existing) and take an extra
2966 * reference to it if fb != null. setplane will take care of dropping
2967 * the reference if the plane update fails.
2968 */
2969 if (req->flags & DRM_MODE_CURSOR_BO) {
2970 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002971 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002972 if (IS_ERR(fb)) {
2973 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2974 return PTR_ERR(fb);
2975 }
Matt Roper161d0dc2014-06-10 08:28:10 -07002976 } else {
2977 fb = NULL;
2978 }
2979 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002980 fb = crtc->cursor->fb;
2981 if (fb)
2982 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002983 }
2984
2985 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2986 crtc_x = req->x;
2987 crtc_y = req->y;
2988 } else {
2989 crtc_x = crtc->cursor_x;
2990 crtc_y = crtc->cursor_y;
2991 }
2992
2993 if (fb) {
2994 crtc_w = fb->width;
2995 crtc_h = fb->height;
2996 src_w = fb->width << 16;
2997 src_h = fb->height << 16;
2998 }
2999
3000 /*
3001 * setplane_internal will take care of deref'ing either the old or new
3002 * framebuffer depending on success.
3003 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003004 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07003005 crtc_x, crtc_y, crtc_w, crtc_h,
3006 0, 0, src_w, src_h);
3007
3008 /* Update successful; save new cursor position, if necessary */
3009 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3010 crtc->cursor_x = req->x;
3011 crtc->cursor_y = req->y;
3012 }
3013
3014 return ret;
3015}
3016
Dave Airlie4c813d42013-06-20 11:48:52 +10003017static int drm_mode_cursor_common(struct drm_device *dev,
3018 struct drm_mode_cursor2 *req,
3019 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003020{
Dave Airlief453ba02008-11-07 14:05:41 -08003021 struct drm_crtc *crtc;
3022 int ret = 0;
3023
Dave Airliefb3b06c2011-02-08 13:55:21 +10003024 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3025 return -EINVAL;
3026
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00003027 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08003028 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003029
Rob Clarka2b34e22013-10-05 16:36:52 -04003030 crtc = drm_crtc_find(dev, req->crtc_id);
3031 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08003032 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003033 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003034 }
Dave Airlief453ba02008-11-07 14:05:41 -08003035
Matt Roper161d0dc2014-06-10 08:28:10 -07003036 /*
3037 * If this crtc has a universal cursor plane, call that plane's update
3038 * handler rather than using legacy cursor handlers.
3039 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01003040 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02003041 if (crtc->cursor) {
3042 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3043 goto out;
3044 }
3045
Dave Airlief453ba02008-11-07 14:05:41 -08003046 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10003047 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08003048 ret = -ENXIO;
3049 goto out;
3050 }
3051 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003052 if (crtc->funcs->cursor_set2)
3053 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3054 req->width, req->height, req->hot_x, req->hot_y);
3055 else
3056 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3057 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003058 }
3059
3060 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3061 if (crtc->funcs->cursor_move) {
3062 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3063 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003064 ret = -EFAULT;
3065 goto out;
3066 }
3067 }
3068out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003069 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003070
Dave Airlief453ba02008-11-07 14:05:41 -08003071 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003072
3073}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003074
3075
3076/**
3077 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3078 * @dev: drm device for the ioctl
3079 * @data: data pointer for the ioctl
3080 * @file_priv: drm file for the ioctl call
3081 *
3082 * Set the cursor configuration based on user request.
3083 *
3084 * Called by the user via ioctl.
3085 *
3086 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003087 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003088 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003089int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003090 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003091{
3092 struct drm_mode_cursor *req = data;
3093 struct drm_mode_cursor2 new_req;
3094
3095 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3096 new_req.hot_x = new_req.hot_y = 0;
3097
3098 return drm_mode_cursor_common(dev, &new_req, file_priv);
3099}
3100
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003101/**
3102 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3103 * @dev: drm device for the ioctl
3104 * @data: data pointer for the ioctl
3105 * @file_priv: drm file for the ioctl call
3106 *
3107 * Set the cursor configuration based on user request. This implements the 2nd
3108 * version of the cursor ioctl, which allows userspace to additionally specify
3109 * the hotspot of the pointer.
3110 *
3111 * Called by the user via ioctl.
3112 *
3113 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003114 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003115 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003116int drm_mode_cursor2_ioctl(struct drm_device *dev,
3117 void *data, struct drm_file *file_priv)
3118{
3119 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003120
Dave Airlie4c813d42013-06-20 11:48:52 +10003121 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003122}
3123
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003124/**
3125 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3126 * @bpp: bits per pixels
3127 * @depth: bit depth per pixel
3128 *
3129 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3130 * Useful in fbdev emulation code, since that deals in those values.
3131 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003132uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3133{
3134 uint32_t fmt;
3135
3136 switch (bpp) {
3137 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003138 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003139 break;
3140 case 16:
3141 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003142 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003143 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003144 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003145 break;
3146 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003147 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003148 break;
3149 case 32:
3150 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003151 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003152 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003153 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003154 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003155 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003156 break;
3157 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003158 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3159 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003160 break;
3161 }
3162
3163 return fmt;
3164}
3165EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3166
Dave Airlief453ba02008-11-07 14:05:41 -08003167/**
3168 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003169 * @dev: drm device for the ioctl
3170 * @data: data pointer for the ioctl
3171 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003172 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003173 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003174 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003175 *
3176 * Called by the user via ioctl.
3177 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003178 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003179 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003180 */
3181int drm_mode_addfb(struct drm_device *dev,
3182 void *data, struct drm_file *file_priv)
3183{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003184 struct drm_mode_fb_cmd *or = data;
3185 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003186 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003187
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003188 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003189 r.fb_id = or->fb_id;
3190 r.width = or->width;
3191 r.height = or->height;
3192 r.pitches[0] = or->pitch;
3193 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3194 r.handles[0] = or->handle;
3195
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003196 ret = drm_mode_addfb2(dev, &r, file_priv);
3197 if (ret)
3198 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003199
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003200 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003201
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003202 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003203}
3204
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003205static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003206{
3207 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3208
3209 switch (format) {
3210 case DRM_FORMAT_C8:
3211 case DRM_FORMAT_RGB332:
3212 case DRM_FORMAT_BGR233:
3213 case DRM_FORMAT_XRGB4444:
3214 case DRM_FORMAT_XBGR4444:
3215 case DRM_FORMAT_RGBX4444:
3216 case DRM_FORMAT_BGRX4444:
3217 case DRM_FORMAT_ARGB4444:
3218 case DRM_FORMAT_ABGR4444:
3219 case DRM_FORMAT_RGBA4444:
3220 case DRM_FORMAT_BGRA4444:
3221 case DRM_FORMAT_XRGB1555:
3222 case DRM_FORMAT_XBGR1555:
3223 case DRM_FORMAT_RGBX5551:
3224 case DRM_FORMAT_BGRX5551:
3225 case DRM_FORMAT_ARGB1555:
3226 case DRM_FORMAT_ABGR1555:
3227 case DRM_FORMAT_RGBA5551:
3228 case DRM_FORMAT_BGRA5551:
3229 case DRM_FORMAT_RGB565:
3230 case DRM_FORMAT_BGR565:
3231 case DRM_FORMAT_RGB888:
3232 case DRM_FORMAT_BGR888:
3233 case DRM_FORMAT_XRGB8888:
3234 case DRM_FORMAT_XBGR8888:
3235 case DRM_FORMAT_RGBX8888:
3236 case DRM_FORMAT_BGRX8888:
3237 case DRM_FORMAT_ARGB8888:
3238 case DRM_FORMAT_ABGR8888:
3239 case DRM_FORMAT_RGBA8888:
3240 case DRM_FORMAT_BGRA8888:
3241 case DRM_FORMAT_XRGB2101010:
3242 case DRM_FORMAT_XBGR2101010:
3243 case DRM_FORMAT_RGBX1010102:
3244 case DRM_FORMAT_BGRX1010102:
3245 case DRM_FORMAT_ARGB2101010:
3246 case DRM_FORMAT_ABGR2101010:
3247 case DRM_FORMAT_RGBA1010102:
3248 case DRM_FORMAT_BGRA1010102:
3249 case DRM_FORMAT_YUYV:
3250 case DRM_FORMAT_YVYU:
3251 case DRM_FORMAT_UYVY:
3252 case DRM_FORMAT_VYUY:
3253 case DRM_FORMAT_AYUV:
3254 case DRM_FORMAT_NV12:
3255 case DRM_FORMAT_NV21:
3256 case DRM_FORMAT_NV16:
3257 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003258 case DRM_FORMAT_NV24:
3259 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003260 case DRM_FORMAT_YUV410:
3261 case DRM_FORMAT_YVU410:
3262 case DRM_FORMAT_YUV411:
3263 case DRM_FORMAT_YVU411:
3264 case DRM_FORMAT_YUV420:
3265 case DRM_FORMAT_YVU420:
3266 case DRM_FORMAT_YUV422:
3267 case DRM_FORMAT_YVU422:
3268 case DRM_FORMAT_YUV444:
3269 case DRM_FORMAT_YVU444:
3270 return 0;
3271 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003272 DRM_DEBUG_KMS("invalid pixel format %s\n",
3273 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003274 return -EINVAL;
3275 }
3276}
3277
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003278static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003279{
3280 int ret, hsub, vsub, num_planes, i;
3281
3282 ret = format_check(r);
3283 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003284 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3285 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003286 return ret;
3287 }
3288
3289 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3290 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3291 num_planes = drm_format_num_planes(r->pixel_format);
3292
3293 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003294 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003295 return -EINVAL;
3296 }
3297
3298 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003299 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003300 return -EINVAL;
3301 }
3302
3303 for (i = 0; i < num_planes; i++) {
3304 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003305 unsigned int height = r->height / (i != 0 ? vsub : 1);
3306 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003307
3308 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003309 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003310 return -EINVAL;
3311 }
3312
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003313 if ((uint64_t) width * cpp > UINT_MAX)
3314 return -ERANGE;
3315
3316 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3317 return -ERANGE;
3318
3319 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003320 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003321 return -EINVAL;
3322 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003323
3324 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3325 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3326 r->modifier[i], i);
3327 return -EINVAL;
3328 }
Rob Clark570655b2015-01-30 20:18:11 +05303329
3330 /* modifier specific checks: */
3331 switch (r->modifier[i]) {
3332 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3333 /* NOTE: the pitch restriction may be lifted later if it turns
3334 * out that no hw has this restriction:
3335 */
3336 if (r->pixel_format != DRM_FORMAT_NV12 ||
3337 width % 128 || height % 32 ||
3338 r->pitches[i] % 128) {
3339 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3340 return -EINVAL;
3341 }
3342 break;
3343
3344 default:
3345 break;
3346 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003347 }
3348
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003349 for (i = num_planes; i < 4; i++) {
3350 if (r->modifier[i]) {
3351 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3352 return -EINVAL;
3353 }
3354
3355 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3356 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3357 continue;
3358
3359 if (r->handles[i]) {
3360 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3361 return -EINVAL;
3362 }
3363
3364 if (r->pitches[i]) {
3365 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3366 return -EINVAL;
3367 }
3368
3369 if (r->offsets[i]) {
3370 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3371 return -EINVAL;
3372 }
3373 }
3374
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003375 return 0;
3376}
3377
Chris Wilson9a6f5132015-02-25 13:45:26 +00003378static struct drm_framebuffer *
3379internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003380 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003381 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003382{
3383 struct drm_mode_config *config = &dev->mode_config;
3384 struct drm_framebuffer *fb;
3385 int ret;
3386
Rob Clarke3eb3252015-02-05 14:41:52 +00003387 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003388 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3389 return ERR_PTR(-EINVAL);
3390 }
3391
3392 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3393 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3394 r->width, config->min_width, config->max_width);
3395 return ERR_PTR(-EINVAL);
3396 }
3397 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3398 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3399 r->height, config->min_height, config->max_height);
3400 return ERR_PTR(-EINVAL);
3401 }
3402
Rob Clarke3eb3252015-02-05 14:41:52 +00003403 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3404 !dev->mode_config.allow_fb_modifiers) {
3405 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3406 return ERR_PTR(-EINVAL);
3407 }
3408
Matt Roperc394c2b2014-06-10 08:28:08 -07003409 ret = framebuffer_check(r);
3410 if (ret)
3411 return ERR_PTR(ret);
3412
3413 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3414 if (IS_ERR(fb)) {
3415 DRM_DEBUG_KMS("could not create framebuffer\n");
3416 return fb;
3417 }
3418
Matt Roperc394c2b2014-06-10 08:28:08 -07003419 return fb;
3420}
3421
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003422/**
3423 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003424 * @dev: drm device for the ioctl
3425 * @data: data pointer for the ioctl
3426 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003427 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003428 * Add a new FB to the specified CRTC, given a user request with format. This is
3429 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3430 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003431 *
3432 * Called by the user via ioctl.
3433 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003434 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003435 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003436 */
3437int drm_mode_addfb2(struct drm_device *dev,
3438 void *data, struct drm_file *file_priv)
3439{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003440 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003441 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003442
Dave Airliefb3b06c2011-02-08 13:55:21 +10003443 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3444 return -EINVAL;
3445
Chris Wilson9a6f5132015-02-25 13:45:26 +00003446 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003447 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003448 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003449
Chris Wilson9a6f5132015-02-25 13:45:26 +00003450 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003451 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003452
3453 /* Transfer ownership to the filp for reaping on close */
3454 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003455 list_add(&fb->filp_head, &file_priv->fbs);
3456 mutex_unlock(&file_priv->fbs_lock);
3457
Matt Roperc394c2b2014-06-10 08:28:08 -07003458 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003459}
3460
3461/**
3462 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003463 * @dev: drm device for the ioctl
3464 * @data: data pointer for the ioctl
3465 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003466 *
Dave Airlief453ba02008-11-07 14:05:41 -08003467 * Remove the FB specified by the user.
3468 *
3469 * Called by the user via ioctl.
3470 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003471 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003472 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003473 */
3474int drm_mode_rmfb(struct drm_device *dev,
3475 void *data, struct drm_file *file_priv)
3476{
Dave Airlief453ba02008-11-07 14:05:41 -08003477 struct drm_framebuffer *fb = NULL;
3478 struct drm_framebuffer *fbl = NULL;
3479 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003480 int found = 0;
3481
Dave Airliefb3b06c2011-02-08 13:55:21 +10003482 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3483 return -EINVAL;
3484
Dave Airlie72fe90b2016-04-15 15:10:40 +10003485 fb = drm_framebuffer_lookup(dev, *id);
3486 if (!fb)
3487 return -ENOENT;
3488
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003489 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003490 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3491 if (fb == fbl)
3492 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003493 if (!found) {
3494 mutex_unlock(&file_priv->fbs_lock);
3495 goto fail_unref;
3496 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003497
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003498 list_del_init(&fb->filp_head);
3499 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003500
Dave Airlie72fe90b2016-04-15 15:10:40 +10003501 /* we now own the reference that was stored in the fbs list */
3502 drm_framebuffer_unreference(fb);
3503
3504 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003505 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003506
Daniel Vetter2b677e82012-12-10 21:16:05 +01003507 return 0;
3508
Dave Airlie72fe90b2016-04-15 15:10:40 +10003509fail_unref:
3510 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003511 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003512}
3513
3514/**
3515 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003516 * @dev: drm device for the ioctl
3517 * @data: data pointer for the ioctl
3518 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003519 *
Dave Airlief453ba02008-11-07 14:05:41 -08003520 * Lookup the FB given its ID and return info about it.
3521 *
3522 * Called by the user via ioctl.
3523 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003524 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003525 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003526 */
3527int drm_mode_getfb(struct drm_device *dev,
3528 void *data, struct drm_file *file_priv)
3529{
3530 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003531 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003532 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003533
Dave Airliefb3b06c2011-02-08 13:55:21 +10003534 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3535 return -EINVAL;
3536
Daniel Vetter786b99e2012-12-02 21:53:40 +01003537 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003538 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003539 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003540
3541 r->height = fb->height;
3542 r->width = fb->width;
3543 r->depth = fb->depth;
3544 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003545 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003546 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003547 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003548 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003549 ret = fb->funcs->create_handle(fb, file_priv,
3550 &r->handle);
3551 } else {
3552 /* GET_FB() is an unprivileged ioctl so we must not
3553 * return a buffer-handle to non-master processes! For
3554 * backwards-compatibility reasons, we cannot make
3555 * GET_FB() privileged, so just return an invalid handle
3556 * for non-masters. */
3557 r->handle = 0;
3558 ret = 0;
3559 }
3560 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003561 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003562 }
Dave Airlief453ba02008-11-07 14:05:41 -08003563
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003564 drm_framebuffer_unreference(fb);
3565
Dave Airlief453ba02008-11-07 14:05:41 -08003566 return ret;
3567}
3568
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003569/**
3570 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3571 * @dev: drm device for the ioctl
3572 * @data: data pointer for the ioctl
3573 * @file_priv: drm file for the ioctl call
3574 *
3575 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3576 * rectangle list. Generic userspace which does frontbuffer rendering must call
3577 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3578 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3579 *
3580 * Modesetting drivers which always update the frontbuffer do not need to
3581 * implement the corresponding ->dirty framebuffer callback.
3582 *
3583 * Called by the user via ioctl.
3584 *
3585 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003586 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003587 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003588int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3589 void *data, struct drm_file *file_priv)
3590{
3591 struct drm_clip_rect __user *clips_ptr;
3592 struct drm_clip_rect *clips = NULL;
3593 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003594 struct drm_framebuffer *fb;
3595 unsigned flags;
3596 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003597 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003598
Dave Airliefb3b06c2011-02-08 13:55:21 +10003599 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3600 return -EINVAL;
3601
Daniel Vetter786b99e2012-12-02 21:53:40 +01003602 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003603 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003604 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003605
3606 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003607 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003608
3609 if (!num_clips != !clips_ptr) {
3610 ret = -EINVAL;
3611 goto out_err1;
3612 }
3613
3614 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3615
3616 /* If userspace annotates copy, clips must come in pairs */
3617 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3618 ret = -EINVAL;
3619 goto out_err1;
3620 }
3621
3622 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003623 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3624 ret = -EINVAL;
3625 goto out_err1;
3626 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003627 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003628 if (!clips) {
3629 ret = -ENOMEM;
3630 goto out_err1;
3631 }
3632
3633 ret = copy_from_user(clips, clips_ptr,
3634 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003635 if (ret) {
3636 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003637 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003638 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003639 }
3640
3641 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003642 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3643 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003644 } else {
3645 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003646 }
3647
3648out_err2:
3649 kfree(clips);
3650out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003651 drm_framebuffer_unreference(fb);
3652
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003653 return ret;
3654}
3655
3656
Dave Airlief453ba02008-11-07 14:05:41 -08003657/**
3658 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003659 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003660 *
Dave Airlief453ba02008-11-07 14:05:41 -08003661 * Destroy all the FBs associated with @filp.
3662 *
3663 * Called by the user via ioctl.
3664 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003665 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003666 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003667 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003668void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003669{
Dave Airlief453ba02008-11-07 14:05:41 -08003670 struct drm_framebuffer *fb, *tfb;
3671
Daniel Vetter1b116292014-09-24 21:51:06 +02003672 /*
3673 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003674 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003675 * avoid upsetting lockdep since the universal cursor code adds a
3676 * framebuffer while holding mutex locks.
3677 *
3678 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3679 * locks is impossible here since no one else but this function can get
3680 * at it any more.
3681 */
Dave Airlief453ba02008-11-07 14:05:41 -08003682 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003683 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003684
Maarten Lankhorst73f75702015-09-09 16:40:57 +02003685 /* This drops the fpriv->fbs reference. */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003686 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003687 }
Dave Airlief453ba02008-11-07 14:05:41 -08003688}
3689
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003690/**
3691 * drm_property_create - create a new property type
3692 * @dev: drm device
3693 * @flags: flags specifying the property type
3694 * @name: name of the property
3695 * @num_values: number of pre-defined values
3696 *
3697 * This creates a new generic drm property which can then be attached to a drm
3698 * object with drm_object_attach_property. The returned property object must be
3699 * freed with drm_property_destroy.
3700 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003701 * Note that the DRM core keeps a per-device list of properties and that, if
3702 * drm_mode_config_cleanup() is called, it will destroy all properties created
3703 * by the driver.
3704 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003705 * Returns:
3706 * A pointer to the newly created property on success, NULL on failure.
3707 */
Dave Airlief453ba02008-11-07 14:05:41 -08003708struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3709 const char *name, int num_values)
3710{
3711 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003712 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003713
3714 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3715 if (!property)
3716 return NULL;
3717
Rob Clark98f75de2014-05-30 11:37:03 -04003718 property->dev = dev;
3719
Dave Airlief453ba02008-11-07 14:05:41 -08003720 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003721 property->values = kcalloc(num_values, sizeof(uint64_t),
3722 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003723 if (!property->values)
3724 goto fail;
3725 }
3726
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003727 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3728 if (ret)
3729 goto fail;
3730
Dave Airlief453ba02008-11-07 14:05:41 -08003731 property->flags = flags;
3732 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003733 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003734
Vinson Lee471dd2e2011-11-10 11:55:40 -08003735 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003736 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003737 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3738 }
Dave Airlief453ba02008-11-07 14:05:41 -08003739
3740 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003741
3742 WARN_ON(!drm_property_type_valid(property));
3743
Dave Airlief453ba02008-11-07 14:05:41 -08003744 return property;
3745fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003746 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003747 kfree(property);
3748 return NULL;
3749}
3750EXPORT_SYMBOL(drm_property_create);
3751
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003752/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003753 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003754 * @dev: drm device
3755 * @flags: flags specifying the property type
3756 * @name: name of the property
3757 * @props: enumeration lists with property values
3758 * @num_values: number of pre-defined values
3759 *
3760 * This creates a new generic drm property which can then be attached to a drm
3761 * object with drm_object_attach_property. The returned property object must be
3762 * freed with drm_property_destroy.
3763 *
3764 * Userspace is only allowed to set one of the predefined values for enumeration
3765 * properties.
3766 *
3767 * Returns:
3768 * A pointer to the newly created property on success, NULL on failure.
3769 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003770struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3771 const char *name,
3772 const struct drm_prop_enum_list *props,
3773 int num_values)
3774{
3775 struct drm_property *property;
3776 int i, ret;
3777
3778 flags |= DRM_MODE_PROP_ENUM;
3779
3780 property = drm_property_create(dev, flags, name, num_values);
3781 if (!property)
3782 return NULL;
3783
3784 for (i = 0; i < num_values; i++) {
3785 ret = drm_property_add_enum(property, i,
3786 props[i].type,
3787 props[i].name);
3788 if (ret) {
3789 drm_property_destroy(dev, property);
3790 return NULL;
3791 }
3792 }
3793
3794 return property;
3795}
3796EXPORT_SYMBOL(drm_property_create_enum);
3797
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003798/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003799 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003800 * @dev: drm device
3801 * @flags: flags specifying the property type
3802 * @name: name of the property
3803 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003804 * @num_props: size of the @props array
3805 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003806 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003807 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003808 * object with drm_object_attach_property. The returned property object must be
3809 * freed with drm_property_destroy.
3810 *
3811 * Compared to plain enumeration properties userspace is allowed to set any
3812 * or'ed together combination of the predefined property bitflag values
3813 *
3814 * Returns:
3815 * A pointer to the newly created property on success, NULL on failure.
3816 */
Rob Clark49e27542012-05-17 02:23:26 -06003817struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3818 int flags, const char *name,
3819 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303820 int num_props,
3821 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003822{
3823 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303824 int i, ret, index = 0;
3825 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003826
3827 flags |= DRM_MODE_PROP_BITMASK;
3828
3829 property = drm_property_create(dev, flags, name, num_values);
3830 if (!property)
3831 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303832 for (i = 0; i < num_props; i++) {
3833 if (!(supported_bits & (1ULL << props[i].type)))
3834 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003835
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303836 if (WARN_ON(index >= num_values)) {
3837 drm_property_destroy(dev, property);
3838 return NULL;
3839 }
3840
3841 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003842 props[i].type,
3843 props[i].name);
3844 if (ret) {
3845 drm_property_destroy(dev, property);
3846 return NULL;
3847 }
3848 }
3849
3850 return property;
3851}
3852EXPORT_SYMBOL(drm_property_create_bitmask);
3853
Rob Clarkebc44cf2012-09-12 22:22:31 -05003854static struct drm_property *property_create_range(struct drm_device *dev,
3855 int flags, const char *name,
3856 uint64_t min, uint64_t max)
3857{
3858 struct drm_property *property;
3859
3860 property = drm_property_create(dev, flags, name, 2);
3861 if (!property)
3862 return NULL;
3863
3864 property->values[0] = min;
3865 property->values[1] = max;
3866
3867 return property;
3868}
3869
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003870/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003871 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003872 * @dev: drm device
3873 * @flags: flags specifying the property type
3874 * @name: name of the property
3875 * @min: minimum value of the property
3876 * @max: maximum value of the property
3877 *
3878 * This creates a new generic drm property which can then be attached to a drm
3879 * object with drm_object_attach_property. The returned property object must be
3880 * freed with drm_property_destroy.
3881 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003882 * Userspace is allowed to set any unsigned integer value in the (min, max)
3883 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003884 *
3885 * Returns:
3886 * A pointer to the newly created property on success, NULL on failure.
3887 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003888struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3889 const char *name,
3890 uint64_t min, uint64_t max)
3891{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003892 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3893 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003894}
3895EXPORT_SYMBOL(drm_property_create_range);
3896
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003897/**
3898 * drm_property_create_signed_range - create a new signed ranged property type
3899 * @dev: drm device
3900 * @flags: flags specifying the property type
3901 * @name: name of the property
3902 * @min: minimum value of the property
3903 * @max: maximum value of the property
3904 *
3905 * This creates a new generic drm property which can then be attached to a drm
3906 * object with drm_object_attach_property. The returned property object must be
3907 * freed with drm_property_destroy.
3908 *
3909 * Userspace is allowed to set any signed integer value in the (min, max)
3910 * range inclusive.
3911 *
3912 * Returns:
3913 * A pointer to the newly created property on success, NULL on failure.
3914 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003915struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3916 int flags, const char *name,
3917 int64_t min, int64_t max)
3918{
3919 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3920 name, I642U64(min), I642U64(max));
3921}
3922EXPORT_SYMBOL(drm_property_create_signed_range);
3923
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003924/**
3925 * drm_property_create_object - create a new object property type
3926 * @dev: drm device
3927 * @flags: flags specifying the property type
3928 * @name: name of the property
3929 * @type: object type from DRM_MODE_OBJECT_* defines
3930 *
3931 * This creates a new generic drm property which can then be attached to a drm
3932 * object with drm_object_attach_property. The returned property object must be
3933 * freed with drm_property_destroy.
3934 *
3935 * Userspace is only allowed to set this to any property value of the given
3936 * @type. Only useful for atomic properties, which is enforced.
3937 *
3938 * Returns:
3939 * A pointer to the newly created property on success, NULL on failure.
3940 */
Rob Clark98f75de2014-05-30 11:37:03 -04003941struct drm_property *drm_property_create_object(struct drm_device *dev,
3942 int flags, const char *name, uint32_t type)
3943{
3944 struct drm_property *property;
3945
3946 flags |= DRM_MODE_PROP_OBJECT;
3947
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003948 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3949 return NULL;
3950
Rob Clark98f75de2014-05-30 11:37:03 -04003951 property = drm_property_create(dev, flags, name, 1);
3952 if (!property)
3953 return NULL;
3954
3955 property->values[0] = type;
3956
3957 return property;
3958}
3959EXPORT_SYMBOL(drm_property_create_object);
3960
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003961/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003962 * drm_property_create_bool - create a new boolean property type
3963 * @dev: drm device
3964 * @flags: flags specifying the property type
3965 * @name: name of the property
3966 *
3967 * This creates a new generic drm property which can then be attached to a drm
3968 * object with drm_object_attach_property. The returned property object must be
3969 * freed with drm_property_destroy.
3970 *
3971 * This is implemented as a ranged property with only {0, 1} as valid values.
3972 *
3973 * Returns:
3974 * A pointer to the newly created property on success, NULL on failure.
3975 */
3976struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3977 const char *name)
3978{
3979 return drm_property_create_range(dev, flags, name, 0, 1);
3980}
3981EXPORT_SYMBOL(drm_property_create_bool);
3982
3983/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003984 * drm_property_add_enum - add a possible value to an enumeration property
3985 * @property: enumeration property to change
3986 * @index: index of the new enumeration
3987 * @value: value of the new enumeration
3988 * @name: symbolic name of the new enumeration
3989 *
3990 * This functions adds enumerations to a property.
3991 *
3992 * It's use is deprecated, drivers should use one of the more specific helpers
3993 * to directly create the property with all enumerations already attached.
3994 *
3995 * Returns:
3996 * Zero on success, error code on failure.
3997 */
Dave Airlief453ba02008-11-07 14:05:41 -08003998int drm_property_add_enum(struct drm_property *property, int index,
3999 uint64_t value, const char *name)
4000{
4001 struct drm_property_enum *prop_enum;
4002
Rob Clark5ea22f22014-05-30 11:34:01 -04004003 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4004 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06004005 return -EINVAL;
4006
4007 /*
4008 * Bitmask enum properties have the additional constraint of values
4009 * from 0 to 63
4010 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004011 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4012 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004013 return -EINVAL;
4014
Daniel Vetter3758b342014-11-19 18:38:10 +01004015 if (!list_empty(&property->enum_list)) {
4016 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004017 if (prop_enum->value == value) {
4018 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4019 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4020 return 0;
4021 }
4022 }
4023 }
4024
4025 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4026 if (!prop_enum)
4027 return -ENOMEM;
4028
4029 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4030 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4031 prop_enum->value = value;
4032
4033 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004034 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004035 return 0;
4036}
4037EXPORT_SYMBOL(drm_property_add_enum);
4038
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004039/**
4040 * drm_property_destroy - destroy a drm property
4041 * @dev: drm device
4042 * @property: property to destry
4043 *
4044 * This function frees a property including any attached resources like
4045 * enumeration values.
4046 */
Dave Airlief453ba02008-11-07 14:05:41 -08004047void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4048{
4049 struct drm_property_enum *prop_enum, *pt;
4050
Daniel Vetter3758b342014-11-19 18:38:10 +01004051 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004052 list_del(&prop_enum->head);
4053 kfree(prop_enum);
4054 }
4055
4056 if (property->num_values)
4057 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004058 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004059 list_del(&property->head);
4060 kfree(property);
4061}
4062EXPORT_SYMBOL(drm_property_destroy);
4063
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004064/**
4065 * drm_object_attach_property - attach a property to a modeset object
4066 * @obj: drm modeset object
4067 * @property: property to attach
4068 * @init_val: initial value of the property
4069 *
4070 * This attaches the given property to the modeset object with the given initial
4071 * value. Currently this function cannot fail since the properties are stored in
4072 * a statically sized array.
4073 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004074void drm_object_attach_property(struct drm_mode_object *obj,
4075 struct drm_property *property,
4076 uint64_t init_val)
4077{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004078 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004079
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004080 if (count == DRM_OBJECT_MAX_PROPERTY) {
4081 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4082 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4083 "you see this message on the same object type.\n",
4084 obj->type);
4085 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004086 }
4087
Rob Clarkb17cd752014-12-16 18:05:30 -05004088 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004089 obj->properties->values[count] = init_val;
4090 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004091 if (property->flags & DRM_MODE_PROP_ATOMIC)
4092 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004093}
4094EXPORT_SYMBOL(drm_object_attach_property);
4095
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004096/**
4097 * drm_object_property_set_value - set the value of a property
4098 * @obj: drm mode object to set property value for
4099 * @property: property to set
4100 * @val: value the property should be set to
4101 *
4102 * This functions sets a given property on a given object. This function only
4103 * changes the software state of the property, it does not call into the
4104 * driver's ->set_property callback.
4105 *
4106 * Returns:
4107 * Zero on success, error code on failure.
4108 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004109int drm_object_property_set_value(struct drm_mode_object *obj,
4110 struct drm_property *property, uint64_t val)
4111{
4112 int i;
4113
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004114 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004115 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004116 obj->properties->values[i] = val;
4117 return 0;
4118 }
4119 }
4120
4121 return -EINVAL;
4122}
4123EXPORT_SYMBOL(drm_object_property_set_value);
4124
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004125/**
4126 * drm_object_property_get_value - retrieve the value of a property
4127 * @obj: drm mode object to get property value from
4128 * @property: property to retrieve
4129 * @val: storage for the property value
4130 *
4131 * This function retrieves the softare state of the given property for the given
4132 * property. Since there is no driver callback to retrieve the current property
4133 * value this might be out of sync with the hardware, depending upon the driver
4134 * and property.
4135 *
4136 * Returns:
4137 * Zero on success, error code on failure.
4138 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004139int drm_object_property_get_value(struct drm_mode_object *obj,
4140 struct drm_property *property, uint64_t *val)
4141{
4142 int i;
4143
Rob Clark88a48e22014-12-18 16:01:50 -05004144 /* read-only properties bypass atomic mechanism and still store
4145 * their value in obj->properties->values[].. mostly to avoid
4146 * having to deal w/ EDID and similar props in atomic paths:
4147 */
4148 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4149 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4150 return drm_atomic_get_property(obj, property, val);
4151
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004152 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004153 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004154 *val = obj->properties->values[i];
4155 return 0;
4156 }
4157 }
4158
4159 return -EINVAL;
4160}
4161EXPORT_SYMBOL(drm_object_property_get_value);
4162
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004163/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004164 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004165 * @dev: DRM device
4166 * @data: ioctl data
4167 * @file_priv: DRM file info
4168 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004169 * This function retrieves the metadata for a given property, like the different
4170 * possible values for an enum property or the limits for a range property.
4171 *
4172 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004173 *
4174 * Called by the user via ioctl.
4175 *
4176 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004177 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004178 */
Dave Airlief453ba02008-11-07 14:05:41 -08004179int drm_mode_getproperty_ioctl(struct drm_device *dev,
4180 void *data, struct drm_file *file_priv)
4181{
Dave Airlief453ba02008-11-07 14:05:41 -08004182 struct drm_mode_get_property *out_resp = data;
4183 struct drm_property *property;
4184 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004185 int value_count = 0;
4186 int ret = 0, i;
4187 int copied;
4188 struct drm_property_enum *prop_enum;
4189 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004190 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004191
Dave Airliefb3b06c2011-02-08 13:55:21 +10004192 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4193 return -EINVAL;
4194
Daniel Vetter84849902012-12-02 00:28:11 +01004195 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004196 property = drm_property_find(dev, out_resp->prop_id);
4197 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004198 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004199 goto done;
4200 }
Dave Airlief453ba02008-11-07 14:05:41 -08004201
Rob Clark5ea22f22014-05-30 11:34:01 -04004202 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4203 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004204 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004205 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004206 }
4207
4208 value_count = property->num_values;
4209
4210 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4211 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4212 out_resp->flags = property->flags;
4213
4214 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004215 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004216 for (i = 0; i < value_count; i++) {
4217 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4218 ret = -EFAULT;
4219 goto done;
4220 }
4221 }
4222 }
4223 out_resp->count_values = value_count;
4224
Rob Clark5ea22f22014-05-30 11:34:01 -04004225 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4226 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004227 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4228 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004229 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004230 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004231
4232 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4233 ret = -EFAULT;
4234 goto done;
4235 }
4236
4237 if (copy_to_user(&enum_ptr[copied].name,
4238 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4239 ret = -EFAULT;
4240 goto done;
4241 }
4242 copied++;
4243 }
4244 }
4245 out_resp->count_enum_blobs = enum_count;
4246 }
4247
Daniel Vetter3758b342014-11-19 18:38:10 +01004248 /*
4249 * NOTE: The idea seems to have been to use this to read all the blob
4250 * property values. But nothing ever added them to the corresponding
4251 * list, userspace always used the special-purpose get_blob ioctl to
4252 * read the value for a blob property. It also doesn't make a lot of
4253 * sense to return values here when everything else is just metadata for
4254 * the property itself.
4255 */
4256 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4257 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004258done:
Daniel Vetter84849902012-12-02 00:28:11 +01004259 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004260 return ret;
4261}
4262
Daniel Stone99531d92015-05-22 13:34:49 +01004263/**
4264 * drm_property_create_blob - Create new blob property
4265 *
4266 * Creates a new blob property for a specified DRM device, optionally
4267 * copying data.
4268 *
4269 * @dev: DRM device to create property for
4270 * @length: Length to allocate for blob data
4271 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004272 *
4273 * Returns:
4274 * New blob property with a single reference on success, or an ERR_PTR
4275 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004276 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004277struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004278drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004279 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004280{
4281 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004282 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004283
Dan Carpenter9ac09342015-10-29 16:37:54 +03004284 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004285 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004286
4287 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4288 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004289 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004290
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004291 /* This must be explicitly initialised, so we can safely call list_del
4292 * on it in the removal handler, even if it isn't in a file list. */
4293 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004294 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004295 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004296
Daniel Stone99531d92015-05-22 13:34:49 +01004297 if (data)
4298 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004299
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004300 mutex_lock(&dev->mode_config.blob_lock);
4301
4302 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4303 if (ret) {
4304 kfree(blob);
4305 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004306 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004307 }
4308
Daniel Stone6bcacf52015-04-20 19:22:55 +01004309 kref_init(&blob->refcount);
4310
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004311 list_add_tail(&blob->head_global,
4312 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004313
4314 mutex_unlock(&dev->mode_config.blob_lock);
4315
Dave Airlief453ba02008-11-07 14:05:41 -08004316 return blob;
4317}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004318EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004319
Daniel Stone6bcacf52015-04-20 19:22:55 +01004320/**
4321 * drm_property_free_blob - Blob property destructor
4322 *
4323 * Internal free function for blob properties; must not be used directly.
4324 *
Daniel Stonef102c162015-05-22 13:34:44 +01004325 * @kref: Reference
Daniel Stone6bcacf52015-04-20 19:22:55 +01004326 */
4327static void drm_property_free_blob(struct kref *kref)
Dave Airlief453ba02008-11-07 14:05:41 -08004328{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004329 struct drm_property_blob *blob =
4330 container_of(kref, struct drm_property_blob, refcount);
4331
4332 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4333
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004334 list_del(&blob->head_global);
4335 list_del(&blob->head_file);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004336 drm_mode_object_unregister(blob->dev, &blob->base);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004337
Dave Airlief453ba02008-11-07 14:05:41 -08004338 kfree(blob);
4339}
4340
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004341/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004342 * drm_property_unreference_blob - Unreference a blob property
4343 *
4344 * Drop a reference on a blob property. May free the object.
4345 *
Daniel Stonef102c162015-05-22 13:34:44 +01004346 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004347 */
4348void drm_property_unreference_blob(struct drm_property_blob *blob)
4349{
4350 struct drm_device *dev;
4351
4352 if (!blob)
4353 return;
4354
4355 dev = blob->dev;
4356
4357 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4358
4359 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4360 &dev->mode_config.blob_lock))
4361 mutex_unlock(&dev->mode_config.blob_lock);
4362 else
4363 might_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004364}
4365EXPORT_SYMBOL(drm_property_unreference_blob);
4366
4367/**
4368 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4369 *
4370 * Drop a reference on a blob property. May free the object. This must be
4371 * called with blob_lock held.
4372 *
Daniel Stonef102c162015-05-22 13:34:44 +01004373 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004374 */
4375static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4376{
4377 if (!blob)
4378 return;
4379
4380 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4381
4382 kref_put(&blob->refcount, drm_property_free_blob);
4383}
4384
4385/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004386 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4387 * @dev: DRM device
4388 * @file_priv: destroy all blobs owned by this file handle
4389 */
4390void drm_property_destroy_user_blobs(struct drm_device *dev,
4391 struct drm_file *file_priv)
4392{
4393 struct drm_property_blob *blob, *bt;
4394
4395 mutex_lock(&dev->mode_config.blob_lock);
4396
4397 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4398 list_del_init(&blob->head_file);
4399 drm_property_unreference_blob_locked(blob);
4400 }
4401
4402 mutex_unlock(&dev->mode_config.blob_lock);
4403}
4404
4405/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004406 * drm_property_reference_blob - Take a reference on an existing property
4407 *
4408 * Take a new reference on an existing blob property.
4409 *
Daniel Stonef102c162015-05-22 13:34:44 +01004410 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004411 */
4412struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4413{
4414 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4415 kref_get(&blob->refcount);
4416 return blob;
4417}
4418EXPORT_SYMBOL(drm_property_reference_blob);
4419
4420/*
4421 * Like drm_property_lookup_blob, but does not return an additional reference.
4422 * Must be called with blob_lock held.
4423 */
4424static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4425 uint32_t id)
4426{
4427 struct drm_mode_object *obj = NULL;
4428 struct drm_property_blob *blob;
4429
4430 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4431
4432 mutex_lock(&dev->mode_config.idr_mutex);
4433 obj = idr_find(&dev->mode_config.crtc_idr, id);
4434 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4435 blob = NULL;
4436 else
4437 blob = obj_to_blob(obj);
4438 mutex_unlock(&dev->mode_config.idr_mutex);
4439
Dave Airlief453ba02008-11-07 14:05:41 -08004440 return blob;
4441}
4442
Daniel Stone6bcacf52015-04-20 19:22:55 +01004443/**
4444 * drm_property_lookup_blob - look up a blob property and take a reference
4445 * @dev: drm device
4446 * @id: id of the blob property
4447 *
4448 * If successful, this takes an additional reference to the blob property.
4449 * callers need to make sure to eventually unreference the returned property
4450 * again, using @drm_property_unreference_blob.
4451 */
4452struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4453 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004454{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004455 struct drm_property_blob *blob;
4456
4457 mutex_lock(&dev->mode_config.blob_lock);
4458 blob = __drm_property_lookup_blob(dev, id);
4459 if (blob) {
4460 if (!kref_get_unless_zero(&blob->refcount))
4461 blob = NULL;
4462 }
4463 mutex_unlock(&dev->mode_config.blob_lock);
4464
4465 return blob;
4466}
4467EXPORT_SYMBOL(drm_property_lookup_blob);
4468
4469/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004470 * drm_property_replace_global_blob - atomically replace existing blob property
4471 * @dev: drm device
4472 * @replace: location of blob property pointer to be replaced
4473 * @length: length of data for new blob, or 0 for no data
4474 * @data: content for new blob, or NULL for no data
4475 * @obj_holds_id: optional object for property holding blob ID
4476 * @prop_holds_id: optional property holding blob ID
4477 * @return 0 on success or error on failure
4478 *
4479 * This function will atomically replace a global property in the blob list,
4480 * optionally updating a property which holds the ID of that property. It is
4481 * guaranteed to be atomic: no caller will be allowed to see intermediate
4482 * results, and either the entire operation will succeed and clean up the
4483 * previous property, or it will fail and the state will be unchanged.
4484 *
4485 * If length is 0 or data is NULL, no new blob will be created, and the holding
4486 * property, if specified, will be set to 0.
4487 *
4488 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4489 * by holding the relevant modesetting object lock for its parent.
4490 *
4491 * For example, a drm_connector has a 'PATH' property, which contains the ID
4492 * of a blob property with the value of the MST path information. Calling this
4493 * function with replace pointing to the connector's path_blob_ptr, length and
4494 * data set for the new path information, obj_holds_id set to the connector's
4495 * base object, and prop_holds_id set to the path property name, will perform
4496 * a completely atomic update. The access to path_blob_ptr is protected by the
4497 * caller holding a lock on the connector.
4498 */
4499static int drm_property_replace_global_blob(struct drm_device *dev,
4500 struct drm_property_blob **replace,
4501 size_t length,
4502 const void *data,
4503 struct drm_mode_object *obj_holds_id,
4504 struct drm_property *prop_holds_id)
4505{
4506 struct drm_property_blob *new_blob = NULL;
4507 struct drm_property_blob *old_blob = NULL;
4508 int ret;
4509
4510 WARN_ON(replace == NULL);
4511
4512 old_blob = *replace;
4513
4514 if (length && data) {
4515 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004516 if (IS_ERR(new_blob))
4517 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004518 }
4519
4520 /* This does not need to be synchronised with blob_lock, as the
4521 * get_properties ioctl locks all modesetting objects, and
4522 * obj_holds_id must be locked before calling here, so we cannot
4523 * have its value out of sync with the list membership modified
4524 * below under blob_lock. */
4525 if (obj_holds_id) {
4526 ret = drm_object_property_set_value(obj_holds_id,
4527 prop_holds_id,
4528 new_blob ?
4529 new_blob->base.id : 0);
4530 if (ret != 0)
4531 goto err_created;
4532 }
4533
Markus Elfringec530822015-07-05 21:55:10 +02004534 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004535 *replace = new_blob;
4536
4537 return 0;
4538
4539err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004540 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004541 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004542}
4543
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004544/**
4545 * drm_mode_getblob_ioctl - get the contents of a blob property value
4546 * @dev: DRM device
4547 * @data: ioctl data
4548 * @file_priv: DRM file info
4549 *
4550 * This function retrieves the contents of a blob property. The value stored in
4551 * an object's blob property is just a normal modeset object id.
4552 *
4553 * Called by the user via ioctl.
4554 *
4555 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004556 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004557 */
Dave Airlief453ba02008-11-07 14:05:41 -08004558int drm_mode_getblob_ioctl(struct drm_device *dev,
4559 void *data, struct drm_file *file_priv)
4560{
Dave Airlief453ba02008-11-07 14:05:41 -08004561 struct drm_mode_get_blob *out_resp = data;
4562 struct drm_property_blob *blob;
4563 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004564 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004565
Dave Airliefb3b06c2011-02-08 13:55:21 +10004566 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4567 return -EINVAL;
4568
Daniel Vetter84849902012-12-02 00:28:11 +01004569 drm_modeset_lock_all(dev);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004570 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004571 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04004572 if (!blob) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004573 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004574 goto done;
4575 }
Dave Airlief453ba02008-11-07 14:05:41 -08004576
4577 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004578 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004579 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004580 ret = -EFAULT;
4581 goto done;
4582 }
4583 }
4584 out_resp->length = blob->length;
4585
4586done:
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004587 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter84849902012-12-02 00:28:11 +01004588 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004589 return ret;
4590}
4591
Dave Airliecc7096f2014-10-22 12:03:04 +10004592/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004593 * drm_mode_createblob_ioctl - create a new blob property
4594 * @dev: DRM device
4595 * @data: ioctl data
4596 * @file_priv: DRM file info
4597 *
4598 * This function creates a new blob property with user-defined values. In order
4599 * to give us sensible validation and checking when creating, rather than at
4600 * every potential use, we also require a type to be provided upfront.
4601 *
4602 * Called by the user via ioctl.
4603 *
4604 * Returns:
4605 * Zero on success, negative errno on failure.
4606 */
4607int drm_mode_createblob_ioctl(struct drm_device *dev,
4608 void *data, struct drm_file *file_priv)
4609{
4610 struct drm_mode_create_blob *out_resp = data;
4611 struct drm_property_blob *blob;
4612 void __user *blob_ptr;
4613 int ret = 0;
4614
4615 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4616 return -EINVAL;
4617
4618 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4619 if (IS_ERR(blob))
4620 return PTR_ERR(blob);
4621
4622 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4623 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4624 ret = -EFAULT;
4625 goto out_blob;
4626 }
4627
4628 /* Dropping the lock between create_blob and our access here is safe
4629 * as only the same file_priv can remove the blob; at this point, it is
4630 * not associated with any file_priv. */
4631 mutex_lock(&dev->mode_config.blob_lock);
4632 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004633 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004634 mutex_unlock(&dev->mode_config.blob_lock);
4635
4636 return 0;
4637
4638out_blob:
4639 drm_property_unreference_blob(blob);
4640 return ret;
4641}
4642
4643/**
4644 * drm_mode_destroyblob_ioctl - destroy a user blob property
4645 * @dev: DRM device
4646 * @data: ioctl data
4647 * @file_priv: DRM file info
4648 *
4649 * Destroy an existing user-defined blob property.
4650 *
4651 * Called by the user via ioctl.
4652 *
4653 * Returns:
4654 * Zero on success, negative errno on failure.
4655 */
4656int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4657 void *data, struct drm_file *file_priv)
4658{
4659 struct drm_mode_destroy_blob *out_resp = data;
4660 struct drm_property_blob *blob = NULL, *bt;
4661 bool found = false;
4662 int ret = 0;
4663
4664 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4665 return -EINVAL;
4666
4667 mutex_lock(&dev->mode_config.blob_lock);
4668 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4669 if (!blob) {
4670 ret = -ENOENT;
4671 goto err;
4672 }
4673
4674 /* Ensure the property was actually created by this user. */
4675 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4676 if (bt == blob) {
4677 found = true;
4678 break;
4679 }
4680 }
4681
4682 if (!found) {
4683 ret = -EPERM;
4684 goto err;
4685 }
4686
4687 /* We must drop head_file here, because we may not be the last
4688 * reference on the blob. */
4689 list_del_init(&blob->head_file);
4690 drm_property_unreference_blob_locked(blob);
4691 mutex_unlock(&dev->mode_config.blob_lock);
4692
4693 return 0;
4694
4695err:
4696 mutex_unlock(&dev->mode_config.blob_lock);
4697 return ret;
4698}
4699
4700/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004701 * drm_mode_connector_set_path_property - set tile property on connector
4702 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004703 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004704 *
4705 * This creates a property to expose to userspace to specify a
4706 * connector path. This is mainly used for DisplayPort MST where
4707 * connectors have a topology and we want to allow userspace to give
4708 * them more meaningful names.
4709 *
4710 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004711 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004712 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004713int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004714 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004715{
4716 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004717 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004718
Daniel Stoned2ed3432015-04-20 19:22:53 +01004719 ret = drm_property_replace_global_blob(dev,
4720 &connector->path_blob_ptr,
4721 strlen(path) + 1,
4722 path,
4723 &connector->base,
4724 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004725 return ret;
4726}
4727EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4728
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004729/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004730 * drm_mode_connector_set_tile_property - set tile property on connector
4731 * @connector: connector to set property on.
4732 *
4733 * This looks up the tile information for a connector, and creates a
4734 * property for userspace to parse if it exists. The property is of
4735 * the form of 8 integers using ':' as a separator.
4736 *
4737 * Returns:
4738 * Zero on success, errno on failure.
4739 */
4740int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4741{
4742 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004743 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004744 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004745
4746 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004747 ret = drm_property_replace_global_blob(dev,
4748 &connector->tile_blob_ptr,
4749 0,
4750 NULL,
4751 &connector->base,
4752 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004753 return ret;
4754 }
4755
4756 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4757 connector->tile_group->id, connector->tile_is_single_monitor,
4758 connector->num_h_tile, connector->num_v_tile,
4759 connector->tile_h_loc, connector->tile_v_loc,
4760 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004761
Daniel Stoned2ed3432015-04-20 19:22:53 +01004762 ret = drm_property_replace_global_blob(dev,
4763 &connector->tile_blob_ptr,
4764 strlen(tile) + 1,
4765 tile,
4766 &connector->base,
4767 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004768 return ret;
4769}
4770EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4771
4772/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004773 * drm_mode_connector_update_edid_property - update the edid property of a connector
4774 * @connector: drm connector
4775 * @edid: new value of the edid property
4776 *
4777 * This function creates a new blob modeset object and assigns its id to the
4778 * connector's edid property.
4779 *
4780 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004781 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004782 */
Dave Airlief453ba02008-11-07 14:05:41 -08004783int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004784 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004785{
4786 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004787 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004788 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004789
Thomas Wood4cf2b282014-06-18 17:52:33 +01004790 /* ignore requests to set edid when overridden */
4791 if (connector->override_edid)
4792 return 0;
4793
Daniel Stoned2ed3432015-04-20 19:22:53 +01004794 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004795 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004796
Daniel Stoned2ed3432015-04-20 19:22:53 +01004797 ret = drm_property_replace_global_blob(dev,
4798 &connector->edid_blob_ptr,
4799 size,
4800 edid,
4801 &connector->base,
4802 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004803 return ret;
4804}
4805EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4806
Rob Clark3843e712014-12-16 18:05:29 -05004807/* Some properties could refer to dynamic refcnt'd objects, or things that
4808 * need special locking to handle lifetime issues (ie. to ensure the prop
4809 * value doesn't become invalid part way through the property update due to
4810 * race). The value returned by reference via 'obj' should be passed back
4811 * to drm_property_change_valid_put() after the property is set (and the
4812 * object to which the property is attached has a chance to take it's own
4813 * reference).
4814 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004815bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004816 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004817{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004818 int i;
4819
Paulo Zanoni26a34812012-05-15 18:08:59 -03004820 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4821 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004822
Rob Clark3843e712014-12-16 18:05:29 -05004823 *ref = NULL;
4824
Rob Clark5ea22f22014-05-30 11:34:01 -04004825 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004826 if (value < property->values[0] || value > property->values[1])
4827 return false;
4828 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004829 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4830 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004831
Rob Clarkebc44cf2012-09-12 22:22:31 -05004832 if (svalue < U642I64(property->values[0]) ||
4833 svalue > U642I64(property->values[1]))
4834 return false;
4835 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004836 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004837 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004838
Rob Clark49e27542012-05-17 02:23:26 -06004839 for (i = 0; i < property->num_values; i++)
4840 valid_mask |= (1ULL << property->values[i]);
4841 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004842 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004843 struct drm_property_blob *blob;
4844
4845 if (value == 0)
4846 return true;
4847
4848 blob = drm_property_lookup_blob(property->dev, value);
4849 if (blob) {
4850 *ref = &blob->base;
4851 return true;
4852 } else {
4853 return false;
4854 }
Rob Clark98f75de2014-05-30 11:37:03 -04004855 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004856 /* a zero value for an object property translates to null: */
4857 if (value == 0)
4858 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004859
4860 /* handle refcnt'd objects specially: */
4861 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4862 struct drm_framebuffer *fb;
4863 fb = drm_framebuffer_lookup(property->dev, value);
4864 if (fb) {
4865 *ref = &fb->base;
4866 return true;
4867 } else {
4868 return false;
4869 }
4870 } else {
4871 return _object_find(property->dev, value, property->values[0]) != NULL;
4872 }
Paulo Zanoni26a34812012-05-15 18:08:59 -03004873 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004874
4875 for (i = 0; i < property->num_values; i++)
4876 if (property->values[i] == value)
4877 return true;
4878 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004879}
4880
Rob Clarkd34f20d2014-12-18 16:01:56 -05004881void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004882 struct drm_mode_object *ref)
4883{
4884 if (!ref)
4885 return;
4886
4887 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4888 if (property->values[0] == DRM_MODE_OBJECT_FB)
4889 drm_framebuffer_unreference(obj_to_fb(ref));
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004890 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4891 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004892}
4893
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004894/**
4895 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4896 * @dev: DRM device
4897 * @data: ioctl data
4898 * @file_priv: DRM file info
4899 *
4900 * This function sets the current value for a connectors's property. It also
4901 * calls into a driver's ->set_property callback to update the hardware state
4902 *
4903 * Called by the user via ioctl.
4904 *
4905 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004906 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004907 */
Dave Airlief453ba02008-11-07 14:05:41 -08004908int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4909 void *data, struct drm_file *file_priv)
4910{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004911 struct drm_mode_connector_set_property *conn_set_prop = data;
4912 struct drm_mode_obj_set_property obj_set_prop = {
4913 .value = conn_set_prop->value,
4914 .prop_id = conn_set_prop->prop_id,
4915 .obj_id = conn_set_prop->connector_id,
4916 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4917 };
Dave Airlief453ba02008-11-07 14:05:41 -08004918
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004919 /* It does all the locking and checking we need */
4920 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004921}
4922
Paulo Zanonic5431882012-05-15 18:09:02 -03004923static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4924 struct drm_property *property,
4925 uint64_t value)
4926{
4927 int ret = -EINVAL;
4928 struct drm_connector *connector = obj_to_connector(obj);
4929
4930 /* Do DPMS ourselves */
4931 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004932 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004933 } else if (connector->funcs->set_property)
4934 ret = connector->funcs->set_property(connector, property, value);
4935
4936 /* store the property value if successful */
4937 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004938 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004939 return ret;
4940}
4941
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004942static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4943 struct drm_property *property,
4944 uint64_t value)
4945{
4946 int ret = -EINVAL;
4947 struct drm_crtc *crtc = obj_to_crtc(obj);
4948
4949 if (crtc->funcs->set_property)
4950 ret = crtc->funcs->set_property(crtc, property, value);
4951 if (!ret)
4952 drm_object_property_set_value(obj, property, value);
4953
4954 return ret;
4955}
4956
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004957/**
4958 * drm_mode_plane_set_obj_prop - set the value of a property
4959 * @plane: drm plane object to set property value for
4960 * @property: property to set
4961 * @value: value the property should be set to
4962 *
4963 * This functions sets a given property on a given plane object. This function
4964 * calls the driver's ->set_property callback and changes the software state of
4965 * the property if the callback succeeds.
4966 *
4967 * Returns:
4968 * Zero on success, error code on failure.
4969 */
4970int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4971 struct drm_property *property,
4972 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004973{
4974 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004975 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004976
4977 if (plane->funcs->set_property)
4978 ret = plane->funcs->set_property(plane, property, value);
4979 if (!ret)
4980 drm_object_property_set_value(obj, property, value);
4981
4982 return ret;
4983}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004984EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004985
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004986/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004987 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004988 * @dev: DRM device
4989 * @data: ioctl data
4990 * @file_priv: DRM file info
4991 *
4992 * This function retrieves the current value for an object's property. Compared
4993 * to the connector specific ioctl this one is extended to also work on crtc and
4994 * plane objects.
4995 *
4996 * Called by the user via ioctl.
4997 *
4998 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004999 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005000 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005001int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
5002 struct drm_file *file_priv)
5003{
5004 struct drm_mode_obj_get_properties *arg = data;
5005 struct drm_mode_object *obj;
5006 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03005007
5008 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5009 return -EINVAL;
5010
Daniel Vetter84849902012-12-02 00:28:11 +01005011 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005012
5013 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5014 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005015 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005016 goto out;
5017 }
5018 if (!obj->properties) {
5019 ret = -EINVAL;
5020 goto out;
5021 }
5022
Rob Clark88a48e22014-12-18 16:01:50 -05005023 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05005024 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5025 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5026 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03005027
Paulo Zanonic5431882012-05-15 18:09:02 -03005028out:
Daniel Vetter84849902012-12-02 00:28:11 +01005029 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005030 return ret;
5031}
5032
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005033/**
5034 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5035 * @dev: DRM device
5036 * @data: ioctl data
5037 * @file_priv: DRM file info
5038 *
5039 * This function sets the current value for an object's property. It also calls
5040 * into a driver's ->set_property callback to update the hardware state.
5041 * Compared to the connector specific ioctl this one is extended to also work on
5042 * crtc and plane objects.
5043 *
5044 * Called by the user via ioctl.
5045 *
5046 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005047 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005048 */
Paulo Zanonic5431882012-05-15 18:09:02 -03005049int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5050 struct drm_file *file_priv)
5051{
5052 struct drm_mode_obj_set_property *arg = data;
5053 struct drm_mode_object *arg_obj;
5054 struct drm_mode_object *prop_obj;
5055 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05005056 int i, ret = -EINVAL;
5057 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005058
5059 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5060 return -EINVAL;
5061
Daniel Vetter84849902012-12-02 00:28:11 +01005062 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005063
5064 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005065 if (!arg_obj) {
5066 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005067 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005068 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005069 if (!arg_obj->properties)
5070 goto out;
5071
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005072 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05005073 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03005074 break;
5075
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03005076 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03005077 goto out;
5078
5079 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5080 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005081 if (!prop_obj) {
5082 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03005083 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005084 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005085 property = obj_to_property(prop_obj);
5086
Rob Clark3843e712014-12-16 18:05:29 -05005087 if (!drm_property_change_valid_get(property, arg->value, &ref))
Paulo Zanonic5431882012-05-15 18:09:02 -03005088 goto out;
5089
5090 switch (arg_obj->type) {
5091 case DRM_MODE_OBJECT_CONNECTOR:
5092 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5093 arg->value);
5094 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005095 case DRM_MODE_OBJECT_CRTC:
5096 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5097 break;
Rob Clark4d939142012-05-17 02:23:27 -06005098 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005099 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5100 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005101 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005102 }
5103
Rob Clark3843e712014-12-16 18:05:29 -05005104 drm_property_change_valid_put(property, ref);
5105
Paulo Zanonic5431882012-05-15 18:09:02 -03005106out:
Daniel Vetter84849902012-12-02 00:28:11 +01005107 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005108 return ret;
5109}
5110
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005111/**
5112 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5113 * @connector: connector to attach
5114 * @encoder: encoder to attach @connector to
5115 *
5116 * This function links up a connector to an encoder. Note that the routing
5117 * restrictions between encoders and crtcs are exposed to userspace through the
5118 * possible_clones and possible_crtcs bitmasks.
5119 *
5120 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005121 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005122 */
Dave Airlief453ba02008-11-07 14:05:41 -08005123int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5124 struct drm_encoder *encoder)
5125{
5126 int i;
5127
Thierry Redingeb47fe82015-11-16 18:19:53 +01005128 /*
5129 * In the past, drivers have attempted to model the static association
5130 * of connector to encoder in simple connector/encoder devices using a
5131 * direct assignment of connector->encoder = encoder. This connection
5132 * is a logical one and the responsibility of the core, so drivers are
5133 * expected not to mess with this.
5134 *
5135 * Note that the error return should've been enough here, but a large
5136 * majority of drivers ignores the return value, so add in a big WARN
5137 * to get people's attention.
5138 */
5139 if (WARN_ON(connector->encoder))
5140 return -EINVAL;
5141
Dave Airlief453ba02008-11-07 14:05:41 -08005142 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5143 if (connector->encoder_ids[i] == 0) {
5144 connector->encoder_ids[i] = encoder->base.id;
5145 return 0;
5146 }
5147 }
5148 return -ENOMEM;
5149}
5150EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5151
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005152/**
5153 * drm_mode_crtc_set_gamma_size - set the gamma table size
5154 * @crtc: CRTC to set the gamma table size for
5155 * @gamma_size: size of the gamma table
5156 *
5157 * Drivers which support gamma tables should set this to the supported gamma
5158 * table size when initializing the CRTC. Currently the drm core only supports a
5159 * fixed gamma table size.
5160 *
5161 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005162 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005163 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005164int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005165 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005166{
5167 crtc->gamma_size = gamma_size;
5168
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005169 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5170 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005171 if (!crtc->gamma_store) {
5172 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005173 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005174 }
5175
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005176 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005177}
5178EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5179
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005180/**
5181 * drm_mode_gamma_set_ioctl - set the gamma table
5182 * @dev: DRM device
5183 * @data: ioctl data
5184 * @file_priv: DRM file info
5185 *
5186 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5187 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5188 *
5189 * Called by the user via ioctl.
5190 *
5191 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005192 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005193 */
Dave Airlief453ba02008-11-07 14:05:41 -08005194int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5195 void *data, struct drm_file *file_priv)
5196{
5197 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005198 struct drm_crtc *crtc;
5199 void *r_base, *g_base, *b_base;
5200 int size;
5201 int ret = 0;
5202
Dave Airliefb3b06c2011-02-08 13:55:21 +10005203 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5204 return -EINVAL;
5205
Daniel Vetter84849902012-12-02 00:28:11 +01005206 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005207 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5208 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005209 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005210 goto out;
5211 }
Dave Airlief453ba02008-11-07 14:05:41 -08005212
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005213 if (crtc->funcs->gamma_set == NULL) {
5214 ret = -ENOSYS;
5215 goto out;
5216 }
5217
Dave Airlief453ba02008-11-07 14:05:41 -08005218 /* memcpy into gamma store */
5219 if (crtc_lut->gamma_size != crtc->gamma_size) {
5220 ret = -EINVAL;
5221 goto out;
5222 }
5223
5224 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5225 r_base = crtc->gamma_store;
5226 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5227 ret = -EFAULT;
5228 goto out;
5229 }
5230
5231 g_base = r_base + size;
5232 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5233 ret = -EFAULT;
5234 goto out;
5235 }
5236
5237 b_base = g_base + size;
5238 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5239 ret = -EFAULT;
5240 goto out;
5241 }
5242
James Simmons72034252010-08-03 01:33:19 +01005243 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005244
5245out:
Daniel Vetter84849902012-12-02 00:28:11 +01005246 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005247 return ret;
5248
5249}
5250
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005251/**
5252 * drm_mode_gamma_get_ioctl - get the gamma table
5253 * @dev: DRM device
5254 * @data: ioctl data
5255 * @file_priv: DRM file info
5256 *
5257 * Copy the current gamma table into the storage provided. This also provides
5258 * the gamma table size the driver expects, which can be used to size the
5259 * allocated storage.
5260 *
5261 * Called by the user via ioctl.
5262 *
5263 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005264 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005265 */
Dave Airlief453ba02008-11-07 14:05:41 -08005266int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5267 void *data, struct drm_file *file_priv)
5268{
5269 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005270 struct drm_crtc *crtc;
5271 void *r_base, *g_base, *b_base;
5272 int size;
5273 int ret = 0;
5274
Dave Airliefb3b06c2011-02-08 13:55:21 +10005275 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5276 return -EINVAL;
5277
Daniel Vetter84849902012-12-02 00:28:11 +01005278 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005279 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5280 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005281 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005282 goto out;
5283 }
Dave Airlief453ba02008-11-07 14:05:41 -08005284
5285 /* memcpy into gamma store */
5286 if (crtc_lut->gamma_size != crtc->gamma_size) {
5287 ret = -EINVAL;
5288 goto out;
5289 }
5290
5291 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5292 r_base = crtc->gamma_store;
5293 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5294 ret = -EFAULT;
5295 goto out;
5296 }
5297
5298 g_base = r_base + size;
5299 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5300 ret = -EFAULT;
5301 goto out;
5302 }
5303
5304 b_base = g_base + size;
5305 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5306 ret = -EFAULT;
5307 goto out;
5308 }
5309out:
Daniel Vetter84849902012-12-02 00:28:11 +01005310 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005311 return ret;
5312}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005313
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005314/**
5315 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5316 * @dev: DRM device
5317 * @data: ioctl data
5318 * @file_priv: DRM file info
5319 *
5320 * This schedules an asynchronous update on a given CRTC, called page flip.
5321 * Optionally a drm event is generated to signal the completion of the event.
5322 * Generic drivers cannot assume that a pageflip with changed framebuffer
5323 * properties (including driver specific metadata like tiling layout) will work,
5324 * but some drivers support e.g. pixel format changes through the pageflip
5325 * ioctl.
5326 *
5327 * Called by the user via ioctl.
5328 *
5329 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005330 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005331 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005332int drm_mode_page_flip_ioctl(struct drm_device *dev,
5333 void *data, struct drm_file *file_priv)
5334{
5335 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005336 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005337 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005338 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005339 int ret = -EINVAL;
5340
5341 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5342 page_flip->reserved != 0)
5343 return -EINVAL;
5344
Keith Packard62f21042013-07-22 18:50:00 -07005345 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5346 return -EINVAL;
5347
Rob Clarka2b34e22013-10-05 16:36:52 -04005348 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5349 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005350 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005351
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005352 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005353 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005354 /* The framebuffer is currently unbound, presumably
5355 * due to a hotplug event, that userspace has not
5356 * yet discovered.
5357 */
5358 ret = -EBUSY;
5359 goto out;
5360 }
5361
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005362 if (crtc->funcs->page_flip == NULL)
5363 goto out;
5364
Daniel Vetter786b99e2012-12-02 21:53:40 +01005365 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005366 if (!fb) {
5367 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005368 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005369 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005370
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005371 if (crtc->state) {
5372 const struct drm_plane_state *state = crtc->primary->state;
5373
5374 ret = check_src_coords(state->src_x, state->src_y,
5375 state->src_w, state->src_h, fb);
5376 } else {
5377 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5378 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005379 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005380 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005381
Matt Roperf4510a22014-04-01 15:22:40 -07005382 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005383 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5384 ret = -EINVAL;
5385 goto out;
5386 }
5387
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005388 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005389 e = kzalloc(sizeof *e, GFP_KERNEL);
5390 if (!e) {
5391 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005392 goto out;
5393 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005394 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005395 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005396 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005397 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5398 if (ret) {
5399 kfree(e);
5400 goto out;
5401 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005402 }
5403
Daniel Vetter3d30a592014-07-27 13:42:42 +02005404 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005405 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005406 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005407 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5408 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005409 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005410 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005411 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005412 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005413 /* Unref only the old framebuffer. */
5414 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005415 }
5416
5417out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005418 if (fb)
5419 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005420 if (crtc->primary->old_fb)
5421 drm_framebuffer_unreference(crtc->primary->old_fb);
5422 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005423 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005424
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005425 return ret;
5426}
Chris Wilsoneb033552011-01-24 15:11:08 +00005427
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005428/**
5429 * drm_mode_config_reset - call ->reset callbacks
5430 * @dev: drm device
5431 *
5432 * This functions calls all the crtc's, encoder's and connector's ->reset
5433 * callback. Drivers can use this in e.g. their driver load or resume code to
5434 * reset hardware and software state.
5435 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005436void drm_mode_config_reset(struct drm_device *dev)
5437{
5438 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005439 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005440 struct drm_encoder *encoder;
5441 struct drm_connector *connector;
5442
Daniel Vettere4f62542015-07-09 23:44:35 +02005443 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005444 if (plane->funcs->reset)
5445 plane->funcs->reset(plane);
5446
Daniel Vettere4f62542015-07-09 23:44:35 +02005447 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005448 if (crtc->funcs->reset)
5449 crtc->funcs->reset(crtc);
5450
Daniel Vettere4f62542015-07-09 23:44:35 +02005451 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005452 if (encoder->funcs->reset)
5453 encoder->funcs->reset(encoder);
5454
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005455 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005456 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005457 if (connector->funcs->reset)
5458 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005459 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005460}
5461EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005462
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005463/**
5464 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5465 * @dev: DRM device
5466 * @data: ioctl data
5467 * @file_priv: DRM file info
5468 *
5469 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5470 * TTM or something else entirely) and returns the resulting buffer handle. This
5471 * handle can then be wrapped up into a framebuffer modeset object.
5472 *
5473 * Note that userspace is not allowed to use such objects for render
5474 * acceleration - drivers must create their own private ioctls for such a use
5475 * case.
5476 *
5477 * Called by the user via ioctl.
5478 *
5479 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005480 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005481 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005482int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5483 void *data, struct drm_file *file_priv)
5484{
5485 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005486 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005487
5488 if (!dev->driver->dumb_create)
5489 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005490 if (!args->width || !args->height || !args->bpp)
5491 return -EINVAL;
5492
5493 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005494 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005495 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005496 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005497 return -EINVAL;
5498 stride = cpp * args->width;
5499 if (args->height > 0xffffffffU / stride)
5500 return -EINVAL;
5501
5502 /* test for wrap-around */
5503 size = args->height * stride;
5504 if (PAGE_ALIGN(size) == 0)
5505 return -EINVAL;
5506
Thierry Redingf6085952014-11-03 11:14:14 +01005507 /*
5508 * handle, pitch and size are output parameters. Zero them out to
5509 * prevent drivers from accidentally using uninitialized data. Since
5510 * not all existing userspace is clearing these fields properly we
5511 * cannot reject IOCTL with garbage in them.
5512 */
5513 args->handle = 0;
5514 args->pitch = 0;
5515 args->size = 0;
5516
Dave Airlieff72145b2011-02-07 12:16:14 +10005517 return dev->driver->dumb_create(file_priv, dev, args);
5518}
5519
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005520/**
5521 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5522 * @dev: DRM device
5523 * @data: ioctl data
5524 * @file_priv: DRM file info
5525 *
5526 * Allocate an offset in the drm device node's address space to be able to
5527 * memory map a dumb buffer.
5528 *
5529 * Called by the user via ioctl.
5530 *
5531 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005532 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005533 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005534int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5535 void *data, struct drm_file *file_priv)
5536{
5537 struct drm_mode_map_dumb *args = data;
5538
5539 /* call driver ioctl to get mmap offset */
5540 if (!dev->driver->dumb_map_offset)
5541 return -ENOSYS;
5542
5543 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5544}
5545
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005546/**
5547 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5548 * @dev: DRM device
5549 * @data: ioctl data
5550 * @file_priv: DRM file info
5551 *
5552 * This destroys the userspace handle for the given dumb backing storage buffer.
5553 * Since buffer objects must be reference counted in the kernel a buffer object
5554 * won't be immediately freed if a framebuffer modeset object still uses it.
5555 *
5556 * Called by the user via ioctl.
5557 *
5558 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005559 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005560 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005561int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5562 void *data, struct drm_file *file_priv)
5563{
5564 struct drm_mode_destroy_dumb *args = data;
5565
5566 if (!dev->driver->dumb_destroy)
5567 return -ENOSYS;
5568
5569 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5570}
Dave Airlie248dbc22011-11-29 20:02:54 +00005571
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005572/**
5573 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5574 * @format: pixel format (DRM_FORMAT_*)
5575 * @depth: storage for the depth value
5576 * @bpp: storage for the bpp value
5577 *
5578 * This only supports RGB formats here for compat with code that doesn't use
5579 * pixel formats directly yet.
Dave Airlie248dbc22011-11-29 20:02:54 +00005580 */
5581void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5582 int *bpp)
5583{
5584 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02005585 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02005586 case DRM_FORMAT_RGB332:
5587 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00005588 *depth = 8;
5589 *bpp = 8;
5590 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005591 case DRM_FORMAT_XRGB1555:
5592 case DRM_FORMAT_XBGR1555:
5593 case DRM_FORMAT_RGBX5551:
5594 case DRM_FORMAT_BGRX5551:
5595 case DRM_FORMAT_ARGB1555:
5596 case DRM_FORMAT_ABGR1555:
5597 case DRM_FORMAT_RGBA5551:
5598 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00005599 *depth = 15;
5600 *bpp = 16;
5601 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005602 case DRM_FORMAT_RGB565:
5603 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00005604 *depth = 16;
5605 *bpp = 16;
5606 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005607 case DRM_FORMAT_RGB888:
5608 case DRM_FORMAT_BGR888:
5609 *depth = 24;
5610 *bpp = 24;
5611 break;
5612 case DRM_FORMAT_XRGB8888:
5613 case DRM_FORMAT_XBGR8888:
5614 case DRM_FORMAT_RGBX8888:
5615 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005616 *depth = 24;
5617 *bpp = 32;
5618 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005619 case DRM_FORMAT_XRGB2101010:
5620 case DRM_FORMAT_XBGR2101010:
5621 case DRM_FORMAT_RGBX1010102:
5622 case DRM_FORMAT_BGRX1010102:
5623 case DRM_FORMAT_ARGB2101010:
5624 case DRM_FORMAT_ABGR2101010:
5625 case DRM_FORMAT_RGBA1010102:
5626 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00005627 *depth = 30;
5628 *bpp = 32;
5629 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02005630 case DRM_FORMAT_ARGB8888:
5631 case DRM_FORMAT_ABGR8888:
5632 case DRM_FORMAT_RGBA8888:
5633 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00005634 *depth = 32;
5635 *bpp = 32;
5636 break;
5637 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03005638 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5639 drm_get_format_name(format));
Dave Airlie248dbc22011-11-29 20:02:54 +00005640 *depth = 0;
5641 *bpp = 0;
5642 break;
5643 }
5644}
5645EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03005646
5647/**
5648 * drm_format_num_planes - get the number of planes for format
5649 * @format: pixel format (DRM_FORMAT_*)
5650 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005651 * Returns:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005652 * The number of planes used by the specified pixel format.
5653 */
5654int drm_format_num_planes(uint32_t format)
5655{
5656 switch (format) {
5657 case DRM_FORMAT_YUV410:
5658 case DRM_FORMAT_YVU410:
5659 case DRM_FORMAT_YUV411:
5660 case DRM_FORMAT_YVU411:
5661 case DRM_FORMAT_YUV420:
5662 case DRM_FORMAT_YVU420:
5663 case DRM_FORMAT_YUV422:
5664 case DRM_FORMAT_YVU422:
5665 case DRM_FORMAT_YUV444:
5666 case DRM_FORMAT_YVU444:
5667 return 3;
5668 case DRM_FORMAT_NV12:
5669 case DRM_FORMAT_NV21:
5670 case DRM_FORMAT_NV16:
5671 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005672 case DRM_FORMAT_NV24:
5673 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03005674 return 2;
5675 default:
5676 return 1;
5677 }
5678}
5679EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005680
5681/**
5682 * drm_format_plane_cpp - determine the bytes per pixel value
5683 * @format: pixel format (DRM_FORMAT_*)
5684 * @plane: plane index
5685 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005686 * Returns:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005687 * The bytes per pixel value for the specified plane.
5688 */
5689int drm_format_plane_cpp(uint32_t format, int plane)
5690{
5691 unsigned int depth;
5692 int bpp;
5693
5694 if (plane >= drm_format_num_planes(format))
5695 return 0;
5696
5697 switch (format) {
5698 case DRM_FORMAT_YUYV:
5699 case DRM_FORMAT_YVYU:
5700 case DRM_FORMAT_UYVY:
5701 case DRM_FORMAT_VYUY:
5702 return 2;
5703 case DRM_FORMAT_NV12:
5704 case DRM_FORMAT_NV21:
5705 case DRM_FORMAT_NV16:
5706 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02005707 case DRM_FORMAT_NV24:
5708 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03005709 return plane ? 2 : 1;
5710 case DRM_FORMAT_YUV410:
5711 case DRM_FORMAT_YVU410:
5712 case DRM_FORMAT_YUV411:
5713 case DRM_FORMAT_YVU411:
5714 case DRM_FORMAT_YUV420:
5715 case DRM_FORMAT_YVU420:
5716 case DRM_FORMAT_YUV422:
5717 case DRM_FORMAT_YVU422:
5718 case DRM_FORMAT_YUV444:
5719 case DRM_FORMAT_YVU444:
5720 return 1;
5721 default:
5722 drm_fb_get_bpp_depth(format, &depth, &bpp);
5723 return bpp >> 3;
5724 }
5725}
5726EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005727
5728/**
5729 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5730 * @format: pixel format (DRM_FORMAT_*)
5731 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005732 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005733 * The horizontal chroma subsampling factor for the
5734 * specified pixel format.
5735 */
5736int drm_format_horz_chroma_subsampling(uint32_t format)
5737{
5738 switch (format) {
5739 case DRM_FORMAT_YUV411:
5740 case DRM_FORMAT_YVU411:
5741 case DRM_FORMAT_YUV410:
5742 case DRM_FORMAT_YVU410:
5743 return 4;
5744 case DRM_FORMAT_YUYV:
5745 case DRM_FORMAT_YVYU:
5746 case DRM_FORMAT_UYVY:
5747 case DRM_FORMAT_VYUY:
5748 case DRM_FORMAT_NV12:
5749 case DRM_FORMAT_NV21:
5750 case DRM_FORMAT_NV16:
5751 case DRM_FORMAT_NV61:
5752 case DRM_FORMAT_YUV422:
5753 case DRM_FORMAT_YVU422:
5754 case DRM_FORMAT_YUV420:
5755 case DRM_FORMAT_YVU420:
5756 return 2;
5757 default:
5758 return 1;
5759 }
5760}
5761EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5762
5763/**
5764 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5765 * @format: pixel format (DRM_FORMAT_*)
5766 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005767 * Returns:
Ville Syrjälä01b68b02012-04-05 21:35:17 +03005768 * The vertical chroma subsampling factor for the
5769 * specified pixel format.
5770 */
5771int drm_format_vert_chroma_subsampling(uint32_t format)
5772{
5773 switch (format) {
5774 case DRM_FORMAT_YUV410:
5775 case DRM_FORMAT_YVU410:
5776 return 4;
5777 case DRM_FORMAT_YUV420:
5778 case DRM_FORMAT_YVU420:
5779 case DRM_FORMAT_NV12:
5780 case DRM_FORMAT_NV21:
5781 return 2;
5782 default:
5783 return 1;
5784 }
5785}
5786EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005787
5788/**
Ville Syrjälä4c617162016-02-09 17:29:44 +02005789 * drm_format_plane_width - width of the plane given the first plane
5790 * @width: width of the first plane
5791 * @format: pixel format
5792 * @plane: plane index
5793 *
5794 * Returns:
5795 * The width of @plane, given that the width of the first plane is @width.
5796 */
5797int drm_format_plane_width(int width, uint32_t format, int plane)
5798{
5799 if (plane >= drm_format_num_planes(format))
5800 return 0;
5801
5802 if (plane == 0)
5803 return width;
5804
5805 return width / drm_format_horz_chroma_subsampling(format);
5806}
5807EXPORT_SYMBOL(drm_format_plane_width);
5808
5809/**
5810 * drm_format_plane_height - height of the plane given the first plane
5811 * @height: height of the first plane
5812 * @format: pixel format
5813 * @plane: plane index
5814 *
5815 * Returns:
5816 * The height of @plane, given that the height of the first plane is @height.
5817 */
5818int drm_format_plane_height(int height, uint32_t format, int plane)
5819{
5820 if (plane >= drm_format_num_planes(format))
5821 return 0;
5822
5823 if (plane == 0)
5824 return height;
5825
5826 return height / drm_format_vert_chroma_subsampling(format);
5827}
5828EXPORT_SYMBOL(drm_format_plane_height);
5829
5830/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305831 * drm_rotation_simplify() - Try to simplify the rotation
5832 * @rotation: Rotation to be simplified
5833 * @supported_rotations: Supported rotations
5834 *
5835 * Attempt to simplify the rotation to a form that is supported.
5836 * Eg. if the hardware supports everything except DRM_REFLECT_X
5837 * one could call this function like this:
5838 *
5839 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5840 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5841 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5842 *
5843 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5844 * transforms the hardware supports, this function may not
5845 * be able to produce a supported transform, so the caller should
5846 * check the result afterwards.
5847 */
5848unsigned int drm_rotation_simplify(unsigned int rotation,
5849 unsigned int supported_rotations)
5850{
5851 if (rotation & ~supported_rotations) {
5852 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005853 rotation = (rotation & DRM_REFLECT_MASK) |
5854 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305855 }
5856
5857 return rotation;
5858}
5859EXPORT_SYMBOL(drm_rotation_simplify);
5860
5861/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005862 * drm_mode_config_init - initialize DRM mode_configuration structure
5863 * @dev: DRM device
5864 *
5865 * Initialize @dev's mode_config structure, used for tracking the graphics
5866 * configuration of @dev.
5867 *
5868 * Since this initializes the modeset locks, no locking is possible. Which is no
5869 * problem, since this should happen single threaded at init time. It is the
5870 * driver's problem to ensure this guarantee.
5871 *
5872 */
5873void drm_mode_config_init(struct drm_device *dev)
5874{
5875 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005876 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005877 mutex_init(&dev->mode_config.idr_mutex);
5878 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005879 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005880 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5881 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5882 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5883 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5884 INIT_LIST_HEAD(&dev->mode_config.property_list);
5885 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5886 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5887 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005888 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005889 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005890
5891 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005892 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005893 drm_modeset_unlock_all(dev);
5894
5895 /* Just to be sure */
5896 dev->mode_config.num_fb = 0;
5897 dev->mode_config.num_connector = 0;
5898 dev->mode_config.num_crtc = 0;
5899 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005900 dev->mode_config.num_overlay_plane = 0;
5901 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005902}
5903EXPORT_SYMBOL(drm_mode_config_init);
5904
5905/**
5906 * drm_mode_config_cleanup - free up DRM mode_config info
5907 * @dev: DRM device
5908 *
5909 * Free up all the connectors and CRTCs associated with this DRM device, then
5910 * free up the framebuffers and associated buffer objects.
5911 *
5912 * Note that since this /should/ happen single-threaded at driver/device
5913 * teardown time, no locking is required. It's the driver's job to ensure that
5914 * this guarantee actually holds true.
5915 *
5916 * FIXME: cleanup any dangling user buffer objects too
5917 */
5918void drm_mode_config_cleanup(struct drm_device *dev)
5919{
5920 struct drm_connector *connector, *ot;
5921 struct drm_crtc *crtc, *ct;
5922 struct drm_encoder *encoder, *enct;
5923 struct drm_framebuffer *fb, *fbt;
5924 struct drm_property *property, *pt;
5925 struct drm_property_blob *blob, *bt;
5926 struct drm_plane *plane, *plt;
5927
5928 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5929 head) {
5930 encoder->funcs->destroy(encoder);
5931 }
5932
5933 list_for_each_entry_safe(connector, ot,
5934 &dev->mode_config.connector_list, head) {
5935 connector->funcs->destroy(connector);
5936 }
5937
5938 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5939 head) {
5940 drm_property_destroy(dev, property);
5941 }
5942
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005943 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5944 head) {
5945 plane->funcs->destroy(plane);
5946 }
5947
5948 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5949 crtc->funcs->destroy(crtc);
5950 }
5951
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005952 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005953 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005954 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005955 }
5956
5957 /*
5958 * Single-threaded teardown context, so it's not required to grab the
5959 * fb_lock to protect against concurrent fb_list access. Contrary, it
5960 * would actually deadlock with the drm_framebuffer_cleanup function.
5961 *
5962 * Also, if there are any framebuffers left, that's a driver leak now,
5963 * so politely WARN about this.
5964 */
5965 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5966 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005967 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005968 }
5969
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005970 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005971 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005972 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005973 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005974}
5975EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305976
5977struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5978 unsigned int supported_rotations)
5979{
5980 static const struct drm_prop_enum_list props[] = {
5981 { DRM_ROTATE_0, "rotate-0" },
5982 { DRM_ROTATE_90, "rotate-90" },
5983 { DRM_ROTATE_180, "rotate-180" },
5984 { DRM_ROTATE_270, "rotate-270" },
5985 { DRM_REFLECT_X, "reflect-x" },
5986 { DRM_REFLECT_Y, "reflect-y" },
5987 };
5988
5989 return drm_property_create_bitmask(dev, 0, "rotation",
5990 props, ARRAY_SIZE(props),
5991 supported_rotations);
5992}
5993EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005994
5995/**
5996 * DOC: Tile group
5997 *
5998 * Tile groups are used to represent tiled monitors with a unique
5999 * integer identifier. Tiled monitors using DisplayID v1.3 have
6000 * a unique 8-byte handle, we store this in a tile group, so we
6001 * have a common identifier for all tiles in a monitor group.
6002 */
6003static void drm_tile_group_free(struct kref *kref)
6004{
6005 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
6006 struct drm_device *dev = tg->dev;
6007 mutex_lock(&dev->mode_config.idr_mutex);
6008 idr_remove(&dev->mode_config.tile_idr, tg->id);
6009 mutex_unlock(&dev->mode_config.idr_mutex);
6010 kfree(tg);
6011}
6012
6013/**
6014 * drm_mode_put_tile_group - drop a reference to a tile group.
6015 * @dev: DRM device
6016 * @tg: tile group to drop reference to.
6017 *
6018 * drop reference to tile group and free if 0.
6019 */
6020void drm_mode_put_tile_group(struct drm_device *dev,
6021 struct drm_tile_group *tg)
6022{
6023 kref_put(&tg->refcount, drm_tile_group_free);
6024}
6025
6026/**
6027 * drm_mode_get_tile_group - get a reference to an existing tile group
6028 * @dev: DRM device
6029 * @topology: 8-bytes unique per monitor.
6030 *
6031 * Use the unique bytes to get a reference to an existing tile group.
6032 *
6033 * RETURNS:
6034 * tile group or NULL if not found.
6035 */
6036struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6037 char topology[8])
6038{
6039 struct drm_tile_group *tg;
6040 int id;
6041 mutex_lock(&dev->mode_config.idr_mutex);
6042 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6043 if (!memcmp(tg->group_data, topology, 8)) {
6044 if (!kref_get_unless_zero(&tg->refcount))
6045 tg = NULL;
6046 mutex_unlock(&dev->mode_config.idr_mutex);
6047 return tg;
6048 }
6049 }
6050 mutex_unlock(&dev->mode_config.idr_mutex);
6051 return NULL;
6052}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006053EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10006054
6055/**
6056 * drm_mode_create_tile_group - create a tile group from a displayid description
6057 * @dev: DRM device
6058 * @topology: 8-bytes unique per monitor.
6059 *
6060 * Create a tile group for the unique monitor, and get a unique
6061 * identifier for the tile group.
6062 *
6063 * RETURNS:
6064 * new tile group or error.
6065 */
6066struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6067 char topology[8])
6068{
6069 struct drm_tile_group *tg;
6070 int ret;
6071
6072 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6073 if (!tg)
6074 return ERR_PTR(-ENOMEM);
6075
6076 kref_init(&tg->refcount);
6077 memcpy(tg->group_data, topology, 8);
6078 tg->dev = dev;
6079
6080 mutex_lock(&dev->mode_config.idr_mutex);
6081 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6082 if (ret >= 0) {
6083 tg->id = ret;
6084 } else {
6085 kfree(tg);
6086 tg = ERR_PTR(ret);
6087 }
6088
6089 mutex_unlock(&dev->mode_config.idr_mutex);
6090 return tg;
6091}
Rob Clark81ddd1b2015-03-27 13:01:52 -04006092EXPORT_SYMBOL(drm_mode_create_tile_group);