blob: aeb5d9e087fc3d4f902e8a8adcc952f3f053b80f [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 Airlied0f37cf2016-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 Airlied0f37cf2016-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 Airlied0f37cf2016-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 Airlied0f37cf2016-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 Airlied0f37cf2016-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 Airlied0f37cf2016-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 Airlied0f37cf2016-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 Airlied0f37cf2016-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
Rob Clark51fd3712013-11-19 12:10:12 -0500641 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200642 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
643 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100644 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800645
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200646 if (name) {
647 va_list ap;
648
649 va_start(ap, name);
650 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
651 va_end(ap);
652 } else {
653 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
654 drm_num_crtcs(dev));
655 }
656 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000657 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200658 return -ENOMEM;
659 }
660
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300661 crtc->base.properties = &crtc->properties;
662
Rob Clark51fd3712013-11-19 12:10:12 -0500663 list_add_tail(&crtc->head, &config->crtc_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100664 crtc->index = config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200665
Matt Ropere13161a2014-04-01 15:22:38 -0700666 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700667 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700668 if (primary)
669 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700670 if (cursor)
671 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700672
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100673 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
674 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100675 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100676 }
677
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100678 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800679}
Matt Ropere13161a2014-04-01 15:22:38 -0700680EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800681
682/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000683 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800684 * @crtc: CRTC to cleanup
685 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000686 * This function cleans up @crtc and removes it from the DRM mode setting
687 * core. Note that the function does *not* free the crtc structure itself,
688 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800689 */
690void drm_crtc_cleanup(struct drm_crtc *crtc)
691{
692 struct drm_device *dev = crtc->dev;
693
Chris Wilson490d3d12016-05-27 20:05:00 +0100694 /* Note that the crtc_list is considered to be static; should we
695 * remove the drm_crtc at runtime we would have to decrement all
696 * the indices on the drm_crtc after us in the crtc_list.
697 */
698
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000699 kfree(crtc->gamma_store);
700 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800701
Rob Clark51fd3712013-11-19 12:10:12 -0500702 drm_modeset_lock_fini(&crtc->mutex);
703
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000704 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800705 list_del(&crtc->head);
706 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100707
708 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
709 if (crtc->state && crtc->funcs->atomic_destroy_state)
710 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100711
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200712 kfree(crtc->name);
713
Thierry Redinga18c0af2014-12-10 11:38:49 +0100714 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800715}
716EXPORT_SYMBOL(drm_crtc_cleanup);
717
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100718/*
Dave Airlief453ba02008-11-07 14:05:41 -0800719 * drm_mode_remove - remove and free a mode
720 * @connector: connector list to modify
721 * @mode: mode to remove
722 *
Dave Airlief453ba02008-11-07 14:05:41 -0800723 * Remove @mode from @connector's mode list, then free it.
724 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100725static void drm_mode_remove(struct drm_connector *connector,
726 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800727{
728 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100729 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800730}
Dave Airlief453ba02008-11-07 14:05:41 -0800731
732/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200733 * drm_display_info_set_bus_formats - set the supported bus formats
734 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100735 * @formats: array containing the supported bus formats
736 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200737 *
738 * Store the supported bus formats in display info structure.
739 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
740 * a full list of available formats.
741 */
742int drm_display_info_set_bus_formats(struct drm_display_info *info,
743 const u32 *formats,
744 unsigned int num_formats)
745{
746 u32 *fmts = NULL;
747
748 if (!formats && num_formats)
749 return -EINVAL;
750
751 if (formats && num_formats) {
752 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
753 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300754 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200755 return -ENOMEM;
756 }
757
758 kfree(info->bus_formats);
759 info->bus_formats = fmts;
760 info->num_bus_formats = num_formats;
761
762 return 0;
763}
764EXPORT_SYMBOL(drm_display_info_set_bus_formats);
765
766/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200767 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
768 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200769 *
770 * The kernel supports per-connector configration of its consoles through
771 * use of the video= parameter. This function parses that option and
772 * extracts the user's specified mode (or enable/disable status) for a
773 * particular connector. This is typically only used during the early fbdev
774 * setup.
775 */
776static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
777{
778 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
779 char *option = NULL;
780
781 if (fb_get_options(connector->name, &option))
782 return;
783
784 if (!drm_mode_parse_command_line_for_connector(option,
785 connector,
786 mode))
787 return;
788
789 if (mode->force) {
790 const char *s;
791
792 switch (mode->force) {
793 case DRM_FORCE_OFF:
794 s = "OFF";
795 break;
796 case DRM_FORCE_ON_DIGITAL:
797 s = "ON - dig";
798 break;
799 default:
800 case DRM_FORCE_ON:
801 s = "ON";
802 break;
803 }
804
805 DRM_INFO("forcing %s connector %s\n", connector->name, s);
806 connector->force = mode->force;
807 }
808
809 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
810 connector->name,
811 mode->xres, mode->yres,
812 mode->refresh_specified ? mode->refresh : 60,
813 mode->rb ? " reduced blanking" : "",
814 mode->margins ? " with margins" : "",
815 mode->interlace ? " interlaced" : "");
816}
817
Dave Airlieb164d312016-04-27 11:10:09 +1000818static void drm_connector_free(struct kref *kref)
819{
820 struct drm_connector *connector =
821 container_of(kref, struct drm_connector, base.refcount);
822 struct drm_device *dev = connector->dev;
823
824 drm_mode_object_unregister(dev, &connector->base);
825 connector->funcs->destroy(connector);
826}
827
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200828/**
Dave Airlief453ba02008-11-07 14:05:41 -0800829 * drm_connector_init - Init a preallocated connector
830 * @dev: DRM device
831 * @connector: the connector to init
832 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100833 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800834 *
Dave Airlief453ba02008-11-07 14:05:41 -0800835 * Initialises a preallocated connector. Connectors should be
836 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200837 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100838 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200839 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800840 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200841int drm_connector_init(struct drm_device *dev,
842 struct drm_connector *connector,
843 const struct drm_connector_funcs *funcs,
844 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800845{
Rob Clarkae16c592014-12-18 16:01:54 -0500846 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200847 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400848 struct ida *connector_ida =
849 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200850
Daniel Vetter84849902012-12-02 00:28:11 +0100851 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800852
Dave Airlieb164d312016-04-27 11:10:09 +1000853 ret = drm_mode_object_get_reg(dev, &connector->base,
854 DRM_MODE_OBJECT_CONNECTOR,
855 false, drm_connector_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200856 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300857 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200858
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300859 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800860 connector->dev = dev;
861 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100862
863 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
864 if (connector->connector_id < 0) {
865 ret = connector->connector_id;
866 goto out_put;
867 }
868
Dave Airlief453ba02008-11-07 14:05:41 -0800869 connector->connector_type = connector_type;
870 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400871 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
872 if (connector->connector_type_id < 0) {
873 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100874 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400875 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300876 connector->name =
877 kasprintf(GFP_KERNEL, "%s-%d",
878 drm_connector_enum_list[connector_type].name,
879 connector->connector_type_id);
880 if (!connector->name) {
881 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100882 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300883 }
884
Dave Airlief453ba02008-11-07 14:05:41 -0800885 INIT_LIST_HEAD(&connector->probed_modes);
886 INIT_LIST_HEAD(&connector->modes);
887 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000888 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800889
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200890 drm_connector_get_cmdline_mode(connector);
891
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100892 /* We should add connectors at the end to avoid upsetting the connector
893 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500894 list_add_tail(&connector->head, &config->connector_list);
895 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800896
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200897 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500898 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500899 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200900 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800901
Rob Clark58495562012-10-11 20:50:56 -0500902 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500903 config->dpms_property, 0);
904
905 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
906 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
907 }
Dave Airlief453ba02008-11-07 14:05:41 -0800908
Thomas Wood30f65702014-06-18 17:52:32 +0100909 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100910out_put_type_id:
911 if (ret)
912 ida_remove(connector_ida, connector->connector_type_id);
913out_put_id:
914 if (ret)
915 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300916out_put:
917 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000918 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300919
920out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100921 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200922
923 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800924}
925EXPORT_SYMBOL(drm_connector_init);
926
927/**
928 * drm_connector_cleanup - cleans up an initialised connector
929 * @connector: connector to cleanup
930 *
Dave Airlief453ba02008-11-07 14:05:41 -0800931 * Cleans up the connector but doesn't free the object.
932 */
933void drm_connector_cleanup(struct drm_connector *connector)
934{
935 struct drm_device *dev = connector->dev;
936 struct drm_display_mode *mode, *t;
937
Dave Airlie40d9b042014-10-20 16:29:33 +1000938 if (connector->tile_group) {
939 drm_mode_put_tile_group(dev, connector->tile_group);
940 connector->tile_group = NULL;
941 }
942
Dave Airlief453ba02008-11-07 14:05:41 -0800943 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
944 drm_mode_remove(connector, mode);
945
946 list_for_each_entry_safe(mode, t, &connector->modes, head)
947 drm_mode_remove(connector, mode);
948
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400949 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
950 connector->connector_type_id);
951
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100952 ida_remove(&dev->mode_config.connector_ida,
953 connector->connector_id);
954
Boris Brezillonb5571e92014-07-22 12:09:10 +0200955 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000956 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300957 kfree(connector->name);
958 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800959 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000960 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100961
962 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
963 if (connector->state && connector->funcs->atomic_destroy_state)
964 connector->funcs->atomic_destroy_state(connector,
965 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100966
967 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800968}
969EXPORT_SYMBOL(drm_connector_cleanup);
970
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100971/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100972 * drm_connector_register - register a connector
973 * @connector: the connector to register
974 *
975 * Register userspace interfaces for a connector
976 *
977 * Returns:
978 * Zero on success, error code on failure.
979 */
980int drm_connector_register(struct drm_connector *connector)
981{
Thomas Wood30f65702014-06-18 17:52:32 +0100982 int ret;
983
984 ret = drm_sysfs_connector_add(connector);
985 if (ret)
986 return ret;
987
988 ret = drm_debugfs_connector_add(connector);
989 if (ret) {
990 drm_sysfs_connector_remove(connector);
991 return ret;
992 }
993
Daniel Vetterfdf2c852016-05-06 14:55:02 +0200994 drm_mode_object_register(connector->dev, &connector->base);
995
Thomas Wood30f65702014-06-18 17:52:32 +0100996 return 0;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100997}
998EXPORT_SYMBOL(drm_connector_register);
999
1000/**
1001 * drm_connector_unregister - unregister a connector
1002 * @connector: the connector to unregister
1003 *
1004 * Unregister userspace interfaces for a connector
1005 */
1006void drm_connector_unregister(struct drm_connector *connector)
1007{
1008 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001009 drm_debugfs_connector_remove(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001010}
1011EXPORT_SYMBOL(drm_connector_unregister);
1012
Thomas Wood34ea3d32014-05-29 16:57:41 +01001013/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001014 * drm_connector_register_all - register all connectors
1015 * @dev: drm device
1016 *
1017 * This function registers all connectors in sysfs and other places so that
1018 * userspace can start to access them. Drivers can call it after calling
1019 * drm_dev_register() to complete the device registration, if they don't call
1020 * drm_connector_register() on each connector individually.
1021 *
1022 * When a device is unplugged and should be removed from userspace access,
1023 * call drm_connector_unregister_all(), which is the inverse of this
1024 * function.
1025 *
1026 * Returns:
1027 * Zero on success, error code on failure.
1028 */
1029int drm_connector_register_all(struct drm_device *dev)
1030{
1031 struct drm_connector *connector;
1032 int ret;
1033
1034 mutex_lock(&dev->mode_config.mutex);
1035
1036 drm_for_each_connector(connector, dev) {
1037 ret = drm_connector_register(connector);
1038 if (ret)
1039 goto err;
1040 }
1041
1042 mutex_unlock(&dev->mode_config.mutex);
1043
1044 return 0;
1045
1046err:
1047 mutex_unlock(&dev->mode_config.mutex);
1048 drm_connector_unregister_all(dev);
1049 return ret;
1050}
1051EXPORT_SYMBOL(drm_connector_register_all);
1052
1053/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001054 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001055 * @dev: drm device
1056 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001057 * This functions unregisters all connectors from sysfs and other places so
1058 * that userspace can no longer access them. Drivers should call this as the
1059 * first step tearing down the device instace, or when the underlying
1060 * physical device disappeared (e.g. USB unplug), right before calling
1061 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001062 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001063void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001064{
1065 struct drm_connector *connector;
1066
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001067 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001068 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001069 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001070}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001071EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001072
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001073/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001074 * drm_encoder_init - Init a preallocated encoder
1075 * @dev: drm device
1076 * @encoder: the encoder to init
1077 * @funcs: callbacks for this encoder
1078 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001079 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001080 *
1081 * Initialises a preallocated encoder. Encoder should be
1082 * subclassed as part of driver encoder objects.
1083 *
1084 * Returns:
1085 * Zero on success, error code on failure.
1086 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001087int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001088 struct drm_encoder *encoder,
1089 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001090 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001091{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001092 int ret;
1093
Daniel Vetter84849902012-12-02 00:28:11 +01001094 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001095
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001096 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1097 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001098 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001099
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001100 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001101 encoder->encoder_type = encoder_type;
1102 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001103 if (name) {
1104 va_list ap;
1105
1106 va_start(ap, name);
1107 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1108 va_end(ap);
1109 } else {
1110 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1111 drm_encoder_enum_list[encoder_type].name,
1112 encoder->base.id);
1113 }
Jani Nikulae5748942014-05-14 16:58:20 +03001114 if (!encoder->name) {
1115 ret = -ENOMEM;
1116 goto out_put;
1117 }
Dave Airlief453ba02008-11-07 14:05:41 -08001118
1119 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001120 encoder->index = dev->mode_config.num_encoder++;
Dave Airlief453ba02008-11-07 14:05:41 -08001121
Jani Nikulae5748942014-05-14 16:58:20 +03001122out_put:
1123 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001124 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001125
1126out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001127 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001128
1129 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001130}
1131EXPORT_SYMBOL(drm_encoder_init);
1132
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001133/**
1134 * drm_encoder_cleanup - cleans up an initialised encoder
1135 * @encoder: encoder to cleanup
1136 *
1137 * Cleans up the encoder but doesn't free the object.
1138 */
Dave Airlief453ba02008-11-07 14:05:41 -08001139void drm_encoder_cleanup(struct drm_encoder *encoder)
1140{
1141 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001142
Chris Wilson490d3d12016-05-27 20:05:00 +01001143 /* Note that the encoder_list is considered to be static; should we
1144 * remove the drm_encoder at runtime we would have to decrement all
1145 * the indices on the drm_encoder after us in the encoder_list.
1146 */
1147
Daniel Vetter84849902012-12-02 00:28:11 +01001148 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001149 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001150 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001151 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001152 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001153 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001154
1155 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001156}
1157EXPORT_SYMBOL(drm_encoder_cleanup);
1158
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001159static unsigned int drm_num_planes(struct drm_device *dev)
1160{
1161 unsigned int num = 0;
1162 struct drm_plane *tmp;
1163
1164 drm_for_each_plane(tmp, dev) {
1165 num++;
1166 }
1167
1168 return num;
1169}
1170
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001171/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001172 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001173 * @dev: DRM device
1174 * @plane: plane object to init
1175 * @possible_crtcs: bitmask of possible CRTCs
1176 * @funcs: callbacks for the new plane
1177 * @formats: array of supported formats (%DRM_FORMAT_*)
1178 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001179 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001180 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001181 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001182 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001183 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001184 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001185 * Zero on success, error code on failure.
1186 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001187int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1188 unsigned long possible_crtcs,
1189 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001190 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001191 enum drm_plane_type type,
1192 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001193{
Rob Clark6b4959f2014-12-18 16:01:53 -05001194 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001195 int ret;
1196
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001197 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1198 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001199 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001200
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001201 drm_modeset_lock_init(&plane->mutex);
1202
Rob Clark4d939142012-05-17 02:23:27 -06001203 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001204 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001205 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001206 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1207 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001208 if (!plane->format_types) {
1209 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001210 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001211 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001212 }
1213
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001214 if (name) {
1215 va_list ap;
1216
1217 va_start(ap, name);
1218 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1219 va_end(ap);
1220 } else {
1221 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1222 drm_num_planes(dev));
1223 }
1224 if (!plane->name) {
1225 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001226 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001227 return -ENOMEM;
1228 }
1229
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001230 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001231 plane->format_count = format_count;
1232 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001233 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001234
Rob Clark6b4959f2014-12-18 16:01:53 -05001235 list_add_tail(&plane->head, &config->plane_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001236 plane->index = config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001237 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001238 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001239
Rob Clark9922ab52014-04-01 20:16:57 -04001240 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001241 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001242 plane->type);
1243
Rob Clark6b4959f2014-12-18 16:01:53 -05001244 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1245 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1246 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1247 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1248 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1249 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1250 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1251 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1252 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1253 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1254 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1255 }
1256
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001257 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001258}
Matt Roperdc415ff2014-04-01 15:22:36 -07001259EXPORT_SYMBOL(drm_universal_plane_init);
1260
1261/**
1262 * drm_plane_init - Initialize a legacy plane
1263 * @dev: DRM device
1264 * @plane: plane object to init
1265 * @possible_crtcs: bitmask of possible CRTCs
1266 * @funcs: callbacks for the new plane
1267 * @formats: array of supported formats (%DRM_FORMAT_*)
1268 * @format_count: number of elements in @formats
1269 * @is_primary: plane type (primary vs overlay)
1270 *
1271 * Legacy API to initialize a DRM plane.
1272 *
1273 * New drivers should call drm_universal_plane_init() instead.
1274 *
1275 * Returns:
1276 * Zero on success, error code on failure.
1277 */
1278int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1279 unsigned long possible_crtcs,
1280 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001281 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001282 bool is_primary)
1283{
1284 enum drm_plane_type type;
1285
1286 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1287 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001288 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001289}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001290EXPORT_SYMBOL(drm_plane_init);
1291
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001292/**
1293 * drm_plane_cleanup - Clean up the core plane usage
1294 * @plane: plane to cleanup
1295 *
1296 * This function cleans up @plane and removes it from the DRM mode setting
1297 * core. Note that the function does *not* free the plane structure itself,
1298 * this is the responsibility of the caller.
1299 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001300void drm_plane_cleanup(struct drm_plane *plane)
1301{
1302 struct drm_device *dev = plane->dev;
1303
Daniel Vetter84849902012-12-02 00:28:11 +01001304 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001305 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001306 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001307
1308 BUG_ON(list_empty(&plane->head));
1309
Chris Wilson490d3d12016-05-27 20:05:00 +01001310 /* Note that the plane_list is considered to be static; should we
1311 * remove the drm_plane at runtime we would have to decrement all
1312 * the indices on the drm_plane after us in the plane_list.
1313 */
1314
Matt Roperdc415ff2014-04-01 15:22:36 -07001315 list_del(&plane->head);
1316 dev->mode_config.num_total_plane--;
1317 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1318 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001319 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001320
1321 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1322 if (plane->state && plane->funcs->atomic_destroy_state)
1323 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001324
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001325 kfree(plane->name);
1326
Thierry Redinga18c0af2014-12-10 11:38:49 +01001327 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001328}
1329EXPORT_SYMBOL(drm_plane_cleanup);
1330
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001331/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001332 * drm_plane_from_index - find the registered plane at an index
1333 * @dev: DRM device
1334 * @idx: index of registered plane to find for
1335 *
1336 * Given a plane index, return the registered plane from DRM device's
1337 * list of planes with matching index.
1338 */
1339struct drm_plane *
1340drm_plane_from_index(struct drm_device *dev, int idx)
1341{
1342 struct drm_plane *plane;
Chandra Konduruf81338a2015-04-09 17:36:21 -07001343
Chris Wilson490d3d12016-05-27 20:05:00 +01001344 drm_for_each_plane(plane, dev)
1345 if (idx == plane->index)
Chandra Konduruf81338a2015-04-09 17:36:21 -07001346 return plane;
Chris Wilson490d3d12016-05-27 20:05:00 +01001347
Chandra Konduruf81338a2015-04-09 17:36:21 -07001348 return NULL;
1349}
1350EXPORT_SYMBOL(drm_plane_from_index);
1351
1352/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001353 * drm_plane_force_disable - Forcibly disable a plane
1354 * @plane: plane to disable
1355 *
1356 * Forces the plane to be disabled.
1357 *
1358 * Used when the plane's current framebuffer is destroyed,
1359 * and when restoring fbdev mode.
1360 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001361void drm_plane_force_disable(struct drm_plane *plane)
1362{
1363 int ret;
1364
Daniel Vetter3d30a592014-07-27 13:42:42 +02001365 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001366 return;
1367
Daniel Vetter3d30a592014-07-27 13:42:42 +02001368 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001369 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001370 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001371 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001372 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001373 return;
1374 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001375 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001376 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001377 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001378 plane->fb = NULL;
1379 plane->crtc = NULL;
1380}
1381EXPORT_SYMBOL(drm_plane_force_disable);
1382
Rob Clark6b4959f2014-12-18 16:01:53 -05001383static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001384{
Rob Clark356af0e2014-12-18 16:01:52 -05001385 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001386
1387 /*
1388 * Standard properties (apply to all connectors)
1389 */
Rob Clark356af0e2014-12-18 16:01:52 -05001390 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001391 DRM_MODE_PROP_IMMUTABLE,
1392 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001393 if (!prop)
1394 return -ENOMEM;
1395 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001396
Rob Clark356af0e2014-12-18 16:01:52 -05001397 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001398 "DPMS", drm_dpms_enum_list,
1399 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001400 if (!prop)
1401 return -ENOMEM;
1402 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001403
Rob Clark356af0e2014-12-18 16:01:52 -05001404 prop = drm_property_create(dev,
1405 DRM_MODE_PROP_BLOB |
1406 DRM_MODE_PROP_IMMUTABLE,
1407 "PATH", 0);
1408 if (!prop)
1409 return -ENOMEM;
1410 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001411
Rob Clark356af0e2014-12-18 16:01:52 -05001412 prop = drm_property_create(dev,
1413 DRM_MODE_PROP_BLOB |
1414 DRM_MODE_PROP_IMMUTABLE,
1415 "TILE", 0);
1416 if (!prop)
1417 return -ENOMEM;
1418 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001419
Rob Clark6b4959f2014-12-18 16:01:53 -05001420 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001421 "type", drm_plane_type_enum_list,
1422 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001423 if (!prop)
1424 return -ENOMEM;
1425 dev->mode_config.plane_type_property = prop;
1426
1427 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1428 "SRC_X", 0, UINT_MAX);
1429 if (!prop)
1430 return -ENOMEM;
1431 dev->mode_config.prop_src_x = prop;
1432
1433 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1434 "SRC_Y", 0, UINT_MAX);
1435 if (!prop)
1436 return -ENOMEM;
1437 dev->mode_config.prop_src_y = prop;
1438
1439 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1440 "SRC_W", 0, UINT_MAX);
1441 if (!prop)
1442 return -ENOMEM;
1443 dev->mode_config.prop_src_w = prop;
1444
1445 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1446 "SRC_H", 0, UINT_MAX);
1447 if (!prop)
1448 return -ENOMEM;
1449 dev->mode_config.prop_src_h = prop;
1450
1451 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1452 "CRTC_X", INT_MIN, INT_MAX);
1453 if (!prop)
1454 return -ENOMEM;
1455 dev->mode_config.prop_crtc_x = prop;
1456
1457 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1458 "CRTC_Y", INT_MIN, INT_MAX);
1459 if (!prop)
1460 return -ENOMEM;
1461 dev->mode_config.prop_crtc_y = prop;
1462
1463 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1464 "CRTC_W", 0, INT_MAX);
1465 if (!prop)
1466 return -ENOMEM;
1467 dev->mode_config.prop_crtc_w = prop;
1468
1469 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1470 "CRTC_H", 0, INT_MAX);
1471 if (!prop)
1472 return -ENOMEM;
1473 dev->mode_config.prop_crtc_h = prop;
1474
1475 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1476 "FB_ID", DRM_MODE_OBJECT_FB);
1477 if (!prop)
1478 return -ENOMEM;
1479 dev->mode_config.prop_fb_id = prop;
1480
1481 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1482 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1483 if (!prop)
1484 return -ENOMEM;
1485 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001486
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001487 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1488 "ACTIVE");
1489 if (!prop)
1490 return -ENOMEM;
1491 dev->mode_config.prop_active = prop;
1492
Daniel Stone955f3c32015-05-25 19:11:52 +01001493 prop = drm_property_create(dev,
1494 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1495 "MODE_ID", 0);
1496 if (!prop)
1497 return -ENOMEM;
1498 dev->mode_config.prop_mode_id = prop;
1499
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001500 prop = drm_property_create(dev,
1501 DRM_MODE_PROP_BLOB,
1502 "DEGAMMA_LUT", 0);
1503 if (!prop)
1504 return -ENOMEM;
1505 dev->mode_config.degamma_lut_property = prop;
1506
1507 prop = drm_property_create_range(dev,
1508 DRM_MODE_PROP_IMMUTABLE,
1509 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1510 if (!prop)
1511 return -ENOMEM;
1512 dev->mode_config.degamma_lut_size_property = prop;
1513
1514 prop = drm_property_create(dev,
1515 DRM_MODE_PROP_BLOB,
1516 "CTM", 0);
1517 if (!prop)
1518 return -ENOMEM;
1519 dev->mode_config.ctm_property = prop;
1520
1521 prop = drm_property_create(dev,
1522 DRM_MODE_PROP_BLOB,
1523 "GAMMA_LUT", 0);
1524 if (!prop)
1525 return -ENOMEM;
1526 dev->mode_config.gamma_lut_property = prop;
1527
1528 prop = drm_property_create_range(dev,
1529 DRM_MODE_PROP_IMMUTABLE,
1530 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1531 if (!prop)
1532 return -ENOMEM;
1533 dev->mode_config.gamma_lut_size_property = prop;
1534
Rob Clark9922ab52014-04-01 20:16:57 -04001535 return 0;
1536}
1537
Dave Airlief453ba02008-11-07 14:05:41 -08001538/**
1539 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1540 * @dev: DRM device
1541 *
1542 * Called by a driver the first time a DVI-I connector is made.
1543 */
1544int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1545{
1546 struct drm_property *dvi_i_selector;
1547 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001548
1549 if (dev->mode_config.dvi_i_select_subconnector_property)
1550 return 0;
1551
1552 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001553 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001554 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001555 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001556 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001557 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1558
Sascha Hauer4a67d392012-02-06 10:58:17 +01001559 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001560 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001561 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001562 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001563 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1564
1565 return 0;
1566}
1567EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1568
1569/**
1570 * drm_create_tv_properties - create TV specific connector properties
1571 * @dev: DRM device
1572 * @num_modes: number of different TV formats (modes) supported
1573 * @modes: array of pointers to strings containing name of each format
1574 *
1575 * Called by a driver's TV initialization routine, this function creates
1576 * the TV specific connector properties for a given device. Caller is
1577 * responsible for allocating a list of format names and passing them to
1578 * this routine.
1579 */
Thierry Reding2f763312014-10-13 12:45:57 +02001580int drm_mode_create_tv_properties(struct drm_device *dev,
1581 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001582 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001583{
1584 struct drm_property *tv_selector;
1585 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001586 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001587
1588 if (dev->mode_config.tv_select_subconnector_property)
1589 return 0;
1590
1591 /*
1592 * Basic connector properties
1593 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001594 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001595 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001596 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001597 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001598 if (!tv_selector)
1599 goto nomem;
1600
Dave Airlief453ba02008-11-07 14:05:41 -08001601 dev->mode_config.tv_select_subconnector_property = tv_selector;
1602
1603 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001604 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1605 "subconnector",
1606 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001607 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001608 if (!tv_subconnector)
1609 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001610 dev->mode_config.tv_subconnector_property = tv_subconnector;
1611
1612 /*
1613 * Other, TV specific properties: margins & TV modes.
1614 */
1615 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001616 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001617 if (!dev->mode_config.tv_left_margin_property)
1618 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001619
1620 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001621 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001622 if (!dev->mode_config.tv_right_margin_property)
1623 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001624
1625 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001626 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001627 if (!dev->mode_config.tv_top_margin_property)
1628 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001629
1630 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001631 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001632 if (!dev->mode_config.tv_bottom_margin_property)
1633 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001634
1635 dev->mode_config.tv_mode_property =
1636 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1637 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001638 if (!dev->mode_config.tv_mode_property)
1639 goto nomem;
1640
Dave Airlief453ba02008-11-07 14:05:41 -08001641 for (i = 0; i < num_modes; i++)
1642 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1643 i, modes[i]);
1644
Francisco Jerezb6b79022009-08-02 04:19:20 +02001645 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001646 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001647 if (!dev->mode_config.tv_brightness_property)
1648 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001649
1650 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001651 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001652 if (!dev->mode_config.tv_contrast_property)
1653 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001654
1655 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001656 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001657 if (!dev->mode_config.tv_flicker_reduction_property)
1658 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001659
Francisco Jereza75f0232009-08-12 02:30:10 +02001660 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001661 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001662 if (!dev->mode_config.tv_overscan_property)
1663 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001664
1665 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001666 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001667 if (!dev->mode_config.tv_saturation_property)
1668 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001669
1670 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001671 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001672 if (!dev->mode_config.tv_hue_property)
1673 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001674
Dave Airlief453ba02008-11-07 14:05:41 -08001675 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001676nomem:
1677 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001678}
1679EXPORT_SYMBOL(drm_mode_create_tv_properties);
1680
1681/**
1682 * drm_mode_create_scaling_mode_property - create scaling mode property
1683 * @dev: DRM device
1684 *
1685 * Called by a driver the first time it's needed, must be attached to desired
1686 * connectors.
1687 */
1688int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1689{
1690 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001691
1692 if (dev->mode_config.scaling_mode_property)
1693 return 0;
1694
1695 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001696 drm_property_create_enum(dev, 0, "scaling mode",
1697 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001698 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001699
1700 dev->mode_config.scaling_mode_property = scaling_mode;
1701
1702 return 0;
1703}
1704EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1705
1706/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301707 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1708 * @dev: DRM device
1709 *
1710 * Called by a driver the first time it's needed, must be attached to desired
1711 * connectors.
1712 *
1713 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001714 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301715 */
1716int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1717{
1718 if (dev->mode_config.aspect_ratio_property)
1719 return 0;
1720
1721 dev->mode_config.aspect_ratio_property =
1722 drm_property_create_enum(dev, 0, "aspect ratio",
1723 drm_aspect_ratio_enum_list,
1724 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1725
1726 if (dev->mode_config.aspect_ratio_property == NULL)
1727 return -ENOMEM;
1728
1729 return 0;
1730}
1731EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1732
1733/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001734 * drm_mode_create_dirty_property - create dirty property
1735 * @dev: DRM device
1736 *
1737 * Called by a driver the first time it's needed, must be attached to desired
1738 * connectors.
1739 */
1740int drm_mode_create_dirty_info_property(struct drm_device *dev)
1741{
1742 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001743
1744 if (dev->mode_config.dirty_info_property)
1745 return 0;
1746
1747 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001748 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001749 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001750 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001751 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001752 dev->mode_config.dirty_info_property = dirty_info;
1753
1754 return 0;
1755}
1756EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1757
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001758/**
1759 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1760 * @dev: DRM device
1761 *
1762 * Create the the suggested x/y offset property for connectors.
1763 */
1764int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1765{
1766 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1767 return 0;
1768
1769 dev->mode_config.suggested_x_property =
1770 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1771
1772 dev->mode_config.suggested_y_property =
1773 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1774
1775 if (dev->mode_config.suggested_x_property == NULL ||
1776 dev->mode_config.suggested_y_property == NULL)
1777 return -ENOMEM;
1778 return 0;
1779}
1780EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1781
Dave Airlief453ba02008-11-07 14:05:41 -08001782/**
Dave Airlief453ba02008-11-07 14:05:41 -08001783 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001784 * @dev: drm device for the ioctl
1785 * @data: data pointer for the ioctl
1786 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001787 *
Dave Airlief453ba02008-11-07 14:05:41 -08001788 * Construct a set of configuration description structures and return
1789 * them to the user, including CRTC, connector and framebuffer configuration.
1790 *
1791 * Called by the user via ioctl.
1792 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001793 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001794 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001795 */
1796int drm_mode_getresources(struct drm_device *dev, void *data,
1797 struct drm_file *file_priv)
1798{
1799 struct drm_mode_card_res *card_res = data;
1800 struct list_head *lh;
1801 struct drm_framebuffer *fb;
1802 struct drm_connector *connector;
1803 struct drm_crtc *crtc;
1804 struct drm_encoder *encoder;
1805 int ret = 0;
1806 int connector_count = 0;
1807 int crtc_count = 0;
1808 int fb_count = 0;
1809 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001810 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001811 uint32_t __user *fb_id;
1812 uint32_t __user *crtc_id;
1813 uint32_t __user *connector_id;
1814 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001815
Dave Airliefb3b06c2011-02-08 13:55:21 +10001816 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1817 return -EINVAL;
1818
Dave Airlief453ba02008-11-07 14:05:41 -08001819
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001820 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001821 /*
1822 * For the non-control nodes we need to limit the list of resources
1823 * by IDs in the group list for this node
1824 */
1825 list_for_each(lh, &file_priv->fbs)
1826 fb_count++;
1827
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001828 /* handle this in 4 parts */
1829 /* FBs */
1830 if (card_res->count_fbs >= fb_count) {
1831 copied = 0;
1832 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1833 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1834 if (put_user(fb->base.id, fb_id + copied)) {
1835 mutex_unlock(&file_priv->fbs_lock);
1836 return -EFAULT;
1837 }
1838 copied++;
1839 }
1840 }
1841 card_res->count_fbs = fb_count;
1842 mutex_unlock(&file_priv->fbs_lock);
1843
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001844 /* mode_config.mutex protects the connector list against e.g. DP MST
1845 * connector hot-adding. CRTC/Plane lists are invariant. */
1846 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001847 drm_for_each_crtc(crtc, dev)
1848 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001849
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001850 drm_for_each_connector(connector, dev)
1851 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001852
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001853 drm_for_each_encoder(encoder, dev)
1854 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001855
1856 card_res->max_height = dev->mode_config.max_height;
1857 card_res->min_height = dev->mode_config.min_height;
1858 card_res->max_width = dev->mode_config.max_width;
1859 card_res->min_width = dev->mode_config.min_width;
1860
Dave Airlief453ba02008-11-07 14:05:41 -08001861 /* CRTCs */
1862 if (card_res->count_crtcs >= crtc_count) {
1863 copied = 0;
1864 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001865 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001866 if (put_user(crtc->base.id, crtc_id + copied)) {
1867 ret = -EFAULT;
1868 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001869 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001870 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001871 }
1872 }
1873 card_res->count_crtcs = crtc_count;
1874
1875 /* Encoders */
1876 if (card_res->count_encoders >= encoder_count) {
1877 copied = 0;
1878 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001879 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001880 if (put_user(encoder->base.id, encoder_id +
1881 copied)) {
1882 ret = -EFAULT;
1883 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001884 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001885 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001886 }
1887 }
1888 card_res->count_encoders = encoder_count;
1889
1890 /* Connectors */
1891 if (card_res->count_connectors >= connector_count) {
1892 copied = 0;
1893 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001894 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001895 if (put_user(connector->base.id,
1896 connector_id + copied)) {
1897 ret = -EFAULT;
1898 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001899 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001900 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001901 }
1902 }
1903 card_res->count_connectors = connector_count;
1904
Dave Airlief453ba02008-11-07 14:05:41 -08001905out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001906 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001907 return ret;
1908}
1909
1910/**
1911 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001912 * @dev: drm device for the ioctl
1913 * @data: data pointer for the ioctl
1914 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001915 *
Dave Airlief453ba02008-11-07 14:05:41 -08001916 * Construct a CRTC configuration structure to return to the user.
1917 *
1918 * Called by the user via ioctl.
1919 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001920 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001921 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001922 */
1923int drm_mode_getcrtc(struct drm_device *dev,
1924 void *data, struct drm_file *file_priv)
1925{
1926 struct drm_mode_crtc *crtc_resp = data;
1927 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001928
Dave Airliefb3b06c2011-02-08 13:55:21 +10001929 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1930 return -EINVAL;
1931
Rob Clarka2b34e22013-10-05 16:36:52 -04001932 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001933 if (!crtc)
1934 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001935
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001936 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08001937 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001938 if (crtc->primary->fb)
1939 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001940 else
1941 crtc_resp->fb_id = 0;
1942
Daniel Vetter31c946e2015-02-22 12:24:17 +01001943 if (crtc->state) {
1944 crtc_resp->x = crtc->primary->state->src_x >> 16;
1945 crtc_resp->y = crtc->primary->state->src_y >> 16;
1946 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001947 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001948 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08001949
Daniel Vetter31c946e2015-02-22 12:24:17 +01001950 } else {
1951 crtc_resp->mode_valid = 0;
1952 }
Dave Airlief453ba02008-11-07 14:05:41 -08001953 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01001954 crtc_resp->x = crtc->x;
1955 crtc_resp->y = crtc->y;
1956 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001957 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001958 crtc_resp->mode_valid = 1;
1959
1960 } else {
1961 crtc_resp->mode_valid = 0;
1962 }
Dave Airlief453ba02008-11-07 14:05:41 -08001963 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001964 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08001965
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001966 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001967}
1968
Damien Lespiau61d8e322013-09-25 16:45:22 +01001969static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1970 const struct drm_file *file_priv)
1971{
1972 /*
1973 * If user-space hasn't configured the driver to expose the stereo 3D
1974 * modes, don't expose them.
1975 */
1976 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1977 return false;
1978
1979 return true;
1980}
1981
Daniel Vetterabd69c52014-11-25 23:50:05 +01001982static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1983{
1984 /* For atomic drivers only state objects are synchronously updated and
1985 * protected by modeset locks, so check those first. */
1986 if (connector->state)
1987 return connector->state->best_encoder;
1988 return connector->encoder;
1989}
1990
Rob Clark95cbf112014-12-18 16:01:49 -05001991/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05001992static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05001993 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
1994 uint32_t *arg_count_props)
1995{
Rob Clark88a48e22014-12-18 16:01:50 -05001996 int props_count;
1997 int i, ret, copied;
1998
1999 props_count = obj->properties->count;
2000 if (!atomic)
2001 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002002
2003 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002004 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002005 struct drm_property *prop = obj->properties->properties[i];
2006 uint64_t val;
2007
Rob Clark88a48e22014-12-18 16:01:50 -05002008 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2009 continue;
2010
Rob Clark95cbf112014-12-18 16:01:49 -05002011 ret = drm_object_property_get_value(obj, prop, &val);
2012 if (ret)
2013 return ret;
2014
2015 if (put_user(prop->base.id, prop_ptr + copied))
2016 return -EFAULT;
2017
2018 if (put_user(val, prop_values + copied))
2019 return -EFAULT;
2020
2021 copied++;
2022 }
2023 }
2024 *arg_count_props = props_count;
2025
2026 return 0;
2027}
2028
Dave Airlief453ba02008-11-07 14:05:41 -08002029/**
2030 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002031 * @dev: drm device for the ioctl
2032 * @data: data pointer for the ioctl
2033 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002034 *
Dave Airlief453ba02008-11-07 14:05:41 -08002035 * Construct a connector configuration structure to return to the user.
2036 *
2037 * Called by the user via ioctl.
2038 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002039 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002040 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002041 */
2042int drm_mode_getconnector(struct drm_device *dev, void *data,
2043 struct drm_file *file_priv)
2044{
2045 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002046 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002047 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002048 struct drm_display_mode *mode;
2049 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002050 int encoders_count = 0;
2051 int ret = 0;
2052 int copied = 0;
2053 int i;
2054 struct drm_mode_modeinfo u_mode;
2055 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002056 uint32_t __user *encoder_ptr;
2057
Dave Airliefb3b06c2011-02-08 13:55:21 +10002058 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2059 return -EINVAL;
2060
Dave Airlief453ba02008-11-07 14:05:41 -08002061 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2062
Daniel Vetter7b240562012-12-12 00:35:33 +01002063 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002064
Dave Airlieb164d312016-04-27 11:10:09 +10002065 connector = drm_connector_lookup(dev, out_resp->connector_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002066 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002067 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002068 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002069 }
Dave Airlief453ba02008-11-07 14:05:41 -08002070
Thierry Reding01073b02014-12-10 13:03:38 +01002071 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2072 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002073 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002074
2075 if (out_resp->count_modes == 0) {
2076 connector->funcs->fill_modes(connector,
2077 dev->mode_config.max_width,
2078 dev->mode_config.max_height);
2079 }
2080
2081 /* delayed so we get modes regardless of pre-fill_modes state */
2082 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002083 if (drm_mode_expose_to_userspace(mode, file_priv))
2084 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002085
2086 out_resp->connector_id = connector->base.id;
2087 out_resp->connector_type = connector->connector_type;
2088 out_resp->connector_type_id = connector->connector_type_id;
2089 out_resp->mm_width = connector->display_info.width_mm;
2090 out_resp->mm_height = connector->display_info.height_mm;
2091 out_resp->subpixel = connector->display_info.subpixel_order;
2092 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002093
2094 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002095 encoder = drm_connector_get_encoder(connector);
2096 if (encoder)
2097 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002098 else
2099 out_resp->encoder_id = 0;
2100
2101 /*
2102 * This ioctl is called twice, once to determine how much space is
2103 * needed, and the 2nd time to fill it.
2104 */
2105 if ((out_resp->count_modes >= mode_count) && mode_count) {
2106 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002107 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002108 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002109 if (!drm_mode_expose_to_userspace(mode, file_priv))
2110 continue;
2111
Daniel Stone934a8a82015-05-22 13:34:48 +01002112 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002113 if (copy_to_user(mode_ptr + copied,
2114 &u_mode, sizeof(u_mode))) {
2115 ret = -EFAULT;
2116 goto out;
2117 }
2118 copied++;
2119 }
2120 }
2121 out_resp->count_modes = mode_count;
2122
Rob Clark88a48e22014-12-18 16:01:50 -05002123 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002124 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2125 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2126 &out_resp->count_props);
2127 if (ret)
2128 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002129
2130 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2131 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002132 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002133 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2134 if (connector->encoder_ids[i] != 0) {
2135 if (put_user(connector->encoder_ids[i],
2136 encoder_ptr + copied)) {
2137 ret = -EFAULT;
2138 goto out;
2139 }
2140 copied++;
2141 }
2142 }
2143 }
2144 out_resp->count_encoders = encoders_count;
2145
2146out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002147 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002148
Dave Airlieb164d312016-04-27 11:10:09 +10002149 drm_connector_unreference(connector);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002150out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002151 mutex_unlock(&dev->mode_config.mutex);
2152
Dave Airlief453ba02008-11-07 14:05:41 -08002153 return ret;
2154}
2155
Daniel Vetterabd69c52014-11-25 23:50:05 +01002156static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2157{
2158 struct drm_connector *connector;
2159 struct drm_device *dev = encoder->dev;
2160 bool uses_atomic = false;
2161
2162 /* For atomic drivers only state objects are synchronously updated and
2163 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002164 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002165 if (!connector->state)
2166 continue;
2167
2168 uses_atomic = true;
2169
2170 if (connector->state->best_encoder != encoder)
2171 continue;
2172
2173 return connector->state->crtc;
2174 }
2175
2176 /* Don't return stale data (e.g. pending async disable). */
2177 if (uses_atomic)
2178 return NULL;
2179
2180 return encoder->crtc;
2181}
2182
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002183/**
2184 * drm_mode_getencoder - get encoder configuration
2185 * @dev: drm device for the ioctl
2186 * @data: data pointer for the ioctl
2187 * @file_priv: drm file for the ioctl call
2188 *
2189 * Construct a encoder configuration structure to return to the user.
2190 *
2191 * Called by the user via ioctl.
2192 *
2193 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002194 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002195 */
Dave Airlief453ba02008-11-07 14:05:41 -08002196int drm_mode_getencoder(struct drm_device *dev, void *data,
2197 struct drm_file *file_priv)
2198{
2199 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002200 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002201 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002202
Dave Airliefb3b06c2011-02-08 13:55:21 +10002203 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2204 return -EINVAL;
2205
Rob Clarka2b34e22013-10-05 16:36:52 -04002206 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002207 if (!encoder)
2208 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002209
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002210 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002211 crtc = drm_encoder_get_crtc(encoder);
2212 if (crtc)
2213 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002214 else
2215 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002216 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2217
Dave Airlief453ba02008-11-07 14:05:41 -08002218 enc_resp->encoder_type = encoder->encoder_type;
2219 enc_resp->encoder_id = encoder->base.id;
2220 enc_resp->possible_crtcs = encoder->possible_crtcs;
2221 enc_resp->possible_clones = encoder->possible_clones;
2222
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002223 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002224}
2225
2226/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002227 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002228 * @dev: DRM device
2229 * @data: ioctl data
2230 * @file_priv: DRM file info
2231 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002232 * Construct a list of plane ids to return to the user.
2233 *
2234 * Called by the user via ioctl.
2235 *
2236 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002237 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002238 */
2239int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002240 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002241{
2242 struct drm_mode_get_plane_res *plane_resp = data;
2243 struct drm_mode_config *config;
2244 struct drm_plane *plane;
2245 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002246 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002247 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002248
2249 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2250 return -EINVAL;
2251
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002252 config = &dev->mode_config;
2253
Matt Roper681e7ec2014-04-01 15:22:42 -07002254 if (file_priv->universal_planes)
2255 num_planes = config->num_total_plane;
2256 else
2257 num_planes = config->num_overlay_plane;
2258
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002259 /*
2260 * This ioctl is called twice, once to determine how much space is
2261 * needed, and the 2nd time to fill it.
2262 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002263 if (num_planes &&
2264 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002265 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002266
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002267 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002268 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002269 /*
2270 * Unless userspace set the 'universal planes'
2271 * capability bit, only advertise overlays.
2272 */
2273 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2274 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002275 continue;
2276
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002277 if (put_user(plane->base.id, plane_ptr + copied))
2278 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002279 copied++;
2280 }
2281 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002282 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002283
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002284 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002285}
2286
2287/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002288 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002289 * @dev: DRM device
2290 * @data: ioctl data
2291 * @file_priv: DRM file info
2292 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002293 * Construct a plane configuration structure to return to the user.
2294 *
2295 * Called by the user via ioctl.
2296 *
2297 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002298 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002299 */
2300int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002301 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002302{
2303 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002304 struct drm_plane *plane;
2305 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002306
2307 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2308 return -EINVAL;
2309
Rob Clarka2b34e22013-10-05 16:36:52 -04002310 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002311 if (!plane)
2312 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002313
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002314 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002315 if (plane->crtc)
2316 plane_resp->crtc_id = plane->crtc->base.id;
2317 else
2318 plane_resp->crtc_id = 0;
2319
2320 if (plane->fb)
2321 plane_resp->fb_id = plane->fb->base.id;
2322 else
2323 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002324 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002325
2326 plane_resp->plane_id = plane->base.id;
2327 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002328 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002329
2330 /*
2331 * This ioctl is called twice, once to determine how much space is
2332 * needed, and the 2nd time to fill it.
2333 */
2334 if (plane->format_count &&
2335 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002336 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002337 if (copy_to_user(format_ptr,
2338 plane->format_types,
2339 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002340 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002341 }
2342 }
2343 plane_resp->count_format_types = plane->format_count;
2344
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002345 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002346}
2347
Laurent Pinchartead86102015-03-05 02:25:43 +02002348/**
2349 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2350 * @plane: plane to check for format support
2351 * @format: the pixel format
2352 *
2353 * Returns:
2354 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2355 * otherwise.
2356 */
2357int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2358{
2359 unsigned int i;
2360
2361 for (i = 0; i < plane->format_count; i++) {
2362 if (format == plane->format_types[i])
2363 return 0;
2364 }
2365
2366 return -EINVAL;
2367}
2368
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002369static int check_src_coords(uint32_t src_x, uint32_t src_y,
2370 uint32_t src_w, uint32_t src_h,
2371 const struct drm_framebuffer *fb)
2372{
2373 unsigned int fb_width, fb_height;
2374
2375 fb_width = fb->width << 16;
2376 fb_height = fb->height << 16;
2377
2378 /* Make sure source coordinates are inside the fb. */
2379 if (src_w > fb_width ||
2380 src_x > fb_width - src_w ||
2381 src_h > fb_height ||
2382 src_y > fb_height - src_h) {
2383 DRM_DEBUG_KMS("Invalid source coordinates "
2384 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2385 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2386 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2387 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2388 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2389 return -ENOSPC;
2390 }
2391
2392 return 0;
2393}
2394
Matt Roperb36552b2014-06-10 08:28:09 -07002395/*
2396 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002397 *
Matt Roperb36552b2014-06-10 08:28:09 -07002398 * Note that we assume an extra reference has already been taken on fb. If the
2399 * update fails, this reference will be dropped before return; if it succeeds,
2400 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002401 *
Matt Roperb36552b2014-06-10 08:28:09 -07002402 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002403 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002404static int __setplane_internal(struct drm_plane *plane,
2405 struct drm_crtc *crtc,
2406 struct drm_framebuffer *fb,
2407 int32_t crtc_x, int32_t crtc_y,
2408 uint32_t crtc_w, uint32_t crtc_h,
2409 /* src_{x,y,w,h} values are 16.16 fixed point */
2410 uint32_t src_x, uint32_t src_y,
2411 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002412{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002413 int ret = 0;
2414
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002415 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002416 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002417 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002418 ret = plane->funcs->disable_plane(plane);
2419 if (!ret) {
2420 plane->crtc = NULL;
2421 plane->fb = NULL;
2422 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002423 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002424 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002425 goto out;
2426 }
2427
Matt Roper7f994f32014-05-29 08:06:51 -07002428 /* Check whether this plane is usable on this CRTC */
2429 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2430 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2431 ret = -EINVAL;
2432 goto out;
2433 }
2434
Ville Syrjälä62443be2011-12-20 00:06:47 +02002435 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002436 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2437 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002438 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2439 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002440 goto out;
2441 }
2442
Matt Roper3968be92015-04-13 11:06:13 -07002443 /* Give drivers some help against integer overflows */
2444 if (crtc_w > INT_MAX ||
2445 crtc_x > INT_MAX - (int32_t) crtc_w ||
2446 crtc_h > INT_MAX ||
2447 crtc_y > INT_MAX - (int32_t) crtc_h) {
2448 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2449 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002450 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002451 goto out;
2452 }
2453
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002454 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2455 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002456 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002457
Daniel Vetter3d30a592014-07-27 13:42:42 +02002458 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002459 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002460 crtc_x, crtc_y, crtc_w, crtc_h,
2461 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002462 if (!ret) {
2463 plane->crtc = crtc;
2464 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002465 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002466 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002467 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002468 }
2469
2470out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002471 if (fb)
2472 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002473 if (plane->old_fb)
2474 drm_framebuffer_unreference(plane->old_fb);
2475 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002476
2477 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002478}
Matt Roperb36552b2014-06-10 08:28:09 -07002479
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002480static int setplane_internal(struct drm_plane *plane,
2481 struct drm_crtc *crtc,
2482 struct drm_framebuffer *fb,
2483 int32_t crtc_x, int32_t crtc_y,
2484 uint32_t crtc_w, uint32_t crtc_h,
2485 /* src_{x,y,w,h} values are 16.16 fixed point */
2486 uint32_t src_x, uint32_t src_y,
2487 uint32_t src_w, uint32_t src_h)
2488{
2489 int ret;
2490
2491 drm_modeset_lock_all(plane->dev);
2492 ret = __setplane_internal(plane, crtc, fb,
2493 crtc_x, crtc_y, crtc_w, crtc_h,
2494 src_x, src_y, src_w, src_h);
2495 drm_modeset_unlock_all(plane->dev);
2496
2497 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002498}
2499
2500/**
2501 * drm_mode_setplane - configure a plane's configuration
2502 * @dev: DRM device
2503 * @data: ioctl data*
2504 * @file_priv: DRM file info
2505 *
2506 * Set plane configuration, including placement, fb, scaling, and other factors.
2507 * Or pass a NULL fb to disable (planes may be disabled without providing a
2508 * valid crtc).
2509 *
2510 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002511 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002512 */
2513int drm_mode_setplane(struct drm_device *dev, void *data,
2514 struct drm_file *file_priv)
2515{
2516 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002517 struct drm_plane *plane;
2518 struct drm_crtc *crtc = NULL;
2519 struct drm_framebuffer *fb = NULL;
2520
2521 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2522 return -EINVAL;
2523
Matt Roperb36552b2014-06-10 08:28:09 -07002524 /*
2525 * First, find the plane, crtc, and fb objects. If not available,
2526 * we don't bother to call the driver.
2527 */
Rob Clark933f6222014-11-25 20:33:11 -05002528 plane = drm_plane_find(dev, plane_req->plane_id);
2529 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002530 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2531 plane_req->plane_id);
2532 return -ENOENT;
2533 }
Matt Roperb36552b2014-06-10 08:28:09 -07002534
2535 if (plane_req->fb_id) {
2536 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2537 if (!fb) {
2538 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2539 plane_req->fb_id);
2540 return -ENOENT;
2541 }
2542
Rob Clark933f6222014-11-25 20:33:11 -05002543 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2544 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002545 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2546 plane_req->crtc_id);
2547 return -ENOENT;
2548 }
Matt Roperb36552b2014-06-10 08:28:09 -07002549 }
2550
Matt Roper161d0dc2014-06-10 08:28:10 -07002551 /*
2552 * setplane_internal will take care of deref'ing either the old or new
2553 * framebuffer depending on success.
2554 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002555 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002556 plane_req->crtc_x, plane_req->crtc_y,
2557 plane_req->crtc_w, plane_req->crtc_h,
2558 plane_req->src_x, plane_req->src_y,
2559 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002560}
2561
2562/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002563 * drm_mode_set_config_internal - helper to call ->set_config
2564 * @set: modeset config to set
2565 *
2566 * This is a little helper to wrap internal calls to the ->set_config driver
2567 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002568 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002569 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002570 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002571 */
2572int drm_mode_set_config_internal(struct drm_mode_set *set)
2573{
2574 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002575 struct drm_framebuffer *fb;
2576 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002577 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002578
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002579 /*
2580 * NOTE: ->set_config can also disable other crtcs (if we steal all
2581 * connectors from it), hence we need to refcount the fbs across all
2582 * crtcs. Atomic modeset will have saner semantics ...
2583 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002584 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002585 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002586
Daniel Vetterb0d12322012-12-11 01:07:12 +01002587 fb = set->fb;
2588
2589 ret = crtc->funcs->set_config(set);
2590 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002591 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002592 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002593 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002594
Daniel Vettere4f62542015-07-09 23:44:35 +02002595 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002596 if (tmp->primary->fb)
2597 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002598 if (tmp->primary->old_fb)
2599 drm_framebuffer_unreference(tmp->primary->old_fb);
2600 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002601 }
2602
2603 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002604}
2605EXPORT_SYMBOL(drm_mode_set_config_internal);
2606
Matt Roperaf936292014-04-01 15:22:34 -07002607/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002608 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2609 * @mode: mode to query
2610 * @hdisplay: hdisplay value to fill in
2611 * @vdisplay: vdisplay value to fill in
2612 *
2613 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2614 * the appropriate layout.
2615 */
2616void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2617 int *hdisplay, int *vdisplay)
2618{
2619 struct drm_display_mode adjusted;
2620
2621 drm_mode_copy(&adjusted, mode);
2622 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2623 *hdisplay = adjusted.crtc_hdisplay;
2624 *vdisplay = adjusted.crtc_vdisplay;
2625}
2626EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2627
2628/**
Matt Roperaf936292014-04-01 15:22:34 -07002629 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2630 * CRTC viewport
2631 * @crtc: CRTC that framebuffer will be displayed on
2632 * @x: x panning
2633 * @y: y panning
2634 * @mode: mode that framebuffer will be displayed under
2635 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002636 */
Matt Roperaf936292014-04-01 15:22:34 -07002637int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2638 int x, int y,
2639 const struct drm_display_mode *mode,
2640 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002641
2642{
2643 int hdisplay, vdisplay;
2644
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002645 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002646
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002647 if (crtc->state &&
2648 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2649 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002650 swap(hdisplay, vdisplay);
2651
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002652 return check_src_coords(x << 16, y << 16,
2653 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002654}
Matt Roperaf936292014-04-01 15:22:34 -07002655EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002656
Daniel Vetter2d13b672012-12-11 13:47:23 +01002657/**
Dave Airlief453ba02008-11-07 14:05:41 -08002658 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002659 * @dev: drm device for the ioctl
2660 * @data: data pointer for the ioctl
2661 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002662 *
Dave Airlief453ba02008-11-07 14:05:41 -08002663 * Build a new CRTC configuration based on user request.
2664 *
2665 * Called by the user via ioctl.
2666 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002667 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002668 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002669 */
2670int drm_mode_setcrtc(struct drm_device *dev, void *data,
2671 struct drm_file *file_priv)
2672{
2673 struct drm_mode_config *config = &dev->mode_config;
2674 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002675 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002676 struct drm_connector **connector_set = NULL, *connector;
2677 struct drm_framebuffer *fb = NULL;
2678 struct drm_display_mode *mode = NULL;
2679 struct drm_mode_set set;
2680 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002681 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002682 int i;
2683
Dave Airliefb3b06c2011-02-08 13:55:21 +10002684 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2685 return -EINVAL;
2686
Zhao Junwang01447e92015-07-07 17:08:35 +08002687 /*
2688 * Universal plane src offsets are only 16.16, prevent havoc for
2689 * drivers using universal plane code internally.
2690 */
2691 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002692 return -ERANGE;
2693
Daniel Vetter84849902012-12-02 00:28:11 +01002694 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002695 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2696 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002697 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002698 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002699 goto out;
2700 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002701 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002702
2703 if (crtc_req->mode_valid) {
2704 /* If we have a mode we need a framebuffer. */
2705 /* If we pass -1, set the mode with the currently bound fb */
2706 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002707 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002708 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2709 ret = -EINVAL;
2710 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002711 }
Matt Roperf4510a22014-04-01 15:22:40 -07002712 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002713 /* Make refcounting symmetric with the lookup path. */
2714 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002715 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002716 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2717 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002718 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2719 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002720 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002721 goto out;
2722 }
Dave Airlief453ba02008-11-07 14:05:41 -08002723 }
2724
2725 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002726 if (!mode) {
2727 ret = -ENOMEM;
2728 goto out;
2729 }
2730
Daniel Stone934a8a82015-05-22 13:34:48 +01002731 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002732 if (ret) {
2733 DRM_DEBUG_KMS("Invalid mode\n");
2734 goto out;
2735 }
2736
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002737 /*
2738 * Check whether the primary plane supports the fb pixel format.
2739 * Drivers not implementing the universal planes API use a
2740 * default formats list provided by the DRM core which doesn't
2741 * match real hardware capabilities. Skip the check in that
2742 * case.
2743 */
2744 if (!crtc->primary->format_default) {
2745 ret = drm_plane_check_pixel_format(crtc->primary,
2746 fb->pixel_format);
2747 if (ret) {
2748 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2749 drm_get_format_name(fb->pixel_format));
2750 goto out;
2751 }
2752 }
2753
Damien Lespiauc11e9282013-09-25 16:45:30 +01002754 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2755 mode, fb);
2756 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002757 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002758
Dave Airlief453ba02008-11-07 14:05:41 -08002759 }
2760
2761 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002762 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002763 ret = -EINVAL;
2764 goto out;
2765 }
2766
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002767 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002768 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002769 crtc_req->count_connectors);
2770 ret = -EINVAL;
2771 goto out;
2772 }
2773
2774 if (crtc_req->count_connectors > 0) {
2775 u32 out_id;
2776
2777 /* Avoid unbounded kernel memory allocation */
2778 if (crtc_req->count_connectors > config->num_connector) {
2779 ret = -EINVAL;
2780 goto out;
2781 }
2782
Thierry Reding2f6c5382014-12-10 13:03:36 +01002783 connector_set = kmalloc_array(crtc_req->count_connectors,
2784 sizeof(struct drm_connector *),
2785 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002786 if (!connector_set) {
2787 ret = -ENOMEM;
2788 goto out;
2789 }
2790
2791 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +10002792 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002793 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002794 if (get_user(out_id, &set_connectors_ptr[i])) {
2795 ret = -EFAULT;
2796 goto out;
2797 }
2798
Dave Airlieb164d312016-04-27 11:10:09 +10002799 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002800 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002801 DRM_DEBUG_KMS("Connector id %d unknown\n",
2802 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002803 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002804 goto out;
2805 }
Jerome Glisse94401062010-07-15 15:43:25 -04002806 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2807 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002808 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002809
2810 connector_set[i] = connector;
2811 }
2812 }
2813
2814 set.crtc = crtc;
2815 set.x = crtc_req->x;
2816 set.y = crtc_req->y;
2817 set.mode = mode;
2818 set.connectors = connector_set;
2819 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002820 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002821 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002822
2823out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002824 if (fb)
2825 drm_framebuffer_unreference(fb);
2826
Dave Airlieb164d312016-04-27 11:10:09 +10002827 if (connector_set) {
2828 for (i = 0; i < crtc_req->count_connectors; i++) {
2829 if (connector_set[i])
2830 drm_connector_unreference(connector_set[i]);
2831 }
2832 }
Dave Airlief453ba02008-11-07 14:05:41 -08002833 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002834 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002835 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002836 return ret;
2837}
2838
Matt Roper161d0dc2014-06-10 08:28:10 -07002839/**
2840 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2841 * universal plane handler call
2842 * @crtc: crtc to update cursor for
2843 * @req: data pointer for the ioctl
2844 * @file_priv: drm file for the ioctl call
2845 *
2846 * Legacy cursor ioctl's work directly with driver buffer handles. To
2847 * translate legacy ioctl calls into universal plane handler calls, we need to
2848 * wrap the native buffer handle in a drm_framebuffer.
2849 *
2850 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2851 * buffer with a pitch of 4*width; the universal plane interface should be used
2852 * directly in cases where the hardware can support other buffer settings and
2853 * userspace wants to make use of these capabilities.
2854 *
2855 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002856 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002857 */
2858static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2859 struct drm_mode_cursor2 *req,
2860 struct drm_file *file_priv)
2861{
2862 struct drm_device *dev = crtc->dev;
2863 struct drm_framebuffer *fb = NULL;
2864 struct drm_mode_fb_cmd2 fbreq = {
2865 .width = req->width,
2866 .height = req->height,
2867 .pixel_format = DRM_FORMAT_ARGB8888,
2868 .pitches = { req->width * 4 },
2869 .handles = { req->handle },
2870 };
2871 int32_t crtc_x, crtc_y;
2872 uint32_t crtc_w = 0, crtc_h = 0;
2873 uint32_t src_w = 0, src_h = 0;
2874 int ret = 0;
2875
2876 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002877 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002878
2879 /*
2880 * Obtain fb we'll be using (either new or existing) and take an extra
2881 * reference to it if fb != null. setplane will take care of dropping
2882 * the reference if the plane update fails.
2883 */
2884 if (req->flags & DRM_MODE_CURSOR_BO) {
2885 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002886 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002887 if (IS_ERR(fb)) {
2888 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2889 return PTR_ERR(fb);
2890 }
Gerd Hoffmanndd546592016-05-31 08:54:52 +02002891 fb->hot_x = req->hot_x;
2892 fb->hot_y = req->hot_y;
Matt Roper161d0dc2014-06-10 08:28:10 -07002893 } else {
2894 fb = NULL;
2895 }
2896 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002897 fb = crtc->cursor->fb;
2898 if (fb)
2899 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002900 }
2901
2902 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2903 crtc_x = req->x;
2904 crtc_y = req->y;
2905 } else {
2906 crtc_x = crtc->cursor_x;
2907 crtc_y = crtc->cursor_y;
2908 }
2909
2910 if (fb) {
2911 crtc_w = fb->width;
2912 crtc_h = fb->height;
2913 src_w = fb->width << 16;
2914 src_h = fb->height << 16;
2915 }
2916
2917 /*
2918 * setplane_internal will take care of deref'ing either the old or new
2919 * framebuffer depending on success.
2920 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002921 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002922 crtc_x, crtc_y, crtc_w, crtc_h,
2923 0, 0, src_w, src_h);
2924
2925 /* Update successful; save new cursor position, if necessary */
2926 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2927 crtc->cursor_x = req->x;
2928 crtc->cursor_y = req->y;
2929 }
2930
2931 return ret;
2932}
2933
Dave Airlie4c813d42013-06-20 11:48:52 +10002934static int drm_mode_cursor_common(struct drm_device *dev,
2935 struct drm_mode_cursor2 *req,
2936 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002937{
Dave Airlief453ba02008-11-07 14:05:41 -08002938 struct drm_crtc *crtc;
2939 int ret = 0;
2940
Dave Airliefb3b06c2011-02-08 13:55:21 +10002941 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2942 return -EINVAL;
2943
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002944 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002945 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002946
Rob Clarka2b34e22013-10-05 16:36:52 -04002947 crtc = drm_crtc_find(dev, req->crtc_id);
2948 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002949 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002950 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002951 }
Dave Airlief453ba02008-11-07 14:05:41 -08002952
Matt Roper161d0dc2014-06-10 08:28:10 -07002953 /*
2954 * If this crtc has a universal cursor plane, call that plane's update
2955 * handler rather than using legacy cursor handlers.
2956 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002957 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002958 if (crtc->cursor) {
2959 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2960 goto out;
2961 }
2962
Dave Airlief453ba02008-11-07 14:05:41 -08002963 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002964 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002965 ret = -ENXIO;
2966 goto out;
2967 }
2968 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002969 if (crtc->funcs->cursor_set2)
2970 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2971 req->width, req->height, req->hot_x, req->hot_y);
2972 else
2973 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2974 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002975 }
2976
2977 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2978 if (crtc->funcs->cursor_move) {
2979 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2980 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002981 ret = -EFAULT;
2982 goto out;
2983 }
2984 }
2985out:
Daniel Vetterd059f652014-07-25 18:07:40 +02002986 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01002987
Dave Airlief453ba02008-11-07 14:05:41 -08002988 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002989
2990}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002991
2992
2993/**
2994 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2995 * @dev: drm device for the ioctl
2996 * @data: data pointer for the ioctl
2997 * @file_priv: drm file for the ioctl call
2998 *
2999 * Set the cursor configuration based on user request.
3000 *
3001 * Called by the user via ioctl.
3002 *
3003 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003004 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003005 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003006int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003007 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003008{
3009 struct drm_mode_cursor *req = data;
3010 struct drm_mode_cursor2 new_req;
3011
3012 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3013 new_req.hot_x = new_req.hot_y = 0;
3014
3015 return drm_mode_cursor_common(dev, &new_req, file_priv);
3016}
3017
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003018/**
3019 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3020 * @dev: drm device for the ioctl
3021 * @data: data pointer for the ioctl
3022 * @file_priv: drm file for the ioctl call
3023 *
3024 * Set the cursor configuration based on user request. This implements the 2nd
3025 * version of the cursor ioctl, which allows userspace to additionally specify
3026 * the hotspot of the pointer.
3027 *
3028 * Called by the user via ioctl.
3029 *
3030 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003031 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003032 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003033int drm_mode_cursor2_ioctl(struct drm_device *dev,
3034 void *data, struct drm_file *file_priv)
3035{
3036 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003037
Dave Airlie4c813d42013-06-20 11:48:52 +10003038 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003039}
3040
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003041/**
3042 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3043 * @bpp: bits per pixels
3044 * @depth: bit depth per pixel
3045 *
3046 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3047 * Useful in fbdev emulation code, since that deals in those values.
3048 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003049uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3050{
3051 uint32_t fmt;
3052
3053 switch (bpp) {
3054 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003055 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003056 break;
3057 case 16:
3058 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003059 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003060 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003061 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003062 break;
3063 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003064 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003065 break;
3066 case 32:
3067 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003068 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003069 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003070 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003071 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003072 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003073 break;
3074 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003075 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3076 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003077 break;
3078 }
3079
3080 return fmt;
3081}
3082EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3083
Dave Airlief453ba02008-11-07 14:05:41 -08003084/**
3085 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003086 * @dev: drm device for the ioctl
3087 * @data: data pointer for the ioctl
3088 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003089 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003090 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003091 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003092 *
3093 * Called by the user via ioctl.
3094 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003095 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003096 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003097 */
3098int drm_mode_addfb(struct drm_device *dev,
3099 void *data, struct drm_file *file_priv)
3100{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003101 struct drm_mode_fb_cmd *or = data;
3102 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003103 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003104
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003105 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003106 r.fb_id = or->fb_id;
3107 r.width = or->width;
3108 r.height = or->height;
3109 r.pitches[0] = or->pitch;
3110 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3111 r.handles[0] = or->handle;
3112
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003113 ret = drm_mode_addfb2(dev, &r, file_priv);
3114 if (ret)
3115 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003116
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003117 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003118
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003119 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003120}
3121
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003122static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003123{
3124 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3125
3126 switch (format) {
3127 case DRM_FORMAT_C8:
3128 case DRM_FORMAT_RGB332:
3129 case DRM_FORMAT_BGR233:
3130 case DRM_FORMAT_XRGB4444:
3131 case DRM_FORMAT_XBGR4444:
3132 case DRM_FORMAT_RGBX4444:
3133 case DRM_FORMAT_BGRX4444:
3134 case DRM_FORMAT_ARGB4444:
3135 case DRM_FORMAT_ABGR4444:
3136 case DRM_FORMAT_RGBA4444:
3137 case DRM_FORMAT_BGRA4444:
3138 case DRM_FORMAT_XRGB1555:
3139 case DRM_FORMAT_XBGR1555:
3140 case DRM_FORMAT_RGBX5551:
3141 case DRM_FORMAT_BGRX5551:
3142 case DRM_FORMAT_ARGB1555:
3143 case DRM_FORMAT_ABGR1555:
3144 case DRM_FORMAT_RGBA5551:
3145 case DRM_FORMAT_BGRA5551:
3146 case DRM_FORMAT_RGB565:
3147 case DRM_FORMAT_BGR565:
3148 case DRM_FORMAT_RGB888:
3149 case DRM_FORMAT_BGR888:
3150 case DRM_FORMAT_XRGB8888:
3151 case DRM_FORMAT_XBGR8888:
3152 case DRM_FORMAT_RGBX8888:
3153 case DRM_FORMAT_BGRX8888:
3154 case DRM_FORMAT_ARGB8888:
3155 case DRM_FORMAT_ABGR8888:
3156 case DRM_FORMAT_RGBA8888:
3157 case DRM_FORMAT_BGRA8888:
3158 case DRM_FORMAT_XRGB2101010:
3159 case DRM_FORMAT_XBGR2101010:
3160 case DRM_FORMAT_RGBX1010102:
3161 case DRM_FORMAT_BGRX1010102:
3162 case DRM_FORMAT_ARGB2101010:
3163 case DRM_FORMAT_ABGR2101010:
3164 case DRM_FORMAT_RGBA1010102:
3165 case DRM_FORMAT_BGRA1010102:
3166 case DRM_FORMAT_YUYV:
3167 case DRM_FORMAT_YVYU:
3168 case DRM_FORMAT_UYVY:
3169 case DRM_FORMAT_VYUY:
3170 case DRM_FORMAT_AYUV:
3171 case DRM_FORMAT_NV12:
3172 case DRM_FORMAT_NV21:
3173 case DRM_FORMAT_NV16:
3174 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003175 case DRM_FORMAT_NV24:
3176 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003177 case DRM_FORMAT_YUV410:
3178 case DRM_FORMAT_YVU410:
3179 case DRM_FORMAT_YUV411:
3180 case DRM_FORMAT_YVU411:
3181 case DRM_FORMAT_YUV420:
3182 case DRM_FORMAT_YVU420:
3183 case DRM_FORMAT_YUV422:
3184 case DRM_FORMAT_YVU422:
3185 case DRM_FORMAT_YUV444:
3186 case DRM_FORMAT_YVU444:
3187 return 0;
3188 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003189 DRM_DEBUG_KMS("invalid pixel format %s\n",
3190 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003191 return -EINVAL;
3192 }
3193}
3194
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003195static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003196{
3197 int ret, hsub, vsub, num_planes, i;
3198
3199 ret = format_check(r);
3200 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003201 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3202 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003203 return ret;
3204 }
3205
3206 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3207 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3208 num_planes = drm_format_num_planes(r->pixel_format);
3209
3210 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003211 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003212 return -EINVAL;
3213 }
3214
3215 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003216 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003217 return -EINVAL;
3218 }
3219
3220 for (i = 0; i < num_planes; i++) {
3221 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003222 unsigned int height = r->height / (i != 0 ? vsub : 1);
3223 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003224
3225 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003226 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003227 return -EINVAL;
3228 }
3229
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003230 if ((uint64_t) width * cpp > UINT_MAX)
3231 return -ERANGE;
3232
3233 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3234 return -ERANGE;
3235
3236 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003237 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003238 return -EINVAL;
3239 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003240
3241 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3242 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3243 r->modifier[i], i);
3244 return -EINVAL;
3245 }
Rob Clark570655b2015-01-30 20:18:11 +05303246
3247 /* modifier specific checks: */
3248 switch (r->modifier[i]) {
3249 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3250 /* NOTE: the pitch restriction may be lifted later if it turns
3251 * out that no hw has this restriction:
3252 */
3253 if (r->pixel_format != DRM_FORMAT_NV12 ||
3254 width % 128 || height % 32 ||
3255 r->pitches[i] % 128) {
3256 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3257 return -EINVAL;
3258 }
3259 break;
3260
3261 default:
3262 break;
3263 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003264 }
3265
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003266 for (i = num_planes; i < 4; i++) {
3267 if (r->modifier[i]) {
3268 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3269 return -EINVAL;
3270 }
3271
3272 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3273 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3274 continue;
3275
3276 if (r->handles[i]) {
3277 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3278 return -EINVAL;
3279 }
3280
3281 if (r->pitches[i]) {
3282 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3283 return -EINVAL;
3284 }
3285
3286 if (r->offsets[i]) {
3287 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3288 return -EINVAL;
3289 }
3290 }
3291
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003292 return 0;
3293}
3294
Chris Wilson9a6f5132015-02-25 13:45:26 +00003295static struct drm_framebuffer *
3296internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003297 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003298 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003299{
3300 struct drm_mode_config *config = &dev->mode_config;
3301 struct drm_framebuffer *fb;
3302 int ret;
3303
Rob Clarke3eb3252015-02-05 14:41:52 +00003304 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003305 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3306 return ERR_PTR(-EINVAL);
3307 }
3308
3309 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3310 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3311 r->width, config->min_width, config->max_width);
3312 return ERR_PTR(-EINVAL);
3313 }
3314 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3315 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3316 r->height, config->min_height, config->max_height);
3317 return ERR_PTR(-EINVAL);
3318 }
3319
Rob Clarke3eb3252015-02-05 14:41:52 +00003320 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3321 !dev->mode_config.allow_fb_modifiers) {
3322 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3323 return ERR_PTR(-EINVAL);
3324 }
3325
Matt Roperc394c2b2014-06-10 08:28:08 -07003326 ret = framebuffer_check(r);
3327 if (ret)
3328 return ERR_PTR(ret);
3329
3330 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3331 if (IS_ERR(fb)) {
3332 DRM_DEBUG_KMS("could not create framebuffer\n");
3333 return fb;
3334 }
3335
Matt Roperc394c2b2014-06-10 08:28:08 -07003336 return fb;
3337}
3338
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003339/**
3340 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003341 * @dev: drm device for the ioctl
3342 * @data: data pointer for the ioctl
3343 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003344 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003345 * Add a new FB to the specified CRTC, given a user request with format. This is
3346 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3347 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003348 *
3349 * Called by the user via ioctl.
3350 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003351 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003352 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003353 */
3354int drm_mode_addfb2(struct drm_device *dev,
3355 void *data, struct drm_file *file_priv)
3356{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003357 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003358 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003359
Dave Airliefb3b06c2011-02-08 13:55:21 +10003360 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3361 return -EINVAL;
3362
Chris Wilson9a6f5132015-02-25 13:45:26 +00003363 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003364 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003365 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003366
Chris Wilson9a6f5132015-02-25 13:45:26 +00003367 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003368 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003369
3370 /* Transfer ownership to the filp for reaping on close */
3371 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003372 list_add(&fb->filp_head, &file_priv->fbs);
3373 mutex_unlock(&file_priv->fbs_lock);
3374
Matt Roperc394c2b2014-06-10 08:28:08 -07003375 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003376}
3377
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003378struct drm_mode_rmfb_work {
3379 struct work_struct work;
3380 struct list_head fbs;
3381};
3382
3383static void drm_mode_rmfb_work_fn(struct work_struct *w)
3384{
3385 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3386
3387 while (!list_empty(&arg->fbs)) {
3388 struct drm_framebuffer *fb =
3389 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3390
3391 list_del_init(&fb->filp_head);
3392 drm_framebuffer_remove(fb);
3393 }
3394}
3395
Dave Airlief453ba02008-11-07 14:05:41 -08003396/**
3397 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003398 * @dev: drm device for the ioctl
3399 * @data: data pointer for the ioctl
3400 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003401 *
Dave Airlief453ba02008-11-07 14:05:41 -08003402 * Remove the FB specified by the user.
3403 *
3404 * Called by the user via ioctl.
3405 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003406 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003407 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003408 */
3409int drm_mode_rmfb(struct drm_device *dev,
3410 void *data, struct drm_file *file_priv)
3411{
Dave Airlief453ba02008-11-07 14:05:41 -08003412 struct drm_framebuffer *fb = NULL;
3413 struct drm_framebuffer *fbl = NULL;
3414 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003415 int found = 0;
3416
Dave Airliefb3b06c2011-02-08 13:55:21 +10003417 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3418 return -EINVAL;
3419
Dave Airlie72fe90b2016-04-15 15:10:40 +10003420 fb = drm_framebuffer_lookup(dev, *id);
3421 if (!fb)
3422 return -ENOENT;
3423
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003424 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003425 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3426 if (fb == fbl)
3427 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003428 if (!found) {
3429 mutex_unlock(&file_priv->fbs_lock);
3430 goto fail_unref;
3431 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003432
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003433 list_del_init(&fb->filp_head);
3434 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003435
Dave Airlie72fe90b2016-04-15 15:10:40 +10003436 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003437 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003438
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003439 /*
3440 * we now own the reference that was stored in the fbs list
3441 *
3442 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3443 * so run this in a separate stack as there's no way to correctly
3444 * handle this after the fb is already removed from the lookup table.
3445 */
3446 if (drm_framebuffer_read_refcount(fb) > 1) {
3447 struct drm_mode_rmfb_work arg;
3448
3449 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3450 INIT_LIST_HEAD(&arg.fbs);
3451 list_add_tail(&fb->filp_head, &arg.fbs);
3452
3453 schedule_work(&arg.work);
3454 flush_work(&arg.work);
3455 destroy_work_on_stack(&arg.work);
3456 } else
3457 drm_framebuffer_unreference(fb);
3458
Daniel Vetter2b677e82012-12-10 21:16:05 +01003459 return 0;
3460
Dave Airlie72fe90b2016-04-15 15:10:40 +10003461fail_unref:
3462 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003463 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003464}
3465
3466/**
3467 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003468 * @dev: drm device for the ioctl
3469 * @data: data pointer for the ioctl
3470 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003471 *
Dave Airlief453ba02008-11-07 14:05:41 -08003472 * Lookup the FB given its ID and return info about it.
3473 *
3474 * Called by the user via ioctl.
3475 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003476 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003477 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003478 */
3479int drm_mode_getfb(struct drm_device *dev,
3480 void *data, struct drm_file *file_priv)
3481{
3482 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003483 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003484 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003485
Dave Airliefb3b06c2011-02-08 13:55:21 +10003486 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3487 return -EINVAL;
3488
Daniel Vetter786b99e2012-12-02 21:53:40 +01003489 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003490 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003491 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003492
3493 r->height = fb->height;
3494 r->width = fb->width;
3495 r->depth = fb->depth;
3496 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003497 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003498 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003499 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003500 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003501 ret = fb->funcs->create_handle(fb, file_priv,
3502 &r->handle);
3503 } else {
3504 /* GET_FB() is an unprivileged ioctl so we must not
3505 * return a buffer-handle to non-master processes! For
3506 * backwards-compatibility reasons, we cannot make
3507 * GET_FB() privileged, so just return an invalid handle
3508 * for non-masters. */
3509 r->handle = 0;
3510 ret = 0;
3511 }
3512 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003513 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003514 }
Dave Airlief453ba02008-11-07 14:05:41 -08003515
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003516 drm_framebuffer_unreference(fb);
3517
Dave Airlief453ba02008-11-07 14:05:41 -08003518 return ret;
3519}
3520
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003521/**
3522 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3523 * @dev: drm device for the ioctl
3524 * @data: data pointer for the ioctl
3525 * @file_priv: drm file for the ioctl call
3526 *
3527 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3528 * rectangle list. Generic userspace which does frontbuffer rendering must call
3529 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3530 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3531 *
3532 * Modesetting drivers which always update the frontbuffer do not need to
3533 * implement the corresponding ->dirty framebuffer callback.
3534 *
3535 * Called by the user via ioctl.
3536 *
3537 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003538 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003539 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003540int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3541 void *data, struct drm_file *file_priv)
3542{
3543 struct drm_clip_rect __user *clips_ptr;
3544 struct drm_clip_rect *clips = NULL;
3545 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003546 struct drm_framebuffer *fb;
3547 unsigned flags;
3548 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003549 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003550
Dave Airliefb3b06c2011-02-08 13:55:21 +10003551 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3552 return -EINVAL;
3553
Daniel Vetter786b99e2012-12-02 21:53:40 +01003554 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003555 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003556 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003557
3558 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003559 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003560
3561 if (!num_clips != !clips_ptr) {
3562 ret = -EINVAL;
3563 goto out_err1;
3564 }
3565
3566 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3567
3568 /* If userspace annotates copy, clips must come in pairs */
3569 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3570 ret = -EINVAL;
3571 goto out_err1;
3572 }
3573
3574 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003575 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3576 ret = -EINVAL;
3577 goto out_err1;
3578 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003579 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003580 if (!clips) {
3581 ret = -ENOMEM;
3582 goto out_err1;
3583 }
3584
3585 ret = copy_from_user(clips, clips_ptr,
3586 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003587 if (ret) {
3588 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003589 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003590 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003591 }
3592
3593 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003594 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3595 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003596 } else {
3597 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003598 }
3599
3600out_err2:
3601 kfree(clips);
3602out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003603 drm_framebuffer_unreference(fb);
3604
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003605 return ret;
3606}
3607
Dave Airlief453ba02008-11-07 14:05:41 -08003608/**
3609 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003610 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003611 *
Dave Airlief453ba02008-11-07 14:05:41 -08003612 * Destroy all the FBs associated with @filp.
3613 *
3614 * Called by the user via ioctl.
3615 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003616 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003617 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003618 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003619void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003620{
Dave Airlief453ba02008-11-07 14:05:41 -08003621 struct drm_framebuffer *fb, *tfb;
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003622 struct drm_mode_rmfb_work arg;
3623
3624 INIT_LIST_HEAD(&arg.fbs);
Dave Airlief453ba02008-11-07 14:05:41 -08003625
Daniel Vetter1b116292014-09-24 21:51:06 +02003626 /*
3627 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003628 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003629 * avoid upsetting lockdep since the universal cursor code adds a
3630 * framebuffer while holding mutex locks.
3631 *
3632 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3633 * locks is impossible here since no one else but this function can get
3634 * at it any more.
3635 */
Dave Airlief453ba02008-11-07 14:05:41 -08003636 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003637 if (drm_framebuffer_read_refcount(fb) > 1) {
3638 list_move_tail(&fb->filp_head, &arg.fbs);
3639 } else {
3640 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003641
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003642 /* This drops the fpriv->fbs reference. */
3643 drm_framebuffer_unreference(fb);
3644 }
3645 }
3646
3647 if (!list_empty(&arg.fbs)) {
3648 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3649
3650 schedule_work(&arg.work);
3651 flush_work(&arg.work);
3652 destroy_work_on_stack(&arg.work);
Dave Airlief453ba02008-11-07 14:05:41 -08003653 }
Dave Airlief453ba02008-11-07 14:05:41 -08003654}
3655
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003656/**
3657 * drm_property_create - create a new property type
3658 * @dev: drm device
3659 * @flags: flags specifying the property type
3660 * @name: name of the property
3661 * @num_values: number of pre-defined values
3662 *
3663 * This creates a new generic drm property which can then be attached to a drm
3664 * object with drm_object_attach_property. The returned property object must be
3665 * freed with drm_property_destroy.
3666 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003667 * Note that the DRM core keeps a per-device list of properties and that, if
3668 * drm_mode_config_cleanup() is called, it will destroy all properties created
3669 * by the driver.
3670 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003671 * Returns:
3672 * A pointer to the newly created property on success, NULL on failure.
3673 */
Dave Airlief453ba02008-11-07 14:05:41 -08003674struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3675 const char *name, int num_values)
3676{
3677 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003678 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003679
3680 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3681 if (!property)
3682 return NULL;
3683
Rob Clark98f75de2014-05-30 11:37:03 -04003684 property->dev = dev;
3685
Dave Airlief453ba02008-11-07 14:05:41 -08003686 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003687 property->values = kcalloc(num_values, sizeof(uint64_t),
3688 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003689 if (!property->values)
3690 goto fail;
3691 }
3692
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003693 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3694 if (ret)
3695 goto fail;
3696
Dave Airlief453ba02008-11-07 14:05:41 -08003697 property->flags = flags;
3698 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003699 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003700
Vinson Lee471dd2e2011-11-10 11:55:40 -08003701 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003702 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003703 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3704 }
Dave Airlief453ba02008-11-07 14:05:41 -08003705
3706 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003707
3708 WARN_ON(!drm_property_type_valid(property));
3709
Dave Airlief453ba02008-11-07 14:05:41 -08003710 return property;
3711fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003712 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003713 kfree(property);
3714 return NULL;
3715}
3716EXPORT_SYMBOL(drm_property_create);
3717
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003718/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003719 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003720 * @dev: drm device
3721 * @flags: flags specifying the property type
3722 * @name: name of the property
3723 * @props: enumeration lists with property values
3724 * @num_values: number of pre-defined values
3725 *
3726 * This creates a new generic drm property which can then be attached to a drm
3727 * object with drm_object_attach_property. The returned property object must be
3728 * freed with drm_property_destroy.
3729 *
3730 * Userspace is only allowed to set one of the predefined values for enumeration
3731 * properties.
3732 *
3733 * Returns:
3734 * A pointer to the newly created property on success, NULL on failure.
3735 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003736struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3737 const char *name,
3738 const struct drm_prop_enum_list *props,
3739 int num_values)
3740{
3741 struct drm_property *property;
3742 int i, ret;
3743
3744 flags |= DRM_MODE_PROP_ENUM;
3745
3746 property = drm_property_create(dev, flags, name, num_values);
3747 if (!property)
3748 return NULL;
3749
3750 for (i = 0; i < num_values; i++) {
3751 ret = drm_property_add_enum(property, i,
3752 props[i].type,
3753 props[i].name);
3754 if (ret) {
3755 drm_property_destroy(dev, property);
3756 return NULL;
3757 }
3758 }
3759
3760 return property;
3761}
3762EXPORT_SYMBOL(drm_property_create_enum);
3763
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003764/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003765 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003766 * @dev: drm device
3767 * @flags: flags specifying the property type
3768 * @name: name of the property
3769 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003770 * @num_props: size of the @props array
3771 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003772 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003773 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003774 * object with drm_object_attach_property. The returned property object must be
3775 * freed with drm_property_destroy.
3776 *
3777 * Compared to plain enumeration properties userspace is allowed to set any
3778 * or'ed together combination of the predefined property bitflag values
3779 *
3780 * Returns:
3781 * A pointer to the newly created property on success, NULL on failure.
3782 */
Rob Clark49e27542012-05-17 02:23:26 -06003783struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3784 int flags, const char *name,
3785 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303786 int num_props,
3787 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003788{
3789 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303790 int i, ret, index = 0;
3791 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003792
3793 flags |= DRM_MODE_PROP_BITMASK;
3794
3795 property = drm_property_create(dev, flags, name, num_values);
3796 if (!property)
3797 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303798 for (i = 0; i < num_props; i++) {
3799 if (!(supported_bits & (1ULL << props[i].type)))
3800 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003801
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303802 if (WARN_ON(index >= num_values)) {
3803 drm_property_destroy(dev, property);
3804 return NULL;
3805 }
3806
3807 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003808 props[i].type,
3809 props[i].name);
3810 if (ret) {
3811 drm_property_destroy(dev, property);
3812 return NULL;
3813 }
3814 }
3815
3816 return property;
3817}
3818EXPORT_SYMBOL(drm_property_create_bitmask);
3819
Rob Clarkebc44cf2012-09-12 22:22:31 -05003820static struct drm_property *property_create_range(struct drm_device *dev,
3821 int flags, const char *name,
3822 uint64_t min, uint64_t max)
3823{
3824 struct drm_property *property;
3825
3826 property = drm_property_create(dev, flags, name, 2);
3827 if (!property)
3828 return NULL;
3829
3830 property->values[0] = min;
3831 property->values[1] = max;
3832
3833 return property;
3834}
3835
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003836/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003837 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003838 * @dev: drm device
3839 * @flags: flags specifying the property type
3840 * @name: name of the property
3841 * @min: minimum value of the property
3842 * @max: maximum value of the property
3843 *
3844 * This creates a new generic drm property which can then be attached to a drm
3845 * object with drm_object_attach_property. The returned property object must be
3846 * freed with drm_property_destroy.
3847 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003848 * Userspace is allowed to set any unsigned integer value in the (min, max)
3849 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003850 *
3851 * Returns:
3852 * A pointer to the newly created property on success, NULL on failure.
3853 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003854struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3855 const char *name,
3856 uint64_t min, uint64_t max)
3857{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003858 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3859 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003860}
3861EXPORT_SYMBOL(drm_property_create_range);
3862
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003863/**
3864 * drm_property_create_signed_range - create a new signed ranged property type
3865 * @dev: drm device
3866 * @flags: flags specifying the property type
3867 * @name: name of the property
3868 * @min: minimum value of the property
3869 * @max: maximum value of the property
3870 *
3871 * This creates a new generic drm property which can then be attached to a drm
3872 * object with drm_object_attach_property. The returned property object must be
3873 * freed with drm_property_destroy.
3874 *
3875 * Userspace is allowed to set any signed integer value in the (min, max)
3876 * range inclusive.
3877 *
3878 * Returns:
3879 * A pointer to the newly created property on success, NULL on failure.
3880 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003881struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3882 int flags, const char *name,
3883 int64_t min, int64_t max)
3884{
3885 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3886 name, I642U64(min), I642U64(max));
3887}
3888EXPORT_SYMBOL(drm_property_create_signed_range);
3889
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003890/**
3891 * drm_property_create_object - create a new object property type
3892 * @dev: drm device
3893 * @flags: flags specifying the property type
3894 * @name: name of the property
3895 * @type: object type from DRM_MODE_OBJECT_* defines
3896 *
3897 * This creates a new generic drm property which can then be attached to a drm
3898 * object with drm_object_attach_property. The returned property object must be
3899 * freed with drm_property_destroy.
3900 *
3901 * Userspace is only allowed to set this to any property value of the given
3902 * @type. Only useful for atomic properties, which is enforced.
3903 *
3904 * Returns:
3905 * A pointer to the newly created property on success, NULL on failure.
3906 */
Rob Clark98f75de2014-05-30 11:37:03 -04003907struct drm_property *drm_property_create_object(struct drm_device *dev,
3908 int flags, const char *name, uint32_t type)
3909{
3910 struct drm_property *property;
3911
3912 flags |= DRM_MODE_PROP_OBJECT;
3913
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003914 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3915 return NULL;
3916
Rob Clark98f75de2014-05-30 11:37:03 -04003917 property = drm_property_create(dev, flags, name, 1);
3918 if (!property)
3919 return NULL;
3920
3921 property->values[0] = type;
3922
3923 return property;
3924}
3925EXPORT_SYMBOL(drm_property_create_object);
3926
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003927/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003928 * drm_property_create_bool - create a new boolean property type
3929 * @dev: drm device
3930 * @flags: flags specifying the property type
3931 * @name: name of the property
3932 *
3933 * This creates a new generic drm property which can then be attached to a drm
3934 * object with drm_object_attach_property. The returned property object must be
3935 * freed with drm_property_destroy.
3936 *
3937 * This is implemented as a ranged property with only {0, 1} as valid values.
3938 *
3939 * Returns:
3940 * A pointer to the newly created property on success, NULL on failure.
3941 */
3942struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3943 const char *name)
3944{
3945 return drm_property_create_range(dev, flags, name, 0, 1);
3946}
3947EXPORT_SYMBOL(drm_property_create_bool);
3948
3949/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003950 * drm_property_add_enum - add a possible value to an enumeration property
3951 * @property: enumeration property to change
3952 * @index: index of the new enumeration
3953 * @value: value of the new enumeration
3954 * @name: symbolic name of the new enumeration
3955 *
3956 * This functions adds enumerations to a property.
3957 *
3958 * It's use is deprecated, drivers should use one of the more specific helpers
3959 * to directly create the property with all enumerations already attached.
3960 *
3961 * Returns:
3962 * Zero on success, error code on failure.
3963 */
Dave Airlief453ba02008-11-07 14:05:41 -08003964int drm_property_add_enum(struct drm_property *property, int index,
3965 uint64_t value, const char *name)
3966{
3967 struct drm_property_enum *prop_enum;
3968
Rob Clark5ea22f22014-05-30 11:34:01 -04003969 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3970 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003971 return -EINVAL;
3972
3973 /*
3974 * Bitmask enum properties have the additional constraint of values
3975 * from 0 to 63
3976 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003977 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3978 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003979 return -EINVAL;
3980
Daniel Vetter3758b342014-11-19 18:38:10 +01003981 if (!list_empty(&property->enum_list)) {
3982 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003983 if (prop_enum->value == value) {
3984 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3985 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3986 return 0;
3987 }
3988 }
3989 }
3990
3991 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3992 if (!prop_enum)
3993 return -ENOMEM;
3994
3995 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3996 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3997 prop_enum->value = value;
3998
3999 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004000 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004001 return 0;
4002}
4003EXPORT_SYMBOL(drm_property_add_enum);
4004
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004005/**
4006 * drm_property_destroy - destroy a drm property
4007 * @dev: drm device
4008 * @property: property to destry
4009 *
4010 * This function frees a property including any attached resources like
4011 * enumeration values.
4012 */
Dave Airlief453ba02008-11-07 14:05:41 -08004013void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4014{
4015 struct drm_property_enum *prop_enum, *pt;
4016
Daniel Vetter3758b342014-11-19 18:38:10 +01004017 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004018 list_del(&prop_enum->head);
4019 kfree(prop_enum);
4020 }
4021
4022 if (property->num_values)
4023 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004024 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004025 list_del(&property->head);
4026 kfree(property);
4027}
4028EXPORT_SYMBOL(drm_property_destroy);
4029
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004030/**
4031 * drm_object_attach_property - attach a property to a modeset object
4032 * @obj: drm modeset object
4033 * @property: property to attach
4034 * @init_val: initial value of the property
4035 *
4036 * This attaches the given property to the modeset object with the given initial
4037 * value. Currently this function cannot fail since the properties are stored in
4038 * a statically sized array.
4039 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004040void drm_object_attach_property(struct drm_mode_object *obj,
4041 struct drm_property *property,
4042 uint64_t init_val)
4043{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004044 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004045
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004046 if (count == DRM_OBJECT_MAX_PROPERTY) {
4047 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4048 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4049 "you see this message on the same object type.\n",
4050 obj->type);
4051 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004052 }
4053
Rob Clarkb17cd752014-12-16 18:05:30 -05004054 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004055 obj->properties->values[count] = init_val;
4056 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004057 if (property->flags & DRM_MODE_PROP_ATOMIC)
4058 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004059}
4060EXPORT_SYMBOL(drm_object_attach_property);
4061
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004062/**
4063 * drm_object_property_set_value - set the value of a property
4064 * @obj: drm mode object to set property value for
4065 * @property: property to set
4066 * @val: value the property should be set to
4067 *
4068 * This functions sets a given property on a given object. This function only
4069 * changes the software state of the property, it does not call into the
4070 * driver's ->set_property callback.
4071 *
4072 * Returns:
4073 * Zero on success, error code on failure.
4074 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004075int drm_object_property_set_value(struct drm_mode_object *obj,
4076 struct drm_property *property, uint64_t val)
4077{
4078 int i;
4079
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004080 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004081 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004082 obj->properties->values[i] = val;
4083 return 0;
4084 }
4085 }
4086
4087 return -EINVAL;
4088}
4089EXPORT_SYMBOL(drm_object_property_set_value);
4090
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004091/**
4092 * drm_object_property_get_value - retrieve the value of a property
4093 * @obj: drm mode object to get property value from
4094 * @property: property to retrieve
4095 * @val: storage for the property value
4096 *
4097 * This function retrieves the softare state of the given property for the given
4098 * property. Since there is no driver callback to retrieve the current property
4099 * value this might be out of sync with the hardware, depending upon the driver
4100 * and property.
4101 *
4102 * Returns:
4103 * Zero on success, error code on failure.
4104 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004105int drm_object_property_get_value(struct drm_mode_object *obj,
4106 struct drm_property *property, uint64_t *val)
4107{
4108 int i;
4109
Rob Clark88a48e22014-12-18 16:01:50 -05004110 /* read-only properties bypass atomic mechanism and still store
4111 * their value in obj->properties->values[].. mostly to avoid
4112 * having to deal w/ EDID and similar props in atomic paths:
4113 */
4114 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4115 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4116 return drm_atomic_get_property(obj, property, val);
4117
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004118 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004119 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004120 *val = obj->properties->values[i];
4121 return 0;
4122 }
4123 }
4124
4125 return -EINVAL;
4126}
4127EXPORT_SYMBOL(drm_object_property_get_value);
4128
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004129/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004130 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004131 * @dev: DRM device
4132 * @data: ioctl data
4133 * @file_priv: DRM file info
4134 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004135 * This function retrieves the metadata for a given property, like the different
4136 * possible values for an enum property or the limits for a range property.
4137 *
4138 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004139 *
4140 * Called by the user via ioctl.
4141 *
4142 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004143 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004144 */
Dave Airlief453ba02008-11-07 14:05:41 -08004145int drm_mode_getproperty_ioctl(struct drm_device *dev,
4146 void *data, struct drm_file *file_priv)
4147{
Dave Airlief453ba02008-11-07 14:05:41 -08004148 struct drm_mode_get_property *out_resp = data;
4149 struct drm_property *property;
4150 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004151 int value_count = 0;
4152 int ret = 0, i;
4153 int copied;
4154 struct drm_property_enum *prop_enum;
4155 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004156 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004157
Dave Airliefb3b06c2011-02-08 13:55:21 +10004158 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4159 return -EINVAL;
4160
Daniel Vetter84849902012-12-02 00:28:11 +01004161 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004162 property = drm_property_find(dev, out_resp->prop_id);
4163 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004164 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004165 goto done;
4166 }
Dave Airlief453ba02008-11-07 14:05:41 -08004167
Rob Clark5ea22f22014-05-30 11:34:01 -04004168 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4169 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004170 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004171 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004172 }
4173
4174 value_count = property->num_values;
4175
4176 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4177 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4178 out_resp->flags = property->flags;
4179
4180 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004181 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004182 for (i = 0; i < value_count; i++) {
4183 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4184 ret = -EFAULT;
4185 goto done;
4186 }
4187 }
4188 }
4189 out_resp->count_values = value_count;
4190
Rob Clark5ea22f22014-05-30 11:34:01 -04004191 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4192 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004193 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4194 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004195 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004196 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004197
4198 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4199 ret = -EFAULT;
4200 goto done;
4201 }
4202
4203 if (copy_to_user(&enum_ptr[copied].name,
4204 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4205 ret = -EFAULT;
4206 goto done;
4207 }
4208 copied++;
4209 }
4210 }
4211 out_resp->count_enum_blobs = enum_count;
4212 }
4213
Daniel Vetter3758b342014-11-19 18:38:10 +01004214 /*
4215 * NOTE: The idea seems to have been to use this to read all the blob
4216 * property values. But nothing ever added them to the corresponding
4217 * list, userspace always used the special-purpose get_blob ioctl to
4218 * read the value for a blob property. It also doesn't make a lot of
4219 * sense to return values here when everything else is just metadata for
4220 * the property itself.
4221 */
4222 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4223 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004224done:
Daniel Vetter84849902012-12-02 00:28:11 +01004225 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004226 return ret;
4227}
4228
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004229static void drm_property_free_blob(struct kref *kref)
4230{
4231 struct drm_property_blob *blob =
4232 container_of(kref, struct drm_property_blob, base.refcount);
4233
4234 mutex_lock(&blob->dev->mode_config.blob_lock);
4235 list_del(&blob->head_global);
4236 mutex_unlock(&blob->dev->mode_config.blob_lock);
4237
4238 drm_mode_object_unregister(blob->dev, &blob->base);
4239
4240 kfree(blob);
4241}
4242
Daniel Stone99531d92015-05-22 13:34:49 +01004243/**
4244 * drm_property_create_blob - Create new blob property
4245 *
4246 * Creates a new blob property for a specified DRM device, optionally
4247 * copying data.
4248 *
4249 * @dev: DRM device to create property for
4250 * @length: Length to allocate for blob data
4251 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004252 *
4253 * Returns:
4254 * New blob property with a single reference on success, or an ERR_PTR
4255 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004256 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004257struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004258drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004259 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004260{
4261 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004262 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004263
Dan Carpenter9ac09342015-10-29 16:37:54 +03004264 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004265 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004266
4267 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4268 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004269 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004270
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004271 /* This must be explicitly initialised, so we can safely call list_del
4272 * on it in the removal handler, even if it isn't in a file list. */
4273 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004274 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004275 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004276
Daniel Stone99531d92015-05-22 13:34:49 +01004277 if (data)
4278 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004279
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004280 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4281 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004282 if (ret) {
4283 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004284 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004285 }
4286
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004287 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004288 list_add_tail(&blob->head_global,
4289 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004290 mutex_unlock(&dev->mode_config.blob_lock);
4291
Dave Airlief453ba02008-11-07 14:05:41 -08004292 return blob;
4293}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004294EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004295
Daniel Stone6bcacf52015-04-20 19:22:55 +01004296/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004297 * drm_property_unreference_blob - Unreference a blob property
4298 *
4299 * Drop a reference on a blob property. May free the object.
4300 *
Daniel Stonef102c162015-05-22 13:34:44 +01004301 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004302 */
4303void drm_property_unreference_blob(struct drm_property_blob *blob)
4304{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004305 if (!blob)
4306 return;
4307
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004308 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004309}
4310EXPORT_SYMBOL(drm_property_unreference_blob);
4311
4312/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004313 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4314 * @dev: DRM device
4315 * @file_priv: destroy all blobs owned by this file handle
4316 */
4317void drm_property_destroy_user_blobs(struct drm_device *dev,
4318 struct drm_file *file_priv)
4319{
4320 struct drm_property_blob *blob, *bt;
4321
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004322 /*
4323 * When the file gets released that means no one else can access the
4324 * blob list any more, so no need to grab dev->blob_lock.
4325 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004326 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4327 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004328 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004329 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004330}
4331
4332/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004333 * drm_property_reference_blob - Take a reference on an existing property
4334 *
4335 * Take a new reference on an existing blob property.
4336 *
Daniel Stonef102c162015-05-22 13:34:44 +01004337 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004338 */
4339struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4340{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004341 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004342 return blob;
4343}
4344EXPORT_SYMBOL(drm_property_reference_blob);
4345
Daniel Stone6bcacf52015-04-20 19:22:55 +01004346/**
4347 * drm_property_lookup_blob - look up a blob property and take a reference
4348 * @dev: drm device
4349 * @id: id of the blob property
4350 *
4351 * If successful, this takes an additional reference to the blob property.
4352 * callers need to make sure to eventually unreference the returned property
4353 * again, using @drm_property_unreference_blob.
4354 */
4355struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4356 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004357{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004358 struct drm_mode_object *obj;
4359 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004360
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004361 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4362 if (obj)
4363 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004364 return blob;
4365}
4366EXPORT_SYMBOL(drm_property_lookup_blob);
4367
4368/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004369 * drm_property_replace_global_blob - atomically replace existing blob property
4370 * @dev: drm device
4371 * @replace: location of blob property pointer to be replaced
4372 * @length: length of data for new blob, or 0 for no data
4373 * @data: content for new blob, or NULL for no data
4374 * @obj_holds_id: optional object for property holding blob ID
4375 * @prop_holds_id: optional property holding blob ID
4376 * @return 0 on success or error on failure
4377 *
4378 * This function will atomically replace a global property in the blob list,
4379 * optionally updating a property which holds the ID of that property. It is
4380 * guaranteed to be atomic: no caller will be allowed to see intermediate
4381 * results, and either the entire operation will succeed and clean up the
4382 * previous property, or it will fail and the state will be unchanged.
4383 *
4384 * If length is 0 or data is NULL, no new blob will be created, and the holding
4385 * property, if specified, will be set to 0.
4386 *
4387 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4388 * by holding the relevant modesetting object lock for its parent.
4389 *
4390 * For example, a drm_connector has a 'PATH' property, which contains the ID
4391 * of a blob property with the value of the MST path information. Calling this
4392 * function with replace pointing to the connector's path_blob_ptr, length and
4393 * data set for the new path information, obj_holds_id set to the connector's
4394 * base object, and prop_holds_id set to the path property name, will perform
4395 * a completely atomic update. The access to path_blob_ptr is protected by the
4396 * caller holding a lock on the connector.
4397 */
4398static int drm_property_replace_global_blob(struct drm_device *dev,
4399 struct drm_property_blob **replace,
4400 size_t length,
4401 const void *data,
4402 struct drm_mode_object *obj_holds_id,
4403 struct drm_property *prop_holds_id)
4404{
4405 struct drm_property_blob *new_blob = NULL;
4406 struct drm_property_blob *old_blob = NULL;
4407 int ret;
4408
4409 WARN_ON(replace == NULL);
4410
4411 old_blob = *replace;
4412
4413 if (length && data) {
4414 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004415 if (IS_ERR(new_blob))
4416 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004417 }
4418
4419 /* This does not need to be synchronised with blob_lock, as the
4420 * get_properties ioctl locks all modesetting objects, and
4421 * obj_holds_id must be locked before calling here, so we cannot
4422 * have its value out of sync with the list membership modified
4423 * below under blob_lock. */
4424 if (obj_holds_id) {
4425 ret = drm_object_property_set_value(obj_holds_id,
4426 prop_holds_id,
4427 new_blob ?
4428 new_blob->base.id : 0);
4429 if (ret != 0)
4430 goto err_created;
4431 }
4432
Markus Elfringec530822015-07-05 21:55:10 +02004433 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004434 *replace = new_blob;
4435
4436 return 0;
4437
4438err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004439 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004440 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004441}
4442
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004443/**
4444 * drm_mode_getblob_ioctl - get the contents of a blob property value
4445 * @dev: DRM device
4446 * @data: ioctl data
4447 * @file_priv: DRM file info
4448 *
4449 * This function retrieves the contents of a blob property. The value stored in
4450 * an object's blob property is just a normal modeset object id.
4451 *
4452 * Called by the user via ioctl.
4453 *
4454 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004455 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004456 */
Dave Airlief453ba02008-11-07 14:05:41 -08004457int drm_mode_getblob_ioctl(struct drm_device *dev,
4458 void *data, struct drm_file *file_priv)
4459{
Dave Airlief453ba02008-11-07 14:05:41 -08004460 struct drm_mode_get_blob *out_resp = data;
4461 struct drm_property_blob *blob;
4462 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004463 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004464
Dave Airliefb3b06c2011-02-08 13:55:21 +10004465 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4466 return -EINVAL;
4467
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004468 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4469 if (!blob)
4470 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004471
4472 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004473 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004474 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004475 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004476 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08004477 }
4478 }
4479 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004480unref:
4481 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004482
Dave Airlief453ba02008-11-07 14:05:41 -08004483 return ret;
4484}
4485
Dave Airliecc7096f2014-10-22 12:03:04 +10004486/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004487 * drm_mode_createblob_ioctl - create a new blob property
4488 * @dev: DRM device
4489 * @data: ioctl data
4490 * @file_priv: DRM file info
4491 *
4492 * This function creates a new blob property with user-defined values. In order
4493 * to give us sensible validation and checking when creating, rather than at
4494 * every potential use, we also require a type to be provided upfront.
4495 *
4496 * Called by the user via ioctl.
4497 *
4498 * Returns:
4499 * Zero on success, negative errno on failure.
4500 */
4501int drm_mode_createblob_ioctl(struct drm_device *dev,
4502 void *data, struct drm_file *file_priv)
4503{
4504 struct drm_mode_create_blob *out_resp = data;
4505 struct drm_property_blob *blob;
4506 void __user *blob_ptr;
4507 int ret = 0;
4508
4509 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4510 return -EINVAL;
4511
4512 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4513 if (IS_ERR(blob))
4514 return PTR_ERR(blob);
4515
4516 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4517 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4518 ret = -EFAULT;
4519 goto out_blob;
4520 }
4521
4522 /* Dropping the lock between create_blob and our access here is safe
4523 * as only the same file_priv can remove the blob; at this point, it is
4524 * not associated with any file_priv. */
4525 mutex_lock(&dev->mode_config.blob_lock);
4526 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004527 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004528 mutex_unlock(&dev->mode_config.blob_lock);
4529
4530 return 0;
4531
4532out_blob:
4533 drm_property_unreference_blob(blob);
4534 return ret;
4535}
4536
4537/**
4538 * drm_mode_destroyblob_ioctl - destroy a user blob property
4539 * @dev: DRM device
4540 * @data: ioctl data
4541 * @file_priv: DRM file info
4542 *
4543 * Destroy an existing user-defined blob property.
4544 *
4545 * Called by the user via ioctl.
4546 *
4547 * Returns:
4548 * Zero on success, negative errno on failure.
4549 */
4550int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4551 void *data, struct drm_file *file_priv)
4552{
4553 struct drm_mode_destroy_blob *out_resp = data;
4554 struct drm_property_blob *blob = NULL, *bt;
4555 bool found = false;
4556 int ret = 0;
4557
4558 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4559 return -EINVAL;
4560
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004561 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4562 if (!blob)
4563 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004564
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004565 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004566 /* Ensure the property was actually created by this user. */
4567 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4568 if (bt == blob) {
4569 found = true;
4570 break;
4571 }
4572 }
4573
4574 if (!found) {
4575 ret = -EPERM;
4576 goto err;
4577 }
4578
4579 /* We must drop head_file here, because we may not be the last
4580 * reference on the blob. */
4581 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004582 mutex_unlock(&dev->mode_config.blob_lock);
4583
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004584 /* One reference from lookup, and one from the filp. */
4585 drm_property_unreference_blob(blob);
4586 drm_property_unreference_blob(blob);
4587
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004588 return 0;
4589
4590err:
4591 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004592 drm_property_unreference_blob(blob);
4593
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004594 return ret;
4595}
4596
4597/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004598 * drm_mode_connector_set_path_property - set tile property on connector
4599 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004600 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004601 *
4602 * This creates a property to expose to userspace to specify a
4603 * connector path. This is mainly used for DisplayPort MST where
4604 * connectors have a topology and we want to allow userspace to give
4605 * them more meaningful names.
4606 *
4607 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004608 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004609 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004610int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004611 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004612{
4613 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004614 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004615
Daniel Stoned2ed3432015-04-20 19:22:53 +01004616 ret = drm_property_replace_global_blob(dev,
4617 &connector->path_blob_ptr,
4618 strlen(path) + 1,
4619 path,
4620 &connector->base,
4621 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004622 return ret;
4623}
4624EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4625
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004626/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004627 * drm_mode_connector_set_tile_property - set tile property on connector
4628 * @connector: connector to set property on.
4629 *
4630 * This looks up the tile information for a connector, and creates a
4631 * property for userspace to parse if it exists. The property is of
4632 * the form of 8 integers using ':' as a separator.
4633 *
4634 * Returns:
4635 * Zero on success, errno on failure.
4636 */
4637int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4638{
4639 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004640 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004641 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004642
4643 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004644 ret = drm_property_replace_global_blob(dev,
4645 &connector->tile_blob_ptr,
4646 0,
4647 NULL,
4648 &connector->base,
4649 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004650 return ret;
4651 }
4652
4653 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4654 connector->tile_group->id, connector->tile_is_single_monitor,
4655 connector->num_h_tile, connector->num_v_tile,
4656 connector->tile_h_loc, connector->tile_v_loc,
4657 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004658
Daniel Stoned2ed3432015-04-20 19:22:53 +01004659 ret = drm_property_replace_global_blob(dev,
4660 &connector->tile_blob_ptr,
4661 strlen(tile) + 1,
4662 tile,
4663 &connector->base,
4664 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004665 return ret;
4666}
4667EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4668
4669/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004670 * drm_mode_connector_update_edid_property - update the edid property of a connector
4671 * @connector: drm connector
4672 * @edid: new value of the edid property
4673 *
4674 * This function creates a new blob modeset object and assigns its id to the
4675 * connector's edid property.
4676 *
4677 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004678 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004679 */
Dave Airlief453ba02008-11-07 14:05:41 -08004680int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004681 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004682{
4683 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004684 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004685 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004686
Thomas Wood4cf2b282014-06-18 17:52:33 +01004687 /* ignore requests to set edid when overridden */
4688 if (connector->override_edid)
4689 return 0;
4690
Daniel Stoned2ed3432015-04-20 19:22:53 +01004691 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004692 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004693
Daniel Stoned2ed3432015-04-20 19:22:53 +01004694 ret = drm_property_replace_global_blob(dev,
4695 &connector->edid_blob_ptr,
4696 size,
4697 edid,
4698 &connector->base,
4699 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004700 return ret;
4701}
4702EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4703
Rob Clark3843e712014-12-16 18:05:29 -05004704/* Some properties could refer to dynamic refcnt'd objects, or things that
4705 * need special locking to handle lifetime issues (ie. to ensure the prop
4706 * value doesn't become invalid part way through the property update due to
4707 * race). The value returned by reference via 'obj' should be passed back
4708 * to drm_property_change_valid_put() after the property is set (and the
4709 * object to which the property is attached has a chance to take it's own
4710 * reference).
4711 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004712bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004713 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004714{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004715 int i;
4716
Paulo Zanoni26a34812012-05-15 18:08:59 -03004717 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4718 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004719
Rob Clark3843e712014-12-16 18:05:29 -05004720 *ref = NULL;
4721
Rob Clark5ea22f22014-05-30 11:34:01 -04004722 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004723 if (value < property->values[0] || value > property->values[1])
4724 return false;
4725 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004726 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4727 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004728
Rob Clarkebc44cf2012-09-12 22:22:31 -05004729 if (svalue < U642I64(property->values[0]) ||
4730 svalue > U642I64(property->values[1]))
4731 return false;
4732 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004733 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004734 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004735
Rob Clark49e27542012-05-17 02:23:26 -06004736 for (i = 0; i < property->num_values; i++)
4737 valid_mask |= (1ULL << property->values[i]);
4738 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004739 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004740 struct drm_property_blob *blob;
4741
4742 if (value == 0)
4743 return true;
4744
4745 blob = drm_property_lookup_blob(property->dev, value);
4746 if (blob) {
4747 *ref = &blob->base;
4748 return true;
4749 } else {
4750 return false;
4751 }
Rob Clark98f75de2014-05-30 11:37:03 -04004752 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004753 /* a zero value for an object property translates to null: */
4754 if (value == 0)
4755 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004756
Tomi Valkeinen1e8985a2016-05-31 15:03:18 +03004757 *ref = _object_find(property->dev, value, property->values[0]);
4758 return *ref != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004759 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004760
4761 for (i = 0; i < property->num_values; i++)
4762 if (property->values[i] == value)
4763 return true;
4764 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004765}
4766
Rob Clarkd34f20d2014-12-18 16:01:56 -05004767void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004768 struct drm_mode_object *ref)
4769{
4770 if (!ref)
4771 return;
4772
4773 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004774 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004775 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4776 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004777}
4778
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004779/**
4780 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4781 * @dev: DRM device
4782 * @data: ioctl data
4783 * @file_priv: DRM file info
4784 *
4785 * This function sets the current value for a connectors's property. It also
4786 * calls into a driver's ->set_property callback to update the hardware state
4787 *
4788 * Called by the user via ioctl.
4789 *
4790 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004791 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004792 */
Dave Airlief453ba02008-11-07 14:05:41 -08004793int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4794 void *data, struct drm_file *file_priv)
4795{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004796 struct drm_mode_connector_set_property *conn_set_prop = data;
4797 struct drm_mode_obj_set_property obj_set_prop = {
4798 .value = conn_set_prop->value,
4799 .prop_id = conn_set_prop->prop_id,
4800 .obj_id = conn_set_prop->connector_id,
4801 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4802 };
Dave Airlief453ba02008-11-07 14:05:41 -08004803
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004804 /* It does all the locking and checking we need */
4805 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004806}
4807
Paulo Zanonic5431882012-05-15 18:09:02 -03004808static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4809 struct drm_property *property,
4810 uint64_t value)
4811{
4812 int ret = -EINVAL;
4813 struct drm_connector *connector = obj_to_connector(obj);
4814
4815 /* Do DPMS ourselves */
4816 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004817 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004818 } else if (connector->funcs->set_property)
4819 ret = connector->funcs->set_property(connector, property, value);
4820
4821 /* store the property value if successful */
4822 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004823 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004824 return ret;
4825}
4826
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004827static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4828 struct drm_property *property,
4829 uint64_t value)
4830{
4831 int ret = -EINVAL;
4832 struct drm_crtc *crtc = obj_to_crtc(obj);
4833
4834 if (crtc->funcs->set_property)
4835 ret = crtc->funcs->set_property(crtc, property, value);
4836 if (!ret)
4837 drm_object_property_set_value(obj, property, value);
4838
4839 return ret;
4840}
4841
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004842/**
4843 * drm_mode_plane_set_obj_prop - set the value of a property
4844 * @plane: drm plane object to set property value for
4845 * @property: property to set
4846 * @value: value the property should be set to
4847 *
4848 * This functions sets a given property on a given plane object. This function
4849 * calls the driver's ->set_property callback and changes the software state of
4850 * the property if the callback succeeds.
4851 *
4852 * Returns:
4853 * Zero on success, error code on failure.
4854 */
4855int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4856 struct drm_property *property,
4857 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004858{
4859 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004860 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004861
4862 if (plane->funcs->set_property)
4863 ret = plane->funcs->set_property(plane, property, value);
4864 if (!ret)
4865 drm_object_property_set_value(obj, property, value);
4866
4867 return ret;
4868}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004869EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004870
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004871/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004872 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004873 * @dev: DRM device
4874 * @data: ioctl data
4875 * @file_priv: DRM file info
4876 *
4877 * This function retrieves the current value for an object's property. Compared
4878 * to the connector specific ioctl this one is extended to also work on crtc and
4879 * plane objects.
4880 *
4881 * Called by the user via ioctl.
4882 *
4883 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004884 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004885 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004886int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4887 struct drm_file *file_priv)
4888{
4889 struct drm_mode_obj_get_properties *arg = data;
4890 struct drm_mode_object *obj;
4891 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004892
4893 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4894 return -EINVAL;
4895
Daniel Vetter84849902012-12-02 00:28:11 +01004896 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004897
4898 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4899 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004900 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004901 goto out;
4902 }
4903 if (!obj->properties) {
4904 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02004905 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004906 }
4907
Rob Clark88a48e22014-12-18 16:01:50 -05004908 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05004909 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4910 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4911 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03004912
Daniel Vetter1649c332016-04-22 22:10:28 +02004913out_unref:
4914 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03004915out:
Daniel Vetter84849902012-12-02 00:28:11 +01004916 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004917 return ret;
4918}
4919
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004920/**
4921 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4922 * @dev: DRM device
4923 * @data: ioctl data
4924 * @file_priv: DRM file info
4925 *
4926 * This function sets the current value for an object's property. It also calls
4927 * into a driver's ->set_property callback to update the hardware state.
4928 * Compared to the connector specific ioctl this one is extended to also work on
4929 * crtc and plane objects.
4930 *
4931 * Called by the user via ioctl.
4932 *
4933 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004934 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004935 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004936int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4937 struct drm_file *file_priv)
4938{
4939 struct drm_mode_obj_set_property *arg = data;
4940 struct drm_mode_object *arg_obj;
4941 struct drm_mode_object *prop_obj;
4942 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05004943 int i, ret = -EINVAL;
4944 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004945
4946 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4947 return -EINVAL;
4948
Daniel Vetter84849902012-12-02 00:28:11 +01004949 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004950
4951 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004952 if (!arg_obj) {
4953 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004954 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004955 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004956 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02004957 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004958
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004959 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05004960 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03004961 break;
4962
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004963 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02004964 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004965
4966 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4967 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004968 if (!prop_obj) {
4969 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02004970 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004971 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004972 property = obj_to_property(prop_obj);
4973
Rob Clark3843e712014-12-16 18:05:29 -05004974 if (!drm_property_change_valid_get(property, arg->value, &ref))
Dave Airlieb164d312016-04-27 11:10:09 +10004975 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004976
4977 switch (arg_obj->type) {
4978 case DRM_MODE_OBJECT_CONNECTOR:
4979 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4980 arg->value);
4981 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004982 case DRM_MODE_OBJECT_CRTC:
4983 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4984 break;
Rob Clark4d939142012-05-17 02:23:27 -06004985 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004986 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4987 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06004988 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004989 }
4990
Rob Clark3843e712014-12-16 18:05:29 -05004991 drm_property_change_valid_put(property, ref);
4992
Daniel Vetter1649c332016-04-22 22:10:28 +02004993out_unref:
4994 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03004995out:
Daniel Vetter84849902012-12-02 00:28:11 +01004996 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004997 return ret;
4998}
4999
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005000/**
5001 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5002 * @connector: connector to attach
5003 * @encoder: encoder to attach @connector to
5004 *
5005 * This function links up a connector to an encoder. Note that the routing
5006 * restrictions between encoders and crtcs are exposed to userspace through the
5007 * possible_clones and possible_crtcs bitmasks.
5008 *
5009 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005010 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005011 */
Dave Airlief453ba02008-11-07 14:05:41 -08005012int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5013 struct drm_encoder *encoder)
5014{
5015 int i;
5016
Thierry Redingeb47fe82015-11-16 18:19:53 +01005017 /*
5018 * In the past, drivers have attempted to model the static association
5019 * of connector to encoder in simple connector/encoder devices using a
5020 * direct assignment of connector->encoder = encoder. This connection
5021 * is a logical one and the responsibility of the core, so drivers are
5022 * expected not to mess with this.
5023 *
5024 * Note that the error return should've been enough here, but a large
5025 * majority of drivers ignores the return value, so add in a big WARN
5026 * to get people's attention.
5027 */
5028 if (WARN_ON(connector->encoder))
5029 return -EINVAL;
5030
Dave Airlief453ba02008-11-07 14:05:41 -08005031 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5032 if (connector->encoder_ids[i] == 0) {
5033 connector->encoder_ids[i] = encoder->base.id;
5034 return 0;
5035 }
5036 }
5037 return -ENOMEM;
5038}
5039EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5040
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005041/**
5042 * drm_mode_crtc_set_gamma_size - set the gamma table size
5043 * @crtc: CRTC to set the gamma table size for
5044 * @gamma_size: size of the gamma table
5045 *
5046 * Drivers which support gamma tables should set this to the supported gamma
5047 * table size when initializing the CRTC. Currently the drm core only supports a
5048 * fixed gamma table size.
5049 *
5050 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005051 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005052 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005053int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005054 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005055{
Daniel Vettercf48e292016-03-30 11:51:16 +02005056 uint16_t *r_base, *g_base, *b_base;
5057 int i;
5058
Dave Airlief453ba02008-11-07 14:05:41 -08005059 crtc->gamma_size = gamma_size;
5060
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005061 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5062 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005063 if (!crtc->gamma_store) {
5064 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005065 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005066 }
5067
Daniel Vettercf48e292016-03-30 11:51:16 +02005068 r_base = crtc->gamma_store;
5069 g_base = r_base + gamma_size;
5070 b_base = g_base + gamma_size;
5071 for (i = 0; i < gamma_size; i++) {
5072 r_base[i] = i << 8;
5073 g_base[i] = i << 8;
5074 b_base[i] = i << 8;
5075 }
5076
5077
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005078 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005079}
5080EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5081
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005082/**
5083 * drm_mode_gamma_set_ioctl - set the gamma table
5084 * @dev: DRM device
5085 * @data: ioctl data
5086 * @file_priv: DRM file info
5087 *
5088 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5089 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5090 *
5091 * Called by the user via ioctl.
5092 *
5093 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005094 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005095 */
Dave Airlief453ba02008-11-07 14:05:41 -08005096int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5097 void *data, struct drm_file *file_priv)
5098{
5099 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005100 struct drm_crtc *crtc;
5101 void *r_base, *g_base, *b_base;
5102 int size;
5103 int ret = 0;
5104
Dave Airliefb3b06c2011-02-08 13:55:21 +10005105 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5106 return -EINVAL;
5107
Daniel Vetter84849902012-12-02 00:28:11 +01005108 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005109 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5110 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005111 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005112 goto out;
5113 }
Dave Airlief453ba02008-11-07 14:05:41 -08005114
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005115 if (crtc->funcs->gamma_set == NULL) {
5116 ret = -ENOSYS;
5117 goto out;
5118 }
5119
Dave Airlief453ba02008-11-07 14:05:41 -08005120 /* memcpy into gamma store */
5121 if (crtc_lut->gamma_size != crtc->gamma_size) {
5122 ret = -EINVAL;
5123 goto out;
5124 }
5125
5126 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5127 r_base = crtc->gamma_store;
5128 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5129 ret = -EFAULT;
5130 goto out;
5131 }
5132
5133 g_base = r_base + size;
5134 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5135 ret = -EFAULT;
5136 goto out;
5137 }
5138
5139 b_base = g_base + size;
5140 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5141 ret = -EFAULT;
5142 goto out;
5143 }
5144
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02005145 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005146
5147out:
Daniel Vetter84849902012-12-02 00:28:11 +01005148 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005149 return ret;
5150
5151}
5152
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005153/**
5154 * drm_mode_gamma_get_ioctl - get the gamma table
5155 * @dev: DRM device
5156 * @data: ioctl data
5157 * @file_priv: DRM file info
5158 *
5159 * Copy the current gamma table into the storage provided. This also provides
5160 * the gamma table size the driver expects, which can be used to size the
5161 * allocated storage.
5162 *
5163 * Called by the user via ioctl.
5164 *
5165 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005166 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005167 */
Dave Airlief453ba02008-11-07 14:05:41 -08005168int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5169 void *data, struct drm_file *file_priv)
5170{
5171 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005172 struct drm_crtc *crtc;
5173 void *r_base, *g_base, *b_base;
5174 int size;
5175 int ret = 0;
5176
Dave Airliefb3b06c2011-02-08 13:55:21 +10005177 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5178 return -EINVAL;
5179
Daniel Vetter84849902012-12-02 00:28:11 +01005180 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005181 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5182 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005183 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005184 goto out;
5185 }
Dave Airlief453ba02008-11-07 14:05:41 -08005186
5187 /* memcpy into gamma store */
5188 if (crtc_lut->gamma_size != crtc->gamma_size) {
5189 ret = -EINVAL;
5190 goto out;
5191 }
5192
5193 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5194 r_base = crtc->gamma_store;
5195 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5196 ret = -EFAULT;
5197 goto out;
5198 }
5199
5200 g_base = r_base + size;
5201 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5202 ret = -EFAULT;
5203 goto out;
5204 }
5205
5206 b_base = g_base + size;
5207 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5208 ret = -EFAULT;
5209 goto out;
5210 }
5211out:
Daniel Vetter84849902012-12-02 00:28:11 +01005212 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005213 return ret;
5214}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005215
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005216/**
5217 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5218 * @dev: DRM device
5219 * @data: ioctl data
5220 * @file_priv: DRM file info
5221 *
5222 * This schedules an asynchronous update on a given CRTC, called page flip.
5223 * Optionally a drm event is generated to signal the completion of the event.
5224 * Generic drivers cannot assume that a pageflip with changed framebuffer
5225 * properties (including driver specific metadata like tiling layout) will work,
5226 * but some drivers support e.g. pixel format changes through the pageflip
5227 * ioctl.
5228 *
5229 * Called by the user via ioctl.
5230 *
5231 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005232 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005233 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005234int drm_mode_page_flip_ioctl(struct drm_device *dev,
5235 void *data, struct drm_file *file_priv)
5236{
5237 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005238 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005239 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005240 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005241 int ret = -EINVAL;
5242
5243 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5244 page_flip->reserved != 0)
5245 return -EINVAL;
5246
Keith Packard62f21042013-07-22 18:50:00 -07005247 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5248 return -EINVAL;
5249
Rob Clarka2b34e22013-10-05 16:36:52 -04005250 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5251 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005252 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005253
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005254 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005255 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005256 /* The framebuffer is currently unbound, presumably
5257 * due to a hotplug event, that userspace has not
5258 * yet discovered.
5259 */
5260 ret = -EBUSY;
5261 goto out;
5262 }
5263
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005264 if (crtc->funcs->page_flip == NULL)
5265 goto out;
5266
Daniel Vetter786b99e2012-12-02 21:53:40 +01005267 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005268 if (!fb) {
5269 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005270 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005271 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005272
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005273 if (crtc->state) {
5274 const struct drm_plane_state *state = crtc->primary->state;
5275
5276 ret = check_src_coords(state->src_x, state->src_y,
5277 state->src_w, state->src_h, fb);
5278 } else {
5279 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5280 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005281 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005282 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005283
Matt Roperf4510a22014-04-01 15:22:40 -07005284 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005285 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5286 ret = -EINVAL;
5287 goto out;
5288 }
5289
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005290 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005291 e = kzalloc(sizeof *e, GFP_KERNEL);
5292 if (!e) {
5293 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005294 goto out;
5295 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005296 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005297 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005298 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005299 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5300 if (ret) {
5301 kfree(e);
5302 goto out;
5303 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005304 }
5305
Daniel Vetter3d30a592014-07-27 13:42:42 +02005306 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005307 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005308 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005309 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5310 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005311 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005312 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005313 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005314 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005315 /* Unref only the old framebuffer. */
5316 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005317 }
5318
5319out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005320 if (fb)
5321 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005322 if (crtc->primary->old_fb)
5323 drm_framebuffer_unreference(crtc->primary->old_fb);
5324 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005325 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005326
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005327 return ret;
5328}
Chris Wilsoneb033552011-01-24 15:11:08 +00005329
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005330/**
5331 * drm_mode_config_reset - call ->reset callbacks
5332 * @dev: drm device
5333 *
5334 * This functions calls all the crtc's, encoder's and connector's ->reset
5335 * callback. Drivers can use this in e.g. their driver load or resume code to
5336 * reset hardware and software state.
5337 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005338void drm_mode_config_reset(struct drm_device *dev)
5339{
5340 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005341 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005342 struct drm_encoder *encoder;
5343 struct drm_connector *connector;
5344
Daniel Vettere4f62542015-07-09 23:44:35 +02005345 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005346 if (plane->funcs->reset)
5347 plane->funcs->reset(plane);
5348
Daniel Vettere4f62542015-07-09 23:44:35 +02005349 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005350 if (crtc->funcs->reset)
5351 crtc->funcs->reset(crtc);
5352
Daniel Vettere4f62542015-07-09 23:44:35 +02005353 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005354 if (encoder->funcs->reset)
5355 encoder->funcs->reset(encoder);
5356
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005357 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005358 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005359 if (connector->funcs->reset)
5360 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005361 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005362}
5363EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005364
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005365/**
5366 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5367 * @dev: DRM device
5368 * @data: ioctl data
5369 * @file_priv: DRM file info
5370 *
5371 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5372 * TTM or something else entirely) and returns the resulting buffer handle. This
5373 * handle can then be wrapped up into a framebuffer modeset object.
5374 *
5375 * Note that userspace is not allowed to use such objects for render
5376 * acceleration - drivers must create their own private ioctls for such a use
5377 * case.
5378 *
5379 * Called by the user via ioctl.
5380 *
5381 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005382 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005383 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005384int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5385 void *data, struct drm_file *file_priv)
5386{
5387 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005388 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005389
5390 if (!dev->driver->dumb_create)
5391 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005392 if (!args->width || !args->height || !args->bpp)
5393 return -EINVAL;
5394
5395 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005396 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005397 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005398 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005399 return -EINVAL;
5400 stride = cpp * args->width;
5401 if (args->height > 0xffffffffU / stride)
5402 return -EINVAL;
5403
5404 /* test for wrap-around */
5405 size = args->height * stride;
5406 if (PAGE_ALIGN(size) == 0)
5407 return -EINVAL;
5408
Thierry Redingf6085952014-11-03 11:14:14 +01005409 /*
5410 * handle, pitch and size are output parameters. Zero them out to
5411 * prevent drivers from accidentally using uninitialized data. Since
5412 * not all existing userspace is clearing these fields properly we
5413 * cannot reject IOCTL with garbage in them.
5414 */
5415 args->handle = 0;
5416 args->pitch = 0;
5417 args->size = 0;
5418
Dave Airlieff72145b2011-02-07 12:16:14 +10005419 return dev->driver->dumb_create(file_priv, dev, args);
5420}
5421
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005422/**
5423 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5424 * @dev: DRM device
5425 * @data: ioctl data
5426 * @file_priv: DRM file info
5427 *
5428 * Allocate an offset in the drm device node's address space to be able to
5429 * memory map a dumb buffer.
5430 *
5431 * Called by the user via ioctl.
5432 *
5433 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005434 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005435 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005436int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5437 void *data, struct drm_file *file_priv)
5438{
5439 struct drm_mode_map_dumb *args = data;
5440
5441 /* call driver ioctl to get mmap offset */
5442 if (!dev->driver->dumb_map_offset)
5443 return -ENOSYS;
5444
5445 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5446}
5447
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005448/**
5449 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5450 * @dev: DRM device
5451 * @data: ioctl data
5452 * @file_priv: DRM file info
5453 *
5454 * This destroys the userspace handle for the given dumb backing storage buffer.
5455 * Since buffer objects must be reference counted in the kernel a buffer object
5456 * won't be immediately freed if a framebuffer modeset object still uses it.
5457 *
5458 * Called by the user via ioctl.
5459 *
5460 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005461 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005462 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005463int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5464 void *data, struct drm_file *file_priv)
5465{
5466 struct drm_mode_destroy_dumb *args = data;
5467
5468 if (!dev->driver->dumb_destroy)
5469 return -ENOSYS;
5470
5471 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5472}
Dave Airlie248dbc22011-11-29 20:02:54 +00005473
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005474/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305475 * drm_rotation_simplify() - Try to simplify the rotation
5476 * @rotation: Rotation to be simplified
5477 * @supported_rotations: Supported rotations
5478 *
5479 * Attempt to simplify the rotation to a form that is supported.
5480 * Eg. if the hardware supports everything except DRM_REFLECT_X
5481 * one could call this function like this:
5482 *
5483 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5484 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5485 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5486 *
5487 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5488 * transforms the hardware supports, this function may not
5489 * be able to produce a supported transform, so the caller should
5490 * check the result afterwards.
5491 */
5492unsigned int drm_rotation_simplify(unsigned int rotation,
5493 unsigned int supported_rotations)
5494{
5495 if (rotation & ~supported_rotations) {
5496 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005497 rotation = (rotation & DRM_REFLECT_MASK) |
5498 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305499 }
5500
5501 return rotation;
5502}
5503EXPORT_SYMBOL(drm_rotation_simplify);
5504
5505/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005506 * drm_mode_config_init - initialize DRM mode_configuration structure
5507 * @dev: DRM device
5508 *
5509 * Initialize @dev's mode_config structure, used for tracking the graphics
5510 * configuration of @dev.
5511 *
5512 * Since this initializes the modeset locks, no locking is possible. Which is no
5513 * problem, since this should happen single threaded at init time. It is the
5514 * driver's problem to ensure this guarantee.
5515 *
5516 */
5517void drm_mode_config_init(struct drm_device *dev)
5518{
5519 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005520 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005521 mutex_init(&dev->mode_config.idr_mutex);
5522 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005523 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005524 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5525 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5526 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5527 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5528 INIT_LIST_HEAD(&dev->mode_config.property_list);
5529 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5530 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5531 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005532 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005533 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005534
5535 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005536 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005537 drm_modeset_unlock_all(dev);
5538
5539 /* Just to be sure */
5540 dev->mode_config.num_fb = 0;
5541 dev->mode_config.num_connector = 0;
5542 dev->mode_config.num_crtc = 0;
5543 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005544 dev->mode_config.num_overlay_plane = 0;
5545 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005546}
5547EXPORT_SYMBOL(drm_mode_config_init);
5548
5549/**
5550 * drm_mode_config_cleanup - free up DRM mode_config info
5551 * @dev: DRM device
5552 *
5553 * Free up all the connectors and CRTCs associated with this DRM device, then
5554 * free up the framebuffers and associated buffer objects.
5555 *
5556 * Note that since this /should/ happen single-threaded at driver/device
5557 * teardown time, no locking is required. It's the driver's job to ensure that
5558 * this guarantee actually holds true.
5559 *
5560 * FIXME: cleanup any dangling user buffer objects too
5561 */
5562void drm_mode_config_cleanup(struct drm_device *dev)
5563{
5564 struct drm_connector *connector, *ot;
5565 struct drm_crtc *crtc, *ct;
5566 struct drm_encoder *encoder, *enct;
5567 struct drm_framebuffer *fb, *fbt;
5568 struct drm_property *property, *pt;
5569 struct drm_property_blob *blob, *bt;
5570 struct drm_plane *plane, *plt;
5571
5572 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5573 head) {
5574 encoder->funcs->destroy(encoder);
5575 }
5576
5577 list_for_each_entry_safe(connector, ot,
5578 &dev->mode_config.connector_list, head) {
5579 connector->funcs->destroy(connector);
5580 }
5581
5582 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5583 head) {
5584 drm_property_destroy(dev, property);
5585 }
5586
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005587 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5588 head) {
5589 plane->funcs->destroy(plane);
5590 }
5591
5592 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5593 crtc->funcs->destroy(crtc);
5594 }
5595
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005596 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005597 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005598 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005599 }
5600
5601 /*
5602 * Single-threaded teardown context, so it's not required to grab the
5603 * fb_lock to protect against concurrent fb_list access. Contrary, it
5604 * would actually deadlock with the drm_framebuffer_cleanup function.
5605 *
5606 * Also, if there are any framebuffers left, that's a driver leak now,
5607 * so politely WARN about this.
5608 */
5609 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5610 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005611 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005612 }
5613
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005614 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005615 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005616 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005617 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005618}
5619EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305620
5621struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5622 unsigned int supported_rotations)
5623{
5624 static const struct drm_prop_enum_list props[] = {
5625 { DRM_ROTATE_0, "rotate-0" },
5626 { DRM_ROTATE_90, "rotate-90" },
5627 { DRM_ROTATE_180, "rotate-180" },
5628 { DRM_ROTATE_270, "rotate-270" },
5629 { DRM_REFLECT_X, "reflect-x" },
5630 { DRM_REFLECT_Y, "reflect-y" },
5631 };
5632
5633 return drm_property_create_bitmask(dev, 0, "rotation",
5634 props, ARRAY_SIZE(props),
5635 supported_rotations);
5636}
5637EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005638
5639/**
5640 * DOC: Tile group
5641 *
5642 * Tile groups are used to represent tiled monitors with a unique
5643 * integer identifier. Tiled monitors using DisplayID v1.3 have
5644 * a unique 8-byte handle, we store this in a tile group, so we
5645 * have a common identifier for all tiles in a monitor group.
5646 */
5647static void drm_tile_group_free(struct kref *kref)
5648{
5649 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5650 struct drm_device *dev = tg->dev;
5651 mutex_lock(&dev->mode_config.idr_mutex);
5652 idr_remove(&dev->mode_config.tile_idr, tg->id);
5653 mutex_unlock(&dev->mode_config.idr_mutex);
5654 kfree(tg);
5655}
5656
5657/**
5658 * drm_mode_put_tile_group - drop a reference to a tile group.
5659 * @dev: DRM device
5660 * @tg: tile group to drop reference to.
5661 *
5662 * drop reference to tile group and free if 0.
5663 */
5664void drm_mode_put_tile_group(struct drm_device *dev,
5665 struct drm_tile_group *tg)
5666{
5667 kref_put(&tg->refcount, drm_tile_group_free);
5668}
5669
5670/**
5671 * drm_mode_get_tile_group - get a reference to an existing tile group
5672 * @dev: DRM device
5673 * @topology: 8-bytes unique per monitor.
5674 *
5675 * Use the unique bytes to get a reference to an existing tile group.
5676 *
5677 * RETURNS:
5678 * tile group or NULL if not found.
5679 */
5680struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5681 char topology[8])
5682{
5683 struct drm_tile_group *tg;
5684 int id;
5685 mutex_lock(&dev->mode_config.idr_mutex);
5686 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5687 if (!memcmp(tg->group_data, topology, 8)) {
5688 if (!kref_get_unless_zero(&tg->refcount))
5689 tg = NULL;
5690 mutex_unlock(&dev->mode_config.idr_mutex);
5691 return tg;
5692 }
5693 }
5694 mutex_unlock(&dev->mode_config.idr_mutex);
5695 return NULL;
5696}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005697EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005698
5699/**
5700 * drm_mode_create_tile_group - create a tile group from a displayid description
5701 * @dev: DRM device
5702 * @topology: 8-bytes unique per monitor.
5703 *
5704 * Create a tile group for the unique monitor, and get a unique
5705 * identifier for the tile group.
5706 *
5707 * RETURNS:
5708 * new tile group or error.
5709 */
5710struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5711 char topology[8])
5712{
5713 struct drm_tile_group *tg;
5714 int ret;
5715
5716 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5717 if (!tg)
5718 return ERR_PTR(-ENOMEM);
5719
5720 kref_init(&tg->refcount);
5721 memcpy(tg->group_data, topology, 8);
5722 tg->dev = dev;
5723
5724 mutex_lock(&dev->mode_config.idr_mutex);
5725 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5726 if (ret >= 0) {
5727 tg->id = ret;
5728 } else {
5729 kfree(tg);
5730 tg = ERR_PTR(ret);
5731 }
5732
5733 mutex_unlock(&dev->mode_config.idr_mutex);
5734 return tg;
5735}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005736EXPORT_SYMBOL(drm_mode_create_tile_group);
Jyri Sarhaf8ed34a2016-06-07 15:09:14 +03005737
5738/**
5739 * drm_crtc_enable_color_mgmt - enable color management properties
5740 * @crtc: DRM CRTC
5741 * @degamma_lut_size: the size of the degamma lut (before CSC)
5742 * @has_ctm: whether to attach ctm_property for CSC matrix
5743 * @gamma_lut_size: the size of the gamma lut (after CSC)
5744 *
5745 * This function lets the driver enable the color correction
5746 * properties on a CRTC. This includes 3 degamma, csc and gamma
5747 * properties that userspace can set and 2 size properties to inform
5748 * the userspace of the lut sizes. Each of the properties are
5749 * optional. The gamma and degamma properties are only attached if
5750 * their size is not 0 and ctm_property is only attached if has_ctm is
5751 * true.
5752 */
5753void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5754 uint degamma_lut_size,
5755 bool has_ctm,
5756 uint gamma_lut_size)
5757{
5758 struct drm_device *dev = crtc->dev;
5759 struct drm_mode_config *config = &dev->mode_config;
5760
5761 if (degamma_lut_size) {
5762 drm_object_attach_property(&crtc->base,
5763 config->degamma_lut_property, 0);
5764 drm_object_attach_property(&crtc->base,
5765 config->degamma_lut_size_property,
5766 degamma_lut_size);
5767 }
5768
5769 if (has_ctm)
5770 drm_object_attach_property(&crtc->base,
5771 config->ctm_property, 0);
5772
5773 if (gamma_lut_size) {
5774 drm_object_attach_property(&crtc->base,
5775 config->gamma_lut_property, 0);
5776 drm_object_attach_property(&crtc->base,
5777 config->gamma_lut_size_property,
5778 gamma_lut_size);
5779 }
5780}
5781EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);