blob: 4ec35f9e6de5758fc377edfceb0f95099509cef7 [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" },
Eric Anholt0b27c022016-03-18 12:34:59 -0700171 { DRM_MODE_CONNECTOR_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800172};
173
Thierry Reding4dfd9092014-12-10 13:03:34 +0100174static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
175 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -0800176 { DRM_MODE_ENCODER_DAC, "DAC" },
177 { DRM_MODE_ENCODER_TMDS, "TMDS" },
178 { DRM_MODE_ENCODER_LVDS, "LVDS" },
179 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200180 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300181 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000182 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Eric Anholt0b27c022016-03-18 12:34:59 -0700183 { DRM_MODE_ENCODER_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800184};
185
Thierry Reding4dfd9092014-12-10 13:03:34 +0100186static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
Jesse Barnesac1bb362014-02-10 15:32:44 -0800187 { SubPixelUnknown, "Unknown" },
188 { SubPixelHorizontalRGB, "Horizontal RGB" },
189 { SubPixelHorizontalBGR, "Horizontal BGR" },
190 { SubPixelVerticalRGB, "Vertical RGB" },
191 { SubPixelVerticalBGR, "Vertical BGR" },
192 { SubPixelNone, "None" },
193};
194
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400195void drm_connector_ida_init(void)
196{
197 int i;
198
199 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
200 ida_init(&drm_connector_enum_list[i].ida);
201}
202
203void drm_connector_ida_destroy(void)
204{
205 int i;
206
207 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
208 ida_destroy(&drm_connector_enum_list[i].ida);
209}
210
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100211/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100212 * drm_get_connector_status_name - return a string for connector status
213 * @status: connector status to compute name of
214 *
215 * In contrast to the other drm_get_*_name functions this one here returns a
216 * const pointer and hence is threadsafe.
217 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000218const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800219{
220 if (status == connector_status_connected)
221 return "connected";
222 else if (status == connector_status_disconnected)
223 return "disconnected";
224 else
225 return "unknown";
226}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000227EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800228
Jesse Barnesac1bb362014-02-10 15:32:44 -0800229/**
230 * drm_get_subpixel_order_name - return a string for a given subpixel enum
231 * @order: enum of subpixel_order
232 *
233 * Note you could abuse this and return something out of bounds, but that
234 * would be a caller error. No unscrubbed user data should make it here.
235 */
236const char *drm_get_subpixel_order_name(enum subpixel_order order)
237{
238 return drm_subpixel_enum_list[order].name;
239}
240EXPORT_SYMBOL(drm_get_subpixel_order_name);
241
Dave Airlie2ee39452014-07-24 11:53:45 +1000242/*
243 * Internal function to assign a slot in the object idr and optionally
244 * register the object into the idr.
245 */
246static int drm_mode_object_get_reg(struct drm_device *dev,
247 struct drm_mode_object *obj,
248 uint32_t obj_type,
Dave Airlied0f37cf62016-04-15 15:10:36 +1000249 bool register_obj,
250 void (*obj_free_cb)(struct kref *kref))
Dave Airlie2ee39452014-07-24 11:53:45 +1000251{
252 int ret;
253
254 mutex_lock(&dev->mode_config.idr_mutex);
255 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
256 if (ret >= 0) {
257 /*
258 * Set up the object linking under the protection of the idr
259 * lock so that other users can't see inconsistent state.
260 */
261 obj->id = ret;
262 obj->type = obj_type;
Dave Airlied0f37cf62016-04-15 15:10:36 +1000263 if (obj_free_cb) {
264 obj->free_cb = obj_free_cb;
265 kref_init(&obj->refcount);
266 }
Dave Airlie2ee39452014-07-24 11:53:45 +1000267 }
268 mutex_unlock(&dev->mode_config.idr_mutex);
269
270 return ret < 0 ? ret : 0;
271}
272
Dave Airlief453ba02008-11-07 14:05:41 -0800273/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100274 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800275 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100276 * @obj: object pointer, used to generate unique ID
277 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800278 *
Dave Airlief453ba02008-11-07 14:05:41 -0800279 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100280 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
281 * modeset identifiers are _not_ reference counted. Hence don't use this for
282 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800283 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100284 * Returns:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200285 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800286 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100287int drm_mode_object_get(struct drm_device *dev,
288 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800289{
Dave Airlied0f37cf62016-04-15 15:10:36 +1000290 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
Dave Airlie2ee39452014-07-24 11:53:45 +1000291}
Dave Airlief453ba02008-11-07 14:05:41 -0800292
Dave Airlie2ee39452014-07-24 11:53:45 +1000293static void drm_mode_object_register(struct drm_device *dev,
294 struct drm_mode_object *obj)
295{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000296 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000297 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000298 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800299}
300
301/**
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000302 * drm_mode_object_unregister - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800303 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100304 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800305 *
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000306 * Free @id from @dev's unique identifier pool.
307 * This function can be called multiple times, and guards against
308 * multiple removals.
309 * These modeset identifiers are _not_ reference counted. Hence don't use this
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100310 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800311 */
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000312void drm_mode_object_unregister(struct drm_device *dev,
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100313 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800314{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000315 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000316 if (object->id) {
317 idr_remove(&dev->mode_config.crtc_idr, object->id);
318 object->id = 0;
319 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000320 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800321}
322
Rob Clark98f75de2014-05-30 11:37:03 -0400323static struct drm_mode_object *_object_find(struct drm_device *dev,
324 uint32_t id, uint32_t type)
325{
326 struct drm_mode_object *obj = NULL;
327
328 mutex_lock(&dev->mode_config.idr_mutex);
329 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200330 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
331 obj = NULL;
332 if (obj && obj->id != id)
333 obj = NULL;
Dave Airlie72fe90b2016-04-15 15:10:40 +1000334
335 if (obj && obj->free_cb) {
336 if (!kref_get_unless_zero(&obj->refcount))
337 obj = NULL;
338 }
Rob Clark98f75de2014-05-30 11:37:03 -0400339 mutex_unlock(&dev->mode_config.idr_mutex);
340
341 return obj;
342}
343
Daniel Vetter786b99e2012-12-02 21:53:40 +0100344/**
345 * drm_mode_object_find - look up a drm object with static lifetime
346 * @dev: drm device
347 * @id: id of the mode object
348 * @type: type of the mode object
349 *
Daniel Vetter059814222016-04-22 22:10:27 +0200350 * This function is used to look up a modeset object. It will acquire a
351 * reference for reference counted objects. This reference must be dropped again
352 * by callind drm_mode_object_unreference().
Daniel Vetter786b99e2012-12-02 21:53:40 +0100353 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200354struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
355 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800356{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000357 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800358
Rob Clark98f75de2014-05-30 11:37:03 -0400359 obj = _object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800360 return obj;
361}
362EXPORT_SYMBOL(drm_mode_object_find);
363
Daniel Vetter059814222016-04-22 22:10:27 +0200364/**
365 * drm_mode_object_unreference - decr the object refcnt
366 * @obj: mode_object
367 *
368 * This functions decrements the object's refcount if it is a refcounted modeset
369 * object. It is a no-op on any other object. This is used to drop references
370 * acquired with drm_mode_object_reference().
371 */
Dave Airlied0f37cf62016-04-15 15:10:36 +1000372void drm_mode_object_unreference(struct drm_mode_object *obj)
373{
374 if (obj->free_cb) {
375 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
376 kref_put(&obj->refcount, obj->free_cb);
377 }
378}
379EXPORT_SYMBOL(drm_mode_object_unreference);
380
381/**
Daniel Vetter059814222016-04-22 22:10:27 +0200382 * drm_mode_object_reference - incr the object refcnt
Dave Airlied0f37cf62016-04-15 15:10:36 +1000383 * @obj: mode_object
384 *
Daniel Vetter059814222016-04-22 22:10:27 +0200385 * This functions increments the object's refcount if it is a refcounted modeset
386 * object. It is a no-op on any other object. References should be dropped again
387 * by calling drm_mode_object_unreference().
Dave Airlied0f37cf62016-04-15 15:10:36 +1000388 */
389void drm_mode_object_reference(struct drm_mode_object *obj)
390{
391 if (obj->free_cb) {
392 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
393 kref_get(&obj->refcount);
394 }
395}
396EXPORT_SYMBOL(drm_mode_object_reference);
397
Dave Airlief55f1f92016-04-15 15:10:33 +1000398static void drm_framebuffer_free(struct kref *kref)
399{
400 struct drm_framebuffer *fb =
Dave Airlied0f37cf62016-04-15 15:10:36 +1000401 container_of(kref, struct drm_framebuffer, base.refcount);
Dave Airlief55f1f92016-04-15 15:10:33 +1000402 struct drm_device *dev = fb->dev;
403
404 /*
405 * The lookup idr holds a weak reference, which has not necessarily been
406 * removed at this point. Check for that.
407 */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000408 drm_mode_object_unregister(dev, &fb->base);
Dave Airlief55f1f92016-04-15 15:10:33 +1000409
410 fb->funcs->destroy(fb);
411}
412
Dave Airlief453ba02008-11-07 14:05:41 -0800413/**
Dave Airlief453ba02008-11-07 14:05:41 -0800414 * drm_framebuffer_init - initialize a framebuffer
415 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100416 * @fb: framebuffer to be initialized
417 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800418 *
Dave Airlief453ba02008-11-07 14:05:41 -0800419 * Allocates an ID for the framebuffer's parent mode object, sets its mode
420 * functions & device file and adds it to the master fd list.
421 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100422 * IMPORTANT:
423 * This functions publishes the fb and makes it available for concurrent access
424 * by other users. Which means by this point the fb _must_ be fully set up -
425 * since all the fb attributes are invariant over its lifetime, no further
426 * locking but only correct reference counting is required.
427 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100428 * Returns:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200429 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800430 */
431int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
432 const struct drm_framebuffer_funcs *funcs)
433{
434 int ret;
435
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100436 INIT_LIST_HEAD(&fb->filp_head);
437 fb->dev = dev;
438 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000439
Dave Airlied0f37cf62016-04-15 15:10:36 +1000440 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
Dave Airlie9cd47422016-04-15 15:10:38 +1000441 false, drm_framebuffer_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200442 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100443 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800444
Dave Airlie9cd47422016-04-15 15:10:38 +1000445 mutex_lock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800446 dev->mode_config.num_fb++;
447 list_add(&fb->head, &dev->mode_config.fb_list);
Dave Airlie2ddea3f2016-04-15 15:10:41 +1000448 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800449
Dave Airlie9cd47422016-04-15 15:10:38 +1000450 drm_mode_object_register(dev, &fb->base);
Dave Airlie9cd47422016-04-15 15:10:38 +1000451out:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200452 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800453}
454EXPORT_SYMBOL(drm_framebuffer_init);
455
Rob Clarkf7eff602012-09-05 21:48:38 +0000456/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100457 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
458 * @dev: drm device
459 * @id: id of the fb object
460 *
461 * If successful, this grabs an additional reference to the framebuffer -
462 * callers need to make sure to eventually unreference the returned framebuffer
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100463 * again, using @drm_framebuffer_unreference.
Daniel Vetter786b99e2012-12-02 21:53:40 +0100464 */
465struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
466 uint32_t id)
467{
Dave Airliecee26ac2016-04-15 15:10:37 +1000468 struct drm_mode_object *obj;
469 struct drm_framebuffer *fb = NULL;
Daniel Vetter786b99e2012-12-02 21:53:40 +0100470
Dave Airliecee26ac2016-04-15 15:10:37 +1000471 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
Dave Airlie72fe90b2016-04-15 15:10:40 +1000472 if (obj)
Dave Airliecee26ac2016-04-15 15:10:37 +1000473 fb = obj_to_fb(obj);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100474 return fb;
475}
476EXPORT_SYMBOL(drm_framebuffer_lookup);
477
478/**
Daniel Vetter36206362012-12-10 20:42:17 +0100479 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
480 * @fb: fb to unregister
481 *
482 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
483 * those used for fbdev. Note that the caller must hold a reference of it's own,
484 * i.e. the object may not be destroyed through this call (since it'll lead to a
485 * locking inversion).
486 */
487void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
488{
Daniel Vettera39a3572015-08-25 15:45:11 +0200489 struct drm_device *dev;
490
491 if (!fb)
492 return;
493
494 dev = fb->dev;
Daniel Vetter2b677e82012-12-10 21:16:05 +0100495
Daniel Vetter2b677e82012-12-10 21:16:05 +0100496 /* Mark fb as reaped and drop idr ref. */
Dave Airlie19ab3f82016-04-15 15:10:34 +1000497 drm_mode_object_unregister(dev, &fb->base);
Daniel Vetter36206362012-12-10 20:42:17 +0100498}
499EXPORT_SYMBOL(drm_framebuffer_unregister_private);
500
501/**
Dave Airlief453ba02008-11-07 14:05:41 -0800502 * drm_framebuffer_cleanup - remove a framebuffer object
503 * @fb: framebuffer to remove
504 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100505 * Cleanup framebuffer. This function is intended to be used from the drivers
506 * ->destroy callback. It can also be used to clean up driver private
Daniel Vetter2e7a5702016-06-01 23:40:36 +0200507 * framebuffers embedded into a larger structure.
Daniel Vetter36206362012-12-10 20:42:17 +0100508 *
509 * Note that this function does not remove the fb from active usuage - if it is
510 * still used anywhere, hilarity can ensue since userspace could call getfb on
511 * the id and get back -EINVAL. Obviously no concern at driver unload time.
512 *
513 * Also, the framebuffer will not be removed from the lookup idr - for
514 * user-created framebuffers this will happen in in the rmfb ioctl. For
515 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
516 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800517 */
518void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
519{
520 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100521
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100522 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000523 list_del(&fb->head);
524 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100525 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000526}
527EXPORT_SYMBOL(drm_framebuffer_cleanup);
528
529/**
530 * drm_framebuffer_remove - remove and unreference a framebuffer object
531 * @fb: framebuffer to remove
532 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000533 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100534 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100535 * passed-in framebuffer. Might take the modeset locks.
536 *
537 * Note that this function optimizes the cleanup away if the caller holds the
538 * last reference to the framebuffer. It is also guaranteed to not take the
539 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000540 */
541void drm_framebuffer_remove(struct drm_framebuffer *fb)
542{
Daniel Vettera39a3572015-08-25 15:45:11 +0200543 struct drm_device *dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800544 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800545 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000546 struct drm_mode_set set;
547 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800548
Daniel Vettera39a3572015-08-25 15:45:11 +0200549 if (!fb)
550 return;
551
552 dev = fb->dev;
553
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100554 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100555
Daniel Vetterb62584e2012-12-11 16:51:35 +0100556 /*
557 * drm ABI mandates that we remove any deleted framebuffers from active
558 * useage. But since most sane clients only remove framebuffers they no
559 * longer need, try to optimize this away.
560 *
561 * Since we're holding a reference ourselves, observing a refcount of 1
562 * means that we're the last holder and can skip it. Also, the refcount
563 * can never increase from 1 again, so we don't need any barriers or
564 * locks.
565 *
566 * Note that userspace could try to race with use and instate a new
567 * usage _after_ we've cleared all current ones. End result will be an
568 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
569 * in this manner.
570 */
Dave Airlie747a5982016-04-15 15:10:35 +1000571 if (drm_framebuffer_read_refcount(fb) > 1) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100572 drm_modeset_lock_all(dev);
573 /* remove from any CRTC */
Daniel Vetter6295d602015-07-09 23:44:25 +0200574 drm_for_each_crtc(crtc, dev) {
Matt Roperf4510a22014-04-01 15:22:40 -0700575 if (crtc->primary->fb == fb) {
Daniel Vetterb62584e2012-12-11 16:51:35 +0100576 /* should turn off the crtc */
577 memset(&set, 0, sizeof(struct drm_mode_set));
578 set.crtc = crtc;
579 set.fb = NULL;
580 ret = drm_mode_set_config_internal(&set);
581 if (ret)
582 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
583 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000584 }
Dave Airlief453ba02008-11-07 14:05:41 -0800585
Daniel Vetter6295d602015-07-09 23:44:25 +0200586 drm_for_each_plane(plane, dev) {
Ville Syrjälä9125e612013-06-03 16:10:40 +0300587 if (plane->fb == fb)
588 drm_plane_force_disable(plane);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800589 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100590 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800591 }
592
Rob Clarkf7eff602012-09-05 21:48:38 +0000593 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800594}
Rob Clarkf7eff602012-09-05 21:48:38 +0000595EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800596
Rob Clark51fd3712013-11-19 12:10:12 -0500597DEFINE_WW_CLASS(crtc_ww_class);
598
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200599static unsigned int drm_num_crtcs(struct drm_device *dev)
600{
601 unsigned int num = 0;
602 struct drm_crtc *tmp;
603
604 drm_for_each_crtc(tmp, dev) {
605 num++;
606 }
607
608 return num;
609}
610
Dave Airlief453ba02008-11-07 14:05:41 -0800611/**
Matt Ropere13161a2014-04-01 15:22:38 -0700612 * drm_crtc_init_with_planes - Initialise a new CRTC object with
613 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800614 * @dev: DRM device
615 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700616 * @primary: Primary plane for CRTC
617 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800618 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200619 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800620 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000621 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200622 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100623 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200624 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800625 */
Matt Ropere13161a2014-04-01 15:22:38 -0700626int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
627 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700628 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200629 const struct drm_crtc_funcs *funcs,
630 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800631{
Rob Clark51fd3712013-11-19 12:10:12 -0500632 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200633 int ret;
634
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100635 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
636 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
637
Dave Airlief453ba02008-11-07 14:05:41 -0800638 crtc->dev = dev;
639 crtc->funcs = funcs;
640
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200641 INIT_LIST_HEAD(&crtc->commit_list);
642 spin_lock_init(&crtc->commit_lock);
643
Rob Clark51fd3712013-11-19 12:10:12 -0500644 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200645 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
646 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100647 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800648
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200649 if (name) {
650 va_list ap;
651
652 va_start(ap, name);
653 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
654 va_end(ap);
655 } else {
656 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
657 drm_num_crtcs(dev));
658 }
659 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000660 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200661 return -ENOMEM;
662 }
663
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300664 crtc->base.properties = &crtc->properties;
665
Rob Clark51fd3712013-11-19 12:10:12 -0500666 list_add_tail(&crtc->head, &config->crtc_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100667 crtc->index = config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200668
Matt Ropere13161a2014-04-01 15:22:38 -0700669 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700670 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700671 if (primary)
672 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700673 if (cursor)
674 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700675
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100676 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
677 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100678 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100679 }
680
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100681 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800682}
Matt Ropere13161a2014-04-01 15:22:38 -0700683EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800684
685/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000686 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800687 * @crtc: CRTC to cleanup
688 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000689 * This function cleans up @crtc and removes it from the DRM mode setting
690 * core. Note that the function does *not* free the crtc structure itself,
691 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800692 */
693void drm_crtc_cleanup(struct drm_crtc *crtc)
694{
695 struct drm_device *dev = crtc->dev;
696
Chris Wilson490d3d12016-05-27 20:05:00 +0100697 /* Note that the crtc_list is considered to be static; should we
698 * remove the drm_crtc at runtime we would have to decrement all
699 * the indices on the drm_crtc after us in the crtc_list.
700 */
701
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000702 kfree(crtc->gamma_store);
703 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800704
Rob Clark51fd3712013-11-19 12:10:12 -0500705 drm_modeset_lock_fini(&crtc->mutex);
706
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000707 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800708 list_del(&crtc->head);
709 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100710
711 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
712 if (crtc->state && crtc->funcs->atomic_destroy_state)
713 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100714
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200715 kfree(crtc->name);
716
Thierry Redinga18c0af2014-12-10 11:38:49 +0100717 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800718}
719EXPORT_SYMBOL(drm_crtc_cleanup);
720
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100721/*
Dave Airlief453ba02008-11-07 14:05:41 -0800722 * drm_mode_remove - remove and free a mode
723 * @connector: connector list to modify
724 * @mode: mode to remove
725 *
Dave Airlief453ba02008-11-07 14:05:41 -0800726 * Remove @mode from @connector's mode list, then free it.
727 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100728static void drm_mode_remove(struct drm_connector *connector,
729 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800730{
731 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100732 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800733}
Dave Airlief453ba02008-11-07 14:05:41 -0800734
735/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200736 * drm_display_info_set_bus_formats - set the supported bus formats
737 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100738 * @formats: array containing the supported bus formats
739 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200740 *
741 * Store the supported bus formats in display info structure.
742 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
743 * a full list of available formats.
744 */
745int drm_display_info_set_bus_formats(struct drm_display_info *info,
746 const u32 *formats,
747 unsigned int num_formats)
748{
749 u32 *fmts = NULL;
750
751 if (!formats && num_formats)
752 return -EINVAL;
753
754 if (formats && num_formats) {
755 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
756 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300757 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200758 return -ENOMEM;
759 }
760
761 kfree(info->bus_formats);
762 info->bus_formats = fmts;
763 info->num_bus_formats = num_formats;
764
765 return 0;
766}
767EXPORT_SYMBOL(drm_display_info_set_bus_formats);
768
769/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200770 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
771 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200772 *
773 * The kernel supports per-connector configration of its consoles through
774 * use of the video= parameter. This function parses that option and
775 * extracts the user's specified mode (or enable/disable status) for a
776 * particular connector. This is typically only used during the early fbdev
777 * setup.
778 */
779static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
780{
781 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
782 char *option = NULL;
783
784 if (fb_get_options(connector->name, &option))
785 return;
786
787 if (!drm_mode_parse_command_line_for_connector(option,
788 connector,
789 mode))
790 return;
791
792 if (mode->force) {
793 const char *s;
794
795 switch (mode->force) {
796 case DRM_FORCE_OFF:
797 s = "OFF";
798 break;
799 case DRM_FORCE_ON_DIGITAL:
800 s = "ON - dig";
801 break;
802 default:
803 case DRM_FORCE_ON:
804 s = "ON";
805 break;
806 }
807
808 DRM_INFO("forcing %s connector %s\n", connector->name, s);
809 connector->force = mode->force;
810 }
811
812 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
813 connector->name,
814 mode->xres, mode->yres,
815 mode->refresh_specified ? mode->refresh : 60,
816 mode->rb ? " reduced blanking" : "",
817 mode->margins ? " with margins" : "",
818 mode->interlace ? " interlaced" : "");
819}
820
Dave Airlieb164d312016-04-27 11:10:09 +1000821static void drm_connector_free(struct kref *kref)
822{
823 struct drm_connector *connector =
824 container_of(kref, struct drm_connector, base.refcount);
825 struct drm_device *dev = connector->dev;
826
827 drm_mode_object_unregister(dev, &connector->base);
828 connector->funcs->destroy(connector);
829}
830
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200831/**
Dave Airlief453ba02008-11-07 14:05:41 -0800832 * drm_connector_init - Init a preallocated connector
833 * @dev: DRM device
834 * @connector: the connector to init
835 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100836 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800837 *
Dave Airlief453ba02008-11-07 14:05:41 -0800838 * Initialises a preallocated connector. Connectors should be
839 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200840 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100841 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200842 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800843 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200844int drm_connector_init(struct drm_device *dev,
845 struct drm_connector *connector,
846 const struct drm_connector_funcs *funcs,
847 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800848{
Rob Clarkae16c592014-12-18 16:01:54 -0500849 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200850 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400851 struct ida *connector_ida =
852 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200853
Daniel Vetter84849902012-12-02 00:28:11 +0100854 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800855
Dave Airlieb164d312016-04-27 11:10:09 +1000856 ret = drm_mode_object_get_reg(dev, &connector->base,
857 DRM_MODE_OBJECT_CONNECTOR,
858 false, drm_connector_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200859 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300860 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200861
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300862 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800863 connector->dev = dev;
864 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100865
866 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
867 if (connector->connector_id < 0) {
868 ret = connector->connector_id;
869 goto out_put;
870 }
871
Dave Airlief453ba02008-11-07 14:05:41 -0800872 connector->connector_type = connector_type;
873 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400874 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
875 if (connector->connector_type_id < 0) {
876 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100877 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400878 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300879 connector->name =
880 kasprintf(GFP_KERNEL, "%s-%d",
881 drm_connector_enum_list[connector_type].name,
882 connector->connector_type_id);
883 if (!connector->name) {
884 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100885 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300886 }
887
Dave Airlief453ba02008-11-07 14:05:41 -0800888 INIT_LIST_HEAD(&connector->probed_modes);
889 INIT_LIST_HEAD(&connector->modes);
890 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000891 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800892
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200893 drm_connector_get_cmdline_mode(connector);
894
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100895 /* We should add connectors at the end to avoid upsetting the connector
896 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500897 list_add_tail(&connector->head, &config->connector_list);
898 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800899
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200900 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500901 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500902 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200903 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800904
Rob Clark58495562012-10-11 20:50:56 -0500905 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500906 config->dpms_property, 0);
907
908 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
909 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
910 }
Dave Airlief453ba02008-11-07 14:05:41 -0800911
Thomas Wood30f65702014-06-18 17:52:32 +0100912 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100913out_put_type_id:
914 if (ret)
915 ida_remove(connector_ida, connector->connector_type_id);
916out_put_id:
917 if (ret)
918 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300919out_put:
920 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000921 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300922
923out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100924 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200925
926 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800927}
928EXPORT_SYMBOL(drm_connector_init);
929
930/**
931 * drm_connector_cleanup - cleans up an initialised connector
932 * @connector: connector to cleanup
933 *
Dave Airlief453ba02008-11-07 14:05:41 -0800934 * Cleans up the connector but doesn't free the object.
935 */
936void drm_connector_cleanup(struct drm_connector *connector)
937{
938 struct drm_device *dev = connector->dev;
939 struct drm_display_mode *mode, *t;
940
Dave Airlie40d9b042014-10-20 16:29:33 +1000941 if (connector->tile_group) {
942 drm_mode_put_tile_group(dev, connector->tile_group);
943 connector->tile_group = NULL;
944 }
945
Dave Airlief453ba02008-11-07 14:05:41 -0800946 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
947 drm_mode_remove(connector, mode);
948
949 list_for_each_entry_safe(mode, t, &connector->modes, head)
950 drm_mode_remove(connector, mode);
951
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400952 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
953 connector->connector_type_id);
954
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100955 ida_remove(&dev->mode_config.connector_ida,
956 connector->connector_id);
957
Boris Brezillonb5571e92014-07-22 12:09:10 +0200958 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000959 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300960 kfree(connector->name);
961 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800962 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000963 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100964
965 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
966 if (connector->state && connector->funcs->atomic_destroy_state)
967 connector->funcs->atomic_destroy_state(connector,
968 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100969
970 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800971}
972EXPORT_SYMBOL(drm_connector_cleanup);
973
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100974/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100975 * drm_connector_register - register a connector
976 * @connector: the connector to register
977 *
978 * Register userspace interfaces for a connector
979 *
980 * Returns:
981 * Zero on success, error code on failure.
982 */
983int drm_connector_register(struct drm_connector *connector)
984{
Thomas Wood30f65702014-06-18 17:52:32 +0100985 int ret;
986
987 ret = drm_sysfs_connector_add(connector);
988 if (ret)
989 return ret;
990
991 ret = drm_debugfs_connector_add(connector);
992 if (ret) {
993 drm_sysfs_connector_remove(connector);
994 return ret;
995 }
996
Daniel Vetterfdf2c852016-05-06 14:55:02 +0200997 drm_mode_object_register(connector->dev, &connector->base);
998
Thomas Wood30f65702014-06-18 17:52:32 +0100999 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001000}
1001EXPORT_SYMBOL(drm_connector_register);
1002
1003/**
1004 * drm_connector_unregister - unregister a connector
1005 * @connector: the connector to unregister
1006 *
1007 * Unregister userspace interfaces for a connector
1008 */
1009void drm_connector_unregister(struct drm_connector *connector)
1010{
1011 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001012 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001013}
1014EXPORT_SYMBOL(drm_connector_unregister);
1015
Thomas Wood34ea3d32014-05-29 16:57:41 +01001016/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001017 * drm_connector_register_all - register all connectors
1018 * @dev: drm device
1019 *
1020 * This function registers all connectors in sysfs and other places so that
1021 * userspace can start to access them. Drivers can call it after calling
1022 * drm_dev_register() to complete the device registration, if they don't call
1023 * drm_connector_register() on each connector individually.
1024 *
1025 * When a device is unplugged and should be removed from userspace access,
1026 * call drm_connector_unregister_all(), which is the inverse of this
1027 * function.
1028 *
1029 * Returns:
1030 * Zero on success, error code on failure.
1031 */
1032int drm_connector_register_all(struct drm_device *dev)
1033{
1034 struct drm_connector *connector;
1035 int ret;
1036
1037 mutex_lock(&dev->mode_config.mutex);
1038
1039 drm_for_each_connector(connector, dev) {
1040 ret = drm_connector_register(connector);
1041 if (ret)
1042 goto err;
1043 }
1044
1045 mutex_unlock(&dev->mode_config.mutex);
1046
1047 return 0;
1048
1049err:
1050 mutex_unlock(&dev->mode_config.mutex);
1051 drm_connector_unregister_all(dev);
1052 return ret;
1053}
1054EXPORT_SYMBOL(drm_connector_register_all);
1055
1056/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001057 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001058 * @dev: drm device
1059 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001060 * This functions unregisters all connectors from sysfs and other places so
1061 * that userspace can no longer access them. Drivers should call this as the
1062 * first step tearing down the device instace, or when the underlying
1063 * physical device disappeared (e.g. USB unplug), right before calling
1064 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001065 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001066void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001067{
1068 struct drm_connector *connector;
1069
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001070 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001071 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001072 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001073}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001074EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001075
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001076/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001077 * drm_encoder_init - Init a preallocated encoder
1078 * @dev: drm device
1079 * @encoder: the encoder to init
1080 * @funcs: callbacks for this encoder
1081 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001082 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001083 *
1084 * Initialises a preallocated encoder. Encoder should be
1085 * subclassed as part of driver encoder objects.
1086 *
1087 * Returns:
1088 * Zero on success, error code on failure.
1089 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001090int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001091 struct drm_encoder *encoder,
1092 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001093 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001094{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001095 int ret;
1096
Daniel Vetter84849902012-12-02 00:28:11 +01001097 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001098
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001099 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1100 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001101 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001102
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001103 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001104 encoder->encoder_type = encoder_type;
1105 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001106 if (name) {
1107 va_list ap;
1108
1109 va_start(ap, name);
1110 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1111 va_end(ap);
1112 } else {
1113 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1114 drm_encoder_enum_list[encoder_type].name,
1115 encoder->base.id);
1116 }
Jani Nikulae5748942014-05-14 16:58:20 +03001117 if (!encoder->name) {
1118 ret = -ENOMEM;
1119 goto out_put;
1120 }
Dave Airlief453ba02008-11-07 14:05:41 -08001121
1122 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001123 encoder->index = dev->mode_config.num_encoder++;
Dave Airlief453ba02008-11-07 14:05:41 -08001124
Jani Nikulae5748942014-05-14 16:58:20 +03001125out_put:
1126 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001127 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001128
1129out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001130 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001131
1132 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001133}
1134EXPORT_SYMBOL(drm_encoder_init);
1135
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001136/**
1137 * drm_encoder_cleanup - cleans up an initialised encoder
1138 * @encoder: encoder to cleanup
1139 *
1140 * Cleans up the encoder but doesn't free the object.
1141 */
Dave Airlief453ba02008-11-07 14:05:41 -08001142void drm_encoder_cleanup(struct drm_encoder *encoder)
1143{
1144 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001145
Chris Wilson490d3d12016-05-27 20:05:00 +01001146 /* Note that the encoder_list is considered to be static; should we
1147 * remove the drm_encoder at runtime we would have to decrement all
1148 * the indices on the drm_encoder after us in the encoder_list.
1149 */
1150
Daniel Vetter84849902012-12-02 00:28:11 +01001151 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001152 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001153 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001154 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001155 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001156 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001157
1158 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001159}
1160EXPORT_SYMBOL(drm_encoder_cleanup);
1161
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001162static unsigned int drm_num_planes(struct drm_device *dev)
1163{
1164 unsigned int num = 0;
1165 struct drm_plane *tmp;
1166
1167 drm_for_each_plane(tmp, dev) {
1168 num++;
1169 }
1170
1171 return num;
1172}
1173
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001174/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001175 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001176 * @dev: DRM device
1177 * @plane: plane object to init
1178 * @possible_crtcs: bitmask of possible CRTCs
1179 * @funcs: callbacks for the new plane
1180 * @formats: array of supported formats (%DRM_FORMAT_*)
1181 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001182 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001183 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001184 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001185 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001186 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001187 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001188 * Zero on success, error code on failure.
1189 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001190int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1191 unsigned long possible_crtcs,
1192 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001193 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001194 enum drm_plane_type type,
1195 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001196{
Rob Clark6b4959f2014-12-18 16:01:53 -05001197 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001198 int ret;
1199
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001200 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1201 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001202 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001203
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001204 drm_modeset_lock_init(&plane->mutex);
1205
Rob Clark4d939142012-05-17 02:23:27 -06001206 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001207 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001208 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001209 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1210 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001211 if (!plane->format_types) {
1212 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001213 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001214 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001215 }
1216
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001217 if (name) {
1218 va_list ap;
1219
1220 va_start(ap, name);
1221 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1222 va_end(ap);
1223 } else {
1224 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1225 drm_num_planes(dev));
1226 }
1227 if (!plane->name) {
1228 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001229 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001230 return -ENOMEM;
1231 }
1232
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001233 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001234 plane->format_count = format_count;
1235 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001236 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001237
Rob Clark6b4959f2014-12-18 16:01:53 -05001238 list_add_tail(&plane->head, &config->plane_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001239 plane->index = config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001240 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001241 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001242
Rob Clark9922ab52014-04-01 20:16:57 -04001243 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001244 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001245 plane->type);
1246
Rob Clark6b4959f2014-12-18 16:01:53 -05001247 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1248 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1249 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1250 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1251 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1252 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1253 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1254 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1255 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1256 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1257 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1258 }
1259
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001260 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001261}
Matt Roperdc415ff2014-04-01 15:22:36 -07001262EXPORT_SYMBOL(drm_universal_plane_init);
1263
1264/**
1265 * drm_plane_init - Initialize a legacy plane
1266 * @dev: DRM device
1267 * @plane: plane object to init
1268 * @possible_crtcs: bitmask of possible CRTCs
1269 * @funcs: callbacks for the new plane
1270 * @formats: array of supported formats (%DRM_FORMAT_*)
1271 * @format_count: number of elements in @formats
1272 * @is_primary: plane type (primary vs overlay)
1273 *
1274 * Legacy API to initialize a DRM plane.
1275 *
1276 * New drivers should call drm_universal_plane_init() instead.
1277 *
1278 * Returns:
1279 * Zero on success, error code on failure.
1280 */
1281int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1282 unsigned long possible_crtcs,
1283 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001284 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001285 bool is_primary)
1286{
1287 enum drm_plane_type type;
1288
1289 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1290 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001291 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001292}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001293EXPORT_SYMBOL(drm_plane_init);
1294
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001295/**
1296 * drm_plane_cleanup - Clean up the core plane usage
1297 * @plane: plane to cleanup
1298 *
1299 * This function cleans up @plane and removes it from the DRM mode setting
1300 * core. Note that the function does *not* free the plane structure itself,
1301 * this is the responsibility of the caller.
1302 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001303void drm_plane_cleanup(struct drm_plane *plane)
1304{
1305 struct drm_device *dev = plane->dev;
1306
Daniel Vetter84849902012-12-02 00:28:11 +01001307 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001308 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001309 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001310
1311 BUG_ON(list_empty(&plane->head));
1312
Chris Wilson490d3d12016-05-27 20:05:00 +01001313 /* Note that the plane_list is considered to be static; should we
1314 * remove the drm_plane at runtime we would have to decrement all
1315 * the indices on the drm_plane after us in the plane_list.
1316 */
1317
Matt Roperdc415ff2014-04-01 15:22:36 -07001318 list_del(&plane->head);
1319 dev->mode_config.num_total_plane--;
1320 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1321 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001322 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001323
1324 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1325 if (plane->state && plane->funcs->atomic_destroy_state)
1326 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001327
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001328 kfree(plane->name);
1329
Thierry Redinga18c0af2014-12-10 11:38:49 +01001330 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001331}
1332EXPORT_SYMBOL(drm_plane_cleanup);
1333
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001334/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001335 * drm_plane_from_index - find the registered plane at an index
1336 * @dev: DRM device
1337 * @idx: index of registered plane to find for
1338 *
1339 * Given a plane index, return the registered plane from DRM device's
1340 * list of planes with matching index.
1341 */
1342struct drm_plane *
1343drm_plane_from_index(struct drm_device *dev, int idx)
1344{
1345 struct drm_plane *plane;
Chandra Konduruf81338a2015-04-09 17:36:21 -07001346
Chris Wilson490d3d12016-05-27 20:05:00 +01001347 drm_for_each_plane(plane, dev)
1348 if (idx == plane->index)
Chandra Konduruf81338a2015-04-09 17:36:21 -07001349 return plane;
Chris Wilson490d3d12016-05-27 20:05:00 +01001350
Chandra Konduruf81338a2015-04-09 17:36:21 -07001351 return NULL;
1352}
1353EXPORT_SYMBOL(drm_plane_from_index);
1354
1355/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001356 * drm_plane_force_disable - Forcibly disable a plane
1357 * @plane: plane to disable
1358 *
1359 * Forces the plane to be disabled.
1360 *
1361 * Used when the plane's current framebuffer is destroyed,
1362 * and when restoring fbdev mode.
1363 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001364void drm_plane_force_disable(struct drm_plane *plane)
1365{
1366 int ret;
1367
Daniel Vetter3d30a592014-07-27 13:42:42 +02001368 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001369 return;
1370
Daniel Vetter3d30a592014-07-27 13:42:42 +02001371 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001372 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001373 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001374 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001375 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001376 return;
1377 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001378 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001379 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001380 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001381 plane->fb = NULL;
1382 plane->crtc = NULL;
1383}
1384EXPORT_SYMBOL(drm_plane_force_disable);
1385
Rob Clark6b4959f2014-12-18 16:01:53 -05001386static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001387{
Rob Clark356af0e2014-12-18 16:01:52 -05001388 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001389
1390 /*
1391 * Standard properties (apply to all connectors)
1392 */
Rob Clark356af0e2014-12-18 16:01:52 -05001393 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001394 DRM_MODE_PROP_IMMUTABLE,
1395 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001396 if (!prop)
1397 return -ENOMEM;
1398 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001399
Rob Clark356af0e2014-12-18 16:01:52 -05001400 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001401 "DPMS", drm_dpms_enum_list,
1402 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001403 if (!prop)
1404 return -ENOMEM;
1405 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001406
Rob Clark356af0e2014-12-18 16:01:52 -05001407 prop = drm_property_create(dev,
1408 DRM_MODE_PROP_BLOB |
1409 DRM_MODE_PROP_IMMUTABLE,
1410 "PATH", 0);
1411 if (!prop)
1412 return -ENOMEM;
1413 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001414
Rob Clark356af0e2014-12-18 16:01:52 -05001415 prop = drm_property_create(dev,
1416 DRM_MODE_PROP_BLOB |
1417 DRM_MODE_PROP_IMMUTABLE,
1418 "TILE", 0);
1419 if (!prop)
1420 return -ENOMEM;
1421 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001422
Rob Clark6b4959f2014-12-18 16:01:53 -05001423 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001424 "type", drm_plane_type_enum_list,
1425 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001426 if (!prop)
1427 return -ENOMEM;
1428 dev->mode_config.plane_type_property = prop;
1429
1430 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1431 "SRC_X", 0, UINT_MAX);
1432 if (!prop)
1433 return -ENOMEM;
1434 dev->mode_config.prop_src_x = prop;
1435
1436 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1437 "SRC_Y", 0, UINT_MAX);
1438 if (!prop)
1439 return -ENOMEM;
1440 dev->mode_config.prop_src_y = prop;
1441
1442 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1443 "SRC_W", 0, UINT_MAX);
1444 if (!prop)
1445 return -ENOMEM;
1446 dev->mode_config.prop_src_w = prop;
1447
1448 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1449 "SRC_H", 0, UINT_MAX);
1450 if (!prop)
1451 return -ENOMEM;
1452 dev->mode_config.prop_src_h = prop;
1453
1454 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1455 "CRTC_X", INT_MIN, INT_MAX);
1456 if (!prop)
1457 return -ENOMEM;
1458 dev->mode_config.prop_crtc_x = prop;
1459
1460 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1461 "CRTC_Y", INT_MIN, INT_MAX);
1462 if (!prop)
1463 return -ENOMEM;
1464 dev->mode_config.prop_crtc_y = prop;
1465
1466 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1467 "CRTC_W", 0, INT_MAX);
1468 if (!prop)
1469 return -ENOMEM;
1470 dev->mode_config.prop_crtc_w = prop;
1471
1472 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1473 "CRTC_H", 0, INT_MAX);
1474 if (!prop)
1475 return -ENOMEM;
1476 dev->mode_config.prop_crtc_h = prop;
1477
1478 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1479 "FB_ID", DRM_MODE_OBJECT_FB);
1480 if (!prop)
1481 return -ENOMEM;
1482 dev->mode_config.prop_fb_id = prop;
1483
1484 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1485 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1486 if (!prop)
1487 return -ENOMEM;
1488 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001489
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001490 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1491 "ACTIVE");
1492 if (!prop)
1493 return -ENOMEM;
1494 dev->mode_config.prop_active = prop;
1495
Daniel Stone955f3c32015-05-25 19:11:52 +01001496 prop = drm_property_create(dev,
1497 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1498 "MODE_ID", 0);
1499 if (!prop)
1500 return -ENOMEM;
1501 dev->mode_config.prop_mode_id = prop;
1502
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001503 prop = drm_property_create(dev,
1504 DRM_MODE_PROP_BLOB,
1505 "DEGAMMA_LUT", 0);
1506 if (!prop)
1507 return -ENOMEM;
1508 dev->mode_config.degamma_lut_property = prop;
1509
1510 prop = drm_property_create_range(dev,
1511 DRM_MODE_PROP_IMMUTABLE,
1512 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1513 if (!prop)
1514 return -ENOMEM;
1515 dev->mode_config.degamma_lut_size_property = prop;
1516
1517 prop = drm_property_create(dev,
1518 DRM_MODE_PROP_BLOB,
1519 "CTM", 0);
1520 if (!prop)
1521 return -ENOMEM;
1522 dev->mode_config.ctm_property = prop;
1523
1524 prop = drm_property_create(dev,
1525 DRM_MODE_PROP_BLOB,
1526 "GAMMA_LUT", 0);
1527 if (!prop)
1528 return -ENOMEM;
1529 dev->mode_config.gamma_lut_property = prop;
1530
1531 prop = drm_property_create_range(dev,
1532 DRM_MODE_PROP_IMMUTABLE,
1533 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1534 if (!prop)
1535 return -ENOMEM;
1536 dev->mode_config.gamma_lut_size_property = prop;
1537
Rob Clark9922ab52014-04-01 20:16:57 -04001538 return 0;
1539}
1540
Dave Airlief453ba02008-11-07 14:05:41 -08001541/**
1542 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1543 * @dev: DRM device
1544 *
1545 * Called by a driver the first time a DVI-I connector is made.
1546 */
1547int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1548{
1549 struct drm_property *dvi_i_selector;
1550 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001551
1552 if (dev->mode_config.dvi_i_select_subconnector_property)
1553 return 0;
1554
1555 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001556 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001557 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001558 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001559 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001560 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1561
Sascha Hauer4a67d392012-02-06 10:58:17 +01001562 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001563 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001564 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001565 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001566 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1567
1568 return 0;
1569}
1570EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1571
1572/**
1573 * drm_create_tv_properties - create TV specific connector properties
1574 * @dev: DRM device
1575 * @num_modes: number of different TV formats (modes) supported
1576 * @modes: array of pointers to strings containing name of each format
1577 *
1578 * Called by a driver's TV initialization routine, this function creates
1579 * the TV specific connector properties for a given device. Caller is
1580 * responsible for allocating a list of format names and passing them to
1581 * this routine.
1582 */
Thierry Reding2f763312014-10-13 12:45:57 +02001583int drm_mode_create_tv_properties(struct drm_device *dev,
1584 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001585 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001586{
1587 struct drm_property *tv_selector;
1588 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001589 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001590
1591 if (dev->mode_config.tv_select_subconnector_property)
1592 return 0;
1593
1594 /*
1595 * Basic connector properties
1596 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001597 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001598 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001599 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001600 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001601 if (!tv_selector)
1602 goto nomem;
1603
Dave Airlief453ba02008-11-07 14:05:41 -08001604 dev->mode_config.tv_select_subconnector_property = tv_selector;
1605
1606 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001607 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1608 "subconnector",
1609 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001610 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001611 if (!tv_subconnector)
1612 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001613 dev->mode_config.tv_subconnector_property = tv_subconnector;
1614
1615 /*
1616 * Other, TV specific properties: margins & TV modes.
1617 */
1618 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001619 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001620 if (!dev->mode_config.tv_left_margin_property)
1621 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001622
1623 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001624 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001625 if (!dev->mode_config.tv_right_margin_property)
1626 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001627
1628 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001629 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001630 if (!dev->mode_config.tv_top_margin_property)
1631 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001632
1633 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001634 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001635 if (!dev->mode_config.tv_bottom_margin_property)
1636 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001637
1638 dev->mode_config.tv_mode_property =
1639 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1640 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001641 if (!dev->mode_config.tv_mode_property)
1642 goto nomem;
1643
Dave Airlief453ba02008-11-07 14:05:41 -08001644 for (i = 0; i < num_modes; i++)
1645 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1646 i, modes[i]);
1647
Francisco Jerezb6b79022009-08-02 04:19:20 +02001648 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001649 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001650 if (!dev->mode_config.tv_brightness_property)
1651 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001652
1653 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001654 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001655 if (!dev->mode_config.tv_contrast_property)
1656 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001657
1658 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001659 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001660 if (!dev->mode_config.tv_flicker_reduction_property)
1661 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001662
Francisco Jereza75f0232009-08-12 02:30:10 +02001663 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001664 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001665 if (!dev->mode_config.tv_overscan_property)
1666 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001667
1668 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001669 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001670 if (!dev->mode_config.tv_saturation_property)
1671 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001672
1673 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001674 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001675 if (!dev->mode_config.tv_hue_property)
1676 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001677
Dave Airlief453ba02008-11-07 14:05:41 -08001678 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001679nomem:
1680 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001681}
1682EXPORT_SYMBOL(drm_mode_create_tv_properties);
1683
1684/**
1685 * drm_mode_create_scaling_mode_property - create scaling mode property
1686 * @dev: DRM device
1687 *
1688 * Called by a driver the first time it's needed, must be attached to desired
1689 * connectors.
1690 */
1691int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1692{
1693 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001694
1695 if (dev->mode_config.scaling_mode_property)
1696 return 0;
1697
1698 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001699 drm_property_create_enum(dev, 0, "scaling mode",
1700 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001701 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001702
1703 dev->mode_config.scaling_mode_property = scaling_mode;
1704
1705 return 0;
1706}
1707EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1708
1709/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301710 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1711 * @dev: DRM device
1712 *
1713 * Called by a driver the first time it's needed, must be attached to desired
1714 * connectors.
1715 *
1716 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001717 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301718 */
1719int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1720{
1721 if (dev->mode_config.aspect_ratio_property)
1722 return 0;
1723
1724 dev->mode_config.aspect_ratio_property =
1725 drm_property_create_enum(dev, 0, "aspect ratio",
1726 drm_aspect_ratio_enum_list,
1727 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1728
1729 if (dev->mode_config.aspect_ratio_property == NULL)
1730 return -ENOMEM;
1731
1732 return 0;
1733}
1734EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1735
1736/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001737 * drm_mode_create_dirty_property - create dirty property
1738 * @dev: DRM device
1739 *
1740 * Called by a driver the first time it's needed, must be attached to desired
1741 * connectors.
1742 */
1743int drm_mode_create_dirty_info_property(struct drm_device *dev)
1744{
1745 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001746
1747 if (dev->mode_config.dirty_info_property)
1748 return 0;
1749
1750 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001751 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001752 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001753 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001754 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001755 dev->mode_config.dirty_info_property = dirty_info;
1756
1757 return 0;
1758}
1759EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1760
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001761/**
1762 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1763 * @dev: DRM device
1764 *
1765 * Create the the suggested x/y offset property for connectors.
1766 */
1767int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1768{
1769 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1770 return 0;
1771
1772 dev->mode_config.suggested_x_property =
1773 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1774
1775 dev->mode_config.suggested_y_property =
1776 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1777
1778 if (dev->mode_config.suggested_x_property == NULL ||
1779 dev->mode_config.suggested_y_property == NULL)
1780 return -ENOMEM;
1781 return 0;
1782}
1783EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1784
Dave Airlief453ba02008-11-07 14:05:41 -08001785/**
Dave Airlief453ba02008-11-07 14:05:41 -08001786 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001787 * @dev: drm device for the ioctl
1788 * @data: data pointer for the ioctl
1789 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001790 *
Dave Airlief453ba02008-11-07 14:05:41 -08001791 * Construct a set of configuration description structures and return
1792 * them to the user, including CRTC, connector and framebuffer configuration.
1793 *
1794 * Called by the user via ioctl.
1795 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001796 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001797 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001798 */
1799int drm_mode_getresources(struct drm_device *dev, void *data,
1800 struct drm_file *file_priv)
1801{
1802 struct drm_mode_card_res *card_res = data;
1803 struct list_head *lh;
1804 struct drm_framebuffer *fb;
1805 struct drm_connector *connector;
1806 struct drm_crtc *crtc;
1807 struct drm_encoder *encoder;
1808 int ret = 0;
1809 int connector_count = 0;
1810 int crtc_count = 0;
1811 int fb_count = 0;
1812 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001813 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001814 uint32_t __user *fb_id;
1815 uint32_t __user *crtc_id;
1816 uint32_t __user *connector_id;
1817 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001818
Dave Airliefb3b06c2011-02-08 13:55:21 +10001819 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1820 return -EINVAL;
1821
Dave Airlief453ba02008-11-07 14:05:41 -08001822
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001823 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001824 /*
1825 * For the non-control nodes we need to limit the list of resources
1826 * by IDs in the group list for this node
1827 */
1828 list_for_each(lh, &file_priv->fbs)
1829 fb_count++;
1830
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001831 /* handle this in 4 parts */
1832 /* FBs */
1833 if (card_res->count_fbs >= fb_count) {
1834 copied = 0;
1835 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1836 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1837 if (put_user(fb->base.id, fb_id + copied)) {
1838 mutex_unlock(&file_priv->fbs_lock);
1839 return -EFAULT;
1840 }
1841 copied++;
1842 }
1843 }
1844 card_res->count_fbs = fb_count;
1845 mutex_unlock(&file_priv->fbs_lock);
1846
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001847 /* mode_config.mutex protects the connector list against e.g. DP MST
1848 * connector hot-adding. CRTC/Plane lists are invariant. */
1849 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001850 drm_for_each_crtc(crtc, dev)
1851 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001852
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001853 drm_for_each_connector(connector, dev)
1854 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001855
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001856 drm_for_each_encoder(encoder, dev)
1857 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001858
1859 card_res->max_height = dev->mode_config.max_height;
1860 card_res->min_height = dev->mode_config.min_height;
1861 card_res->max_width = dev->mode_config.max_width;
1862 card_res->min_width = dev->mode_config.min_width;
1863
Dave Airlief453ba02008-11-07 14:05:41 -08001864 /* CRTCs */
1865 if (card_res->count_crtcs >= crtc_count) {
1866 copied = 0;
1867 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001868 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001869 if (put_user(crtc->base.id, crtc_id + copied)) {
1870 ret = -EFAULT;
1871 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001872 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001873 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001874 }
1875 }
1876 card_res->count_crtcs = crtc_count;
1877
1878 /* Encoders */
1879 if (card_res->count_encoders >= encoder_count) {
1880 copied = 0;
1881 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001882 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001883 if (put_user(encoder->base.id, encoder_id +
1884 copied)) {
1885 ret = -EFAULT;
1886 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001887 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001888 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001889 }
1890 }
1891 card_res->count_encoders = encoder_count;
1892
1893 /* Connectors */
1894 if (card_res->count_connectors >= connector_count) {
1895 copied = 0;
1896 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001897 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001898 if (put_user(connector->base.id,
1899 connector_id + copied)) {
1900 ret = -EFAULT;
1901 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001902 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001903 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001904 }
1905 }
1906 card_res->count_connectors = connector_count;
1907
Dave Airlief453ba02008-11-07 14:05:41 -08001908out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001909 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001910 return ret;
1911}
1912
1913/**
1914 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001915 * @dev: drm device for the ioctl
1916 * @data: data pointer for the ioctl
1917 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001918 *
Dave Airlief453ba02008-11-07 14:05:41 -08001919 * Construct a CRTC configuration structure to return to the user.
1920 *
1921 * Called by the user via ioctl.
1922 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001923 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001924 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001925 */
1926int drm_mode_getcrtc(struct drm_device *dev,
1927 void *data, struct drm_file *file_priv)
1928{
1929 struct drm_mode_crtc *crtc_resp = data;
1930 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001931
Dave Airliefb3b06c2011-02-08 13:55:21 +10001932 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1933 return -EINVAL;
1934
Rob Clarka2b34e22013-10-05 16:36:52 -04001935 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001936 if (!crtc)
1937 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001938
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001939 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08001940 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001941 if (crtc->primary->fb)
1942 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001943 else
1944 crtc_resp->fb_id = 0;
1945
Daniel Vetter31c946e2015-02-22 12:24:17 +01001946 if (crtc->state) {
1947 crtc_resp->x = crtc->primary->state->src_x >> 16;
1948 crtc_resp->y = crtc->primary->state->src_y >> 16;
1949 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001950 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001951 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08001952
Daniel Vetter31c946e2015-02-22 12:24:17 +01001953 } else {
1954 crtc_resp->mode_valid = 0;
1955 }
Dave Airlief453ba02008-11-07 14:05:41 -08001956 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01001957 crtc_resp->x = crtc->x;
1958 crtc_resp->y = crtc->y;
1959 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001960 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001961 crtc_resp->mode_valid = 1;
1962
1963 } else {
1964 crtc_resp->mode_valid = 0;
1965 }
Dave Airlief453ba02008-11-07 14:05:41 -08001966 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001967 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08001968
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001969 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001970}
1971
Damien Lespiau61d8e322013-09-25 16:45:22 +01001972static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1973 const struct drm_file *file_priv)
1974{
1975 /*
1976 * If user-space hasn't configured the driver to expose the stereo 3D
1977 * modes, don't expose them.
1978 */
1979 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1980 return false;
1981
1982 return true;
1983}
1984
Daniel Vetterabd69c52014-11-25 23:50:05 +01001985static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1986{
1987 /* For atomic drivers only state objects are synchronously updated and
1988 * protected by modeset locks, so check those first. */
1989 if (connector->state)
1990 return connector->state->best_encoder;
1991 return connector->encoder;
1992}
1993
Rob Clark95cbf112014-12-18 16:01:49 -05001994/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05001995static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05001996 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
1997 uint32_t *arg_count_props)
1998{
Rob Clark88a48e22014-12-18 16:01:50 -05001999 int props_count;
2000 int i, ret, copied;
2001
2002 props_count = obj->properties->count;
2003 if (!atomic)
2004 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002005
2006 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002007 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002008 struct drm_property *prop = obj->properties->properties[i];
2009 uint64_t val;
2010
Rob Clark88a48e22014-12-18 16:01:50 -05002011 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2012 continue;
2013
Rob Clark95cbf112014-12-18 16:01:49 -05002014 ret = drm_object_property_get_value(obj, prop, &val);
2015 if (ret)
2016 return ret;
2017
2018 if (put_user(prop->base.id, prop_ptr + copied))
2019 return -EFAULT;
2020
2021 if (put_user(val, prop_values + copied))
2022 return -EFAULT;
2023
2024 copied++;
2025 }
2026 }
2027 *arg_count_props = props_count;
2028
2029 return 0;
2030}
2031
Dave Airlief453ba02008-11-07 14:05:41 -08002032/**
2033 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002034 * @dev: drm device for the ioctl
2035 * @data: data pointer for the ioctl
2036 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002037 *
Dave Airlief453ba02008-11-07 14:05:41 -08002038 * Construct a connector configuration structure to return to the user.
2039 *
2040 * Called by the user via ioctl.
2041 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002042 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002043 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002044 */
2045int drm_mode_getconnector(struct drm_device *dev, void *data,
2046 struct drm_file *file_priv)
2047{
2048 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002049 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002050 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002051 struct drm_display_mode *mode;
2052 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002053 int encoders_count = 0;
2054 int ret = 0;
2055 int copied = 0;
2056 int i;
2057 struct drm_mode_modeinfo u_mode;
2058 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002059 uint32_t __user *encoder_ptr;
2060
Dave Airliefb3b06c2011-02-08 13:55:21 +10002061 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2062 return -EINVAL;
2063
Dave Airlief453ba02008-11-07 14:05:41 -08002064 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2065
Daniel Vetter7b240562012-12-12 00:35:33 +01002066 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002067
Dave Airlieb164d312016-04-27 11:10:09 +10002068 connector = drm_connector_lookup(dev, out_resp->connector_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002069 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002070 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002071 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002072 }
Dave Airlief453ba02008-11-07 14:05:41 -08002073
Thierry Reding01073b02014-12-10 13:03:38 +01002074 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2075 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002076 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002077
2078 if (out_resp->count_modes == 0) {
2079 connector->funcs->fill_modes(connector,
2080 dev->mode_config.max_width,
2081 dev->mode_config.max_height);
2082 }
2083
2084 /* delayed so we get modes regardless of pre-fill_modes state */
2085 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002086 if (drm_mode_expose_to_userspace(mode, file_priv))
2087 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002088
2089 out_resp->connector_id = connector->base.id;
2090 out_resp->connector_type = connector->connector_type;
2091 out_resp->connector_type_id = connector->connector_type_id;
2092 out_resp->mm_width = connector->display_info.width_mm;
2093 out_resp->mm_height = connector->display_info.height_mm;
2094 out_resp->subpixel = connector->display_info.subpixel_order;
2095 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002096
2097 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002098 encoder = drm_connector_get_encoder(connector);
2099 if (encoder)
2100 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002101 else
2102 out_resp->encoder_id = 0;
2103
2104 /*
2105 * This ioctl is called twice, once to determine how much space is
2106 * needed, and the 2nd time to fill it.
2107 */
2108 if ((out_resp->count_modes >= mode_count) && mode_count) {
2109 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002110 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002111 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002112 if (!drm_mode_expose_to_userspace(mode, file_priv))
2113 continue;
2114
Daniel Stone934a8a82015-05-22 13:34:48 +01002115 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002116 if (copy_to_user(mode_ptr + copied,
2117 &u_mode, sizeof(u_mode))) {
2118 ret = -EFAULT;
2119 goto out;
2120 }
2121 copied++;
2122 }
2123 }
2124 out_resp->count_modes = mode_count;
2125
Rob Clark88a48e22014-12-18 16:01:50 -05002126 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002127 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2128 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2129 &out_resp->count_props);
2130 if (ret)
2131 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002132
2133 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2134 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002135 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002136 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2137 if (connector->encoder_ids[i] != 0) {
2138 if (put_user(connector->encoder_ids[i],
2139 encoder_ptr + copied)) {
2140 ret = -EFAULT;
2141 goto out;
2142 }
2143 copied++;
2144 }
2145 }
2146 }
2147 out_resp->count_encoders = encoders_count;
2148
2149out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002150 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002151
Dave Airlieb164d312016-04-27 11:10:09 +10002152 drm_connector_unreference(connector);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002153out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002154 mutex_unlock(&dev->mode_config.mutex);
2155
Dave Airlief453ba02008-11-07 14:05:41 -08002156 return ret;
2157}
2158
Daniel Vetterabd69c52014-11-25 23:50:05 +01002159static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2160{
2161 struct drm_connector *connector;
2162 struct drm_device *dev = encoder->dev;
2163 bool uses_atomic = false;
2164
2165 /* For atomic drivers only state objects are synchronously updated and
2166 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002167 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002168 if (!connector->state)
2169 continue;
2170
2171 uses_atomic = true;
2172
2173 if (connector->state->best_encoder != encoder)
2174 continue;
2175
2176 return connector->state->crtc;
2177 }
2178
2179 /* Don't return stale data (e.g. pending async disable). */
2180 if (uses_atomic)
2181 return NULL;
2182
2183 return encoder->crtc;
2184}
2185
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002186/**
2187 * drm_mode_getencoder - get encoder configuration
2188 * @dev: drm device for the ioctl
2189 * @data: data pointer for the ioctl
2190 * @file_priv: drm file for the ioctl call
2191 *
2192 * Construct a encoder configuration structure to return to the user.
2193 *
2194 * Called by the user via ioctl.
2195 *
2196 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002197 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002198 */
Dave Airlief453ba02008-11-07 14:05:41 -08002199int drm_mode_getencoder(struct drm_device *dev, void *data,
2200 struct drm_file *file_priv)
2201{
2202 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002203 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002204 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002205
Dave Airliefb3b06c2011-02-08 13:55:21 +10002206 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2207 return -EINVAL;
2208
Rob Clarka2b34e22013-10-05 16:36:52 -04002209 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002210 if (!encoder)
2211 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002212
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002213 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002214 crtc = drm_encoder_get_crtc(encoder);
2215 if (crtc)
2216 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002217 else
2218 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002219 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2220
Dave Airlief453ba02008-11-07 14:05:41 -08002221 enc_resp->encoder_type = encoder->encoder_type;
2222 enc_resp->encoder_id = encoder->base.id;
2223 enc_resp->possible_crtcs = encoder->possible_crtcs;
2224 enc_resp->possible_clones = encoder->possible_clones;
2225
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002226 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002227}
2228
2229/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002230 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002231 * @dev: DRM device
2232 * @data: ioctl data
2233 * @file_priv: DRM file info
2234 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002235 * Construct a list of plane ids to return to the user.
2236 *
2237 * Called by the user via ioctl.
2238 *
2239 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002240 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002241 */
2242int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002243 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002244{
2245 struct drm_mode_get_plane_res *plane_resp = data;
2246 struct drm_mode_config *config;
2247 struct drm_plane *plane;
2248 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002249 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002250 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002251
2252 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2253 return -EINVAL;
2254
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002255 config = &dev->mode_config;
2256
Matt Roper681e7ec2014-04-01 15:22:42 -07002257 if (file_priv->universal_planes)
2258 num_planes = config->num_total_plane;
2259 else
2260 num_planes = config->num_overlay_plane;
2261
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002262 /*
2263 * This ioctl is called twice, once to determine how much space is
2264 * needed, and the 2nd time to fill it.
2265 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002266 if (num_planes &&
2267 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002268 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002269
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002270 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002271 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002272 /*
2273 * Unless userspace set the 'universal planes'
2274 * capability bit, only advertise overlays.
2275 */
2276 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2277 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002278 continue;
2279
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002280 if (put_user(plane->base.id, plane_ptr + copied))
2281 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002282 copied++;
2283 }
2284 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002285 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002286
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002287 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002288}
2289
2290/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002291 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002292 * @dev: DRM device
2293 * @data: ioctl data
2294 * @file_priv: DRM file info
2295 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002296 * Construct a plane configuration structure to return to the user.
2297 *
2298 * Called by the user via ioctl.
2299 *
2300 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002301 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002302 */
2303int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002304 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002305{
2306 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002307 struct drm_plane *plane;
2308 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002309
2310 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2311 return -EINVAL;
2312
Rob Clarka2b34e22013-10-05 16:36:52 -04002313 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002314 if (!plane)
2315 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002316
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002317 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002318 if (plane->crtc)
2319 plane_resp->crtc_id = plane->crtc->base.id;
2320 else
2321 plane_resp->crtc_id = 0;
2322
2323 if (plane->fb)
2324 plane_resp->fb_id = plane->fb->base.id;
2325 else
2326 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002327 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002328
2329 plane_resp->plane_id = plane->base.id;
2330 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002331 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002332
2333 /*
2334 * This ioctl is called twice, once to determine how much space is
2335 * needed, and the 2nd time to fill it.
2336 */
2337 if (plane->format_count &&
2338 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002339 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002340 if (copy_to_user(format_ptr,
2341 plane->format_types,
2342 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002343 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002344 }
2345 }
2346 plane_resp->count_format_types = plane->format_count;
2347
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002348 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002349}
2350
Laurent Pinchartead86102015-03-05 02:25:43 +02002351/**
2352 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2353 * @plane: plane to check for format support
2354 * @format: the pixel format
2355 *
2356 * Returns:
2357 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2358 * otherwise.
2359 */
2360int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2361{
2362 unsigned int i;
2363
2364 for (i = 0; i < plane->format_count; i++) {
2365 if (format == plane->format_types[i])
2366 return 0;
2367 }
2368
2369 return -EINVAL;
2370}
2371
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002372static int check_src_coords(uint32_t src_x, uint32_t src_y,
2373 uint32_t src_w, uint32_t src_h,
2374 const struct drm_framebuffer *fb)
2375{
2376 unsigned int fb_width, fb_height;
2377
2378 fb_width = fb->width << 16;
2379 fb_height = fb->height << 16;
2380
2381 /* Make sure source coordinates are inside the fb. */
2382 if (src_w > fb_width ||
2383 src_x > fb_width - src_w ||
2384 src_h > fb_height ||
2385 src_y > fb_height - src_h) {
2386 DRM_DEBUG_KMS("Invalid source coordinates "
2387 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2388 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2389 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2390 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2391 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2392 return -ENOSPC;
2393 }
2394
2395 return 0;
2396}
2397
Matt Roperb36552b2014-06-10 08:28:09 -07002398/*
2399 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002400 *
Matt Roperb36552b2014-06-10 08:28:09 -07002401 * Note that we assume an extra reference has already been taken on fb. If the
2402 * update fails, this reference will be dropped before return; if it succeeds,
2403 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002404 *
Matt Roperb36552b2014-06-10 08:28:09 -07002405 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002406 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002407static int __setplane_internal(struct drm_plane *plane,
2408 struct drm_crtc *crtc,
2409 struct drm_framebuffer *fb,
2410 int32_t crtc_x, int32_t crtc_y,
2411 uint32_t crtc_w, uint32_t crtc_h,
2412 /* src_{x,y,w,h} values are 16.16 fixed point */
2413 uint32_t src_x, uint32_t src_y,
2414 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002415{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002416 int ret = 0;
2417
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002418 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002419 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002420 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002421 ret = plane->funcs->disable_plane(plane);
2422 if (!ret) {
2423 plane->crtc = NULL;
2424 plane->fb = NULL;
2425 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002426 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002427 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002428 goto out;
2429 }
2430
Matt Roper7f994f32014-05-29 08:06:51 -07002431 /* Check whether this plane is usable on this CRTC */
2432 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2433 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2434 ret = -EINVAL;
2435 goto out;
2436 }
2437
Ville Syrjälä62443be2011-12-20 00:06:47 +02002438 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002439 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2440 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002441 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2442 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002443 goto out;
2444 }
2445
Matt Roper3968be92015-04-13 11:06:13 -07002446 /* Give drivers some help against integer overflows */
2447 if (crtc_w > INT_MAX ||
2448 crtc_x > INT_MAX - (int32_t) crtc_w ||
2449 crtc_h > INT_MAX ||
2450 crtc_y > INT_MAX - (int32_t) crtc_h) {
2451 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2452 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002453 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002454 goto out;
2455 }
2456
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002457 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2458 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002459 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002460
Daniel Vetter3d30a592014-07-27 13:42:42 +02002461 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002462 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002463 crtc_x, crtc_y, crtc_w, crtc_h,
2464 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002465 if (!ret) {
2466 plane->crtc = crtc;
2467 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002468 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002469 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002470 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002471 }
2472
2473out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002474 if (fb)
2475 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002476 if (plane->old_fb)
2477 drm_framebuffer_unreference(plane->old_fb);
2478 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002479
2480 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002481}
Matt Roperb36552b2014-06-10 08:28:09 -07002482
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002483static int setplane_internal(struct drm_plane *plane,
2484 struct drm_crtc *crtc,
2485 struct drm_framebuffer *fb,
2486 int32_t crtc_x, int32_t crtc_y,
2487 uint32_t crtc_w, uint32_t crtc_h,
2488 /* src_{x,y,w,h} values are 16.16 fixed point */
2489 uint32_t src_x, uint32_t src_y,
2490 uint32_t src_w, uint32_t src_h)
2491{
2492 int ret;
2493
2494 drm_modeset_lock_all(plane->dev);
2495 ret = __setplane_internal(plane, crtc, fb,
2496 crtc_x, crtc_y, crtc_w, crtc_h,
2497 src_x, src_y, src_w, src_h);
2498 drm_modeset_unlock_all(plane->dev);
2499
2500 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002501}
2502
2503/**
2504 * drm_mode_setplane - configure a plane's configuration
2505 * @dev: DRM device
2506 * @data: ioctl data*
2507 * @file_priv: DRM file info
2508 *
2509 * Set plane configuration, including placement, fb, scaling, and other factors.
2510 * Or pass a NULL fb to disable (planes may be disabled without providing a
2511 * valid crtc).
2512 *
2513 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002514 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002515 */
2516int drm_mode_setplane(struct drm_device *dev, void *data,
2517 struct drm_file *file_priv)
2518{
2519 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002520 struct drm_plane *plane;
2521 struct drm_crtc *crtc = NULL;
2522 struct drm_framebuffer *fb = NULL;
2523
2524 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2525 return -EINVAL;
2526
Matt Roperb36552b2014-06-10 08:28:09 -07002527 /*
2528 * First, find the plane, crtc, and fb objects. If not available,
2529 * we don't bother to call the driver.
2530 */
Rob Clark933f6222014-11-25 20:33:11 -05002531 plane = drm_plane_find(dev, plane_req->plane_id);
2532 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002533 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2534 plane_req->plane_id);
2535 return -ENOENT;
2536 }
Matt Roperb36552b2014-06-10 08:28:09 -07002537
2538 if (plane_req->fb_id) {
2539 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2540 if (!fb) {
2541 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2542 plane_req->fb_id);
2543 return -ENOENT;
2544 }
2545
Rob Clark933f6222014-11-25 20:33:11 -05002546 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2547 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002548 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2549 plane_req->crtc_id);
2550 return -ENOENT;
2551 }
Matt Roperb36552b2014-06-10 08:28:09 -07002552 }
2553
Matt Roper161d0dc2014-06-10 08:28:10 -07002554 /*
2555 * setplane_internal will take care of deref'ing either the old or new
2556 * framebuffer depending on success.
2557 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002558 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002559 plane_req->crtc_x, plane_req->crtc_y,
2560 plane_req->crtc_w, plane_req->crtc_h,
2561 plane_req->src_x, plane_req->src_y,
2562 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002563}
2564
2565/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002566 * drm_mode_set_config_internal - helper to call ->set_config
2567 * @set: modeset config to set
2568 *
2569 * This is a little helper to wrap internal calls to the ->set_config driver
2570 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002571 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002572 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002573 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002574 */
2575int drm_mode_set_config_internal(struct drm_mode_set *set)
2576{
2577 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002578 struct drm_framebuffer *fb;
2579 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002580 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002581
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002582 /*
2583 * NOTE: ->set_config can also disable other crtcs (if we steal all
2584 * connectors from it), hence we need to refcount the fbs across all
2585 * crtcs. Atomic modeset will have saner semantics ...
2586 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002587 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002588 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002589
Daniel Vetterb0d12322012-12-11 01:07:12 +01002590 fb = set->fb;
2591
2592 ret = crtc->funcs->set_config(set);
2593 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002594 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002595 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002596 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002597
Daniel Vettere4f62542015-07-09 23:44:35 +02002598 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002599 if (tmp->primary->fb)
2600 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002601 if (tmp->primary->old_fb)
2602 drm_framebuffer_unreference(tmp->primary->old_fb);
2603 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002604 }
2605
2606 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002607}
2608EXPORT_SYMBOL(drm_mode_set_config_internal);
2609
Matt Roperaf936292014-04-01 15:22:34 -07002610/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002611 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2612 * @mode: mode to query
2613 * @hdisplay: hdisplay value to fill in
2614 * @vdisplay: vdisplay value to fill in
2615 *
2616 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2617 * the appropriate layout.
2618 */
2619void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2620 int *hdisplay, int *vdisplay)
2621{
2622 struct drm_display_mode adjusted;
2623
2624 drm_mode_copy(&adjusted, mode);
2625 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2626 *hdisplay = adjusted.crtc_hdisplay;
2627 *vdisplay = adjusted.crtc_vdisplay;
2628}
2629EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2630
2631/**
Matt Roperaf936292014-04-01 15:22:34 -07002632 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2633 * CRTC viewport
2634 * @crtc: CRTC that framebuffer will be displayed on
2635 * @x: x panning
2636 * @y: y panning
2637 * @mode: mode that framebuffer will be displayed under
2638 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002639 */
Matt Roperaf936292014-04-01 15:22:34 -07002640int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2641 int x, int y,
2642 const struct drm_display_mode *mode,
2643 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002644
2645{
2646 int hdisplay, vdisplay;
2647
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002648 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002649
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002650 if (crtc->state &&
2651 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2652 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002653 swap(hdisplay, vdisplay);
2654
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002655 return check_src_coords(x << 16, y << 16,
2656 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002657}
Matt Roperaf936292014-04-01 15:22:34 -07002658EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002659
Daniel Vetter2d13b672012-12-11 13:47:23 +01002660/**
Dave Airlief453ba02008-11-07 14:05:41 -08002661 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002662 * @dev: drm device for the ioctl
2663 * @data: data pointer for the ioctl
2664 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002665 *
Dave Airlief453ba02008-11-07 14:05:41 -08002666 * Build a new CRTC configuration based on user request.
2667 *
2668 * Called by the user via ioctl.
2669 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002670 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002671 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002672 */
2673int drm_mode_setcrtc(struct drm_device *dev, void *data,
2674 struct drm_file *file_priv)
2675{
2676 struct drm_mode_config *config = &dev->mode_config;
2677 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002678 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002679 struct drm_connector **connector_set = NULL, *connector;
2680 struct drm_framebuffer *fb = NULL;
2681 struct drm_display_mode *mode = NULL;
2682 struct drm_mode_set set;
2683 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002684 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002685 int i;
2686
Dave Airliefb3b06c2011-02-08 13:55:21 +10002687 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2688 return -EINVAL;
2689
Zhao Junwang01447e92015-07-07 17:08:35 +08002690 /*
2691 * Universal plane src offsets are only 16.16, prevent havoc for
2692 * drivers using universal plane code internally.
2693 */
2694 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002695 return -ERANGE;
2696
Daniel Vetter84849902012-12-02 00:28:11 +01002697 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002698 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2699 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002700 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002701 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002702 goto out;
2703 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002704 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002705
2706 if (crtc_req->mode_valid) {
2707 /* If we have a mode we need a framebuffer. */
2708 /* If we pass -1, set the mode with the currently bound fb */
2709 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002710 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002711 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2712 ret = -EINVAL;
2713 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002714 }
Matt Roperf4510a22014-04-01 15:22:40 -07002715 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002716 /* Make refcounting symmetric with the lookup path. */
2717 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002718 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002719 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2720 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002721 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2722 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002723 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002724 goto out;
2725 }
Dave Airlief453ba02008-11-07 14:05:41 -08002726 }
2727
2728 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002729 if (!mode) {
2730 ret = -ENOMEM;
2731 goto out;
2732 }
2733
Daniel Stone934a8a82015-05-22 13:34:48 +01002734 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002735 if (ret) {
2736 DRM_DEBUG_KMS("Invalid mode\n");
2737 goto out;
2738 }
2739
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002740 /*
2741 * Check whether the primary plane supports the fb pixel format.
2742 * Drivers not implementing the universal planes API use a
2743 * default formats list provided by the DRM core which doesn't
2744 * match real hardware capabilities. Skip the check in that
2745 * case.
2746 */
2747 if (!crtc->primary->format_default) {
2748 ret = drm_plane_check_pixel_format(crtc->primary,
2749 fb->pixel_format);
2750 if (ret) {
2751 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2752 drm_get_format_name(fb->pixel_format));
2753 goto out;
2754 }
2755 }
2756
Damien Lespiauc11e9282013-09-25 16:45:30 +01002757 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2758 mode, fb);
2759 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002760 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002761
Dave Airlief453ba02008-11-07 14:05:41 -08002762 }
2763
2764 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002765 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002766 ret = -EINVAL;
2767 goto out;
2768 }
2769
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002770 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002771 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002772 crtc_req->count_connectors);
2773 ret = -EINVAL;
2774 goto out;
2775 }
2776
2777 if (crtc_req->count_connectors > 0) {
2778 u32 out_id;
2779
2780 /* Avoid unbounded kernel memory allocation */
2781 if (crtc_req->count_connectors > config->num_connector) {
2782 ret = -EINVAL;
2783 goto out;
2784 }
2785
Thierry Reding2f6c5382014-12-10 13:03:36 +01002786 connector_set = kmalloc_array(crtc_req->count_connectors,
2787 sizeof(struct drm_connector *),
2788 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002789 if (!connector_set) {
2790 ret = -ENOMEM;
2791 goto out;
2792 }
2793
2794 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +10002795 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002796 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002797 if (get_user(out_id, &set_connectors_ptr[i])) {
2798 ret = -EFAULT;
2799 goto out;
2800 }
2801
Dave Airlieb164d312016-04-27 11:10:09 +10002802 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002803 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002804 DRM_DEBUG_KMS("Connector id %d unknown\n",
2805 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002806 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002807 goto out;
2808 }
Jerome Glisse94401062010-07-15 15:43:25 -04002809 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2810 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002811 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002812
2813 connector_set[i] = connector;
2814 }
2815 }
2816
2817 set.crtc = crtc;
2818 set.x = crtc_req->x;
2819 set.y = crtc_req->y;
2820 set.mode = mode;
2821 set.connectors = connector_set;
2822 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002823 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002824 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002825
2826out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002827 if (fb)
2828 drm_framebuffer_unreference(fb);
2829
Dave Airlieb164d312016-04-27 11:10:09 +10002830 if (connector_set) {
2831 for (i = 0; i < crtc_req->count_connectors; i++) {
2832 if (connector_set[i])
2833 drm_connector_unreference(connector_set[i]);
2834 }
2835 }
Dave Airlief453ba02008-11-07 14:05:41 -08002836 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002837 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002838 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002839 return ret;
2840}
2841
Matt Roper161d0dc2014-06-10 08:28:10 -07002842/**
2843 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2844 * universal plane handler call
2845 * @crtc: crtc to update cursor for
2846 * @req: data pointer for the ioctl
2847 * @file_priv: drm file for the ioctl call
2848 *
2849 * Legacy cursor ioctl's work directly with driver buffer handles. To
2850 * translate legacy ioctl calls into universal plane handler calls, we need to
2851 * wrap the native buffer handle in a drm_framebuffer.
2852 *
2853 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2854 * buffer with a pitch of 4*width; the universal plane interface should be used
2855 * directly in cases where the hardware can support other buffer settings and
2856 * userspace wants to make use of these capabilities.
2857 *
2858 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002859 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002860 */
2861static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2862 struct drm_mode_cursor2 *req,
2863 struct drm_file *file_priv)
2864{
2865 struct drm_device *dev = crtc->dev;
2866 struct drm_framebuffer *fb = NULL;
2867 struct drm_mode_fb_cmd2 fbreq = {
2868 .width = req->width,
2869 .height = req->height,
2870 .pixel_format = DRM_FORMAT_ARGB8888,
2871 .pitches = { req->width * 4 },
2872 .handles = { req->handle },
2873 };
2874 int32_t crtc_x, crtc_y;
2875 uint32_t crtc_w = 0, crtc_h = 0;
2876 uint32_t src_w = 0, src_h = 0;
2877 int ret = 0;
2878
2879 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002880 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002881
2882 /*
2883 * Obtain fb we'll be using (either new or existing) and take an extra
2884 * reference to it if fb != null. setplane will take care of dropping
2885 * the reference if the plane update fails.
2886 */
2887 if (req->flags & DRM_MODE_CURSOR_BO) {
2888 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002889 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002890 if (IS_ERR(fb)) {
2891 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2892 return PTR_ERR(fb);
2893 }
Gerd Hoffmanndd546592016-05-31 08:54:52 +02002894 fb->hot_x = req->hot_x;
2895 fb->hot_y = req->hot_y;
Matt Roper161d0dc2014-06-10 08:28:10 -07002896 } else {
2897 fb = NULL;
2898 }
2899 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002900 fb = crtc->cursor->fb;
2901 if (fb)
2902 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002903 }
2904
2905 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2906 crtc_x = req->x;
2907 crtc_y = req->y;
2908 } else {
2909 crtc_x = crtc->cursor_x;
2910 crtc_y = crtc->cursor_y;
2911 }
2912
2913 if (fb) {
2914 crtc_w = fb->width;
2915 crtc_h = fb->height;
2916 src_w = fb->width << 16;
2917 src_h = fb->height << 16;
2918 }
2919
2920 /*
2921 * setplane_internal will take care of deref'ing either the old or new
2922 * framebuffer depending on success.
2923 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002924 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002925 crtc_x, crtc_y, crtc_w, crtc_h,
2926 0, 0, src_w, src_h);
2927
2928 /* Update successful; save new cursor position, if necessary */
2929 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2930 crtc->cursor_x = req->x;
2931 crtc->cursor_y = req->y;
2932 }
2933
2934 return ret;
2935}
2936
Dave Airlie4c813d42013-06-20 11:48:52 +10002937static int drm_mode_cursor_common(struct drm_device *dev,
2938 struct drm_mode_cursor2 *req,
2939 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002940{
Dave Airlief453ba02008-11-07 14:05:41 -08002941 struct drm_crtc *crtc;
2942 int ret = 0;
2943
Dave Airliefb3b06c2011-02-08 13:55:21 +10002944 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2945 return -EINVAL;
2946
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002947 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002948 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002949
Rob Clarka2b34e22013-10-05 16:36:52 -04002950 crtc = drm_crtc_find(dev, req->crtc_id);
2951 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002952 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002953 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002954 }
Dave Airlief453ba02008-11-07 14:05:41 -08002955
Matt Roper161d0dc2014-06-10 08:28:10 -07002956 /*
2957 * If this crtc has a universal cursor plane, call that plane's update
2958 * handler rather than using legacy cursor handlers.
2959 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002960 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002961 if (crtc->cursor) {
2962 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2963 goto out;
2964 }
2965
Dave Airlief453ba02008-11-07 14:05:41 -08002966 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002967 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002968 ret = -ENXIO;
2969 goto out;
2970 }
2971 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002972 if (crtc->funcs->cursor_set2)
2973 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2974 req->width, req->height, req->hot_x, req->hot_y);
2975 else
2976 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2977 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002978 }
2979
2980 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2981 if (crtc->funcs->cursor_move) {
2982 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2983 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002984 ret = -EFAULT;
2985 goto out;
2986 }
2987 }
2988out:
Daniel Vetterd059f652014-07-25 18:07:40 +02002989 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01002990
Dave Airlief453ba02008-11-07 14:05:41 -08002991 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002992
2993}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002994
2995
2996/**
2997 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2998 * @dev: drm device for the ioctl
2999 * @data: data pointer for the ioctl
3000 * @file_priv: drm file for the ioctl call
3001 *
3002 * Set the cursor configuration based on user request.
3003 *
3004 * Called by the user via ioctl.
3005 *
3006 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003007 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003008 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003009int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003010 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003011{
3012 struct drm_mode_cursor *req = data;
3013 struct drm_mode_cursor2 new_req;
3014
3015 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3016 new_req.hot_x = new_req.hot_y = 0;
3017
3018 return drm_mode_cursor_common(dev, &new_req, file_priv);
3019}
3020
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003021/**
3022 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3023 * @dev: drm device for the ioctl
3024 * @data: data pointer for the ioctl
3025 * @file_priv: drm file for the ioctl call
3026 *
3027 * Set the cursor configuration based on user request. This implements the 2nd
3028 * version of the cursor ioctl, which allows userspace to additionally specify
3029 * the hotspot of the pointer.
3030 *
3031 * Called by the user via ioctl.
3032 *
3033 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003034 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003035 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003036int drm_mode_cursor2_ioctl(struct drm_device *dev,
3037 void *data, struct drm_file *file_priv)
3038{
3039 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003040
Dave Airlie4c813d42013-06-20 11:48:52 +10003041 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003042}
3043
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003044/**
3045 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3046 * @bpp: bits per pixels
3047 * @depth: bit depth per pixel
3048 *
3049 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3050 * Useful in fbdev emulation code, since that deals in those values.
3051 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003052uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3053{
3054 uint32_t fmt;
3055
3056 switch (bpp) {
3057 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003058 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003059 break;
3060 case 16:
3061 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003062 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003063 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003064 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003065 break;
3066 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003067 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003068 break;
3069 case 32:
3070 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003071 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003072 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003073 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003074 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003075 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003076 break;
3077 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003078 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3079 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003080 break;
3081 }
3082
3083 return fmt;
3084}
3085EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3086
Dave Airlief453ba02008-11-07 14:05:41 -08003087/**
3088 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003089 * @dev: drm device for the ioctl
3090 * @data: data pointer for the ioctl
3091 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003092 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003093 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003094 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003095 *
3096 * Called by the user via ioctl.
3097 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003098 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003099 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003100 */
3101int drm_mode_addfb(struct drm_device *dev,
3102 void *data, struct drm_file *file_priv)
3103{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003104 struct drm_mode_fb_cmd *or = data;
3105 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003106 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003107
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003108 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003109 r.fb_id = or->fb_id;
3110 r.width = or->width;
3111 r.height = or->height;
3112 r.pitches[0] = or->pitch;
3113 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3114 r.handles[0] = or->handle;
3115
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003116 ret = drm_mode_addfb2(dev, &r, file_priv);
3117 if (ret)
3118 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003119
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003120 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003121
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003122 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003123}
3124
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003125static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003126{
3127 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3128
3129 switch (format) {
3130 case DRM_FORMAT_C8:
3131 case DRM_FORMAT_RGB332:
3132 case DRM_FORMAT_BGR233:
3133 case DRM_FORMAT_XRGB4444:
3134 case DRM_FORMAT_XBGR4444:
3135 case DRM_FORMAT_RGBX4444:
3136 case DRM_FORMAT_BGRX4444:
3137 case DRM_FORMAT_ARGB4444:
3138 case DRM_FORMAT_ABGR4444:
3139 case DRM_FORMAT_RGBA4444:
3140 case DRM_FORMAT_BGRA4444:
3141 case DRM_FORMAT_XRGB1555:
3142 case DRM_FORMAT_XBGR1555:
3143 case DRM_FORMAT_RGBX5551:
3144 case DRM_FORMAT_BGRX5551:
3145 case DRM_FORMAT_ARGB1555:
3146 case DRM_FORMAT_ABGR1555:
3147 case DRM_FORMAT_RGBA5551:
3148 case DRM_FORMAT_BGRA5551:
3149 case DRM_FORMAT_RGB565:
3150 case DRM_FORMAT_BGR565:
3151 case DRM_FORMAT_RGB888:
3152 case DRM_FORMAT_BGR888:
3153 case DRM_FORMAT_XRGB8888:
3154 case DRM_FORMAT_XBGR8888:
3155 case DRM_FORMAT_RGBX8888:
3156 case DRM_FORMAT_BGRX8888:
3157 case DRM_FORMAT_ARGB8888:
3158 case DRM_FORMAT_ABGR8888:
3159 case DRM_FORMAT_RGBA8888:
3160 case DRM_FORMAT_BGRA8888:
3161 case DRM_FORMAT_XRGB2101010:
3162 case DRM_FORMAT_XBGR2101010:
3163 case DRM_FORMAT_RGBX1010102:
3164 case DRM_FORMAT_BGRX1010102:
3165 case DRM_FORMAT_ARGB2101010:
3166 case DRM_FORMAT_ABGR2101010:
3167 case DRM_FORMAT_RGBA1010102:
3168 case DRM_FORMAT_BGRA1010102:
3169 case DRM_FORMAT_YUYV:
3170 case DRM_FORMAT_YVYU:
3171 case DRM_FORMAT_UYVY:
3172 case DRM_FORMAT_VYUY:
3173 case DRM_FORMAT_AYUV:
3174 case DRM_FORMAT_NV12:
3175 case DRM_FORMAT_NV21:
3176 case DRM_FORMAT_NV16:
3177 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003178 case DRM_FORMAT_NV24:
3179 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003180 case DRM_FORMAT_YUV410:
3181 case DRM_FORMAT_YVU410:
3182 case DRM_FORMAT_YUV411:
3183 case DRM_FORMAT_YVU411:
3184 case DRM_FORMAT_YUV420:
3185 case DRM_FORMAT_YVU420:
3186 case DRM_FORMAT_YUV422:
3187 case DRM_FORMAT_YVU422:
3188 case DRM_FORMAT_YUV444:
3189 case DRM_FORMAT_YVU444:
3190 return 0;
3191 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003192 DRM_DEBUG_KMS("invalid pixel format %s\n",
3193 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003194 return -EINVAL;
3195 }
3196}
3197
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003198static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003199{
3200 int ret, hsub, vsub, num_planes, i;
3201
3202 ret = format_check(r);
3203 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003204 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3205 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003206 return ret;
3207 }
3208
3209 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3210 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3211 num_planes = drm_format_num_planes(r->pixel_format);
3212
3213 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003214 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003215 return -EINVAL;
3216 }
3217
3218 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003219 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003220 return -EINVAL;
3221 }
3222
3223 for (i = 0; i < num_planes; i++) {
3224 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003225 unsigned int height = r->height / (i != 0 ? vsub : 1);
3226 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003227
3228 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003229 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003230 return -EINVAL;
3231 }
3232
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003233 if ((uint64_t) width * cpp > UINT_MAX)
3234 return -ERANGE;
3235
3236 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3237 return -ERANGE;
3238
3239 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003240 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003241 return -EINVAL;
3242 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003243
3244 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3245 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3246 r->modifier[i], i);
3247 return -EINVAL;
3248 }
Rob Clark570655b2015-01-30 20:18:11 +05303249
3250 /* modifier specific checks: */
3251 switch (r->modifier[i]) {
3252 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3253 /* NOTE: the pitch restriction may be lifted later if it turns
3254 * out that no hw has this restriction:
3255 */
3256 if (r->pixel_format != DRM_FORMAT_NV12 ||
3257 width % 128 || height % 32 ||
3258 r->pitches[i] % 128) {
3259 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3260 return -EINVAL;
3261 }
3262 break;
3263
3264 default:
3265 break;
3266 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003267 }
3268
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003269 for (i = num_planes; i < 4; i++) {
3270 if (r->modifier[i]) {
3271 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3272 return -EINVAL;
3273 }
3274
3275 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3276 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3277 continue;
3278
3279 if (r->handles[i]) {
3280 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3281 return -EINVAL;
3282 }
3283
3284 if (r->pitches[i]) {
3285 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3286 return -EINVAL;
3287 }
3288
3289 if (r->offsets[i]) {
3290 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3291 return -EINVAL;
3292 }
3293 }
3294
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003295 return 0;
3296}
3297
Chris Wilson9a6f5132015-02-25 13:45:26 +00003298static struct drm_framebuffer *
3299internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003300 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003301 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003302{
3303 struct drm_mode_config *config = &dev->mode_config;
3304 struct drm_framebuffer *fb;
3305 int ret;
3306
Rob Clarke3eb3252015-02-05 14:41:52 +00003307 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003308 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3309 return ERR_PTR(-EINVAL);
3310 }
3311
3312 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3313 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3314 r->width, config->min_width, config->max_width);
3315 return ERR_PTR(-EINVAL);
3316 }
3317 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3318 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3319 r->height, config->min_height, config->max_height);
3320 return ERR_PTR(-EINVAL);
3321 }
3322
Rob Clarke3eb3252015-02-05 14:41:52 +00003323 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3324 !dev->mode_config.allow_fb_modifiers) {
3325 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3326 return ERR_PTR(-EINVAL);
3327 }
3328
Matt Roperc394c2b2014-06-10 08:28:08 -07003329 ret = framebuffer_check(r);
3330 if (ret)
3331 return ERR_PTR(ret);
3332
3333 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3334 if (IS_ERR(fb)) {
3335 DRM_DEBUG_KMS("could not create framebuffer\n");
3336 return fb;
3337 }
3338
Matt Roperc394c2b2014-06-10 08:28:08 -07003339 return fb;
3340}
3341
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003342/**
3343 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003344 * @dev: drm device for the ioctl
3345 * @data: data pointer for the ioctl
3346 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003347 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003348 * Add a new FB to the specified CRTC, given a user request with format. This is
3349 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3350 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003351 *
3352 * Called by the user via ioctl.
3353 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003354 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003355 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003356 */
3357int drm_mode_addfb2(struct drm_device *dev,
3358 void *data, struct drm_file *file_priv)
3359{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003360 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003361 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003362
Dave Airliefb3b06c2011-02-08 13:55:21 +10003363 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3364 return -EINVAL;
3365
Chris Wilson9a6f5132015-02-25 13:45:26 +00003366 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003367 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003368 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003369
Chris Wilson9a6f5132015-02-25 13:45:26 +00003370 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003371 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003372
3373 /* Transfer ownership to the filp for reaping on close */
3374 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003375 list_add(&fb->filp_head, &file_priv->fbs);
3376 mutex_unlock(&file_priv->fbs_lock);
3377
Matt Roperc394c2b2014-06-10 08:28:08 -07003378 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003379}
3380
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003381struct drm_mode_rmfb_work {
3382 struct work_struct work;
3383 struct list_head fbs;
3384};
3385
3386static void drm_mode_rmfb_work_fn(struct work_struct *w)
3387{
3388 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3389
3390 while (!list_empty(&arg->fbs)) {
3391 struct drm_framebuffer *fb =
3392 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3393
3394 list_del_init(&fb->filp_head);
3395 drm_framebuffer_remove(fb);
3396 }
3397}
3398
Dave Airlief453ba02008-11-07 14:05:41 -08003399/**
3400 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003401 * @dev: drm device for the ioctl
3402 * @data: data pointer for the ioctl
3403 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003404 *
Dave Airlief453ba02008-11-07 14:05:41 -08003405 * Remove the FB specified by the user.
3406 *
3407 * Called by the user via ioctl.
3408 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003409 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003410 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003411 */
3412int drm_mode_rmfb(struct drm_device *dev,
3413 void *data, struct drm_file *file_priv)
3414{
Dave Airlief453ba02008-11-07 14:05:41 -08003415 struct drm_framebuffer *fb = NULL;
3416 struct drm_framebuffer *fbl = NULL;
3417 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003418 int found = 0;
3419
Dave Airliefb3b06c2011-02-08 13:55:21 +10003420 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3421 return -EINVAL;
3422
Dave Airlie72fe90b2016-04-15 15:10:40 +10003423 fb = drm_framebuffer_lookup(dev, *id);
3424 if (!fb)
3425 return -ENOENT;
3426
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003427 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003428 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3429 if (fb == fbl)
3430 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003431 if (!found) {
3432 mutex_unlock(&file_priv->fbs_lock);
3433 goto fail_unref;
3434 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003435
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003436 list_del_init(&fb->filp_head);
3437 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003438
Dave Airlie72fe90b2016-04-15 15:10:40 +10003439 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003440 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003441
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003442 /*
3443 * we now own the reference that was stored in the fbs list
3444 *
3445 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3446 * so run this in a separate stack as there's no way to correctly
3447 * handle this after the fb is already removed from the lookup table.
3448 */
3449 if (drm_framebuffer_read_refcount(fb) > 1) {
3450 struct drm_mode_rmfb_work arg;
3451
3452 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3453 INIT_LIST_HEAD(&arg.fbs);
3454 list_add_tail(&fb->filp_head, &arg.fbs);
3455
3456 schedule_work(&arg.work);
3457 flush_work(&arg.work);
3458 destroy_work_on_stack(&arg.work);
3459 } else
3460 drm_framebuffer_unreference(fb);
3461
Daniel Vetter2b677e82012-12-10 21:16:05 +01003462 return 0;
3463
Dave Airlie72fe90b2016-04-15 15:10:40 +10003464fail_unref:
3465 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003466 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003467}
3468
3469/**
3470 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003471 * @dev: drm device for the ioctl
3472 * @data: data pointer for the ioctl
3473 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003474 *
Dave Airlief453ba02008-11-07 14:05:41 -08003475 * Lookup the FB given its ID and return info about it.
3476 *
3477 * Called by the user via ioctl.
3478 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003479 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003480 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003481 */
3482int drm_mode_getfb(struct drm_device *dev,
3483 void *data, struct drm_file *file_priv)
3484{
3485 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003486 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003487 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003488
Dave Airliefb3b06c2011-02-08 13:55:21 +10003489 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3490 return -EINVAL;
3491
Daniel Vetter786b99e2012-12-02 21:53:40 +01003492 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003493 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003494 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003495
3496 r->height = fb->height;
3497 r->width = fb->width;
3498 r->depth = fb->depth;
3499 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003500 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003501 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003502 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003503 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003504 ret = fb->funcs->create_handle(fb, file_priv,
3505 &r->handle);
3506 } else {
3507 /* GET_FB() is an unprivileged ioctl so we must not
3508 * return a buffer-handle to non-master processes! For
3509 * backwards-compatibility reasons, we cannot make
3510 * GET_FB() privileged, so just return an invalid handle
3511 * for non-masters. */
3512 r->handle = 0;
3513 ret = 0;
3514 }
3515 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003516 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003517 }
Dave Airlief453ba02008-11-07 14:05:41 -08003518
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003519 drm_framebuffer_unreference(fb);
3520
Dave Airlief453ba02008-11-07 14:05:41 -08003521 return ret;
3522}
3523
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003524/**
3525 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3526 * @dev: drm device for the ioctl
3527 * @data: data pointer for the ioctl
3528 * @file_priv: drm file for the ioctl call
3529 *
3530 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3531 * rectangle list. Generic userspace which does frontbuffer rendering must call
3532 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3533 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3534 *
3535 * Modesetting drivers which always update the frontbuffer do not need to
3536 * implement the corresponding ->dirty framebuffer callback.
3537 *
3538 * Called by the user via ioctl.
3539 *
3540 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003541 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003542 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003543int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3544 void *data, struct drm_file *file_priv)
3545{
3546 struct drm_clip_rect __user *clips_ptr;
3547 struct drm_clip_rect *clips = NULL;
3548 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003549 struct drm_framebuffer *fb;
3550 unsigned flags;
3551 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003552 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003553
Dave Airliefb3b06c2011-02-08 13:55:21 +10003554 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3555 return -EINVAL;
3556
Daniel Vetter786b99e2012-12-02 21:53:40 +01003557 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003558 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003559 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003560
3561 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003562 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003563
3564 if (!num_clips != !clips_ptr) {
3565 ret = -EINVAL;
3566 goto out_err1;
3567 }
3568
3569 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3570
3571 /* If userspace annotates copy, clips must come in pairs */
3572 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3573 ret = -EINVAL;
3574 goto out_err1;
3575 }
3576
3577 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003578 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3579 ret = -EINVAL;
3580 goto out_err1;
3581 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003582 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003583 if (!clips) {
3584 ret = -ENOMEM;
3585 goto out_err1;
3586 }
3587
3588 ret = copy_from_user(clips, clips_ptr,
3589 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003590 if (ret) {
3591 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003592 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003593 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003594 }
3595
3596 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003597 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3598 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003599 } else {
3600 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003601 }
3602
3603out_err2:
3604 kfree(clips);
3605out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003606 drm_framebuffer_unreference(fb);
3607
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003608 return ret;
3609}
3610
Dave Airlief453ba02008-11-07 14:05:41 -08003611/**
3612 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003613 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003614 *
Dave Airlief453ba02008-11-07 14:05:41 -08003615 * Destroy all the FBs associated with @filp.
3616 *
3617 * Called by the user via ioctl.
3618 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003619 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003620 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003621 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003622void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003623{
Dave Airlief453ba02008-11-07 14:05:41 -08003624 struct drm_framebuffer *fb, *tfb;
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003625 struct drm_mode_rmfb_work arg;
3626
3627 INIT_LIST_HEAD(&arg.fbs);
Dave Airlief453ba02008-11-07 14:05:41 -08003628
Daniel Vetter1b116292014-09-24 21:51:06 +02003629 /*
3630 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003631 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003632 * avoid upsetting lockdep since the universal cursor code adds a
3633 * framebuffer while holding mutex locks.
3634 *
3635 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3636 * locks is impossible here since no one else but this function can get
3637 * at it any more.
3638 */
Dave Airlief453ba02008-11-07 14:05:41 -08003639 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003640 if (drm_framebuffer_read_refcount(fb) > 1) {
3641 list_move_tail(&fb->filp_head, &arg.fbs);
3642 } else {
3643 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003644
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003645 /* This drops the fpriv->fbs reference. */
3646 drm_framebuffer_unreference(fb);
3647 }
3648 }
3649
3650 if (!list_empty(&arg.fbs)) {
3651 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3652
3653 schedule_work(&arg.work);
3654 flush_work(&arg.work);
3655 destroy_work_on_stack(&arg.work);
Dave Airlief453ba02008-11-07 14:05:41 -08003656 }
Dave Airlief453ba02008-11-07 14:05:41 -08003657}
3658
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003659/**
3660 * drm_property_create - create a new property type
3661 * @dev: drm device
3662 * @flags: flags specifying the property type
3663 * @name: name of the property
3664 * @num_values: number of pre-defined values
3665 *
3666 * This creates a new generic drm property which can then be attached to a drm
3667 * object with drm_object_attach_property. The returned property object must be
3668 * freed with drm_property_destroy.
3669 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003670 * Note that the DRM core keeps a per-device list of properties and that, if
3671 * drm_mode_config_cleanup() is called, it will destroy all properties created
3672 * by the driver.
3673 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003674 * Returns:
3675 * A pointer to the newly created property on success, NULL on failure.
3676 */
Dave Airlief453ba02008-11-07 14:05:41 -08003677struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3678 const char *name, int num_values)
3679{
3680 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003681 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003682
3683 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3684 if (!property)
3685 return NULL;
3686
Rob Clark98f75de2014-05-30 11:37:03 -04003687 property->dev = dev;
3688
Dave Airlief453ba02008-11-07 14:05:41 -08003689 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003690 property->values = kcalloc(num_values, sizeof(uint64_t),
3691 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003692 if (!property->values)
3693 goto fail;
3694 }
3695
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003696 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3697 if (ret)
3698 goto fail;
3699
Dave Airlief453ba02008-11-07 14:05:41 -08003700 property->flags = flags;
3701 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003702 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003703
Vinson Lee471dd2e2011-11-10 11:55:40 -08003704 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003705 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003706 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3707 }
Dave Airlief453ba02008-11-07 14:05:41 -08003708
3709 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003710
3711 WARN_ON(!drm_property_type_valid(property));
3712
Dave Airlief453ba02008-11-07 14:05:41 -08003713 return property;
3714fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003715 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003716 kfree(property);
3717 return NULL;
3718}
3719EXPORT_SYMBOL(drm_property_create);
3720
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003721/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003722 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003723 * @dev: drm device
3724 * @flags: flags specifying the property type
3725 * @name: name of the property
3726 * @props: enumeration lists with property values
3727 * @num_values: number of pre-defined values
3728 *
3729 * This creates a new generic drm property which can then be attached to a drm
3730 * object with drm_object_attach_property. The returned property object must be
3731 * freed with drm_property_destroy.
3732 *
3733 * Userspace is only allowed to set one of the predefined values for enumeration
3734 * properties.
3735 *
3736 * Returns:
3737 * A pointer to the newly created property on success, NULL on failure.
3738 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003739struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3740 const char *name,
3741 const struct drm_prop_enum_list *props,
3742 int num_values)
3743{
3744 struct drm_property *property;
3745 int i, ret;
3746
3747 flags |= DRM_MODE_PROP_ENUM;
3748
3749 property = drm_property_create(dev, flags, name, num_values);
3750 if (!property)
3751 return NULL;
3752
3753 for (i = 0; i < num_values; i++) {
3754 ret = drm_property_add_enum(property, i,
3755 props[i].type,
3756 props[i].name);
3757 if (ret) {
3758 drm_property_destroy(dev, property);
3759 return NULL;
3760 }
3761 }
3762
3763 return property;
3764}
3765EXPORT_SYMBOL(drm_property_create_enum);
3766
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003767/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003768 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003769 * @dev: drm device
3770 * @flags: flags specifying the property type
3771 * @name: name of the property
3772 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003773 * @num_props: size of the @props array
3774 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003775 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003776 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003777 * object with drm_object_attach_property. The returned property object must be
3778 * freed with drm_property_destroy.
3779 *
3780 * Compared to plain enumeration properties userspace is allowed to set any
3781 * or'ed together combination of the predefined property bitflag values
3782 *
3783 * Returns:
3784 * A pointer to the newly created property on success, NULL on failure.
3785 */
Rob Clark49e27542012-05-17 02:23:26 -06003786struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3787 int flags, const char *name,
3788 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303789 int num_props,
3790 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003791{
3792 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303793 int i, ret, index = 0;
3794 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003795
3796 flags |= DRM_MODE_PROP_BITMASK;
3797
3798 property = drm_property_create(dev, flags, name, num_values);
3799 if (!property)
3800 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303801 for (i = 0; i < num_props; i++) {
3802 if (!(supported_bits & (1ULL << props[i].type)))
3803 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003804
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303805 if (WARN_ON(index >= num_values)) {
3806 drm_property_destroy(dev, property);
3807 return NULL;
3808 }
3809
3810 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003811 props[i].type,
3812 props[i].name);
3813 if (ret) {
3814 drm_property_destroy(dev, property);
3815 return NULL;
3816 }
3817 }
3818
3819 return property;
3820}
3821EXPORT_SYMBOL(drm_property_create_bitmask);
3822
Rob Clarkebc44cf2012-09-12 22:22:31 -05003823static struct drm_property *property_create_range(struct drm_device *dev,
3824 int flags, const char *name,
3825 uint64_t min, uint64_t max)
3826{
3827 struct drm_property *property;
3828
3829 property = drm_property_create(dev, flags, name, 2);
3830 if (!property)
3831 return NULL;
3832
3833 property->values[0] = min;
3834 property->values[1] = max;
3835
3836 return property;
3837}
3838
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003839/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003840 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003841 * @dev: drm device
3842 * @flags: flags specifying the property type
3843 * @name: name of the property
3844 * @min: minimum value of the property
3845 * @max: maximum value of the property
3846 *
3847 * This creates a new generic drm property which can then be attached to a drm
3848 * object with drm_object_attach_property. The returned property object must be
3849 * freed with drm_property_destroy.
3850 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003851 * Userspace is allowed to set any unsigned integer value in the (min, max)
3852 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003853 *
3854 * Returns:
3855 * A pointer to the newly created property on success, NULL on failure.
3856 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003857struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3858 const char *name,
3859 uint64_t min, uint64_t max)
3860{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003861 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3862 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003863}
3864EXPORT_SYMBOL(drm_property_create_range);
3865
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003866/**
3867 * drm_property_create_signed_range - create a new signed ranged property type
3868 * @dev: drm device
3869 * @flags: flags specifying the property type
3870 * @name: name of the property
3871 * @min: minimum value of the property
3872 * @max: maximum value of the property
3873 *
3874 * This creates a new generic drm property which can then be attached to a drm
3875 * object with drm_object_attach_property. The returned property object must be
3876 * freed with drm_property_destroy.
3877 *
3878 * Userspace is allowed to set any signed integer value in the (min, max)
3879 * range inclusive.
3880 *
3881 * Returns:
3882 * A pointer to the newly created property on success, NULL on failure.
3883 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003884struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3885 int flags, const char *name,
3886 int64_t min, int64_t max)
3887{
3888 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3889 name, I642U64(min), I642U64(max));
3890}
3891EXPORT_SYMBOL(drm_property_create_signed_range);
3892
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003893/**
3894 * drm_property_create_object - create a new object property type
3895 * @dev: drm device
3896 * @flags: flags specifying the property type
3897 * @name: name of the property
3898 * @type: object type from DRM_MODE_OBJECT_* defines
3899 *
3900 * This creates a new generic drm property which can then be attached to a drm
3901 * object with drm_object_attach_property. The returned property object must be
3902 * freed with drm_property_destroy.
3903 *
3904 * Userspace is only allowed to set this to any property value of the given
3905 * @type. Only useful for atomic properties, which is enforced.
3906 *
3907 * Returns:
3908 * A pointer to the newly created property on success, NULL on failure.
3909 */
Rob Clark98f75de2014-05-30 11:37:03 -04003910struct drm_property *drm_property_create_object(struct drm_device *dev,
3911 int flags, const char *name, uint32_t type)
3912{
3913 struct drm_property *property;
3914
3915 flags |= DRM_MODE_PROP_OBJECT;
3916
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003917 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3918 return NULL;
3919
Rob Clark98f75de2014-05-30 11:37:03 -04003920 property = drm_property_create(dev, flags, name, 1);
3921 if (!property)
3922 return NULL;
3923
3924 property->values[0] = type;
3925
3926 return property;
3927}
3928EXPORT_SYMBOL(drm_property_create_object);
3929
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003930/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003931 * drm_property_create_bool - create a new boolean property type
3932 * @dev: drm device
3933 * @flags: flags specifying the property type
3934 * @name: name of the property
3935 *
3936 * This creates a new generic drm property which can then be attached to a drm
3937 * object with drm_object_attach_property. The returned property object must be
3938 * freed with drm_property_destroy.
3939 *
3940 * This is implemented as a ranged property with only {0, 1} as valid values.
3941 *
3942 * Returns:
3943 * A pointer to the newly created property on success, NULL on failure.
3944 */
3945struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3946 const char *name)
3947{
3948 return drm_property_create_range(dev, flags, name, 0, 1);
3949}
3950EXPORT_SYMBOL(drm_property_create_bool);
3951
3952/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003953 * drm_property_add_enum - add a possible value to an enumeration property
3954 * @property: enumeration property to change
3955 * @index: index of the new enumeration
3956 * @value: value of the new enumeration
3957 * @name: symbolic name of the new enumeration
3958 *
3959 * This functions adds enumerations to a property.
3960 *
3961 * It's use is deprecated, drivers should use one of the more specific helpers
3962 * to directly create the property with all enumerations already attached.
3963 *
3964 * Returns:
3965 * Zero on success, error code on failure.
3966 */
Dave Airlief453ba02008-11-07 14:05:41 -08003967int drm_property_add_enum(struct drm_property *property, int index,
3968 uint64_t value, const char *name)
3969{
3970 struct drm_property_enum *prop_enum;
3971
Rob Clark5ea22f22014-05-30 11:34:01 -04003972 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3973 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003974 return -EINVAL;
3975
3976 /*
3977 * Bitmask enum properties have the additional constraint of values
3978 * from 0 to 63
3979 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003980 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3981 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003982 return -EINVAL;
3983
Daniel Vetter3758b342014-11-19 18:38:10 +01003984 if (!list_empty(&property->enum_list)) {
3985 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003986 if (prop_enum->value == value) {
3987 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3988 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3989 return 0;
3990 }
3991 }
3992 }
3993
3994 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3995 if (!prop_enum)
3996 return -ENOMEM;
3997
3998 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3999 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4000 prop_enum->value = value;
4001
4002 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004003 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004004 return 0;
4005}
4006EXPORT_SYMBOL(drm_property_add_enum);
4007
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004008/**
4009 * drm_property_destroy - destroy a drm property
4010 * @dev: drm device
4011 * @property: property to destry
4012 *
4013 * This function frees a property including any attached resources like
4014 * enumeration values.
4015 */
Dave Airlief453ba02008-11-07 14:05:41 -08004016void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4017{
4018 struct drm_property_enum *prop_enum, *pt;
4019
Daniel Vetter3758b342014-11-19 18:38:10 +01004020 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004021 list_del(&prop_enum->head);
4022 kfree(prop_enum);
4023 }
4024
4025 if (property->num_values)
4026 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004027 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004028 list_del(&property->head);
4029 kfree(property);
4030}
4031EXPORT_SYMBOL(drm_property_destroy);
4032
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004033/**
4034 * drm_object_attach_property - attach a property to a modeset object
4035 * @obj: drm modeset object
4036 * @property: property to attach
4037 * @init_val: initial value of the property
4038 *
4039 * This attaches the given property to the modeset object with the given initial
4040 * value. Currently this function cannot fail since the properties are stored in
4041 * a statically sized array.
4042 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004043void drm_object_attach_property(struct drm_mode_object *obj,
4044 struct drm_property *property,
4045 uint64_t init_val)
4046{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004047 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004048
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004049 if (count == DRM_OBJECT_MAX_PROPERTY) {
4050 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4051 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4052 "you see this message on the same object type.\n",
4053 obj->type);
4054 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004055 }
4056
Rob Clarkb17cd752014-12-16 18:05:30 -05004057 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004058 obj->properties->values[count] = init_val;
4059 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004060 if (property->flags & DRM_MODE_PROP_ATOMIC)
4061 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004062}
4063EXPORT_SYMBOL(drm_object_attach_property);
4064
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004065/**
4066 * drm_object_property_set_value - set the value of a property
4067 * @obj: drm mode object to set property value for
4068 * @property: property to set
4069 * @val: value the property should be set to
4070 *
4071 * This functions sets a given property on a given object. This function only
4072 * changes the software state of the property, it does not call into the
4073 * driver's ->set_property callback.
4074 *
4075 * Returns:
4076 * Zero on success, error code on failure.
4077 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004078int drm_object_property_set_value(struct drm_mode_object *obj,
4079 struct drm_property *property, uint64_t val)
4080{
4081 int i;
4082
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004083 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004084 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004085 obj->properties->values[i] = val;
4086 return 0;
4087 }
4088 }
4089
4090 return -EINVAL;
4091}
4092EXPORT_SYMBOL(drm_object_property_set_value);
4093
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004094/**
4095 * drm_object_property_get_value - retrieve the value of a property
4096 * @obj: drm mode object to get property value from
4097 * @property: property to retrieve
4098 * @val: storage for the property value
4099 *
4100 * This function retrieves the softare state of the given property for the given
4101 * property. Since there is no driver callback to retrieve the current property
4102 * value this might be out of sync with the hardware, depending upon the driver
4103 * and property.
4104 *
4105 * Returns:
4106 * Zero on success, error code on failure.
4107 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004108int drm_object_property_get_value(struct drm_mode_object *obj,
4109 struct drm_property *property, uint64_t *val)
4110{
4111 int i;
4112
Rob Clark88a48e22014-12-18 16:01:50 -05004113 /* read-only properties bypass atomic mechanism and still store
4114 * their value in obj->properties->values[].. mostly to avoid
4115 * having to deal w/ EDID and similar props in atomic paths:
4116 */
4117 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4118 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4119 return drm_atomic_get_property(obj, property, val);
4120
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004121 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004122 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004123 *val = obj->properties->values[i];
4124 return 0;
4125 }
4126 }
4127
4128 return -EINVAL;
4129}
4130EXPORT_SYMBOL(drm_object_property_get_value);
4131
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004132/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004133 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004134 * @dev: DRM device
4135 * @data: ioctl data
4136 * @file_priv: DRM file info
4137 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004138 * This function retrieves the metadata for a given property, like the different
4139 * possible values for an enum property or the limits for a range property.
4140 *
4141 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004142 *
4143 * Called by the user via ioctl.
4144 *
4145 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004146 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004147 */
Dave Airlief453ba02008-11-07 14:05:41 -08004148int drm_mode_getproperty_ioctl(struct drm_device *dev,
4149 void *data, struct drm_file *file_priv)
4150{
Dave Airlief453ba02008-11-07 14:05:41 -08004151 struct drm_mode_get_property *out_resp = data;
4152 struct drm_property *property;
4153 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004154 int value_count = 0;
4155 int ret = 0, i;
4156 int copied;
4157 struct drm_property_enum *prop_enum;
4158 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004159 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004160
Dave Airliefb3b06c2011-02-08 13:55:21 +10004161 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4162 return -EINVAL;
4163
Daniel Vetter84849902012-12-02 00:28:11 +01004164 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004165 property = drm_property_find(dev, out_resp->prop_id);
4166 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004167 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004168 goto done;
4169 }
Dave Airlief453ba02008-11-07 14:05:41 -08004170
Rob Clark5ea22f22014-05-30 11:34:01 -04004171 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4172 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004173 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004174 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004175 }
4176
4177 value_count = property->num_values;
4178
4179 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4180 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4181 out_resp->flags = property->flags;
4182
4183 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004184 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004185 for (i = 0; i < value_count; i++) {
4186 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4187 ret = -EFAULT;
4188 goto done;
4189 }
4190 }
4191 }
4192 out_resp->count_values = value_count;
4193
Rob Clark5ea22f22014-05-30 11:34:01 -04004194 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4195 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004196 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4197 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004198 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004199 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004200
4201 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4202 ret = -EFAULT;
4203 goto done;
4204 }
4205
4206 if (copy_to_user(&enum_ptr[copied].name,
4207 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4208 ret = -EFAULT;
4209 goto done;
4210 }
4211 copied++;
4212 }
4213 }
4214 out_resp->count_enum_blobs = enum_count;
4215 }
4216
Daniel Vetter3758b342014-11-19 18:38:10 +01004217 /*
4218 * NOTE: The idea seems to have been to use this to read all the blob
4219 * property values. But nothing ever added them to the corresponding
4220 * list, userspace always used the special-purpose get_blob ioctl to
4221 * read the value for a blob property. It also doesn't make a lot of
4222 * sense to return values here when everything else is just metadata for
4223 * the property itself.
4224 */
4225 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4226 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004227done:
Daniel Vetter84849902012-12-02 00:28:11 +01004228 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004229 return ret;
4230}
4231
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004232static void drm_property_free_blob(struct kref *kref)
4233{
4234 struct drm_property_blob *blob =
4235 container_of(kref, struct drm_property_blob, base.refcount);
4236
4237 mutex_lock(&blob->dev->mode_config.blob_lock);
4238 list_del(&blob->head_global);
4239 mutex_unlock(&blob->dev->mode_config.blob_lock);
4240
4241 drm_mode_object_unregister(blob->dev, &blob->base);
4242
4243 kfree(blob);
4244}
4245
Daniel Stone99531d92015-05-22 13:34:49 +01004246/**
4247 * drm_property_create_blob - Create new blob property
4248 *
4249 * Creates a new blob property for a specified DRM device, optionally
4250 * copying data.
4251 *
4252 * @dev: DRM device to create property for
4253 * @length: Length to allocate for blob data
4254 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004255 *
4256 * Returns:
4257 * New blob property with a single reference on success, or an ERR_PTR
4258 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004259 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004260struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004261drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004262 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004263{
4264 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004265 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004266
Dan Carpenter9ac09342015-10-29 16:37:54 +03004267 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004268 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004269
4270 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4271 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004272 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004273
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004274 /* This must be explicitly initialised, so we can safely call list_del
4275 * on it in the removal handler, even if it isn't in a file list. */
4276 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004277 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004278 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004279
Daniel Stone99531d92015-05-22 13:34:49 +01004280 if (data)
4281 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004282
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004283 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4284 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004285 if (ret) {
4286 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004287 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004288 }
4289
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004290 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004291 list_add_tail(&blob->head_global,
4292 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004293 mutex_unlock(&dev->mode_config.blob_lock);
4294
Dave Airlief453ba02008-11-07 14:05:41 -08004295 return blob;
4296}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004297EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004298
Daniel Stone6bcacf52015-04-20 19:22:55 +01004299/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004300 * drm_property_unreference_blob - Unreference a blob property
4301 *
4302 * Drop a reference on a blob property. May free the object.
4303 *
Daniel Stonef102c162015-05-22 13:34:44 +01004304 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004305 */
4306void drm_property_unreference_blob(struct drm_property_blob *blob)
4307{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004308 if (!blob)
4309 return;
4310
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004311 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004312}
4313EXPORT_SYMBOL(drm_property_unreference_blob);
4314
4315/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004316 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4317 * @dev: DRM device
4318 * @file_priv: destroy all blobs owned by this file handle
4319 */
4320void drm_property_destroy_user_blobs(struct drm_device *dev,
4321 struct drm_file *file_priv)
4322{
4323 struct drm_property_blob *blob, *bt;
4324
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004325 /*
4326 * When the file gets released that means no one else can access the
4327 * blob list any more, so no need to grab dev->blob_lock.
4328 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004329 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4330 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004331 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004332 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004333}
4334
4335/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004336 * drm_property_reference_blob - Take a reference on an existing property
4337 *
4338 * Take a new reference on an existing blob property.
4339 *
Daniel Stonef102c162015-05-22 13:34:44 +01004340 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004341 */
4342struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4343{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004344 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004345 return blob;
4346}
4347EXPORT_SYMBOL(drm_property_reference_blob);
4348
Daniel Stone6bcacf52015-04-20 19:22:55 +01004349/**
4350 * drm_property_lookup_blob - look up a blob property and take a reference
4351 * @dev: drm device
4352 * @id: id of the blob property
4353 *
4354 * If successful, this takes an additional reference to the blob property.
4355 * callers need to make sure to eventually unreference the returned property
4356 * again, using @drm_property_unreference_blob.
4357 */
4358struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4359 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004360{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004361 struct drm_mode_object *obj;
4362 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004363
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004364 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4365 if (obj)
4366 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004367 return blob;
4368}
4369EXPORT_SYMBOL(drm_property_lookup_blob);
4370
4371/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004372 * drm_property_replace_global_blob - atomically replace existing blob property
4373 * @dev: drm device
4374 * @replace: location of blob property pointer to be replaced
4375 * @length: length of data for new blob, or 0 for no data
4376 * @data: content for new blob, or NULL for no data
4377 * @obj_holds_id: optional object for property holding blob ID
4378 * @prop_holds_id: optional property holding blob ID
4379 * @return 0 on success or error on failure
4380 *
4381 * This function will atomically replace a global property in the blob list,
4382 * optionally updating a property which holds the ID of that property. It is
4383 * guaranteed to be atomic: no caller will be allowed to see intermediate
4384 * results, and either the entire operation will succeed and clean up the
4385 * previous property, or it will fail and the state will be unchanged.
4386 *
4387 * If length is 0 or data is NULL, no new blob will be created, and the holding
4388 * property, if specified, will be set to 0.
4389 *
4390 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4391 * by holding the relevant modesetting object lock for its parent.
4392 *
4393 * For example, a drm_connector has a 'PATH' property, which contains the ID
4394 * of a blob property with the value of the MST path information. Calling this
4395 * function with replace pointing to the connector's path_blob_ptr, length and
4396 * data set for the new path information, obj_holds_id set to the connector's
4397 * base object, and prop_holds_id set to the path property name, will perform
4398 * a completely atomic update. The access to path_blob_ptr is protected by the
4399 * caller holding a lock on the connector.
4400 */
4401static int drm_property_replace_global_blob(struct drm_device *dev,
4402 struct drm_property_blob **replace,
4403 size_t length,
4404 const void *data,
4405 struct drm_mode_object *obj_holds_id,
4406 struct drm_property *prop_holds_id)
4407{
4408 struct drm_property_blob *new_blob = NULL;
4409 struct drm_property_blob *old_blob = NULL;
4410 int ret;
4411
4412 WARN_ON(replace == NULL);
4413
4414 old_blob = *replace;
4415
4416 if (length && data) {
4417 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004418 if (IS_ERR(new_blob))
4419 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004420 }
4421
4422 /* This does not need to be synchronised with blob_lock, as the
4423 * get_properties ioctl locks all modesetting objects, and
4424 * obj_holds_id must be locked before calling here, so we cannot
4425 * have its value out of sync with the list membership modified
4426 * below under blob_lock. */
4427 if (obj_holds_id) {
4428 ret = drm_object_property_set_value(obj_holds_id,
4429 prop_holds_id,
4430 new_blob ?
4431 new_blob->base.id : 0);
4432 if (ret != 0)
4433 goto err_created;
4434 }
4435
Markus Elfringec530822015-07-05 21:55:10 +02004436 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004437 *replace = new_blob;
4438
4439 return 0;
4440
4441err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004442 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004443 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004444}
4445
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004446/**
4447 * drm_mode_getblob_ioctl - get the contents of a blob property value
4448 * @dev: DRM device
4449 * @data: ioctl data
4450 * @file_priv: DRM file info
4451 *
4452 * This function retrieves the contents of a blob property. The value stored in
4453 * an object's blob property is just a normal modeset object id.
4454 *
4455 * Called by the user via ioctl.
4456 *
4457 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004458 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004459 */
Dave Airlief453ba02008-11-07 14:05:41 -08004460int drm_mode_getblob_ioctl(struct drm_device *dev,
4461 void *data, struct drm_file *file_priv)
4462{
Dave Airlief453ba02008-11-07 14:05:41 -08004463 struct drm_mode_get_blob *out_resp = data;
4464 struct drm_property_blob *blob;
4465 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004466 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004467
Dave Airliefb3b06c2011-02-08 13:55:21 +10004468 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4469 return -EINVAL;
4470
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004471 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4472 if (!blob)
4473 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004474
4475 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004476 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004477 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004478 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004479 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08004480 }
4481 }
4482 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004483unref:
4484 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004485
Dave Airlief453ba02008-11-07 14:05:41 -08004486 return ret;
4487}
4488
Dave Airliecc7096f2014-10-22 12:03:04 +10004489/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004490 * drm_mode_createblob_ioctl - create a new blob property
4491 * @dev: DRM device
4492 * @data: ioctl data
4493 * @file_priv: DRM file info
4494 *
4495 * This function creates a new blob property with user-defined values. In order
4496 * to give us sensible validation and checking when creating, rather than at
4497 * every potential use, we also require a type to be provided upfront.
4498 *
4499 * Called by the user via ioctl.
4500 *
4501 * Returns:
4502 * Zero on success, negative errno on failure.
4503 */
4504int drm_mode_createblob_ioctl(struct drm_device *dev,
4505 void *data, struct drm_file *file_priv)
4506{
4507 struct drm_mode_create_blob *out_resp = data;
4508 struct drm_property_blob *blob;
4509 void __user *blob_ptr;
4510 int ret = 0;
4511
4512 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4513 return -EINVAL;
4514
4515 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4516 if (IS_ERR(blob))
4517 return PTR_ERR(blob);
4518
4519 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4520 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4521 ret = -EFAULT;
4522 goto out_blob;
4523 }
4524
4525 /* Dropping the lock between create_blob and our access here is safe
4526 * as only the same file_priv can remove the blob; at this point, it is
4527 * not associated with any file_priv. */
4528 mutex_lock(&dev->mode_config.blob_lock);
4529 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004530 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004531 mutex_unlock(&dev->mode_config.blob_lock);
4532
4533 return 0;
4534
4535out_blob:
4536 drm_property_unreference_blob(blob);
4537 return ret;
4538}
4539
4540/**
4541 * drm_mode_destroyblob_ioctl - destroy a user blob property
4542 * @dev: DRM device
4543 * @data: ioctl data
4544 * @file_priv: DRM file info
4545 *
4546 * Destroy an existing user-defined blob property.
4547 *
4548 * Called by the user via ioctl.
4549 *
4550 * Returns:
4551 * Zero on success, negative errno on failure.
4552 */
4553int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4554 void *data, struct drm_file *file_priv)
4555{
4556 struct drm_mode_destroy_blob *out_resp = data;
4557 struct drm_property_blob *blob = NULL, *bt;
4558 bool found = false;
4559 int ret = 0;
4560
4561 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4562 return -EINVAL;
4563
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004564 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4565 if (!blob)
4566 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004567
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004568 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004569 /* Ensure the property was actually created by this user. */
4570 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4571 if (bt == blob) {
4572 found = true;
4573 break;
4574 }
4575 }
4576
4577 if (!found) {
4578 ret = -EPERM;
4579 goto err;
4580 }
4581
4582 /* We must drop head_file here, because we may not be the last
4583 * reference on the blob. */
4584 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004585 mutex_unlock(&dev->mode_config.blob_lock);
4586
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004587 /* One reference from lookup, and one from the filp. */
4588 drm_property_unreference_blob(blob);
4589 drm_property_unreference_blob(blob);
4590
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004591 return 0;
4592
4593err:
4594 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004595 drm_property_unreference_blob(blob);
4596
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004597 return ret;
4598}
4599
4600/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004601 * drm_mode_connector_set_path_property - set tile property on connector
4602 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004603 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004604 *
4605 * This creates a property to expose to userspace to specify a
4606 * connector path. This is mainly used for DisplayPort MST where
4607 * connectors have a topology and we want to allow userspace to give
4608 * them more meaningful names.
4609 *
4610 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004611 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004612 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004613int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004614 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004615{
4616 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004617 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004618
Daniel Stoned2ed3432015-04-20 19:22:53 +01004619 ret = drm_property_replace_global_blob(dev,
4620 &connector->path_blob_ptr,
4621 strlen(path) + 1,
4622 path,
4623 &connector->base,
4624 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004625 return ret;
4626}
4627EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4628
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004629/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004630 * drm_mode_connector_set_tile_property - set tile property on connector
4631 * @connector: connector to set property on.
4632 *
4633 * This looks up the tile information for a connector, and creates a
4634 * property for userspace to parse if it exists. The property is of
4635 * the form of 8 integers using ':' as a separator.
4636 *
4637 * Returns:
4638 * Zero on success, errno on failure.
4639 */
4640int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4641{
4642 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004643 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004644 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004645
4646 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004647 ret = drm_property_replace_global_blob(dev,
4648 &connector->tile_blob_ptr,
4649 0,
4650 NULL,
4651 &connector->base,
4652 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004653 return ret;
4654 }
4655
4656 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4657 connector->tile_group->id, connector->tile_is_single_monitor,
4658 connector->num_h_tile, connector->num_v_tile,
4659 connector->tile_h_loc, connector->tile_v_loc,
4660 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004661
Daniel Stoned2ed3432015-04-20 19:22:53 +01004662 ret = drm_property_replace_global_blob(dev,
4663 &connector->tile_blob_ptr,
4664 strlen(tile) + 1,
4665 tile,
4666 &connector->base,
4667 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004668 return ret;
4669}
4670EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4671
4672/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004673 * drm_mode_connector_update_edid_property - update the edid property of a connector
4674 * @connector: drm connector
4675 * @edid: new value of the edid property
4676 *
4677 * This function creates a new blob modeset object and assigns its id to the
4678 * connector's edid property.
4679 *
4680 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004681 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004682 */
Dave Airlief453ba02008-11-07 14:05:41 -08004683int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004684 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004685{
4686 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004687 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004688 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004689
Thomas Wood4cf2b282014-06-18 17:52:33 +01004690 /* ignore requests to set edid when overridden */
4691 if (connector->override_edid)
4692 return 0;
4693
Daniel Stoned2ed3432015-04-20 19:22:53 +01004694 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004695 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004696
Daniel Stoned2ed3432015-04-20 19:22:53 +01004697 ret = drm_property_replace_global_blob(dev,
4698 &connector->edid_blob_ptr,
4699 size,
4700 edid,
4701 &connector->base,
4702 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004703 return ret;
4704}
4705EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4706
Rob Clark3843e712014-12-16 18:05:29 -05004707/* Some properties could refer to dynamic refcnt'd objects, or things that
4708 * need special locking to handle lifetime issues (ie. to ensure the prop
4709 * value doesn't become invalid part way through the property update due to
4710 * race). The value returned by reference via 'obj' should be passed back
4711 * to drm_property_change_valid_put() after the property is set (and the
4712 * object to which the property is attached has a chance to take it's own
4713 * reference).
4714 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004715bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004716 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004717{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004718 int i;
4719
Paulo Zanoni26a34812012-05-15 18:08:59 -03004720 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4721 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004722
Rob Clark3843e712014-12-16 18:05:29 -05004723 *ref = NULL;
4724
Rob Clark5ea22f22014-05-30 11:34:01 -04004725 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004726 if (value < property->values[0] || value > property->values[1])
4727 return false;
4728 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004729 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4730 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004731
Rob Clarkebc44cf2012-09-12 22:22:31 -05004732 if (svalue < U642I64(property->values[0]) ||
4733 svalue > U642I64(property->values[1]))
4734 return false;
4735 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004736 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004737 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004738
Rob Clark49e27542012-05-17 02:23:26 -06004739 for (i = 0; i < property->num_values; i++)
4740 valid_mask |= (1ULL << property->values[i]);
4741 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004742 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004743 struct drm_property_blob *blob;
4744
4745 if (value == 0)
4746 return true;
4747
4748 blob = drm_property_lookup_blob(property->dev, value);
4749 if (blob) {
4750 *ref = &blob->base;
4751 return true;
4752 } else {
4753 return false;
4754 }
Rob Clark98f75de2014-05-30 11:37:03 -04004755 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004756 /* a zero value for an object property translates to null: */
4757 if (value == 0)
4758 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004759
Tomi Valkeinen1e8985a2016-05-31 15:03:18 +03004760 *ref = _object_find(property->dev, value, property->values[0]);
4761 return *ref != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004762 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004763
4764 for (i = 0; i < property->num_values; i++)
4765 if (property->values[i] == value)
4766 return true;
4767 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004768}
4769
Rob Clarkd34f20d2014-12-18 16:01:56 -05004770void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004771 struct drm_mode_object *ref)
4772{
4773 if (!ref)
4774 return;
4775
4776 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004777 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004778 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4779 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004780}
4781
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004782/**
4783 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4784 * @dev: DRM device
4785 * @data: ioctl data
4786 * @file_priv: DRM file info
4787 *
4788 * This function sets the current value for a connectors's property. It also
4789 * calls into a driver's ->set_property callback to update the hardware state
4790 *
4791 * Called by the user via ioctl.
4792 *
4793 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004794 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004795 */
Dave Airlief453ba02008-11-07 14:05:41 -08004796int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4797 void *data, struct drm_file *file_priv)
4798{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004799 struct drm_mode_connector_set_property *conn_set_prop = data;
4800 struct drm_mode_obj_set_property obj_set_prop = {
4801 .value = conn_set_prop->value,
4802 .prop_id = conn_set_prop->prop_id,
4803 .obj_id = conn_set_prop->connector_id,
4804 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4805 };
Dave Airlief453ba02008-11-07 14:05:41 -08004806
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004807 /* It does all the locking and checking we need */
4808 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004809}
4810
Paulo Zanonic5431882012-05-15 18:09:02 -03004811static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4812 struct drm_property *property,
4813 uint64_t value)
4814{
4815 int ret = -EINVAL;
4816 struct drm_connector *connector = obj_to_connector(obj);
4817
4818 /* Do DPMS ourselves */
4819 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004820 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004821 } else if (connector->funcs->set_property)
4822 ret = connector->funcs->set_property(connector, property, value);
4823
4824 /* store the property value if successful */
4825 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004826 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004827 return ret;
4828}
4829
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004830static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4831 struct drm_property *property,
4832 uint64_t value)
4833{
4834 int ret = -EINVAL;
4835 struct drm_crtc *crtc = obj_to_crtc(obj);
4836
4837 if (crtc->funcs->set_property)
4838 ret = crtc->funcs->set_property(crtc, property, value);
4839 if (!ret)
4840 drm_object_property_set_value(obj, property, value);
4841
4842 return ret;
4843}
4844
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004845/**
4846 * drm_mode_plane_set_obj_prop - set the value of a property
4847 * @plane: drm plane object to set property value for
4848 * @property: property to set
4849 * @value: value the property should be set to
4850 *
4851 * This functions sets a given property on a given plane object. This function
4852 * calls the driver's ->set_property callback and changes the software state of
4853 * the property if the callback succeeds.
4854 *
4855 * Returns:
4856 * Zero on success, error code on failure.
4857 */
4858int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4859 struct drm_property *property,
4860 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004861{
4862 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004863 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004864
4865 if (plane->funcs->set_property)
4866 ret = plane->funcs->set_property(plane, property, value);
4867 if (!ret)
4868 drm_object_property_set_value(obj, property, value);
4869
4870 return ret;
4871}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004872EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004873
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004874/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004875 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004876 * @dev: DRM device
4877 * @data: ioctl data
4878 * @file_priv: DRM file info
4879 *
4880 * This function retrieves the current value for an object's property. Compared
4881 * to the connector specific ioctl this one is extended to also work on crtc and
4882 * plane objects.
4883 *
4884 * Called by the user via ioctl.
4885 *
4886 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004887 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004888 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004889int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4890 struct drm_file *file_priv)
4891{
4892 struct drm_mode_obj_get_properties *arg = data;
4893 struct drm_mode_object *obj;
4894 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004895
4896 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4897 return -EINVAL;
4898
Daniel Vetter84849902012-12-02 00:28:11 +01004899 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004900
4901 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4902 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004903 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004904 goto out;
4905 }
4906 if (!obj->properties) {
4907 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02004908 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004909 }
4910
Rob Clark88a48e22014-12-18 16:01:50 -05004911 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05004912 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4913 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4914 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03004915
Daniel Vetter1649c332016-04-22 22:10:28 +02004916out_unref:
4917 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03004918out:
Daniel Vetter84849902012-12-02 00:28:11 +01004919 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004920 return ret;
4921}
4922
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004923/**
4924 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4925 * @dev: DRM device
4926 * @data: ioctl data
4927 * @file_priv: DRM file info
4928 *
4929 * This function sets the current value for an object's property. It also calls
4930 * into a driver's ->set_property callback to update the hardware state.
4931 * Compared to the connector specific ioctl this one is extended to also work on
4932 * crtc and plane objects.
4933 *
4934 * Called by the user via ioctl.
4935 *
4936 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004937 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004938 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004939int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4940 struct drm_file *file_priv)
4941{
4942 struct drm_mode_obj_set_property *arg = data;
4943 struct drm_mode_object *arg_obj;
4944 struct drm_mode_object *prop_obj;
4945 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05004946 int i, ret = -EINVAL;
4947 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004948
4949 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4950 return -EINVAL;
4951
Daniel Vetter84849902012-12-02 00:28:11 +01004952 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004953
4954 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004955 if (!arg_obj) {
4956 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004957 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004958 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004959 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02004960 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004961
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004962 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05004963 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03004964 break;
4965
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004966 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02004967 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004968
4969 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4970 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004971 if (!prop_obj) {
4972 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02004973 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004974 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004975 property = obj_to_property(prop_obj);
4976
Rob Clark3843e712014-12-16 18:05:29 -05004977 if (!drm_property_change_valid_get(property, arg->value, &ref))
Dave Airlieb164d312016-04-27 11:10:09 +10004978 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004979
4980 switch (arg_obj->type) {
4981 case DRM_MODE_OBJECT_CONNECTOR:
4982 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4983 arg->value);
4984 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004985 case DRM_MODE_OBJECT_CRTC:
4986 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4987 break;
Rob Clark4d939142012-05-17 02:23:27 -06004988 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004989 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4990 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06004991 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004992 }
4993
Rob Clark3843e712014-12-16 18:05:29 -05004994 drm_property_change_valid_put(property, ref);
4995
Daniel Vetter1649c332016-04-22 22:10:28 +02004996out_unref:
4997 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03004998out:
Daniel Vetter84849902012-12-02 00:28:11 +01004999 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005000 return ret;
5001}
5002
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005003/**
5004 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5005 * @connector: connector to attach
5006 * @encoder: encoder to attach @connector to
5007 *
5008 * This function links up a connector to an encoder. Note that the routing
5009 * restrictions between encoders and crtcs are exposed to userspace through the
5010 * possible_clones and possible_crtcs bitmasks.
5011 *
5012 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005013 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005014 */
Dave Airlief453ba02008-11-07 14:05:41 -08005015int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5016 struct drm_encoder *encoder)
5017{
5018 int i;
5019
Thierry Redingeb47fe82015-11-16 18:19:53 +01005020 /*
5021 * In the past, drivers have attempted to model the static association
5022 * of connector to encoder in simple connector/encoder devices using a
5023 * direct assignment of connector->encoder = encoder. This connection
5024 * is a logical one and the responsibility of the core, so drivers are
5025 * expected not to mess with this.
5026 *
5027 * Note that the error return should've been enough here, but a large
5028 * majority of drivers ignores the return value, so add in a big WARN
5029 * to get people's attention.
5030 */
5031 if (WARN_ON(connector->encoder))
5032 return -EINVAL;
5033
Dave Airlief453ba02008-11-07 14:05:41 -08005034 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5035 if (connector->encoder_ids[i] == 0) {
5036 connector->encoder_ids[i] = encoder->base.id;
5037 return 0;
5038 }
5039 }
5040 return -ENOMEM;
5041}
5042EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5043
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005044/**
5045 * drm_mode_crtc_set_gamma_size - set the gamma table size
5046 * @crtc: CRTC to set the gamma table size for
5047 * @gamma_size: size of the gamma table
5048 *
5049 * Drivers which support gamma tables should set this to the supported gamma
5050 * table size when initializing the CRTC. Currently the drm core only supports a
5051 * fixed gamma table size.
5052 *
5053 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005054 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005055 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005056int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005057 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005058{
Daniel Vettercf48e292016-03-30 11:51:16 +02005059 uint16_t *r_base, *g_base, *b_base;
5060 int i;
5061
Dave Airlief453ba02008-11-07 14:05:41 -08005062 crtc->gamma_size = gamma_size;
5063
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005064 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5065 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005066 if (!crtc->gamma_store) {
5067 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005068 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005069 }
5070
Daniel Vettercf48e292016-03-30 11:51:16 +02005071 r_base = crtc->gamma_store;
5072 g_base = r_base + gamma_size;
5073 b_base = g_base + gamma_size;
5074 for (i = 0; i < gamma_size; i++) {
5075 r_base[i] = i << 8;
5076 g_base[i] = i << 8;
5077 b_base[i] = i << 8;
5078 }
5079
5080
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005081 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005082}
5083EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5084
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005085/**
5086 * drm_mode_gamma_set_ioctl - set the gamma table
5087 * @dev: DRM device
5088 * @data: ioctl data
5089 * @file_priv: DRM file info
5090 *
5091 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5092 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5093 *
5094 * Called by the user via ioctl.
5095 *
5096 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005097 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005098 */
Dave Airlief453ba02008-11-07 14:05:41 -08005099int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5100 void *data, struct drm_file *file_priv)
5101{
5102 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005103 struct drm_crtc *crtc;
5104 void *r_base, *g_base, *b_base;
5105 int size;
5106 int ret = 0;
5107
Dave Airliefb3b06c2011-02-08 13:55:21 +10005108 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5109 return -EINVAL;
5110
Daniel Vetter84849902012-12-02 00:28:11 +01005111 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005112 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5113 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005114 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005115 goto out;
5116 }
Dave Airlief453ba02008-11-07 14:05:41 -08005117
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005118 if (crtc->funcs->gamma_set == NULL) {
5119 ret = -ENOSYS;
5120 goto out;
5121 }
5122
Dave Airlief453ba02008-11-07 14:05:41 -08005123 /* memcpy into gamma store */
5124 if (crtc_lut->gamma_size != crtc->gamma_size) {
5125 ret = -EINVAL;
5126 goto out;
5127 }
5128
5129 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5130 r_base = crtc->gamma_store;
5131 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5132 ret = -EFAULT;
5133 goto out;
5134 }
5135
5136 g_base = r_base + size;
5137 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5138 ret = -EFAULT;
5139 goto out;
5140 }
5141
5142 b_base = g_base + size;
5143 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5144 ret = -EFAULT;
5145 goto out;
5146 }
5147
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02005148 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005149
5150out:
Daniel Vetter84849902012-12-02 00:28:11 +01005151 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005152 return ret;
5153
5154}
5155
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005156/**
5157 * drm_mode_gamma_get_ioctl - get the gamma table
5158 * @dev: DRM device
5159 * @data: ioctl data
5160 * @file_priv: DRM file info
5161 *
5162 * Copy the current gamma table into the storage provided. This also provides
5163 * the gamma table size the driver expects, which can be used to size the
5164 * allocated storage.
5165 *
5166 * Called by the user via ioctl.
5167 *
5168 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005169 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005170 */
Dave Airlief453ba02008-11-07 14:05:41 -08005171int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5172 void *data, struct drm_file *file_priv)
5173{
5174 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005175 struct drm_crtc *crtc;
5176 void *r_base, *g_base, *b_base;
5177 int size;
5178 int ret = 0;
5179
Dave Airliefb3b06c2011-02-08 13:55:21 +10005180 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5181 return -EINVAL;
5182
Daniel Vetter84849902012-12-02 00:28:11 +01005183 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005184 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5185 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005186 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005187 goto out;
5188 }
Dave Airlief453ba02008-11-07 14:05:41 -08005189
5190 /* memcpy into gamma store */
5191 if (crtc_lut->gamma_size != crtc->gamma_size) {
5192 ret = -EINVAL;
5193 goto out;
5194 }
5195
5196 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5197 r_base = crtc->gamma_store;
5198 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5199 ret = -EFAULT;
5200 goto out;
5201 }
5202
5203 g_base = r_base + size;
5204 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5205 ret = -EFAULT;
5206 goto out;
5207 }
5208
5209 b_base = g_base + size;
5210 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5211 ret = -EFAULT;
5212 goto out;
5213 }
5214out:
Daniel Vetter84849902012-12-02 00:28:11 +01005215 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005216 return ret;
5217}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005218
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005219/**
5220 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5221 * @dev: DRM device
5222 * @data: ioctl data
5223 * @file_priv: DRM file info
5224 *
5225 * This schedules an asynchronous update on a given CRTC, called page flip.
5226 * Optionally a drm event is generated to signal the completion of the event.
5227 * Generic drivers cannot assume that a pageflip with changed framebuffer
5228 * properties (including driver specific metadata like tiling layout) will work,
5229 * but some drivers support e.g. pixel format changes through the pageflip
5230 * ioctl.
5231 *
5232 * Called by the user via ioctl.
5233 *
5234 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005235 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005236 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005237int drm_mode_page_flip_ioctl(struct drm_device *dev,
5238 void *data, struct drm_file *file_priv)
5239{
5240 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005241 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005242 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005243 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005244 int ret = -EINVAL;
5245
5246 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5247 page_flip->reserved != 0)
5248 return -EINVAL;
5249
Keith Packard62f21042013-07-22 18:50:00 -07005250 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5251 return -EINVAL;
5252
Rob Clarka2b34e22013-10-05 16:36:52 -04005253 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5254 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005255 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005256
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005257 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005258 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005259 /* The framebuffer is currently unbound, presumably
5260 * due to a hotplug event, that userspace has not
5261 * yet discovered.
5262 */
5263 ret = -EBUSY;
5264 goto out;
5265 }
5266
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005267 if (crtc->funcs->page_flip == NULL)
5268 goto out;
5269
Daniel Vetter786b99e2012-12-02 21:53:40 +01005270 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005271 if (!fb) {
5272 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005273 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005274 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005275
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005276 if (crtc->state) {
5277 const struct drm_plane_state *state = crtc->primary->state;
5278
5279 ret = check_src_coords(state->src_x, state->src_y,
5280 state->src_w, state->src_h, fb);
5281 } else {
5282 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5283 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005284 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005285 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005286
Matt Roperf4510a22014-04-01 15:22:40 -07005287 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005288 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5289 ret = -EINVAL;
5290 goto out;
5291 }
5292
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005293 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005294 e = kzalloc(sizeof *e, GFP_KERNEL);
5295 if (!e) {
5296 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005297 goto out;
5298 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005299 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005300 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005301 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005302 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5303 if (ret) {
5304 kfree(e);
5305 goto out;
5306 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005307 }
5308
Daniel Vetter3d30a592014-07-27 13:42:42 +02005309 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005310 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005311 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005312 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5313 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005314 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005315 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005316 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005317 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005318 /* Unref only the old framebuffer. */
5319 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005320 }
5321
5322out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005323 if (fb)
5324 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005325 if (crtc->primary->old_fb)
5326 drm_framebuffer_unreference(crtc->primary->old_fb);
5327 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005328 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005329
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005330 return ret;
5331}
Chris Wilsoneb033552011-01-24 15:11:08 +00005332
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005333/**
5334 * drm_mode_config_reset - call ->reset callbacks
5335 * @dev: drm device
5336 *
5337 * This functions calls all the crtc's, encoder's and connector's ->reset
5338 * callback. Drivers can use this in e.g. their driver load or resume code to
5339 * reset hardware and software state.
5340 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005341void drm_mode_config_reset(struct drm_device *dev)
5342{
5343 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005344 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005345 struct drm_encoder *encoder;
5346 struct drm_connector *connector;
5347
Daniel Vettere4f62542015-07-09 23:44:35 +02005348 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005349 if (plane->funcs->reset)
5350 plane->funcs->reset(plane);
5351
Daniel Vettere4f62542015-07-09 23:44:35 +02005352 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005353 if (crtc->funcs->reset)
5354 crtc->funcs->reset(crtc);
5355
Daniel Vettere4f62542015-07-09 23:44:35 +02005356 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005357 if (encoder->funcs->reset)
5358 encoder->funcs->reset(encoder);
5359
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005360 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005361 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005362 if (connector->funcs->reset)
5363 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005364 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005365}
5366EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005367
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005368/**
5369 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5370 * @dev: DRM device
5371 * @data: ioctl data
5372 * @file_priv: DRM file info
5373 *
5374 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5375 * TTM or something else entirely) and returns the resulting buffer handle. This
5376 * handle can then be wrapped up into a framebuffer modeset object.
5377 *
5378 * Note that userspace is not allowed to use such objects for render
5379 * acceleration - drivers must create their own private ioctls for such a use
5380 * case.
5381 *
5382 * Called by the user via ioctl.
5383 *
5384 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005385 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005386 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005387int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5388 void *data, struct drm_file *file_priv)
5389{
5390 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005391 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005392
5393 if (!dev->driver->dumb_create)
5394 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005395 if (!args->width || !args->height || !args->bpp)
5396 return -EINVAL;
5397
5398 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005399 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005400 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005401 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005402 return -EINVAL;
5403 stride = cpp * args->width;
5404 if (args->height > 0xffffffffU / stride)
5405 return -EINVAL;
5406
5407 /* test for wrap-around */
5408 size = args->height * stride;
5409 if (PAGE_ALIGN(size) == 0)
5410 return -EINVAL;
5411
Thierry Redingf6085952014-11-03 11:14:14 +01005412 /*
5413 * handle, pitch and size are output parameters. Zero them out to
5414 * prevent drivers from accidentally using uninitialized data. Since
5415 * not all existing userspace is clearing these fields properly we
5416 * cannot reject IOCTL with garbage in them.
5417 */
5418 args->handle = 0;
5419 args->pitch = 0;
5420 args->size = 0;
5421
Dave Airlieff72145b2011-02-07 12:16:14 +10005422 return dev->driver->dumb_create(file_priv, dev, args);
5423}
5424
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005425/**
5426 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5427 * @dev: DRM device
5428 * @data: ioctl data
5429 * @file_priv: DRM file info
5430 *
5431 * Allocate an offset in the drm device node's address space to be able to
5432 * memory map a dumb buffer.
5433 *
5434 * Called by the user via ioctl.
5435 *
5436 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005437 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005438 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005439int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5440 void *data, struct drm_file *file_priv)
5441{
5442 struct drm_mode_map_dumb *args = data;
5443
5444 /* call driver ioctl to get mmap offset */
5445 if (!dev->driver->dumb_map_offset)
5446 return -ENOSYS;
5447
5448 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5449}
5450
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005451/**
5452 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5453 * @dev: DRM device
5454 * @data: ioctl data
5455 * @file_priv: DRM file info
5456 *
5457 * This destroys the userspace handle for the given dumb backing storage buffer.
5458 * Since buffer objects must be reference counted in the kernel a buffer object
5459 * won't be immediately freed if a framebuffer modeset object still uses it.
5460 *
5461 * Called by the user via ioctl.
5462 *
5463 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005464 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005465 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005466int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5467 void *data, struct drm_file *file_priv)
5468{
5469 struct drm_mode_destroy_dumb *args = data;
5470
5471 if (!dev->driver->dumb_destroy)
5472 return -ENOSYS;
5473
5474 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5475}
Dave Airlie248dbc22011-11-29 20:02:54 +00005476
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005477/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305478 * drm_rotation_simplify() - Try to simplify the rotation
5479 * @rotation: Rotation to be simplified
5480 * @supported_rotations: Supported rotations
5481 *
5482 * Attempt to simplify the rotation to a form that is supported.
5483 * Eg. if the hardware supports everything except DRM_REFLECT_X
5484 * one could call this function like this:
5485 *
5486 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5487 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5488 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5489 *
5490 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5491 * transforms the hardware supports, this function may not
5492 * be able to produce a supported transform, so the caller should
5493 * check the result afterwards.
5494 */
5495unsigned int drm_rotation_simplify(unsigned int rotation,
5496 unsigned int supported_rotations)
5497{
5498 if (rotation & ~supported_rotations) {
5499 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005500 rotation = (rotation & DRM_REFLECT_MASK) |
5501 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305502 }
5503
5504 return rotation;
5505}
5506EXPORT_SYMBOL(drm_rotation_simplify);
5507
5508/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005509 * drm_mode_config_init - initialize DRM mode_configuration structure
5510 * @dev: DRM device
5511 *
5512 * Initialize @dev's mode_config structure, used for tracking the graphics
5513 * configuration of @dev.
5514 *
5515 * Since this initializes the modeset locks, no locking is possible. Which is no
5516 * problem, since this should happen single threaded at init time. It is the
5517 * driver's problem to ensure this guarantee.
5518 *
5519 */
5520void drm_mode_config_init(struct drm_device *dev)
5521{
5522 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005523 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005524 mutex_init(&dev->mode_config.idr_mutex);
5525 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005526 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005527 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5528 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5529 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5530 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5531 INIT_LIST_HEAD(&dev->mode_config.property_list);
5532 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5533 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5534 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005535 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005536 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005537
5538 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005539 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005540 drm_modeset_unlock_all(dev);
5541
5542 /* Just to be sure */
5543 dev->mode_config.num_fb = 0;
5544 dev->mode_config.num_connector = 0;
5545 dev->mode_config.num_crtc = 0;
5546 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005547 dev->mode_config.num_overlay_plane = 0;
5548 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005549}
5550EXPORT_SYMBOL(drm_mode_config_init);
5551
5552/**
5553 * drm_mode_config_cleanup - free up DRM mode_config info
5554 * @dev: DRM device
5555 *
5556 * Free up all the connectors and CRTCs associated with this DRM device, then
5557 * free up the framebuffers and associated buffer objects.
5558 *
5559 * Note that since this /should/ happen single-threaded at driver/device
5560 * teardown time, no locking is required. It's the driver's job to ensure that
5561 * this guarantee actually holds true.
5562 *
5563 * FIXME: cleanup any dangling user buffer objects too
5564 */
5565void drm_mode_config_cleanup(struct drm_device *dev)
5566{
5567 struct drm_connector *connector, *ot;
5568 struct drm_crtc *crtc, *ct;
5569 struct drm_encoder *encoder, *enct;
5570 struct drm_framebuffer *fb, *fbt;
5571 struct drm_property *property, *pt;
5572 struct drm_property_blob *blob, *bt;
5573 struct drm_plane *plane, *plt;
5574
5575 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5576 head) {
5577 encoder->funcs->destroy(encoder);
5578 }
5579
5580 list_for_each_entry_safe(connector, ot,
5581 &dev->mode_config.connector_list, head) {
5582 connector->funcs->destroy(connector);
5583 }
5584
5585 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5586 head) {
5587 drm_property_destroy(dev, property);
5588 }
5589
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005590 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5591 head) {
5592 plane->funcs->destroy(plane);
5593 }
5594
5595 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5596 crtc->funcs->destroy(crtc);
5597 }
5598
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005599 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005600 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005601 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005602 }
5603
5604 /*
5605 * Single-threaded teardown context, so it's not required to grab the
5606 * fb_lock to protect against concurrent fb_list access. Contrary, it
5607 * would actually deadlock with the drm_framebuffer_cleanup function.
5608 *
5609 * Also, if there are any framebuffers left, that's a driver leak now,
5610 * so politely WARN about this.
5611 */
5612 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5613 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf62016-04-15 15:10:36 +10005614 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005615 }
5616
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005617 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005618 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005619 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005620 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005621}
5622EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305623
5624struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5625 unsigned int supported_rotations)
5626{
5627 static const struct drm_prop_enum_list props[] = {
5628 { DRM_ROTATE_0, "rotate-0" },
5629 { DRM_ROTATE_90, "rotate-90" },
5630 { DRM_ROTATE_180, "rotate-180" },
5631 { DRM_ROTATE_270, "rotate-270" },
5632 { DRM_REFLECT_X, "reflect-x" },
5633 { DRM_REFLECT_Y, "reflect-y" },
5634 };
5635
5636 return drm_property_create_bitmask(dev, 0, "rotation",
5637 props, ARRAY_SIZE(props),
5638 supported_rotations);
5639}
5640EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005641
5642/**
5643 * DOC: Tile group
5644 *
5645 * Tile groups are used to represent tiled monitors with a unique
5646 * integer identifier. Tiled monitors using DisplayID v1.3 have
5647 * a unique 8-byte handle, we store this in a tile group, so we
5648 * have a common identifier for all tiles in a monitor group.
5649 */
5650static void drm_tile_group_free(struct kref *kref)
5651{
5652 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5653 struct drm_device *dev = tg->dev;
5654 mutex_lock(&dev->mode_config.idr_mutex);
5655 idr_remove(&dev->mode_config.tile_idr, tg->id);
5656 mutex_unlock(&dev->mode_config.idr_mutex);
5657 kfree(tg);
5658}
5659
5660/**
5661 * drm_mode_put_tile_group - drop a reference to a tile group.
5662 * @dev: DRM device
5663 * @tg: tile group to drop reference to.
5664 *
5665 * drop reference to tile group and free if 0.
5666 */
5667void drm_mode_put_tile_group(struct drm_device *dev,
5668 struct drm_tile_group *tg)
5669{
5670 kref_put(&tg->refcount, drm_tile_group_free);
5671}
5672
5673/**
5674 * drm_mode_get_tile_group - get a reference to an existing tile group
5675 * @dev: DRM device
5676 * @topology: 8-bytes unique per monitor.
5677 *
5678 * Use the unique bytes to get a reference to an existing tile group.
5679 *
5680 * RETURNS:
5681 * tile group or NULL if not found.
5682 */
5683struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5684 char topology[8])
5685{
5686 struct drm_tile_group *tg;
5687 int id;
5688 mutex_lock(&dev->mode_config.idr_mutex);
5689 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5690 if (!memcmp(tg->group_data, topology, 8)) {
5691 if (!kref_get_unless_zero(&tg->refcount))
5692 tg = NULL;
5693 mutex_unlock(&dev->mode_config.idr_mutex);
5694 return tg;
5695 }
5696 }
5697 mutex_unlock(&dev->mode_config.idr_mutex);
5698 return NULL;
5699}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005700EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005701
5702/**
5703 * drm_mode_create_tile_group - create a tile group from a displayid description
5704 * @dev: DRM device
5705 * @topology: 8-bytes unique per monitor.
5706 *
5707 * Create a tile group for the unique monitor, and get a unique
5708 * identifier for the tile group.
5709 *
5710 * RETURNS:
5711 * new tile group or error.
5712 */
5713struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5714 char topology[8])
5715{
5716 struct drm_tile_group *tg;
5717 int ret;
5718
5719 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5720 if (!tg)
5721 return ERR_PTR(-ENOMEM);
5722
5723 kref_init(&tg->refcount);
5724 memcpy(tg->group_data, topology, 8);
5725 tg->dev = dev;
5726
5727 mutex_lock(&dev->mode_config.idr_mutex);
5728 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5729 if (ret >= 0) {
5730 tg->id = ret;
5731 } else {
5732 kfree(tg);
5733 tg = ERR_PTR(ret);
5734 }
5735
5736 mutex_unlock(&dev->mode_config.idr_mutex);
5737 return tg;
5738}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005739EXPORT_SYMBOL(drm_mode_create_tile_group);
Jyri Sarhaf8ed34a2016-06-07 15:09:14 +03005740
5741/**
5742 * drm_crtc_enable_color_mgmt - enable color management properties
5743 * @crtc: DRM CRTC
5744 * @degamma_lut_size: the size of the degamma lut (before CSC)
5745 * @has_ctm: whether to attach ctm_property for CSC matrix
5746 * @gamma_lut_size: the size of the gamma lut (after CSC)
5747 *
5748 * This function lets the driver enable the color correction
5749 * properties on a CRTC. This includes 3 degamma, csc and gamma
5750 * properties that userspace can set and 2 size properties to inform
5751 * the userspace of the lut sizes. Each of the properties are
5752 * optional. The gamma and degamma properties are only attached if
5753 * their size is not 0 and ctm_property is only attached if has_ctm is
5754 * true.
5755 */
5756void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5757 uint degamma_lut_size,
5758 bool has_ctm,
5759 uint gamma_lut_size)
5760{
5761 struct drm_device *dev = crtc->dev;
5762 struct drm_mode_config *config = &dev->mode_config;
5763
5764 if (degamma_lut_size) {
5765 drm_object_attach_property(&crtc->base,
5766 config->degamma_lut_property, 0);
5767 drm_object_attach_property(&crtc->base,
5768 config->degamma_lut_size_property,
5769 degamma_lut_size);
5770 }
5771
5772 if (has_ctm)
5773 drm_object_attach_property(&crtc->base,
5774 config->ctm_property, 0);
5775
5776 if (gamma_lut_size) {
5777 drm_object_attach_property(&crtc->base,
5778 config->gamma_lut_property, 0);
5779 drm_object_attach_property(&crtc->base,
5780 config->gamma_lut_size_property,
5781 gamma_lut_size);
5782 }
5783}
5784EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);