blob: e7c862bd2f1972103709bf21844d3e7ba61c19ed [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
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200641 INIT_LIST_HEAD(&crtc->commit_list);
642 spin_lock_init(&crtc->commit_lock);
643
Rob Clark51fd3712013-11-19 12:10:12 -0500644 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200645 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
646 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100647 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800648
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200649 if (name) {
650 va_list ap;
651
652 va_start(ap, name);
653 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
654 va_end(ap);
655 } else {
656 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
657 drm_num_crtcs(dev));
658 }
659 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000660 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200661 return -ENOMEM;
662 }
663
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300664 crtc->base.properties = &crtc->properties;
665
Rob Clark51fd3712013-11-19 12:10:12 -0500666 list_add_tail(&crtc->head, &config->crtc_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100667 crtc->index = config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200668
Matt Ropere13161a2014-04-01 15:22:38 -0700669 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700670 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700671 if (primary)
672 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700673 if (cursor)
674 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700675
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100676 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
677 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100678 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100679 }
680
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100681 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800682}
Matt Ropere13161a2014-04-01 15:22:38 -0700683EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800684
685/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000686 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800687 * @crtc: CRTC to cleanup
688 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000689 * This function cleans up @crtc and removes it from the DRM mode setting
690 * core. Note that the function does *not* free the crtc structure itself,
691 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800692 */
693void drm_crtc_cleanup(struct drm_crtc *crtc)
694{
695 struct drm_device *dev = crtc->dev;
696
Chris Wilson490d3d12016-05-27 20:05:00 +0100697 /* Note that the crtc_list is considered to be static; should we
698 * remove the drm_crtc at runtime we would have to decrement all
699 * the indices on the drm_crtc after us in the crtc_list.
700 */
701
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000702 kfree(crtc->gamma_store);
703 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800704
Rob Clark51fd3712013-11-19 12:10:12 -0500705 drm_modeset_lock_fini(&crtc->mutex);
706
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000707 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800708 list_del(&crtc->head);
709 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100710
711 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
712 if (crtc->state && crtc->funcs->atomic_destroy_state)
713 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100714
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200715 kfree(crtc->name);
716
Thierry Redinga18c0af2014-12-10 11:38:49 +0100717 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800718}
719EXPORT_SYMBOL(drm_crtc_cleanup);
720
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100721/*
Dave Airlief453ba02008-11-07 14:05:41 -0800722 * drm_mode_remove - remove and free a mode
723 * @connector: connector list to modify
724 * @mode: mode to remove
725 *
Dave Airlief453ba02008-11-07 14:05:41 -0800726 * Remove @mode from @connector's mode list, then free it.
727 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100728static void drm_mode_remove(struct drm_connector *connector,
729 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800730{
731 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100732 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800733}
Dave Airlief453ba02008-11-07 14:05:41 -0800734
735/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200736 * drm_display_info_set_bus_formats - set the supported bus formats
737 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100738 * @formats: array containing the supported bus formats
739 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200740 *
741 * Store the supported bus formats in display info structure.
742 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
743 * a full list of available formats.
744 */
745int drm_display_info_set_bus_formats(struct drm_display_info *info,
746 const u32 *formats,
747 unsigned int num_formats)
748{
749 u32 *fmts = NULL;
750
751 if (!formats && num_formats)
752 return -EINVAL;
753
754 if (formats && num_formats) {
755 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
756 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300757 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200758 return -ENOMEM;
759 }
760
761 kfree(info->bus_formats);
762 info->bus_formats = fmts;
763 info->num_bus_formats = num_formats;
764
765 return 0;
766}
767EXPORT_SYMBOL(drm_display_info_set_bus_formats);
768
769/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200770 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
771 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200772 *
773 * The kernel supports per-connector configration of its consoles through
774 * use of the video= parameter. This function parses that option and
775 * extracts the user's specified mode (or enable/disable status) for a
776 * particular connector. This is typically only used during the early fbdev
777 * setup.
778 */
779static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
780{
781 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
782 char *option = NULL;
783
784 if (fb_get_options(connector->name, &option))
785 return;
786
787 if (!drm_mode_parse_command_line_for_connector(option,
788 connector,
789 mode))
790 return;
791
792 if (mode->force) {
793 const char *s;
794
795 switch (mode->force) {
796 case DRM_FORCE_OFF:
797 s = "OFF";
798 break;
799 case DRM_FORCE_ON_DIGITAL:
800 s = "ON - dig";
801 break;
802 default:
803 case DRM_FORCE_ON:
804 s = "ON";
805 break;
806 }
807
808 DRM_INFO("forcing %s connector %s\n", connector->name, s);
809 connector->force = mode->force;
810 }
811
812 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
813 connector->name,
814 mode->xres, mode->yres,
815 mode->refresh_specified ? mode->refresh : 60,
816 mode->rb ? " reduced blanking" : "",
817 mode->margins ? " with margins" : "",
818 mode->interlace ? " interlaced" : "");
819}
820
Dave Airlieb164d312016-04-27 11:10:09 +1000821static void drm_connector_free(struct kref *kref)
822{
823 struct drm_connector *connector =
824 container_of(kref, struct drm_connector, base.refcount);
825 struct drm_device *dev = connector->dev;
826
827 drm_mode_object_unregister(dev, &connector->base);
828 connector->funcs->destroy(connector);
829}
830
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200831/**
Dave Airlief453ba02008-11-07 14:05:41 -0800832 * drm_connector_init - Init a preallocated connector
833 * @dev: DRM device
834 * @connector: the connector to init
835 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100836 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800837 *
Dave Airlief453ba02008-11-07 14:05:41 -0800838 * Initialises a preallocated connector. Connectors should be
839 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200840 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100841 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200842 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800843 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200844int drm_connector_init(struct drm_device *dev,
845 struct drm_connector *connector,
846 const struct drm_connector_funcs *funcs,
847 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800848{
Rob Clarkae16c592014-12-18 16:01:54 -0500849 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200850 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400851 struct ida *connector_ida =
852 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200853
Daniel Vetter84849902012-12-02 00:28:11 +0100854 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800855
Dave Airlieb164d312016-04-27 11:10:09 +1000856 ret = drm_mode_object_get_reg(dev, &connector->base,
857 DRM_MODE_OBJECT_CONNECTOR,
858 false, drm_connector_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200859 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300860 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200861
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300862 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800863 connector->dev = dev;
864 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100865
866 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
867 if (connector->connector_id < 0) {
868 ret = connector->connector_id;
869 goto out_put;
870 }
871
Dave Airlief453ba02008-11-07 14:05:41 -0800872 connector->connector_type = connector_type;
873 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400874 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
875 if (connector->connector_type_id < 0) {
876 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100877 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400878 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300879 connector->name =
880 kasprintf(GFP_KERNEL, "%s-%d",
881 drm_connector_enum_list[connector_type].name,
882 connector->connector_type_id);
883 if (!connector->name) {
884 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100885 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300886 }
887
Dave Airlief453ba02008-11-07 14:05:41 -0800888 INIT_LIST_HEAD(&connector->probed_modes);
889 INIT_LIST_HEAD(&connector->modes);
890 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000891 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800892
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200893 drm_connector_get_cmdline_mode(connector);
894
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100895 /* We should add connectors at the end to avoid upsetting the connector
896 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500897 list_add_tail(&connector->head, &config->connector_list);
898 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800899
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200900 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500901 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500902 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200903 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800904
Rob Clark58495562012-10-11 20:50:56 -0500905 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500906 config->dpms_property, 0);
907
908 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
909 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
910 }
Dave Airlief453ba02008-11-07 14:05:41 -0800911
Thomas Wood30f65702014-06-18 17:52:32 +0100912 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100913out_put_type_id:
914 if (ret)
915 ida_remove(connector_ida, connector->connector_type_id);
916out_put_id:
917 if (ret)
918 ida_remove(&config->connector_ida, connector->connector_id);
Jani Nikula2abdd312014-05-14 16:58:19 +0300919out_put:
920 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000921 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300922
923out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100924 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200925
926 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800927}
928EXPORT_SYMBOL(drm_connector_init);
929
930/**
931 * drm_connector_cleanup - cleans up an initialised connector
932 * @connector: connector to cleanup
933 *
Dave Airlief453ba02008-11-07 14:05:41 -0800934 * Cleans up the connector but doesn't free the object.
935 */
936void drm_connector_cleanup(struct drm_connector *connector)
937{
938 struct drm_device *dev = connector->dev;
939 struct drm_display_mode *mode, *t;
940
Chris Wilson80de3462016-06-15 13:17:49 +0100941 /* The connector should have been removed from userspace long before
942 * it is finally destroyed.
943 */
944 if (WARN_ON(connector->registered))
945 drm_connector_unregister(connector);
946
Dave Airlie40d9b042014-10-20 16:29:33 +1000947 if (connector->tile_group) {
948 drm_mode_put_tile_group(dev, connector->tile_group);
949 connector->tile_group = NULL;
950 }
951
Dave Airlief453ba02008-11-07 14:05:41 -0800952 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
953 drm_mode_remove(connector, mode);
954
955 list_for_each_entry_safe(mode, t, &connector->modes, head)
956 drm_mode_remove(connector, mode);
957
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400958 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
959 connector->connector_type_id);
960
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100961 ida_remove(&dev->mode_config.connector_ida,
962 connector->connector_id);
963
Boris Brezillonb5571e92014-07-22 12:09:10 +0200964 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000965 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300966 kfree(connector->name);
967 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800968 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000969 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100970
971 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
972 if (connector->state && connector->funcs->atomic_destroy_state)
973 connector->funcs->atomic_destroy_state(connector,
974 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100975
976 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800977}
978EXPORT_SYMBOL(drm_connector_cleanup);
979
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100980/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100981 * drm_connector_register - register a connector
982 * @connector: the connector to register
983 *
984 * Register userspace interfaces for a connector
985 *
986 * Returns:
987 * Zero on success, error code on failure.
988 */
989int drm_connector_register(struct drm_connector *connector)
990{
Thomas Wood30f65702014-06-18 17:52:32 +0100991 int ret;
992
Chris Wilson40daac62016-06-15 13:17:48 +0100993 if (connector->registered)
994 return 0;
995
Thomas Wood30f65702014-06-18 17:52:32 +0100996 ret = drm_sysfs_connector_add(connector);
997 if (ret)
998 return ret;
999
1000 ret = drm_debugfs_connector_add(connector);
1001 if (ret) {
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001002 goto err_sysfs;
1003 }
1004
1005 if (connector->funcs->late_register) {
1006 ret = connector->funcs->late_register(connector);
1007 if (ret)
1008 goto err_debugfs;
Thomas Wood30f65702014-06-18 17:52:32 +01001009 }
1010
Daniel Vetterfdf2c852016-05-06 14:55:02 +02001011 drm_mode_object_register(connector->dev, &connector->base);
1012
Chris Wilson40daac62016-06-15 13:17:48 +01001013 connector->registered = true;
Thomas Wood30f65702014-06-18 17:52:32 +01001014 return 0;
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001015
1016err_debugfs:
1017 drm_debugfs_connector_remove(connector);
1018err_sysfs:
1019 drm_sysfs_connector_remove(connector);
1020 return ret;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001021}
1022EXPORT_SYMBOL(drm_connector_register);
1023
1024/**
1025 * drm_connector_unregister - unregister a connector
1026 * @connector: the connector to unregister
1027 *
1028 * Unregister userspace interfaces for a connector
1029 */
1030void drm_connector_unregister(struct drm_connector *connector)
1031{
Chris Wilson40daac62016-06-15 13:17:48 +01001032 if (!connector->registered)
1033 return;
1034
Chris Wilsonaaf285e2016-06-15 13:17:47 +01001035 if (connector->funcs->early_unregister)
1036 connector->funcs->early_unregister(connector);
1037
Thomas Wood34ea3d32014-05-29 16:57:41 +01001038 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +01001039 drm_debugfs_connector_remove(connector);
Chris Wilson40daac62016-06-15 13:17:48 +01001040
1041 connector->registered = false;
Thomas Wood34ea3d32014-05-29 16:57:41 +01001042}
1043EXPORT_SYMBOL(drm_connector_unregister);
1044
Thomas Wood34ea3d32014-05-29 16:57:41 +01001045/**
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001046 * drm_connector_register_all - register all connectors
1047 * @dev: drm device
1048 *
1049 * This function registers all connectors in sysfs and other places so that
Chris Wilsone28cd4d2016-06-17 09:25:17 +01001050 * userspace can start to access them. drm_connector_register_all() is called
1051 * automatically from drm_dev_register() to complete the device registration,
1052 * if they don't call drm_connector_register() on each connector individually.
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +03001053 *
1054 * When a device is unplugged and should be removed from userspace access,
1055 * call drm_connector_unregister_all(), which is the inverse of this
1056 * function.
1057 *
1058 * Returns:
1059 * Zero on success, error code on failure.
1060 */
1061int drm_connector_register_all(struct drm_device *dev)
1062{
1063 struct drm_connector *connector;
1064 int ret;
1065
1066 mutex_lock(&dev->mode_config.mutex);
1067
1068 drm_for_each_connector(connector, dev) {
1069 ret = drm_connector_register(connector);
1070 if (ret)
1071 goto err;
1072 }
1073
1074 mutex_unlock(&dev->mode_config.mutex);
1075
1076 return 0;
1077
1078err:
1079 mutex_unlock(&dev->mode_config.mutex);
1080 drm_connector_unregister_all(dev);
1081 return ret;
1082}
1083EXPORT_SYMBOL(drm_connector_register_all);
1084
1085/**
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001086 * drm_connector_unregister_all - unregister connector userspace interfaces
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001087 * @dev: drm device
1088 *
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001089 * This functions unregisters all connectors from sysfs and other places so
1090 * that userspace can no longer access them. Drivers should call this as the
1091 * first step tearing down the device instace, or when the underlying
1092 * physical device disappeared (e.g. USB unplug), right before calling
1093 * drm_dev_unregister().
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001094 */
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001095void drm_connector_unregister_all(struct drm_device *dev)
Dave Airliecbc7e222012-02-20 14:16:40 +00001096{
1097 struct drm_connector *connector;
1098
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001099 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
Laurent Pinchart14ba0032016-04-21 01:21:14 +03001100 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
Thomas Wood34ea3d32014-05-29 16:57:41 +01001101 drm_connector_unregister(connector);
Dave Airliecbc7e222012-02-20 14:16:40 +00001102}
Alexey Brodkin6c87e5c2016-03-23 11:42:54 +03001103EXPORT_SYMBOL(drm_connector_unregister_all);
Dave Airliecbc7e222012-02-20 14:16:40 +00001104
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001105/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001106 * drm_encoder_init - Init a preallocated encoder
1107 * @dev: drm device
1108 * @encoder: the encoder to init
1109 * @funcs: callbacks for this encoder
1110 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001111 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001112 *
1113 * Initialises a preallocated encoder. Encoder should be
1114 * subclassed as part of driver encoder objects.
1115 *
1116 * Returns:
1117 * Zero on success, error code on failure.
1118 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001119int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +00001120 struct drm_encoder *encoder,
1121 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001122 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -08001123{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001124 int ret;
1125
Daniel Vetter84849902012-12-02 00:28:11 +01001126 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001127
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001128 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1129 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +03001130 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001131
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001132 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08001133 encoder->encoder_type = encoder_type;
1134 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +02001135 if (name) {
1136 va_list ap;
1137
1138 va_start(ap, name);
1139 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1140 va_end(ap);
1141 } else {
1142 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1143 drm_encoder_enum_list[encoder_type].name,
1144 encoder->base.id);
1145 }
Jani Nikulae5748942014-05-14 16:58:20 +03001146 if (!encoder->name) {
1147 ret = -ENOMEM;
1148 goto out_put;
1149 }
Dave Airlief453ba02008-11-07 14:05:41 -08001150
1151 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001152 encoder->index = dev->mode_config.num_encoder++;
Dave Airlief453ba02008-11-07 14:05:41 -08001153
Jani Nikulae5748942014-05-14 16:58:20 +03001154out_put:
1155 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001156 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001157
1158out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001159 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001160
1161 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001162}
1163EXPORT_SYMBOL(drm_encoder_init);
1164
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001165/**
1166 * drm_encoder_cleanup - cleans up an initialised encoder
1167 * @encoder: encoder to cleanup
1168 *
1169 * Cleans up the encoder but doesn't free the object.
1170 */
Dave Airlief453ba02008-11-07 14:05:41 -08001171void drm_encoder_cleanup(struct drm_encoder *encoder)
1172{
1173 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001174
Chris Wilson490d3d12016-05-27 20:05:00 +01001175 /* Note that the encoder_list is considered to be static; should we
1176 * remove the drm_encoder at runtime we would have to decrement all
1177 * the indices on the drm_encoder after us in the encoder_list.
1178 */
1179
Daniel Vetter84849902012-12-02 00:28:11 +01001180 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001181 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001182 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001183 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001184 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001185 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001186
1187 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001188}
1189EXPORT_SYMBOL(drm_encoder_cleanup);
1190
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001191static unsigned int drm_num_planes(struct drm_device *dev)
1192{
1193 unsigned int num = 0;
1194 struct drm_plane *tmp;
1195
1196 drm_for_each_plane(tmp, dev) {
1197 num++;
1198 }
1199
1200 return num;
1201}
1202
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001203/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001204 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001205 * @dev: DRM device
1206 * @plane: plane object to init
1207 * @possible_crtcs: bitmask of possible CRTCs
1208 * @funcs: callbacks for the new plane
1209 * @formats: array of supported formats (%DRM_FORMAT_*)
1210 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001211 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001212 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001213 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001214 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001215 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001216 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001217 * Zero on success, error code on failure.
1218 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001219int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1220 unsigned long possible_crtcs,
1221 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001222 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001223 enum drm_plane_type type,
1224 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001225{
Rob Clark6b4959f2014-12-18 16:01:53 -05001226 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001227 int ret;
1228
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001229 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1230 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001231 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001232
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001233 drm_modeset_lock_init(&plane->mutex);
1234
Rob Clark4d939142012-05-17 02:23:27 -06001235 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001236 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001237 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001238 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1239 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001240 if (!plane->format_types) {
1241 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001242 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001243 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001244 }
1245
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001246 if (name) {
1247 va_list ap;
1248
1249 va_start(ap, name);
1250 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1251 va_end(ap);
1252 } else {
1253 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1254 drm_num_planes(dev));
1255 }
1256 if (!plane->name) {
1257 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001258 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001259 return -ENOMEM;
1260 }
1261
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001262 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001263 plane->format_count = format_count;
1264 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001265 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001266
Rob Clark6b4959f2014-12-18 16:01:53 -05001267 list_add_tail(&plane->head, &config->plane_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001268 plane->index = config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001269 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001270 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001271
Rob Clark9922ab52014-04-01 20:16:57 -04001272 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001273 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001274 plane->type);
1275
Rob Clark6b4959f2014-12-18 16:01:53 -05001276 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1277 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1278 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1279 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1280 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1281 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1282 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1283 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1284 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1285 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1286 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1287 }
1288
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001289 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001290}
Matt Roperdc415ff2014-04-01 15:22:36 -07001291EXPORT_SYMBOL(drm_universal_plane_init);
1292
1293/**
1294 * drm_plane_init - Initialize a legacy plane
1295 * @dev: DRM device
1296 * @plane: plane object to init
1297 * @possible_crtcs: bitmask of possible CRTCs
1298 * @funcs: callbacks for the new plane
1299 * @formats: array of supported formats (%DRM_FORMAT_*)
1300 * @format_count: number of elements in @formats
1301 * @is_primary: plane type (primary vs overlay)
1302 *
1303 * Legacy API to initialize a DRM plane.
1304 *
1305 * New drivers should call drm_universal_plane_init() instead.
1306 *
1307 * Returns:
1308 * Zero on success, error code on failure.
1309 */
1310int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1311 unsigned long possible_crtcs,
1312 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001313 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001314 bool is_primary)
1315{
1316 enum drm_plane_type type;
1317
1318 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1319 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001320 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001321}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001322EXPORT_SYMBOL(drm_plane_init);
1323
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001324/**
1325 * drm_plane_cleanup - Clean up the core plane usage
1326 * @plane: plane to cleanup
1327 *
1328 * This function cleans up @plane and removes it from the DRM mode setting
1329 * core. Note that the function does *not* free the plane structure itself,
1330 * this is the responsibility of the caller.
1331 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001332void drm_plane_cleanup(struct drm_plane *plane)
1333{
1334 struct drm_device *dev = plane->dev;
1335
Daniel Vetter84849902012-12-02 00:28:11 +01001336 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001337 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001338 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001339
1340 BUG_ON(list_empty(&plane->head));
1341
Chris Wilson490d3d12016-05-27 20:05:00 +01001342 /* Note that the plane_list is considered to be static; should we
1343 * remove the drm_plane at runtime we would have to decrement all
1344 * the indices on the drm_plane after us in the plane_list.
1345 */
1346
Matt Roperdc415ff2014-04-01 15:22:36 -07001347 list_del(&plane->head);
1348 dev->mode_config.num_total_plane--;
1349 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1350 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001351 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001352
1353 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1354 if (plane->state && plane->funcs->atomic_destroy_state)
1355 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001356
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001357 kfree(plane->name);
1358
Thierry Redinga18c0af2014-12-10 11:38:49 +01001359 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001360}
1361EXPORT_SYMBOL(drm_plane_cleanup);
1362
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001363/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001364 * drm_plane_from_index - find the registered plane at an index
1365 * @dev: DRM device
1366 * @idx: index of registered plane to find for
1367 *
1368 * Given a plane index, return the registered plane from DRM device's
1369 * list of planes with matching index.
1370 */
1371struct drm_plane *
1372drm_plane_from_index(struct drm_device *dev, int idx)
1373{
1374 struct drm_plane *plane;
Chandra Konduruf81338a2015-04-09 17:36:21 -07001375
Chris Wilson490d3d12016-05-27 20:05:00 +01001376 drm_for_each_plane(plane, dev)
1377 if (idx == plane->index)
Chandra Konduruf81338a2015-04-09 17:36:21 -07001378 return plane;
Chris Wilson490d3d12016-05-27 20:05:00 +01001379
Chandra Konduruf81338a2015-04-09 17:36:21 -07001380 return NULL;
1381}
1382EXPORT_SYMBOL(drm_plane_from_index);
1383
1384/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001385 * drm_plane_force_disable - Forcibly disable a plane
1386 * @plane: plane to disable
1387 *
1388 * Forces the plane to be disabled.
1389 *
1390 * Used when the plane's current framebuffer is destroyed,
1391 * and when restoring fbdev mode.
1392 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001393void drm_plane_force_disable(struct drm_plane *plane)
1394{
1395 int ret;
1396
Daniel Vetter3d30a592014-07-27 13:42:42 +02001397 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001398 return;
1399
Daniel Vetter3d30a592014-07-27 13:42:42 +02001400 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001401 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001402 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001403 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001404 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001405 return;
1406 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001407 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001408 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001409 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001410 plane->fb = NULL;
1411 plane->crtc = NULL;
1412}
1413EXPORT_SYMBOL(drm_plane_force_disable);
1414
Rob Clark6b4959f2014-12-18 16:01:53 -05001415static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001416{
Rob Clark356af0e2014-12-18 16:01:52 -05001417 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001418
1419 /*
1420 * Standard properties (apply to all connectors)
1421 */
Rob Clark356af0e2014-12-18 16:01:52 -05001422 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001423 DRM_MODE_PROP_IMMUTABLE,
1424 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001425 if (!prop)
1426 return -ENOMEM;
1427 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001428
Rob Clark356af0e2014-12-18 16:01:52 -05001429 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001430 "DPMS", drm_dpms_enum_list,
1431 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001432 if (!prop)
1433 return -ENOMEM;
1434 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001435
Rob Clark356af0e2014-12-18 16:01:52 -05001436 prop = drm_property_create(dev,
1437 DRM_MODE_PROP_BLOB |
1438 DRM_MODE_PROP_IMMUTABLE,
1439 "PATH", 0);
1440 if (!prop)
1441 return -ENOMEM;
1442 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001443
Rob Clark356af0e2014-12-18 16:01:52 -05001444 prop = drm_property_create(dev,
1445 DRM_MODE_PROP_BLOB |
1446 DRM_MODE_PROP_IMMUTABLE,
1447 "TILE", 0);
1448 if (!prop)
1449 return -ENOMEM;
1450 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001451
Rob Clark6b4959f2014-12-18 16:01:53 -05001452 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001453 "type", drm_plane_type_enum_list,
1454 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001455 if (!prop)
1456 return -ENOMEM;
1457 dev->mode_config.plane_type_property = prop;
1458
1459 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1460 "SRC_X", 0, UINT_MAX);
1461 if (!prop)
1462 return -ENOMEM;
1463 dev->mode_config.prop_src_x = prop;
1464
1465 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1466 "SRC_Y", 0, UINT_MAX);
1467 if (!prop)
1468 return -ENOMEM;
1469 dev->mode_config.prop_src_y = prop;
1470
1471 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1472 "SRC_W", 0, UINT_MAX);
1473 if (!prop)
1474 return -ENOMEM;
1475 dev->mode_config.prop_src_w = prop;
1476
1477 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1478 "SRC_H", 0, UINT_MAX);
1479 if (!prop)
1480 return -ENOMEM;
1481 dev->mode_config.prop_src_h = prop;
1482
1483 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1484 "CRTC_X", INT_MIN, INT_MAX);
1485 if (!prop)
1486 return -ENOMEM;
1487 dev->mode_config.prop_crtc_x = prop;
1488
1489 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1490 "CRTC_Y", INT_MIN, INT_MAX);
1491 if (!prop)
1492 return -ENOMEM;
1493 dev->mode_config.prop_crtc_y = prop;
1494
1495 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1496 "CRTC_W", 0, INT_MAX);
1497 if (!prop)
1498 return -ENOMEM;
1499 dev->mode_config.prop_crtc_w = prop;
1500
1501 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1502 "CRTC_H", 0, INT_MAX);
1503 if (!prop)
1504 return -ENOMEM;
1505 dev->mode_config.prop_crtc_h = prop;
1506
1507 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1508 "FB_ID", DRM_MODE_OBJECT_FB);
1509 if (!prop)
1510 return -ENOMEM;
1511 dev->mode_config.prop_fb_id = prop;
1512
1513 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1514 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1515 if (!prop)
1516 return -ENOMEM;
1517 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001518
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001519 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1520 "ACTIVE");
1521 if (!prop)
1522 return -ENOMEM;
1523 dev->mode_config.prop_active = prop;
1524
Daniel Stone955f3c32015-05-25 19:11:52 +01001525 prop = drm_property_create(dev,
1526 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1527 "MODE_ID", 0);
1528 if (!prop)
1529 return -ENOMEM;
1530 dev->mode_config.prop_mode_id = prop;
1531
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001532 prop = drm_property_create(dev,
1533 DRM_MODE_PROP_BLOB,
1534 "DEGAMMA_LUT", 0);
1535 if (!prop)
1536 return -ENOMEM;
1537 dev->mode_config.degamma_lut_property = prop;
1538
1539 prop = drm_property_create_range(dev,
1540 DRM_MODE_PROP_IMMUTABLE,
1541 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1542 if (!prop)
1543 return -ENOMEM;
1544 dev->mode_config.degamma_lut_size_property = prop;
1545
1546 prop = drm_property_create(dev,
1547 DRM_MODE_PROP_BLOB,
1548 "CTM", 0);
1549 if (!prop)
1550 return -ENOMEM;
1551 dev->mode_config.ctm_property = prop;
1552
1553 prop = drm_property_create(dev,
1554 DRM_MODE_PROP_BLOB,
1555 "GAMMA_LUT", 0);
1556 if (!prop)
1557 return -ENOMEM;
1558 dev->mode_config.gamma_lut_property = prop;
1559
1560 prop = drm_property_create_range(dev,
1561 DRM_MODE_PROP_IMMUTABLE,
1562 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1563 if (!prop)
1564 return -ENOMEM;
1565 dev->mode_config.gamma_lut_size_property = prop;
1566
Rob Clark9922ab52014-04-01 20:16:57 -04001567 return 0;
1568}
1569
Dave Airlief453ba02008-11-07 14:05:41 -08001570/**
1571 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1572 * @dev: DRM device
1573 *
1574 * Called by a driver the first time a DVI-I connector is made.
1575 */
1576int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1577{
1578 struct drm_property *dvi_i_selector;
1579 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001580
1581 if (dev->mode_config.dvi_i_select_subconnector_property)
1582 return 0;
1583
1584 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001585 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001586 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001587 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001588 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001589 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1590
Sascha Hauer4a67d392012-02-06 10:58:17 +01001591 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001592 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001593 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001594 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001595 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1596
1597 return 0;
1598}
1599EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1600
1601/**
1602 * drm_create_tv_properties - create TV specific connector properties
1603 * @dev: DRM device
1604 * @num_modes: number of different TV formats (modes) supported
1605 * @modes: array of pointers to strings containing name of each format
1606 *
1607 * Called by a driver's TV initialization routine, this function creates
1608 * the TV specific connector properties for a given device. Caller is
1609 * responsible for allocating a list of format names and passing them to
1610 * this routine.
1611 */
Thierry Reding2f763312014-10-13 12:45:57 +02001612int drm_mode_create_tv_properties(struct drm_device *dev,
1613 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001614 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001615{
1616 struct drm_property *tv_selector;
1617 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001618 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001619
1620 if (dev->mode_config.tv_select_subconnector_property)
1621 return 0;
1622
1623 /*
1624 * Basic connector properties
1625 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001626 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001627 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001628 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001629 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001630 if (!tv_selector)
1631 goto nomem;
1632
Dave Airlief453ba02008-11-07 14:05:41 -08001633 dev->mode_config.tv_select_subconnector_property = tv_selector;
1634
1635 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001636 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1637 "subconnector",
1638 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001639 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001640 if (!tv_subconnector)
1641 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001642 dev->mode_config.tv_subconnector_property = tv_subconnector;
1643
1644 /*
1645 * Other, TV specific properties: margins & TV modes.
1646 */
1647 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001648 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001649 if (!dev->mode_config.tv_left_margin_property)
1650 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001651
1652 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001653 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001654 if (!dev->mode_config.tv_right_margin_property)
1655 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001656
1657 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001658 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001659 if (!dev->mode_config.tv_top_margin_property)
1660 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001661
1662 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001663 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001664 if (!dev->mode_config.tv_bottom_margin_property)
1665 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001666
1667 dev->mode_config.tv_mode_property =
1668 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1669 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001670 if (!dev->mode_config.tv_mode_property)
1671 goto nomem;
1672
Dave Airlief453ba02008-11-07 14:05:41 -08001673 for (i = 0; i < num_modes; i++)
1674 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1675 i, modes[i]);
1676
Francisco Jerezb6b79022009-08-02 04:19:20 +02001677 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001678 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001679 if (!dev->mode_config.tv_brightness_property)
1680 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001681
1682 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001683 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001684 if (!dev->mode_config.tv_contrast_property)
1685 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001686
1687 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001688 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001689 if (!dev->mode_config.tv_flicker_reduction_property)
1690 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001691
Francisco Jereza75f0232009-08-12 02:30:10 +02001692 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001693 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001694 if (!dev->mode_config.tv_overscan_property)
1695 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001696
1697 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001698 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001699 if (!dev->mode_config.tv_saturation_property)
1700 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001701
1702 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001703 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001704 if (!dev->mode_config.tv_hue_property)
1705 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001706
Dave Airlief453ba02008-11-07 14:05:41 -08001707 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001708nomem:
1709 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001710}
1711EXPORT_SYMBOL(drm_mode_create_tv_properties);
1712
1713/**
1714 * drm_mode_create_scaling_mode_property - create scaling mode property
1715 * @dev: DRM device
1716 *
1717 * Called by a driver the first time it's needed, must be attached to desired
1718 * connectors.
1719 */
1720int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1721{
1722 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001723
1724 if (dev->mode_config.scaling_mode_property)
1725 return 0;
1726
1727 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001728 drm_property_create_enum(dev, 0, "scaling mode",
1729 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001730 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001731
1732 dev->mode_config.scaling_mode_property = scaling_mode;
1733
1734 return 0;
1735}
1736EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1737
1738/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301739 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1740 * @dev: DRM device
1741 *
1742 * Called by a driver the first time it's needed, must be attached to desired
1743 * connectors.
1744 *
1745 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001746 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301747 */
1748int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1749{
1750 if (dev->mode_config.aspect_ratio_property)
1751 return 0;
1752
1753 dev->mode_config.aspect_ratio_property =
1754 drm_property_create_enum(dev, 0, "aspect ratio",
1755 drm_aspect_ratio_enum_list,
1756 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1757
1758 if (dev->mode_config.aspect_ratio_property == NULL)
1759 return -ENOMEM;
1760
1761 return 0;
1762}
1763EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1764
1765/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001766 * drm_mode_create_dirty_property - create dirty property
1767 * @dev: DRM device
1768 *
1769 * Called by a driver the first time it's needed, must be attached to desired
1770 * connectors.
1771 */
1772int drm_mode_create_dirty_info_property(struct drm_device *dev)
1773{
1774 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001775
1776 if (dev->mode_config.dirty_info_property)
1777 return 0;
1778
1779 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001780 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001781 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001782 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001783 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001784 dev->mode_config.dirty_info_property = dirty_info;
1785
1786 return 0;
1787}
1788EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1789
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001790/**
1791 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1792 * @dev: DRM device
1793 *
1794 * Create the the suggested x/y offset property for connectors.
1795 */
1796int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1797{
1798 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1799 return 0;
1800
1801 dev->mode_config.suggested_x_property =
1802 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1803
1804 dev->mode_config.suggested_y_property =
1805 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1806
1807 if (dev->mode_config.suggested_x_property == NULL ||
1808 dev->mode_config.suggested_y_property == NULL)
1809 return -ENOMEM;
1810 return 0;
1811}
1812EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1813
Dave Airlief453ba02008-11-07 14:05:41 -08001814/**
Dave Airlief453ba02008-11-07 14:05:41 -08001815 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001816 * @dev: drm device for the ioctl
1817 * @data: data pointer for the ioctl
1818 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001819 *
Dave Airlief453ba02008-11-07 14:05:41 -08001820 * Construct a set of configuration description structures and return
1821 * them to the user, including CRTC, connector and framebuffer configuration.
1822 *
1823 * Called by the user via ioctl.
1824 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001825 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001826 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001827 */
1828int drm_mode_getresources(struct drm_device *dev, void *data,
1829 struct drm_file *file_priv)
1830{
1831 struct drm_mode_card_res *card_res = data;
1832 struct list_head *lh;
1833 struct drm_framebuffer *fb;
1834 struct drm_connector *connector;
1835 struct drm_crtc *crtc;
1836 struct drm_encoder *encoder;
1837 int ret = 0;
1838 int connector_count = 0;
1839 int crtc_count = 0;
1840 int fb_count = 0;
1841 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001842 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001843 uint32_t __user *fb_id;
1844 uint32_t __user *crtc_id;
1845 uint32_t __user *connector_id;
1846 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001847
Dave Airliefb3b06c2011-02-08 13:55:21 +10001848 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1849 return -EINVAL;
1850
Dave Airlief453ba02008-11-07 14:05:41 -08001851
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001852 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001853 /*
1854 * For the non-control nodes we need to limit the list of resources
1855 * by IDs in the group list for this node
1856 */
1857 list_for_each(lh, &file_priv->fbs)
1858 fb_count++;
1859
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001860 /* handle this in 4 parts */
1861 /* FBs */
1862 if (card_res->count_fbs >= fb_count) {
1863 copied = 0;
1864 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1865 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1866 if (put_user(fb->base.id, fb_id + copied)) {
1867 mutex_unlock(&file_priv->fbs_lock);
1868 return -EFAULT;
1869 }
1870 copied++;
1871 }
1872 }
1873 card_res->count_fbs = fb_count;
1874 mutex_unlock(&file_priv->fbs_lock);
1875
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001876 /* mode_config.mutex protects the connector list against e.g. DP MST
1877 * connector hot-adding. CRTC/Plane lists are invariant. */
1878 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001879 drm_for_each_crtc(crtc, dev)
1880 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001881
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001882 drm_for_each_connector(connector, dev)
1883 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001884
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001885 drm_for_each_encoder(encoder, dev)
1886 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001887
1888 card_res->max_height = dev->mode_config.max_height;
1889 card_res->min_height = dev->mode_config.min_height;
1890 card_res->max_width = dev->mode_config.max_width;
1891 card_res->min_width = dev->mode_config.min_width;
1892
Dave Airlief453ba02008-11-07 14:05:41 -08001893 /* CRTCs */
1894 if (card_res->count_crtcs >= crtc_count) {
1895 copied = 0;
1896 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001897 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001898 if (put_user(crtc->base.id, crtc_id + copied)) {
1899 ret = -EFAULT;
1900 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001901 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001902 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001903 }
1904 }
1905 card_res->count_crtcs = crtc_count;
1906
1907 /* Encoders */
1908 if (card_res->count_encoders >= encoder_count) {
1909 copied = 0;
1910 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001911 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001912 if (put_user(encoder->base.id, encoder_id +
1913 copied)) {
1914 ret = -EFAULT;
1915 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001916 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001917 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001918 }
1919 }
1920 card_res->count_encoders = encoder_count;
1921
1922 /* Connectors */
1923 if (card_res->count_connectors >= connector_count) {
1924 copied = 0;
1925 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001926 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001927 if (put_user(connector->base.id,
1928 connector_id + copied)) {
1929 ret = -EFAULT;
1930 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001931 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001932 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001933 }
1934 }
1935 card_res->count_connectors = connector_count;
1936
Dave Airlief453ba02008-11-07 14:05:41 -08001937out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001938 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001939 return ret;
1940}
1941
1942/**
1943 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001944 * @dev: drm device for the ioctl
1945 * @data: data pointer for the ioctl
1946 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001947 *
Dave Airlief453ba02008-11-07 14:05:41 -08001948 * Construct a CRTC configuration structure to return to the user.
1949 *
1950 * Called by the user via ioctl.
1951 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001952 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001953 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001954 */
1955int drm_mode_getcrtc(struct drm_device *dev,
1956 void *data, struct drm_file *file_priv)
1957{
1958 struct drm_mode_crtc *crtc_resp = data;
1959 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001960
Dave Airliefb3b06c2011-02-08 13:55:21 +10001961 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1962 return -EINVAL;
1963
Rob Clarka2b34e22013-10-05 16:36:52 -04001964 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001965 if (!crtc)
1966 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001967
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001968 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08001969 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001970 if (crtc->primary->fb)
1971 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001972 else
1973 crtc_resp->fb_id = 0;
1974
Daniel Vetter31c946e2015-02-22 12:24:17 +01001975 if (crtc->state) {
1976 crtc_resp->x = crtc->primary->state->src_x >> 16;
1977 crtc_resp->y = crtc->primary->state->src_y >> 16;
1978 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001979 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001980 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08001981
Daniel Vetter31c946e2015-02-22 12:24:17 +01001982 } else {
1983 crtc_resp->mode_valid = 0;
1984 }
Dave Airlief453ba02008-11-07 14:05:41 -08001985 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01001986 crtc_resp->x = crtc->x;
1987 crtc_resp->y = crtc->y;
1988 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001989 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001990 crtc_resp->mode_valid = 1;
1991
1992 } else {
1993 crtc_resp->mode_valid = 0;
1994 }
Dave Airlief453ba02008-11-07 14:05:41 -08001995 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001996 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08001997
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001998 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001999}
2000
Damien Lespiau61d8e322013-09-25 16:45:22 +01002001static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2002 const struct drm_file *file_priv)
2003{
2004 /*
2005 * If user-space hasn't configured the driver to expose the stereo 3D
2006 * modes, don't expose them.
2007 */
2008 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2009 return false;
2010
2011 return true;
2012}
2013
Daniel Vetterabd69c52014-11-25 23:50:05 +01002014static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2015{
2016 /* For atomic drivers only state objects are synchronously updated and
2017 * protected by modeset locks, so check those first. */
2018 if (connector->state)
2019 return connector->state->best_encoder;
2020 return connector->encoder;
2021}
2022
Rob Clark95cbf112014-12-18 16:01:49 -05002023/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05002024static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002025 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2026 uint32_t *arg_count_props)
2027{
Rob Clark88a48e22014-12-18 16:01:50 -05002028 int props_count;
2029 int i, ret, copied;
2030
2031 props_count = obj->properties->count;
2032 if (!atomic)
2033 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05002034
2035 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05002036 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05002037 struct drm_property *prop = obj->properties->properties[i];
2038 uint64_t val;
2039
Rob Clark88a48e22014-12-18 16:01:50 -05002040 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2041 continue;
2042
Rob Clark95cbf112014-12-18 16:01:49 -05002043 ret = drm_object_property_get_value(obj, prop, &val);
2044 if (ret)
2045 return ret;
2046
2047 if (put_user(prop->base.id, prop_ptr + copied))
2048 return -EFAULT;
2049
2050 if (put_user(val, prop_values + copied))
2051 return -EFAULT;
2052
2053 copied++;
2054 }
2055 }
2056 *arg_count_props = props_count;
2057
2058 return 0;
2059}
2060
Dave Airlief453ba02008-11-07 14:05:41 -08002061/**
2062 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002063 * @dev: drm device for the ioctl
2064 * @data: data pointer for the ioctl
2065 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002066 *
Dave Airlief453ba02008-11-07 14:05:41 -08002067 * Construct a connector configuration structure to return to the user.
2068 *
2069 * Called by the user via ioctl.
2070 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002071 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002072 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002073 */
2074int drm_mode_getconnector(struct drm_device *dev, void *data,
2075 struct drm_file *file_priv)
2076{
2077 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002078 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002079 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08002080 struct drm_display_mode *mode;
2081 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002082 int encoders_count = 0;
2083 int ret = 0;
2084 int copied = 0;
2085 int i;
2086 struct drm_mode_modeinfo u_mode;
2087 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002088 uint32_t __user *encoder_ptr;
2089
Dave Airliefb3b06c2011-02-08 13:55:21 +10002090 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2091 return -EINVAL;
2092
Dave Airlief453ba02008-11-07 14:05:41 -08002093 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2094
Daniel Vetter7b240562012-12-12 00:35:33 +01002095 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002096
Dave Airlieb164d312016-04-27 11:10:09 +10002097 connector = drm_connector_lookup(dev, out_resp->connector_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002098 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002099 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03002100 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08002101 }
Dave Airlief453ba02008-11-07 14:05:41 -08002102
Thierry Reding01073b02014-12-10 13:03:38 +01002103 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2104 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002105 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002106
2107 if (out_resp->count_modes == 0) {
2108 connector->funcs->fill_modes(connector,
2109 dev->mode_config.max_width,
2110 dev->mode_config.max_height);
2111 }
2112
2113 /* delayed so we get modes regardless of pre-fill_modes state */
2114 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002115 if (drm_mode_expose_to_userspace(mode, file_priv))
2116 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002117
2118 out_resp->connector_id = connector->base.id;
2119 out_resp->connector_type = connector->connector_type;
2120 out_resp->connector_type_id = connector->connector_type_id;
2121 out_resp->mm_width = connector->display_info.width_mm;
2122 out_resp->mm_height = connector->display_info.height_mm;
2123 out_resp->subpixel = connector->display_info.subpixel_order;
2124 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002125
2126 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002127 encoder = drm_connector_get_encoder(connector);
2128 if (encoder)
2129 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002130 else
2131 out_resp->encoder_id = 0;
2132
2133 /*
2134 * This ioctl is called twice, once to determine how much space is
2135 * needed, and the 2nd time to fill it.
2136 */
2137 if ((out_resp->count_modes >= mode_count) && mode_count) {
2138 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002139 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002140 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002141 if (!drm_mode_expose_to_userspace(mode, file_priv))
2142 continue;
2143
Daniel Stone934a8a82015-05-22 13:34:48 +01002144 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002145 if (copy_to_user(mode_ptr + copied,
2146 &u_mode, sizeof(u_mode))) {
2147 ret = -EFAULT;
2148 goto out;
2149 }
2150 copied++;
2151 }
2152 }
2153 out_resp->count_modes = mode_count;
2154
Rob Clark88a48e22014-12-18 16:01:50 -05002155 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002156 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2157 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2158 &out_resp->count_props);
2159 if (ret)
2160 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002161
2162 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2163 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002164 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002165 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2166 if (connector->encoder_ids[i] != 0) {
2167 if (put_user(connector->encoder_ids[i],
2168 encoder_ptr + copied)) {
2169 ret = -EFAULT;
2170 goto out;
2171 }
2172 copied++;
2173 }
2174 }
2175 }
2176 out_resp->count_encoders = encoders_count;
2177
2178out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002179 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002180
Dave Airlieb164d312016-04-27 11:10:09 +10002181 drm_connector_unreference(connector);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002182out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002183 mutex_unlock(&dev->mode_config.mutex);
2184
Dave Airlief453ba02008-11-07 14:05:41 -08002185 return ret;
2186}
2187
Daniel Vetterabd69c52014-11-25 23:50:05 +01002188static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2189{
2190 struct drm_connector *connector;
2191 struct drm_device *dev = encoder->dev;
2192 bool uses_atomic = false;
2193
2194 /* For atomic drivers only state objects are synchronously updated and
2195 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002196 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002197 if (!connector->state)
2198 continue;
2199
2200 uses_atomic = true;
2201
2202 if (connector->state->best_encoder != encoder)
2203 continue;
2204
2205 return connector->state->crtc;
2206 }
2207
2208 /* Don't return stale data (e.g. pending async disable). */
2209 if (uses_atomic)
2210 return NULL;
2211
2212 return encoder->crtc;
2213}
2214
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002215/**
2216 * drm_mode_getencoder - get encoder configuration
2217 * @dev: drm device for the ioctl
2218 * @data: data pointer for the ioctl
2219 * @file_priv: drm file for the ioctl call
2220 *
2221 * Construct a encoder configuration structure to return to the user.
2222 *
2223 * Called by the user via ioctl.
2224 *
2225 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002226 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002227 */
Dave Airlief453ba02008-11-07 14:05:41 -08002228int drm_mode_getencoder(struct drm_device *dev, void *data,
2229 struct drm_file *file_priv)
2230{
2231 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002232 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002233 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002234
Dave Airliefb3b06c2011-02-08 13:55:21 +10002235 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2236 return -EINVAL;
2237
Rob Clarka2b34e22013-10-05 16:36:52 -04002238 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002239 if (!encoder)
2240 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002241
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002242 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002243 crtc = drm_encoder_get_crtc(encoder);
2244 if (crtc)
2245 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002246 else
2247 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002248 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2249
Dave Airlief453ba02008-11-07 14:05:41 -08002250 enc_resp->encoder_type = encoder->encoder_type;
2251 enc_resp->encoder_id = encoder->base.id;
2252 enc_resp->possible_crtcs = encoder->possible_crtcs;
2253 enc_resp->possible_clones = encoder->possible_clones;
2254
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002255 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002256}
2257
2258/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002259 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002260 * @dev: DRM device
2261 * @data: ioctl data
2262 * @file_priv: DRM file info
2263 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002264 * Construct a list of plane ids to return to the user.
2265 *
2266 * Called by the user via ioctl.
2267 *
2268 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002269 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002270 */
2271int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002272 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002273{
2274 struct drm_mode_get_plane_res *plane_resp = data;
2275 struct drm_mode_config *config;
2276 struct drm_plane *plane;
2277 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002278 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002279 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002280
2281 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2282 return -EINVAL;
2283
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002284 config = &dev->mode_config;
2285
Matt Roper681e7ec2014-04-01 15:22:42 -07002286 if (file_priv->universal_planes)
2287 num_planes = config->num_total_plane;
2288 else
2289 num_planes = config->num_overlay_plane;
2290
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002291 /*
2292 * This ioctl is called twice, once to determine how much space is
2293 * needed, and the 2nd time to fill it.
2294 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002295 if (num_planes &&
2296 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002297 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002298
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002299 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002300 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002301 /*
2302 * Unless userspace set the 'universal planes'
2303 * capability bit, only advertise overlays.
2304 */
2305 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2306 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002307 continue;
2308
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002309 if (put_user(plane->base.id, plane_ptr + copied))
2310 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002311 copied++;
2312 }
2313 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002314 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002315
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002316 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002317}
2318
2319/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002320 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002321 * @dev: DRM device
2322 * @data: ioctl data
2323 * @file_priv: DRM file info
2324 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002325 * Construct a plane configuration structure to return to the user.
2326 *
2327 * Called by the user via ioctl.
2328 *
2329 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002330 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002331 */
2332int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002333 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002334{
2335 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002336 struct drm_plane *plane;
2337 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002338
2339 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2340 return -EINVAL;
2341
Rob Clarka2b34e22013-10-05 16:36:52 -04002342 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002343 if (!plane)
2344 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002345
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002346 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002347 if (plane->crtc)
2348 plane_resp->crtc_id = plane->crtc->base.id;
2349 else
2350 plane_resp->crtc_id = 0;
2351
2352 if (plane->fb)
2353 plane_resp->fb_id = plane->fb->base.id;
2354 else
2355 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002356 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002357
2358 plane_resp->plane_id = plane->base.id;
2359 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002360 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002361
2362 /*
2363 * This ioctl is called twice, once to determine how much space is
2364 * needed, and the 2nd time to fill it.
2365 */
2366 if (plane->format_count &&
2367 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002368 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002369 if (copy_to_user(format_ptr,
2370 plane->format_types,
2371 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002372 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002373 }
2374 }
2375 plane_resp->count_format_types = plane->format_count;
2376
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002377 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002378}
2379
Laurent Pinchartead86102015-03-05 02:25:43 +02002380/**
2381 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2382 * @plane: plane to check for format support
2383 * @format: the pixel format
2384 *
2385 * Returns:
2386 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2387 * otherwise.
2388 */
2389int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2390{
2391 unsigned int i;
2392
2393 for (i = 0; i < plane->format_count; i++) {
2394 if (format == plane->format_types[i])
2395 return 0;
2396 }
2397
2398 return -EINVAL;
2399}
2400
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002401static int check_src_coords(uint32_t src_x, uint32_t src_y,
2402 uint32_t src_w, uint32_t src_h,
2403 const struct drm_framebuffer *fb)
2404{
2405 unsigned int fb_width, fb_height;
2406
2407 fb_width = fb->width << 16;
2408 fb_height = fb->height << 16;
2409
2410 /* Make sure source coordinates are inside the fb. */
2411 if (src_w > fb_width ||
2412 src_x > fb_width - src_w ||
2413 src_h > fb_height ||
2414 src_y > fb_height - src_h) {
2415 DRM_DEBUG_KMS("Invalid source coordinates "
2416 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2417 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2418 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2419 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2420 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2421 return -ENOSPC;
2422 }
2423
2424 return 0;
2425}
2426
Matt Roperb36552b2014-06-10 08:28:09 -07002427/*
2428 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002429 *
Matt Roperb36552b2014-06-10 08:28:09 -07002430 * Note that we assume an extra reference has already been taken on fb. If the
2431 * update fails, this reference will be dropped before return; if it succeeds,
2432 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002433 *
Matt Roperb36552b2014-06-10 08:28:09 -07002434 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002435 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002436static int __setplane_internal(struct drm_plane *plane,
2437 struct drm_crtc *crtc,
2438 struct drm_framebuffer *fb,
2439 int32_t crtc_x, int32_t crtc_y,
2440 uint32_t crtc_w, uint32_t crtc_h,
2441 /* src_{x,y,w,h} values are 16.16 fixed point */
2442 uint32_t src_x, uint32_t src_y,
2443 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002444{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002445 int ret = 0;
2446
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002447 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002448 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002449 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002450 ret = plane->funcs->disable_plane(plane);
2451 if (!ret) {
2452 plane->crtc = NULL;
2453 plane->fb = NULL;
2454 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002455 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002456 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002457 goto out;
2458 }
2459
Matt Roper7f994f32014-05-29 08:06:51 -07002460 /* Check whether this plane is usable on this CRTC */
2461 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2462 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2463 ret = -EINVAL;
2464 goto out;
2465 }
2466
Ville Syrjälä62443be2011-12-20 00:06:47 +02002467 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002468 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2469 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03002470 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2471 drm_get_format_name(fb->pixel_format));
Ville Syrjälä62443be2011-12-20 00:06:47 +02002472 goto out;
2473 }
2474
Matt Roper3968be92015-04-13 11:06:13 -07002475 /* Give drivers some help against integer overflows */
2476 if (crtc_w > INT_MAX ||
2477 crtc_x > INT_MAX - (int32_t) crtc_w ||
2478 crtc_h > INT_MAX ||
2479 crtc_y > INT_MAX - (int32_t) crtc_h) {
2480 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2481 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002482 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002483 goto out;
2484 }
2485
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002486 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2487 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002488 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002489
Daniel Vetter3d30a592014-07-27 13:42:42 +02002490 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002491 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002492 crtc_x, crtc_y, crtc_w, crtc_h,
2493 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002494 if (!ret) {
2495 plane->crtc = crtc;
2496 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002497 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002498 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002499 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002500 }
2501
2502out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002503 if (fb)
2504 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002505 if (plane->old_fb)
2506 drm_framebuffer_unreference(plane->old_fb);
2507 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002508
2509 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002510}
Matt Roperb36552b2014-06-10 08:28:09 -07002511
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002512static int setplane_internal(struct drm_plane *plane,
2513 struct drm_crtc *crtc,
2514 struct drm_framebuffer *fb,
2515 int32_t crtc_x, int32_t crtc_y,
2516 uint32_t crtc_w, uint32_t crtc_h,
2517 /* src_{x,y,w,h} values are 16.16 fixed point */
2518 uint32_t src_x, uint32_t src_y,
2519 uint32_t src_w, uint32_t src_h)
2520{
2521 int ret;
2522
2523 drm_modeset_lock_all(plane->dev);
2524 ret = __setplane_internal(plane, crtc, fb,
2525 crtc_x, crtc_y, crtc_w, crtc_h,
2526 src_x, src_y, src_w, src_h);
2527 drm_modeset_unlock_all(plane->dev);
2528
2529 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002530}
2531
2532/**
2533 * drm_mode_setplane - configure a plane's configuration
2534 * @dev: DRM device
2535 * @data: ioctl data*
2536 * @file_priv: DRM file info
2537 *
2538 * Set plane configuration, including placement, fb, scaling, and other factors.
2539 * Or pass a NULL fb to disable (planes may be disabled without providing a
2540 * valid crtc).
2541 *
2542 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002543 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002544 */
2545int drm_mode_setplane(struct drm_device *dev, void *data,
2546 struct drm_file *file_priv)
2547{
2548 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002549 struct drm_plane *plane;
2550 struct drm_crtc *crtc = NULL;
2551 struct drm_framebuffer *fb = NULL;
2552
2553 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2554 return -EINVAL;
2555
Matt Roperb36552b2014-06-10 08:28:09 -07002556 /*
2557 * First, find the plane, crtc, and fb objects. If not available,
2558 * we don't bother to call the driver.
2559 */
Rob Clark933f6222014-11-25 20:33:11 -05002560 plane = drm_plane_find(dev, plane_req->plane_id);
2561 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002562 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2563 plane_req->plane_id);
2564 return -ENOENT;
2565 }
Matt Roperb36552b2014-06-10 08:28:09 -07002566
2567 if (plane_req->fb_id) {
2568 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2569 if (!fb) {
2570 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2571 plane_req->fb_id);
2572 return -ENOENT;
2573 }
2574
Rob Clark933f6222014-11-25 20:33:11 -05002575 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2576 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002577 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2578 plane_req->crtc_id);
2579 return -ENOENT;
2580 }
Matt Roperb36552b2014-06-10 08:28:09 -07002581 }
2582
Matt Roper161d0dc2014-06-10 08:28:10 -07002583 /*
2584 * setplane_internal will take care of deref'ing either the old or new
2585 * framebuffer depending on success.
2586 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002587 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002588 plane_req->crtc_x, plane_req->crtc_y,
2589 plane_req->crtc_w, plane_req->crtc_h,
2590 plane_req->src_x, plane_req->src_y,
2591 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002592}
2593
2594/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002595 * drm_mode_set_config_internal - helper to call ->set_config
2596 * @set: modeset config to set
2597 *
2598 * This is a little helper to wrap internal calls to the ->set_config driver
2599 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002600 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002601 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002602 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002603 */
2604int drm_mode_set_config_internal(struct drm_mode_set *set)
2605{
2606 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002607 struct drm_framebuffer *fb;
2608 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002609 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002610
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002611 /*
2612 * NOTE: ->set_config can also disable other crtcs (if we steal all
2613 * connectors from it), hence we need to refcount the fbs across all
2614 * crtcs. Atomic modeset will have saner semantics ...
2615 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002616 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002617 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002618
Daniel Vetterb0d12322012-12-11 01:07:12 +01002619 fb = set->fb;
2620
2621 ret = crtc->funcs->set_config(set);
2622 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002623 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002624 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002625 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002626
Daniel Vettere4f62542015-07-09 23:44:35 +02002627 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002628 if (tmp->primary->fb)
2629 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002630 if (tmp->primary->old_fb)
2631 drm_framebuffer_unreference(tmp->primary->old_fb);
2632 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002633 }
2634
2635 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002636}
2637EXPORT_SYMBOL(drm_mode_set_config_internal);
2638
Matt Roperaf936292014-04-01 15:22:34 -07002639/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002640 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2641 * @mode: mode to query
2642 * @hdisplay: hdisplay value to fill in
2643 * @vdisplay: vdisplay value to fill in
2644 *
2645 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2646 * the appropriate layout.
2647 */
2648void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2649 int *hdisplay, int *vdisplay)
2650{
2651 struct drm_display_mode adjusted;
2652
2653 drm_mode_copy(&adjusted, mode);
2654 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2655 *hdisplay = adjusted.crtc_hdisplay;
2656 *vdisplay = adjusted.crtc_vdisplay;
2657}
2658EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2659
2660/**
Matt Roperaf936292014-04-01 15:22:34 -07002661 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2662 * CRTC viewport
2663 * @crtc: CRTC that framebuffer will be displayed on
2664 * @x: x panning
2665 * @y: y panning
2666 * @mode: mode that framebuffer will be displayed under
2667 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002668 */
Matt Roperaf936292014-04-01 15:22:34 -07002669int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2670 int x, int y,
2671 const struct drm_display_mode *mode,
2672 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002673
2674{
2675 int hdisplay, vdisplay;
2676
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002677 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002678
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002679 if (crtc->state &&
2680 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2681 BIT(DRM_ROTATE_270)))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002682 swap(hdisplay, vdisplay);
2683
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002684 return check_src_coords(x << 16, y << 16,
2685 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002686}
Matt Roperaf936292014-04-01 15:22:34 -07002687EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002688
Daniel Vetter2d13b672012-12-11 13:47:23 +01002689/**
Dave Airlief453ba02008-11-07 14:05:41 -08002690 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002691 * @dev: drm device for the ioctl
2692 * @data: data pointer for the ioctl
2693 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002694 *
Dave Airlief453ba02008-11-07 14:05:41 -08002695 * Build a new CRTC configuration based on user request.
2696 *
2697 * Called by the user via ioctl.
2698 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002699 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002700 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002701 */
2702int drm_mode_setcrtc(struct drm_device *dev, void *data,
2703 struct drm_file *file_priv)
2704{
2705 struct drm_mode_config *config = &dev->mode_config;
2706 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002707 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002708 struct drm_connector **connector_set = NULL, *connector;
2709 struct drm_framebuffer *fb = NULL;
2710 struct drm_display_mode *mode = NULL;
2711 struct drm_mode_set set;
2712 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002713 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002714 int i;
2715
Dave Airliefb3b06c2011-02-08 13:55:21 +10002716 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2717 return -EINVAL;
2718
Zhao Junwang01447e92015-07-07 17:08:35 +08002719 /*
2720 * Universal plane src offsets are only 16.16, prevent havoc for
2721 * drivers using universal plane code internally.
2722 */
2723 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002724 return -ERANGE;
2725
Daniel Vetter84849902012-12-02 00:28:11 +01002726 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002727 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2728 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002729 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002730 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002731 goto out;
2732 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002733 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002734
2735 if (crtc_req->mode_valid) {
2736 /* If we have a mode we need a framebuffer. */
2737 /* If we pass -1, set the mode with the currently bound fb */
2738 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002739 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002740 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2741 ret = -EINVAL;
2742 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002743 }
Matt Roperf4510a22014-04-01 15:22:40 -07002744 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002745 /* Make refcounting symmetric with the lookup path. */
2746 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002747 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002748 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2749 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002750 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2751 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002752 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002753 goto out;
2754 }
Dave Airlief453ba02008-11-07 14:05:41 -08002755 }
2756
2757 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002758 if (!mode) {
2759 ret = -ENOMEM;
2760 goto out;
2761 }
2762
Daniel Stone934a8a82015-05-22 13:34:48 +01002763 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002764 if (ret) {
2765 DRM_DEBUG_KMS("Invalid mode\n");
2766 goto out;
2767 }
2768
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002769 /*
2770 * Check whether the primary plane supports the fb pixel format.
2771 * Drivers not implementing the universal planes API use a
2772 * default formats list provided by the DRM core which doesn't
2773 * match real hardware capabilities. Skip the check in that
2774 * case.
2775 */
2776 if (!crtc->primary->format_default) {
2777 ret = drm_plane_check_pixel_format(crtc->primary,
2778 fb->pixel_format);
2779 if (ret) {
2780 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2781 drm_get_format_name(fb->pixel_format));
2782 goto out;
2783 }
2784 }
2785
Damien Lespiauc11e9282013-09-25 16:45:30 +01002786 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2787 mode, fb);
2788 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002789 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002790
Dave Airlief453ba02008-11-07 14:05:41 -08002791 }
2792
2793 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002794 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002795 ret = -EINVAL;
2796 goto out;
2797 }
2798
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002799 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002800 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002801 crtc_req->count_connectors);
2802 ret = -EINVAL;
2803 goto out;
2804 }
2805
2806 if (crtc_req->count_connectors > 0) {
2807 u32 out_id;
2808
2809 /* Avoid unbounded kernel memory allocation */
2810 if (crtc_req->count_connectors > config->num_connector) {
2811 ret = -EINVAL;
2812 goto out;
2813 }
2814
Thierry Reding2f6c5382014-12-10 13:03:36 +01002815 connector_set = kmalloc_array(crtc_req->count_connectors,
2816 sizeof(struct drm_connector *),
2817 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002818 if (!connector_set) {
2819 ret = -ENOMEM;
2820 goto out;
2821 }
2822
2823 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +10002824 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002825 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002826 if (get_user(out_id, &set_connectors_ptr[i])) {
2827 ret = -EFAULT;
2828 goto out;
2829 }
2830
Dave Airlieb164d312016-04-27 11:10:09 +10002831 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002832 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002833 DRM_DEBUG_KMS("Connector id %d unknown\n",
2834 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002835 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002836 goto out;
2837 }
Jerome Glisse94401062010-07-15 15:43:25 -04002838 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2839 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002840 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002841
2842 connector_set[i] = connector;
2843 }
2844 }
2845
2846 set.crtc = crtc;
2847 set.x = crtc_req->x;
2848 set.y = crtc_req->y;
2849 set.mode = mode;
2850 set.connectors = connector_set;
2851 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002852 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002853 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002854
2855out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002856 if (fb)
2857 drm_framebuffer_unreference(fb);
2858
Dave Airlieb164d312016-04-27 11:10:09 +10002859 if (connector_set) {
2860 for (i = 0; i < crtc_req->count_connectors; i++) {
2861 if (connector_set[i])
2862 drm_connector_unreference(connector_set[i]);
2863 }
2864 }
Dave Airlief453ba02008-11-07 14:05:41 -08002865 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002866 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002867 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002868 return ret;
2869}
2870
Matt Roper161d0dc2014-06-10 08:28:10 -07002871/**
2872 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2873 * universal plane handler call
2874 * @crtc: crtc to update cursor for
2875 * @req: data pointer for the ioctl
2876 * @file_priv: drm file for the ioctl call
2877 *
2878 * Legacy cursor ioctl's work directly with driver buffer handles. To
2879 * translate legacy ioctl calls into universal plane handler calls, we need to
2880 * wrap the native buffer handle in a drm_framebuffer.
2881 *
2882 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2883 * buffer with a pitch of 4*width; the universal plane interface should be used
2884 * directly in cases where the hardware can support other buffer settings and
2885 * userspace wants to make use of these capabilities.
2886 *
2887 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002888 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002889 */
2890static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2891 struct drm_mode_cursor2 *req,
2892 struct drm_file *file_priv)
2893{
2894 struct drm_device *dev = crtc->dev;
2895 struct drm_framebuffer *fb = NULL;
2896 struct drm_mode_fb_cmd2 fbreq = {
2897 .width = req->width,
2898 .height = req->height,
2899 .pixel_format = DRM_FORMAT_ARGB8888,
2900 .pitches = { req->width * 4 },
2901 .handles = { req->handle },
2902 };
2903 int32_t crtc_x, crtc_y;
2904 uint32_t crtc_w = 0, crtc_h = 0;
2905 uint32_t src_w = 0, src_h = 0;
2906 int ret = 0;
2907
2908 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002909 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002910
2911 /*
2912 * Obtain fb we'll be using (either new or existing) and take an extra
2913 * reference to it if fb != null. setplane will take care of dropping
2914 * the reference if the plane update fails.
2915 */
2916 if (req->flags & DRM_MODE_CURSOR_BO) {
2917 if (req->handle) {
Chris Wilson9a6f5132015-02-25 13:45:26 +00002918 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002919 if (IS_ERR(fb)) {
2920 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2921 return PTR_ERR(fb);
2922 }
Gerd Hoffmanndd546592016-05-31 08:54:52 +02002923 fb->hot_x = req->hot_x;
2924 fb->hot_y = req->hot_y;
Matt Roper161d0dc2014-06-10 08:28:10 -07002925 } else {
2926 fb = NULL;
2927 }
2928 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002929 fb = crtc->cursor->fb;
2930 if (fb)
2931 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002932 }
2933
2934 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2935 crtc_x = req->x;
2936 crtc_y = req->y;
2937 } else {
2938 crtc_x = crtc->cursor_x;
2939 crtc_y = crtc->cursor_y;
2940 }
2941
2942 if (fb) {
2943 crtc_w = fb->width;
2944 crtc_h = fb->height;
2945 src_w = fb->width << 16;
2946 src_h = fb->height << 16;
2947 }
2948
2949 /*
2950 * setplane_internal will take care of deref'ing either the old or new
2951 * framebuffer depending on success.
2952 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002953 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002954 crtc_x, crtc_y, crtc_w, crtc_h,
2955 0, 0, src_w, src_h);
2956
2957 /* Update successful; save new cursor position, if necessary */
2958 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2959 crtc->cursor_x = req->x;
2960 crtc->cursor_y = req->y;
2961 }
2962
2963 return ret;
2964}
2965
Dave Airlie4c813d42013-06-20 11:48:52 +10002966static int drm_mode_cursor_common(struct drm_device *dev,
2967 struct drm_mode_cursor2 *req,
2968 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002969{
Dave Airlief453ba02008-11-07 14:05:41 -08002970 struct drm_crtc *crtc;
2971 int ret = 0;
2972
Dave Airliefb3b06c2011-02-08 13:55:21 +10002973 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2974 return -EINVAL;
2975
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002976 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002977 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002978
Rob Clarka2b34e22013-10-05 16:36:52 -04002979 crtc = drm_crtc_find(dev, req->crtc_id);
2980 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002981 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002982 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002983 }
Dave Airlief453ba02008-11-07 14:05:41 -08002984
Matt Roper161d0dc2014-06-10 08:28:10 -07002985 /*
2986 * If this crtc has a universal cursor plane, call that plane's update
2987 * handler rather than using legacy cursor handlers.
2988 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002989 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002990 if (crtc->cursor) {
2991 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2992 goto out;
2993 }
2994
Dave Airlief453ba02008-11-07 14:05:41 -08002995 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002996 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002997 ret = -ENXIO;
2998 goto out;
2999 }
3000 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003001 if (crtc->funcs->cursor_set2)
3002 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3003 req->width, req->height, req->hot_x, req->hot_y);
3004 else
3005 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3006 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08003007 }
3008
3009 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3010 if (crtc->funcs->cursor_move) {
3011 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3012 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08003013 ret = -EFAULT;
3014 goto out;
3015 }
3016 }
3017out:
Daniel Vetterd059f652014-07-25 18:07:40 +02003018 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01003019
Dave Airlief453ba02008-11-07 14:05:41 -08003020 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10003021
3022}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003023
3024
3025/**
3026 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3027 * @dev: drm device for the ioctl
3028 * @data: data pointer for the ioctl
3029 * @file_priv: drm file for the ioctl call
3030 *
3031 * Set the cursor configuration based on user request.
3032 *
3033 * Called by the user via ioctl.
3034 *
3035 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003036 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003037 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003038int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003039 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10003040{
3041 struct drm_mode_cursor *req = data;
3042 struct drm_mode_cursor2 new_req;
3043
3044 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3045 new_req.hot_x = new_req.hot_y = 0;
3046
3047 return drm_mode_cursor_common(dev, &new_req, file_priv);
3048}
3049
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003050/**
3051 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3052 * @dev: drm device for the ioctl
3053 * @data: data pointer for the ioctl
3054 * @file_priv: drm file for the ioctl call
3055 *
3056 * Set the cursor configuration based on user request. This implements the 2nd
3057 * version of the cursor ioctl, which allows userspace to additionally specify
3058 * the hotspot of the pointer.
3059 *
3060 * Called by the user via ioctl.
3061 *
3062 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003063 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003064 */
Dave Airlie4c813d42013-06-20 11:48:52 +10003065int drm_mode_cursor2_ioctl(struct drm_device *dev,
3066 void *data, struct drm_file *file_priv)
3067{
3068 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003069
Dave Airlie4c813d42013-06-20 11:48:52 +10003070 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003071}
3072
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003073/**
3074 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3075 * @bpp: bits per pixels
3076 * @depth: bit depth per pixel
3077 *
3078 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3079 * Useful in fbdev emulation code, since that deals in those values.
3080 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003081uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3082{
3083 uint32_t fmt;
3084
3085 switch (bpp) {
3086 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02003087 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003088 break;
3089 case 16:
3090 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003091 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003092 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003093 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003094 break;
3095 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003096 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003097 break;
3098 case 32:
3099 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003100 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003101 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003102 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003103 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003104 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003105 break;
3106 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003107 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3108 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003109 break;
3110 }
3111
3112 return fmt;
3113}
3114EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3115
Dave Airlief453ba02008-11-07 14:05:41 -08003116/**
3117 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003118 * @dev: drm device for the ioctl
3119 * @data: data pointer for the ioctl
3120 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003121 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003122 * Add a new FB to the specified CRTC, given a user request. This is the
Chuck Ebbert209f5522014-10-08 11:37:20 -05003123 * original addfb ioctl which only supported RGB formats.
Dave Airlief453ba02008-11-07 14:05:41 -08003124 *
3125 * Called by the user via ioctl.
3126 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003127 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003128 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003129 */
3130int drm_mode_addfb(struct drm_device *dev,
3131 void *data, struct drm_file *file_priv)
3132{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003133 struct drm_mode_fb_cmd *or = data;
3134 struct drm_mode_fb_cmd2 r = {};
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003135 int ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003136
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003137 /* convert to new format and call new ioctl */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003138 r.fb_id = or->fb_id;
3139 r.width = or->width;
3140 r.height = or->height;
3141 r.pitches[0] = or->pitch;
3142 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3143 r.handles[0] = or->handle;
3144
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003145 ret = drm_mode_addfb2(dev, &r, file_priv);
3146 if (ret)
3147 return ret;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003148
Chuck Ebbert228f2cb2014-10-08 11:40:34 -05003149 or->fb_id = r.fb_id;
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003150
Daniel Vetterbaf698b2014-11-12 11:59:47 +01003151 return 0;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003152}
3153
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003154static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02003155{
3156 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3157
3158 switch (format) {
3159 case DRM_FORMAT_C8:
3160 case DRM_FORMAT_RGB332:
3161 case DRM_FORMAT_BGR233:
3162 case DRM_FORMAT_XRGB4444:
3163 case DRM_FORMAT_XBGR4444:
3164 case DRM_FORMAT_RGBX4444:
3165 case DRM_FORMAT_BGRX4444:
3166 case DRM_FORMAT_ARGB4444:
3167 case DRM_FORMAT_ABGR4444:
3168 case DRM_FORMAT_RGBA4444:
3169 case DRM_FORMAT_BGRA4444:
3170 case DRM_FORMAT_XRGB1555:
3171 case DRM_FORMAT_XBGR1555:
3172 case DRM_FORMAT_RGBX5551:
3173 case DRM_FORMAT_BGRX5551:
3174 case DRM_FORMAT_ARGB1555:
3175 case DRM_FORMAT_ABGR1555:
3176 case DRM_FORMAT_RGBA5551:
3177 case DRM_FORMAT_BGRA5551:
3178 case DRM_FORMAT_RGB565:
3179 case DRM_FORMAT_BGR565:
3180 case DRM_FORMAT_RGB888:
3181 case DRM_FORMAT_BGR888:
3182 case DRM_FORMAT_XRGB8888:
3183 case DRM_FORMAT_XBGR8888:
3184 case DRM_FORMAT_RGBX8888:
3185 case DRM_FORMAT_BGRX8888:
3186 case DRM_FORMAT_ARGB8888:
3187 case DRM_FORMAT_ABGR8888:
3188 case DRM_FORMAT_RGBA8888:
3189 case DRM_FORMAT_BGRA8888:
3190 case DRM_FORMAT_XRGB2101010:
3191 case DRM_FORMAT_XBGR2101010:
3192 case DRM_FORMAT_RGBX1010102:
3193 case DRM_FORMAT_BGRX1010102:
3194 case DRM_FORMAT_ARGB2101010:
3195 case DRM_FORMAT_ABGR2101010:
3196 case DRM_FORMAT_RGBA1010102:
3197 case DRM_FORMAT_BGRA1010102:
3198 case DRM_FORMAT_YUYV:
3199 case DRM_FORMAT_YVYU:
3200 case DRM_FORMAT_UYVY:
3201 case DRM_FORMAT_VYUY:
3202 case DRM_FORMAT_AYUV:
3203 case DRM_FORMAT_NV12:
3204 case DRM_FORMAT_NV21:
3205 case DRM_FORMAT_NV16:
3206 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003207 case DRM_FORMAT_NV24:
3208 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02003209 case DRM_FORMAT_YUV410:
3210 case DRM_FORMAT_YVU410:
3211 case DRM_FORMAT_YUV411:
3212 case DRM_FORMAT_YVU411:
3213 case DRM_FORMAT_YUV420:
3214 case DRM_FORMAT_YVU420:
3215 case DRM_FORMAT_YUV422:
3216 case DRM_FORMAT_YVU422:
3217 case DRM_FORMAT_YUV444:
3218 case DRM_FORMAT_YVU444:
3219 return 0;
3220 default:
Ville Syrjälä23c453a2013-10-15 21:06:51 +03003221 DRM_DEBUG_KMS("invalid pixel format %s\n",
3222 drm_get_format_name(r->pixel_format));
Ville Syrjälä935b5972011-12-20 00:06:48 +02003223 return -EINVAL;
3224 }
3225}
3226
Ville Syrjäläcff91b62012-05-24 20:54:00 +03003227static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003228{
3229 int ret, hsub, vsub, num_planes, i;
3230
3231 ret = format_check(r);
3232 if (ret) {
Ville Syrjälä6ba6d032013-06-10 11:15:10 +03003233 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3234 drm_get_format_name(r->pixel_format));
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003235 return ret;
3236 }
3237
3238 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3239 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3240 num_planes = drm_format_num_planes(r->pixel_format);
3241
3242 if (r->width == 0 || r->width % hsub) {
Chuck Ebbert209f5522014-10-08 11:37:20 -05003243 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003244 return -EINVAL;
3245 }
3246
3247 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003248 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003249 return -EINVAL;
3250 }
3251
3252 for (i = 0; i < num_planes; i++) {
3253 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003254 unsigned int height = r->height / (i != 0 ? vsub : 1);
3255 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003256
3257 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003258 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003259 return -EINVAL;
3260 }
3261
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00003262 if ((uint64_t) width * cpp > UINT_MAX)
3263 return -ERANGE;
3264
3265 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3266 return -ERANGE;
3267
3268 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01003269 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003270 return -EINVAL;
3271 }
Rob Clarke3eb3252015-02-05 14:41:52 +00003272
3273 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3274 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3275 r->modifier[i], i);
3276 return -EINVAL;
3277 }
Rob Clark570655b2015-01-30 20:18:11 +05303278
3279 /* modifier specific checks: */
3280 switch (r->modifier[i]) {
3281 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3282 /* NOTE: the pitch restriction may be lifted later if it turns
3283 * out that no hw has this restriction:
3284 */
3285 if (r->pixel_format != DRM_FORMAT_NV12 ||
3286 width % 128 || height % 32 ||
3287 r->pitches[i] % 128) {
3288 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3289 return -EINVAL;
3290 }
3291 break;
3292
3293 default:
3294 break;
3295 }
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003296 }
3297
Daniel Vetterbbe16a42015-05-20 16:53:53 +02003298 for (i = num_planes; i < 4; i++) {
3299 if (r->modifier[i]) {
3300 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3301 return -EINVAL;
3302 }
3303
3304 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3305 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3306 continue;
3307
3308 if (r->handles[i]) {
3309 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3310 return -EINVAL;
3311 }
3312
3313 if (r->pitches[i]) {
3314 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3315 return -EINVAL;
3316 }
3317
3318 if (r->offsets[i]) {
3319 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3320 return -EINVAL;
3321 }
3322 }
3323
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03003324 return 0;
3325}
3326
Chris Wilson9a6f5132015-02-25 13:45:26 +00003327static struct drm_framebuffer *
3328internal_framebuffer_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02003329 const struct drm_mode_fb_cmd2 *r,
Chris Wilson9a6f5132015-02-25 13:45:26 +00003330 struct drm_file *file_priv)
Matt Roperc394c2b2014-06-10 08:28:08 -07003331{
3332 struct drm_mode_config *config = &dev->mode_config;
3333 struct drm_framebuffer *fb;
3334 int ret;
3335
Rob Clarke3eb3252015-02-05 14:41:52 +00003336 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
Matt Roperc394c2b2014-06-10 08:28:08 -07003337 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3338 return ERR_PTR(-EINVAL);
3339 }
3340
3341 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3342 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3343 r->width, config->min_width, config->max_width);
3344 return ERR_PTR(-EINVAL);
3345 }
3346 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3347 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3348 r->height, config->min_height, config->max_height);
3349 return ERR_PTR(-EINVAL);
3350 }
3351
Rob Clarke3eb3252015-02-05 14:41:52 +00003352 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3353 !dev->mode_config.allow_fb_modifiers) {
3354 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3355 return ERR_PTR(-EINVAL);
3356 }
3357
Matt Roperc394c2b2014-06-10 08:28:08 -07003358 ret = framebuffer_check(r);
3359 if (ret)
3360 return ERR_PTR(ret);
3361
3362 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3363 if (IS_ERR(fb)) {
3364 DRM_DEBUG_KMS("could not create framebuffer\n");
3365 return fb;
3366 }
3367
Matt Roperc394c2b2014-06-10 08:28:08 -07003368 return fb;
3369}
3370
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003371/**
3372 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003373 * @dev: drm device for the ioctl
3374 * @data: data pointer for the ioctl
3375 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003376 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003377 * Add a new FB to the specified CRTC, given a user request with format. This is
3378 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3379 * and uses fourcc codes as pixel format specifiers.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003380 *
3381 * Called by the user via ioctl.
3382 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003383 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003384 * Zero on success, negative errno on failure.
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003385 */
3386int drm_mode_addfb2(struct drm_device *dev,
3387 void *data, struct drm_file *file_priv)
3388{
Chris Wilson9a6f5132015-02-25 13:45:26 +00003389 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003390 struct drm_framebuffer *fb;
Dave Airlief453ba02008-11-07 14:05:41 -08003391
Dave Airliefb3b06c2011-02-08 13:55:21 +10003392 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3393 return -EINVAL;
3394
Chris Wilson9a6f5132015-02-25 13:45:26 +00003395 fb = internal_framebuffer_create(dev, r, file_priv);
Matt Roperc394c2b2014-06-10 08:28:08 -07003396 if (IS_ERR(fb))
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003397 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08003398
Chris Wilson9a6f5132015-02-25 13:45:26 +00003399 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003400 r->fb_id = fb->base.id;
Dave Airliec7e1c592016-04-15 15:10:39 +10003401
3402 /* Transfer ownership to the filp for reaping on close */
3403 mutex_lock(&file_priv->fbs_lock);
Chris Wilson9a6f5132015-02-25 13:45:26 +00003404 list_add(&fb->filp_head, &file_priv->fbs);
3405 mutex_unlock(&file_priv->fbs_lock);
3406
Matt Roperc394c2b2014-06-10 08:28:08 -07003407 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003408}
3409
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003410struct drm_mode_rmfb_work {
3411 struct work_struct work;
3412 struct list_head fbs;
3413};
3414
3415static void drm_mode_rmfb_work_fn(struct work_struct *w)
3416{
3417 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3418
3419 while (!list_empty(&arg->fbs)) {
3420 struct drm_framebuffer *fb =
3421 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3422
3423 list_del_init(&fb->filp_head);
3424 drm_framebuffer_remove(fb);
3425 }
3426}
3427
Dave Airlief453ba02008-11-07 14:05:41 -08003428/**
3429 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003430 * @dev: drm device for the ioctl
3431 * @data: data pointer for the ioctl
3432 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003433 *
Dave Airlief453ba02008-11-07 14:05:41 -08003434 * Remove the FB specified by the user.
3435 *
3436 * Called by the user via ioctl.
3437 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003438 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003439 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003440 */
3441int drm_mode_rmfb(struct drm_device *dev,
3442 void *data, struct drm_file *file_priv)
3443{
Dave Airlief453ba02008-11-07 14:05:41 -08003444 struct drm_framebuffer *fb = NULL;
3445 struct drm_framebuffer *fbl = NULL;
3446 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003447 int found = 0;
3448
Dave Airliefb3b06c2011-02-08 13:55:21 +10003449 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3450 return -EINVAL;
3451
Dave Airlie72fe90b2016-04-15 15:10:40 +10003452 fb = drm_framebuffer_lookup(dev, *id);
3453 if (!fb)
3454 return -ENOENT;
3455
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003456 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003457 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3458 if (fb == fbl)
3459 found = 1;
Dave Airlie72fe90b2016-04-15 15:10:40 +10003460 if (!found) {
3461 mutex_unlock(&file_priv->fbs_lock);
3462 goto fail_unref;
3463 }
Daniel Vetter2b677e82012-12-10 21:16:05 +01003464
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003465 list_del_init(&fb->filp_head);
3466 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08003467
Dave Airlie72fe90b2016-04-15 15:10:40 +10003468 /* drop the reference we picked up in framebuffer lookup */
Maarten Lankhorst13803132015-09-09 16:40:56 +02003469 drm_framebuffer_unreference(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01003470
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003471 /*
3472 * we now own the reference that was stored in the fbs list
3473 *
3474 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3475 * so run this in a separate stack as there's no way to correctly
3476 * handle this after the fb is already removed from the lookup table.
3477 */
3478 if (drm_framebuffer_read_refcount(fb) > 1) {
3479 struct drm_mode_rmfb_work arg;
3480
3481 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3482 INIT_LIST_HEAD(&arg.fbs);
3483 list_add_tail(&fb->filp_head, &arg.fbs);
3484
3485 schedule_work(&arg.work);
3486 flush_work(&arg.work);
3487 destroy_work_on_stack(&arg.work);
3488 } else
3489 drm_framebuffer_unreference(fb);
3490
Daniel Vetter2b677e82012-12-10 21:16:05 +01003491 return 0;
3492
Dave Airlie72fe90b2016-04-15 15:10:40 +10003493fail_unref:
3494 drm_framebuffer_unreference(fb);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003495 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003496}
3497
3498/**
3499 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003500 * @dev: drm device for the ioctl
3501 * @data: data pointer for the ioctl
3502 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08003503 *
Dave Airlief453ba02008-11-07 14:05:41 -08003504 * Lookup the FB given its ID and return info about it.
3505 *
3506 * Called by the user via ioctl.
3507 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003508 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003509 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003510 */
3511int drm_mode_getfb(struct drm_device *dev,
3512 void *data, struct drm_file *file_priv)
3513{
3514 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08003515 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003516 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003517
Dave Airliefb3b06c2011-02-08 13:55:21 +10003518 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3519 return -EINVAL;
3520
Daniel Vetter786b99e2012-12-02 21:53:40 +01003521 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003522 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003523 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003524
3525 r->height = fb->height;
3526 r->width = fb->width;
3527 r->depth = fb->depth;
3528 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02003529 r->pitch = fb->pitches[0];
David Herrmann101b96f2013-08-26 15:16:49 +02003530 if (fb->funcs->create_handle) {
Thomas Hellstrom09f308f2014-03-13 10:00:42 +01003531 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
Thomas Hellstrom43683052014-03-13 11:07:44 +01003532 drm_is_control_client(file_priv)) {
David Herrmann101b96f2013-08-26 15:16:49 +02003533 ret = fb->funcs->create_handle(fb, file_priv,
3534 &r->handle);
3535 } else {
3536 /* GET_FB() is an unprivileged ioctl so we must not
3537 * return a buffer-handle to non-master processes! For
3538 * backwards-compatibility reasons, we cannot make
3539 * GET_FB() privileged, so just return an invalid handle
3540 * for non-masters. */
3541 r->handle = 0;
3542 ret = 0;
3543 }
3544 } else {
Daniel Vetteraf26ef32012-12-13 23:07:50 +01003545 ret = -ENODEV;
David Herrmann101b96f2013-08-26 15:16:49 +02003546 }
Dave Airlief453ba02008-11-07 14:05:41 -08003547
Daniel Vetter58c0dca2012-12-13 23:06:08 +01003548 drm_framebuffer_unreference(fb);
3549
Dave Airlief453ba02008-11-07 14:05:41 -08003550 return ret;
3551}
3552
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003553/**
3554 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3555 * @dev: drm device for the ioctl
3556 * @data: data pointer for the ioctl
3557 * @file_priv: drm file for the ioctl call
3558 *
3559 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3560 * rectangle list. Generic userspace which does frontbuffer rendering must call
3561 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3562 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3563 *
3564 * Modesetting drivers which always update the frontbuffer do not need to
3565 * implement the corresponding ->dirty framebuffer callback.
3566 *
3567 * Called by the user via ioctl.
3568 *
3569 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003570 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003571 */
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003572int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3573 void *data, struct drm_file *file_priv)
3574{
3575 struct drm_clip_rect __user *clips_ptr;
3576 struct drm_clip_rect *clips = NULL;
3577 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003578 struct drm_framebuffer *fb;
3579 unsigned flags;
3580 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003581 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003582
Dave Airliefb3b06c2011-02-08 13:55:21 +10003583 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3584 return -EINVAL;
3585
Daniel Vetter786b99e2012-12-02 21:53:40 +01003586 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003587 if (!fb)
Ville Syrjälä37c4e702013-10-17 13:35:01 +03003588 return -ENOENT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003589
3590 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003591 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003592
3593 if (!num_clips != !clips_ptr) {
3594 ret = -EINVAL;
3595 goto out_err1;
3596 }
3597
3598 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3599
3600 /* If userspace annotates copy, clips must come in pairs */
3601 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3602 ret = -EINVAL;
3603 goto out_err1;
3604 }
3605
3606 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05003607 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3608 ret = -EINVAL;
3609 goto out_err1;
3610 }
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003611 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003612 if (!clips) {
3613 ret = -ENOMEM;
3614 goto out_err1;
3615 }
3616
3617 ret = copy_from_user(clips, clips_ptr,
3618 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02003619 if (ret) {
3620 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003621 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02003622 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003623 }
3624
3625 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02003626 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3627 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003628 } else {
3629 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003630 }
3631
3632out_err2:
3633 kfree(clips);
3634out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01003635 drm_framebuffer_unreference(fb);
3636
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00003637 return ret;
3638}
3639
Dave Airlief453ba02008-11-07 14:05:41 -08003640/**
3641 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01003642 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08003643 *
Dave Airlief453ba02008-11-07 14:05:41 -08003644 * Destroy all the FBs associated with @filp.
3645 *
3646 * Called by the user via ioctl.
3647 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003648 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003649 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08003650 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05003651void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08003652{
Dave Airlief453ba02008-11-07 14:05:41 -08003653 struct drm_framebuffer *fb, *tfb;
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003654 struct drm_mode_rmfb_work arg;
3655
3656 INIT_LIST_HEAD(&arg.fbs);
Dave Airlief453ba02008-11-07 14:05:41 -08003657
Daniel Vetter1b116292014-09-24 21:51:06 +02003658 /*
3659 * When the file gets released that means no one else can access the fb
Martin Perese2db7262014-12-09 07:24:04 +01003660 * list any more, so no need to grab fpriv->fbs_lock. And we need to
Daniel Vetter1b116292014-09-24 21:51:06 +02003661 * avoid upsetting lockdep since the universal cursor code adds a
3662 * framebuffer while holding mutex locks.
3663 *
3664 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3665 * locks is impossible here since no one else but this function can get
3666 * at it any more.
3667 */
Dave Airlief453ba02008-11-07 14:05:41 -08003668 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003669 if (drm_framebuffer_read_refcount(fb) > 1) {
3670 list_move_tail(&fb->filp_head, &arg.fbs);
3671 } else {
3672 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01003673
Maarten Lankhorstf2d580b2016-05-04 14:38:26 +02003674 /* This drops the fpriv->fbs reference. */
3675 drm_framebuffer_unreference(fb);
3676 }
3677 }
3678
3679 if (!list_empty(&arg.fbs)) {
3680 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3681
3682 schedule_work(&arg.work);
3683 flush_work(&arg.work);
3684 destroy_work_on_stack(&arg.work);
Dave Airlief453ba02008-11-07 14:05:41 -08003685 }
Dave Airlief453ba02008-11-07 14:05:41 -08003686}
3687
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003688/**
3689 * drm_property_create - create a new property type
3690 * @dev: drm device
3691 * @flags: flags specifying the property type
3692 * @name: name of the property
3693 * @num_values: number of pre-defined values
3694 *
3695 * This creates a new generic drm property which can then be attached to a drm
3696 * object with drm_object_attach_property. The returned property object must be
3697 * freed with drm_property_destroy.
3698 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003699 * Note that the DRM core keeps a per-device list of properties and that, if
3700 * drm_mode_config_cleanup() is called, it will destroy all properties created
3701 * by the driver.
3702 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003703 * Returns:
3704 * A pointer to the newly created property on success, NULL on failure.
3705 */
Dave Airlief453ba02008-11-07 14:05:41 -08003706struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3707 const char *name, int num_values)
3708{
3709 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003710 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003711
3712 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3713 if (!property)
3714 return NULL;
3715
Rob Clark98f75de2014-05-30 11:37:03 -04003716 property->dev = dev;
3717
Dave Airlief453ba02008-11-07 14:05:41 -08003718 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003719 property->values = kcalloc(num_values, sizeof(uint64_t),
3720 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003721 if (!property->values)
3722 goto fail;
3723 }
3724
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003725 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3726 if (ret)
3727 goto fail;
3728
Dave Airlief453ba02008-11-07 14:05:41 -08003729 property->flags = flags;
3730 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003731 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003732
Vinson Lee471dd2e2011-11-10 11:55:40 -08003733 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003734 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003735 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3736 }
Dave Airlief453ba02008-11-07 14:05:41 -08003737
3738 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003739
3740 WARN_ON(!drm_property_type_valid(property));
3741
Dave Airlief453ba02008-11-07 14:05:41 -08003742 return property;
3743fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003744 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003745 kfree(property);
3746 return NULL;
3747}
3748EXPORT_SYMBOL(drm_property_create);
3749
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003750/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003751 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003752 * @dev: drm device
3753 * @flags: flags specifying the property type
3754 * @name: name of the property
3755 * @props: enumeration lists with property values
3756 * @num_values: number of pre-defined values
3757 *
3758 * This creates a new generic drm property which can then be attached to a drm
3759 * object with drm_object_attach_property. The returned property object must be
3760 * freed with drm_property_destroy.
3761 *
3762 * Userspace is only allowed to set one of the predefined values for enumeration
3763 * properties.
3764 *
3765 * Returns:
3766 * A pointer to the newly created property on success, NULL on failure.
3767 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003768struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3769 const char *name,
3770 const struct drm_prop_enum_list *props,
3771 int num_values)
3772{
3773 struct drm_property *property;
3774 int i, ret;
3775
3776 flags |= DRM_MODE_PROP_ENUM;
3777
3778 property = drm_property_create(dev, flags, name, num_values);
3779 if (!property)
3780 return NULL;
3781
3782 for (i = 0; i < num_values; i++) {
3783 ret = drm_property_add_enum(property, i,
3784 props[i].type,
3785 props[i].name);
3786 if (ret) {
3787 drm_property_destroy(dev, property);
3788 return NULL;
3789 }
3790 }
3791
3792 return property;
3793}
3794EXPORT_SYMBOL(drm_property_create_enum);
3795
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003796/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003797 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003798 * @dev: drm device
3799 * @flags: flags specifying the property type
3800 * @name: name of the property
3801 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003802 * @num_props: size of the @props array
3803 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003804 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003805 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003806 * object with drm_object_attach_property. The returned property object must be
3807 * freed with drm_property_destroy.
3808 *
3809 * Compared to plain enumeration properties userspace is allowed to set any
3810 * or'ed together combination of the predefined property bitflag values
3811 *
3812 * Returns:
3813 * A pointer to the newly created property on success, NULL on failure.
3814 */
Rob Clark49e27542012-05-17 02:23:26 -06003815struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3816 int flags, const char *name,
3817 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303818 int num_props,
3819 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003820{
3821 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303822 int i, ret, index = 0;
3823 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003824
3825 flags |= DRM_MODE_PROP_BITMASK;
3826
3827 property = drm_property_create(dev, flags, name, num_values);
3828 if (!property)
3829 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303830 for (i = 0; i < num_props; i++) {
3831 if (!(supported_bits & (1ULL << props[i].type)))
3832 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003833
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303834 if (WARN_ON(index >= num_values)) {
3835 drm_property_destroy(dev, property);
3836 return NULL;
3837 }
3838
3839 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003840 props[i].type,
3841 props[i].name);
3842 if (ret) {
3843 drm_property_destroy(dev, property);
3844 return NULL;
3845 }
3846 }
3847
3848 return property;
3849}
3850EXPORT_SYMBOL(drm_property_create_bitmask);
3851
Rob Clarkebc44cf2012-09-12 22:22:31 -05003852static struct drm_property *property_create_range(struct drm_device *dev,
3853 int flags, const char *name,
3854 uint64_t min, uint64_t max)
3855{
3856 struct drm_property *property;
3857
3858 property = drm_property_create(dev, flags, name, 2);
3859 if (!property)
3860 return NULL;
3861
3862 property->values[0] = min;
3863 property->values[1] = max;
3864
3865 return property;
3866}
3867
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003868/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003869 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003870 * @dev: drm device
3871 * @flags: flags specifying the property type
3872 * @name: name of the property
3873 * @min: minimum value of the property
3874 * @max: maximum value of the property
3875 *
3876 * This creates a new generic drm property which can then be attached to a drm
3877 * object with drm_object_attach_property. The returned property object must be
3878 * freed with drm_property_destroy.
3879 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003880 * Userspace is allowed to set any unsigned integer value in the (min, max)
3881 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003882 *
3883 * Returns:
3884 * A pointer to the newly created property on success, NULL on failure.
3885 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003886struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3887 const char *name,
3888 uint64_t min, uint64_t max)
3889{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003890 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3891 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003892}
3893EXPORT_SYMBOL(drm_property_create_range);
3894
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003895/**
3896 * drm_property_create_signed_range - create a new signed ranged property type
3897 * @dev: drm device
3898 * @flags: flags specifying the property type
3899 * @name: name of the property
3900 * @min: minimum value of the property
3901 * @max: maximum value of the property
3902 *
3903 * This creates a new generic drm property which can then be attached to a drm
3904 * object with drm_object_attach_property. The returned property object must be
3905 * freed with drm_property_destroy.
3906 *
3907 * Userspace is allowed to set any signed integer value in the (min, max)
3908 * range inclusive.
3909 *
3910 * Returns:
3911 * A pointer to the newly created property on success, NULL on failure.
3912 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003913struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3914 int flags, const char *name,
3915 int64_t min, int64_t max)
3916{
3917 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3918 name, I642U64(min), I642U64(max));
3919}
3920EXPORT_SYMBOL(drm_property_create_signed_range);
3921
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003922/**
3923 * drm_property_create_object - create a new object property type
3924 * @dev: drm device
3925 * @flags: flags specifying the property type
3926 * @name: name of the property
3927 * @type: object type from DRM_MODE_OBJECT_* defines
3928 *
3929 * This creates a new generic drm property which can then be attached to a drm
3930 * object with drm_object_attach_property. The returned property object must be
3931 * freed with drm_property_destroy.
3932 *
3933 * Userspace is only allowed to set this to any property value of the given
3934 * @type. Only useful for atomic properties, which is enforced.
3935 *
3936 * Returns:
3937 * A pointer to the newly created property on success, NULL on failure.
3938 */
Rob Clark98f75de2014-05-30 11:37:03 -04003939struct drm_property *drm_property_create_object(struct drm_device *dev,
3940 int flags, const char *name, uint32_t type)
3941{
3942 struct drm_property *property;
3943
3944 flags |= DRM_MODE_PROP_OBJECT;
3945
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003946 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3947 return NULL;
3948
Rob Clark98f75de2014-05-30 11:37:03 -04003949 property = drm_property_create(dev, flags, name, 1);
3950 if (!property)
3951 return NULL;
3952
3953 property->values[0] = type;
3954
3955 return property;
3956}
3957EXPORT_SYMBOL(drm_property_create_object);
3958
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003959/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003960 * drm_property_create_bool - create a new boolean property type
3961 * @dev: drm device
3962 * @flags: flags specifying the property type
3963 * @name: name of the property
3964 *
3965 * This creates a new generic drm property which can then be attached to a drm
3966 * object with drm_object_attach_property. The returned property object must be
3967 * freed with drm_property_destroy.
3968 *
3969 * This is implemented as a ranged property with only {0, 1} as valid values.
3970 *
3971 * Returns:
3972 * A pointer to the newly created property on success, NULL on failure.
3973 */
3974struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3975 const char *name)
3976{
3977 return drm_property_create_range(dev, flags, name, 0, 1);
3978}
3979EXPORT_SYMBOL(drm_property_create_bool);
3980
3981/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003982 * drm_property_add_enum - add a possible value to an enumeration property
3983 * @property: enumeration property to change
3984 * @index: index of the new enumeration
3985 * @value: value of the new enumeration
3986 * @name: symbolic name of the new enumeration
3987 *
3988 * This functions adds enumerations to a property.
3989 *
3990 * It's use is deprecated, drivers should use one of the more specific helpers
3991 * to directly create the property with all enumerations already attached.
3992 *
3993 * Returns:
3994 * Zero on success, error code on failure.
3995 */
Dave Airlief453ba02008-11-07 14:05:41 -08003996int drm_property_add_enum(struct drm_property *property, int index,
3997 uint64_t value, const char *name)
3998{
3999 struct drm_property_enum *prop_enum;
4000
Rob Clark5ea22f22014-05-30 11:34:01 -04004001 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4002 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06004003 return -EINVAL;
4004
4005 /*
4006 * Bitmask enum properties have the additional constraint of values
4007 * from 0 to 63
4008 */
Rob Clark5ea22f22014-05-30 11:34:01 -04004009 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4010 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08004011 return -EINVAL;
4012
Daniel Vetter3758b342014-11-19 18:38:10 +01004013 if (!list_empty(&property->enum_list)) {
4014 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004015 if (prop_enum->value == value) {
4016 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4017 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4018 return 0;
4019 }
4020 }
4021 }
4022
4023 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4024 if (!prop_enum)
4025 return -ENOMEM;
4026
4027 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4028 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4029 prop_enum->value = value;
4030
4031 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01004032 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08004033 return 0;
4034}
4035EXPORT_SYMBOL(drm_property_add_enum);
4036
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004037/**
4038 * drm_property_destroy - destroy a drm property
4039 * @dev: drm device
4040 * @property: property to destry
4041 *
4042 * This function frees a property including any attached resources like
4043 * enumeration values.
4044 */
Dave Airlief453ba02008-11-07 14:05:41 -08004045void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4046{
4047 struct drm_property_enum *prop_enum, *pt;
4048
Daniel Vetter3758b342014-11-19 18:38:10 +01004049 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004050 list_del(&prop_enum->head);
4051 kfree(prop_enum);
4052 }
4053
4054 if (property->num_values)
4055 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10004056 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08004057 list_del(&property->head);
4058 kfree(property);
4059}
4060EXPORT_SYMBOL(drm_property_destroy);
4061
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004062/**
4063 * drm_object_attach_property - attach a property to a modeset object
4064 * @obj: drm modeset object
4065 * @property: property to attach
4066 * @init_val: initial value of the property
4067 *
4068 * This attaches the given property to the modeset object with the given initial
4069 * value. Currently this function cannot fail since the properties are stored in
4070 * a statically sized array.
4071 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004072void drm_object_attach_property(struct drm_mode_object *obj,
4073 struct drm_property *property,
4074 uint64_t init_val)
4075{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004076 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03004077
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004078 if (count == DRM_OBJECT_MAX_PROPERTY) {
4079 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4080 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4081 "you see this message on the same object type.\n",
4082 obj->type);
4083 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03004084 }
4085
Rob Clarkb17cd752014-12-16 18:05:30 -05004086 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004087 obj->properties->values[count] = init_val;
4088 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05004089 if (property->flags & DRM_MODE_PROP_ATOMIC)
4090 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03004091}
4092EXPORT_SYMBOL(drm_object_attach_property);
4093
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004094/**
4095 * drm_object_property_set_value - set the value of a property
4096 * @obj: drm mode object to set property value for
4097 * @property: property to set
4098 * @val: value the property should be set to
4099 *
4100 * This functions sets a given property on a given object. This function only
4101 * changes the software state of the property, it does not call into the
4102 * driver's ->set_property callback.
4103 *
4104 * Returns:
4105 * Zero on success, error code on failure.
4106 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004107int drm_object_property_set_value(struct drm_mode_object *obj,
4108 struct drm_property *property, uint64_t val)
4109{
4110 int i;
4111
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004112 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004113 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004114 obj->properties->values[i] = val;
4115 return 0;
4116 }
4117 }
4118
4119 return -EINVAL;
4120}
4121EXPORT_SYMBOL(drm_object_property_set_value);
4122
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004123/**
4124 * drm_object_property_get_value - retrieve the value of a property
4125 * @obj: drm mode object to get property value from
4126 * @property: property to retrieve
4127 * @val: storage for the property value
4128 *
4129 * This function retrieves the softare state of the given property for the given
4130 * property. Since there is no driver callback to retrieve the current property
4131 * value this might be out of sync with the hardware, depending upon the driver
4132 * and property.
4133 *
4134 * Returns:
4135 * Zero on success, error code on failure.
4136 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004137int drm_object_property_get_value(struct drm_mode_object *obj,
4138 struct drm_property *property, uint64_t *val)
4139{
4140 int i;
4141
Rob Clark88a48e22014-12-18 16:01:50 -05004142 /* read-only properties bypass atomic mechanism and still store
4143 * their value in obj->properties->values[].. mostly to avoid
4144 * having to deal w/ EDID and similar props in atomic paths:
4145 */
4146 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4147 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4148 return drm_atomic_get_property(obj, property, val);
4149
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004150 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05004151 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03004152 *val = obj->properties->values[i];
4153 return 0;
4154 }
4155 }
4156
4157 return -EINVAL;
4158}
4159EXPORT_SYMBOL(drm_object_property_get_value);
4160
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004161/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004162 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004163 * @dev: DRM device
4164 * @data: ioctl data
4165 * @file_priv: DRM file info
4166 *
Daniel Vetter1a498632014-11-19 18:38:09 +01004167 * This function retrieves the metadata for a given property, like the different
4168 * possible values for an enum property or the limits for a range property.
4169 *
4170 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004171 *
4172 * Called by the user via ioctl.
4173 *
4174 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004175 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004176 */
Dave Airlief453ba02008-11-07 14:05:41 -08004177int drm_mode_getproperty_ioctl(struct drm_device *dev,
4178 void *data, struct drm_file *file_priv)
4179{
Dave Airlief453ba02008-11-07 14:05:41 -08004180 struct drm_mode_get_property *out_resp = data;
4181 struct drm_property *property;
4182 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004183 int value_count = 0;
4184 int ret = 0, i;
4185 int copied;
4186 struct drm_property_enum *prop_enum;
4187 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004188 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004189
Dave Airliefb3b06c2011-02-08 13:55:21 +10004190 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4191 return -EINVAL;
4192
Daniel Vetter84849902012-12-02 00:28:11 +01004193 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004194 property = drm_property_find(dev, out_resp->prop_id);
4195 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004196 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004197 goto done;
4198 }
Dave Airlief453ba02008-11-07 14:05:41 -08004199
Rob Clark5ea22f22014-05-30 11:34:01 -04004200 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4201 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01004202 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08004203 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08004204 }
4205
4206 value_count = property->num_values;
4207
4208 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4209 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4210 out_resp->flags = property->flags;
4211
4212 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004213 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004214 for (i = 0; i < value_count; i++) {
4215 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4216 ret = -EFAULT;
4217 goto done;
4218 }
4219 }
4220 }
4221 out_resp->count_values = value_count;
4222
Rob Clark5ea22f22014-05-30 11:34:01 -04004223 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4224 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004225 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4226 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004227 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01004228 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08004229
4230 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4231 ret = -EFAULT;
4232 goto done;
4233 }
4234
4235 if (copy_to_user(&enum_ptr[copied].name,
4236 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4237 ret = -EFAULT;
4238 goto done;
4239 }
4240 copied++;
4241 }
4242 }
4243 out_resp->count_enum_blobs = enum_count;
4244 }
4245
Daniel Vetter3758b342014-11-19 18:38:10 +01004246 /*
4247 * NOTE: The idea seems to have been to use this to read all the blob
4248 * property values. But nothing ever added them to the corresponding
4249 * list, userspace always used the special-purpose get_blob ioctl to
4250 * read the value for a blob property. It also doesn't make a lot of
4251 * sense to return values here when everything else is just metadata for
4252 * the property itself.
4253 */
4254 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4255 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004256done:
Daniel Vetter84849902012-12-02 00:28:11 +01004257 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004258 return ret;
4259}
4260
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004261static void drm_property_free_blob(struct kref *kref)
4262{
4263 struct drm_property_blob *blob =
4264 container_of(kref, struct drm_property_blob, base.refcount);
4265
4266 mutex_lock(&blob->dev->mode_config.blob_lock);
4267 list_del(&blob->head_global);
4268 mutex_unlock(&blob->dev->mode_config.blob_lock);
4269
4270 drm_mode_object_unregister(blob->dev, &blob->base);
4271
4272 kfree(blob);
4273}
4274
Daniel Stone99531d92015-05-22 13:34:49 +01004275/**
4276 * drm_property_create_blob - Create new blob property
4277 *
4278 * Creates a new blob property for a specified DRM device, optionally
4279 * copying data.
4280 *
4281 * @dev: DRM device to create property for
4282 * @length: Length to allocate for blob data
4283 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01004284 *
4285 * Returns:
4286 * New blob property with a single reference on success, or an ERR_PTR
4287 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01004288 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01004289struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02004290drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004291 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08004292{
4293 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02004294 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004295
Dan Carpenter9ac09342015-10-29 16:37:54 +03004296 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01004297 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08004298
4299 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4300 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01004301 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08004302
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004303 /* This must be explicitly initialised, so we can safely call list_del
4304 * on it in the removal handler, even if it isn't in a file list. */
4305 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08004306 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004307 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08004308
Daniel Stone99531d92015-05-22 13:34:49 +01004309 if (data)
4310 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08004311
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004312 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4313 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004314 if (ret) {
4315 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004316 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004317 }
4318
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004319 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004320 list_add_tail(&blob->head_global,
4321 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004322 mutex_unlock(&dev->mode_config.blob_lock);
4323
Dave Airlief453ba02008-11-07 14:05:41 -08004324 return blob;
4325}
Daniel Stone6bcacf52015-04-20 19:22:55 +01004326EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004327
Daniel Stone6bcacf52015-04-20 19:22:55 +01004328/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004329 * drm_property_unreference_blob - Unreference a blob property
4330 *
4331 * Drop a reference on a blob property. May free the object.
4332 *
Daniel Stonef102c162015-05-22 13:34:44 +01004333 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004334 */
4335void drm_property_unreference_blob(struct drm_property_blob *blob)
4336{
Daniel Stone6bcacf52015-04-20 19:22:55 +01004337 if (!blob)
4338 return;
4339
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004340 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004341}
4342EXPORT_SYMBOL(drm_property_unreference_blob);
4343
4344/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004345 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4346 * @dev: DRM device
4347 * @file_priv: destroy all blobs owned by this file handle
4348 */
4349void drm_property_destroy_user_blobs(struct drm_device *dev,
4350 struct drm_file *file_priv)
4351{
4352 struct drm_property_blob *blob, *bt;
4353
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004354 /*
4355 * When the file gets released that means no one else can access the
4356 * blob list any more, so no need to grab dev->blob_lock.
4357 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004358 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4359 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004360 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004361 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004362}
4363
4364/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01004365 * drm_property_reference_blob - Take a reference on an existing property
4366 *
4367 * Take a new reference on an existing blob property.
4368 *
Daniel Stonef102c162015-05-22 13:34:44 +01004369 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01004370 */
4371struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4372{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004373 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004374 return blob;
4375}
4376EXPORT_SYMBOL(drm_property_reference_blob);
4377
Daniel Stone6bcacf52015-04-20 19:22:55 +01004378/**
4379 * drm_property_lookup_blob - look up a blob property and take a reference
4380 * @dev: drm device
4381 * @id: id of the blob property
4382 *
4383 * If successful, this takes an additional reference to the blob property.
4384 * callers need to make sure to eventually unreference the returned property
4385 * again, using @drm_property_unreference_blob.
4386 */
4387struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4388 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08004389{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004390 struct drm_mode_object *obj;
4391 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01004392
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004393 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4394 if (obj)
4395 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01004396 return blob;
4397}
4398EXPORT_SYMBOL(drm_property_lookup_blob);
4399
4400/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01004401 * drm_property_replace_global_blob - atomically replace existing blob property
4402 * @dev: drm device
4403 * @replace: location of blob property pointer to be replaced
4404 * @length: length of data for new blob, or 0 for no data
4405 * @data: content for new blob, or NULL for no data
4406 * @obj_holds_id: optional object for property holding blob ID
4407 * @prop_holds_id: optional property holding blob ID
4408 * @return 0 on success or error on failure
4409 *
4410 * This function will atomically replace a global property in the blob list,
4411 * optionally updating a property which holds the ID of that property. It is
4412 * guaranteed to be atomic: no caller will be allowed to see intermediate
4413 * results, and either the entire operation will succeed and clean up the
4414 * previous property, or it will fail and the state will be unchanged.
4415 *
4416 * If length is 0 or data is NULL, no new blob will be created, and the holding
4417 * property, if specified, will be set to 0.
4418 *
4419 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4420 * by holding the relevant modesetting object lock for its parent.
4421 *
4422 * For example, a drm_connector has a 'PATH' property, which contains the ID
4423 * of a blob property with the value of the MST path information. Calling this
4424 * function with replace pointing to the connector's path_blob_ptr, length and
4425 * data set for the new path information, obj_holds_id set to the connector's
4426 * base object, and prop_holds_id set to the path property name, will perform
4427 * a completely atomic update. The access to path_blob_ptr is protected by the
4428 * caller holding a lock on the connector.
4429 */
4430static int drm_property_replace_global_blob(struct drm_device *dev,
4431 struct drm_property_blob **replace,
4432 size_t length,
4433 const void *data,
4434 struct drm_mode_object *obj_holds_id,
4435 struct drm_property *prop_holds_id)
4436{
4437 struct drm_property_blob *new_blob = NULL;
4438 struct drm_property_blob *old_blob = NULL;
4439 int ret;
4440
4441 WARN_ON(replace == NULL);
4442
4443 old_blob = *replace;
4444
4445 if (length && data) {
4446 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01004447 if (IS_ERR(new_blob))
4448 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004449 }
4450
4451 /* This does not need to be synchronised with blob_lock, as the
4452 * get_properties ioctl locks all modesetting objects, and
4453 * obj_holds_id must be locked before calling here, so we cannot
4454 * have its value out of sync with the list membership modified
4455 * below under blob_lock. */
4456 if (obj_holds_id) {
4457 ret = drm_object_property_set_value(obj_holds_id,
4458 prop_holds_id,
4459 new_blob ?
4460 new_blob->base.id : 0);
4461 if (ret != 0)
4462 goto err_created;
4463 }
4464
Markus Elfringec530822015-07-05 21:55:10 +02004465 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004466 *replace = new_blob;
4467
4468 return 0;
4469
4470err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01004471 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01004472 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004473}
4474
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004475/**
4476 * drm_mode_getblob_ioctl - get the contents of a blob property value
4477 * @dev: DRM device
4478 * @data: ioctl data
4479 * @file_priv: DRM file info
4480 *
4481 * This function retrieves the contents of a blob property. The value stored in
4482 * an object's blob property is just a normal modeset object id.
4483 *
4484 * Called by the user via ioctl.
4485 *
4486 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004487 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004488 */
Dave Airlief453ba02008-11-07 14:05:41 -08004489int drm_mode_getblob_ioctl(struct drm_device *dev,
4490 void *data, struct drm_file *file_priv)
4491{
Dave Airlief453ba02008-11-07 14:05:41 -08004492 struct drm_mode_get_blob *out_resp = data;
4493 struct drm_property_blob *blob;
4494 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004495 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08004496
Dave Airliefb3b06c2011-02-08 13:55:21 +10004497 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4498 return -EINVAL;
4499
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004500 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4501 if (!blob)
4502 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004503
4504 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02004505 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004506 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08004507 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004508 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08004509 }
4510 }
4511 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004512unref:
4513 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08004514
Dave Airlief453ba02008-11-07 14:05:41 -08004515 return ret;
4516}
4517
Dave Airliecc7096f2014-10-22 12:03:04 +10004518/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004519 * drm_mode_createblob_ioctl - create a new blob property
4520 * @dev: DRM device
4521 * @data: ioctl data
4522 * @file_priv: DRM file info
4523 *
4524 * This function creates a new blob property with user-defined values. In order
4525 * to give us sensible validation and checking when creating, rather than at
4526 * every potential use, we also require a type to be provided upfront.
4527 *
4528 * Called by the user via ioctl.
4529 *
4530 * Returns:
4531 * Zero on success, negative errno on failure.
4532 */
4533int drm_mode_createblob_ioctl(struct drm_device *dev,
4534 void *data, struct drm_file *file_priv)
4535{
4536 struct drm_mode_create_blob *out_resp = data;
4537 struct drm_property_blob *blob;
4538 void __user *blob_ptr;
4539 int ret = 0;
4540
4541 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4542 return -EINVAL;
4543
4544 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4545 if (IS_ERR(blob))
4546 return PTR_ERR(blob);
4547
4548 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4549 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4550 ret = -EFAULT;
4551 goto out_blob;
4552 }
4553
4554 /* Dropping the lock between create_blob and our access here is safe
4555 * as only the same file_priv can remove the blob; at this point, it is
4556 * not associated with any file_priv. */
4557 mutex_lock(&dev->mode_config.blob_lock);
4558 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04004559 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004560 mutex_unlock(&dev->mode_config.blob_lock);
4561
4562 return 0;
4563
4564out_blob:
4565 drm_property_unreference_blob(blob);
4566 return ret;
4567}
4568
4569/**
4570 * drm_mode_destroyblob_ioctl - destroy a user blob property
4571 * @dev: DRM device
4572 * @data: ioctl data
4573 * @file_priv: DRM file info
4574 *
4575 * Destroy an existing user-defined blob property.
4576 *
4577 * Called by the user via ioctl.
4578 *
4579 * Returns:
4580 * Zero on success, negative errno on failure.
4581 */
4582int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4583 void *data, struct drm_file *file_priv)
4584{
4585 struct drm_mode_destroy_blob *out_resp = data;
4586 struct drm_property_blob *blob = NULL, *bt;
4587 bool found = false;
4588 int ret = 0;
4589
4590 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4591 return -EINVAL;
4592
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004593 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4594 if (!blob)
4595 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004596
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004597 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004598 /* Ensure the property was actually created by this user. */
4599 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4600 if (bt == blob) {
4601 found = true;
4602 break;
4603 }
4604 }
4605
4606 if (!found) {
4607 ret = -EPERM;
4608 goto err;
4609 }
4610
4611 /* We must drop head_file here, because we may not be the last
4612 * reference on the blob. */
4613 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004614 mutex_unlock(&dev->mode_config.blob_lock);
4615
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004616 /* One reference from lookup, and one from the filp. */
4617 drm_property_unreference_blob(blob);
4618 drm_property_unreference_blob(blob);
4619
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004620 return 0;
4621
4622err:
4623 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02004624 drm_property_unreference_blob(blob);
4625
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004626 return ret;
4627}
4628
4629/**
Dave Airliecc7096f2014-10-22 12:03:04 +10004630 * drm_mode_connector_set_path_property - set tile property on connector
4631 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01004632 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10004633 *
4634 * This creates a property to expose to userspace to specify a
4635 * connector path. This is mainly used for DisplayPort MST where
4636 * connectors have a topology and we want to allow userspace to give
4637 * them more meaningful names.
4638 *
4639 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004640 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10004641 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10004642int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004643 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10004644{
4645 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02004646 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10004647
Daniel Stoned2ed3432015-04-20 19:22:53 +01004648 ret = drm_property_replace_global_blob(dev,
4649 &connector->path_blob_ptr,
4650 strlen(path) + 1,
4651 path,
4652 &connector->base,
4653 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10004654 return ret;
4655}
4656EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4657
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004658/**
Dave Airlie6f134d72014-10-20 16:30:50 +10004659 * drm_mode_connector_set_tile_property - set tile property on connector
4660 * @connector: connector to set property on.
4661 *
4662 * This looks up the tile information for a connector, and creates a
4663 * property for userspace to parse if it exists. The property is of
4664 * the form of 8 integers using ':' as a separator.
4665 *
4666 * Returns:
4667 * Zero on success, errno on failure.
4668 */
4669int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4670{
4671 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004672 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004673 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004674
4675 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004676 ret = drm_property_replace_global_blob(dev,
4677 &connector->tile_blob_ptr,
4678 0,
4679 NULL,
4680 &connector->base,
4681 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004682 return ret;
4683 }
4684
4685 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4686 connector->tile_group->id, connector->tile_is_single_monitor,
4687 connector->num_h_tile, connector->num_v_tile,
4688 connector->tile_h_loc, connector->tile_v_loc,
4689 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004690
Daniel Stoned2ed3432015-04-20 19:22:53 +01004691 ret = drm_property_replace_global_blob(dev,
4692 &connector->tile_blob_ptr,
4693 strlen(tile) + 1,
4694 tile,
4695 &connector->base,
4696 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004697 return ret;
4698}
4699EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4700
4701/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004702 * drm_mode_connector_update_edid_property - update the edid property of a connector
4703 * @connector: drm connector
4704 * @edid: new value of the edid property
4705 *
4706 * This function creates a new blob modeset object and assigns its id to the
4707 * connector's edid property.
4708 *
4709 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004710 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004711 */
Dave Airlief453ba02008-11-07 14:05:41 -08004712int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004713 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004714{
4715 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004716 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004717 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004718
Thomas Wood4cf2b282014-06-18 17:52:33 +01004719 /* ignore requests to set edid when overridden */
4720 if (connector->override_edid)
4721 return 0;
4722
Daniel Stoned2ed3432015-04-20 19:22:53 +01004723 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004724 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004725
Daniel Stoned2ed3432015-04-20 19:22:53 +01004726 ret = drm_property_replace_global_blob(dev,
4727 &connector->edid_blob_ptr,
4728 size,
4729 edid,
4730 &connector->base,
4731 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004732 return ret;
4733}
4734EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4735
Rob Clark3843e712014-12-16 18:05:29 -05004736/* Some properties could refer to dynamic refcnt'd objects, or things that
4737 * need special locking to handle lifetime issues (ie. to ensure the prop
4738 * value doesn't become invalid part way through the property update due to
4739 * race). The value returned by reference via 'obj' should be passed back
4740 * to drm_property_change_valid_put() after the property is set (and the
4741 * object to which the property is attached has a chance to take it's own
4742 * reference).
4743 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004744bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004745 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004746{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004747 int i;
4748
Paulo Zanoni26a34812012-05-15 18:08:59 -03004749 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4750 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004751
Rob Clark3843e712014-12-16 18:05:29 -05004752 *ref = NULL;
4753
Rob Clark5ea22f22014-05-30 11:34:01 -04004754 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004755 if (value < property->values[0] || value > property->values[1])
4756 return false;
4757 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004758 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4759 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004760
Rob Clarkebc44cf2012-09-12 22:22:31 -05004761 if (svalue < U642I64(property->values[0]) ||
4762 svalue > U642I64(property->values[1]))
4763 return false;
4764 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004765 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004766 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004767
Rob Clark49e27542012-05-17 02:23:26 -06004768 for (i = 0; i < property->num_values; i++)
4769 valid_mask |= (1ULL << property->values[i]);
4770 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004771 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004772 struct drm_property_blob *blob;
4773
4774 if (value == 0)
4775 return true;
4776
4777 blob = drm_property_lookup_blob(property->dev, value);
4778 if (blob) {
4779 *ref = &blob->base;
4780 return true;
4781 } else {
4782 return false;
4783 }
Rob Clark98f75de2014-05-30 11:37:03 -04004784 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004785 /* a zero value for an object property translates to null: */
4786 if (value == 0)
4787 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004788
Tomi Valkeinen1e8985a2016-05-31 15:03:18 +03004789 *ref = _object_find(property->dev, value, property->values[0]);
4790 return *ref != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004791 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004792
4793 for (i = 0; i < property->num_values; i++)
4794 if (property->values[i] == value)
4795 return true;
4796 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004797}
4798
Rob Clarkd34f20d2014-12-18 16:01:56 -05004799void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004800 struct drm_mode_object *ref)
4801{
4802 if (!ref)
4803 return;
4804
4805 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004806 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004807 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4808 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004809}
4810
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004811/**
4812 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4813 * @dev: DRM device
4814 * @data: ioctl data
4815 * @file_priv: DRM file info
4816 *
4817 * This function sets the current value for a connectors's property. It also
4818 * calls into a driver's ->set_property callback to update the hardware state
4819 *
4820 * Called by the user via ioctl.
4821 *
4822 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004823 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004824 */
Dave Airlief453ba02008-11-07 14:05:41 -08004825int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4826 void *data, struct drm_file *file_priv)
4827{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004828 struct drm_mode_connector_set_property *conn_set_prop = data;
4829 struct drm_mode_obj_set_property obj_set_prop = {
4830 .value = conn_set_prop->value,
4831 .prop_id = conn_set_prop->prop_id,
4832 .obj_id = conn_set_prop->connector_id,
4833 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4834 };
Dave Airlief453ba02008-11-07 14:05:41 -08004835
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004836 /* It does all the locking and checking we need */
4837 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004838}
4839
Paulo Zanonic5431882012-05-15 18:09:02 -03004840static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4841 struct drm_property *property,
4842 uint64_t value)
4843{
4844 int ret = -EINVAL;
4845 struct drm_connector *connector = obj_to_connector(obj);
4846
4847 /* Do DPMS ourselves */
4848 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004849 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004850 } else if (connector->funcs->set_property)
4851 ret = connector->funcs->set_property(connector, property, value);
4852
4853 /* store the property value if successful */
4854 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004855 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004856 return ret;
4857}
4858
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004859static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4860 struct drm_property *property,
4861 uint64_t value)
4862{
4863 int ret = -EINVAL;
4864 struct drm_crtc *crtc = obj_to_crtc(obj);
4865
4866 if (crtc->funcs->set_property)
4867 ret = crtc->funcs->set_property(crtc, property, value);
4868 if (!ret)
4869 drm_object_property_set_value(obj, property, value);
4870
4871 return ret;
4872}
4873
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004874/**
4875 * drm_mode_plane_set_obj_prop - set the value of a property
4876 * @plane: drm plane object to set property value for
4877 * @property: property to set
4878 * @value: value the property should be set to
4879 *
4880 * This functions sets a given property on a given plane object. This function
4881 * calls the driver's ->set_property callback and changes the software state of
4882 * the property if the callback succeeds.
4883 *
4884 * Returns:
4885 * Zero on success, error code on failure.
4886 */
4887int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4888 struct drm_property *property,
4889 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004890{
4891 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004892 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004893
4894 if (plane->funcs->set_property)
4895 ret = plane->funcs->set_property(plane, property, value);
4896 if (!ret)
4897 drm_object_property_set_value(obj, property, value);
4898
4899 return ret;
4900}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004901EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004902
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004903/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004904 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004905 * @dev: DRM device
4906 * @data: ioctl data
4907 * @file_priv: DRM file info
4908 *
4909 * This function retrieves the current value for an object's property. Compared
4910 * to the connector specific ioctl this one is extended to also work on crtc and
4911 * plane objects.
4912 *
4913 * Called by the user via ioctl.
4914 *
4915 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004916 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004917 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004918int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4919 struct drm_file *file_priv)
4920{
4921 struct drm_mode_obj_get_properties *arg = data;
4922 struct drm_mode_object *obj;
4923 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004924
4925 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4926 return -EINVAL;
4927
Daniel Vetter84849902012-12-02 00:28:11 +01004928 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004929
4930 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4931 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004932 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004933 goto out;
4934 }
4935 if (!obj->properties) {
4936 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02004937 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004938 }
4939
Rob Clark88a48e22014-12-18 16:01:50 -05004940 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05004941 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4942 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4943 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03004944
Daniel Vetter1649c332016-04-22 22:10:28 +02004945out_unref:
4946 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03004947out:
Daniel Vetter84849902012-12-02 00:28:11 +01004948 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004949 return ret;
4950}
4951
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004952/**
4953 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4954 * @dev: DRM device
4955 * @data: ioctl data
4956 * @file_priv: DRM file info
4957 *
4958 * This function sets the current value for an object's property. It also calls
4959 * into a driver's ->set_property callback to update the hardware state.
4960 * Compared to the connector specific ioctl this one is extended to also work on
4961 * crtc and plane objects.
4962 *
4963 * Called by the user via ioctl.
4964 *
4965 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004966 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004967 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004968int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4969 struct drm_file *file_priv)
4970{
4971 struct drm_mode_obj_set_property *arg = data;
4972 struct drm_mode_object *arg_obj;
4973 struct drm_mode_object *prop_obj;
4974 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05004975 int i, ret = -EINVAL;
4976 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004977
4978 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4979 return -EINVAL;
4980
Daniel Vetter84849902012-12-02 00:28:11 +01004981 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004982
4983 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004984 if (!arg_obj) {
4985 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004986 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004987 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004988 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02004989 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004990
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004991 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05004992 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03004993 break;
4994
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004995 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02004996 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004997
4998 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4999 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005000 if (!prop_obj) {
5001 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02005002 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005003 }
Paulo Zanonic5431882012-05-15 18:09:02 -03005004 property = obj_to_property(prop_obj);
5005
Rob Clark3843e712014-12-16 18:05:29 -05005006 if (!drm_property_change_valid_get(property, arg->value, &ref))
Dave Airlieb164d312016-04-27 11:10:09 +10005007 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03005008
5009 switch (arg_obj->type) {
5010 case DRM_MODE_OBJECT_CONNECTOR:
5011 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5012 arg->value);
5013 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03005014 case DRM_MODE_OBJECT_CRTC:
5015 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5016 break;
Rob Clark4d939142012-05-17 02:23:27 -06005017 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01005018 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5019 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06005020 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03005021 }
5022
Rob Clark3843e712014-12-16 18:05:29 -05005023 drm_property_change_valid_put(property, ref);
5024
Daniel Vetter1649c332016-04-22 22:10:28 +02005025out_unref:
5026 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03005027out:
Daniel Vetter84849902012-12-02 00:28:11 +01005028 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03005029 return ret;
5030}
5031
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005032/**
5033 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5034 * @connector: connector to attach
5035 * @encoder: encoder to attach @connector to
5036 *
5037 * This function links up a connector to an encoder. Note that the routing
5038 * restrictions between encoders and crtcs are exposed to userspace through the
5039 * possible_clones and possible_crtcs bitmasks.
5040 *
5041 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005042 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005043 */
Dave Airlief453ba02008-11-07 14:05:41 -08005044int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5045 struct drm_encoder *encoder)
5046{
5047 int i;
5048
Thierry Redingeb47fe82015-11-16 18:19:53 +01005049 /*
5050 * In the past, drivers have attempted to model the static association
5051 * of connector to encoder in simple connector/encoder devices using a
5052 * direct assignment of connector->encoder = encoder. This connection
5053 * is a logical one and the responsibility of the core, so drivers are
5054 * expected not to mess with this.
5055 *
5056 * Note that the error return should've been enough here, but a large
5057 * majority of drivers ignores the return value, so add in a big WARN
5058 * to get people's attention.
5059 */
5060 if (WARN_ON(connector->encoder))
5061 return -EINVAL;
5062
Dave Airlief453ba02008-11-07 14:05:41 -08005063 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5064 if (connector->encoder_ids[i] == 0) {
5065 connector->encoder_ids[i] = encoder->base.id;
5066 return 0;
5067 }
5068 }
5069 return -ENOMEM;
5070}
5071EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5072
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005073/**
5074 * drm_mode_crtc_set_gamma_size - set the gamma table size
5075 * @crtc: CRTC to set the gamma table size for
5076 * @gamma_size: size of the gamma table
5077 *
5078 * Drivers which support gamma tables should set this to the supported gamma
5079 * table size when initializing the CRTC. Currently the drm core only supports a
5080 * fixed gamma table size.
5081 *
5082 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005083 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005084 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005085int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005086 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08005087{
Daniel Vettercf48e292016-03-30 11:51:16 +02005088 uint16_t *r_base, *g_base, *b_base;
5089 int i;
5090
Dave Airlief453ba02008-11-07 14:05:41 -08005091 crtc->gamma_size = gamma_size;
5092
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01005093 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5094 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08005095 if (!crtc->gamma_store) {
5096 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005097 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08005098 }
5099
Daniel Vettercf48e292016-03-30 11:51:16 +02005100 r_base = crtc->gamma_store;
5101 g_base = r_base + gamma_size;
5102 b_base = g_base + gamma_size;
5103 for (i = 0; i < gamma_size; i++) {
5104 r_base[i] = i << 8;
5105 g_base[i] = i << 8;
5106 b_base[i] = i << 8;
5107 }
5108
5109
Sascha Hauer4cae5b82012-02-01 11:38:23 +01005110 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08005111}
5112EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5113
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005114/**
5115 * drm_mode_gamma_set_ioctl - set the gamma table
5116 * @dev: DRM device
5117 * @data: ioctl data
5118 * @file_priv: DRM file info
5119 *
5120 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5121 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5122 *
5123 * Called by the user via ioctl.
5124 *
5125 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005126 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005127 */
Dave Airlief453ba02008-11-07 14:05:41 -08005128int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5129 void *data, struct drm_file *file_priv)
5130{
5131 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005132 struct drm_crtc *crtc;
5133 void *r_base, *g_base, *b_base;
5134 int size;
5135 int ret = 0;
5136
Dave Airliefb3b06c2011-02-08 13:55:21 +10005137 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5138 return -EINVAL;
5139
Daniel Vetter84849902012-12-02 00:28:11 +01005140 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005141 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5142 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005143 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005144 goto out;
5145 }
Dave Airlief453ba02008-11-07 14:05:41 -08005146
Laurent Pinchartebe0f242012-05-17 13:27:24 +02005147 if (crtc->funcs->gamma_set == NULL) {
5148 ret = -ENOSYS;
5149 goto out;
5150 }
5151
Dave Airlief453ba02008-11-07 14:05:41 -08005152 /* memcpy into gamma store */
5153 if (crtc_lut->gamma_size != crtc->gamma_size) {
5154 ret = -EINVAL;
5155 goto out;
5156 }
5157
5158 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5159 r_base = crtc->gamma_store;
5160 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5161 ret = -EFAULT;
5162 goto out;
5163 }
5164
5165 g_base = r_base + size;
5166 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5167 ret = -EFAULT;
5168 goto out;
5169 }
5170
5171 b_base = g_base + size;
5172 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5173 ret = -EFAULT;
5174 goto out;
5175 }
5176
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02005177 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08005178
5179out:
Daniel Vetter84849902012-12-02 00:28:11 +01005180 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005181 return ret;
5182
5183}
5184
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005185/**
5186 * drm_mode_gamma_get_ioctl - get the gamma table
5187 * @dev: DRM device
5188 * @data: ioctl data
5189 * @file_priv: DRM file info
5190 *
5191 * Copy the current gamma table into the storage provided. This also provides
5192 * the gamma table size the driver expects, which can be used to size the
5193 * allocated storage.
5194 *
5195 * Called by the user via ioctl.
5196 *
5197 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005198 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005199 */
Dave Airlief453ba02008-11-07 14:05:41 -08005200int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5201 void *data, struct drm_file *file_priv)
5202{
5203 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08005204 struct drm_crtc *crtc;
5205 void *r_base, *g_base, *b_base;
5206 int size;
5207 int ret = 0;
5208
Dave Airliefb3b06c2011-02-08 13:55:21 +10005209 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5210 return -EINVAL;
5211
Daniel Vetter84849902012-12-02 00:28:11 +01005212 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04005213 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5214 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005215 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08005216 goto out;
5217 }
Dave Airlief453ba02008-11-07 14:05:41 -08005218
5219 /* memcpy into gamma store */
5220 if (crtc_lut->gamma_size != crtc->gamma_size) {
5221 ret = -EINVAL;
5222 goto out;
5223 }
5224
5225 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5226 r_base = crtc->gamma_store;
5227 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5228 ret = -EFAULT;
5229 goto out;
5230 }
5231
5232 g_base = r_base + size;
5233 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5234 ret = -EFAULT;
5235 goto out;
5236 }
5237
5238 b_base = g_base + size;
5239 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5240 ret = -EFAULT;
5241 goto out;
5242 }
5243out:
Daniel Vetter84849902012-12-02 00:28:11 +01005244 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08005245 return ret;
5246}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005247
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005248/**
5249 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5250 * @dev: DRM device
5251 * @data: ioctl data
5252 * @file_priv: DRM file info
5253 *
5254 * This schedules an asynchronous update on a given CRTC, called page flip.
5255 * Optionally a drm event is generated to signal the completion of the event.
5256 * Generic drivers cannot assume that a pageflip with changed framebuffer
5257 * properties (including driver specific metadata like tiling layout) will work,
5258 * but some drivers support e.g. pixel format changes through the pageflip
5259 * ioctl.
5260 *
5261 * Called by the user via ioctl.
5262 *
5263 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005264 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005265 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005266int drm_mode_page_flip_ioctl(struct drm_device *dev,
5267 void *data, struct drm_file *file_priv)
5268{
5269 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005270 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02005271 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005272 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005273 int ret = -EINVAL;
5274
5275 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5276 page_flip->reserved != 0)
5277 return -EINVAL;
5278
Keith Packard62f21042013-07-22 18:50:00 -07005279 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5280 return -EINVAL;
5281
Rob Clarka2b34e22013-10-05 16:36:52 -04005282 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5283 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03005284 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005285
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01005286 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07005287 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01005288 /* The framebuffer is currently unbound, presumably
5289 * due to a hotplug event, that userspace has not
5290 * yet discovered.
5291 */
5292 ret = -EBUSY;
5293 goto out;
5294 }
5295
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005296 if (crtc->funcs->page_flip == NULL)
5297 goto out;
5298
Daniel Vetter786b99e2012-12-02 21:53:40 +01005299 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005300 if (!fb) {
5301 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005302 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03005303 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005304
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03005305 if (crtc->state) {
5306 const struct drm_plane_state *state = crtc->primary->state;
5307
5308 ret = check_src_coords(state->src_x, state->src_y,
5309 state->src_w, state->src_h, fb);
5310 } else {
5311 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5312 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01005313 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005314 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02005315
Matt Roperf4510a22014-04-01 15:22:40 -07005316 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02005317 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5318 ret = -EINVAL;
5319 goto out;
5320 }
5321
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005322 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005323 e = kzalloc(sizeof *e, GFP_KERNEL);
5324 if (!e) {
5325 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005326 goto out;
5327 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08005328 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01005329 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005330 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005331 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5332 if (ret) {
5333 kfree(e);
5334 goto out;
5335 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005336 }
5337
Daniel Vetter3d30a592014-07-27 13:42:42 +02005338 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07005339 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005340 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01005341 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5342 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01005343 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02005344 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005345 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02005346 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01005347 /* Unref only the old framebuffer. */
5348 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005349 }
5350
5351out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01005352 if (fb)
5353 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02005354 if (crtc->primary->old_fb)
5355 drm_framebuffer_unreference(crtc->primary->old_fb);
5356 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02005357 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01005358
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05005359 return ret;
5360}
Chris Wilsoneb033552011-01-24 15:11:08 +00005361
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005362/**
5363 * drm_mode_config_reset - call ->reset callbacks
5364 * @dev: drm device
5365 *
5366 * This functions calls all the crtc's, encoder's and connector's ->reset
5367 * callback. Drivers can use this in e.g. their driver load or resume code to
5368 * reset hardware and software state.
5369 */
Chris Wilsoneb033552011-01-24 15:11:08 +00005370void drm_mode_config_reset(struct drm_device *dev)
5371{
5372 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005373 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00005374 struct drm_encoder *encoder;
5375 struct drm_connector *connector;
5376
Daniel Vettere4f62542015-07-09 23:44:35 +02005377 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02005378 if (plane->funcs->reset)
5379 plane->funcs->reset(plane);
5380
Daniel Vettere4f62542015-07-09 23:44:35 +02005381 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005382 if (crtc->funcs->reset)
5383 crtc->funcs->reset(crtc);
5384
Daniel Vettere4f62542015-07-09 23:44:35 +02005385 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005386 if (encoder->funcs->reset)
5387 encoder->funcs->reset(encoder);
5388
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005389 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10005390 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00005391 if (connector->funcs->reset)
5392 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02005393 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00005394}
5395EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10005396
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005397/**
5398 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5399 * @dev: DRM device
5400 * @data: ioctl data
5401 * @file_priv: DRM file info
5402 *
5403 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5404 * TTM or something else entirely) and returns the resulting buffer handle. This
5405 * handle can then be wrapped up into a framebuffer modeset object.
5406 *
5407 * Note that userspace is not allowed to use such objects for render
5408 * acceleration - drivers must create their own private ioctls for such a use
5409 * case.
5410 *
5411 * Called by the user via ioctl.
5412 *
5413 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005414 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005415 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005416int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5417 void *data, struct drm_file *file_priv)
5418{
5419 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01005420 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10005421
5422 if (!dev->driver->dumb_create)
5423 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01005424 if (!args->width || !args->height || !args->bpp)
5425 return -EINVAL;
5426
5427 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02005428 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01005429 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02005430 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01005431 return -EINVAL;
5432 stride = cpp * args->width;
5433 if (args->height > 0xffffffffU / stride)
5434 return -EINVAL;
5435
5436 /* test for wrap-around */
5437 size = args->height * stride;
5438 if (PAGE_ALIGN(size) == 0)
5439 return -EINVAL;
5440
Thierry Redingf6085952014-11-03 11:14:14 +01005441 /*
5442 * handle, pitch and size are output parameters. Zero them out to
5443 * prevent drivers from accidentally using uninitialized data. Since
5444 * not all existing userspace is clearing these fields properly we
5445 * cannot reject IOCTL with garbage in them.
5446 */
5447 args->handle = 0;
5448 args->pitch = 0;
5449 args->size = 0;
5450
Dave Airlieff72145b2011-02-07 12:16:14 +10005451 return dev->driver->dumb_create(file_priv, dev, args);
5452}
5453
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005454/**
5455 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5456 * @dev: DRM device
5457 * @data: ioctl data
5458 * @file_priv: DRM file info
5459 *
5460 * Allocate an offset in the drm device node's address space to be able to
5461 * memory map a dumb buffer.
5462 *
5463 * Called by the user via ioctl.
5464 *
5465 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005466 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005467 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005468int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5469 void *data, struct drm_file *file_priv)
5470{
5471 struct drm_mode_map_dumb *args = data;
5472
5473 /* call driver ioctl to get mmap offset */
5474 if (!dev->driver->dumb_map_offset)
5475 return -ENOSYS;
5476
5477 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5478}
5479
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005480/**
5481 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5482 * @dev: DRM device
5483 * @data: ioctl data
5484 * @file_priv: DRM file info
5485 *
5486 * This destroys the userspace handle for the given dumb backing storage buffer.
5487 * Since buffer objects must be reference counted in the kernel a buffer object
5488 * won't be immediately freed if a framebuffer modeset object still uses it.
5489 *
5490 * Called by the user via ioctl.
5491 *
5492 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01005493 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005494 */
Dave Airlieff72145b2011-02-07 12:16:14 +10005495int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5496 void *data, struct drm_file *file_priv)
5497{
5498 struct drm_mode_destroy_dumb *args = data;
5499
5500 if (!dev->driver->dumb_destroy)
5501 return -ENOSYS;
5502
5503 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5504}
Dave Airlie248dbc22011-11-29 20:02:54 +00005505
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01005506/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305507 * drm_rotation_simplify() - Try to simplify the rotation
5508 * @rotation: Rotation to be simplified
5509 * @supported_rotations: Supported rotations
5510 *
5511 * Attempt to simplify the rotation to a form that is supported.
5512 * Eg. if the hardware supports everything except DRM_REFLECT_X
5513 * one could call this function like this:
5514 *
5515 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5516 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5517 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5518 *
5519 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5520 * transforms the hardware supports, this function may not
5521 * be able to produce a supported transform, so the caller should
5522 * check the result afterwards.
5523 */
5524unsigned int drm_rotation_simplify(unsigned int rotation,
5525 unsigned int supported_rotations)
5526{
5527 if (rotation & ~supported_rotations) {
5528 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
Joonas Lahtinen14152c82015-10-01 10:00:58 +03005529 rotation = (rotation & DRM_REFLECT_MASK) |
5530 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05305531 }
5532
5533 return rotation;
5534}
5535EXPORT_SYMBOL(drm_rotation_simplify);
5536
5537/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005538 * drm_mode_config_init - initialize DRM mode_configuration structure
5539 * @dev: DRM device
5540 *
5541 * Initialize @dev's mode_config structure, used for tracking the graphics
5542 * configuration of @dev.
5543 *
5544 * Since this initializes the modeset locks, no locking is possible. Which is no
5545 * problem, since this should happen single threaded at init time. It is the
5546 * driver's problem to ensure this guarantee.
5547 *
5548 */
5549void drm_mode_config_init(struct drm_device *dev)
5550{
5551 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05005552 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005553 mutex_init(&dev->mode_config.idr_mutex);
5554 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01005555 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005556 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5557 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5558 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5559 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5560 INIT_LIST_HEAD(&dev->mode_config.property_list);
5561 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5562 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5563 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005564 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005565 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005566
5567 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05005568 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005569 drm_modeset_unlock_all(dev);
5570
5571 /* Just to be sure */
5572 dev->mode_config.num_fb = 0;
5573 dev->mode_config.num_connector = 0;
5574 dev->mode_config.num_crtc = 0;
5575 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07005576 dev->mode_config.num_overlay_plane = 0;
5577 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005578}
5579EXPORT_SYMBOL(drm_mode_config_init);
5580
5581/**
5582 * drm_mode_config_cleanup - free up DRM mode_config info
5583 * @dev: DRM device
5584 *
5585 * Free up all the connectors and CRTCs associated with this DRM device, then
5586 * free up the framebuffers and associated buffer objects.
5587 *
5588 * Note that since this /should/ happen single-threaded at driver/device
5589 * teardown time, no locking is required. It's the driver's job to ensure that
5590 * this guarantee actually holds true.
5591 *
5592 * FIXME: cleanup any dangling user buffer objects too
5593 */
5594void drm_mode_config_cleanup(struct drm_device *dev)
5595{
5596 struct drm_connector *connector, *ot;
5597 struct drm_crtc *crtc, *ct;
5598 struct drm_encoder *encoder, *enct;
5599 struct drm_framebuffer *fb, *fbt;
5600 struct drm_property *property, *pt;
5601 struct drm_property_blob *blob, *bt;
5602 struct drm_plane *plane, *plt;
5603
5604 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5605 head) {
5606 encoder->funcs->destroy(encoder);
5607 }
5608
5609 list_for_each_entry_safe(connector, ot,
5610 &dev->mode_config.connector_list, head) {
5611 connector->funcs->destroy(connector);
5612 }
5613
5614 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5615 head) {
5616 drm_property_destroy(dev, property);
5617 }
5618
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01005619 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5620 head) {
5621 plane->funcs->destroy(plane);
5622 }
5623
5624 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5625 crtc->funcs->destroy(crtc);
5626 }
5627
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005628 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01005629 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01005630 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005631 }
5632
5633 /*
5634 * Single-threaded teardown context, so it's not required to grab the
5635 * fb_lock to protect against concurrent fb_list access. Contrary, it
5636 * would actually deadlock with the drm_framebuffer_cleanup function.
5637 *
5638 * Also, if there are any framebuffers left, that's a driver leak now,
5639 * so politely WARN about this.
5640 */
5641 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5642 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10005643 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005644 }
5645
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01005646 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005647 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005648 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05005649 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02005650}
5651EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05305652
5653struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5654 unsigned int supported_rotations)
5655{
5656 static const struct drm_prop_enum_list props[] = {
5657 { DRM_ROTATE_0, "rotate-0" },
5658 { DRM_ROTATE_90, "rotate-90" },
5659 { DRM_ROTATE_180, "rotate-180" },
5660 { DRM_ROTATE_270, "rotate-270" },
5661 { DRM_REFLECT_X, "reflect-x" },
5662 { DRM_REFLECT_Y, "reflect-y" },
5663 };
5664
5665 return drm_property_create_bitmask(dev, 0, "rotation",
5666 props, ARRAY_SIZE(props),
5667 supported_rotations);
5668}
5669EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005670
5671/**
5672 * DOC: Tile group
5673 *
5674 * Tile groups are used to represent tiled monitors with a unique
5675 * integer identifier. Tiled monitors using DisplayID v1.3 have
5676 * a unique 8-byte handle, we store this in a tile group, so we
5677 * have a common identifier for all tiles in a monitor group.
5678 */
5679static void drm_tile_group_free(struct kref *kref)
5680{
5681 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5682 struct drm_device *dev = tg->dev;
5683 mutex_lock(&dev->mode_config.idr_mutex);
5684 idr_remove(&dev->mode_config.tile_idr, tg->id);
5685 mutex_unlock(&dev->mode_config.idr_mutex);
5686 kfree(tg);
5687}
5688
5689/**
5690 * drm_mode_put_tile_group - drop a reference to a tile group.
5691 * @dev: DRM device
5692 * @tg: tile group to drop reference to.
5693 *
5694 * drop reference to tile group and free if 0.
5695 */
5696void drm_mode_put_tile_group(struct drm_device *dev,
5697 struct drm_tile_group *tg)
5698{
5699 kref_put(&tg->refcount, drm_tile_group_free);
5700}
5701
5702/**
5703 * drm_mode_get_tile_group - get a reference to an existing tile group
5704 * @dev: DRM device
5705 * @topology: 8-bytes unique per monitor.
5706 *
5707 * Use the unique bytes to get a reference to an existing tile group.
5708 *
5709 * RETURNS:
5710 * tile group or NULL if not found.
5711 */
5712struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5713 char topology[8])
5714{
5715 struct drm_tile_group *tg;
5716 int id;
5717 mutex_lock(&dev->mode_config.idr_mutex);
5718 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5719 if (!memcmp(tg->group_data, topology, 8)) {
5720 if (!kref_get_unless_zero(&tg->refcount))
5721 tg = NULL;
5722 mutex_unlock(&dev->mode_config.idr_mutex);
5723 return tg;
5724 }
5725 }
5726 mutex_unlock(&dev->mode_config.idr_mutex);
5727 return NULL;
5728}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005729EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005730
5731/**
5732 * drm_mode_create_tile_group - create a tile group from a displayid description
5733 * @dev: DRM device
5734 * @topology: 8-bytes unique per monitor.
5735 *
5736 * Create a tile group for the unique monitor, and get a unique
5737 * identifier for the tile group.
5738 *
5739 * RETURNS:
5740 * new tile group or error.
5741 */
5742struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5743 char topology[8])
5744{
5745 struct drm_tile_group *tg;
5746 int ret;
5747
5748 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5749 if (!tg)
5750 return ERR_PTR(-ENOMEM);
5751
5752 kref_init(&tg->refcount);
5753 memcpy(tg->group_data, topology, 8);
5754 tg->dev = dev;
5755
5756 mutex_lock(&dev->mode_config.idr_mutex);
5757 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5758 if (ret >= 0) {
5759 tg->id = ret;
5760 } else {
5761 kfree(tg);
5762 tg = ERR_PTR(ret);
5763 }
5764
5765 mutex_unlock(&dev->mode_config.idr_mutex);
5766 return tg;
5767}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005768EXPORT_SYMBOL(drm_mode_create_tile_group);
Jyri Sarhaf8ed34a2016-06-07 15:09:14 +03005769
5770/**
5771 * drm_crtc_enable_color_mgmt - enable color management properties
5772 * @crtc: DRM CRTC
5773 * @degamma_lut_size: the size of the degamma lut (before CSC)
5774 * @has_ctm: whether to attach ctm_property for CSC matrix
5775 * @gamma_lut_size: the size of the gamma lut (after CSC)
5776 *
5777 * This function lets the driver enable the color correction
5778 * properties on a CRTC. This includes 3 degamma, csc and gamma
5779 * properties that userspace can set and 2 size properties to inform
5780 * the userspace of the lut sizes. Each of the properties are
5781 * optional. The gamma and degamma properties are only attached if
5782 * their size is not 0 and ctm_property is only attached if has_ctm is
5783 * true.
5784 */
5785void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5786 uint degamma_lut_size,
5787 bool has_ctm,
5788 uint gamma_lut_size)
5789{
5790 struct drm_device *dev = crtc->dev;
5791 struct drm_mode_config *config = &dev->mode_config;
5792
5793 if (degamma_lut_size) {
5794 drm_object_attach_property(&crtc->base,
5795 config->degamma_lut_property, 0);
5796 drm_object_attach_property(&crtc->base,
5797 config->degamma_lut_size_property,
5798 degamma_lut_size);
5799 }
5800
5801 if (has_ctm)
5802 drm_object_attach_property(&crtc->base,
5803 config->ctm_property, 0);
5804
5805 if (gamma_lut_size) {
5806 drm_object_attach_property(&crtc->base,
5807 config->gamma_lut_property, 0);
5808 drm_object_attach_property(&crtc->base,
5809 config->gamma_lut_size_property,
5810 gamma_lut_size);
5811 }
5812}
5813EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);